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