FileSnapshot: don't try to read file attributes twice

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
This commit is contained in:
Matthias Sohn 2021-01-03 15:33:36 +01:00
parent 0f442d7083
commit 877ce01d29
1 changed files with 17 additions and 4 deletions

View File

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