// Get the object we're going to delete.
final HashedFileObject hfo = repo.getObject(key);
// Make sure the object exists -- if there was no object
// with the given key in the repo then it does not exist.
if(hfo == null) {
throw new ObjectNotFoundException("Object " +
"not found (id=" + repo.getRepoId() + ", key=" +
key + ")");
}
// Now attempt to grab an exclusive write lock on the
// file object do delete. And, attempt to follow through
// on the physical delete from the platters.
return new ReentrantReadWriteEntityLock<HashedFileObject>(hfo) {
@Override
public HashedFileObject transaction() throws Exception {
final String eTag = hfo.getFirstHeader(ETAG);
// If we have an incoming If-Match, we need to
// compare that against the current HFO before we
// attempt to delete. If the If-Match ETag does not
// match, fail.
if(ifMatch != null && eTag != null) {
// OK, we have an incoming If-Match ETag, use it.
if(!ifMatch.equals(eTag)) {
throw new ObjectConflictException("Failed " +
"to delete HFO; incoming If-Match " +
"ETag does not match (hfo=" +
hfo.getName() + ", etag=" +
eTag + ", if-match=" + ifMatch + ")");
}
}
// OK, we either didn't have an incoming If-Match
// to check, or we did and it passed -- grab a
// pointer to the actual File on disk.
final File hfoFile = getCanonicalObject(repo.getFile(),
getSHA256Hash(key), false).getFile();
// Validate that the file exists before we attempt
// to physically remove it from disk.
if(hfoFile.exists()) {
if(deleteQuietly(hfoFile)) {
repo.deleteObject(key);
} else {
throw new ObjectDeletionException("Failed " +
"to delete object from disk (file=" +
hfoFile.getAbsolutePath() +
", id=" + repo.getRepoId() + ")");
}
} else {
// So the object was in the index in memory, but
// was not found on disk. Hm, probably a bigger
// issue.
throw new ObjectNotFoundException("Object " +
"file on disk not found (file=" +
hfoFile.getAbsolutePath() + ", id=" +
repo.getRepoId() + ", key=" +
key + ")");
}