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 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. /** Mode indicating an entry is a {@link Tree}. */
  77. @SuppressWarnings("synthetic-access")
  78. public static final FileMode TREE = new FileMode(TYPE_TREE,
  79. Constants.OBJ_TREE) {
  80. public boolean equals(final int modeBits) {
  81. return (modeBits & TYPE_MASK) == TYPE_TREE;
  82. }
  83. };
  84. /** Mode indicating an entry is a {@link SymlinkTreeEntry}. */
  85. @SuppressWarnings("synthetic-access")
  86. public static final FileMode SYMLINK = new FileMode(TYPE_SYMLINK,
  87. Constants.OBJ_BLOB) {
  88. public boolean equals(final int modeBits) {
  89. return (modeBits & TYPE_MASK) == TYPE_SYMLINK;
  90. }
  91. };
  92. /** Mode indicating an entry is a non-executable {@link FileTreeEntry}. */
  93. @SuppressWarnings("synthetic-access")
  94. public static final FileMode REGULAR_FILE = new FileMode(0100644,
  95. Constants.OBJ_BLOB) {
  96. public boolean equals(final int modeBits) {
  97. return (modeBits & TYPE_MASK) == TYPE_FILE && (modeBits & 0111) == 0;
  98. }
  99. };
  100. /** Mode indicating an entry is an executable {@link FileTreeEntry}. */
  101. @SuppressWarnings("synthetic-access")
  102. public static final FileMode EXECUTABLE_FILE = new FileMode(0100755,
  103. Constants.OBJ_BLOB) {
  104. public boolean equals(final int modeBits) {
  105. return (modeBits & TYPE_MASK) == TYPE_FILE && (modeBits & 0111) != 0;
  106. }
  107. };
  108. /** Mode indicating an entry is a submodule commit in another repository. */
  109. @SuppressWarnings("synthetic-access")
  110. public static final FileMode GITLINK = new FileMode(TYPE_GITLINK,
  111. Constants.OBJ_COMMIT) {
  112. public boolean equals(final int modeBits) {
  113. return (modeBits & TYPE_MASK) == TYPE_GITLINK;
  114. }
  115. };
  116. /** Mode indicating an entry is missing during parallel walks. */
  117. @SuppressWarnings("synthetic-access")
  118. public static final FileMode MISSING = new FileMode(TYPE_MISSING,
  119. Constants.OBJ_BAD) {
  120. public boolean equals(final int modeBits) {
  121. return modeBits == 0;
  122. }
  123. };
  124. /**
  125. * Convert a set of mode bits into a FileMode enumerated value.
  126. *
  127. * @param bits
  128. * the mode bits the caller has somehow obtained.
  129. * @return the FileMode instance that represents the given bits.
  130. */
  131. public static final FileMode fromBits(final int bits) {
  132. switch (bits & TYPE_MASK) {
  133. case TYPE_MISSING:
  134. if (bits == 0)
  135. return MISSING;
  136. break;
  137. case TYPE_TREE:
  138. return TREE;
  139. case TYPE_FILE:
  140. if ((bits & 0111) != 0)
  141. return EXECUTABLE_FILE;
  142. return REGULAR_FILE;
  143. case TYPE_SYMLINK:
  144. return SYMLINK;
  145. case TYPE_GITLINK:
  146. return GITLINK;
  147. }
  148. return new FileMode(bits, Constants.OBJ_BAD) {
  149. @Override
  150. public boolean equals(final int a) {
  151. return bits == a;
  152. }
  153. };
  154. }
  155. private final byte[] octalBytes;
  156. private final int modeBits;
  157. private final int objectType;
  158. private FileMode(int mode, final int expType) {
  159. modeBits = mode;
  160. objectType = expType;
  161. if (mode != 0) {
  162. final byte[] tmp = new byte[10];
  163. int p = tmp.length;
  164. while (mode != 0) {
  165. tmp[--p] = (byte) ('0' + (mode & 07));
  166. mode >>= 3;
  167. }
  168. octalBytes = new byte[tmp.length - p];
  169. for (int k = 0; k < octalBytes.length; k++) {
  170. octalBytes[k] = tmp[p + k];
  171. }
  172. } else {
  173. octalBytes = new byte[] { '0' };
  174. }
  175. }
  176. /**
  177. * Test a file mode for equality with this {@link FileMode} object.
  178. *
  179. * @param modebits
  180. * @return true if the mode bits represent the same mode as this object
  181. */
  182. public abstract boolean equals(final int modebits);
  183. /**
  184. * Copy this mode as a sequence of octal US-ASCII bytes.
  185. * <p>
  186. * The mode is copied as a sequence of octal digits using the US-ASCII
  187. * character encoding. The sequence does not use a leading '0' prefix to
  188. * indicate octal notation. This method is suitable for generation of a mode
  189. * string within a GIT tree object.
  190. * </p>
  191. *
  192. * @param os
  193. * stream to copy the mode to.
  194. * @throws IOException
  195. * the stream encountered an error during the copy.
  196. */
  197. public void copyTo(final OutputStream os) throws IOException {
  198. os.write(octalBytes);
  199. }
  200. /**
  201. * @return the number of bytes written by {@link #copyTo(OutputStream)}.
  202. */
  203. public int copyToLength() {
  204. return octalBytes.length;
  205. }
  206. /**
  207. * Get the object type that should appear for this type of mode.
  208. * <p>
  209. * See the object type constants in {@link Constants}.
  210. *
  211. * @return one of the well known object type constants.
  212. */
  213. public int getObjectType() {
  214. return objectType;
  215. }
  216. /** Format this mode as an octal string (for debugging only). */
  217. public String toString() {
  218. return Integer.toOctalString(modeBits);
  219. }
  220. /**
  221. * @return The mode bits as an integer.
  222. */
  223. public int getBits() {
  224. return modeBits;
  225. }
  226. }