diff options
| author | Hisham Muhammad <hisham@gobolinux.org> | 2015-01-12 17:41:56 -0200 |
|---|---|---|
| committer | Hisham Muhammad <hisham@gobolinux.org> | 2015-01-12 17:41:56 -0200 |
| commit | e7f9680bd66191b575e10ec2c9231bfff30694b7 (patch) | |
| tree | ebd20e56ef18ec9236326bf9c9e34c22cd7fe85e | |
| parent | db46b2203d2348c3b470605430cdb183466e259e (diff) | |
| download | luarocks-e7f9680bd66191b575e10ec2c9231bfff30694b7.tar.gz luarocks-e7f9680bd66191b575e10ec2c9231bfff30694b7.tar.bz2 luarocks-e7f9680bd66191b575e10ec2c9231bfff30694b7.zip | |
Error out on bad config files.
Alternative implementation to the one given by @Tieske,
following discussion in #260.
Closes #260. Closes #228.
| -rw-r--r-- | src/luarocks/cfg.lua | 27 | ||||
| -rw-r--r-- | src/luarocks/persist.lua | 63 |
2 files changed, 54 insertions, 36 deletions
diff --git a/src/luarocks/cfg.lua b/src/luarocks/cfg.lua index 2d02230b..32ab1f37 100644 --- a/src/luarocks/cfg.lua +++ b/src/luarocks/cfg.lua | |||
| @@ -44,6 +44,7 @@ cfg.errorcodes = setmetatable({ | |||
| 44 | OK = 0, | 44 | OK = 0, |
| 45 | UNSPECIFIED = 1, | 45 | UNSPECIFIED = 1, |
| 46 | PERMISSIONDENIED = 2, | 46 | PERMISSIONDENIED = 2, |
| 47 | CONFIGFILE = 3, | ||
| 47 | CRASH = 99 | 48 | CRASH = 99 |
| 48 | },{ | 49 | },{ |
| 49 | __index = function(t, key) | 50 | __index = function(t, key) |
| @@ -145,28 +146,29 @@ cfg.variables = {} | |||
| 145 | cfg.rocks_trees = {} | 146 | cfg.rocks_trees = {} |
| 146 | 147 | ||
| 147 | sys_config_file = site_config.LUAROCKS_SYSCONFIG or sys_config_dir.."/config-"..cfg.lua_version..".lua" | 148 | sys_config_file = site_config.LUAROCKS_SYSCONFIG or sys_config_dir.."/config-"..cfg.lua_version..".lua" |
| 148 | local err | 149 | local err, errcode |
| 149 | sys_config_ok, err = persist.load_into_table(sys_config_file, cfg) | 150 | sys_config_ok, err, errcode = persist.load_into_table(sys_config_file, cfg) |
| 150 | 151 | ||
| 151 | if not sys_config_ok then | 152 | if (not sys_config_ok) and errcode ~= "run" then |
| 152 | sys_config_file = sys_config_dir.."/config.lua" | 153 | sys_config_file = sys_config_dir.."/config.lua" |
| 153 | sys_config_ok, err = persist.load_into_table(sys_config_file, cfg) | 154 | sys_config_ok, err, errcode = persist.load_into_table(sys_config_file, cfg) |
| 154 | end | 155 | end |
| 155 | if err and sys_config_ok == nil then | 156 | if (not sys_config_ok) and errcode ~= "open" then |
| 156 | io.stderr:write(err.."\n") | 157 | io.stderr:write(err.."\n") |
| 158 | os.exit(cfg.errorcodes.CONFIGFILE) | ||
| 157 | end | 159 | end |
| 158 | 160 | ||
| 159 | if not site_config.LUAROCKS_FORCE_CONFIG then | 161 | if not site_config.LUAROCKS_FORCE_CONFIG then |
| 160 | local home_overrides, err | 162 | local home_overrides, err, errcode |
| 161 | home_config_file = os.getenv("LUAROCKS_CONFIG_" .. version_suffix) or os.getenv("LUAROCKS_CONFIG") | 163 | home_config_file = os.getenv("LUAROCKS_CONFIG_" .. version_suffix) or os.getenv("LUAROCKS_CONFIG") |
| 162 | if home_config_file then | 164 | if home_config_file then |
| 163 | home_overrides, err = persist.load_into_table(home_config_file, { home = cfg.home, lua_version = cfg.lua_version }) | 165 | home_overrides, err, errcode = persist.load_into_table(home_config_file, { home = cfg.home, lua_version = cfg.lua_version }) |
| 164 | else | 166 | else |
| 165 | home_config_file = home_config_dir.."/config-"..cfg.lua_version..".lua" | 167 | home_config_file = home_config_dir.."/config-"..cfg.lua_version..".lua" |
| 166 | home_overrides, err = persist.load_into_table(home_config_file, { home = cfg.home, lua_version = cfg.lua_version }) | 168 | home_overrides, err, errcode = persist.load_into_table(home_config_file, { home = cfg.home, lua_version = cfg.lua_version }) |
| 167 | if not home_overrides then | 169 | if (not home_overrides) and (not errcode == "run") then |
| 168 | home_config_file = home_config_dir.."/config.lua" | 170 | home_config_file = home_config_dir.."/config.lua" |
| 169 | home_overrides, err = persist.load_into_table(home_config_file, { home = cfg.home, lua_version = cfg.lua_version }) | 171 | home_overrides, err, errcode = persist.load_into_table(home_config_file, { home = cfg.home, lua_version = cfg.lua_version }) |
| 170 | end | 172 | end |
| 171 | end | 173 | end |
| 172 | if home_overrides then | 174 | if home_overrides then |
| @@ -178,10 +180,11 @@ if not site_config.LUAROCKS_FORCE_CONFIG then | |||
| 178 | cfg.rocks_servers = nil | 180 | cfg.rocks_servers = nil |
| 179 | end | 181 | end |
| 180 | util.deep_merge(cfg, home_overrides) | 182 | util.deep_merge(cfg, home_overrides) |
| 181 | else -- nil or false | 183 | else |
| 182 | home_config_ok = home_overrides | 184 | home_config_ok = home_overrides |
| 183 | if err and home_config_ok == nil then | 185 | if errcode ~= "open" then |
| 184 | io.stderr:write(err.."\n") | 186 | io.stderr:write(err.."\n") |
| 187 | os.exit(cfg.errorcodes.CONFIGFILE) | ||
| 185 | end | 188 | end |
| 186 | end | 189 | end |
| 187 | end | 190 | end |
diff --git a/src/luarocks/persist.lua b/src/luarocks/persist.lua index 9d601a43..87db96b2 100644 --- a/src/luarocks/persist.lua +++ b/src/luarocks/persist.lua | |||
| @@ -9,18 +9,48 @@ package.loaded["luarocks.persist"] = persist | |||
| 9 | 9 | ||
| 10 | local util = require("luarocks.util") | 10 | local util = require("luarocks.util") |
| 11 | 11 | ||
| 12 | local function run_file(filename, env) | ||
| 13 | local fd, err, errno = io.open(filename) | ||
| 14 | if not fd then | ||
| 15 | return nil, err, "open" | ||
| 16 | end | ||
| 17 | local str = fd:read("*a"):gsub("^#![^\n]*\n", "") | ||
| 18 | fd:close() | ||
| 19 | local chunk, ran | ||
| 20 | if _VERSION == "Lua 5.1" then -- Lua 5.1 | ||
| 21 | chunk, err = loadstring(str, filename) | ||
| 22 | if chunk then | ||
| 23 | setfenv(chunk, env) | ||
| 24 | ran, err = pcall(chunk) | ||
| 25 | end | ||
| 26 | else -- Lua 5.2 | ||
| 27 | chunk, err = loadfile(filename, "t", env) | ||
| 28 | if chunk then | ||
| 29 | ran, err = pcall(chunk) | ||
| 30 | end | ||
| 31 | end | ||
| 32 | if not chunk then | ||
| 33 | return nil, "Error loading file: "..err, "load" | ||
| 34 | end | ||
| 35 | if not ran then | ||
| 36 | return nil, "Error running file: "..err, "run" | ||
| 37 | end | ||
| 38 | return true, err | ||
| 39 | end | ||
| 40 | |||
| 12 | --- Load a Lua file containing assignments, storing them in a table. | 41 | --- Load a Lua file containing assignments, storing them in a table. |
| 13 | -- The global environment is not propagated to the loaded file. | 42 | -- The global environment is not propagated to the loaded file. |
| 14 | -- @param filename string: the name of the file. | 43 | -- @param filename string: the name of the file. |
| 15 | -- @param tbl table or nil: if given, this table is used to store | 44 | -- @param tbl table or nil: if given, this table is used to store |
| 16 | -- loaded values. | 45 | -- loaded values. |
| 17 | -- @return table or (nil, string): a table with the file's assignments | 46 | -- @return table or (nil, string, string): a table with the file's assignments |
| 18 | -- as fields, or nil and a message in case of errors. | 47 | -- as fields, or nil, an error message and an error code ("load" or "run") |
| 48 | -- in case of errors. | ||
| 19 | function persist.load_into_table(filename, tbl) | 49 | function persist.load_into_table(filename, tbl) |
| 20 | assert(type(filename) == "string") | 50 | assert(type(filename) == "string") |
| 21 | assert(type(tbl) == "table" or not tbl) | 51 | assert(type(tbl) == "table" or not tbl) |
| 22 | 52 | ||
| 23 | local result, chunk, ran, err | 53 | local result, ok, err |
| 24 | result = tbl or {} | 54 | result = tbl or {} |
| 25 | local globals = {} | 55 | local globals = {} |
| 26 | local globals_mt = { | 56 | local globals_mt = { |
| @@ -31,28 +61,13 @@ function persist.load_into_table(filename, tbl) | |||
| 31 | } | 61 | } |
| 32 | local save_mt = getmetatable(result) | 62 | local save_mt = getmetatable(result) |
| 33 | setmetatable(result, globals_mt) | 63 | setmetatable(result, globals_mt) |
| 34 | if _VERSION == "Lua 5.1" then -- Lua 5.1 | ||
| 35 | chunk, err = loadfile(filename) | ||
| 36 | if chunk then | ||
| 37 | setfenv(chunk, result) | ||
| 38 | ran, err = pcall(chunk) | ||
| 39 | end | ||
| 40 | else -- Lua 5.2 | ||
| 41 | chunk, err = loadfile(filename, "t", result) | ||
| 42 | if chunk then | ||
| 43 | ran, err = pcall(chunk) | ||
| 44 | end | ||
| 45 | end | ||
| 46 | setmetatable(result, save_mt) | ||
| 47 | 64 | ||
| 48 | if not chunk then | 65 | ok, err, errcode = run_file(filename, result) |
| 49 | if err:sub(1,5) ~= filename:sub(1,5) then | 66 | |
| 50 | return false, err | 67 | setmetatable(result, save_mt) |
| 51 | end | 68 | |
| 52 | return nil, "Error loading file: "..err | 69 | if not ok then |
| 53 | end | 70 | return nil, err, errcode |
| 54 | if not ran then | ||
| 55 | return nil, "Error running file: "..err | ||
| 56 | end | 71 | end |
| 57 | return result, globals | 72 | return result, globals |
| 58 | end | 73 | end |
