diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2024-05-03 09:13:00 +0200 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2024-05-03 09:13:00 +0200 |
| commit | 064717fcc800d9ca94366745610305d12a5532a4 (patch) | |
| tree | 538e14ed2bccd4d176c163a22f709def397df9a1 /init.lua | |
| parent | 6bce026b0c664ba9686a37e614330cb84be3a5e6 (diff) | |
| download | vis-lockfiles-064717fcc800d9ca94366745610305d12a5532a4.tar.gz vis-lockfiles-064717fcc800d9ca94366745610305d12a5532a4.zip | |
split lock file functions into separate file to enable testing
Diffstat (limited to 'init.lua')
| -rw-r--r-- | init.lua | 87 |
1 files changed, 13 insertions, 74 deletions
@@ -12,51 +12,23 @@ -- You should have received a copy of the GNU General Public License along with -- vis-lockfiles found in the LICENSE file. -- If not, see <https://www.gnu.org/licenses/>. +local source_str = debug.getinfo(1, 'S').source:sub(2) +local script_path = source_str:match('(.*/)') + +local lockfiles = dofile(script_path .. 'lockfiles.lua') + local M = {} M.directory = '${XDG_STATE_HOME:-${HOME}/.local/state}/vis/locks' M.directory_expanded = nil --- Helper to execute a command and capture its output -local function capture_cmd(cmd) - local p = assert(io.popen(cmd, 'r')) - local s = assert(p:read('*a')) - local success, _, status = p:close() - if not success then - local err = cmd .. ' failed with exit code: ' .. status - vis:info('vis-lockfiles: ' .. err) - end - return s -end - local function get_lock_dir() if M.directory_expanded ~= M.directory then - M.directory = capture_cmd('printf %s "' .. M.directory .. '"') + M.directory = lockfiles.capture_cmd('printf %s "' .. M.directory .. '"') M.directory_expanded = M.directory end return M.directory end -local function lock_file_path(path) - -- escape '%' first - local filename = path:gsub('%%', '%%25'):gsub('/', '%%2F') - return get_lock_dir() .. '/' .. filename .. '.lock' -end - --- determine vis' pid -local vis_pid -do - local vis_proc_file = io.open('/proc/self/stat', 'r') - if vis_proc_file then - vis_pid = vis_proc_file:read('*n') - vis_proc_file:close() - - else -- fallback if /proc/self/stat is not available - local out = capture_cmd('sh -c "echo $PPID"') - vis_pid = tonumber(out) - end -end -assert(vis_pid) - -- get a user confirmation -- return true if user selected yes, false otherwise local function confirm(path) @@ -82,49 +54,15 @@ local function confirm(path) end local created_locks = {} -local function create_lock_file(lfile_path) - local lock_file = io.open(lfile_path) - if lock_file then - lock_file:close() - return false - end - - -- create the lock file with our pid - if not io.open(lfile_path, 'w') then - assert(os.execute('mkdir -p "' .. get_lock_dir() .. '"')) - end - lock_file = assert(io.open(lfile_path, 'w')) - lock_file:write(vis_pid) - lock_file:close() - - -- detect a possible race - lock_file = assert(io.open(lfile_path)) - local lock_pid = lock_file:read('*n') - lock_file:close() - local locked = tonumber(lock_pid) == vis_pid - - -- remember we created the lock file - if locked then - created_locks[lfile_path] = true - end - - return locked -end - -local function delete_lock_file(lfile_path) - local success, err = os.remove(lfile_path) - if not success then - vis:info('vis-lockfiles: ' .. err) - end - created_locks[lfile_path] = nil -end vis.events.subscribe(vis.events.FILE_OPEN, function(file) if not file.path then return end - local lfile_path = lock_file_path(file.path) - if not create_lock_file(lfile_path) and not confirm(lfile_path) then + local lfile_path = lockfiles.lock_file_path(get_lock_dir(), file.path) + if lockfiles.create_lock_file(get_lock_dir(), lfile_path) then + created_locks[lfile_path] = true + elseif not confirm(lfile_path) then os.exit(1) end end) @@ -133,9 +71,10 @@ vis.events.subscribe(vis.events.FILE_CLOSE, function(file) if not file.path then return end - local lfile_path = lock_file_path(file.path) + local lfile_path = lockfiles.lock_file_path(get_lock_dir(), file.path) if created_locks[lfile_path] then - delete_lock_file(lfile_path) + lockfiles.delete_lock_file(lfile_path) + created_locks[lfile_path] = nil end end) |
