Search in sources :

Example 71 with FileAlreadyExistsException

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;
}
Also used : Path(java.nio.file.Path) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException)

Example 72 with FileAlreadyExistsException

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;
}
Also used : Path(java.nio.file.Path) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) StandardCopyOption(java.nio.file.StandardCopyOption) CopyOption(java.nio.file.CopyOption) IOException(java.io.IOException)

Example 73 with FileAlreadyExistsException

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);
    }
}
Also used : Path(java.nio.file.Path) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) IOException(java.io.IOException)

Example 74 with FileAlreadyExistsException

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;
    }
}
Also used : Path(java.nio.file.Path) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) FileChannel(java.nio.channels.FileChannel) NoSuchFileException(java.nio.file.NoSuchFileException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) Directory(org.cryptomator.cryptofs.CryptoPathMapper.Directory)

Example 75 with FileAlreadyExistsException

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();
}
Also used : FunctionMetaData(org.apache.pulsar.functions.proto.Function.FunctionMetaData) Function(org.apache.pulsar.functions.proto.Function) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) InstanceConfig(org.apache.pulsar.functions.instance.InstanceConfig) FileOutputStream(java.io.FileOutputStream) File(java.io.File) RuntimeSpawner(org.apache.pulsar.functions.runtime.RuntimeSpawner)

Aggregations

FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)104 Path (java.nio.file.Path)49 IOException (java.io.IOException)44 File (java.io.File)24 NoSuchFileException (java.nio.file.NoSuchFileException)22 Test (org.junit.Test)15 FileNotFoundException (java.io.FileNotFoundException)10 FileSystemException (java.nio.file.FileSystemException)9 AccessDeniedException (java.nio.file.AccessDeniedException)8 DirectoryNotEmptyException (java.nio.file.DirectoryNotEmptyException)8 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)8 NotDirectoryException (java.nio.file.NotDirectoryException)7 AtomicMoveNotSupportedException (java.nio.file.AtomicMoveNotSupportedException)5 CopyOption (java.nio.file.CopyOption)5 FileSystem (java.nio.file.FileSystem)5 FileVisitResult (java.nio.file.FileVisitResult)5 MCRPath (org.mycore.datamodel.niofs.MCRPath)5 FileOutputStream (java.io.FileOutputStream)4 InputStream (java.io.InputStream)4 FileSystemLoopException (java.nio.file.FileSystemLoopException)4