Files.deleteIfExists(hiddenTarget.toPath());
}
catch (IOException e1) {
// best effort to delete, we already have what to throw
}
throw new LocalStorageException(String.format(
"Got exception during storing on path \"%s\" (while writing to hiddenTarget: \"%s\")",
item.getRepositoryItemUid().toString(), hiddenTarget.getAbsolutePath()), e);
}
// NEXUS-4550: Part Two, moving the "hidden" (temp) file to final location
// In case of error cleaning up both files
// Locking is needed, AbstractRepository got shared lock only for destination
// NEXUS-4550: FSPeer is the one that handles the rename in case of FS LS,
// so we need here to claim exclusive lock on actual UID to perform the rename
final RepositoryItemUidLock uidLock = item.getRepositoryItemUid().getLock();
uidLock.lock(Action.create);
try {
handleRenameOperation(hiddenTarget, target);
target.setLastModified(item.getModified());
}
catch (IOException e) {
// if we ARE NOT handling attributes, do proper cleanup in case of IOEx
// if we ARE handling attributes, leave backups in case of IOEx
final boolean isCleanupNeeded =
!item.getRepositoryItemUid().getBooleanAttributeValue(IsItemAttributeMetacontentAttribute.class);
if (target != null && (isCleanupNeeded ||
// NEXUS-4871 prevent zero length/corrupt files
target.length() == 0)) {
try {
Files.delete(target.toPath());
}
catch (IOException e1) {
log.warn("Could not delete file: " + target.getAbsolutePath(), e);
}
}
if (hiddenTarget != null && (isCleanupNeeded ||
// NEXUS-4871 prevent zero length/corrupt files
hiddenTarget.length() == 0)) {
try {
Files.delete(hiddenTarget.toPath());
}
catch (IOException e1) {
log.warn("Could not delete file: " + target.getAbsolutePath(), e);
}
}
if (!isCleanupNeeded) {
log.warn(
"No cleanup done for error that happened while trying to save attibutes of item {}, the backup is left as {}!",
item.getRepositoryItemUid().toString(), hiddenTarget.getAbsolutePath());
}
throw new LocalStorageException(String.format(
"Got exception during storing on path \"%s\" (while moving to final destination)",
item.getRepositoryItemUid().toString()), e);
}
finally {
uidLock.unlock();