Search in sources :

Example 6 with RepositoryStorage

use of org.apache.archiva.repository.storage.RepositoryStorage in project archiva by apache.

the class AbstractStorageUtilTest method testDeleteWithExceptionFailFast.

@Test
void testDeleteWithExceptionFailFast() {
    StorageAsset root = createTree();
    RepositoryStorage storage = createStorage(root);
    activateException(root);
    StorageUtil.deleteRecursively(root, true);
    int expected = 113;
    testDeletionStatus(expected, storage);
}
Also used : RepositoryStorage(org.apache.archiva.repository.storage.RepositoryStorage) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) Test(org.junit.jupiter.api.Test)

Example 7 with RepositoryStorage

use of org.apache.archiva.repository.storage.RepositoryStorage in project archiva by apache.

the class StorageUtil method deleteRecursively.

/**
 * Deletes the given asset and all child assets recursively.
 * IOExceptions during deletion are ignored.
 *
 * @param baseDir The base asset to remove.
 */
public static final void deleteRecursively(StorageAsset baseDir) {
    RepositoryStorage storage = baseDir.getStorage();
    walk(baseDir, a -> {
        try {
            storage.removeAsset(a);
        } catch (IOException e) {
            LOG.error("Could not delete asset {}: {}", a.getPath(), e.getMessage(), e);
        }
    });
}
Also used : RepositoryStorage(org.apache.archiva.repository.storage.RepositoryStorage) IOException(java.io.IOException)

Example 8 with RepositoryStorage

use of org.apache.archiva.repository.storage.RepositoryStorage in project archiva by apache.

the class FsStorageUtil method copyAsset.

/**
 * Copies the source asset to the target. The assets may be from different RepositoryStorage instances.
 * If you know that source and asset are from the same storage instance, the copy method of the storage
 * instance may be faster.
 *
 * @param source The source asset
 * @param target The target asset
 * @param locked If true, a readlock is set on the source and a write lock is set on the target.
 * @param copyOptions Copy options
 * @throws IOException
 */
public static final void copyAsset(final StorageAsset source, final StorageAsset target, boolean locked, final CopyOption... copyOptions) throws IOException {
    if (source.isFileBased() && target.isFileBased()) {
        // Short cut for FS operations
        final Path sourcePath = source.getFilePath();
        final Path targetPath = target.getFilePath();
        if (locked) {
            final FileLockManager lmSource = ((FilesystemStorage) source.getStorage()).getFileLockManager();
            final FileLockManager lmTarget = ((FilesystemStorage) target.getStorage()).getFileLockManager();
            Lock lockRead = null;
            Lock lockWrite = null;
            try {
                lockRead = lmSource.readFileLock(sourcePath);
            } catch (Exception e) {
                log.error("Could not create read lock on {}", sourcePath);
                throw new IOException(e);
            }
            try {
                lockWrite = lmTarget.writeFileLock(targetPath);
            } catch (Exception e) {
                log.error("Could not create write lock on {}", targetPath);
                throw new IOException(e);
            }
            try {
                Files.copy(sourcePath, targetPath, copyOptions);
            } finally {
                if (lockRead != null) {
                    try {
                        lmSource.release(lockRead);
                    } catch (FileLockException e) {
                        log.error("Error during lock release of read lock {}", lockRead.getFile());
                    }
                }
                if (lockWrite != null) {
                    try {
                        lmTarget.release(lockWrite);
                    } catch (FileLockException e) {
                        log.error("Error during lock release of write lock {}", lockWrite.getFile());
                    }
                }
            }
        } else {
            Files.copy(sourcePath, targetPath, copyOptions);
        }
    } else {
        try {
            final RepositoryStorage sourceStorage = source.getStorage();
            final RepositoryStorage targetStorage = target.getStorage();
            sourceStorage.consumeDataFromChannel(source, is -> wrapWriteFunction(is, targetStorage, target, locked), locked);
        } catch (IOException e) {
            throw e;
        } catch (Throwable e) {
            Throwable cause = e.getCause();
            if (cause instanceof IOException) {
                throw (IOException) cause;
            } else {
                throw new IOException(e);
            }
        }
    }
}
Also used : FileLockManager(org.apache.archiva.common.filelock.FileLockManager) RepositoryStorage(org.apache.archiva.repository.storage.RepositoryStorage) FileLockException(org.apache.archiva.common.filelock.FileLockException) IOException(java.io.IOException) IOException(java.io.IOException) FileLockException(org.apache.archiva.common.filelock.FileLockException) Lock(org.apache.archiva.common.filelock.Lock)

Aggregations

RepositoryStorage (org.apache.archiva.repository.storage.RepositoryStorage)8 StorageAsset (org.apache.archiva.repository.storage.StorageAsset)5 IOException (java.io.IOException)4 Test (org.junit.jupiter.api.Test)4 Path (java.nio.file.Path)1 FileLockException (org.apache.archiva.common.filelock.FileLockException)1 FileLockManager (org.apache.archiva.common.filelock.FileLockManager)1 Lock (org.apache.archiva.common.filelock.Lock)1 FilesystemStorage (org.apache.archiva.repository.storage.fs.FilesystemStorage)1