2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
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
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
.IOException
;
41 import java
.io
.InputStream
;
42 import java
.io
.OutputStream
;
43 import java
.nio
.ByteBuffer
;
44 import java
.util
.Arrays
;
46 import org
.spearce
.jgit
.lib
.Constants
;
47 import org
.spearce
.jgit
.lib
.FileMode
;
48 import org
.spearce
.jgit
.lib
.ObjectId
;
49 import org
.spearce
.jgit
.util
.NB
;
52 * A single file (or stage of a file) in a {@link DirCache}.
54 * An entry represents exactly one stage of a file. If a file path is unmerged
55 * then multiple DirCacheEntry instances may appear for the same path name.
57 public class DirCacheEntry
{
58 private static final byte[] nullpad
= new byte[8];
60 // private static final int P_CTIME = 0;
62 // private static final int P_CTIME_NSEC = 4;
64 private static final int P_MTIME
= 8;
66 // private static final int P_MTIME_NSEC = 12;
68 // private static final int P_DEV = 16;
70 // private static final int P_INO = 20;
72 private static final int P_MODE
= 24;
74 // private static final int P_UID = 28;
76 // private static final int P_GID = 32;
78 private static final int P_SIZE
= 36;
80 private static final int P_OBJECTID
= 40;
82 private static final int P_FLAGS
= 60;
84 /** Mask applied to data in {@link #P_FLAGS} to get the name length. */
85 private static final int NAME_MASK
= 0xfff;
87 static final int INFO_LEN
= 62;
89 private static final int ASSUME_VALID
= 0x80;
91 /** (Possibly shared) header information storage. */
92 private final byte[] info
;
94 /** First location within {@link #info} where our header starts. */
95 private final int infoOffset
;
97 /** Our encoded path name, from the root of the repository. */
100 DirCacheEntry(final byte[] sharedInfo
, final int infoAt
,
101 final InputStream in
) throws IOException
{
105 NB
.readFully(in
, info
, infoOffset
, INFO_LEN
);
107 int pathLen
= NB
.decodeUInt16(info
, infoOffset
+ P_FLAGS
) & NAME_MASK
;
108 if (pathLen
== NAME_MASK
)
109 throw new IOException("Path name too long for jgit");
110 path
= new byte[pathLen
];
111 NB
.readFully(in
, path
, 0, pathLen
);
113 // Index records are padded out to the next 8 byte alignment
114 // for historical reasons related to how C Git read the files.
116 final int actLen
= INFO_LEN
+ pathLen
;
117 final int expLen
= (actLen
+ 8) & ~
7;
118 if (actLen
!= expLen
)
119 NB
.skipFully(in
, expLen
- actLen
);
123 * Create an empty entry.
126 * name of the cache entry.
128 public DirCacheEntry(final String newPath
) {
129 this(Constants
.encode(newPath
));
133 * Create an empty entry.
136 * name of the cache entry, in the standard encoding.
138 public DirCacheEntry(final byte[] newPath
) {
139 info
= new byte[INFO_LEN
];
143 if (path
.length
>= NAME_MASK
)
144 throw new IllegalArgumentException("Path name too long for jgit");
145 NB
.encodeInt16(info
, infoOffset
+ P_FLAGS
, path
.length
);
148 void write(final OutputStream os
) throws IOException
{
149 final int pathLen
= path
.length
;
150 os
.write(info
, infoOffset
, INFO_LEN
);
151 os
.write(path
, 0, pathLen
);
153 // Index records are padded out to the next 8 byte alignment
154 // for historical reasons related to how C Git read the files.
156 final int actLen
= INFO_LEN
+ pathLen
;
157 final int expLen
= (actLen
+ 8) & ~
7;
158 if (actLen
!= expLen
)
159 os
.write(nullpad
, 0, expLen
- actLen
);
163 * Is it possible for this entry to be accidently assumed clean?
165 * The "racy git" problem happens when a work file can be updated faster
166 * than the filesystem records file modification timestamps. It is possible
167 * for an application to edit a work file, update the index, then edit it
168 * again before the filesystem will give the work file a new modification
169 * timestamp. This method tests to see if file was written out at the same
173 * seconds component of the index's last modified time.
175 * nanoseconds component of the index's last modified time.
176 * @return true if extra careful checks should be used.
178 final boolean mightBeRacilyClean(final int smudge_s
, final int smudge_ns
) {
179 // If the index has a modification time then it came from disk
180 // and was not generated from scratch in memory. In such cases
181 // the entry is 'racily clean' if the entry's cached modification
182 // time is equal to or later than the index modification time. In
183 // such cases the work file is too close to the index to tell if
184 // it is clean or not based on the modification time alone.
186 final int base
= infoOffset
+ P_MTIME
;
187 final int mtime
= NB
.decodeInt32(info
, base
);
188 if (smudge_s
< mtime
)
190 if (smudge_s
== mtime
)
191 return smudge_ns
<= NB
.decodeInt32(info
, base
+ 4) / 1000000;
196 * Force this entry to no longer match its working tree file.
198 * This avoids the "racy git" problem by making this index entry no longer
199 * match the file in the working directory. Later git will be forced to
200 * compare the file content to ensure the file matches the working tree.
202 final void smudgeRacilyClean() {
203 // We don't use the same approach as C Git to smudge the entry,
204 // as we cannot compare the working tree file to our SHA-1 and
205 // thus cannot use the "size to 0" trick without accidentally
206 // thinking a zero length file is clean.
208 // Instead we force the mtime to the largest possible value, so
209 // it is certainly after the index's own modification time and
210 // on a future read will cause mightBeRacilyClean to say "yes!".
211 // It is also unlikely to match with the working tree file.
213 // I'll see you again before Jan 19, 2038, 03:14:07 AM GMT.
215 final int base
= infoOffset
+ P_MTIME
;
216 Arrays
.fill(info
, base
, base
+ 8, (byte) 127);
219 final byte[] idBuffer() {
223 final int idOffset() {
224 return infoOffset
+ P_OBJECTID
;
228 * Is this entry always thought to be unmodified?
230 * Most entries in the index do not have this flag set. Users may however
231 * set them on if the file system stat() costs are too high on this working
232 * directory, such as on NFS or SMB volumes.
234 * @return true if we must assume the entry is unmodified.
236 public boolean isAssumeValid() {
237 return (info
[infoOffset
+ P_FLAGS
] & ASSUME_VALID
) != 0;
241 * Set the assume valid flag for this entry,
244 * true to ignore apparent modifications; false to look at last
245 * modified to detect file modifications.
247 public void setAssumeValid(final boolean assume
) {
249 info
[infoOffset
+ P_FLAGS
] |= ASSUME_VALID
;
251 info
[infoOffset
+ P_FLAGS
] &= ~ASSUME_VALID
;
255 * Get the stage of this entry.
257 * Entries have one of 4 possible stages: 0-3.
259 * @return the stage of this entry.
261 public int getStage() {
262 return (info
[infoOffset
+ P_FLAGS
] >>> 4) & 0x3;
266 * Obtain the raw {@link FileMode} bits for this entry.
268 * @return mode bits for the entry.
269 * @see FileMode#fromBits(int)
271 public int getRawMode() {
272 return NB
.decodeInt32(info
, infoOffset
+ P_MODE
);
276 * Set the file mode for this entry.
279 * the new mode constant.
281 public void setFileMode(final FileMode mode
) {
282 NB
.encodeInt32(info
, infoOffset
+ P_MODE
, mode
.getBits());
286 * Get the cached last modification date of this file, in milliseconds.
288 * One of the indicators that the file has been modified by an application
289 * changing the working tree is if the last modification time for the file
290 * differs from the time stored in this entry.
292 * @return last modification time of this file, in milliseconds since the
293 * Java epoch (midnight Jan 1, 1970 UTC).
295 public long getLastModified() {
296 return decodeTS(P_MTIME
);
300 * Set the cached last modification date of this file, using milliseconds.
303 * new cached modification date of the file, in milliseconds.
305 public void setLastModified(final long when
) {
306 encodeTS(P_MTIME
, when
);
310 * Get the cached size (in bytes) of this file.
312 * One of the indicators that the file has been modified by an application
313 * changing the working tree is if the size of the file (in bytes) differs
314 * from the size stored in this entry.
316 * Note that this is the length of the file in the working directory, which
317 * may differ from the size of the decompressed blob if work tree filters
318 * are being used, such as LF<->CRLF conversion.
320 * @return cached size of the working directory file, in bytes.
322 public int getLength() {
323 return NB
.decodeInt32(info
, infoOffset
+ P_SIZE
);
327 * Set the cached size (in bytes) of this file.
330 * new cached size of the file, as bytes.
332 public void setLength(final int sz
) {
333 NB
.encodeInt32(info
, infoOffset
+ P_SIZE
, sz
);
337 * Obtain the ObjectId for the entry.
339 * Using this method to compare ObjectId values between entries is
340 * inefficient as it causes memory allocation.
342 * @return object identifier for the entry.
344 public ObjectId
getObjectId() {
345 return ObjectId
.fromRaw(idBuffer(), idOffset());
349 * Get the entry's complete path.
351 * This method is not very efficient and is primarily meant for debugging
352 * and final output generation. Applications should try to avoid calling it,
353 * and if invoked do so only once per interesting entry, where the name is
354 * absolutely required for correct function.
356 * @return complete path of the entry, from the root of the repository. If
357 * the entry is in a subtree there will be at least one '/' in the
360 public String
getPathString() {
361 return Constants
.CHARSET
.decode(ByteBuffer
.wrap(path
)).toString();
365 * Copy the ObjectId and other meta fields from an existing entry.
367 * This method copies everything except the path from one entry to another,
368 * supporting renaming.
371 * the entry to copy ObjectId and meta fields from.
373 public void copyMetaData(final DirCacheEntry src
) {
374 final int pLen
= NB
.decodeUInt16(info
, infoOffset
+ P_FLAGS
) & NAME_MASK
;
375 System
.arraycopy(src
.info
, src
.infoOffset
, info
, infoOffset
, INFO_LEN
);
376 NB
.encodeInt16(info
, infoOffset
+ P_FLAGS
, pLen
377 | NB
.decodeUInt16(info
, infoOffset
+ P_FLAGS
) & ~NAME_MASK
);
380 private long decodeTS(final int pIdx
) {
381 final int base
= infoOffset
+ pIdx
;
382 final int sec
= NB
.decodeInt32(info
, base
);
383 final int ms
= NB
.decodeInt32(info
, base
+ 4) / 1000000;
384 return 1000L * sec
+ ms
;
387 private void encodeTS(final int pIdx
, final long when
) {
388 final int base
= infoOffset
+ pIdx
;
389 NB
.encodeInt32(info
, base
, (int) (when
/ 1000));
390 NB
.encodeInt32(info
, base
+ 4, ((int) (when
% 1000)) * 1000000);