GC: Use PackFile to de-dup logic

GC has several places where it tries to build files names for packs that
we can use the PackFile class for instead.

Change-Id: I99e5ceff9050f8583368fca35279251955e4644d
Signed-off-by: Nasser Grainawi <quic_nasserg@quicinc.com>
This commit is contained in:
Nasser Grainawi 2021-02-18 17:36:49 -07:00 committed by Matthias Sohn
parent 971dafd302
commit dc7f0bfee9
1 changed files with 16 additions and 25 deletions

View File

@ -346,7 +346,7 @@ private void deleteOldPacks(Collection<Pack> oldPacks,
if (shouldLoosen) { if (shouldLoosen) {
loosen(inserter, reader, oldPack, ids); loosen(inserter, reader, oldPack, ids);
} }
prunePack(oldName); prunePack(oldPack.getPackFile());
} }
} }
@ -360,19 +360,17 @@ private void deleteOldPacks(Collection<Pack> oldPacks,
* moves the pack file to the preserved directory * moves the pack file to the preserved directory
* *
* @param packFile * @param packFile
* @param packName
* @param ext
* @param deleteOptions * @param deleteOptions
* @throws IOException * @throws IOException
*/ */
private void removeOldPack(File packFile, String packName, PackExt ext, private void removeOldPack(PackFile packFile, int deleteOptions)
int deleteOptions) throws IOException { throws IOException {
if (pconfig.isPreserveOldPacks()) { if (pconfig.isPreserveOldPacks()) {
File oldPackDir = repo.getObjectDatabase().getPreservedDirectory(); File oldPackDir = repo.getObjectDatabase().getPreservedDirectory();
FileUtils.mkdir(oldPackDir, true); FileUtils.mkdir(oldPackDir, true);
String oldPackName = "pack-" + packName + ".old-" + ext.getExtension(); //$NON-NLS-1$ //$NON-NLS-2$ PackFile oldPackFile = packFile
File oldPackFile = new File(oldPackDir, oldPackName); .createPreservedForDirectory(oldPackDir);
FileUtils.rename(packFile, oldPackFile); FileUtils.rename(packFile, oldPackFile);
} else { } else {
FileUtils.delete(packFile, deleteOptions); FileUtils.delete(packFile, deleteOptions);
@ -401,27 +399,21 @@ private void prunePreserved() {
* ".index" file and when failing to delete the ".pack" file we are left * ".index" file and when failing to delete the ".pack" file we are left
* with a ".pack" file without a ".index" file. * with a ".pack" file without a ".index" file.
* *
* @param packName * @param packFile
*/ */
private void prunePack(String packName) { private void prunePack(PackFile packFile) {
PackExt[] extensions = PackExt.values();
try { try {
// Delete the .pack file first and if this fails give up on deleting // Delete the .pack file first and if this fails give up on deleting
// the other files // the other files
int deleteOptions = FileUtils.RETRY | FileUtils.SKIP_MISSING; int deleteOptions = FileUtils.RETRY | FileUtils.SKIP_MISSING;
for (PackExt ext : extensions) removeOldPack(packFile.create(PackExt.PACK), deleteOptions);
if (PackExt.PACK.equals(ext)) {
File f = nameFor(packName, "." + ext.getExtension()); //$NON-NLS-1$
removeOldPack(f, packName, ext, deleteOptions);
break;
}
// The .pack file has been deleted. Delete as many as the other // The .pack file has been deleted. Delete as many as the other
// files as you can. // files as you can.
deleteOptions |= FileUtils.IGNORE_ERRORS; deleteOptions |= FileUtils.IGNORE_ERRORS;
for (PackExt ext : extensions) { for (PackExt ext : PackExt.values()) {
if (!PackExt.PACK.equals(ext)) { if (!PackExt.PACK.equals(ext)) {
File f = nameFor(packName, "." + ext.getExtension()); //$NON-NLS-1$ removeOldPack(packFile.create(ext), deleteOptions);
removeOldPack(f, packName, ext, deleteOptions);
} }
} }
} catch (IOException e) { } catch (IOException e) {
@ -1218,7 +1210,7 @@ private Pack writePack(@NonNull Set<? extends ObjectId> want,
} }
// rename the temporary files to real files // rename the temporary files to real files
File realPack = nameFor(id, ".pack"); //$NON-NLS-1$ File realPack = nameFor(id, PackExt.PACK);
repo.getObjectDatabase().closeAllPackHandles(realPack); repo.getObjectDatabase().closeAllPackHandles(realPack);
tmpPack.setReadOnly(); tmpPack.setReadOnly();
@ -1228,8 +1220,7 @@ private Pack writePack(@NonNull Set<? extends ObjectId> want,
File tmpExt = tmpEntry.getValue(); File tmpExt = tmpEntry.getValue();
tmpExt.setReadOnly(); tmpExt.setReadOnly();
File realExt = nameFor(id, File realExt = nameFor(id, tmpEntry.getKey());
"." + tmpEntry.getKey().getExtension()); //$NON-NLS-1$
try { try {
FileUtils.rename(tmpExt, realExt, FileUtils.rename(tmpExt, realExt,
StandardCopyOption.ATOMIC_MOVE); StandardCopyOption.ATOMIC_MOVE);
@ -1275,9 +1266,9 @@ private Pack writePack(@NonNull Set<? extends ObjectId> want,
} }
} }
private File nameFor(String name, String ext) { private PackFile nameFor(String name, PackExt ext) {
File packdir = repo.getObjectDatabase().getPackDirectory(); return new PackFile(repo.getObjectDatabase().getPackDirectory(),
return new File(packdir, "pack-" + name + ext); //$NON-NLS-1$ "pack-" + name).create(ext); //$NON-NLS-1$
} }
private void checkCancelled() throws CancelledException { private void checkCancelled() throws CancelledException {