From 8d5cb0ce23e72c6079b40971aac2a3e45327261f Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Mon, 2 Dec 2024 21:34:24 +0100 Subject: [PATCH] Add more default directories --- .../engine/base/utility/FileAccess.java | 84 +++++++++++++++++-- 1 file changed, 76 insertions(+), 8 deletions(-) diff --git a/base/src/main/java/de/staropensource/engine/base/utility/FileAccess.java b/base/src/main/java/de/staropensource/engine/base/utility/FileAccess.java index 53a4c7c..6b8f5f8 100644 --- a/base/src/main/java/de/staropensource/engine/base/utility/FileAccess.java +++ b/base/src/main/java/de/staropensource/engine/base/utility/FileAccess.java @@ -62,18 +62,78 @@ public final class FileAccess { /** * Contains a {@link FileAccess} instance to - * a cache directory provided by the engine. + * a temporary cache directory provided by the engine. * - * @since v1-alpha8 + * @since v1-alpha9 * -- GETTER -- * Returns a {@link FileAccess} instance to - * a cache directory provided by the engine. + * a temporary cache directory provided by the engine. * - * @return cache directory - * @since v1-alpha8 + * @return temporary cache directory + * @since v1-alpha9 */ @Getter - private static FileAccess cacheDirectory; + private static FileAccess temporaryCacheDirectory; + + /** + * Contains a {@link FileAccess} instance to + * the persistent cache directory of the system. + * + * @since v1-alpha9 + * -- GETTER -- + * Returns a {@link FileAccess} instance to + * the persistent cache directory of the system. + * + * @return persistent cache directory + * @since v1-alpha9 + */ + @Getter + private static FileAccess persistentCacheDirectory; + + /** + * Contains a {@link FileAccess} instance + * to the user's home directory. + * + * @since v1-alpha9 + * -- GETTER -- + * Returns a {@link FileAccess} instance + * to the user's home directory. + * + * @return home directory + * @since v1-alpha9 + */ + @Getter + private static FileAccess homeDirectory; + + /** + * Contains a {@link FileAccess} instance + * to the user's configuration directory. + * + * @since v1-alpha9 + * -- GETTER -- + * Returns a {@link FileAccess} instance + * to the user's configuration directory. + * + * @return configuration directory + * @since v1-alpha9 + */ + @Getter + private static FileAccess configDirectory; + + /** + * Contains a {@link FileAccess} instance + * to the user's data directory. + * + * @since v1-alpha9 + * -- GETTER -- + * Returns a {@link FileAccess} instance + * to the user's data directory. + * + * @return data directory + * @since v1-alpha9 + */ + @Getter + private static FileAccess dataDirectory; // -----> Instance variables /** @@ -167,8 +227,15 @@ public final class FileAccess { * @since v1-alpha8 */ public static void initializeInstances() throws IOException { - if (Engine.getInstance().getState() == EngineState.EARLY_STARTUP) - cacheDirectory = new FileAccess(System.getProperties().getProperty("java.io.tmpdir").replace("\\", "/") + "/sosengine-cache-" + ProcessHandle.current().pid()).createDirectory().deleteOnShutdown(); + // TODO add support for non-default and/or non-unix directories here + + if (Engine.getInstance().getState() == EngineState.EARLY_STARTUP) { + temporaryCacheDirectory = new FileAccess(System.getProperties().getProperty("java.io.tmpdir") + "/sosengine-cache-" + ProcessHandle.current().pid()).createDirectory().deleteOnShutdown(); + homeDirectory = new FileAccess(System.getProperties().getProperty("user.home")).createDirectory(); + persistentCacheDirectory = homeDirectory.traverse( ".cache").createDirectory(); + configDirectory = homeDirectory.traverse(".config").createDirectory(); + dataDirectory = homeDirectory.traverse(".local/share").createDirectory(); + } } /** * Deletes all files scheduled for deletion. @@ -207,6 +274,7 @@ public final class FileAccess { public static @NotNull Path formatPath(@NotNull String path) { return Path.of( path + .replace("\\", "/") .replace("/./", "/") .replace("/", File.separator) );