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
|
|
|
|
2021-05-11 17:51:34 +02: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)) {
|
2021-03-04 14:59:41 +01:00
|
|
|
try (DirectoryStream<Path> paths = Files.newDirectoryStream(s)) {
|
|
|
|
return !paths.iterator().hasNext();
|
2020-12-29 22:22:23 +01:00
|
|
|
} catch (IOException e) {
|
2022-06-05 17:40:50 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2022-06-05 17:40:50 +02:00
|
|
|
}
|