Refine search
/** * Checks that this entry does not exist, throwing an exception if it does. * * @return this * @throws FileAlreadyExistsException if this entry does not exist */ public DirectoryEntry requireDoesNotExist(Path pathForException) throws FileAlreadyExistsException { if (exists()) { throw new FileAlreadyExistsException(pathForException.toString()); } return this; }
private void dump( String database, DatabaseLayout databaseLayout, Path transactionalLogsDirectory, Path archive ) throws CommandFailed { Path databasePath = databaseLayout.databaseDirectory().toPath(); try { File storeLockFile = databaseLayout.getStoreLayout().storeLockFile(); dumper.dump( databasePath, transactionalLogsDirectory, archive, path -> Objects.equals( path.getFileName().toString(), storeLockFile.getName() ) ); } catch ( FileAlreadyExistsException e ) { throw new CommandFailed( "archive already exists: " + e.getMessage(), e ); } catch ( NoSuchFileException e ) { if ( Paths.get( e.getMessage() ).toAbsolutePath().equals( databasePath ) ) { throw new CommandFailed( "database does not exist: " + database, e ); } wrapIOException( e ); } catch ( IOException e ) { wrapIOException( e ); } }
Files.delete(path); } catch (IOException e) { throw ElytronMessages.log.fileSystemUpdatedFailed(path.toAbsolutePath().toString(), e); Files.deleteIfExists(tempPath); } catch (IOException e2) { e.addSuppressed(e2);
private IOException asIoException(StorageException oops) { // RPC API can only throw StorageException, but CloudStorageFileSystemProvider // can only throw IOException. Square peg, round hole. // TODO(#810): Research if other codes should be translated similarly. if (oops.getCode() == 404) { return new NoSuchFileException(oops.getReason()); } Throwable cause = oops.getCause(); try { if (cause instanceof FileAlreadyExistsException) { throw new FileAlreadyExistsException(((FileAlreadyExistsException) cause).getReason()); } // fallback if (cause != null && cause instanceof IOException) { return (IOException) cause; } } catch (IOException okEx) { return okEx; } return new IOException(oops.getMessage(), oops); } }
private void validatePath( Path path ) throws FileSystemException { if ( exists( path ) ) { throw new FileAlreadyExistsException( path.toString() ); } checkWritableDirectory( path.getParent() ); }
@Override public void createSymbolicLink(Path link, Path target, FileAttribute<?>... attrs) throws IOException { String linkPath = link.toString(); if (Files.exists(link, LinkOption.NOFOLLOW_LINKS)) { throw new FileAlreadyExistsException(linkPath); } if (null != attrs && attrs.length > 0) { throw new UnsupportedOperationException("glfs_symlink does not support atomic mode/perms"); } GlusterFileSystem fileSystem = (GlusterFileSystem) link.getFileSystem(); long volptr = fileSystem.getVolptr(); int ret = GLFS.glfs_symlink(volptr, target.toString(), linkPath); if (0 != ret) { throw new IOException("Unknown error creating symlink: " + linkPath); } }
@Override public void createDirectory(Path path, FileAttribute<?>... fileAttributes) throws IOException { if (Files.exists(path)) { throw new FileAlreadyExistsException(path.toString()); } if (!Files.exists(path.getParent())) { throw new IOException(); } int mode = 0775; if (fileAttributes.length > 0) { mode = GlusterFileAttributes.parseAttrs(fileAttributes); } int ret = GLFS.glfs_mkdir(((GlusterFileSystem) path.getFileSystem()).getVolptr(), path.toString(), mode); if (ret < 0) { throw new IOException(path.toString()); } }
public void moveBlobAtomic(final String sourceBlobName, final String targetBlobName, final boolean failIfAlreadyExists) throws IOException { final Path sourceBlobPath = path.resolve(sourceBlobName); final Path targetBlobPath = path.resolve(targetBlobName); // If the target file exists then Files.move() behaviour is implementation specific // the existing file might be replaced or this method fails by throwing an IOException. if (Files.exists(targetBlobPath)) { if (failIfAlreadyExists) { throw new FileAlreadyExistsException("blob [" + targetBlobPath + "] already exists, cannot overwrite"); } else { deleteBlobIgnoringIfNotExists(targetBlobName); } } Files.move(sourceBlobPath, targetBlobPath, StandardCopyOption.ATOMIC_MOVE); }
@Test void shouldGiveAClearErrorIfTheArchiveAlreadyExists() throws IOException { Path directory = testDirectory.directory( "a-directory" ).toPath(); Path archive = testDirectory.file( "the-archive.dump" ).toPath(); Files.write( archive, new byte[0] ); FileAlreadyExistsException exception = assertThrows( FileAlreadyExistsException.class, () -> new Dumper().dump( directory, directory, archive, Predicates.alwaysFalse() ) ); assertEquals( archive.toString(), exception.getMessage() ); }
private static void checkExistingAnyExtension(Path path) throws IOException, FileAlreadyExistsException { Path existing = anyExtension(path); if (!path.equals(existing)) throw new FileAlreadyExistsException(existing.toString()); }
static void copy(final Path source, final Path target, final FileSystem sourceFileSystem, final FileSystem targetFileSystem, CopyOption... options) throws IOException { if (source.equals(target)) { return; if (sourceReal.equals(targetReal)) { return; throw new AtomicMoveNotSupportedException(source.getFileName().toString(), target.getFileName().toString(), "Atomic move not supported"); throw new FileAlreadyExistsException(target.toString());
final Path src = Objects.requireNonNull(source).toRealPath(); final Path dst = Objects.requireNonNull(destination).toAbsolutePath(); throw new FileAlreadyExistsException(destination.toString());
void init(GlusterFileSystem fileSystem, Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException { this.fileSystem = fileSystem; if (!path.isAbsolute()) { throw new IllegalStateException("Only absolute paths are supported at this time"); } this.path = (GlusterPath) path; this.options = options; int flags = parseOptions(options); int mode = GlusterFileAttributes.parseAttrs(attrs); String pathString = path.toUri().getPath(); boolean createNew = options.contains(StandardOpenOption.CREATE_NEW); if (options.contains(StandardOpenOption.CREATE) || createNew) { fileptr = GLFS.glfs_creat(fileSystem.getVolptr(), pathString, flags, mode); } if (createNew && 0 == fileptr) { throw new FileAlreadyExistsException(path.toString()); } if (0 >= fileptr) { fileptr = GLFS.glfs_open(fileSystem.getVolptr(), pathString, flags); } if (0 >= fileptr) { throw new IOException("Unable to create or open file '" + pathString + "' on volume '" + fileSystem.toString() + "'"); } }
/** * Create a new zip file as a filesystem * * @param path the path to the zip to create * @return a filesystem * @throws FileAlreadyExistsException zip file already exists * @throws IOException other errors */ @Nonnull @WillNotClose public static FileSystem createZip(final Path path) throws IOException { if (Files.exists(path)) throw new FileAlreadyExistsException(path.toString()); final URI uri = URI.create("jar:" + path.toUri()); return FileSystems.newFileSystem(uri, ZIP_CREATE); }
LoggerUtils.fine(logger, envImpl, "Creating snapshot: " + snapshotDir.getFileName() + ", now: " + formatTime(currentTimeMs())); snapshotDir.resolve(logFilePath.getFileName()), envHomeDir.resolve(logFilePath)); throw new IllegalStateException( "Snapshot directory should not already contain file: " + e.getFile(), e); } finally {
/** * Save the string content to a file at the provided file path, optionally overwriting the file if it exists. * * If the file path exists and is a directory or overwrite is false, an exception will be thrown, otherwise the file * will be deleted before being recreated with the new content. * * @param filePath Path to the file to save. File path cannot be to an existing directory. * @param content File contents to write. * @param overwrite Whether or not to overwrite the file if it already exists. * @throws IOException If the file path cant be overwritten, the file path is invalid or an general IO error occurred. */ public static void save(String filePath, String content, boolean overwrite) throws IOException { Path path = Paths.get(filePath); if(Files.exists(path)){ if(Files.isDirectory(path)){ throw new FileAlreadyExistsException("File is a directory. Refusing to destroy."); } else if(!overwrite) { throw new FileAlreadyExistsException("Refusing to overwrite existing file."); } } FileUtils.writeStringToFile(path.toFile(), content, (String) null); }
Boolean exists = null; if (options.get(EXCLUSIVE) && (exists = exists(entryFile))) throw new FileAlreadyExistsException(entry.toString()); if (options.get(CACHE)) { || null == exists && (exists = exists(entryFile))) { entryFile .getFileSystem() .provider() .checkAccess(entryFile, AccessMode.WRITE); final Path parentFile = entryFile.getParent(); if (null != parentFile) { try {
throw new FileAlreadyExistsException( target.toString() ); if( osource.getFileSystem().isReadOnly() ) { throw new ReadOnlyFileSystemException(); if( parent.equals( source ) ) { throw new FileSystemException( "a directory can not be moved into attributes of its own subdirectories" ); parent = parent.getParent();
Path path = Paths.get("/tmp/foo/bar.txt"); Files.createDirectories(path.getParent()); try { Files.createFile(path); } catch (FileAlreadyExistsException e) { System.err.println("already exists: " + e.getMessage()); } } }
private void createFile(final AbstractPath path, final MemoryEntryCreator creator) throws IOException { this.checker.check(); AbstractPath absolutePath = (AbstractPath) path.toAbsolutePath().normalize(); if (absolutePath.isRoot()) { throw new FileAlreadyExistsException(path.toString(), null, "can not create root"); } final ElementPath elementPath = (ElementPath) absolutePath; this.accessDirectoryWriting((AbstractPath) elementPath.getParent(), true, new MemoryDirectoryBlock<Void>() { @Override public Void value(MemoryDirectory directory) throws IOException { String name = MemoryFileSystem.this.storeTransformer.transform(elementPath.getLastNameElement()); MemoryEntry newEntry = creator.create(name); String key = MemoryFileSystem.this.lookUpTransformer.transform(newEntry.getOriginalName()); directory.checkAccess(WRITE); directory.addEntry(key, newEntry, path); return null; } }); }