Search in sources :

Example 11 with DirectoryNotEmptyException

use of java.nio.file.DirectoryNotEmptyException in project elasticsearch by elastic.

the class BlobStoreRepository method deleteSnapshot.

@Override
public void deleteSnapshot(SnapshotId snapshotId, long repositoryStateId) {
    if (isReadOnly()) {
        throw new RepositoryException(metadata.name(), "cannot delete snapshot from a readonly repository");
    }
    final RepositoryData repositoryData = getRepositoryData();
    List<String> indices = Collections.emptyList();
    SnapshotInfo snapshot = null;
    try {
        snapshot = getSnapshotInfo(snapshotId);
        indices = snapshot.indices();
    } catch (SnapshotMissingException ex) {
        throw ex;
    } catch (IllegalStateException | SnapshotException | ElasticsearchParseException ex) {
        logger.warn((Supplier<?>) () -> new ParameterizedMessage("cannot read snapshot file [{}]", snapshotId), ex);
    }
    MetaData metaData = null;
    try {
        if (snapshot != null) {
            metaData = readSnapshotMetaData(snapshotId, snapshot.version(), repositoryData.resolveIndices(indices), true);
        } else {
            metaData = readSnapshotMetaData(snapshotId, null, repositoryData.resolveIndices(indices), true);
        }
    } catch (IOException | SnapshotException ex) {
        logger.warn((Supplier<?>) () -> new ParameterizedMessage("cannot read metadata for snapshot [{}]", snapshotId), ex);
    }
    try {
        // Delete snapshot from the index file, since it is the maintainer of truth of active snapshots
        final RepositoryData updatedRepositoryData = repositoryData.removeSnapshot(snapshotId);
        writeIndexGen(updatedRepositoryData, repositoryStateId);
        // delete the snapshot file
        safeSnapshotBlobDelete(snapshot, snapshotId.getUUID());
        // delete the global metadata file
        safeGlobalMetaDataBlobDelete(snapshot, snapshotId.getUUID());
        // Now delete all indices
        for (String index : indices) {
            final IndexId indexId = repositoryData.resolveIndexId(index);
            BlobPath indexPath = basePath().add("indices").add(indexId.getId());
            BlobContainer indexMetaDataBlobContainer = blobStore().blobContainer(indexPath);
            try {
                indexMetaDataFormat.delete(indexMetaDataBlobContainer, snapshotId.getUUID());
            } catch (IOException ex) {
                logger.warn((Supplier<?>) () -> new ParameterizedMessage("[{}] failed to delete metadata for index [{}]", snapshotId, index), ex);
            }
            if (metaData != null) {
                IndexMetaData indexMetaData = metaData.index(index);
                if (indexMetaData != null) {
                    for (int shardId = 0; shardId < indexMetaData.getNumberOfShards(); shardId++) {
                        try {
                            delete(snapshotId, snapshot.version(), indexId, new ShardId(indexMetaData.getIndex(), shardId));
                        } catch (SnapshotException ex) {
                            final int finalShardId = shardId;
                            logger.warn((Supplier<?>) () -> new ParameterizedMessage("[{}] failed to delete shard data for shard [{}][{}]", snapshotId, index, finalShardId), ex);
                        }
                    }
                }
            }
        }
        // cleanup indices that are no longer part of the repository
        final Collection<IndexId> indicesToCleanUp = Sets.newHashSet(repositoryData.getIndices().values());
        indicesToCleanUp.removeAll(updatedRepositoryData.getIndices().values());
        final BlobContainer indicesBlobContainer = blobStore().blobContainer(basePath().add("indices"));
        for (final IndexId indexId : indicesToCleanUp) {
            try {
                indicesBlobContainer.deleteBlob(indexId.getId());
            } catch (DirectoryNotEmptyException dnee) {
                // if the directory isn't empty for some reason, it will fail to clean up;
                // we'll ignore that and accept that cleanup didn't fully succeed.
                // since we are using UUIDs for path names, this won't be an issue for
                // snapshotting indices of the same name
                logger.debug((Supplier<?>) () -> new ParameterizedMessage("[{}] index [{}] no longer part of any snapshots in the repository, but failed to clean up " + "its index folder due to the directory not being empty.", metadata.name(), indexId), dnee);
            } catch (IOException ioe) {
                // a different IOException occurred while trying to delete - will just log the issue for now
                logger.debug((Supplier<?>) () -> new ParameterizedMessage("[{}] index [{}] no longer part of any snapshots in the repository, but failed to clean up " + "its index folder.", metadata.name(), indexId), ioe);
            }
        }
    } catch (IOException ex) {
        throw new RepositoryException(metadata.name(), "failed to update snapshot in repository", ex);
    }
}
Also used : IndexId(org.elasticsearch.repositories.IndexId) BlobPath(org.elasticsearch.common.blobstore.BlobPath) RepositoryException(org.elasticsearch.repositories.RepositoryException) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException) IOException(java.io.IOException) SnapshotException(org.elasticsearch.snapshots.SnapshotException) IndexShardSnapshotException(org.elasticsearch.index.snapshots.IndexShardSnapshotException) RepositoryData(org.elasticsearch.repositories.RepositoryData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) ShardId(org.elasticsearch.index.shard.ShardId) SnapshotInfo(org.elasticsearch.snapshots.SnapshotInfo) SnapshotMissingException(org.elasticsearch.snapshots.SnapshotMissingException) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) StoreFileMetaData(org.elasticsearch.index.store.StoreFileMetaData) BlobMetaData(org.elasticsearch.common.blobstore.BlobMetaData) RepositoryMetaData(org.elasticsearch.cluster.metadata.RepositoryMetaData) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) BlobContainer(org.elasticsearch.common.blobstore.BlobContainer) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage)

Example 12 with DirectoryNotEmptyException

use of java.nio.file.DirectoryNotEmptyException in project elasticsearch by elastic.

the class ExceptionSerializationTests method testFileSystemExceptions.

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());
        }
    }
}
Also used : FileSystemException(java.nio.file.FileSystemException) NotDirectoryException(java.nio.file.NotDirectoryException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) AccessDeniedException(java.nio.file.AccessDeniedException) NoSuchFileException(java.nio.file.NoSuchFileException) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException) AtomicMoveNotSupportedException(java.nio.file.AtomicMoveNotSupportedException) FileSystemLoopException(java.nio.file.FileSystemLoopException)

Example 13 with DirectoryNotEmptyException

use of java.nio.file.DirectoryNotEmptyException in project Terasology by MovingBlocks.

the class SaveTransactionHelper method mergeChanges.

/**
 * Merges all outstanding changes into the save game. If this operation gets interrupted it can be started again
 * without any file corruption when the file system supports atomic moves.
 * <br><br>
 * The write lock for the save directory should be acquired before this method gets called.
 */
public void mergeChanges() throws IOException {
    final Path sourceDirectory = storagePathProvider.getUnmergedChangesPath();
    final Path targetDirectory = storagePathProvider.getStoragePathDirectory();
    Files.walkFileTree(sourceDirectory, new SimpleFileVisitor<Path>() {

        boolean atomicNotPossibleLogged;

        @Override
        public FileVisitResult preVisitDirectory(Path sourceSubDir, BasicFileAttributes attrs) throws IOException {
            Path targetSubDir = targetDirectory.resolve(sourceDirectory.relativize(sourceSubDir));
            if (!Files.isDirectory(targetSubDir)) {
                Files.createDirectory(targetSubDir);
            }
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFile(Path sourcePath, BasicFileAttributes attrs) throws IOException {
            Path targetPath = targetDirectory.resolve(sourceDirectory.relativize(sourcePath));
            try {
                // Delete file, as behavior of atomic move is undefined if target file exists:
                Files.deleteIfExists(targetPath);
                Files.move(sourcePath, targetPath, StandardCopyOption.ATOMIC_MOVE);
            } catch (AtomicMoveNotSupportedException e) {
                if (!atomicNotPossibleLogged) {
                    logger.warn("Atomic move was not possible, doing it non atomically...");
                    atomicNotPossibleLogged = true;
                }
                Files.move(sourcePath, targetPath);
            }
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
            try {
                Files.delete(dir);
            } catch (DirectoryNotEmptyException e) {
                /**
                 * Happens rarely for some players on windows (See issue #2160). Exact reason for this behavior is
                 * unknown. Maybe they have some kind of background task that processes that creates a temporary
                 * files in new directories to store some intermediate scan result.
                 */
                logger.warn("The save job could not cleanup a temporarly created directory, it will retry once in one second");
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e1) {
                    // Reset flag and ignore it
                    Thread.currentThread().interrupt();
                }
                Files.delete(dir);
            }
            return FileVisitResult.CONTINUE;
        }
    });
}
Also used : Path(java.nio.file.Path) FileVisitResult(java.nio.file.FileVisitResult) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException) IOException(java.io.IOException) AtomicMoveNotSupportedException(java.nio.file.AtomicMoveNotSupportedException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 14 with DirectoryNotEmptyException

use of java.nio.file.DirectoryNotEmptyException in project graal by oracle.

the class MemoryFileSystem method delete.

@Override
public void delete(Path path) throws IOException {
    final Path absolutePath = toAbsolutePath(path);
    final Path parentPath = absolutePath.getParent();
    if (parentPath == null) {
        throw new IOException("Cannot delete root.");
    }
    Map.Entry<Long, Map<String, Long>> e = readDir(parentPath);
    final long inode = e.getKey();
    final Map<String, Long> dirents = e.getValue();
    if (!inodes.get(inode).permissions.contains(AccessMode.WRITE)) {
        throw new IOException("Read only dir: " + path);
    }
    final String fileName = absolutePath.getFileName().toString();
    final Long fileInode = dirents.get(fileName);
    if (fileInode == null) {
        throw new NoSuchFileException(path.toString());
    }
    if (inodes.get(fileInode).isDirectory()) {
        if (!readDir(fileInode).isEmpty()) {
            throw new DirectoryNotEmptyException(path.toString());
        }
    }
    inodes.remove(fileInode);
    blocks.remove(fileInode);
    dirents.remove(fileName);
    writeDir(inode, dirents);
}
Also used : Path(java.nio.file.Path) NoSuchFileException(java.nio.file.NoSuchFileException) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) AbstractMap(java.util.AbstractMap)

Example 15 with DirectoryNotEmptyException

use of java.nio.file.DirectoryNotEmptyException in project cryptofs by cryptomator.

the class CryptoFileSystemImpl method delete.

void delete(CryptoPath cleartextPath) throws IOException {
    Path ciphertextFile = cryptoPathMapper.getCiphertextFilePath(cleartextPath, CiphertextFileType.FILE);
    // try to delete ciphertext file:
    if (!Files.deleteIfExists(ciphertextFile)) {
        // filePath doesn't exist, maybe it's an directory:
        Path ciphertextDir = cryptoPathMapper.getCiphertextDirPath(cleartextPath);
        Path ciphertextDirFile = cryptoPathMapper.getCiphertextFilePath(cleartextPath, CiphertextFileType.DIRECTORY);
        try {
            ciphertextDirDeleter.deleteCiphertextDirIncludingNonCiphertextFiles(ciphertextDir, cleartextPath);
            if (!Files.deleteIfExists(ciphertextDirFile)) {
                // should not happen. Nevertheless this is a valid state, so who no big deal...
                LOG.warn("Successfully deleted dir {}, but didn't find corresponding dir file {}", ciphertextDir, ciphertextDirFile);
            }
            dirIdProvider.delete(ciphertextDirFile);
        } catch (NoSuchFileException e) {
            // translate ciphertext path to cleartext path
            throw new NoSuchFileException(cleartextPath.toString());
        } catch (DirectoryNotEmptyException e) {
            // translate ciphertext path to cleartext path
            throw new DirectoryNotEmptyException(cleartextPath.toString());
        }
    }
}
Also used : Path(java.nio.file.Path) NoSuchFileException(java.nio.file.NoSuchFileException) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException)

Aggregations

DirectoryNotEmptyException (java.nio.file.DirectoryNotEmptyException)21 NoSuchFileException (java.nio.file.NoSuchFileException)12 IOException (java.io.IOException)10 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)8 Path (java.nio.file.Path)8 AccessDeniedException (java.nio.file.AccessDeniedException)6 AtomicMoveNotSupportedException (java.nio.file.AtomicMoveNotSupportedException)6 NotDirectoryException (java.nio.file.NotDirectoryException)6 FileSystemException (java.nio.file.FileSystemException)5 FileNotFoundException (java.io.FileNotFoundException)4 FileSystemLoopException (java.nio.file.FileSystemLoopException)4 EOFException (java.io.EOFException)3 File (java.io.File)3 CorruptIndexException (org.apache.lucene.index.CorruptIndexException)3 IndexFormatTooNewException (org.apache.lucene.index.IndexFormatTooNewException)3 IndexFormatTooOldException (org.apache.lucene.index.IndexFormatTooOldException)3 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)3 LockObtainFailedException (org.apache.lucene.store.LockObtainFailedException)3 MCRDirectory (org.mycore.datamodel.ifs.MCRDirectory)3 MCRPath (org.mycore.datamodel.niofs.MCRPath)3