You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by GitBox <gi...@apache.org> on 2018/12/27 02:31:39 UTC

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

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

 ##########
 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:
   From what I can see this code will always cause throwing a new 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:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org