aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlin Mr <almr.oss@outlook.com>2024-05-02 19:22:31 +0000
committerFlorian Fischer <florian.fischer@muhq.space>2024-05-03 08:29:01 +0200
commitf92cb0ef715867016ff33cbe4d5f621b3c1c85bd (patch)
tree6eba5583668e815159661ab09b5e503c42caf196
parent84051c589a1d94e3e84fe920c4f5a102476ca017 (diff)
downloadvis-lockfiles-f92cb0ef715867016ff33cbe4d5f621b3c1c85bd.tar.gz
vis-lockfiles-f92cb0ef715867016ff33cbe4d5f621b3c1c85bd.zip
init.lua: create & expand M.dir: only once
-rw-r--r--init.lua14
1 files changed, 12 insertions, 2 deletions
diff --git a/init.lua b/init.lua
index c9ac004..2481a14 100644
--- a/init.lua
+++ b/init.lua
@@ -14,6 +14,7 @@
-- If not, see <https://www.gnu.org/licenses/>.
local M = {}
M.directory = '${XDG_STATE_HOME:-${HOME}/.local/state}/vis/locks'
+M.directory_expanded = false
-- Helper to execute a command and capture its output
local function capture_cmd(cmd)
@@ -27,10 +28,16 @@ local function capture_cmd(cmd)
return s
end
+local function get_lock_dir()
+ if not M.directory_expanded then
+ M.directory = capture_cmd('printf %s "' .. M.directory .. '"')
+ end
+ return M.directory
+end
+
local function lock_file_path(path)
local filename = path:gsub('/', '%%')
- assert(os.execute('mkdir -p "' .. M.directory .. '"'))
- return capture_cmd('printf %s "' .. M.directory .. '/' .. filename .. '.lock"')
+ return get_lock_dir() .. '/' .. filename .. '.lock'
end
-- determine vis' pid
@@ -81,6 +88,9 @@ local function create_lock_file(lfile_path)
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()