Browse Source

ObjectDirectory: Explicitly handle NoSuchFileException

On the first attempt to move the temp file, NoSuchFileException can
be raised if the destination folder does not exist. Instead of handling
this implicitly in the catch of IOException and then continuing to
create the destination folder and try again, explicitly catch it and
create the destination folder. If any other IOException occurs, treat
it as an unexpected error and return FAILURE.

Subsequently, on the second attempt to move the temp file, if ANY kind
of IOException occurs, also consider this an unexpected error and
return FAILURE.

In both catch blocks for IOException, add logging at ERROR level.

Change-Id: I9de9ee3d2b368be36e02ee1c0daf8e844f7e46c8
Signed-off-by: David Pursehouse <david.pursehouse@gmail.com>
stable-5.7
David Pursehouse 5 years ago
parent
commit
949ee670c6
  1. 32
      org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/ObjectDirectory.java

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

@ -21,6 +21,7 @@ import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.nio.file.AtomicMoveNotSupportedException; import java.nio.file.AtomicMoveNotSupportedException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.ArrayList; import java.util.ArrayList;
@ -695,15 +696,19 @@ public class ObjectDirectory extends FileObjectDatabase {
LOG.error(e.getMessage(), e); LOG.error(e.getMessage(), e);
FileUtils.delete(tmp, FileUtils.RETRY); FileUtils.delete(tmp, FileUtils.RETRY);
return InsertLooseObjectResult.FAILURE; return InsertLooseObjectResult.FAILURE;
} catch (NoSuchFileException e) {
// It's possible the directory doesn't exist yet as the object
// directories are always lazily created. Note that we try the
// rename/move first as the directory likely does exist.
// Create the directory
FileUtils.mkdir(dst.getParentFile(), true);
} catch (IOException e) { } catch (IOException e) {
// ignore LOG.error(e.getMessage(), e);
FileUtils.delete(tmp, FileUtils.RETRY);
return InsertLooseObjectResult.FAILURE;
} }
// Maybe the directory doesn't exist yet as the object
// directories are always lazily created. Note that we
// try the rename first as the directory likely does exist.
//
FileUtils.mkdir(dst.getParentFile(), true);
try { try {
Files.move(FileUtils.toPath(tmp), FileUtils.toPath(dst), Files.move(FileUtils.toPath(tmp), FileUtils.toPath(dst),
StandardCopyOption.ATOMIC_MOVE); StandardCopyOption.ATOMIC_MOVE);
@ -711,22 +716,11 @@ public class ObjectDirectory extends FileObjectDatabase {
unpackedObjectCache.add(id); unpackedObjectCache.add(id);
return InsertLooseObjectResult.INSERTED; return InsertLooseObjectResult.INSERTED;
} catch (IOException e) { } catch (IOException e) {
LOG.debug(e.getMessage(), e); LOG.error(e.getMessage(), e);
}
if (!createDuplicate && has(id)) {
FileUtils.delete(tmp, FileUtils.RETRY);
return InsertLooseObjectResult.EXISTS_PACKED;
}
// The object failed to be renamed into its proper
// location and it doesn't exist in the repository
// either. We really don't know what went wrong, so
// fail.
//
FileUtils.delete(tmp, FileUtils.RETRY); FileUtils.delete(tmp, FileUtils.RETRY);
return InsertLooseObjectResult.FAILURE; return InsertLooseObjectResult.FAILURE;
} }
}
boolean searchPacksAgain(PackList old) { boolean searchPacksAgain(PackList old) {
// Whether to trust the pack folder's modification time. If set // Whether to trust the pack folder's modification time. If set

Loading…
Cancel
Save