Resclone/src/main/java/io/gitlab/jfronny/resclone/processors/RemoveEmptyProcessor.java

30 lines
1.1 KiB
Java
Raw Normal View History

2020-12-29 16:14:53 +01:00
package io.gitlab.jfronny.resclone.processors;
2023-02-26 14:00:38 +01:00
import io.gitlab.jfronny.resclone.Resclone;
2021-05-07 17:39:13 +02:00
import io.gitlab.jfronny.resclone.util.io.PathPruneVisitor;
2020-12-29 16:14:53 +01:00
import java.io.IOException;
2023-02-26 14:00:38 +01:00
import java.nio.file.*;
2020-12-29 16:14:53 +01:00
public class RemoveEmptyProcessor implements PackProcessor {
2020-12-29 16:14:53 +01:00
@Override
2021-04-03 02:40:38 +02:00
public void process(FileSystem p) throws Exception {
2020-12-29 22:22:23 +01:00
if (Files.exists(p.getPath("/assets"))) {
try {
Files.walkFileTree(p.getPath("/assets"), new PathPruneVisitor(s -> {
if (Files.isDirectory(s)) {
try (DirectoryStream<Path> paths = Files.newDirectoryStream(s)) {
return !paths.iterator().hasNext();
2020-12-29 22:22:23 +01:00
} catch (IOException e) {
Resclone.LOGGER.error("Could not check whether directory has entries", e);
2020-12-29 22:22:23 +01:00
}
2020-12-29 16:14:53 +01:00
}
2020-12-29 22:22:23 +01:00
return false;
}));
} catch (Throwable e) {
2021-04-03 02:40:38 +02:00
throw new Exception("Failed to prune empty directories", e);
2020-12-29 22:22:23 +01:00
}
2020-12-29 16:14:53 +01:00
}
}
}