]> source.dussan.org Git - jgit.git/commitdiff
FileSnapshot: don't try to read file attributes twice 88/174188/1
authorMatthias Sohn <matthias.sohn@sap.com>
Sun, 3 Jan 2021 14:33:36 +0000 (15:33 +0100)
committerMatthias Sohn <matthias.sohn@sap.com>
Sun, 3 Jan 2021 14:33:36 +0000 (15:33 +0100)
If file doesn't exist set state to MISSING_FILE immediately. Doing that
by calling File#lastModified and File#length effectively does the same
since they set the value to 0 if the file doesn't exist.

Log an error if a different exception than NoSuchFileException is
caught.

Change-Id: I0d4396b9f80446692a088d17522d64f735ce6708

org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/FileSnapshot.java

index 54ff7d29c6c7e1fbeaae96ef8a3c7a3c39a125cd..7b4406a4b9c4656b52a00be1921968b4e5169822 100644 (file)
@@ -12,8 +12,10 @@ package org.eclipse.jgit.internal.storage.file;
 
 import static org.eclipse.jgit.util.FS.FileStoreAttributes.FALLBACK_FILESTORE_ATTRIBUTES;
 import static org.eclipse.jgit.util.FS.FileStoreAttributes.FALLBACK_TIMESTAMP_RESOLUTION;
+
 import java.io.File;
 import java.io.IOException;
+import java.nio.file.NoSuchFileException;
 import java.nio.file.attribute.BasicFileAttributes;
 import java.time.Duration;
 import java.time.Instant;
@@ -226,9 +228,15 @@ public class FileSnapshot {
                BasicFileAttributes fileAttributes = null;
                try {
                        fileAttributes = FS.DETECTED.fileAttributes(file);
+               } catch (NoSuchFileException e) {
+                       this.lastModified = Instant.EPOCH;
+                       this.size = 0L;
+                       this.fileKey = MISSING_FILEKEY;
+                       return;
                } catch (IOException e) {
-                       this.lastModified = Instant.ofEpochMilli(file.lastModified());
-                       this.size = file.length();
+                       LOG.error(e.getMessage(), e);
+                       this.lastModified = Instant.EPOCH;
+                       this.size = 0L;
                        this.fileKey = MISSING_FILEKEY;
                        return;
                }
@@ -309,9 +317,14 @@ public class FileSnapshot {
                        currLastModified = fileAttributes.lastModifiedTime().toInstant();
                        currSize = fileAttributes.size();
                        currFileKey = getFileKey(fileAttributes);
+               } catch (NoSuchFileException e) {
+                       currLastModified = Instant.EPOCH;
+                       currSize = 0L;
+                       currFileKey = MISSING_FILEKEY;
                } catch (IOException e) {
-                       currLastModified = Instant.ofEpochMilli(path.lastModified());
-                       currSize = path.length();
+                       LOG.error(e.getMessage(), e);
+                       currLastModified = Instant.EPOCH;
+                       currSize = 0L;
                        currFileKey = MISSING_FILEKEY;
                }
                sizeChanged = isSizeChanged(currSize);