From 742dc2254e3a58c69a1d67def04852f53753199c Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Wed, 9 May 2012 01:02:00 +0200 Subject: prosodyctl: Replace hack with lfs for checking if a file exists --- prosodyctl | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'prosodyctl') diff --git a/prosodyctl b/prosodyctl index 1c4d84cd..4511ec2c 100755 --- a/prosodyctl +++ b/prosodyctl @@ -614,14 +614,18 @@ function commands.unregister(arg) end local openssl = require "util.openssl"; +local lfs = require "lfs"; local cert_commands = {}; +local function ask_overwrite(filename) + return lfs.attributes(filename) and not show_yesno("Overwrite "..filename .. "?"); +end + function cert_commands.config(arg) if #arg >= 1 and arg[1] ~= "--help" then local conf_filename = (CFG_DATADIR or ".") .. "/" .. arg[1] .. ".cnf"; - if os.execute("test -f "..conf_filename) == 0 - and not show_yesno("Overwrite "..conf_filename .. "?") then + if ask_overwrite(conf_filename) then return nil, conf_filename; end local conf = openssl.config.new(); @@ -651,12 +655,10 @@ end function cert_commands.key(arg) if #arg >= 1 and arg[1] ~= "--help" then local key_filename = (CFG_DATADIR or ".") .. "/" .. arg[1] .. ".key"; - if os.execute("test -f "..key_filename) == 0 then - if not show_yesno("Overwrite "..key_filename .. "?") then - return nil, key_filename; - end - os.remove(key_filename); -- We chmod this file to not have write permissions + if ask_overwrite(key_filename) then + return nil, key_filename; end + os.remove(key_filename); -- We chmod this file to not have write permissions local key_size = tonumber(arg[2] or show_prompt("Choose key size (2048):") or 2048); if openssl.genrsa{out=key_filename, key_size} then os.execute(("chmod 400 '%s'"):format(key_filename)); @@ -672,8 +674,7 @@ end function cert_commands.request(arg) if #arg >= 1 and arg[1] ~= "--help" then local req_filename = (CFG_DATADIR or ".") .. "/" .. arg[1] .. ".req"; - if os.execute("test -f "..req_filename) == 0 - and not show_yesno("Overwrite "..req_filename .. "?") then + if ask_overwrite(req_filename) then return nil, req_filename; end local _, key_filename = cert_commands.key({arg[1]}); @@ -691,9 +692,8 @@ end function cert_commands.generate(arg) if #arg >= 1 and arg[1] ~= "--help" then local cert_filename = (CFG_DATADIR or ".") .. "/" .. arg[1] .. ".cert"; - if os.execute("test -f "..cert_filename) == 0 - and not show_yesno("Overwrite "..cert_filename .. "?") then - return nil, cert_filename; + if ask_overwrite(cert_filename) then + return nil, conf_filename; end local _, key_filename = cert_commands.key({arg[1]}); local _, conf_filename = cert_commands.config({arg[1]}); -- cgit v1.2.3