use of java.nio.file.AtomicMoveNotSupportedException in project compss by bsc-wdc.
the class NIOWorker method askForFile.
private void askForFile(NIOParam param, int index, TransferringTask tt) {
WORKER_LOGGER.debug(" - " + (String) param.getValue() + " registered as file.");
boolean locationsInHost = false;
boolean askTransfer = false;
// Try if parameter is in the host
WORKER_LOGGER.debug(" - Checking if file " + (String) param.getValue() + " exists.");
File f = new File(param.getValue().toString());
if (!f.exists()) {
// Try if any of the locations is in the same host
WORKER_LOGGER.debug(" - Checking if " + (String) param.getValue() + " exists in worker");
NIOURI loc = param.getData().getURIinHost(host);
if (loc != null) {
// Data is already present at host
WORKER_LOGGER.debug(" - Parameter " + index + "(" + (String) param.getValue() + ") found at host.");
try {
File source = new File(loc.getPath());
File target = new File(param.getValue().toString());
if (param.isPreserveSourceData()) {
WORKER_LOGGER.debug(" - Parameter " + index + "(" + (String) param.getValue() + ") preserves sources. COPYING");
WORKER_LOGGER.debug(" Source: " + source);
WORKER_LOGGER.debug(" Target: " + target);
// if (!source.exists() && (Lang.valueOf(getLang().toUpperCase()) != Lang.C)) {
if (!source.exists()) {
WORKER_LOGGER.debug("source does not exist, preserve data");
WORKER_LOGGER.debug("lang is " + getLang());
if (getLang().toUpperCase() != "C") {
WORKER_LOGGER.debug("[ERROR] File " + loc.getPath() + " does not exist but it could be an object in cache. Ignoring.");
}
// TODO The file to be copied needs to be serialized to a file from cache (or serialize from
// memory to memory
// if possible with a specific function
} else {
WORKER_LOGGER.debug("before copy");
Files.copy(source.toPath(), target.toPath());
WORKER_LOGGER.debug("after copy");
}
} else {
WORKER_LOGGER.debug(" - Parameter " + index + "(" + (String) param.getValue() + ") erases sources. MOVING");
WORKER_LOGGER.debug(" Source: " + source);
WORKER_LOGGER.debug(" Target: " + target);
if (!source.exists()) {
WORKER_LOGGER.debug("source does not exist, no preserve data");
WORKER_LOGGER.debug("lang is " + getLang() + ", in uppercase is " + getLang().toUpperCase());
if (getLang().toUpperCase() != "C") {
WORKER_LOGGER.debug("File " + loc.getPath() + " does not exist but it could be an object in cache. Ignoring.");
}
} else {
try {
Files.move(source.toPath(), target.toPath(), StandardCopyOption.ATOMIC_MOVE);
} catch (AtomicMoveNotSupportedException amnse) {
WORKER_LOGGER.warn("WARN: AtomicMoveNotSupportedException. File cannot be atomically moved. Trying to move without atomic");
Files.move(source.toPath(), target.toPath());
}
}
}
locationsInHost = true;
} catch (IOException ioe) {
WORKER_LOGGER.error("IOException", ioe);
}
}
if (!locationsInHost) {
// We must transfer the file
askTransfer = true;
}
} else {
// Check if it is not currently transferred
if (getDataRequests(param.getData().getName()) != null) {
askTransfer = true;
}
}
// Request the transfer if needed
askForTransfer(askTransfer, param, index, tt);
}
use of java.nio.file.AtomicMoveNotSupportedException in project opencast by opencast.
the class WorkingFileRepositoryImpl method put.
/**
* {@inheritDoc}
*
* @see org.opencastproject.workingfilerepository.api.WorkingFileRepository#put(java.lang.String, java.lang.String,
* java.lang.String, java.io.InputStream)
*/
public URI put(String mediaPackageID, String mediaPackageElementID, String filename, InputStream in) throws IOException {
checkPathSafe(mediaPackageID);
checkPathSafe(mediaPackageElementID);
File dir = getElementDirectory(mediaPackageID, mediaPackageElementID);
File[] filesToDelete = null;
if (dir.exists()) {
filesToDelete = dir.listFiles();
} else {
logger.debug("Attempting to create a new directory at {}", dir.getAbsolutePath());
FileUtils.forceMkdir(dir);
}
// Destination files
File f = new File(dir, PathSupport.toSafeName(filename));
File md5File = getMd5File(f);
// Temporary files while adding
File fTmp = null;
File md5FileTmp = null;
if (f.exists()) {
logger.debug("Updating file {}", f.getAbsolutePath());
} else {
logger.debug("Adding file {}", f.getAbsolutePath());
}
FileOutputStream out = null;
try {
fTmp = File.createTempFile(f.getName(), ".tmp", dir);
md5FileTmp = File.createTempFile(md5File.getName(), ".tmp", dir);
logger.trace("Writing to new temporary file {}", fTmp.getAbsolutePath());
out = new FileOutputStream(fTmp);
// Wrap the input stream and copy the input stream to the file
MessageDigest messageDigest = null;
DigestInputStream dis = null;
try {
messageDigest = MessageDigest.getInstance("MD5");
dis = new DigestInputStream(in, messageDigest);
IOUtils.copy(dis, out);
} catch (NoSuchAlgorithmException e1) {
logger.error("Unable to create md5 message digest");
}
// Store the hash
String md5 = Checksum.convertToHex(dis.getMessageDigest().digest());
try {
FileUtils.writeStringToFile(md5FileTmp, md5);
} catch (IOException e) {
FileUtils.deleteQuietly(md5FileTmp);
throw e;
} finally {
IOUtils.closeQuietly(dis);
}
} catch (IOException e) {
IOUtils.closeQuietly(out);
FileUtils.deleteQuietly(dir);
throw e;
} finally {
IOUtils.closeQuietly(out);
IOUtils.closeQuietly(in);
}
// Rename temporary files to the final version atomically
try {
Files.move(md5FileTmp.toPath(), md5File.toPath(), StandardCopyOption.ATOMIC_MOVE);
Files.move(fTmp.toPath(), f.toPath(), StandardCopyOption.ATOMIC_MOVE);
} catch (AtomicMoveNotSupportedException e) {
logger.trace("Atomic move not supported by this filesystem: using replace instead");
Files.move(md5FileTmp.toPath(), md5File.toPath(), StandardCopyOption.REPLACE_EXISTING);
Files.move(fTmp.toPath(), f.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
// Clean up any other files
if (filesToDelete != null && filesToDelete.length > 0) {
for (File fileToDelete : filesToDelete) {
if (!fileToDelete.equals(f) && !fileToDelete.equals(md5File)) {
logger.trace("delete {}", fileToDelete.getAbsolutePath());
if (!fileToDelete.delete()) {
throw new IllegalStateException("Unable to delete file: " + fileToDelete.getAbsolutePath());
}
}
}
}
return getURI(mediaPackageID, mediaPackageElementID, filename);
}
use of java.nio.file.AtomicMoveNotSupportedException in project cryptofs by cryptomator.
the class CryptoFileSystemImpl method move.
void move(CryptoPath cleartextSource, CryptoPath cleartextTarget, CopyOption... options) throws IOException {
if (cleartextSource.equals(cleartextTarget)) {
return;
}
Path ciphertextSourceFile = cryptoPathMapper.getCiphertextFilePath(cleartextSource, CiphertextFileType.FILE);
Path ciphertextSourceDirFile = cryptoPathMapper.getCiphertextFilePath(cleartextSource, CiphertextFileType.DIRECTORY);
if (Files.exists(ciphertextSourceFile)) {
// FILE:
Path ciphertextTargetFile = cryptoPathMapper.getCiphertextFilePath(cleartextTarget, CiphertextFileType.FILE);
Files.move(ciphertextSourceFile, ciphertextTargetFile, options);
} else if (Files.exists(ciphertextSourceDirFile)) {
// DIRECTORY:
Path ciphertextTargetDirFile = cryptoPathMapper.getCiphertextFilePath(cleartextTarget, CiphertextFileType.DIRECTORY);
if (!ArrayUtils.contains(options, StandardCopyOption.REPLACE_EXISTING)) {
// try to move, don't replace:
Files.move(ciphertextSourceDirFile, ciphertextTargetDirFile, options);
} else if (ArrayUtils.contains(options, StandardCopyOption.ATOMIC_MOVE)) {
// replace atomically (impossible):
assert ArrayUtils.contains(options, StandardCopyOption.REPLACE_EXISTING);
throw new AtomicMoveNotSupportedException(cleartextSource.toString(), cleartextTarget.toString(), "Replacing directories during move requires non-atomic status checks.");
} else {
// move and replace (if dir is empty):
assert ArrayUtils.contains(options, StandardCopyOption.REPLACE_EXISTING);
assert !ArrayUtils.contains(options, StandardCopyOption.ATOMIC_MOVE);
if (Files.exists(ciphertextTargetDirFile)) {
Path ciphertextTargetDir = cryptoPathMapper.getCiphertextDirPath(cleartextTarget);
try (DirectoryStream<Path> ds = Files.newDirectoryStream(ciphertextTargetDir)) {
if (ds.iterator().hasNext()) {
throw new DirectoryNotEmptyException(cleartextTarget.toString());
}
}
Files.delete(ciphertextTargetDir);
}
Files.move(ciphertextSourceDirFile, ciphertextTargetDirFile, options);
}
dirIdProvider.move(ciphertextSourceDirFile, ciphertextTargetDirFile);
} else {
throw new NoSuchFileException(cleartextSource.toString());
}
}
use of java.nio.file.AtomicMoveNotSupportedException in project Terasology by MovingBlocks.
the class SaveTransaction method renameMergeFolder.
private void renameMergeFolder() throws IOException {
Path directoryForUnfinishedFiles = storagePathProvider.getUnfinishedSaveTransactionPath();
Path directoryForFinishedFiles = storagePathProvider.getUnmergedChangesPath();
try {
Files.move(directoryForUnfinishedFiles, directoryForFinishedFiles, StandardCopyOption.ATOMIC_MOVE);
} catch (AtomicMoveNotSupportedException e) {
logger.warn("Atomic rename of merge folder was not possible, doing it non atomically...");
Files.move(directoryForUnfinishedFiles, directoryForFinishedFiles);
}
}
use of java.nio.file.AtomicMoveNotSupportedException in project crate by crate.
the class NodeEnvironment method ensureAtomicMoveSupported.
/**
* This method tries to write an empty file and moves it using an atomic move operation.
* This method throws an {@link IllegalStateException} if this operation is
* not supported by the filesystem. This test is executed on each of the data directories.
* This method cleans up all files even in the case of an error.
*/
private static void ensureAtomicMoveSupported(final NodePath[] nodePaths) throws IOException {
for (NodePath nodePath : nodePaths) {
assert Files.isDirectory(nodePath.path) : nodePath.path + " is not a directory";
final Path src = nodePath.path.resolve(TEMP_FILE_NAME + ".tmp");
final Path target = nodePath.path.resolve(TEMP_FILE_NAME + ".final");
try {
Files.deleteIfExists(src);
Files.createFile(src);
Files.move(src, target, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);
} catch (AtomicMoveNotSupportedException ex) {
throw new IllegalStateException("atomic_move is not supported by the filesystem on path [" + nodePath.path + "] atomic_move is required for cratedb to work correctly.", ex);
} finally {
try {
Files.deleteIfExists(src);
} finally {
Files.deleteIfExists(target);
}
}
}
}
Aggregations