/// <exception cref="System.IO.IOException"></exception>
internal override FileObjectDatabase.InsertLooseObjectResult InsertUnpackedObject
(FilePath tmp, ObjectId id, bool createDuplicate)
{
// If the object is already in the repository, remove temporary file.
//
if (unpackedObjectCache.IsUnpacked(id))
{
FileUtils.Delete(tmp);
return FileObjectDatabase.InsertLooseObjectResult.EXISTS_LOOSE;
}
if (!createDuplicate && Has(id))
{
FileUtils.Delete(tmp);
return FileObjectDatabase.InsertLooseObjectResult.EXISTS_PACKED;
}
tmp.SetReadOnly();
FilePath dst = FileFor(id);
if (dst.Exists())
{
// We want to be extra careful and avoid replacing an object
// that already exists. We can't be sure renameTo() would
// fail on all platforms if dst exists, so we check first.
//
FileUtils.Delete(tmp);
return FileObjectDatabase.InsertLooseObjectResult.EXISTS_LOOSE;
}
if (tmp.RenameTo(dst))
{
unpackedObjectCache.Add(id);
return FileObjectDatabase.InsertLooseObjectResult.INSERTED;
}
// 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.
//
dst.GetParentFile().Mkdir();
if (tmp.RenameTo(dst))
{
unpackedObjectCache.Add(id);
return FileObjectDatabase.InsertLooseObjectResult.INSERTED;
}
if (!createDuplicate && Has(id))
{
FileUtils.Delete(tmp);
return FileObjectDatabase.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);
return FileObjectDatabase.InsertLooseObjectResult.FAILURE;
}