From 273969fd77a7a7d9e638dec0b4ad9c9221aea780 Mon Sep 17 00:00:00 2001 From: zhongfly <11155705+zhongfly@users.noreply.github.com> Date: Mon, 20 Dec 2021 21:46:22 +0800 Subject: [PATCH] =?UTF-8?q?thumbnail:=20=E7=BC=A9=E7=95=A5=E5=9B=BE?= =?UTF-8?q?=E8=84=9A=E6=9C=AC=E7=9A=84=E8=87=AA=E5=8A=A8=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E5=85=BC=E5=AE=B9=E5=A4=9A=E5=AE=9E=E4=BE=8B=20(#68)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix: 缩略图脚本的自动删除兼容多实例 通过文件锁实现,参见https://github.com/hooke007/MPV_lazy/discussions/48#discussioncomment-1713433 * Fix: 总是尝试删除文件锁,无论是否需要自动删除 --- portable_config/scripts/thumbnailer.lua | 60 ++++++++++++++++++++----- 1 file changed, 50 insertions(+), 10 deletions(-) diff --git a/portable_config/scripts/thumbnailer.lua b/portable_config/scripts/thumbnailer.lua index 0220157a..f42a2765 100644 --- a/portable_config/scripts/thumbnailer.lua +++ b/portable_config/scripts/thumbnailer.lua @@ -185,6 +185,31 @@ local function delete_dir(path) return run_subprocess( OPERATING_SYSTEM == OS_WIN and {'cmd', '/e:on', '/c', 'rd', '/s', '/q', path} or {'rm', '-r', path} ) end +local function delete_file(path) + if not file_exists(path) then return end + msg.warn('Deleting File:', path) + return os.remove(path) +end + +local function add_lock(path) + msg.debug('Add file lock to:', path) + local file = io.open(join_paths(path, tostring(utils.getpid())), 'w') + if file then + file:close() + return true + end + return false +end + +local function remove_lock(path) + msg.debug('Remove file lock from:', path) + return delete_file(join_paths(path, tostring(utils.getpid()))) +end + +local function is_locked(path) + return #utils.readdir(path,'files') ~= 0 +end + -------------------- -- Data Structure -- @@ -424,6 +449,7 @@ end local stop_conditions local worker_script_path +local auto_delete = nil local function create_workers() local workers_requested = (state and state.max_workers) and state.max_workers or user_opts.max_workers @@ -474,6 +500,12 @@ local function create_ouput_dir(filepath, filename, dimension, rotate) end msg.debug('Creating Output Dir: Using ', name) + if auto_delete == nil then auto_delete = user_opts.auto_delete end + if auto_delete > 0 then + add_lock(user_opts.cache_dir) + add_lock(basepath) + end + local fullpath = join_paths(basepath, dimension, rotate) if not create_dir(fullpath) then return { basepath = nil, fullpath = nil } end return {basepath = basepath, fullpath = fullpath} @@ -633,26 +665,34 @@ local function is_thumbnailable() return true end -local auto_delete = nil - local function delete_cache_dir() if auto_delete == nil then auto_delete = user_opts.auto_delete end + local path = user_opts.cache_dir + remove_lock(path) if auto_delete > 0 then - local path = user_opts.cache_dir - msg.debug('Clearing Cache on Shutdown:', path) - if path:len() < 16 then return end - delete_dir(path) + if not is_locked(path) then + msg.debug('Clearing Cache on Shutdown:', path) + if path:len() < 16 then return end + delete_dir(path) + else + msg.debug('Clearing Cache on Shutdown:ignore ', path, '- Locked') + end end end local function delete_cache_subdir() if not state then return end if auto_delete == nil then auto_delete = user_opts.auto_delete end + local path = state.cache_dir_base + remove_lock(path) if auto_delete == 1 then - local path = state.cache_dir_base - msg.debug('Clearing Cache for File:', path) - if path:len() < 16 then return end - delete_dir(path) + if not is_locked(path) then + msg.debug('Clearing Cache for File:', path) + if path:len() < 16 then return end + delete_dir(path) + else + msg.debug('Clearing Cache for File:ignore ', path, '- Locked') + end end end