Search in sources :

Example 16 with FileAlreadyExistsException

use of java.nio.file.FileAlreadyExistsException in project jimfs by google.

the class FileSystemView method copy.

/**
   * Copies or moves the file at the given source path to the given dest path.
   */
public void copy(JimfsPath source, FileSystemView destView, JimfsPath dest, Set<CopyOption> options, boolean move) throws IOException {
    checkNotNull(source);
    checkNotNull(destView);
    checkNotNull(dest);
    checkNotNull(options);
    boolean sameFileSystem = isSameFileSystem(destView);
    File sourceFile;
    // non-null after block completes iff source file was copied
    File copyFile = null;
    lockBoth(store.writeLock(), destView.store.writeLock());
    try {
        DirectoryEntry sourceEntry = lookUp(source, options).requireExists(source);
        DirectoryEntry destEntry = destView.lookUp(dest, Options.NOFOLLOW_LINKS);
        Directory sourceParent = sourceEntry.directory();
        sourceFile = sourceEntry.file();
        Directory destParent = destEntry.directory();
        if (move && sourceFile.isDirectory()) {
            if (sameFileSystem) {
                checkMovable(sourceFile, source);
                checkNotAncestor(sourceFile, destParent, destView);
            } else {
                // move to another file system is accomplished by copy-then-delete, so the source file
                // must be deletable to be moved
                checkDeletable(sourceFile, DeleteMode.ANY, source);
            }
        }
        if (destEntry.exists()) {
            if (destEntry.file().equals(sourceFile)) {
                return;
            } else if (options.contains(REPLACE_EXISTING)) {
                destView.delete(destEntry, DeleteMode.ANY, dest);
            } else {
                throw new FileAlreadyExistsException(dest.toString());
            }
        }
        if (move && sameFileSystem) {
            // Real move on the same file system.
            sourceParent.unlink(source.name());
            sourceParent.updateModifiedTime();
            destParent.link(dest.name(), sourceFile);
            destParent.updateModifiedTime();
        } else {
            // Doing a copy OR a move to a different file system, which must be implemented by copy and
            // delete.
            // By default, don't copy attributes.
            AttributeCopyOption attributeCopyOption = AttributeCopyOption.NONE;
            if (move) {
                // Copy only the basic attributes of the file to the other file system, as it may not
                // support all the attribute views that this file system does. This also matches the
                // behavior of moving a file to a foreign file system with a different
                // FileSystemProvider.
                attributeCopyOption = AttributeCopyOption.BASIC;
            } else if (options.contains(COPY_ATTRIBUTES)) {
                // As with move, if we're copying the file to a different file system, only copy its
                // basic attributes.
                attributeCopyOption = sameFileSystem ? AttributeCopyOption.ALL : AttributeCopyOption.BASIC;
            }
            // Copy the file, but don't copy its content while we're holding the file store locks.
            copyFile = destView.store.copyWithoutContent(sourceFile, attributeCopyOption);
            destParent.link(dest.name(), copyFile);
            destParent.updateModifiedTime();
            // In order for the copy to be atomic (not strictly necessary, but seems preferable since
            // we can) lock both source and copy files before leaving the file store locks. This
            // ensures that users cannot observe the copy's content until the content has been copied.
            // This also marks the source file as opened, preventing its content from being deleted
            // until after it's copied if the source file itself is deleted in the next step.
            lockSourceAndCopy(sourceFile, copyFile);
            if (move) {
                // It should not be possible for delete to throw an exception here, because we already
                // checked that the file was deletable above.
                delete(sourceEntry, DeleteMode.ANY, source);
            }
        }
    } finally {
        destView.store.writeLock().unlock();
        store.writeLock().unlock();
    }
    if (copyFile != null) {
        // different threads.
        try {
            sourceFile.copyContentTo(copyFile);
        } finally {
            // Unlock the files, allowing the content of the copy to be observed by the user. This also
            // closes the source file, allowing its content to be deleted if it was deleted.
            unlockSourceAndCopy(sourceFile, copyFile);
        }
    }
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException)

Example 17 with FileAlreadyExistsException

use of java.nio.file.FileAlreadyExistsException in project lucene-solr by apache.

the class SimpleFSLockFactory method obtainFSLock.

@Override
protected Lock obtainFSLock(FSDirectory dir, String lockName) throws IOException {
    Path lockDir = dir.getDirectory();
    // Ensure that lockDir exists and is a directory.
    // note: this will fail if lockDir is a symlink
    Files.createDirectories(lockDir);
    Path lockFile = lockDir.resolve(lockName);
    // create the file: this will fail if it already exists
    try {
        Files.createFile(lockFile);
    } catch (FileAlreadyExistsException | AccessDeniedException e) {
        // convert optional specific exception to our optional specific exception
        throw new LockObtainFailedException("Lock held elsewhere: " + lockFile, e);
    }
    // used as a best-effort check, to see if the underlying file has changed
    final FileTime creationTime = Files.readAttributes(lockFile, BasicFileAttributes.class).creationTime();
    return new SimpleFSLock(lockFile, creationTime);
}
Also used : Path(java.nio.file.Path) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) AccessDeniedException(java.nio.file.AccessDeniedException) FileTime(java.nio.file.attribute.FileTime) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 18 with FileAlreadyExistsException

use of java.nio.file.FileAlreadyExistsException in project google-cloud-java by GoogleCloudPlatform.

the class FakeStorageRpc method open.

@Override
public String open(StorageObject object, Map<Option, ?> options) throws StorageException {
    String key = fullname(object);
    boolean mustNotExist = false;
    for (Option option : options.keySet()) {
        // this is a bit of a hack, since we don't implement generations.
        if (option == Option.IF_GENERATION_MATCH && ((Long) options.get(option)) == 0L) {
            mustNotExist = true;
        }
    }
    if (mustNotExist && metadata.containsKey(key)) {
        throw new StorageException(new FileAlreadyExistsException(key));
    }
    metadata.put(key, object);
    return fullname(object);
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) StorageException(com.google.cloud.storage.StorageException)

Example 19 with FileAlreadyExistsException

use of java.nio.file.FileAlreadyExistsException in project google-cloud-java by GoogleCloudPlatform.

the class FakeStorageRpc method openRewrite.

@Override
public RewriteResponse openRewrite(RewriteRequest rewriteRequest) throws StorageException {
    String sourceKey = fullname(rewriteRequest.source);
    // a little hackish, just good enough for the tests to work.
    if (!contents.containsKey(sourceKey)) {
        throw new StorageException(404, "File not found: " + sourceKey);
    }
    boolean mustNotExist = false;
    for (Option option : rewriteRequest.targetOptions.keySet()) {
        // this is a bit of a hack, since we don't implement generations.
        if (option == Option.IF_GENERATION_MATCH && (Long) rewriteRequest.targetOptions.get(option) == 0L) {
            mustNotExist = true;
        }
    }
    String destKey = fullname(rewriteRequest.target);
    if (mustNotExist && contents.containsKey(destKey)) {
        throw new StorageException(new FileAlreadyExistsException(destKey));
    }
    metadata.put(destKey, rewriteRequest.target);
    byte[] data = contents.get(sourceKey);
    contents.put(destKey, Arrays.copyOf(data, data.length));
    return new RewriteResponse(rewriteRequest, rewriteRequest.target, data.length, true, "rewriteToken goes here", data.length);
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) StorageException(com.google.cloud.storage.StorageException)

Example 20 with FileAlreadyExistsException

use of java.nio.file.FileAlreadyExistsException in project CategoricalSyllogism by bkthomps.

the class SaveOrLoad method load.

String[] load() {
    String saveFile = null;
    try {
        Files.createFile(FILE);
    } catch (FileAlreadyExistsException x) {
        try (InputStream in = Files.newInputStream(FILE);
            BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
            String line;
            while ((line = reader.readLine()) != null) {
                saveFile = line;
            }
        } catch (IOException e) {
            System.err.println("Error in SaveOrLoad.load: could not open file.");
        }
    } catch (IOException x) {
        System.err.println("Error in SaveOrLoad.load: could not create file.");
    }
    if (saveFile == null) {
        saveFile = "Bailey's_sentient_robots cats cows dogs elephants foxes grenades russians germans austrians";
    }
    // Create String array from String that is separated by spaces
    final String[] database = saveFile.split("\\s+");
    for (int i = 0; i < database.length; i++) {
        database[i] = database[i].replaceAll("_", " ");
    }
    return database;
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException)

Aggregations

FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)38 IOException (java.io.IOException)15 Path (java.nio.file.Path)14 NoSuchFileException (java.nio.file.NoSuchFileException)9 Test (org.junit.Test)9 File (java.io.File)8 FileNotFoundException (java.io.FileNotFoundException)5 AccessDeniedException (java.nio.file.AccessDeniedException)5 FileSystem (java.nio.file.FileSystem)4 FileSystemException (java.nio.file.FileSystemException)4 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)4 VertxException (io.vertx.core.VertxException)3 EOFException (java.io.EOFException)3 InputStream (java.io.InputStream)3 AtomicMoveNotSupportedException (java.nio.file.AtomicMoveNotSupportedException)3 DirectoryNotEmptyException (java.nio.file.DirectoryNotEmptyException)3 FileSystemLoopException (java.nio.file.FileSystemLoopException)3 NotDirectoryException (java.nio.file.NotDirectoryException)3 ZipFile (java.util.zip.ZipFile)3 StorageException (com.google.cloud.storage.StorageException)2