Skip to content

Commit

Permalink
thumbnail: 缩略图脚本的自动删除兼容多实例 (#68)
Browse files Browse the repository at this point in the history
* Fix: 缩略图脚本的自动删除兼容多实例

通过文件锁实现,参见#48 (comment)

* Fix: 总是尝试删除文件锁,无论是否需要自动删除
  • Loading branch information
zhongfly authored Dec 20, 2021
1 parent bdcddf5 commit 273969f
Showing 1 changed file with 50 additions and 10 deletions.
60 changes: 50 additions & 10 deletions portable_config/scripts/thumbnailer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 --
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 273969f

Please sign in to comment.