final String name = ObjectId.fromRaw(d.digest()).name();
final File packDir = new File(repo.getObjectsDirectory(), "pack");
final File finalPack = new File(packDir, "pack-" + name + ".pack");
final File finalIdx = new File(packDir, "pack-" + name + ".idx");
final PackLock keep = new PackLock(finalPack, repo.getFS());
if (!packDir.exists() && !packDir.mkdir() && !packDir.exists()) {
// The objects/pack directory isn't present, and we are unable
// to create it. There is no way to move this pack in.
//
cleanupTemporaryFiles();
throw new IOException(MessageFormat.format(JGitText.get().cannotCreateDirectory, packDir.getAbsolutePath()));
}
if (finalPack.exists()) {
// If the pack is already present we should never replace it.
//
cleanupTemporaryFiles();
return null;
}
if (lockMessage != null) {
// If we have a reason to create a keep file for this pack, do
// so, or fail fast and don't put the pack in place.
//
try {
if (!keep.lock(lockMessage))
throw new IOException(MessageFormat.format(JGitText.get().cannotLockPackIn, finalPack));
} catch (IOException e) {
cleanupTemporaryFiles();
throw e;
}
}
if (!dstPack.renameTo(finalPack)) {
cleanupTemporaryFiles();
keep.unlock();
throw new IOException(MessageFormat.format(JGitText.get().cannotMovePackTo, finalPack));
}
if (!dstIdx.renameTo(finalIdx)) {
cleanupTemporaryFiles();
keep.unlock();
if (!finalPack.delete())
finalPack.deleteOnExit();
throw new IOException(MessageFormat.format(JGitText.get().cannotMoveIndexTo, finalIdx));
}
try {
repo.openPack(finalPack, finalIdx);
} catch (IOException err) {
keep.unlock();
finalPack.delete();
finalIdx.delete();
throw err;
}