Search in sources :

Example 56 with FileAlreadyExistsException

use of java.nio.file.FileAlreadyExistsException in project buck by facebook.

the class ProjectWorkspace method setUp.

/**
   * This will copy the template directory, renaming files named {@code foo.fixture} to {@code foo}
   * in the process. Files whose names end in {@code .expected} will not be copied.
   */
@SuppressWarnings("PMD.EmptyCatchBlock")
public ProjectWorkspace setUp() throws IOException {
    MoreFiles.copyRecursively(templatePath, destPath, BUILD_FILE_RENAME);
    // Stamp the buck-out directory if it exists and isn't stamped already
    try (OutputStream outputStream = new BufferedOutputStream(Channels.newOutputStream(Files.newByteChannel(destPath.resolve(BuckConstant.getBuckOutputPath().resolve(".currentversion")), ImmutableSet.<OpenOption>of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE))))) {
        outputStream.write(BuckVersion.getVersion().getBytes(Charsets.UTF_8));
    } catch (FileAlreadyExistsException | NoSuchFileException e) {
    // If the current version file already exists we don't need to create it
    // If buck-out doesn't exist we don't need to stamp it
    }
    if (Platform.detect() == Platform.WINDOWS) {
        // Hack for symlinks on Windows.
        SimpleFileVisitor<Path> copyDirVisitor = new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
                // On NTFS length of path must be greater than 0 and less than 4096.
                if (attrs.size() > 0 && attrs.size() <= 4096) {
                    String linkTo = new String(Files.readAllBytes(path), UTF_8);
                    Path linkToFile;
                    try {
                        linkToFile = templatePath.resolve(linkTo);
                    } catch (InvalidPathException e) {
                        // link.
                        return FileVisitResult.CONTINUE;
                    }
                    if (Files.isRegularFile(linkToFile)) {
                        Files.copy(linkToFile, path, StandardCopyOption.REPLACE_EXISTING);
                    } else if (Files.isDirectory(linkToFile)) {
                        Files.delete(path);
                        MoreFiles.copyRecursively(linkToFile, path);
                    }
                }
                return FileVisitResult.CONTINUE;
            }
        };
        Files.walkFileTree(destPath, copyDirVisitor);
    }
    if (!Files.exists(getPath(".buckconfig.local"))) {
        manageLocalConfigs = true;
        // Disable the directory cache by default.  Tests that want to enable it can call
        // `enableDirCache` on this object.  Only do this if a .buckconfig.local file does not already
        // exist, however (we assume the test knows what it is doing at that point).
        addBuckConfigLocalOption("cache", "mode", "");
        // Limit the number of threads by default to prevent multiple integration tests running at the
        // same time from creating a quadratic number of threads. Tests can disable this using
        // `disableThreadLimitOverride`.
        addBuckConfigLocalOption("build", "threads", "2");
    }
    isSetUp = true;
    return this;
}
Also used : Path(java.nio.file.Path) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) NoSuchFileException(java.nio.file.NoSuchFileException) BufferedOutputStream(java.io.BufferedOutputStream) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) InvalidPathException(java.nio.file.InvalidPathException)

Example 57 with FileAlreadyExistsException

use of java.nio.file.FileAlreadyExistsException in project elasticsearch by elastic.

the class InstallPluginCommandTests method testConfigConflict.

public void testConfigConflict() throws Exception {
    Tuple<Path, Environment> env = createEnv(fs, temp);
    Path pluginDir = createPluginDir(temp);
    Path configDir = pluginDir.resolve("config");
    Files.createDirectory(configDir);
    Files.createFile(configDir.resolve("myconfig.yml"));
    String pluginZip = createPlugin("elasticsearch.yml", pluginDir);
    FileAlreadyExistsException e = expectThrows(FileAlreadyExistsException.class, () -> installPlugin(pluginZip, env.v1()));
    assertTrue(e.getMessage(), e.getMessage().contains(env.v2().configFile().resolve("elasticsearch.yml").toString()));
    assertInstallCleaned(env.v2());
}
Also used : Path(java.nio.file.Path) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) Environment(org.elasticsearch.env.Environment) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 58 with FileAlreadyExistsException

use of java.nio.file.FileAlreadyExistsException in project vert.x by eclipse.

the class FileSystemImpl method copyInternal.

private BlockingAction<Void> copyInternal(String from, String to, boolean recursive, Handler<AsyncResult<Void>> handler) {
    Objects.requireNonNull(from);
    Objects.requireNonNull(to);
    return new BlockingAction<Void>(handler) {

        public Void perform() {
            try {
                Path source = vertx.resolveFile(from).toPath();
                Path target = vertx.resolveFile(to).toPath();
                if (recursive) {
                    Files.walkFileTree(source, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {

                        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                            Path targetDir = target.resolve(source.relativize(dir));
                            try {
                                Files.copy(dir, targetDir);
                            } catch (FileAlreadyExistsException e) {
                                if (!Files.isDirectory(targetDir)) {
                                    throw e;
                                }
                            }
                            return FileVisitResult.CONTINUE;
                        }

                        @Override
                        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                            Files.copy(file, target.resolve(source.relativize(file)));
                            return FileVisitResult.CONTINUE;
                        }
                    });
                } else {
                    Files.copy(source, target);
                }
            } catch (IOException e) {
                throw new FileSystemException(e);
            }
            return null;
        }
    };
}
Also used : Path(java.nio.file.Path) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) FileSystemException(io.vertx.core.file.FileSystemException) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 59 with FileAlreadyExistsException

use of java.nio.file.FileAlreadyExistsException in project elasticsearch by elastic.

the class ExceptionSerializationTests method testFileSystemExceptions.

public void testFileSystemExceptions() throws IOException {
    for (FileSystemException ex : Arrays.asList(new FileSystemException("a", "b", "c"), new NoSuchFileException("a", "b", "c"), new NotDirectoryException("a"), new DirectoryNotEmptyException("a"), new AtomicMoveNotSupportedException("a", "b", "c"), new FileAlreadyExistsException("a", "b", "c"), new AccessDeniedException("a", "b", "c"), new FileSystemLoopException("a"))) {
        FileSystemException serialize = serialize(ex);
        assertEquals(serialize.getClass(), ex.getClass());
        assertEquals("a", serialize.getFile());
        if (serialize.getClass() == NotDirectoryException.class || serialize.getClass() == FileSystemLoopException.class || serialize.getClass() == DirectoryNotEmptyException.class) {
            assertNull(serialize.getOtherFile());
            assertNull(serialize.getReason());
        } else {
            assertEquals(serialize.getClass().toString(), "b", serialize.getOtherFile());
            assertEquals(serialize.getClass().toString(), "c", serialize.getReason());
        }
    }
}
Also used : FileSystemException(java.nio.file.FileSystemException) NotDirectoryException(java.nio.file.NotDirectoryException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) AccessDeniedException(java.nio.file.AccessDeniedException) NoSuchFileException(java.nio.file.NoSuchFileException) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException) AtomicMoveNotSupportedException(java.nio.file.AtomicMoveNotSupportedException) FileSystemLoopException(java.nio.file.FileSystemLoopException)

Example 60 with FileAlreadyExistsException

use of java.nio.file.FileAlreadyExistsException in project druid by druid-io.

the class LocalDataSegmentPusher method push.

@Override
public DataSegment push(File dataSegmentFile, DataSegment segment) throws IOException {
    final String storageDir = DataSegmentPusherUtil.getStorageDir(segment);
    final File baseStorageDir = config.getStorageDirectory();
    final File outDir = new File(baseStorageDir, storageDir);
    log.info("Copying segment[%s] to local filesystem at location[%s]", segment.getIdentifier(), outDir.toString());
    if (dataSegmentFile.equals(outDir)) {
        long size = 0;
        for (File file : dataSegmentFile.listFiles()) {
            size += file.length();
        }
        return createDescriptorFile(segment.withLoadSpec(makeLoadSpec(outDir)).withSize(size).withBinaryVersion(SegmentUtils.getVersionFromDir(dataSegmentFile)), outDir);
    }
    final File tmpOutDir = new File(baseStorageDir, intermediateDirFor(storageDir));
    log.info("Creating intermediate directory[%s] for segment[%s]", tmpOutDir.toString(), segment.getIdentifier());
    final long size = compressSegment(dataSegmentFile, tmpOutDir);
    final DataSegment dataSegment = createDescriptorFile(segment.withLoadSpec(makeLoadSpec(new File(outDir, "index.zip"))).withSize(size).withBinaryVersion(SegmentUtils.getVersionFromDir(dataSegmentFile)), tmpOutDir);
    // moving the temporary directory to the final destination, once success the potentially concurrent push operations
    // will be failed and will read the descriptor.json created by current push operation directly
    FileUtils.forceMkdir(outDir.getParentFile());
    try {
        java.nio.file.Files.move(tmpOutDir.toPath(), outDir.toPath());
    } catch (FileAlreadyExistsException e) {
        log.warn("Push destination directory[%s] exists, ignore this message if replication is configured.", outDir);
        FileUtils.deleteDirectory(tmpOutDir);
        return jsonMapper.readValue(new File(outDir, "descriptor.json"), DataSegment.class);
    }
    return dataSegment;
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) File(java.io.File) DataSegment(io.druid.timeline.DataSegment)

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