1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 package org.eclipse.jgit.dircache;
48
49 import java.io.ByteArrayOutputStream;
50 import java.io.EOFException;
51 import java.io.IOException;
52 import java.io.InputStream;
53 import java.io.OutputStream;
54 import java.nio.ByteBuffer;
55 import java.security.MessageDigest;
56 import java.text.MessageFormat;
57 import java.util.Arrays;
58
59 import org.eclipse.jgit.errors.CorruptObjectException;
60 import org.eclipse.jgit.internal.JGitText;
61 import org.eclipse.jgit.lib.AnyObjectId;
62 import org.eclipse.jgit.lib.Constants;
63 import org.eclipse.jgit.lib.FileMode;
64 import org.eclipse.jgit.lib.ObjectId;
65 import org.eclipse.jgit.util.IO;
66 import org.eclipse.jgit.util.MutableInteger;
67 import org.eclipse.jgit.util.NB;
68 import org.eclipse.jgit.util.SystemReader;
69
70
71
72
73
74
75
76 public class DirCacheEntry {
77 private static final byte[] nullpad = new byte[8];
78
79
80 public static final int STAGE_0 = 0;
81
82
83 public static final int STAGE_1 = 1;
84
85
86 public static final int STAGE_2 = 2;
87
88
89 public static final int STAGE_3 = 3;
90
91 private static final int P_CTIME = 0;
92
93
94
95 private static final int P_MTIME = 8;
96
97
98
99
100
101
102
103 private static final int P_MODE = 24;
104
105
106
107
108
109 private static final int P_SIZE = 36;
110
111 private static final int P_OBJECTID = 40;
112
113 private static final int P_FLAGS = 60;
114 private static final int P_FLAGS2 = 62;
115
116
117 private static final int NAME_MASK = 0xfff;
118
119 private static final int INTENT_TO_ADD = 0x20000000;
120 private static final int SKIP_WORKTREE = 0x40000000;
121 private static final int EXTENDED_FLAGS = (INTENT_TO_ADD | SKIP_WORKTREE);
122
123 private static final int INFO_LEN = 62;
124 private static final int INFO_LEN_EXTENDED = 64;
125
126 private static final int EXTENDED = 0x40;
127 private static final int ASSUME_VALID = 0x80;
128
129
130 private static final int UPDATE_NEEDED = 0x1;
131
132
133 private final byte[] info;
134
135
136 private final int infoOffset;
137
138
139 final byte[] path;
140
141
142 private byte inCoreFlags;
143
144 DirCacheEntry(final byte[] sharedInfo, final MutableInteger infoAt,
145 final InputStream in, final MessageDigest md, final int smudge_s,
146 final int smudge_ns) throws IOException {
147 info = sharedInfo;
148 infoOffset = infoAt.value;
149
150 IO.readFully(in, info, infoOffset, INFO_LEN);
151
152 final int len;
153 if (isExtended()) {
154 len = INFO_LEN_EXTENDED;
155 IO.readFully(in, info, infoOffset + INFO_LEN, INFO_LEN_EXTENDED - INFO_LEN);
156
157 if ((getExtendedFlags() & ~EXTENDED_FLAGS) != 0)
158 throw new IOException(MessageFormat.format(JGitText.get()
159 .DIRCUnrecognizedExtendedFlags, String.valueOf(getExtendedFlags())));
160 } else
161 len = INFO_LEN;
162
163 infoAt.value += len;
164 md.update(info, infoOffset, len);
165
166 int pathLen = NB.decodeUInt16(info, infoOffset + P_FLAGS) & NAME_MASK;
167 int skipped = 0;
168 if (pathLen < NAME_MASK) {
169 path = new byte[pathLen];
170 IO.readFully(in, path, 0, pathLen);
171 md.update(path, 0, pathLen);
172 } else {
173 final ByteArrayOutputStream tmp = new ByteArrayOutputStream();
174 {
175 final byte[] buf = new byte[NAME_MASK];
176 IO.readFully(in, buf, 0, NAME_MASK);
177 tmp.write(buf);
178 }
179 for (;;) {
180 final int c = in.read();
181 if (c < 0)
182 throw new EOFException(JGitText.get().shortReadOfBlock);
183 if (c == 0)
184 break;
185 tmp.write(c);
186 }
187 path = tmp.toByteArray();
188 pathLen = path.length;
189 skipped = 1;
190 md.update(path, 0, pathLen);
191 md.update((byte) 0);
192 }
193
194 try {
195 checkPath(path);
196 } catch (InvalidPathException e) {
197 CorruptObjectException p =
198 new CorruptObjectException(e.getMessage());
199 if (e.getCause() != null)
200 p.initCause(e.getCause());
201 throw p;
202 }
203
204
205
206
207 final int actLen = len + pathLen;
208 final int expLen = (actLen + 8) & ~7;
209 final int padLen = expLen - actLen - skipped;
210 if (padLen > 0) {
211 IO.skipFully(in, padLen);
212 md.update(nullpad, 0, padLen);
213 }
214
215 if (mightBeRacilyClean(smudge_s, smudge_ns))
216 smudgeRacilyClean();
217 }
218
219
220
221
222
223
224
225
226
227
228
229 public DirCacheEntry(final String newPath) {
230 this(Constants.encode(newPath), STAGE_0);
231 }
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246 public DirCacheEntry(final String newPath, final int stage) {
247 this(Constants.encode(newPath), stage);
248 }
249
250
251
252
253
254
255
256
257
258
259
260 public DirCacheEntry(final byte[] newPath) {
261 this(newPath, STAGE_0);
262 }
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277 @SuppressWarnings("boxing")
278 public DirCacheEntry(byte[] path, final int stage) {
279 checkPath(path);
280 if (stage < 0 || 3 < stage)
281 throw new IllegalArgumentException(MessageFormat.format(
282 JGitText.get().invalidStageForPath,
283 stage, toString(path)));
284
285 info = new byte[INFO_LEN];
286 infoOffset = 0;
287 this.path = path;
288
289 int flags = ((stage & 0x3) << 12);
290 if (path.length < NAME_MASK)
291 flags |= path.length;
292 else
293 flags |= NAME_MASK;
294 NB.encodeInt16(info, infoOffset + P_FLAGS, flags);
295 }
296
297
298
299
300
301
302
303
304
305
306
307 public DirCacheEntry(DirCacheEntry src) {
308 path = src.path;
309 info = new byte[INFO_LEN];
310 infoOffset = 0;
311 System.arraycopy(src.info, src.infoOffset, info, 0, INFO_LEN);
312 }
313
314 void write(final OutputStream os) throws IOException {
315 final int len = isExtended() ? INFO_LEN_EXTENDED : INFO_LEN;
316 final int pathLen = path.length;
317 os.write(info, infoOffset, len);
318 os.write(path, 0, pathLen);
319
320
321
322
323 final int actLen = len + pathLen;
324 final int expLen = (actLen + 8) & ~7;
325 if (actLen != expLen)
326 os.write(nullpad, 0, expLen - actLen);
327 }
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345 public final boolean mightBeRacilyClean(final int smudge_s, final int smudge_ns) {
346
347
348
349
350
351
352
353 final int base = infoOffset + P_MTIME;
354 final int mtime = NB.decodeInt32(info, base);
355 if (smudge_s == mtime)
356 return smudge_ns <= NB.decodeInt32(info, base + 4);
357 return false;
358 }
359
360
361
362
363
364
365
366
367 public final void smudgeRacilyClean() {
368
369
370
371
372
373
374 final int base = infoOffset + P_SIZE;
375 Arrays.fill(info, base, base + 4, (byte) 0);
376 }
377
378
379
380
381
382
383
384
385
386
387
388 public final boolean isSmudged() {
389 final int base = infoOffset + P_OBJECTID;
390 return (getLength() == 0) && (Constants.EMPTY_BLOB_ID.compareTo(info, base) != 0);
391 }
392
393 final byte[] idBuffer() {
394 return info;
395 }
396
397 final int idOffset() {
398 return infoOffset + P_OBJECTID;
399 }
400
401
402
403
404
405
406
407
408
409
410 public boolean isAssumeValid() {
411 return (info[infoOffset + P_FLAGS] & ASSUME_VALID) != 0;
412 }
413
414
415
416
417
418
419
420
421 public void setAssumeValid(final boolean assume) {
422 if (assume)
423 info[infoOffset + P_FLAGS] |= ASSUME_VALID;
424 else
425 info[infoOffset + P_FLAGS] &= ~ASSUME_VALID;
426 }
427
428
429
430
431 public boolean isUpdateNeeded() {
432 return (inCoreFlags & UPDATE_NEEDED) != 0;
433 }
434
435
436
437
438
439
440 public void setUpdateNeeded(boolean updateNeeded) {
441 if (updateNeeded)
442 inCoreFlags |= UPDATE_NEEDED;
443 else
444 inCoreFlags &= ~UPDATE_NEEDED;
445 }
446
447
448
449
450
451
452
453
454 public int getStage() {
455 return (info[infoOffset + P_FLAGS] >>> 4) & 0x3;
456 }
457
458
459
460
461
462
463 public boolean isSkipWorkTree() {
464 return (getExtendedFlags() & SKIP_WORKTREE) != 0;
465 }
466
467
468
469
470
471
472 public boolean isIntentToAdd() {
473 return (getExtendedFlags() & INTENT_TO_ADD) != 0;
474 }
475
476
477
478
479
480
481
482 public boolean isMerged() {
483 return getStage() == STAGE_0;
484 }
485
486
487
488
489
490
491
492 public int getRawMode() {
493 return NB.decodeInt32(info, infoOffset + P_MODE);
494 }
495
496
497
498
499
500
501 public FileMode getFileMode() {
502 return FileMode.fromBits(getRawMode());
503 }
504
505
506
507
508
509
510
511
512
513
514
515 public void setFileMode(final FileMode mode) {
516 switch (mode.getBits() & FileMode.TYPE_MASK) {
517 case FileMode.TYPE_MISSING:
518 case FileMode.TYPE_TREE:
519 throw new IllegalArgumentException(MessageFormat.format(
520 JGitText.get().invalidModeForPath, mode, getPathString()));
521 }
522 NB.encodeInt32(info, infoOffset + P_MODE, mode.getBits());
523 }
524
525 void setFileMode(int mode) {
526 NB.encodeInt32(info, infoOffset + P_MODE, mode);
527 }
528
529
530
531
532
533
534
535 public long getCreationTime() {
536 return decodeTS(P_CTIME);
537 }
538
539
540
541
542
543
544
545 public void setCreationTime(final long when) {
546 encodeTS(P_CTIME, when);
547 }
548
549
550
551
552
553
554
555
556
557
558
559 public long getLastModified() {
560 return decodeTS(P_MTIME);
561 }
562
563
564
565
566
567
568
569 public void setLastModified(final long when) {
570 encodeTS(P_MTIME, when);
571 }
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590 public int getLength() {
591 return NB.decodeInt32(info, infoOffset + P_SIZE);
592 }
593
594
595
596
597
598
599
600
601 public void setLength(final int sz) {
602 NB.encodeInt32(info, infoOffset + P_SIZE, sz);
603 }
604
605
606
607
608
609
610
611 public void setLength(final long sz) {
612 setLength((int) sz);
613 }
614
615
616
617
618
619
620
621
622
623 public ObjectId getObjectId() {
624 return ObjectId.fromRaw(idBuffer(), idOffset());
625 }
626
627
628
629
630
631
632
633
634 public void setObjectId(final AnyObjectId id) {
635 id.copyRawTo(idBuffer(), idOffset());
636 }
637
638
639
640
641
642
643
644
645
646
647 public void setObjectIdFromRaw(final byte[] bs, final int p) {
648 final int n = Constants.OBJECT_ID_LENGTH;
649 System.arraycopy(bs, p, idBuffer(), idOffset(), n);
650 }
651
652
653
654
655
656
657
658
659
660
661
662
663
664 public String getPathString() {
665 return toString(path);
666 }
667
668
669
670
671
672
673
674 public byte[] getRawPath() {
675 return path.clone();
676 }
677
678
679
680
681 @SuppressWarnings("nls")
682 @Override
683 public String toString() {
684 return getFileMode() + " " + getLength() + " " + getLastModified()
685 + " " + getObjectId() + " " + getStage() + " "
686 + getPathString() + "\n";
687 }
688
689
690
691
692
693
694
695
696
697
698 public void copyMetaData(final DirCacheEntry src) {
699 copyMetaData(src, false);
700 }
701
702
703
704
705
706
707
708
709
710
711
712
713 void copyMetaData(final DirCacheEntry src, boolean keepStage) {
714 int origflags = NB.decodeUInt16(info, infoOffset + P_FLAGS);
715 int newflags = NB.decodeUInt16(src.info, src.infoOffset + P_FLAGS);
716 System.arraycopy(src.info, src.infoOffset, info, infoOffset, INFO_LEN);
717 final int pLen = origflags & NAME_MASK;
718 final int SHIFTED_STAGE_MASK = 0x3 << 12;
719 final int pStageShifted;
720 if (keepStage)
721 pStageShifted = origflags & SHIFTED_STAGE_MASK;
722 else
723 pStageShifted = newflags & SHIFTED_STAGE_MASK;
724 NB.encodeInt16(info, infoOffset + P_FLAGS, pStageShifted | pLen
725 | (newflags & ~NAME_MASK & ~SHIFTED_STAGE_MASK));
726 }
727
728
729
730
731 boolean isExtended() {
732 return (info[infoOffset + P_FLAGS] & EXTENDED) != 0;
733 }
734
735 private long decodeTS(final int pIdx) {
736 final int base = infoOffset + pIdx;
737 final int sec = NB.decodeInt32(info, base);
738 final int ms = NB.decodeInt32(info, base + 4) / 1000000;
739 return 1000L * sec + ms;
740 }
741
742 private void encodeTS(final int pIdx, final long when) {
743 final int base = infoOffset + pIdx;
744 NB.encodeInt32(info, base, (int) (when / 1000));
745 NB.encodeInt32(info, base + 4, ((int) (when % 1000)) * 1000000);
746 }
747
748 private int getExtendedFlags() {
749 if (isExtended())
750 return NB.decodeUInt16(info, infoOffset + P_FLAGS2) << 16;
751 else
752 return 0;
753 }
754
755 private static void checkPath(byte[] path) {
756 try {
757 SystemReader.getInstance().checkPath(path);
758 } catch (CorruptObjectException e) {
759 InvalidPathException p = new InvalidPathException(toString(path));
760 p.initCause(e);
761 throw p;
762 }
763 }
764
765 static String toString(final byte[] path) {
766 return Constants.CHARSET.decode(ByteBuffer.wrap(path)).toString();
767 }
768
769 static int getMaximumInfoLength(boolean extended) {
770 return extended ? INFO_LEN_EXTENDED : INFO_LEN;
771 }
772 }