Search in sources :

Example 1 with CopyOption

use of java.nio.file.CopyOption in project neo4j by neo4j.

the class EphemeralFileSystemAbstraction method renameFile.

@Override
public void renameFile(File from, File to, CopyOption... copyOptions) throws IOException {
    from = canonicalFile(from);
    to = canonicalFile(to);
    if (!files.containsKey(from)) {
        throw new NoSuchFileException("'" + from + "' doesn't exist");
    }
    boolean replaceExisting = false;
    for (CopyOption copyOption : copyOptions) {
        replaceExisting |= copyOption == REPLACE_EXISTING;
    }
    if (files.containsKey(to) && !replaceExisting) {
        throw new FileAlreadyExistsException("'" + to + "' already exists");
    }
    if (!isDirectory(to.getParentFile())) {
        throw new NoSuchFileException("Target directory[" + to.getParent() + "] does not exists");
    }
    files.put(to, files.remove(from));
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) CopyOption(java.nio.file.CopyOption) NoSuchFileException(java.nio.file.NoSuchFileException)

Example 2 with CopyOption

use of java.nio.file.CopyOption in project neo4j by neo4j.

the class FileRoleRepositoryTest method shouldRecoverIfCrashedDuringMove.

@Test
public void shouldRecoverIfCrashedDuringMove() throws Throwable {
    // Given
    final IOException exception = new IOException("simulated IO Exception on create");
    FileSystemAbstraction craschingFileSystem = new DelegatingFileSystemAbstraction(fs) {

        @Override
        public void renameFile(File oldLocation, File newLocation, CopyOption... copyOptions) throws IOException {
            if (roleFile.getName().equals(newLocation.getName())) {
                throw exception;
            }
            super.renameFile(oldLocation, newLocation, copyOptions);
        }
    };
    roleRepository = new FileRoleRepository(craschingFileSystem, roleFile, logProvider);
    roleRepository.start();
    RoleRecord role = new RoleRecord("admin", "jake");
    // When
    try {
        roleRepository.create(role);
        fail("Expected an IOException");
    } catch (IOException e) {
        assertSame(exception, e);
    }
    // Then
    assertFalse(craschingFileSystem.fileExists(roleFile));
    assertThat(craschingFileSystem.listFiles(roleFile.getParentFile()).length, equalTo(0));
}
Also used : DelegatingFileSystemAbstraction(org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction) DelegateFileSystemAbstraction(org.neo4j.io.fs.DelegateFileSystemAbstraction) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) CopyOption(java.nio.file.CopyOption) IOException(java.io.IOException) DelegatingFileSystemAbstraction(org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction) File(java.io.File) Test(org.junit.Test)

Example 3 with CopyOption

use of java.nio.file.CopyOption in project neo4j by neo4j.

the class FileUserRepositoryTest method shouldRecoverIfCrashedDuringMove.

@Test
public void shouldRecoverIfCrashedDuringMove() throws Throwable {
    // Given
    final IOException exception = new IOException("simulated IO Exception on create");
    FileSystemAbstraction craschingFileSystem = new DelegatingFileSystemAbstraction(fs) {

        @Override
        public void renameFile(File oldLocation, File newLocation, CopyOption... copyOptions) throws IOException {
            if (authFile.getName().equals(newLocation.getName())) {
                throw exception;
            }
            super.renameFile(oldLocation, newLocation, copyOptions);
        }
    };
    FileUserRepository users = new FileUserRepository(craschingFileSystem, authFile, logProvider);
    users.start();
    User user = new User.Builder("jake", Credential.INACCESSIBLE).withRequiredPasswordChange(true).build();
    // When
    try {
        users.create(user);
        fail("Expected an IOException");
    } catch (IOException e) {
        assertSame(exception, e);
    }
    // Then
    assertFalse(craschingFileSystem.fileExists(authFile));
    assertThat(craschingFileSystem.listFiles(authFile.getParentFile()).length, equalTo(0));
}
Also used : DelegatingFileSystemAbstraction(org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction) DelegateFileSystemAbstraction(org.neo4j.io.fs.DelegateFileSystemAbstraction) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) CopyOption(java.nio.file.CopyOption) User(org.neo4j.kernel.impl.security.User) IOException(java.io.IOException) DelegatingFileSystemAbstraction(org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction) File(java.io.File) Test(org.junit.Test)

Example 4 with CopyOption

use of java.nio.file.CopyOption in project neo4j by neo4j.

the class MuninnPageCache method checkingFileHandle.

private FileHandle checkingFileHandle(FileHandle fileHandle) {
    return new FileHandle() {

        @Override
        public File getFile() {
            return fileHandle.getFile();
        }

        @Override
        public File getRelativeFile() {
            return fileHandle.getRelativeFile();
        }

        @Override
        public void rename(File targetFile, CopyOption... options) throws IOException {
            synchronized (MuninnPageCache.this) {
                File sourceFile = getFile();
                sourceFile = sourceFile.getCanonicalFile();
                targetFile = targetFile.getCanonicalFile();
                assertNotMapped(sourceFile, FileIsMappedException.Operation.RENAME);
                assertNotMapped(targetFile, FileIsMappedException.Operation.RENAME);
                fileHandle.rename(targetFile, options);
            }
        }

        @Override
        public String toString() {
            return fileHandle.toString();
        }

        @Override
        public void delete() throws IOException {
            synchronized (MuninnPageCache.this) {
                assertNotMapped(getFile(), FileIsMappedException.Operation.DELETE);
                fileHandle.delete();
            }
        }
    };
}
Also used : CopyOption(java.nio.file.CopyOption) FileHandle(org.neo4j.io.pagecache.FileHandle) PagedFile(org.neo4j.io.pagecache.PagedFile) File(java.io.File)

Example 5 with CopyOption

use of java.nio.file.CopyOption in project heron by twitter.

the class LocalFileSystemUploader method uploadPackage.

/**
   * Upload the topology package to the destined location in local file system
   *
   * @return destination URI of where the topology package has
   * been uploaded if successful, or {@code null} if failed.
   */
@Override
public URI uploadPackage() throws UploaderException {
    // first, check if the topology package exists
    boolean fileExists = new File(topologyPackageLocation).isFile();
    if (!fileExists) {
        throw new UploaderException(String.format("Topology package does not exist at '%s'", topologyPackageLocation));
    }
    // get the directory containing the file
    Path filePath = Paths.get(destTopologyFile);
    File parentDirectory = filePath.getParent().toFile();
    assert parentDirectory != null;
    // if the dest directory does not exist, create it.
    if (!parentDirectory.exists()) {
        LOG.fine(String.format("Working directory does not exist. Creating it now at %s", parentDirectory.getPath()));
        if (!parentDirectory.mkdirs()) {
            throw new UploaderException(String.format("Failed to create directory for topology package at %s", parentDirectory.getPath()));
        }
    }
    // if the dest file exists, write a log message
    fileExists = new File(filePath.toString()).isFile();
    if (fileExists) {
        LOG.fine(String.format("Target topology package already exists at '%s'. Overwriting it now", filePath.toString()));
    }
    // copy the topology package to target working directory
    LOG.fine(String.format("Copying topology package at '%s' to target working directory '%s'", topologyPackageLocation, filePath.toString()));
    Path source = Paths.get(topologyPackageLocation);
    try {
        CopyOption[] options = new CopyOption[] { StandardCopyOption.REPLACE_EXISTING };
        Files.copy(source, filePath, options);
    } catch (IOException e) {
        throw new UploaderException(String.format("Unable to copy topology file from '%s' to '%s'", source, filePath), e);
    }
    return getUri(destTopologyFile);
}
Also used : Path(java.nio.file.Path) StandardCopyOption(java.nio.file.StandardCopyOption) CopyOption(java.nio.file.CopyOption) IOException(java.io.IOException) UploaderException(com.twitter.heron.spi.uploader.UploaderException) File(java.io.File)

Aggregations

CopyOption (java.nio.file.CopyOption)7 File (java.io.File)4 IOException (java.io.IOException)3 StandardCopyOption (java.nio.file.StandardCopyOption)3 NoSuchFileException (java.nio.file.NoSuchFileException)2 Test (org.junit.Test)2 DelegatingFileSystemAbstraction (org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction)2 DelegateFileSystemAbstraction (org.neo4j.io.fs.DelegateFileSystemAbstraction)2 FileSystemAbstraction (org.neo4j.io.fs.FileSystemAbstraction)2 BlobInfo (com.google.cloud.storage.BlobInfo)1 CopyWriter (com.google.cloud.storage.CopyWriter)1 StorageException (com.google.cloud.storage.StorageException)1 UploaderException (com.twitter.heron.spi.uploader.UploaderException)1 AtomicMoveNotSupportedException (java.nio.file.AtomicMoveNotSupportedException)1 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)1 Path (java.nio.file.Path)1 FileHandle (org.neo4j.io.pagecache.FileHandle)1 PagedFile (org.neo4j.io.pagecache.PagedFile)1 User (org.neo4j.kernel.impl.security.User)1