Add writeTree support to DirCache
[egit/charleso.git] / org.spearce.jgit / src / org / spearce / jgit / lib / FileMode.java
blobcf42f37eacde6e3d6419c323ea5856d8919e5a12
1 /*
2 * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
9 * conditions are met:
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
19 * - Neither the name of the Git Development Community nor the
20 * names of its contributors may be used to endorse or promote
21 * products derived from this software without specific prior
22 * written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
25 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
26 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 package org.spearce.jgit.lib;
41 import java.io.IOException;
42 import java.io.OutputStream;
44 /**
45 * Constants describing various file modes recognized by GIT.
46 * <p>
47 * GIT uses a subset of the available UNIX file permission bits. The
48 * <code>FileMode</code> class provides access to constants defining the modes
49 * actually used by GIT.
50 * </p>
52 public abstract class FileMode {
53 /** Mode indicating an entry is a {@link Tree}. */
54 @SuppressWarnings("synthetic-access")
55 public static final FileMode TREE = new FileMode(0040000,
56 Constants.OBJ_TREE) {
57 public boolean equals(final int modeBits) {
58 return (modeBits & 0170000) == 0040000;
62 /** Mode indicating an entry is a {@link SymlinkTreeEntry}. */
63 @SuppressWarnings("synthetic-access")
64 public static final FileMode SYMLINK = new FileMode(0120000,
65 Constants.OBJ_BLOB) {
66 public boolean equals(final int modeBits) {
67 return (modeBits & 0170000) == 0120000;
71 /** Mode indicating an entry is a non-executable {@link FileTreeEntry}. */
72 @SuppressWarnings("synthetic-access")
73 public static final FileMode REGULAR_FILE = new FileMode(0100644,
74 Constants.OBJ_BLOB) {
75 public boolean equals(final int modeBits) {
76 return (modeBits & 0170000) == 0100000 && (modeBits & 0111) == 0;
80 /** Mode indicating an entry is an executable {@link FileTreeEntry}. */
81 @SuppressWarnings("synthetic-access")
82 public static final FileMode EXECUTABLE_FILE = new FileMode(0100755,
83 Constants.OBJ_BLOB) {
84 public boolean equals(final int modeBits) {
85 return (modeBits & 0170000) == 0100000 && (modeBits & 0111) != 0;
89 /** Mode indicating an entry is a submodule commit in another repository. */
90 @SuppressWarnings("synthetic-access")
91 public static final FileMode GITLINK = new FileMode(0160000,
92 Constants.OBJ_COMMIT) {
93 public boolean equals(final int modeBits) {
94 return (modeBits & 0170000) == 0160000;
98 /** Mode indicating an entry is missing during parallel walks. */
99 @SuppressWarnings("synthetic-access")
100 public static final FileMode MISSING = new FileMode(0000000,
101 Constants.OBJ_BAD) {
102 public boolean equals(final int modeBits) {
103 return modeBits == 0;
108 * Convert a set of mode bits into a FileMode enumerated value.
110 * @param bits
111 * the mode bits the caller has somehow obtained.
112 * @return the FileMode instance that represents the given bits.
114 public static final FileMode fromBits(final int bits) {
115 switch (bits & 0170000) {
116 case 0000000:
117 if (bits == 0)
118 return MISSING;
119 break;
120 case 0040000:
121 return TREE;
122 case 0100000:
123 if ((bits & 0111) != 0)
124 return EXECUTABLE_FILE;
125 return REGULAR_FILE;
126 case 0120000:
127 return SYMLINK;
128 case 0160000:
129 return GITLINK;
132 return new FileMode(bits, Constants.OBJ_BAD) {
133 @Override
134 public boolean equals(final int a) {
135 return bits == a;
140 private final byte[] octalBytes;
142 private final int modeBits;
144 private final int objectType;
146 private FileMode(int mode, final int expType) {
147 modeBits = mode;
148 objectType = expType;
149 if (mode != 0) {
150 final byte[] tmp = new byte[10];
151 int p = tmp.length;
153 while (mode != 0) {
154 tmp[--p] = (byte) ('0' + (mode & 07));
155 mode >>= 3;
158 octalBytes = new byte[tmp.length - p];
159 for (int k = 0; k < octalBytes.length; k++) {
160 octalBytes[k] = tmp[p + k];
162 } else {
163 octalBytes = new byte[] { '0' };
168 * Test a file mode for equality with this {@link FileMode} object.
170 * @param modebits
171 * @return true if the mode bits represent the same mode as this object
173 public abstract boolean equals(final int modebits);
176 * Copy this mode as a sequence of octal US-ASCII bytes.
177 * <p>
178 * The mode is copied as a sequence of octal digits using the US-ASCII
179 * character encoding. The sequence does not use a leading '0' prefix to
180 * indicate octal notation. This method is suitable for generation of a mode
181 * string within a GIT tree object.
182 * </p>
184 * @param os
185 * stream to copy the mode to.
186 * @throws IOException
187 * the stream encountered an error during the copy.
189 public void copyTo(final OutputStream os) throws IOException {
190 os.write(octalBytes);
194 * @return the number of bytes written by {@link #copyTo(OutputStream)}.
196 public int copyToLength() {
197 return octalBytes.length;
201 * Get the object type that should appear for this type of mode.
202 * <p>
203 * See the object type constants in {@link Constants}.
205 * @return one of the well known object type constants.
207 public int getObjectType() {
208 return objectType;
211 /** Format this mode as an octal string (for debugging only). */
212 public String toString() {
213 return Integer.toOctalString(modeBits);
217 * @return The mode bits as an integer.
219 public int getBits() {
220 return modeBits;