This is an automated email from the ASF dual-hosted git repository.

rnewson pushed a commit to branch deleteall-fix
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit e64e7c480ceee0be30626cdb33914dc302f4d673
Author: Robert Newson <[email protected]>
AuthorDate: Wed Oct 28 14:37:38 2026 +0000

    change deleteAll to work cross-platform
---
 .../apache/couchdb/nouveau/core/IndexManager.java  | 64 ++++++++++------------
 1 file changed, 30 insertions(+), 34 deletions(-)

diff --git 
a/extra/nouveau/src/main/java/org/apache/couchdb/nouveau/core/IndexManager.java 
b/extra/nouveau/src/main/java/org/apache/couchdb/nouveau/core/IndexManager.java
index bd55ced16..c926ce4f4 100644
--- 
a/extra/nouveau/src/main/java/org/apache/couchdb/nouveau/core/IndexManager.java
+++ 
b/extra/nouveau/src/main/java/org/apache/couchdb/nouveau/core/IndexManager.java
@@ -20,9 +20,12 @@ import jakarta.ws.rs.WebApplicationException;
 import jakarta.ws.rs.core.Response.Status;
 import java.io.File;
 import java.io.IOException;
+import java.nio.file.DirectoryNotEmptyException;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.StandardCopyOption;
+import java.util.Arrays;
+import java.util.Collections;
 import java.util.HashSet;
 import java.util.LinkedHashMap;
 import java.util.List;
@@ -248,50 +251,43 @@ public final class IndexManager implements Managed {
         return Files.exists(indexDefinitionPath(name));
     }
 
-    public void deleteAll(final String path, final List<String> exclusions) 
throws IOException {
-        LOGGER.info(
-                "deleting indexes matching {} (excluding {})",
-                path,
-                exclusions == null || exclusions.isEmpty() ? "nothing" : 
exclusions);
+    public void deleteAll(final String path, List<String> exclusions) throws 
IOException {
+        if (exclusions == null) {
+            exclusions = Collections.emptyList();
+        }
+        LOGGER.info("deleting indexes matching {} (excluding {})", path, 
exclusions);
         var parts = path.split("/");
-        deleteAll(rootDir, parts, 0, exclusions);
+        deleteAll(rootDir, parts, exclusions);
     }
 
-    private void deleteAll(final Path path, final String[] parts, final int 
index, final List<String> exclusions)
-            throws IOException {
-        // End of the path
-        if (index == parts.length - 1) {
-            try (var stream = Files.newDirectoryStream(path, parts[index])) {
-                stream.forEach(p -> {
-                    if (exclusions != null && 
exclusions.indexOf(p.getFileName().toString()) != -1) {
-                        return;
-                    }
-                    final String relativeName = 
rootDir.relativize(p).toString();
+    private void deleteAll(final Path path, final String[] parts, final 
List<String> exclusions) throws IOException {
+        var glob = String.join(File.separator, "**", parts[0]);
+        var m = path.getFileSystem().getPathMatcher("glob:" + glob);
+        var subpaths = Files.list(path).toList();
+        for (var p : subpaths) {
+            if (m.matches(p)) {
+                if (parts.length > 1) {
+                    deleteAll(p, Arrays.copyOfRange(parts, 1, parts.length), 
exclusions);
+                } else if (!exclusions.contains(p.getFileName().toString())) {
+                    var indexName = rootDir.relativize(p).toString();
                     try {
-                        deleteIndex(relativeName);
-                    } catch (final IOException | InterruptedException e) {
-                        LOGGER.error("Exception deleting {}", p, e);
+                        deleteIndex(indexName);
+                    } catch (InterruptedException e) {
+                        throw new WebApplicationException(
+                                "Interrupted while deleting " + indexName, e, 
Status.INTERNAL_SERVER_ERROR);
                     }
                     // Clean any newly empty directories.
                     do {
-                        final File f = p.toFile();
-                        if (f.isDirectory() && f.list().length == 0) {
-                            f.delete();
+                        if (Files.isDirectory(p)) {
+                            try {
+                                Files.delete(p);
+                            } catch (DirectoryNotEmptyException e) {
+                                // ignore
+                            }
                         }
                     } while ((p = p.getParent()) != null && 
!rootDir.equals(p));
-                });
-            }
-            return;
-        }
-        // Recurse
-        try (var stream = Files.newDirectoryStream(path, parts[index])) {
-            stream.forEach(p -> {
-                try {
-                    deleteAll(p, parts, index + 1, exclusions);
-                } catch (IOException e) {
-                    LOGGER.warn("Exception during delete of " + 
rootDir.relativize(p), e);
                 }
-            });
+            }
         }
     }
 

Reply via email to