gnmyt / MCDash

MCDash is a simple dashboard for your Minecraft server. It contains a http server that allows the dashboard to manage the server
http://mcdash.gnmyt.dev/
MIT License
89 stars 17 forks source link

[BUG] Error when checking size of volatile files #167

Open marcjoancr opened 2 months ago

marcjoancr commented 2 months ago

I have CoreProtect and it looks like uses db-wal, and it is a volatile file. If you use FileUtils crawling the files maybe check if it exists before checking size may solve the bug or maybe just with a simple try/catch of NoSuchFileException.

Aug 22 20:28:45 [20:28:45 WARN]: java.lang.reflect.InvocationTargetException
Aug 22 20:28:45 [20:28:45 WARN]:         at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:118)
Aug 22 20:28:45 [20:28:45 WARN]:         at java.base/java.lang.reflect.Method.invoke(Method.java:580)
Aug 22 20:28:45 [20:28:45 WARN]:         at MCDash.jar//de.gnmyt.mcdash.api.handler.DefaultHandler.execute(DefaultHandler.java:79)
Aug 22 20:28:45 [20:28:45 WARN]:         at MCDash.jar//de.gnmyt.mcdash.api.handler.DefaultHandler.lambda$handle$0(DefaultHandler.java:67)
Aug 22 20:28:45 [20:28:45 WARN]:         at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:572)
Aug 22 20:28:45 [20:28:45 WARN]:         at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317)
Aug 22 20:28:45 [20:28:45 WARN]:         at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)
Aug 22 20:28:45 [20:28:45 WARN]:         at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
Aug 22 20:28:45 [20:28:45 WARN]:         at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
Aug 22 20:28:45 [20:28:45 WARN]:         at java.base/java.lang.Thread.run(Thread.java:1583)
Aug 22 20:28:45 [20:28:45 WARN]: Caused by: java.io.UncheckedIOException: java.nio.file.NoSuchFileException: ./plugins/CoreProtect/database.db-wal
Aug 22 20:28:45 [20:28:45 WARN]:         at org.apache.commons.io.function.Uncheck.wrap(Uncheck.java:339)
Aug 22 20:28:45 [20:28:45 WARN]:         at org.apache.commons.io.function.Uncheck.get(Uncheck.java:199)
Aug 22 20:28:45 [20:28:45 WARN]:         at org.apache.commons.io.FileUtils.sizeOfDirectory(FileUtils.java:2947)
Aug 22 20:28:45 [20:28:45 WARN]:         at MCDash.jar//de.gnmyt.mcdash.api.controller.StatsController.getUsedSpace(StatsController.java:87)
Aug 22 20:28:45 [20:28:45 WARN]:         at MCDash.jar//de.gnmyt.mcdash.panel.routes.stats.StatsRoute.get(StatsRoute.java:23)
Aug 22 20:28:45 [20:28:45 WARN]:         at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
Aug 22 20:28:45 [20:28:45 WARN]:         ... 9 more
Aug 22 20:28:45 [20:28:45 WARN]: Caused by: java.nio.file.NoSuchFileException: ./plugins/CoreProtect/database.db-wal
Aug 22 20:28:45 [20:28:45 WARN]:         at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:92)
Aug 22 20:28:45 [20:28:45 WARN]:         at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:106)
Aug 22 20:28:45 [20:28:45 WARN]:         at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111)
Aug 22 20:28:45 [20:28:45 WARN]:         at java.base/sun.nio.fs.UnixFileAttributeViews$Basic.readAttributes(UnixFileAttributeViews.java:55)
Aug 22 20:28:45 [20:28:45 WARN]:         at java.base/sun.nio.fs.UnixFileSystemProvider.readAttributes(UnixFileSystemProvider.java:171)
Aug 22 20:28:45 [20:28:45 WARN]:         at java.base/sun.nio.fs.LinuxFileSystemProvider.readAttributes(LinuxFileSystemProvider.java:99)
Aug 22 20:28:45 [20:28:45 WARN]:         at java.base/java.nio.file.Files.readAttributes(Files.java:1853)
Aug 22 20:28:45 [20:28:45 WARN]:         at java.base/java.nio.file.FileTreeWalker.getAttributes(FileTreeWalker.java:220)
Aug 22 20:28:45 [20:28:45 WARN]:         at java.base/java.nio.file.FileTreeWalker.visit(FileTreeWalker.java:277)
Aug 22 20:28:45 [20:28:45 WARN]:         at java.base/java.nio.file.FileTreeWalker.next(FileTreeWalker.java:374)
Aug 22 20:28:45 [20:28:45 WARN]:         at java.base/java.nio.file.Files.walkFileTree(Files.java:2820)
Aug 22 20:28:45 [20:28:45 WARN]:         at java.base/java.nio.file.Files.walkFileTree(Files.java:2857)
Aug 22 20:28:45 [20:28:45 WARN]:         at org.apache.commons.io.file.PathUtils.visitFileTree(PathUtils.java:1653)
Aug 22 20:28:45 [20:28:45 WARN]:         at org.apache.commons.io.file.PathUtils.countDirectory(PathUtils.java:349)
Aug 22 20:28:45 [20:28:45 WARN]:         at org.apache.commons.io.file.PathUtils.sizeOfDirectory(PathUtils.java:1591)
Aug 22 20:28:45 [20:28:45 WARN]:         at org.apache.commons.io.FileUtils.lambda$sizeOfDirectory$15(FileUtils.java:2947)
Aug 22 20:28:45 [20:28:45 WARN]:         at org.apache.commons.io.function.Uncheck.get(Uncheck.java:197)
Aug 22 20:28:45 [20:28:45 WARN]:         ... 13 more
marcjoancr commented 2 months ago

I just checked the code (sorry for not doing it before opening the issue):

    public long getUsedSpace() {
        return FileUtils.sizeOfDirectory(SERVER_FOLDER);
    }

Seems like if you do a try/catch you will not return any value, I suggest doing a crawler in order to check the files safely and always having a value to return, like so:


    public long getUsedSpace() {
        final long[] size = {0};

        Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                size[0] += attrs.size();
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                if (exc instanceof NoSuchFileException) {
                    return FileVisitResult.CONTINUE;
                }
            }
        });

        return size[0];
    }

Disclaimer: Haven't tested it.