Allow application code to set ObjectIds in DirCacheEntry
[egit.git] / org.spearce.jgit / src / org / spearce / jgit / dircache / DirCacheEntry.java
blobb23d96c8446d3af7c1972a94f406cc98d32a7b38
1 /*
2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
21 * written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org.spearce.jgit.dircache;
40 import java.io.ByteArrayOutputStream;
41 import java.io.EOFException;
42 import java.io.IOException;
43 import java.io.InputStream;
44 import java.io.OutputStream;
45 import java.nio.ByteBuffer;
46 import java.util.Arrays;
48 import org.spearce.jgit.lib.AnyObjectId;
49 import org.spearce.jgit.lib.Constants;
50 import org.spearce.jgit.lib.FileMode;
51 import org.spearce.jgit.lib.ObjectId;
52 import org.spearce.jgit.util.NB;
54 /**
55 * A single file (or stage of a file) in a {@link DirCache}.
56 * <p>
57 * An entry represents exactly one stage of a file. If a file path is unmerged
58 * then multiple DirCacheEntry instances may appear for the same path name.
60 public class DirCacheEntry {
61 private static final byte[] nullpad = new byte[8];
63 // private static final int P_CTIME = 0;
65 // private static final int P_CTIME_NSEC = 4;
67 private static final int P_MTIME = 8;
69 // private static final int P_MTIME_NSEC = 12;
71 // private static final int P_DEV = 16;
73 // private static final int P_INO = 20;
75 private static final int P_MODE = 24;
77 // private static final int P_UID = 28;
79 // private static final int P_GID = 32;
81 private static final int P_SIZE = 36;
83 private static final int P_OBJECTID = 40;
85 private static final int P_FLAGS = 60;
87 /** Mask applied to data in {@link #P_FLAGS} to get the name length. */
88 private static final int NAME_MASK = 0xfff;
90 static final int INFO_LEN = 62;
92 private static final int ASSUME_VALID = 0x80;
94 /** (Possibly shared) header information storage. */
95 private final byte[] info;
97 /** First location within {@link #info} where our header starts. */
98 private final int infoOffset;
100 /** Our encoded path name, from the root of the repository. */
101 final byte[] path;
103 DirCacheEntry(final byte[] sharedInfo, final int infoAt,
104 final InputStream in) throws IOException {
105 info = sharedInfo;
106 infoOffset = infoAt;
108 NB.readFully(in, info, infoOffset, INFO_LEN);
110 int pathLen = NB.decodeUInt16(info, infoOffset + P_FLAGS) & NAME_MASK;
111 int skipped = 0;
112 if (pathLen < NAME_MASK) {
113 path = new byte[pathLen];
114 NB.readFully(in, path, 0, pathLen);
115 } else {
116 final ByteArrayOutputStream tmp = new ByteArrayOutputStream();
118 final byte[] buf = new byte[NAME_MASK];
119 NB.readFully(in, buf, 0, NAME_MASK);
120 tmp.write(buf);
122 for (;;) {
123 final int c = in.read();
124 if (c < 0)
125 throw new EOFException("Short read of block.");
126 if (c == 0)
127 break;
128 tmp.write(c);
130 path = tmp.toByteArray();
131 pathLen = path.length;
132 skipped = 1; // we already skipped 1 '\0' above to break the loop.
135 // Index records are padded out to the next 8 byte alignment
136 // for historical reasons related to how C Git read the files.
138 final int actLen = INFO_LEN + pathLen;
139 final int expLen = (actLen + 8) & ~7;
140 NB.skipFully(in, expLen - actLen - skipped);
144 * Create an empty entry.
146 * @param newPath
147 * name of the cache entry.
149 public DirCacheEntry(final String newPath) {
150 this(Constants.encode(newPath));
154 * Create an empty entry.
156 * @param newPath
157 * name of the cache entry, in the standard encoding.
159 public DirCacheEntry(final byte[] newPath) {
160 info = new byte[INFO_LEN];
161 infoOffset = 0;
163 path = newPath;
164 if (path.length < NAME_MASK)
165 NB.encodeInt16(info, infoOffset + P_FLAGS, path.length);
166 else
167 NB.encodeInt16(info, infoOffset + P_FLAGS, NAME_MASK);
170 void write(final OutputStream os) throws IOException {
171 final int pathLen = path.length;
172 os.write(info, infoOffset, INFO_LEN);
173 os.write(path, 0, pathLen);
175 // Index records are padded out to the next 8 byte alignment
176 // for historical reasons related to how C Git read the files.
178 final int actLen = INFO_LEN + pathLen;
179 final int expLen = (actLen + 8) & ~7;
180 if (actLen != expLen)
181 os.write(nullpad, 0, expLen - actLen);
185 * Is it possible for this entry to be accidently assumed clean?
186 * <p>
187 * The "racy git" problem happens when a work file can be updated faster
188 * than the filesystem records file modification timestamps. It is possible
189 * for an application to edit a work file, update the index, then edit it
190 * again before the filesystem will give the work file a new modification
191 * timestamp. This method tests to see if file was written out at the same
192 * time as the index.
194 * @param smudge_s
195 * seconds component of the index's last modified time.
196 * @param smudge_ns
197 * nanoseconds component of the index's last modified time.
198 * @return true if extra careful checks should be used.
200 final boolean mightBeRacilyClean(final int smudge_s, final int smudge_ns) {
201 // If the index has a modification time then it came from disk
202 // and was not generated from scratch in memory. In such cases
203 // the entry is 'racily clean' if the entry's cached modification
204 // time is equal to or later than the index modification time. In
205 // such cases the work file is too close to the index to tell if
206 // it is clean or not based on the modification time alone.
208 final int base = infoOffset + P_MTIME;
209 final int mtime = NB.decodeInt32(info, base);
210 if (smudge_s < mtime)
211 return true;
212 if (smudge_s == mtime)
213 return smudge_ns <= NB.decodeInt32(info, base + 4) / 1000000;
214 return false;
218 * Force this entry to no longer match its working tree file.
219 * <p>
220 * This avoids the "racy git" problem by making this index entry no longer
221 * match the file in the working directory. Later git will be forced to
222 * compare the file content to ensure the file matches the working tree.
224 final void smudgeRacilyClean() {
225 // We don't use the same approach as C Git to smudge the entry,
226 // as we cannot compare the working tree file to our SHA-1 and
227 // thus cannot use the "size to 0" trick without accidentally
228 // thinking a zero length file is clean.
230 // Instead we force the mtime to the largest possible value, so
231 // it is certainly after the index's own modification time and
232 // on a future read will cause mightBeRacilyClean to say "yes!".
233 // It is also unlikely to match with the working tree file.
235 // I'll see you again before Jan 19, 2038, 03:14:07 AM GMT.
237 final int base = infoOffset + P_MTIME;
238 Arrays.fill(info, base, base + 8, (byte) 127);
241 final byte[] idBuffer() {
242 return info;
245 final int idOffset() {
246 return infoOffset + P_OBJECTID;
250 * Is this entry always thought to be unmodified?
251 * <p>
252 * Most entries in the index do not have this flag set. Users may however
253 * set them on if the file system stat() costs are too high on this working
254 * directory, such as on NFS or SMB volumes.
256 * @return true if we must assume the entry is unmodified.
258 public boolean isAssumeValid() {
259 return (info[infoOffset + P_FLAGS] & ASSUME_VALID) != 0;
263 * Set the assume valid flag for this entry,
265 * @param assume
266 * true to ignore apparent modifications; false to look at last
267 * modified to detect file modifications.
269 public void setAssumeValid(final boolean assume) {
270 if (assume)
271 info[infoOffset + P_FLAGS] |= ASSUME_VALID;
272 else
273 info[infoOffset + P_FLAGS] &= ~ASSUME_VALID;
277 * Get the stage of this entry.
278 * <p>
279 * Entries have one of 4 possible stages: 0-3.
281 * @return the stage of this entry.
283 public int getStage() {
284 return (info[infoOffset + P_FLAGS] >>> 4) & 0x3;
288 * Obtain the raw {@link FileMode} bits for this entry.
290 * @return mode bits for the entry.
291 * @see FileMode#fromBits(int)
293 public int getRawMode() {
294 return NB.decodeInt32(info, infoOffset + P_MODE);
298 * Set the file mode for this entry.
300 * @param mode
301 * the new mode constant.
303 public void setFileMode(final FileMode mode) {
304 NB.encodeInt32(info, infoOffset + P_MODE, mode.getBits());
308 * Get the cached last modification date of this file, in milliseconds.
309 * <p>
310 * One of the indicators that the file has been modified by an application
311 * changing the working tree is if the last modification time for the file
312 * differs from the time stored in this entry.
314 * @return last modification time of this file, in milliseconds since the
315 * Java epoch (midnight Jan 1, 1970 UTC).
317 public long getLastModified() {
318 return decodeTS(P_MTIME);
322 * Set the cached last modification date of this file, using milliseconds.
324 * @param when
325 * new cached modification date of the file, in milliseconds.
327 public void setLastModified(final long when) {
328 encodeTS(P_MTIME, when);
332 * Get the cached size (in bytes) of this file.
333 * <p>
334 * One of the indicators that the file has been modified by an application
335 * changing the working tree is if the size of the file (in bytes) differs
336 * from the size stored in this entry.
337 * <p>
338 * Note that this is the length of the file in the working directory, which
339 * may differ from the size of the decompressed blob if work tree filters
340 * are being used, such as LF<->CRLF conversion.
342 * @return cached size of the working directory file, in bytes.
344 public int getLength() {
345 return NB.decodeInt32(info, infoOffset + P_SIZE);
349 * Set the cached size (in bytes) of this file.
351 * @param sz
352 * new cached size of the file, as bytes.
354 public void setLength(final int sz) {
355 NB.encodeInt32(info, infoOffset + P_SIZE, sz);
359 * Obtain the ObjectId for the entry.
360 * <p>
361 * Using this method to compare ObjectId values between entries is
362 * inefficient as it causes memory allocation.
364 * @return object identifier for the entry.
366 public ObjectId getObjectId() {
367 return ObjectId.fromRaw(idBuffer(), idOffset());
371 * Set the ObjectId for the entry.
373 * @param id
374 * new object identifier for the entry. May be
375 * {@link ObjectId#zeroId()} to remove the current identifier.
377 public void setObjectId(final AnyObjectId id) {
378 id.copyRawTo(idBuffer(), idOffset());
382 * Set the ObjectId for the entry from the raw binary representation.
384 * @param bs
385 * the raw byte buffer to read from. At least 20 bytes after p
386 * must be available within this byte array.
387 * @param p
388 * position to read the first byte of data from.
390 public void setObjectIdFromRaw(final byte[] bs, final int p) {
391 final int n = Constants.OBJECT_ID_LENGTH;
392 System.arraycopy(bs, p, idBuffer(), idOffset(), n);
396 * Get the entry's complete path.
397 * <p>
398 * This method is not very efficient and is primarily meant for debugging
399 * and final output generation. Applications should try to avoid calling it,
400 * and if invoked do so only once per interesting entry, where the name is
401 * absolutely required for correct function.
403 * @return complete path of the entry, from the root of the repository. If
404 * the entry is in a subtree there will be at least one '/' in the
405 * returned string.
407 public String getPathString() {
408 return Constants.CHARSET.decode(ByteBuffer.wrap(path)).toString();
412 * Copy the ObjectId and other meta fields from an existing entry.
413 * <p>
414 * This method copies everything except the path from one entry to another,
415 * supporting renaming.
417 * @param src
418 * the entry to copy ObjectId and meta fields from.
420 public void copyMetaData(final DirCacheEntry src) {
421 final int pLen = NB.decodeUInt16(info, infoOffset + P_FLAGS) & NAME_MASK;
422 System.arraycopy(src.info, src.infoOffset, info, infoOffset, INFO_LEN);
423 NB.encodeInt16(info, infoOffset + P_FLAGS, pLen
424 | NB.decodeUInt16(info, infoOffset + P_FLAGS) & ~NAME_MASK);
427 private long decodeTS(final int pIdx) {
428 final int base = infoOffset + pIdx;
429 final int sec = NB.decodeInt32(info, base);
430 final int ms = NB.decodeInt32(info, base + 4) / 1000000;
431 return 1000L * sec + ms;
434 private void encodeTS(final int pIdx, final long when) {
435 final int base = infoOffset + pIdx;
436 NB.encodeInt32(info, base, (int) (when / 1000));
437 NB.encodeInt32(info, base + 4, ((int) (when % 1000)) * 1000000);