Browse Source

Raise error if FileNotFoundException is caught for an existing file

File, FileInputStream and friends may throw FileNotFoundException even
if the file is existing e.g. when file permissions don't allow to access
the file content. In most cases this is a severe error we should not
suppress hence rethrow the FileNotFoundException in this case.

This may also fix bug 451508.

Bug: 451508
Change-Id: If4a94217fb5b7cfd4c04d881902f3e86193c7008
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
stable-4.3
Matthias Sohn 9 years ago committed by Jonathan Nieder
parent
commit
d2faec27a7
  1. 1
      org.eclipse.jgit/resources/org/eclipse/jgit/internal/JGitText.properties
  2. 3
      org.eclipse.jgit/src/org/eclipse/jgit/api/RebaseCommand.java
  3. 1
      org.eclipse.jgit/src/org/eclipse/jgit/internal/JGitText.java
  4. 4
      org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/LockFile.java
  5. 25
      org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/ObjectDirectory.java
  6. 8
      org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/RefDirectory.java
  7. 6
      org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/ReflogReaderImpl.java
  8. 3
      org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/UnpackedObject.java
  9. 6
      org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java
  10. 3
      org.eclipse.jgit/src/org/eclipse/jgit/storage/file/FileBasedConfig.java

1
org.eclipse.jgit/resources/org/eclipse/jgit/internal/JGitText.properties

@ -451,6 +451,7 @@ packfileIsTruncated=Packfile {0} is truncated.
packfileIsTruncatedNoParam=Packfile is truncated. packfileIsTruncatedNoParam=Packfile is truncated.
packHandleIsStale=Pack file {0} handle is stale, removing it from pack list packHandleIsStale=Pack file {0} handle is stale, removing it from pack list
packHasUnresolvedDeltas=pack has unresolved deltas packHasUnresolvedDeltas=pack has unresolved deltas
packInaccessible=Pack file {0} now inaccessible; removing it from pack list
packingCancelledDuringObjectsWriting=Packing cancelled during objects writing packingCancelledDuringObjectsWriting=Packing cancelled during objects writing
packObjectCountMismatch=Pack object count mismatch: pack {0} index {1}: {2} packObjectCountMismatch=Pack object count mismatch: pack {0} index {1}: {2}
packRefs=Pack refs packRefs=Pack refs

3
org.eclipse.jgit/src/org/eclipse/jgit/api/RebaseCommand.java

@ -980,6 +980,9 @@ public class RebaseCommand extends GitCommand<RebaseResult> {
try { try {
raw = IO.readFully(authorScriptFile); raw = IO.readFully(authorScriptFile);
} catch (FileNotFoundException notFound) { } catch (FileNotFoundException notFound) {
if (authorScriptFile.exists()) {
throw notFound;
}
return null; return null;
} }
return parseAuthor(raw); return parseAuthor(raw);

1
org.eclipse.jgit/src/org/eclipse/jgit/internal/JGitText.java

@ -510,6 +510,7 @@ public class JGitText extends TranslationBundle {
/***/ public String packfileIsTruncatedNoParam; /***/ public String packfileIsTruncatedNoParam;
/***/ public String packHandleIsStale; /***/ public String packHandleIsStale;
/***/ public String packHasUnresolvedDeltas; /***/ public String packHasUnresolvedDeltas;
/***/ public String packInaccessible;
/***/ public String packingCancelledDuringObjectsWriting; /***/ public String packingCancelledDuringObjectsWriting;
/***/ public String packObjectCountMismatch; /***/ public String packObjectCountMismatch;
/***/ public String packRefs; /***/ public String packRefs;

4
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/LockFile.java

@ -227,6 +227,10 @@ public class LockFile {
fis.close(); fis.close();
} }
} catch (FileNotFoundException fnfe) { } catch (FileNotFoundException fnfe) {
if (ref.exists()) {
unlock();
throw fnfe;
}
// Don't worry about a file that doesn't exist yet, it // Don't worry about a file that doesn't exist yet, it
// conceptually has no current content to copy. // conceptually has no current content to copy.
// //

25
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/ObjectDirectory.java

@ -433,16 +433,14 @@ public class ObjectDirectory extends FileObjectDatabase {
ObjectLoader openLooseObject(WindowCursor curs, AnyObjectId id) ObjectLoader openLooseObject(WindowCursor curs, AnyObjectId id)
throws IOException { throws IOException {
try {
File path = fileFor(id); File path = fileFor(id);
FileInputStream in = new FileInputStream(path); try (FileInputStream in = new FileInputStream(path)) {
try {
unpackedObjectCache.add(id); unpackedObjectCache.add(id);
return UnpackedObject.open(in, path, id, curs); return UnpackedObject.open(in, path, id, curs);
} finally {
in.close();
}
} catch (FileNotFoundException noFile) { } catch (FileNotFoundException noFile) {
if (path.exists()) {
throw noFile;
}
unpackedObjectCache.remove(id); unpackedObjectCache.remove(id);
return null; return null;
} }
@ -513,15 +511,14 @@ public class ObjectDirectory extends FileObjectDatabase {
private long getLooseObjectSize(WindowCursor curs, AnyObjectId id) private long getLooseObjectSize(WindowCursor curs, AnyObjectId id)
throws IOException { throws IOException {
try { File f = fileFor(id);
FileInputStream in = new FileInputStream(fileFor(id)); try (FileInputStream in = new FileInputStream(f)) {
try {
unpackedObjectCache.add(id); unpackedObjectCache.add(id);
return UnpackedObject.getSize(in, id, curs); return UnpackedObject.getSize(in, id, curs);
} finally {
in.close();
}
} catch (FileNotFoundException noFile) { } catch (FileNotFoundException noFile) {
if (f.exists()) {
throw noFile;
}
unpackedObjectCache.remove(id); unpackedObjectCache.remove(id);
return -1; return -1;
} }
@ -561,7 +558,11 @@ public class ObjectDirectory extends FileObjectDatabase {
// Assume the pack is corrupted, and remove it from the list. // Assume the pack is corrupted, and remove it from the list.
removePack(p); removePack(p);
} else if (e instanceof FileNotFoundException) { } else if (e instanceof FileNotFoundException) {
if (p.getPackFile().exists()) {
warnTmpl = JGitText.get().packInaccessible;
} else {
warnTmpl = JGitText.get().packWasDeleted; warnTmpl = JGitText.get().packWasDeleted;
}
removePack(p); removePack(p);
} else if (FileUtils.isStaleFileHandle(e)) { } else if (FileUtils.isStaleFileHandle(e)) {
warnTmpl = JGitText.get().packHandleIsStale; warnTmpl = JGitText.get().packHandleIsStale;

8
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/RefDirectory.java

@ -788,6 +788,9 @@ public class RefDirectory extends RefDatabase {
new DigestInputStream(new FileInputStream(packedRefsFile), new DigestInputStream(new FileInputStream(packedRefsFile),
digest), CHARSET)); digest), CHARSET));
} catch (FileNotFoundException noPackedRefs) { } catch (FileNotFoundException noPackedRefs) {
if (packedRefsFile.exists()) {
throw noPackedRefs;
}
// Ignore it and leave the new list empty. // Ignore it and leave the new list empty.
return PackedRefList.NO_PACKED_REFS; return PackedRefList.NO_PACKED_REFS;
} }
@ -944,7 +947,10 @@ public class RefDirectory extends RefDatabase {
try { try {
buf = IO.readSome(path, limit); buf = IO.readSome(path, limit);
} catch (FileNotFoundException noFile) { } catch (FileNotFoundException noFile) {
return null; // doesn't exist; not a reference. if (path.exists() && path.isFile()) {
throw noFile;
}
return null; // doesn't exist or no file; not a reference.
} }
int n = buf.length; int n = buf.length;

6
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/ReflogReaderImpl.java

@ -96,6 +96,9 @@ class ReflogReaderImpl implements ReflogReader {
try { try {
log = IO.readFully(logName); log = IO.readFully(logName);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
if (logName.exists()) {
throw e;
}
return null; return null;
} }
@ -118,6 +121,9 @@ class ReflogReaderImpl implements ReflogReader {
try { try {
log = IO.readFully(logName); log = IO.readFully(logName);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
if (logName.exists()) {
throw e;
}
return Collections.emptyList(); return Collections.emptyList();
} }

3
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/UnpackedObject.java

@ -399,6 +399,9 @@ public class UnpackedObject {
try { try {
in = buffer(new FileInputStream(path)); in = buffer(new FileInputStream(path));
} catch (FileNotFoundException gone) { } catch (FileNotFoundException gone) {
if (path.exists()) {
throw gone;
}
// If the loose file no longer exists, it may have been // If the loose file no longer exists, it may have been
// moved into a pack file in the mean time. Try again // moved into a pack file in the mean time. Try again
// to locate the object. // to locate the object.

6
org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java

@ -1590,6 +1590,9 @@ public abstract class Repository implements AutoCloseable {
try { try {
return RawParseUtils.decode(IO.readFully(mergeMsgFile)); return RawParseUtils.decode(IO.readFully(mergeMsgFile));
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
if (mergeMsgFile.exists()) {
throw e;
}
// the file has disappeared in the meantime ignore it // the file has disappeared in the meantime ignore it
return null; return null;
} }
@ -1621,6 +1624,9 @@ public abstract class Repository implements AutoCloseable {
byte[] raw = IO.readFully(file); byte[] raw = IO.readFully(file);
return raw.length > 0 ? raw : null; return raw.length > 0 ? raw : null;
} catch (FileNotFoundException notFound) { } catch (FileNotFoundException notFound) {
if (file.exists()) {
throw notFound;
}
return null; return null;
} }
} }

3
org.eclipse.jgit/src/org/eclipse/jgit/storage/file/FileBasedConfig.java

@ -163,6 +163,9 @@ public class FileBasedConfig extends StoredConfig {
hash = newHash; hash = newHash;
} }
} catch (FileNotFoundException noFile) { } catch (FileNotFoundException noFile) {
if (configFile.exists()) {
throw noFile;
}
clear(); clear();
snapshot = newSnapshot; snapshot = newSnapshot;
} catch (IOException e) { } catch (IOException e) {

Loading…
Cancel
Save