use of java.nio.file.FileAlreadyExistsException in project certmgr by hdecarne.
the class CertExportController method createUniqueFile.
private Path createUniqueFile(Path dir, String namePattern) throws IOException {
int nameIndex = 1;
Path lastTestName = null;
Path uniqueName = null;
while (uniqueName == null) {
Path testName = dir.resolve(String.format(namePattern, nameIndex));
if (testName.equals(lastTestName)) {
throw new IllegalArgumentException(namePattern);
}
lastTestName = testName;
try {
Files.createFile(testName, FileAttributes.userDirectoryDefault(dir));
uniqueName = testName;
} catch (FileAlreadyExistsException e) {
Exceptions.ignore(e);
nameIndex++;
}
}
return uniqueName;
}
use of java.nio.file.FileAlreadyExistsException in project n4js by eclipse.
the class FileCopier method preVisitDirectory.
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
CopyOption[] options = new CopyOption[0];
Path newdir = target.resolve(source.relativize(dir));
try {
Files.copy(dir, newdir, options);
} catch (FileAlreadyExistsException e) {
// ignore
} catch (IOException e) {
logError(newdir, e);
return SKIP_SUBTREE;
}
return CONTINUE;
}
use of java.nio.file.FileAlreadyExistsException in project Singularity by HubSpot.
the class ArtifactManager method copy.
public void copy(Path source, Path destination, String destinationFilename) {
log.info("Copying {} to {}", source, destination);
Path destinationPath = destination.resolve(destinationFilename);
try {
Files.createDirectories(destination);
Files.copy(source, destinationPath);
} catch (FileAlreadyExistsException e) {
if (!calculateMd5sum(source).equals(calculateMd5sum(destinationPath))) {
throw new RuntimeException(e);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of java.nio.file.FileAlreadyExistsException in project cryptofs by cryptomator.
the class CryptoFileSystemImpl method createDirectory.
void createDirectory(CryptoPath cleartextDir, FileAttribute<?>... attrs) throws IOException {
CryptoPath cleartextParentDir = cleartextDir.getParent();
if (cleartextParentDir == null) {
return;
}
Path ciphertextParentDir = cryptoPathMapper.getCiphertextDirPath(cleartextParentDir);
if (!Files.exists(ciphertextParentDir)) {
throw new NoSuchFileException(cleartextParentDir.toString());
}
Path ciphertextFile = cryptoPathMapper.getCiphertextFilePath(cleartextDir, CiphertextFileType.FILE);
if (Files.exists(ciphertextFile)) {
throw new FileAlreadyExistsException(cleartextDir.toString());
}
Path ciphertextDirFile = cryptoPathMapper.getCiphertextFilePath(cleartextDir, CiphertextFileType.DIRECTORY);
Directory ciphertextDir = cryptoPathMapper.getCiphertextDir(cleartextDir);
// atomically check for FileAlreadyExists and create otherwise:
try (FileChannel channel = FileChannel.open(ciphertextDirFile, EnumSet.of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE), attrs)) {
channel.write(ByteBuffer.wrap(ciphertextDir.dirId.getBytes(UTF_8)));
}
// create dir if and only if the dirFile has been created right now (not if it has been created before):
try {
Files.createDirectories(ciphertextDir.path);
} catch (IOException e) {
// make sure there is no orphan dir file:
Files.delete(ciphertextDirFile);
dirIdProvider.delete(ciphertextDirFile);
throw e;
}
}
use of java.nio.file.FileAlreadyExistsException in project incubator-pulsar by apache.
the class FunctionActioner method startFunction.
private void startFunction(FunctionRuntimeInfo functionRuntimeInfo) throws Exception {
Function.Instance instance = functionRuntimeInfo.getFunctionInstance();
FunctionMetaData functionMetaData = instance.getFunctionMetaData();
log.info("Starting function {} - {} ...", functionMetaData.getFunctionConfig().getName(), instance.getInstanceId());
File pkgDir = new File(workerConfig.getDownloadDirectory(), getDownloadPackagePath(functionMetaData));
pkgDir.mkdirs();
int instanceId = functionRuntimeInfo.getFunctionInstance().getInstanceId();
File pkgFile = new File(pkgDir, new File(FunctionConfigUtils.getDownloadFileName(functionMetaData.getFunctionConfig())).getName());
if (!pkgFile.exists()) {
// download only when the package file doesn't exist
File tempPkgFile;
while (true) {
tempPkgFile = new File(pkgDir, pkgFile.getName() + "." + instanceId + "." + UUID.randomUUID().toString());
if (!tempPkgFile.exists() && tempPkgFile.createNewFile()) {
break;
}
}
try {
log.info("Function package file {} will be downloaded from {}", tempPkgFile, functionMetaData.getPackageLocation());
Utils.downloadFromBookkeeper(dlogNamespace, new FileOutputStream(tempPkgFile), functionMetaData.getPackageLocation().getPackagePath());
// this ensures one instance will successfully download the package.
try {
Files.createLink(Paths.get(pkgFile.toURI()), Paths.get(tempPkgFile.toURI()));
log.info("Function package file is linked from {} to {}", tempPkgFile, pkgFile);
} catch (FileAlreadyExistsException faee) {
// file already exists
log.warn("Function package has been downloaded from {} and saved at {}", functionMetaData.getPackageLocation(), pkgFile);
}
} finally {
tempPkgFile.delete();
}
}
InstanceConfig instanceConfig = new InstanceConfig();
instanceConfig.setFunctionConfig(functionMetaData.getFunctionConfig());
// TODO: set correct function id and version when features implemented
instanceConfig.setFunctionId(UUID.randomUUID().toString());
instanceConfig.setFunctionVersion(UUID.randomUUID().toString());
instanceConfig.setInstanceId(String.valueOf(instanceId));
instanceConfig.setMaxBufferedTuples(1024);
RuntimeSpawner runtimeSpawner = new RuntimeSpawner(instanceConfig, pkgFile.getAbsolutePath(), runtimeFactory, workerConfig.getInstanceLivenessCheckFreqMs());
functionRuntimeInfo.setRuntimeSpawner(runtimeSpawner);
runtimeSpawner.start();
}
Aggregations