Skip to content

Commit

Permalink
Raise IOException in case of errors during file removal
Browse files Browse the repository at this point in the history
  • Loading branch information
enricovianello committed Nov 14, 2024
1 parent 208f499 commit aee7266
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
Expand All @@ -30,8 +31,6 @@
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.google.common.io.Files;

@Component
public class DefaultFSStrategy implements FilesystemAccess {

Expand Down Expand Up @@ -60,10 +59,10 @@ public File mkdir(File parentDirectory, String dirName) {
}

@Override
public boolean rm(File f) {
public void rm(File f) throws IOException {

LOG.debug("rm: {}", f.getAbsolutePath());
return f.delete();
Files.delete(f.toPath());
}

@Override
Expand All @@ -79,7 +78,7 @@ public void mv(File source, File dest) {
}

// Overwrites the destination, if it exists
Files.move(source, dest);
Files.move(source.toPath(), dest.toPath());

} catch (IOException e) {
throw new StoRMWebDAVError(e.getMessage(), e);
Expand Down Expand Up @@ -112,7 +111,7 @@ public void cp(File source, File dest) {

} else {

Files.copy(source, dest);
Files.copy(source.toPath(), dest.toPath());

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
package org.italiangrid.storm.webdav.fs;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public interface FilesystemAccess {

public File mkdir(File parentDirectory, String dirName);

public boolean rm(File f);
public void rm(File f) throws IOException;

public void mv(File source, File dest);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static com.codahale.metrics.MetricRegistry.name;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import com.codahale.metrics.MetricRegistry;
Expand Down Expand Up @@ -68,12 +69,12 @@ public File mkdir(File parentDirectory, String dirName) {
}

@Override
public boolean rm(File f) {
public void rm(File f) throws IOException {

final Timer.Context context = rmTimer.time();

try {
return delegate.rm(f);
delegate.rm(f);

} finally {
context.stop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@
import java.io.InputStream;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

import org.italiangrid.storm.webdav.error.DirectoryNotEmpty;
import org.italiangrid.storm.webdav.error.StoRMWebDAVError;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.milton.http.Request;
import io.milton.http.exceptions.BadRequestException;
Expand All @@ -42,6 +45,8 @@
public class StoRMDirectoryResource extends StoRMResource implements PutableResource,
MakeCollectionableResource, DeletableResource, DeletableCollectionResource, CopyableResource {

private static final Logger logger = LoggerFactory.getLogger(StoRMDirectoryResource.class);

public StoRMDirectoryResource(StoRMResourceFactory factory, File f) {

super(factory, f);
Expand Down Expand Up @@ -112,8 +117,13 @@ public void delete() throws NotAuthorizedException, ConflictException, BadReques
throw new StoRMWebDAVError(e);
}

getFilesystemAccess().rm(getFile());

try {
getFilesystemAccess().rm(getFile());
} catch (NoSuchFileException e) {
logger.warn("Unable to remove directory {}: {}", getFile(), e.getMessage());
} catch (IOException e) {
throw new StoRMWebDAVError(e);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.io.OutputStream;
import java.net.FileNameMap;
import java.net.URLConnection;
import java.nio.file.NoSuchFileException;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -84,8 +85,14 @@ public StoRMFileResource(StoRMResourceFactory factory, File f) {
@Override
public void delete() throws NotAuthorizedException, ConflictException, BadRequestException {

getFilesystemAccess().rm(getFile());

try {
getFilesystemAccess().rm(getFile());
} catch (NoSuchFileException e) {
logger.warn("Unable to remove file {}: {}", getFile(), e.getMessage());
} catch (IOException e) {
logger.error("Unable to remove file {}: {}", getFile(), e.getMessage());
throw new StoRMWebDAVError(e);
}
}

protected void handleIOException(IOException e) {
Expand Down

0 comments on commit aee7266

Please sign in to comment.