You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FileMode.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
  3. * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.lib;
  45. import java.io.IOException;
  46. import java.io.OutputStream;
  47. /**
  48. * Constants describing various file modes recognized by GIT.
  49. * <p>
  50. * GIT uses a subset of the available UNIX file permission bits. The
  51. * <code>FileMode</code> class provides access to constants defining the modes
  52. * actually used by GIT.
  53. * </p>
  54. */
  55. public abstract class FileMode {
  56. /**
  57. * Mask to apply to a file mode to obtain its type bits.
  58. *
  59. * @see #TYPE_TREE
  60. * @see #TYPE_SYMLINK
  61. * @see #TYPE_FILE
  62. * @see #TYPE_GITLINK
  63. * @see #TYPE_MISSING
  64. */
  65. public static final int TYPE_MASK = 0170000;
  66. /** Bit pattern for {@link #TYPE_MASK} matching {@link #TREE}. */
  67. public static final int TYPE_TREE = 0040000;
  68. /** Bit pattern for {@link #TYPE_MASK} matching {@link #SYMLINK}. */
  69. public static final int TYPE_SYMLINK = 0120000;
  70. /** Bit pattern for {@link #TYPE_MASK} matching {@link #REGULAR_FILE}. */
  71. public static final int TYPE_FILE = 0100000;
  72. /** Bit pattern for {@link #TYPE_MASK} matching {@link #GITLINK}. */
  73. public static final int TYPE_GITLINK = 0160000;
  74. /** Bit pattern for {@link #TYPE_MASK} matching {@link #MISSING}. */
  75. public static final int TYPE_MISSING = 0000000;
  76. /**
  77. * Mode indicating an entry is a tree (aka directory).
  78. */
  79. public static final FileMode TREE = new FileMode(TYPE_TREE,
  80. Constants.OBJ_TREE) {
  81. @Override
  82. public boolean equals(int modeBits) {
  83. return (modeBits & TYPE_MASK) == TYPE_TREE;
  84. }
  85. };
  86. /** Mode indicating an entry is a symbolic link. */
  87. public static final FileMode SYMLINK = new FileMode(TYPE_SYMLINK,
  88. Constants.OBJ_BLOB) {
  89. @Override
  90. public boolean equals(int modeBits) {
  91. return (modeBits & TYPE_MASK) == TYPE_SYMLINK;
  92. }
  93. };
  94. /** Mode indicating an entry is a non-executable file. */
  95. public static final FileMode REGULAR_FILE = new FileMode(0100644,
  96. Constants.OBJ_BLOB) {
  97. @Override
  98. public boolean equals(int modeBits) {
  99. return (modeBits & TYPE_MASK) == TYPE_FILE && (modeBits & 0111) == 0;
  100. }
  101. };
  102. /** Mode indicating an entry is an executable file. */
  103. public static final FileMode EXECUTABLE_FILE = new FileMode(0100755,
  104. Constants.OBJ_BLOB) {
  105. @Override
  106. public boolean equals(int modeBits) {
  107. return (modeBits & TYPE_MASK) == TYPE_FILE && (modeBits & 0111) != 0;
  108. }
  109. };
  110. /** Mode indicating an entry is a submodule commit in another repository. */
  111. public static final FileMode GITLINK = new FileMode(TYPE_GITLINK,
  112. Constants.OBJ_COMMIT) {
  113. @Override
  114. public boolean equals(int modeBits) {
  115. return (modeBits & TYPE_MASK) == TYPE_GITLINK;
  116. }
  117. };
  118. /** Mode indicating an entry is missing during parallel walks. */
  119. public static final FileMode MISSING = new FileMode(TYPE_MISSING,
  120. Constants.OBJ_BAD) {
  121. @Override
  122. public boolean equals(int modeBits) {
  123. return modeBits == 0;
  124. }
  125. };
  126. /**
  127. * Convert a set of mode bits into a FileMode enumerated value.
  128. *
  129. * @param bits
  130. * the mode bits the caller has somehow obtained.
  131. * @return the FileMode instance that represents the given bits.
  132. */
  133. public static final FileMode fromBits(int bits) {
  134. switch (bits & TYPE_MASK) {
  135. case TYPE_MISSING:
  136. if (bits == 0)
  137. return MISSING;
  138. break;
  139. case TYPE_TREE:
  140. return TREE;
  141. case TYPE_FILE:
  142. if ((bits & 0111) != 0)
  143. return EXECUTABLE_FILE;
  144. return REGULAR_FILE;
  145. case TYPE_SYMLINK:
  146. return SYMLINK;
  147. case TYPE_GITLINK:
  148. return GITLINK;
  149. }
  150. return new FileMode(bits, Constants.OBJ_BAD) {
  151. @Override
  152. public boolean equals(int a) {
  153. return bits == a;
  154. }
  155. };
  156. }
  157. private final byte[] octalBytes;
  158. private final int modeBits;
  159. private final int objectType;
  160. private FileMode(int mode, int expType) {
  161. modeBits = mode;
  162. objectType = expType;
  163. if (mode != 0) {
  164. final byte[] tmp = new byte[10];
  165. int p = tmp.length;
  166. while (mode != 0) {
  167. tmp[--p] = (byte) ('0' + (mode & 07));
  168. mode >>= 3;
  169. }
  170. octalBytes = new byte[tmp.length - p];
  171. for (int k = 0; k < octalBytes.length; k++) {
  172. octalBytes[k] = tmp[p + k];
  173. }
  174. } else {
  175. octalBytes = new byte[] { '0' };
  176. }
  177. }
  178. /**
  179. * Test a file mode for equality with this
  180. * {@link org.eclipse.jgit.lib.FileMode} object.
  181. *
  182. * @param modebits
  183. * a int.
  184. * @return true if the mode bits represent the same mode as this object
  185. */
  186. public abstract boolean equals(int modebits);
  187. /**
  188. * Copy this mode as a sequence of octal US-ASCII bytes.
  189. * <p>
  190. * The mode is copied as a sequence of octal digits using the US-ASCII
  191. * character encoding. The sequence does not use a leading '0' prefix to
  192. * indicate octal notation. This method is suitable for generation of a mode
  193. * string within a GIT tree object.
  194. * </p>
  195. *
  196. * @param os
  197. * stream to copy the mode to.
  198. * @throws java.io.IOException
  199. * the stream encountered an error during the copy.
  200. */
  201. public void copyTo(OutputStream os) throws IOException {
  202. os.write(octalBytes);
  203. }
  204. /**
  205. * Copy this mode as a sequence of octal US-ASCII bytes.
  206. *
  207. * The mode is copied as a sequence of octal digits using the US-ASCII
  208. * character encoding. The sequence does not use a leading '0' prefix to
  209. * indicate octal notation. This method is suitable for generation of a mode
  210. * string within a GIT tree object.
  211. *
  212. * @param buf
  213. * buffer to copy the mode to.
  214. * @param ptr
  215. * position within {@code buf} for first digit.
  216. */
  217. public void copyTo(byte[] buf, int ptr) {
  218. System.arraycopy(octalBytes, 0, buf, ptr, octalBytes.length);
  219. }
  220. /**
  221. * Copy the number of bytes written by {@link #copyTo(OutputStream)}.
  222. *
  223. * @return the number of bytes written by {@link #copyTo(OutputStream)}.
  224. */
  225. public int copyToLength() {
  226. return octalBytes.length;
  227. }
  228. /**
  229. * Get the object type that should appear for this type of mode.
  230. * <p>
  231. * See the object type constants in {@link org.eclipse.jgit.lib.Constants}.
  232. *
  233. * @return one of the well known object type constants.
  234. */
  235. public int getObjectType() {
  236. return objectType;
  237. }
  238. /**
  239. * {@inheritDoc}
  240. * <p>
  241. * Format this mode as an octal string (for debugging only).
  242. */
  243. @Override
  244. public String toString() {
  245. return Integer.toOctalString(modeBits);
  246. }
  247. /**
  248. * Get the mode bits as an integer.
  249. *
  250. * @return The mode bits as an integer.
  251. */
  252. public int getBits() {
  253. return modeBits;
  254. }
  255. }