From 956fb45ef5c3a56f84532493d12ac5a68fec2597 Mon Sep 17 00:00:00 2001 From: David Maisonave <47364845+David-Maisonave@users.noreply.github.com> Date: Tue, 13 Aug 2024 04:23:13 -0400 Subject: [PATCH] Added logic to check DB file has correct prefix before deleting --- plugins/FileMonitor/filemonitor.py | 14 +++++--- plugins/FileMonitor/filemonitor_config.py | 39 ++++++++++++----------- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/plugins/FileMonitor/filemonitor.py b/plugins/FileMonitor/filemonitor.py index 2bd296d..ca7bcf5 100644 --- a/plugins/FileMonitor/filemonitor.py +++ b/plugins/FileMonitor/filemonitor.py @@ -1,7 +1,7 @@ # Description: This is a Stash plugin which updates Stash if any changes occurs in the Stash library paths. # By David Maisonave (aka Axter) Jul-2024 (https://www.axter.com/) # Get the latest developers version from following link: https://github.com/David-Maisonave/Axter-Stash/tree/main/plugins/FileMonitor -# Note: To call this script outside of Stash, pass --url and the Stash URL. +# Note: To call this script outside of Stash, pass argument --url and the Stash URL. # Example: python filemonitor.py --url http://localhost:9999 import os, sys, time, pathlib, argparse from StashPluginHelper import StashPluginHelper @@ -51,13 +51,13 @@ signal = Condition(mutex) shouldUpdate = False TargetPaths = [] -SHAREDMEMORY_NAME = "DavidMaisonaveAxter_FileMonitor" +SHAREDMEMORY_NAME = "DavidMaisonaveAxter_FileMonitor" # Unique name for shared memory RECURSIVE = plugin.pluginSettings["recursiveDisabled"] == False SCAN_MODIFIED = plugin.pluginConfig["scanModified"] RUN_CLEAN_AFTER_DELETE = plugin.pluginConfig["runCleanAfterDelete"] RUN_GENERATE_CONTENT = plugin.pluginConfig['runGenerateContent'] SCAN_ON_ANY_EVENT = plugin.pluginConfig['onAnyEvent'] -SIGNAL_TIMEOUT = plugin.pluginConfig['timeOut'] +SIGNAL_TIMEOUT = plugin.pluginConfig['timeOut'] if plugin.pluginConfig['timeOut'] > 0 else 1 CREATE_SPECIAL_FILE_TO_EXIT = plugin.pluginConfig['createSpecFileToExit'] DELETE_SPECIAL_FILE_ON_STOP = plugin.pluginConfig['deleteSpecFileInStop'] @@ -205,12 +205,16 @@ class StashScheduler: # Stash Scheduler if len(dbPath) < 5: # For safety and security, short path not supported. plugin.LogOnce(f"Exiting trimDbFiles, because path {dbPath} is to short. Len={len(dbPath)}. Path string must be at least 5 characters in length.") return + stashPrefixSqlDbFileName = "stash-go.sqlite." dbFiles = sorted(os.listdir(dbPath)) n = len(dbFiles) for i in range(0, n-maxFiles): dbFilePath = f"{dbPath}{os.sep}{dbFiles[i]}" - plugin.Warn(f"Deleting file {dbFilePath}") - os.remove(dbFilePath) + if dbFiles[i].startswith(stashPrefixSqlDbFileName): + plugin.Warn(f"Deleting DB file {dbFilePath}") + os.remove(dbFilePath) + else: + plugin.LogOnce(f"Skipping deleting file {dbFiles[i]} because the file doesn't start with {stashPrefixSqlDbFileName}.") def checkSchedulePending(self): import schedule # pip install schedule # https://github.com/dbader/schedule diff --git a/plugins/FileMonitor/filemonitor_config.py b/plugins/FileMonitor/filemonitor_config.py index 5c28895..fbfcfba 100644 --- a/plugins/FileMonitor/filemonitor_config.py +++ b/plugins/FileMonitor/filemonitor_config.py @@ -2,21 +2,6 @@ # By David Maisonave (aka Axter) Jul-2024 (https://www.axter.com/) # Get the latest developers version from following link: https://github.com/David-Maisonave/Axter-Stash/tree/main/plugins/FileMonitor config = { - # Enable to run metadata_generate (Generate Content) after metadata scan. - "runGenerateContent": False, - # Enable to run scan when triggered by on_any_event. - "onAnyEvent": False, - # Enable to monitor changes in file system for modification flag. This option is NOT needed for Windows, because on Windows changes are triggered via CREATE, DELETE, and MOVE flags. Other OS may differ. - "scanModified": False, - # Timeout in seconds. This is how often it will check the scheduler and (in-plugin mode) if another job (Task) is in the queue - "timeOut": 60, - # Enable to exit FileMonitor by creating special file in plugin folder\working - "createSpecFileToExit": True, - # Enable to delete special file imediately after it's created in stop process - "deleteSpecFileInStop": False, - # Enable to run metadata clean task after file deletion. - "runCleanAfterDelete": False, - # The task scheduler list. # Task can be scheduled to run monthly, weekly, hourly, and by minutes. For best results use the scheduler with FileMonitor running as a service. # The frequency field can be in minutes or hours. A zero frequency value disables the task. @@ -45,6 +30,7 @@ config = { # 4 = 4th specified weekday of the month. # Example monthly method. {"task" : "Backup", "weekday" : "sunday", "time" : "01:00", "monthly" : 2}, # Backup -> [Backup] 2nd sunday of the month at 1AM (01:00) + # {"task" : "Backup", "seconds" : 30}, # Example commented out test task. # Note: # The below example tasks are done using hours and minutes because the task is easily disabled (deactivated) by a zero value entry. @@ -63,11 +49,28 @@ config = { {"task" : "execute", "command" : "C:\\MyPath\\HelloWorld.bat", "args" : "", "hours" : 0}, ], - # When enabled, if CREATE flag is triggered, DupFileManager task is called if the plugin is installed. - "onCreateCallDupFileManager": False, # Not yet implemented!!!! + # Timeout in seconds. This is how often FileMonitor will check the scheduler and (in-plugin mode) check if another job (Task) is in the queue. + "timeOut": 60, + # Enable to run metadata clean task after file deletion. + "runCleanAfterDelete": False, + # Enable to run metadata_generate (Generate Content) after metadata scan. + "runGenerateContent": False, - # The following fields are ONLY used when running FileMonitor in script mode + # The following fields are ONLY used when running FileMonitor in script mode. "endpoint_Scheme" : "http", # Define endpoint to use when contacting the Stash server "endpoint_Host" : "0.0.0.0", # Define endpoint to use when contacting the Stash server "endpoint_Port" : 9999, # Define endpoint to use when contacting the Stash server + + # The following are advanced user options. + # Enable to run scan when triggered by on_any_event. + "onAnyEvent": False, # If enabled may cause excessive triggers. + # Enable to monitor changes in file system for modification flag. This option is NOT needed for Windows, because on Windows changes are triggered via CREATE, DELETE, and MOVE flags. Other OS may differ. + "scanModified": False, # Warning: Enabling this in Windows OS may cause excessive triggers when user is only viewing directory content. + # Enable to exit FileMonitor by creating special file in plugin folder\working + "createSpecFileToExit": True, + # Enable to delete special file imediately after it's created in stop process. + "deleteSpecFileInStop": False, + + # When enabled, if CREATE flag is triggered, DupFileManager task is called if the plugin is installed. + "onCreateCallDupFileManager": False, # Not yet implemented!!!! }