From 7882ea469da3e8dcf5bcfaac5d847aafbc957f74 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Thu, 15 Aug 2013 14:50:10 -0300 Subject: Make sure fs.change_dir is always checked and results in proper errors. --- src/luarocks/add.lua | 6 ++++-- src/luarocks/admin_remove.lua | 6 ++++-- src/luarocks/build.lua | 8 +++++--- src/luarocks/cache.lua | 5 +++-- src/luarocks/fetch.lua | 17 +++++++++++------ src/luarocks/fetch/cvs.lua | 3 ++- src/luarocks/fetch/git.lua | 6 ++++-- src/luarocks/fetch/hg.lua | 6 ++++-- src/luarocks/fetch/svn.lua | 6 ++++-- src/luarocks/fs/lua.lua | 2 +- src/luarocks/fs/unix/tools.lua | 6 +++++- src/luarocks/fs/win32/tools.lua | 7 ++++++- src/luarocks/new_version.lua | 3 ++- src/luarocks/pack.lua | 6 ++++-- src/luarocks/unpack.lua | 12 ++++++++---- 15 files changed, 67 insertions(+), 32 deletions(-) (limited to 'src') diff --git a/src/luarocks/add.lua b/src/luarocks/add.lua index d7c293e5..54991291 100644 --- a/src/luarocks/add.lua +++ b/src/luarocks/add.lua @@ -45,7 +45,8 @@ local function add_files_to_server(refresh, rockfiles, server, upload_server) login_url = protocol.."://"..server_path end - fs.change_dir(at) + local ok, err = fs.change_dir(at) + if not ok then return nil, err end local files = {} for i, rockfile in ipairs(rockfiles) do @@ -62,7 +63,8 @@ local function add_files_to_server(refresh, rockfiles, server, upload_server) return nil, "No files found" end - fs.change_dir(local_cache) + local ok, err = fs.change_dir(local_cache) + if not ok then return nil, err end util.printout("Updating manifest...") manif.make_manifest(local_cache, "one", true) diff --git a/src/luarocks/admin_remove.lua b/src/luarocks/admin_remove.lua index 83b57fc9..31325a30 100644 --- a/src/luarocks/admin_remove.lua +++ b/src/luarocks/admin_remove.lua @@ -41,7 +41,8 @@ local function remove_files_from_server(refresh, rockfiles, server, upload_serve return nil, "This command requires 'rsync', check your configuration." end - fs.change_dir(at) + local ok, err = fs.change_dir(at) + if not ok then return nil, err end local nr_files = 0 for i, rockfile in ipairs(rockfiles) do @@ -58,7 +59,8 @@ local function remove_files_from_server(refresh, rockfiles, server, upload_serve return nil, "No files removed." end - fs.change_dir(local_cache) + local ok, err = fs.change_dir(local_cache) + if not ok then return nil, err end util.printout("Updating manifest...") manif.make_manifest(local_cache, "one", true) diff --git a/src/luarocks/build.lua b/src/luarocks/build.lua index 40f02e38..7bd54a0c 100644 --- a/src/luarocks/build.lua +++ b/src/luarocks/build.lua @@ -148,7 +148,7 @@ function build_rockspec(rockspec_file, need_to_fetch, minimal_mode, deps_mode) end end - ok, err, errcode = deps.check_external_deps(rockspec, "build") + local ok, err, errcode = deps.check_external_deps(rockspec, "build") if err then return nil, err, errcode end @@ -165,7 +165,8 @@ function build_rockspec(rockspec_file, need_to_fetch, minimal_mode, deps_mode) if not ok then return nil, source_dir, errcode end - fs.change_dir(source_dir) + local ok, err = fs.change_dir(source_dir) + if not ok then return nil, err end elseif rockspec.source.file then local ok, err = fs.unpack_archive(rockspec.source.file) if not ok then @@ -304,7 +305,8 @@ function build_rock(rock_file, need_to_fetch, deps_mode) return nil, err, errcode end local rockspec_file = path.rockspec_name_from_rock(rock_file) - fs.change_dir(unpack_dir) + local ok, err = fs.change_dir(unpack_dir) + if not ok then return nil, err end local ok, err, errcode = build_rockspec(rockspec_file, need_to_fetch, false, deps_mode) fs.pop_dir() return ok, err, errcode diff --git a/src/luarocks/cache.lua b/src/luarocks/cache.lua index fe74ba18..6ea6e82a 100644 --- a/src/luarocks/cache.lua +++ b/src/luarocks/cache.lua @@ -62,11 +62,12 @@ function refresh_local_cache(server, url, user, password) local_cache, err = fs.make_temp_dir("local_cache") tmp_cache = true end - local ok = fs.make_dir(local_cache) + local ok, err = fs.make_dir(local_cache) if not ok then - return nil, "Failed creating local cache dir." + return nil, "Failed creating local cache dir: "..err end fs.change_dir(local_cache) + if not ok then return nil, err end util.printout("Refreshing cache "..local_cache.."...") -- TODO abstract away explicit 'wget' call diff --git a/src/luarocks/fetch.lua b/src/luarocks/fetch.lua index be2f32c6..a742b7fc 100644 --- a/src/luarocks/fetch.lua +++ b/src/luarocks/fetch.lua @@ -71,7 +71,8 @@ function fetch_url_at_temp_dir(url, tmpname, filename) return nil, "Failed creating temporary directory "..tmpname..": "..err end util.schedule_function(fs.delete, temp_dir) - fs.change_dir(temp_dir) + local ok, err = fs.change_dir(temp_dir) + if not ok then return nil, err end local file, err, errcode = fetch_url(url, filename) fs.pop_dir() if not file then @@ -114,8 +115,9 @@ function fetch_and_unpack_rock(rock_file, dest) if not dest then util.schedule_function(fs.delete, unpack_dir) end - fs.change_dir(unpack_dir) - local ok = fs.unzip(rock_file) + local ok, err = fs.change_dir(unpack_dir) + if not ok then return nil, err end + ok = fs.unzip(rock_file) if not ok then return nil, "Failed unpacking rock file: " .. rock_file end @@ -234,7 +236,8 @@ function load_rockspec(filename, location) local err, errcode if location then - fs.change_dir(location) + local ok, err = fs.change_dir(location) + if not ok then return nil, err end filename, err = fetch_url(filename) fs.pop_dir() else @@ -265,7 +268,8 @@ function get_sources(rockspec, extract, dest_dir) local filename = rockspec.source.file local source_file, store_dir, err, errcode if dest_dir then - fs.change_dir(dest_dir) + local ok, err = fs.change_dir(dest_dir) + if not ok then return nil, err end source_file, err, errcode = fetch_url(url, filename) fs.pop_dir() store_dir = dest_dir @@ -281,7 +285,8 @@ function get_sources(rockspec, extract, dest_dir) end end if extract then - fs.change_dir(store_dir) + local ok, err = fs.change_dir(store_dir) + if not ok then return nil, err end fs.unpack_archive(rockspec.source.file) if not fs.exists(rockspec.source.dir) then return nil, "Directory "..rockspec.source.dir.." not found inside archive "..rockspec.source.file diff --git a/src/luarocks/fetch/cvs.lua b/src/luarocks/fetch/cvs.lua index a622cdcb..6b4cf186 100644 --- a/src/luarocks/fetch/cvs.lua +++ b/src/luarocks/fetch/cvs.lua @@ -34,7 +34,8 @@ function get_sources(rockspec, extract, dest_dir) else store_dir = dest_dir end - fs.change_dir(store_dir) + local ok, err = fs.change_dir(store_dir) + if not ok then return nil, err end if not fs.execute(unpack(command)) then return nil, "Failed fetching files from CVS." end diff --git a/src/luarocks/fetch/git.lua b/src/luarocks/fetch/git.lua index e7c89435..44151927 100644 --- a/src/luarocks/fetch/git.lua +++ b/src/luarocks/fetch/git.lua @@ -48,7 +48,8 @@ function get_sources(rockspec, extract, dest_dir) store_dir = dest_dir end store_dir = fs.absolute_name(store_dir) - fs.change_dir(store_dir) + local ok, err = fs.change_dir(store_dir) + if not ok then return nil, err end local command = {git_cmd, "clone", "--depth=1", rockspec.source.url, module} local tag_or_branch = rockspec.source.tag or rockspec.source.branch @@ -65,7 +66,8 @@ function get_sources(rockspec, extract, dest_dir) if not fs.execute(unpack(command)) then return nil, "Failed cloning git repository." end - fs.change_dir(module) + local ok, err = fs.change_dir(module) + if not ok then return nil, err end if tag_or_branch and not git_can_clone_by_tag() then local checkout_command = {git_cmd, "checkout", tag_or_branch} if not fs.execute(unpack(checkout_command)) then diff --git a/src/luarocks/fetch/hg.lua b/src/luarocks/fetch/hg.lua index a08520a5..32e40469 100644 --- a/src/luarocks/fetch/hg.lua +++ b/src/luarocks/fetch/hg.lua @@ -39,11 +39,13 @@ function get_sources(rockspec, extract, dest_dir) else store_dir = dest_dir end - fs.change_dir(store_dir) + local ok, err = fs.change_dir(store_dir) + if not ok then return nil, err end if not fs.execute(unpack(command)) then return nil, "Failed cloning hg repository." end - fs.change_dir(module) + local ok, err = fs.change_dir(module) + if not ok then return nil, err end fs.delete(dir.path(store_dir, module, ".hg")) fs.delete(dir.path(store_dir, module, ".hgignore")) diff --git a/src/luarocks/fetch/svn.lua b/src/luarocks/fetch/svn.lua index a4e952d2..7aa37ec5 100644 --- a/src/luarocks/fetch/svn.lua +++ b/src/luarocks/fetch/svn.lua @@ -36,11 +36,13 @@ function get_sources(rockspec, extract, dest_dir) else store_dir = dest_dir end - fs.change_dir(store_dir) + local ok, err = fs.change_dir(store_dir) + if not ok then return nil, err end if not fs.execute(unpack(command)) then return nil, "Failed fetching files from Subversion." end - fs.change_dir(module) + ok, err = fs.change_dir(module) + if not ok then return nil, err end for _, d in ipairs(fs.find(".")) do if dir.base_name(d) == ".svn" then fs.delete(dir.path(store_dir, module, d)) diff --git a/src/luarocks/fs/lua.lua b/src/luarocks/fs/lua.lua index 7e208357..f50ecf75 100644 --- a/src/luarocks/fs/lua.lua +++ b/src/luarocks/fs/lua.lua @@ -150,7 +150,7 @@ end function change_dir(d) table.insert(dir_stack, lfs.currentdir()) d = dir.normalize(d) - lfs.chdir(d) + return lfs.chdir(d) end --- Change directory to root. diff --git a/src/luarocks/fs/unix/tools.lua b/src/luarocks/fs/unix/tools.lua index 23bd8161..64992b91 100644 --- a/src/luarocks/fs/unix/tools.lua +++ b/src/luarocks/fs/unix/tools.lua @@ -48,7 +48,11 @@ end -- @param directory string: The directory to switch to. function change_dir(directory) assert(type(directory) == "string") - table.insert(dir_stack, directory) + if fs.is_dir(directory) then + table.insert(dir_stack, directory) + return true + end + return nil, "directory not found: "..directory end --- Change directory to root. diff --git a/src/luarocks/fs/win32/tools.lua b/src/luarocks/fs/win32/tools.lua index 97ba4fe5..6960ab35 100644 --- a/src/luarocks/fs/win32/tools.lua +++ b/src/luarocks/fs/win32/tools.lua @@ -77,9 +77,14 @@ end -- semantics of chdir, as it does not handle errors the same way, -- but works well for our purposes for now. -- @param directory string: The directory to switch to. +-- @return boolean or (nil, string): true if successful, (nil, error message) if failed. function change_dir(directory) assert(type(directory) == "string") - table.insert(dir_stack, directory) + if fs.is_dir(directory) then + table.insert(dir_stack, directory) + return true + end + return nil, "directory not found: "..directory end --- Change directory to root. diff --git a/src/luarocks/new_version.lua b/src/luarocks/new_version.lua index cb170321..f93d0c29 100644 --- a/src/luarocks/new_version.lua +++ b/src/luarocks/new_version.lua @@ -64,7 +64,8 @@ local function check_url_and_update_md5(out_rs, out_name) end util.printout("File successfully downloaded. Updating MD5 checksum...") out_rs.source.md5 = fs.get_md5(file) - fs.change_dir(temp_dir) + local ok, err = fs.change_dir(temp_dir) + if not ok then return nil, err end fs.unpack_archive(file) local base_dir = out_rs.source.dir or fetch.url_to_base_dir(out_rs.source.url) if not fs.exists(base_dir) then diff --git a/src/luarocks/pack.lua b/src/luarocks/pack.lua index ede992ce..0ef7344b 100644 --- a/src/luarocks/pack.lua +++ b/src/luarocks/pack.lua @@ -45,7 +45,8 @@ local function pack_source_rock(rockspec_file) if not source_file then return nil, source_dir end - fs.change_dir(source_dir) + local ok, err = fs.change_dir(source_dir) + if not ok then return nil, err end fs.delete(rock_file) fs.copy(rockspec_file, source_dir) @@ -139,7 +140,8 @@ local function do_pack_binary_rock(name, version) if not ok then return nil, "Failed copying back files: " .. err end end - fs.change_dir(temp_dir) + local ok, err = fs.change_dir(temp_dir) + if not ok then return nil, err end if not is_binary and not repos.has_binaries(name, version) then rock_file = rock_file:gsub("%."..cfg.arch:gsub("%-","%%-").."%.", ".all.") end diff --git a/src/luarocks/unpack.lua b/src/luarocks/unpack.lua index 67db5ca5..2e6166ca 100644 --- a/src/luarocks/unpack.lua +++ b/src/luarocks/unpack.lua @@ -31,12 +31,14 @@ local function unpack_rockspec(rockspec_file, dir_name) if not rockspec then return nil, "Failed loading rockspec "..rockspec_file..": "..err end - fs.change_dir(dir_name) + local ok, err = fs.change_dir(dir_name) + if not ok then return nil, err end local ok, sources_dir = fetch.fetch_sources(rockspec, true, ".") if not ok then return nil, sources_dir end - fs.change_dir(dir_name) + ok, err = fs.change_dir(dir_name) + if not ok then return nil, err end build.apply_patches(rockspec) fs.pop_dir() return rockspec @@ -57,7 +59,8 @@ local function unpack_rock(rock_file, dir_name, kind) if not ok then return nil, "Failed unzipping rock "..rock_file, errcode end - fs.change_dir(dir_name) + ok, err = fs.change_dir(dir_name) + if not ok then return nil, err end local rockspec_file = dir_name..".rockspec" local rockspec, err = fetch.load_rockspec(rockspec_file) if not rockspec then @@ -69,7 +72,8 @@ local function unpack_rock(rock_file, dir_name, kind) if not ok then return nil, err end - fs.change_dir(rockspec.source.dir) + ok, err = fs.change_dir(rockspec.source.dir) + if not ok then return nil, err end build.apply_patches(rockspec) fs.pop_dir() end -- cgit v1.2.3-55-g6feb