aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGeorge Roman <george.roman.99@gmail.com>2018-07-07 18:49:41 +0300
committerHisham Muhammad <hisham@gobolinux.org>2018-07-16 20:34:30 -0300
commit4b3ee931cd8ecc5937811cd8e9316d79c0c9c00e (patch)
tree9b5f36a8f75df296b1bace3413eb4967d9325729 /src
parent1b3b6525a4313404af84fce0fbbc29695e664f73 (diff)
downloadluarocks-4b3ee931cd8ecc5937811cd8e9316d79c0c9c00e.tar.gz
luarocks-4b3ee931cd8ecc5937811cd8e9316d79c0c9c00e.tar.bz2
luarocks-4b3ee931cd8ecc5937811cd8e9316d79c0c9c00e.zip
Add general improvements to the fs module
Diffstat (limited to 'src')
-rw-r--r--src/luarocks/fs/lua.lua17
-rw-r--r--src/luarocks/fs/tools.lua7
-rw-r--r--src/luarocks/fs/win32/tools.lua11
3 files changed, 25 insertions, 10 deletions
diff --git a/src/luarocks/fs/lua.lua b/src/luarocks/fs/lua.lua
index 373dcbdd..9da3875b 100644
--- a/src/luarocks/fs/lua.lua
+++ b/src/luarocks/fs/lua.lua
@@ -464,9 +464,13 @@ end
464--- Internal implementation function for fs.dir. 464--- Internal implementation function for fs.dir.
465-- Yields a filename on each iteration. 465-- Yields a filename on each iteration.
466-- @param at string: directory to list 466-- @param at string: directory to list
467-- @return nil 467-- @return nil or (nil and string): an error message on failure
468function fs_lua.dir_iterator(at) 468function fs_lua.dir_iterator(at)
469 for file in lfs.dir(at) do 469 local pok, iter, arg = pcall(lfs.dir, at)
470 if not pok then
471 return nil, iter
472 end
473 for file in iter, arg do
470 if file ~= "." and file ~= ".." then 474 if file ~= "." and file ~= ".." then
471 coroutine.yield(file) 475 coroutine.yield(file)
472 end 476 end
@@ -479,7 +483,11 @@ end
479-- @param prefix string: Auxiliary prefix string to form pathname. 483-- @param prefix string: Auxiliary prefix string to form pathname.
480-- @param result table: Array of strings where results are collected. 484-- @param result table: Array of strings where results are collected.
481local function recursive_find(cwd, prefix, result) 485local function recursive_find(cwd, prefix, result)
482 for file in lfs.dir(cwd) do 486 local pok, iter, arg = pcall(lfs.dir, cwd)
487 if not pok then
488 return nil
489 end
490 for file in iter, arg do
483 if file ~= "." and file ~= ".." then 491 if file ~= "." and file ~= ".." then
484 local item = prefix .. file 492 local item = prefix .. file
485 table.insert(result, item) 493 table.insert(result, item)
@@ -502,9 +510,6 @@ function fs_lua.find(at)
502 at = fs.current_dir() 510 at = fs.current_dir()
503 end 511 end
504 at = dir.normalize(at) 512 at = dir.normalize(at)
505 if not fs.is_dir(at) then
506 return {}
507 end
508 local result = {} 513 local result = {}
509 recursive_find(at, "", result) 514 recursive_find(at, "", result)
510 return result 515 return result
diff --git a/src/luarocks/fs/tools.lua b/src/luarocks/fs/tools.lua
index 0cfdc9f7..d2dc08ae 100644
--- a/src/luarocks/fs/tools.lua
+++ b/src/luarocks/fs/tools.lua
@@ -82,7 +82,12 @@ end
82-- Allows leaving a directory (e.g. for deleting it) in 82-- Allows leaving a directory (e.g. for deleting it) in
83-- a crossplatform way. 83-- a crossplatform way.
84function tools.change_dir_to_root() 84function tools.change_dir_to_root()
85 local curr_dir = fs.current_dir()
86 if not curr_dir or not fs.is_dir(curr_dir) then
87 return false
88 end
85 table.insert(dir_stack, "/") 89 table.insert(dir_stack, "/")
90 return true
86end 91end
87 92
88--- Change working directory to the previous in the directory stack. 93--- Change working directory to the previous in the directory stack.
@@ -113,7 +118,7 @@ end
113-- @param at string: directory to list 118-- @param at string: directory to list
114-- @return nil 119-- @return nil
115function tools.dir_iterator(at) 120function tools.dir_iterator(at)
116 local pipe = io.popen(fs.command_at(at, fs.Q(vars.LS))) 121 local pipe = io.popen(fs.command_at(at, fs.Q(vars.LS), true))
117 for file in pipe:lines() do 122 for file in pipe:lines() do
118 if file ~= "." and file ~= ".." then 123 if file ~= "." and file ~= ".." then
119 coroutine.yield(file) 124 coroutine.yield(file)
diff --git a/src/luarocks/fs/win32/tools.lua b/src/luarocks/fs/win32/tools.lua
index c267b316..de3d9031 100644
--- a/src/luarocks/fs/win32/tools.lua
+++ b/src/luarocks/fs/win32/tools.lua
@@ -13,10 +13,15 @@ local vars = setmetatable({}, { __index = function(_,k) return cfg.variables[k]
13--- Adds prefix to command to make it run from a directory. 13--- Adds prefix to command to make it run from a directory.
14-- @param directory string: Path to a directory. 14-- @param directory string: Path to a directory.
15-- @param cmd string: A command-line string. 15-- @param cmd string: A command-line string.
16-- @param exit_on_error bool: Exits immediately if entering the directory failed.
16-- @return string: The command-line with prefix. 17-- @return string: The command-line with prefix.
17function tools.command_at(directory, cmd) 18function tools.command_at(directory, cmd, exit_on_error)
18 local drive = directory:match("^([A-Za-z]:)") 19 local drive = directory:match("^([A-Za-z]:)")
19 cmd = "cd " .. fs.Q(directory) .. " & " .. cmd 20 local op = " & "
21 if exit_on_error then
22 op = " && "
23 end
24 local cmd = "cd " .. fs.Q(directory) .. op .. cmd
20 if drive then 25 if drive then
21 cmd = drive .. " & " .. cmd 26 cmd = drive .. " & " .. cmd
22 end 27 end
@@ -113,7 +118,7 @@ function tools.find(at)
113 return {} 118 return {}
114 end 119 end
115 local result = {} 120 local result = {}
116 local pipe = io.popen(fs.command_at(at, fs.quiet_stderr(fs.Q(vars.FIND)))) 121 local pipe = io.popen(fs.command_at(at, fs.quiet_stderr(fs.Q(vars.FIND)), true))
117 for file in pipe:lines() do 122 for file in pipe:lines() do
118 -- Windows find is a bit different 123 -- Windows find is a bit different
119 local first_two = file:sub(1,2) 124 local first_two = file:sub(1,2)