public void testFileSystemExceptions() throws IOException { for (FileSystemException ex : Arrays.asList(new FileSystemException("a", "b", "c"), new NoSuchFileException("a", "b", "c"), new NotDirectoryException("a"), new DirectoryNotEmptyException("a"), new AtomicMoveNotSupportedException("a", "b", "c"), new FileAlreadyExistsException("a", "b", "c"), new AccessDeniedException("a", "b", "c"), new FileSystemLoopException("a"))) { FileSystemException serialize = serialize(ex); assertEquals(serialize.getClass(), ex.getClass()); assertEquals("a", serialize.getFile()); if (serialize.getClass() == NotDirectoryException.class || serialize.getClass() == FileSystemLoopException.class || serialize.getClass() == DirectoryNotEmptyException.class) { assertNull(serialize.getOtherFile()); assertNull(serialize.getReason()); } else { assertEquals(serialize.getClass().toString(), "b", serialize.getOtherFile()); assertEquals(serialize.getClass().toString(), "c", serialize.getReason()); } } }
@Override public void move(Path source, Path target, CopyOption... options) throws IOException { HashSet<CopyOption> copyOptions = Sets.newHashSet(options); if (copyOptions.contains(StandardCopyOption.ATOMIC_MOVE)) { throw new AtomicMoveNotSupportedException(source.toString(), target.toString(), "ATOMIC_MOVE not supported yet"); } if (Files.isDirectory(source)) { MCRPath src = MCRFileSystemUtils.checkPathAbsolute(source); MCRDirectory srcRootDirectory = getRootDirectory(src); if (srcRootDirectory.hasChildren()) { throw new IOException("Directory is not empty"); } } copy(source, target, options); delete(source); }
@Override public void copyIfLocked(final Path source, final Path target, final Mover mover) throws IOException { checkNotNull(source); checkNotNull(target); try { mover.accept(source, target); } catch (AtomicMoveNotSupportedException atomicMoveNotSupported) { throw atomicMoveNotSupported; } catch (FileSystemException e) { // NOSONAR // Windows can throw a FileSystemException on move or moveAtomic // if the file is in use; in this case, copy and attempt to delete // source log.warn("Using copy to move {} to {}", source, target); copy(source, target); try { delete(source); } catch (IOException deleteException) { // NOSONAR log.error("Unable to delete {} after move: {}", source, deleteException.getMessage()); } } }
@Test public void temporaryBlobMoveFallback() throws Exception { final byte[] content = new byte[TEST_DATA_LENGTH]; new Random().nextBytes(content); doThrow(new AtomicMoveNotSupportedException("", "", "")).when(fileOperations).moveAtomic(any(), any()); final Blob blob = underTest.create(new ByteArrayInputStream(content), TEST_HEADERS); verifyMoveOperations(blob); final byte[] output = extractContent(blob); assertThat("data must survive", content, is(equalTo(output))); final BlobMetrics metrics = blob.getMetrics(); assertThat("size must be calculated correctly", metrics.getContentSize(), is(equalTo((long) TEST_DATA_LENGTH))); }
@Override public void close() throws IOException { try { this.channel.position(0); // reset channel to beginning to compute full SHA1 String calculatedSha1 = ZsyncUtil.computeSha1(this.channel); if (!this.sha1.equals(calculatedSha1)) { throw new ChecksumValidationIOException(this.sha1, calculatedSha1); } try { Files.move(this.tempPath, this.path, REPLACE_EXISTING, ATOMIC_MOVE); } catch (AtomicMoveNotSupportedException e) { Files.move(this.tempPath, this.path, REPLACE_EXISTING); } Files.setLastModifiedTime(this.path, fromMillis(this.mtime)); } finally { this.channel.close(); this.listener.close(); } }
public boolean sauvegarde(Object o) { if (this.getParentFile() != null && !this.getParentFile().exists()) { this.getParentFile().mkdirs(); } IOMethod m; m = new JsonIO(); // m = new GsonIO(); // m = new Jackson(); try { File security = new File(this.getParent()+Adresse.separatorChar+"."+this.getName()); m.sauvegarde(security, o); if(!this.exists()) {this.createNewFile();} try { Files.move(security.toPath(), this.toPath(), StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE); } catch(AtomicMoveNotSupportedException ex) { Files.move(security.toPath(), this.toPath(), StandardCopyOption.REPLACE_EXISTING); } if(!security.delete()) {security.deleteOnExit();} } catch(Exception e) { Logger.getLogger(Adresse.class.getName()).log(Level.SEVERE, "adresse : "+this.getAbsolutePath(), e); DialogueBloquant.error("dialog file not saved"); return false; } return true; }
public synchronized static void replaceFile(final File sourceFile, final File destFile) throws IOException { if (sourceFile == null) { throw new NullPointerException("sourceFile"); } if (destFile == null) { throw new NullPointerException("destFile"); } Path sourcePath = Paths.get(sourceFile.getAbsolutePath()); Path destPath = Paths.get(destFile.getAbsolutePath()); try { Files.move(sourcePath, destPath, FileReplaceOptions); } catch (AtomicMoveNotSupportedException ex) { Files.move(sourcePath, destPath, FallbackFileReplaceOptions); } }
private static void rename(Path sourceFile, Path targetFile) throws IOException { try { Files.move(sourceFile, targetFile, StandardCopyOption.ATOMIC_MOVE); } catch (AtomicMoveNotSupportedException e) { LOG.warn("Atomic rename from '{}' to '{}' not supported", sourceFile, targetFile); Files.move(sourceFile, targetFile); } }
private static void moveIntoPlace(final File from, final File to) throws IOException { try { move(from.toPath(), to.toPath(), ATOMIC_MOVE); } catch (final AtomicMoveNotSupportedException ex) { move(from.toPath(), to.toPath(), REPLACE_EXISTING); } finally { deleteIfExists(from.toPath()); } }
@Override public void move(Path srcpath, SecureDirectoryStream<Path> targetdir, Path targetpath) throws IOException { checkClosed(); checkFileSystem(srcpath); checkFileSystem(targetpath); throw new AtomicMoveNotSupportedException(srcpath.toString(), targetpath.toString(), "Currently not implemented"); }
@Override public void moveAtomic(final Path source, final Path target) throws IOException { checkNotNull(source); checkNotNull(target); DirectoryHelper.mkdir(target.getParent()); try { Files.move(source, target, StandardCopyOption.ATOMIC_MOVE); } catch (UnsupportedOperationException e) { // NOSONAR throw new AtomicMoveNotSupportedException(source.toString(), target.toString(), e.getMessage()); } }
@Override public void overwriteAtomic(final Path source, final Path target) throws IOException { checkNotNull(source); checkNotNull(target); DirectoryHelper.mkdir(target.getParent()); try { Files.move(source, target, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING); } catch (UnsupportedOperationException e) { // NOSONAR throw new AtomicMoveNotSupportedException(source.toString(), target.toString(), e.getMessage()); } }
private void move(final Path source, final Path target) throws IOException { if (supportsAtomicMove) { try { fileOperations.copyIfLocked(source, target, fileOperations::moveAtomic); return; } catch (AtomicMoveNotSupportedException e) { // NOSONAR supportsAtomicMove = false; log.warn("Disabling atomic moves for blob store {}, could not move {} to {}, reason deleted: {}", blobStoreConfiguration.getName(), source, target, e.getReason()); } } log.trace("Using normal move for blob store {}, moving {} to {}", blobStoreConfiguration.getName(), source, target); fileOperations.copyIfLocked(source, target, fileOperations::move); }
private void overwrite(final Path source, final Path target) throws IOException { if (supportsAtomicMove) { try { fileOperations.copyIfLocked(source, target, fileOperations::overwriteAtomic); return; } catch (AtomicMoveNotSupportedException e) { // NOSONAR supportsAtomicMove = false; log.warn("Disabling atomic moves for blob store {}, could not overwrite {} with {}, reason deleted: {}", blobStoreConfiguration.getName(), source, target, e.getReason()); } } log.trace("Using normal overwrite for blob store {}, overwriting {} with {}", blobStoreConfiguration.getName(), source, target); fileOperations.copyIfLocked(source, target, fileOperations::overwrite); }
@Test public void temporaryBlobMoveFallbackPersists() throws Exception { final byte[] content = new byte[TEST_DATA_LENGTH]; new Random().nextBytes(content); doThrow(new AtomicMoveNotSupportedException("", "", "")).when(fileOperations).moveAtomic(any(), any()); underTest.create(new ByteArrayInputStream(content), TEST_HEADERS); underTest.create(new ByteArrayInputStream(content), TEST_HEADERS); verify(fileOperations, times(1)).moveAtomic(any(), any()); verify(fileOperations, times(4)).move(any(), any()); }
/** * Move a file atomically, if it fails, it falls back to a non-atomic operation * @param from * @param to * @throws IOException */ private static void atomicMoveWithFallback(Path from, Path to) throws IOException { try { Files.move(from, to, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE); } catch (AtomicMoveNotSupportedException e) { logger.debug("Could not do an atomic move", e); Files.move(from, to, StandardCopyOption.REPLACE_EXISTING); } }
static void move(Path source, Path target) throws IOException { try { Files.move(source, target, ATOMIC_MOVE, REPLACE_EXISTING); } catch (AtomicMoveNotSupportedException ex) { logger.debug(shortError, "-- move() - fallback, atomic move not supported: ", ex); Files.move(source, target, REPLACE_EXISTING); } }
@Override public void move(String location) throws IOException { final Path source = Paths.get(getURI()); final Path target = Paths.get(resolver.getResource(location).getURI()); try { Files.move(source, target, REPLACE_EXISTING, ATOMIC_MOVE); } catch (AtomicMoveNotSupportedException e) { LOGGER.debug("Atomic move not supported, trying without it.", e); Files.move(source, target, REPLACE_EXISTING); } }
void execute(Terminal terminal, String pluginName, Environment env) throws Exception { if (pluginName == null) { throw new UserException(ExitCodes.USAGE, "plugin name is required"); } terminal.println("-> Removing " + Strings.coalesceToEmpty(pluginName) + "..."); final Path pluginDir = env.pluginsFile().resolve(pluginName); if (Files.exists(pluginDir) == false) { throw new UserException( ExitCodes.CONFIG, "plugin " + pluginName + " not found; run 'elasticsearch-plugin list' to get list of installed plugins"); } final List<Path> pluginPaths = new ArrayList<>(); final Path pluginBinDir = env.binFile().resolve(pluginName); if (Files.exists(pluginBinDir)) { if (Files.isDirectory(pluginBinDir) == false) { throw new UserException(ExitCodes.IO_ERROR, "Bin dir for " + pluginName + " is not a directory"); } pluginPaths.add(pluginBinDir); terminal.println(VERBOSE, "Removing: " + pluginBinDir); } terminal.println(VERBOSE, "Removing: " + pluginDir); final Path tmpPluginDir = env.pluginsFile().resolve(".removing-" + pluginName); try { Files.move(pluginDir, tmpPluginDir, StandardCopyOption.ATOMIC_MOVE); } catch (final AtomicMoveNotSupportedException e) { // this can happen on a union filesystem when a plugin is not installed on the top layer; we fall back to a non-atomic move Files.move(pluginDir, tmpPluginDir); } pluginPaths.add(tmpPluginDir); IOUtils.rm(pluginPaths.toArray(new Path[pluginPaths.size()])); // we preserve the config files in case the user is upgrading the plugin, but we print // a message so the user knows in case they want to remove manually final Path pluginConfigDir = env.configFile().resolve(pluginName); if (Files.exists(pluginConfigDir)) { terminal.println( "-> Preserving plugin config files [" + pluginConfigDir + "] in case of upgrade, delete manually if not needed"); } }
/** * Attempts to move the file represented by the specified {@link Path} to the specified destination atomically, * resorting to moving it non-atomically if atomic operations are not supported by the source or destination file * system. * * @param source The source path. * @param destination The destination path. * @throws IOException If the file could not be moved. */ public static void move(Path source, Path destination) throws IOException { try { Files.move(source, destination, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE); } catch (AtomicMoveNotSupportedException e) { Files.move(source, destination, StandardCopyOption.REPLACE_EXISTING); } }