1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.internal.storage.file;
12
13 import static java.nio.charset.StandardCharsets.UTF_8;
14 import static org.eclipse.jgit.internal.storage.pack.PackExt.PACK;
15 import static org.eclipse.jgit.internal.storage.pack.PackExt.BITMAP_INDEX;
16 import static org.eclipse.jgit.internal.storage.pack.PackExt.INDEX;
17
18 import java.io.BufferedReader;
19 import java.io.File;
20 import java.io.FileNotFoundException;
21 import java.io.IOException;
22 import java.nio.file.Files;
23 import java.text.MessageFormat;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.HashSet;
28 import java.util.List;
29 import java.util.Objects;
30 import java.util.Set;
31 import java.util.concurrent.atomic.AtomicReference;
32
33 import org.eclipse.jgit.internal.JGitText;
34 import org.eclipse.jgit.internal.storage.pack.ObjectToPack;
35 import org.eclipse.jgit.internal.storage.pack.PackExt;
36 import org.eclipse.jgit.internal.storage.pack.PackWriter;
37 import org.eclipse.jgit.lib.AbbreviatedObjectId;
38 import org.eclipse.jgit.lib.AnyObjectId;
39 import org.eclipse.jgit.lib.Config;
40 import org.eclipse.jgit.lib.Constants;
41 import org.eclipse.jgit.lib.ObjectDatabase;
42 import org.eclipse.jgit.lib.ObjectId;
43 import org.eclipse.jgit.lib.ObjectLoader;
44 import org.eclipse.jgit.lib.RepositoryCache;
45 import org.eclipse.jgit.lib.RepositoryCache.FileKey;
46 import org.eclipse.jgit.util.FS;
47 import org.eclipse.jgit.util.FileUtils;
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 public class ObjectDirectory extends FileObjectDatabase {
68
69 private static final int RESOLVE_ABBREV_LIMIT = 256;
70
71 private final AlternateHandle handle = new AlternateHandle(this);
72
73 private final Config config;
74
75 private final File objects;
76
77 private final File infoDirectory;
78
79 private final LooseObjects loose;
80
81 private final PackDirectory packed;
82
83 private final PackDirectory preserved;
84
85 private final File alternatesFile;
86
87 private final FS fs;
88
89 private final AtomicReference<AlternateHandle[]> alternates;
90
91 private final File shallowFile;
92
93 private FileSnapshot shallowFileSnapshot = FileSnapshot.DIRTY;
94
95 private Set<ObjectId> shallowCommitsIds;
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115 public ObjectDirectory(final Config cfg, final File dir,
116 File[] alternatePaths, FS fs, File shallowFile) throws IOException {
117 config = cfg;
118 objects = dir;
119 infoDirectory = new File(objects, "info");
120 File packDirectory = new File(objects, "pack");
121 File preservedDirectory = new File(packDirectory, "preserved");
122 alternatesFile = new File(objects, Constants.INFO_ALTERNATES);
123 loose = new LooseObjects(objects);
124 packed = new PackDirectory(config, packDirectory);
125 preserved = new PackDirectory(config, preservedDirectory);
126 this.fs = fs;
127 this.shallowFile = shallowFile;
128
129 alternates = new AtomicReference<>();
130 if (alternatePaths != null) {
131 AlternateHandle[] alt;
132
133 alt = new AlternateHandle[alternatePaths.length];
134 for (int i = 0; i < alternatePaths.length; i++)
135 alt[i] = openAlternate(alternatePaths[i]);
136 alternates.set(alt);
137 }
138 }
139
140
141 @Override
142 public final File getDirectory() {
143 return loose.getDirectory();
144 }
145
146
147
148
149
150
151 public final File getPackDirectory() {
152 return packed.getDirectory();
153 }
154
155
156
157
158
159
160 public final File getPreservedDirectory() {
161 return preserved.getDirectory();
162 }
163
164
165 @Override
166 public boolean exists() {
167 return fs.exists(objects);
168 }
169
170
171 @Override
172 public void create() throws IOException {
173 loose.create();
174 FileUtils.mkdir(infoDirectory);
175 packed.create();
176 }
177
178
179 @Override
180 public ObjectDirectoryInserter newInserter() {
181 return new ObjectDirectoryInserter(this, config);
182 }
183
184
185
186
187
188
189
190 public PackInserter newPackInserter() {
191 return new PackInserter(this);
192 }
193
194
195 @Override
196 public void close() {
197 loose.close();
198
199 packed.close();
200
201
202 AlternateHandle[] alt = alternates.get();
203 if (alt != null && alternates.compareAndSet(alt, null)) {
204 for(AlternateHandle od : alt)
205 od.close();
206 }
207 }
208
209
210 @Override
211 public Collection<Pack> getPacks() {
212 return packed.getPacks();
213 }
214
215
216
217
218
219
220 @Override
221 public Pack openPack(File pack) throws IOException {
222 PackFile pf;
223 try {
224 pf = new PackFile(pack);
225 } catch (IllegalArgumentException e) {
226 throw new IOException(
227 MessageFormat.format(JGitText.get().notAValidPack, pack),
228 e);
229 }
230
231 String p = pf.getName();
232
233 if (p.length() != 50 || !p.startsWith("pack-")
234 || !pf.getPackExt().equals(PACK)) {
235 throw new IOException(
236 MessageFormat.format(JGitText.get().notAValidPack, pack));
237 }
238
239 PackFile bitmapIdx = pf.create(BITMAP_INDEX);
240 Pack res = new Pack(pack, bitmapIdx.exists() ? bitmapIdx : null);
241 packed.insert(res);
242 return res;
243 }
244
245
246 @Override
247 public String toString() {
248 return "ObjectDirectory[" + getDirectory() + "]";
249 }
250
251
252 @Override
253 public boolean has(AnyObjectId objectId) {
254 return loose.hasCached(objectId)
255 || hasPackedOrLooseInSelfOrAlternate(objectId)
256 || (restoreFromSelfOrAlternate(objectId, null)
257 && hasPackedOrLooseInSelfOrAlternate(objectId));
258 }
259
260 private boolean hasPackedOrLooseInSelfOrAlternate(AnyObjectId objectId) {
261 return hasPackedInSelfOrAlternate(objectId, null)
262 || hasLooseInSelfOrAlternate(objectId, null);
263 }
264
265 private boolean hasPackedInSelfOrAlternate(AnyObjectId objectId,
266 Set<AlternateHandle.Id> skips) {
267 if (hasPackedObject(objectId)) {
268 return true;
269 }
270 skips = addMe(skips);
271 for (AlternateHandle alt : myAlternates()) {
272 if (!skips.contains(alt.getId())) {
273 if (alt.db.hasPackedInSelfOrAlternate(objectId, skips)) {
274 return true;
275 }
276 }
277 }
278 return false;
279 }
280
281 private boolean hasLooseInSelfOrAlternate(AnyObjectId objectId,
282 Set<AlternateHandle.Id> skips) {
283 if (loose.has(objectId)) {
284 return true;
285 }
286 skips = addMe(skips);
287 for (AlternateHandle alt : myAlternates()) {
288 if (!skips.contains(alt.getId())) {
289 if (alt.db.hasLooseInSelfOrAlternate(objectId, skips)) {
290 return true;
291 }
292 }
293 }
294 return false;
295 }
296
297 boolean hasPackedObject(AnyObjectId objectId) {
298 return packed.has(objectId);
299 }
300
301 @Override
302 void resolve(Set<ObjectId> matches, AbbreviatedObjectId id)
303 throws IOException {
304 resolve(matches, id, null);
305 }
306
307 private void resolve(Set<ObjectId> matches, AbbreviatedObjectId id,
308 Set<AlternateHandle.Id> skips)
309 throws IOException {
310 if (!packed.resolve(matches, id, RESOLVE_ABBREV_LIMIT))
311 return;
312
313 if (!loose.resolve(matches, id, RESOLVE_ABBREV_LIMIT))
314 return;
315
316 skips = addMe(skips);
317 for (AlternateHandle alt : myAlternates()) {
318 if (!skips.contains(alt.getId())) {
319 alt.db.resolve(matches, id, skips);
320 if (matches.size() > RESOLVE_ABBREV_LIMIT) {
321 return;
322 }
323 }
324 }
325 }
326
327 @Override
328 ObjectLoader openObject(WindowCursor curs, AnyObjectId objectId)
329 throws IOException {
330 ObjectLoader ldr = openObjectWithoutRestoring(curs, objectId);
331 if (ldr == null && restoreFromSelfOrAlternate(objectId, null)) {
332 ldr = openObjectWithoutRestoring(curs, objectId);
333 }
334 return ldr;
335 }
336
337 private ObjectLoader openObjectWithoutRestoring(WindowCursor curs, AnyObjectId objectId)
338 throws IOException {
339 if (loose.hasCached(objectId)) {
340 ObjectLoader ldr = openLooseObject(curs, objectId);
341 if (ldr != null) {
342 return ldr;
343 }
344 }
345 ObjectLoader ldr = openPackedFromSelfOrAlternate(curs, objectId, null);
346 if (ldr != null) {
347 return ldr;
348 }
349 return openLooseFromSelfOrAlternate(curs, objectId, null);
350 }
351
352 private ObjectLoader openPackedFromSelfOrAlternate(WindowCursor curs,
353 AnyObjectId objectId, Set<AlternateHandle.Id> skips) {
354 ObjectLoader ldr = openPackedObject(curs, objectId);
355 if (ldr != null) {
356 return ldr;
357 }
358 skips = addMe(skips);
359 for (AlternateHandle alt : myAlternates()) {
360 if (!skips.contains(alt.getId())) {
361 ldr = alt.db.openPackedFromSelfOrAlternate(curs, objectId, skips);
362 if (ldr != null) {
363 return ldr;
364 }
365 }
366 }
367 return null;
368 }
369
370 private ObjectLoader openLooseFromSelfOrAlternate(WindowCursor curs,
371 AnyObjectId objectId, Set<AlternateHandle.Id> skips)
372 throws IOException {
373 ObjectLoader ldr = openLooseObject(curs, objectId);
374 if (ldr != null) {
375 return ldr;
376 }
377 skips = addMe(skips);
378 for (AlternateHandle alt : myAlternates()) {
379 if (!skips.contains(alt.getId())) {
380 ldr = alt.db.openLooseFromSelfOrAlternate(curs, objectId, skips);
381 if (ldr != null) {
382 return ldr;
383 }
384 }
385 }
386 return null;
387 }
388
389 ObjectLoader openPackedObject(WindowCursor curs, AnyObjectId objectId) {
390 return packed.open(curs, objectId);
391 }
392
393 @Override
394 ObjectLoader openLooseObject(WindowCursor curs, AnyObjectId id)
395 throws IOException {
396 return loose.open(curs, id);
397 }
398
399 @Override
400 long getObjectSize(WindowCursor curs, AnyObjectId id) throws IOException {
401 long sz = getObjectSizeWithoutRestoring(curs, id);
402 if (0 > sz && restoreFromSelfOrAlternate(id, null)) {
403 sz = getObjectSizeWithoutRestoring(curs, id);
404 }
405 return sz;
406 }
407
408 private long getObjectSizeWithoutRestoring(WindowCursor curs,
409 AnyObjectId id) throws IOException {
410 if (loose.hasCached(id)) {
411 long len = loose.getSize(curs, id);
412 if (0 <= len) {
413 return len;
414 }
415 }
416 long len = getPackedSizeFromSelfOrAlternate(curs, id, null);
417 if (0 <= len) {
418 return len;
419 }
420 return getLooseSizeFromSelfOrAlternate(curs, id, null);
421 }
422
423 private long getPackedSizeFromSelfOrAlternate(WindowCursor curs,
424 AnyObjectId id, Set<AlternateHandle.Id> skips) {
425 long len = packed.getSize(curs, id);
426 if (0 <= len) {
427 return len;
428 }
429 skips = addMe(skips);
430 for (AlternateHandle alt : myAlternates()) {
431 if (!skips.contains(alt.getId())) {
432 len = alt.db.getPackedSizeFromSelfOrAlternate(curs, id, skips);
433 if (0 <= len) {
434 return len;
435 }
436 }
437 }
438 return -1;
439 }
440
441 private long getLooseSizeFromSelfOrAlternate(WindowCursor curs,
442 AnyObjectId id, Set<AlternateHandle.Id> skips) throws IOException {
443 long len = loose.getSize(curs, id);
444 if (0 <= len) {
445 return len;
446 }
447 skips = addMe(skips);
448 for (AlternateHandle alt : myAlternates()) {
449 if (!skips.contains(alt.getId())) {
450 len = alt.db.getLooseSizeFromSelfOrAlternate(curs, id, skips);
451 if (0 <= len) {
452 return len;
453 }
454 }
455 }
456 return -1;
457 }
458
459 @Override
460 void selectObjectRepresentation(PackWriter packer, ObjectToPack otp,
461 WindowCursor curs) throws IOException {
462 selectObjectRepresentation(packer, otp, curs, null);
463 }
464
465 private void selectObjectRepresentation(PackWriter packer, ObjectToPack otp,
466 WindowCursor curs, Set<AlternateHandle.Id> skips) throws IOException {
467 packed.selectRepresentation(packer, otp, curs);
468
469 skips = addMe(skips);
470 for (AlternateHandle h : myAlternates()) {
471 if (!skips.contains(h.getId())) {
472 h.db.selectObjectRepresentation(packer, otp, curs, skips);
473 }
474 }
475 }
476
477 private boolean restoreFromSelfOrAlternate(AnyObjectId objectId,
478 Set<AlternateHandle.Id> skips) {
479 if (restoreFromSelf(objectId)) {
480 return true;
481 }
482
483 skips = addMe(skips);
484 for (AlternateHandle alt : myAlternates()) {
485 if (!skips.contains(alt.getId())) {
486 if (alt.db.restoreFromSelfOrAlternate(objectId, skips)) {
487 return true;
488 }
489 }
490 }
491 return false;
492 }
493
494 private boolean restoreFromSelf(AnyObjectId objectId) {
495 Pack preservedPack = preserved.getPack(objectId);
496 if (preservedPack == null) {
497 return false;
498 }
499 PackFile preservedFile = new PackFile(preservedPack.getPackFile());
500
501
502 for (PackExt ext : PackExt.values()) {
503 if (!INDEX.equals(ext)) {
504 restore(preservedFile.create(ext));
505 }
506 }
507 restore(preservedFile.create(INDEX));
508 return true;
509 }
510
511 private boolean restore(PackFile preservedPack) {
512 PackFile restored = preservedPack
513 .createForDirectory(packed.getDirectory());
514 try {
515 Files.createLink(restored.toPath(), preservedPack.toPath());
516 } catch (IOException e) {
517 return false;
518 }
519 return true;
520 }
521
522 @Override
523 InsertLooseObjectResult insertUnpackedObject(File tmp, ObjectId id,
524 boolean createDuplicate) throws IOException {
525
526
527 if (loose.hasCached(id)) {
528 FileUtils.delete(tmp, FileUtils.RETRY);
529 return InsertLooseObjectResult.EXISTS_LOOSE;
530 }
531 if (!createDuplicate && has(id)) {
532 FileUtils.delete(tmp, FileUtils.RETRY);
533 return InsertLooseObjectResult.EXISTS_PACKED;
534 }
535 return loose.insert(tmp, id);
536 }
537
538 @Override
539 Config getConfig() {
540 return config;
541 }
542
543 @Override
544 FS getFS() {
545 return fs;
546 }
547
548 @Override
549 Set<ObjectId> getShallowCommits() throws IOException {
550 if (shallowFile == null || !shallowFile.isFile())
551 return Collections.emptySet();
552
553 if (shallowFileSnapshot == null
554 || shallowFileSnapshot.isModified(shallowFile)) {
555 shallowCommitsIds = new HashSet<>();
556
557 try (BufferedReader reader = open(shallowFile)) {
558 String line;
559 while ((line = reader.readLine()) != null) {
560 try {
561 shallowCommitsIds.add(ObjectId.fromString(line));
562 } catch (IllegalArgumentException ex) {
563 throw new IOException(MessageFormat
564 .format(JGitText.get().badShallowLine, line),
565 ex);
566 }
567 }
568 }
569
570 shallowFileSnapshot = FileSnapshot.save(shallowFile);
571 }
572
573 return shallowCommitsIds;
574 }
575
576 void closeAllPackHandles(File packFile) {
577
578
579
580
581 if (packFile.exists()) {
582 for (Pack p : packed.getPacks()) {
583 if (packFile.getPath().equals(p.getPackFile().getPath())) {
584 p.close();
585 break;
586 }
587 }
588 }
589 }
590
591 AlternateHandle[] myAlternates() {
592 AlternateHandle[] alt = alternates.get();
593 if (alt == null) {
594 synchronized (alternates) {
595 alt = alternates.get();
596 if (alt == null) {
597 try {
598 alt = loadAlternates();
599 } catch (IOException e) {
600 alt = new AlternateHandle[0];
601 }
602 alternates.set(alt);
603 }
604 }
605 }
606 return alt;
607 }
608
609 Set<AlternateHandle.Id> addMe(Set<AlternateHandle.Id> skips) {
610 if (skips == null) {
611 skips = new HashSet<>();
612 }
613 skips.add(handle.getId());
614 return skips;
615 }
616
617 private AlternateHandle[] loadAlternates() throws IOException {
618 final List<AlternateHandle> l = new ArrayList<>(4);
619 try (BufferedReader br = open(alternatesFile)) {
620 String line;
621 while ((line = br.readLine()) != null) {
622 l.add(openAlternate(line));
623 }
624 }
625 return l.toArray(new AlternateHandle[0]);
626 }
627
628 private static BufferedReader open(File f)
629 throws IOException, FileNotFoundException {
630 return Files.newBufferedReader(f.toPath(), UTF_8);
631 }
632
633 private AlternateHandle openAlternate(String location)
634 throws IOException {
635 final File objdir = fs.resolve(objects, location);
636 return openAlternate(objdir);
637 }
638
639 private AlternateHandle openAlternate(File objdir) throws IOException {
640 final File parent = objdir.getParentFile();
641 if (FileKey.isGitRepository(parent, fs)) {
642 FileKey key = FileKey.exact(parent, fs);
643 FileRepository db = (FileRepository) RepositoryCache.open(key);
644 return new AlternateRepository(db);
645 }
646
647 ObjectDirectory db = new ObjectDirectory(config, objdir, null, fs, null);
648 return new AlternateHandle(db);
649 }
650
651
652
653
654 @Override
655 public File fileFor(AnyObjectId objectId) {
656 return loose.fileFor(objectId);
657 }
658
659 static class AlternateHandle {
660 static class Id {
661 String alternateId;
662
663 public Id(File object) {
664 try {
665 this.alternateId = object.getCanonicalPath();
666 } catch (Exception e) {
667 alternateId = null;
668 }
669 }
670
671 @Override
672 public boolean equals(Object o) {
673 if (o == this) {
674 return true;
675 }
676 if (o == null || !(o instanceof Id)) {
677 return false;
678 }
679 Id aId = (Id) o;
680 return Objects.equals(alternateId, aId.alternateId);
681 }
682
683 @Override
684 public int hashCode() {
685 if (alternateId == null) {
686 return 1;
687 }
688 return alternateId.hashCode();
689 }
690 }
691
692 final ObjectDirectory db;
693
694 AlternateHandle(ObjectDirectory db) {
695 this.db = db;
696 }
697
698 void close() {
699 db.close();
700 }
701
702 public Id getId(){
703 return db.getAlternateId();
704 }
705 }
706
707 static class AlternateRepository extends AlternateHandle {
708 final FileRepository repository;
709
710 AlternateRepository(FileRepository r) {
711 super(r.getObjectDatabase());
712 repository = r;
713 }
714
715 @Override
716 void close() {
717 repository.close();
718 }
719 }
720
721
722 @Override
723 public ObjectDatabase newCachedDatabase() {
724 return newCachedFileObjectDatabase();
725 }
726
727 CachedObjectDirectory newCachedFileObjectDatabase() {
728 return new CachedObjectDirectory(this);
729 }
730
731 AlternateHandle.Id getAlternateId() {
732 return new AlternateHandle.Id(objects);
733 }
734 }