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