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));
}
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));
}
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));
}
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();
}
}
};
}
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);
}
Aggregations