[GitHub] jvz commented on a change in pull request #72: [IO-596] Add DeleteFiles utility class

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[GitHub] jvz commented on a change in pull request #72: [IO-596] Add DeleteFiles utility class

GitBox
jvz commented on a change in pull request #72: [IO-596] Add DeleteFiles utility class
URL: https://github.com/apache/commons-io/pull/72#discussion_r244167531
 
 

 ##########
 File path: src/main/java/org/apache/commons/io/DeleteFiles.java
 ##########
 @@ -0,0 +1,339 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.io;
+
+import java.io.IOException;
+import java.nio.file.DirectoryStream;
+import java.nio.file.Files;
+import java.nio.file.LinkOption;
+import java.nio.file.Path;
+import java.nio.file.attribute.PosixFileAttributeView;
+import java.nio.file.attribute.PosixFilePermission;
+import java.util.ArrayList;
+import java.util.EnumSet;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Provides different strategies to delete files and directories.
+ */
+public final class DeleteFiles {
+
+    private static final DeleteFiles DEFAULT = newConfig().build();
+
+    /**
+     * Gets the default DeleteFiles strategy.
+     */
+    public static DeleteFiles getDefault() {
+        return DEFAULT;
+    }
+
+    /**
+     * Creates a new Config builder object to override options in DeleteFiles.
+     */
+    public static Config newConfig() {
+        return new Config();
+    }
+
+    private final int maxRetries;
+    private final long waitBetweenRetriesMillis;
+    private final double backoffMultiplier;
+    private final boolean retryOverridingFileAttributes;
+    private final boolean overrideAllAttributes;
+
+    private DeleteFiles(final int maxRetries, final long waitBetweenRetriesMillis, final double backoffMultiplier,
+                        final boolean retryOverridingFileAttributes, final boolean overrideAllAttributes) {
+        this.maxRetries = maxRetries;
+        this.waitBetweenRetriesMillis = waitBetweenRetriesMillis;
+        this.backoffMultiplier = backoffMultiplier;
+        this.retryOverridingFileAttributes = retryOverridingFileAttributes;
+        this.overrideAllAttributes = overrideAllAttributes;
+    }
+
+    /**
+     * Deletes a file or directory and its contents recursively.
+     *
+     * @param path path to delete
+     * @throws IOException if there is an error deleting anything
+     */
+    public void forceDelete(final Path path) throws IOException {
+        final List<IOException> accumulatedErrors = new ArrayList<>();
+        for (int retriesAttempted = 0; retriesAttempted <= maxRetries; retriesAttempted++) {
+            final List<IOException> errors = tryDeleteRecursive(path);
+            if (errors.isEmpty()) {
+                return;
+            }
+            accumulatedErrors.addAll(errors);
+            if (waitToRetry(retriesAttempted)) {
+                throw new CompositeIOException(failureMessage(path, retriesAttempted, true), accumulatedErrors);
+            }
+        }
+        throw new CompositeIOException(failureMessage(path, maxRetries, false), accumulatedErrors);
 
 Review comment:
   Line 80 with the `return` will terminate the loop. This only returns successfully if none of the subpaths threw an exception.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[hidden email]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]