From 7f79d5a020d2345704193aeb3cb17aee99c87b4a Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Thu, 18 Nov 2010 22:23:47 -0200 Subject: Fix usage of cfg.root_dir, which can now be either a string or a table. Thanks to Fabio Mascarenhas for the heads-up. --- src/luarocks/build.lua | 9 ++++----- src/luarocks/fs/lua.lua | 15 +++++++++++++++ src/luarocks/install.lua | 9 ++++----- src/luarocks/make.lua | 6 ++---- src/luarocks/util.lua | 1 - 5 files changed, 25 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/luarocks/build.lua b/src/luarocks/build.lua index 6eca0c3e..e6afde54 100644 --- a/src/luarocks/build.lua +++ b/src/luarocks/build.lua @@ -235,8 +235,9 @@ function build_rockspec(rockspec_file, need_to_fetch, minimal_mode) license = ("(license: "..rockspec.description.license..")") end + local root_dir = path.root_dir(cfg.rocks_dir) print() - print(name.." "..version.." is now built and installed in "..cfg.root_dir.." "..license) + print(name.." "..version.." is now built and installed in "..root_dir.." "..license) util.remove_scheduled_function(rollback) return true @@ -278,10 +279,8 @@ function run(...) end assert(type(version) == "string" or not version) - if not flags["local"] and (fs.exists(cfg.root_dir) and not fs.is_writable(cfg.root_dir)) then - return nil, "Your user does not have write permissions in " .. cfg.root_dir .. - " \n-- you may want to run as a privileged user or use your local tree with --local." - end + local ok, err = fs.check_command_permissions(flags) + if not ok then return nil, err end if name:match("%.rockspec$") then return build_rockspec(name, true) diff --git a/src/luarocks/fs/lua.lua b/src/luarocks/fs/lua.lua index 70df65be..5b3efd94 100644 --- a/src/luarocks/fs/lua.lua +++ b/src/luarocks/fs/lua.lua @@ -8,6 +8,7 @@ local fs = require("luarocks.fs") local cfg = require("luarocks.cfg") local dir = require("luarocks.dir") local util = require("luarocks.util") +local path = require("luarocks.path") local socket_ok, http = pcall(require, "socket.http") local _, ftp = pcall(require, "socket.ftp") @@ -615,3 +616,17 @@ function move(src, dest) end return true end + +--- Check if user has write permissions for the command. +-- Assumes the configuration variables under cfg have been previously set up. +-- @param flags table: the flags table passed to run() drivers. +-- @return boolean or (boolean, string): true on success, false on failure, +-- plus an error message. +function check_command_permissions(flags) + local root_dir = path.root_dir(cfg.rocks_dir) + if not flags["local"] and not fs.is_writable(root_dir) then + return nil, "Your user does not have write permissions in " .. root_dir .. + " \n-- you may want to run as a privileged user or use your local tree with --local." + end + return true +end diff --git a/src/luarocks/install.lua b/src/luarocks/install.lua index f9f27ccf..016e73bb 100644 --- a/src/luarocks/install.lua +++ b/src/luarocks/install.lua @@ -84,8 +84,9 @@ function install_binary_rock(rock_file) license = ("(license: "..rockspec.description.license..")") end + local root_dir = path.root_dir(cfg.rocks_dir) print() - print(name.." "..version.." is now installed in "..cfg.root_dir.." "..license) + print(name.." "..version.." is now installed in "..root_dir.." "..license) util.remove_scheduled_function(rollback) return true @@ -107,10 +108,8 @@ function run(...) return nil, "Argument missing, see help." end - if not flags["local"] and (fs.exists(cfg.root_dir) and not fs.is_writable(cfg.root_dir)) then - return nil, "Your user does not have write permissions in " .. cfg.root_dir .. - " \n-- you may want to run as a privileged user or use your local tree with --local." - end + local ok, err = fs.check_command_permissions(flags) + if not ok then return nil, err end if name:match("%.rockspec$") or name:match("%.src%.rock$") then local build = require("luarocks.build") diff --git a/src/luarocks/make.lua b/src/luarocks/make.lua index ec3772ac..4af5a16c 100644 --- a/src/luarocks/make.lua +++ b/src/luarocks/make.lua @@ -32,10 +32,8 @@ function run(...) local flags, rockspec = util.parse_flags(...) assert(type(rockspec) == "string" or not rockspec) - if not flags["local"] and not fs.is_writable(cfg.root_dir) then - return nil, "Your user does not have write permissions in " .. cfg.root_dir .. - " \n-- you may want to run as a privileged user or use your local tree with --local." - end + local ok, err = fs.check_command_permissions(flags) + if not ok then return nil, err end if not rockspec then local files = fs.list_dir(fs.current_dir()) diff --git a/src/luarocks/util.lua b/src/luarocks/util.lua index ed70b2ba..10fc1e36 100644 --- a/src/luarocks/util.lua +++ b/src/luarocks/util.lua @@ -362,4 +362,3 @@ function show_table(t, name, indent) addtocart(t, name, indent) return cart .. autoref end - -- cgit v1.2.3-55-g6feb