Skip to content

Commit fe48d74

Browse files
committed
lib/dtutils/file - replaced os.execute and io.popen with wrapper functions
to sanitize windows calls
1 parent 2f330ea commit fe48d74

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

lib/dtutils/file.lua

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ end
4242

4343
local function _win_os_execute(cmd)
4444
local result = nil
45-
local p = io.popen(cmd)
45+
local p = dsys.io_popen(cmd)
4646
local output = p:read("*a")
4747
p:close()
4848
if string.match(output, "true") then
@@ -94,7 +94,7 @@ dtutils_file.libdoc.functions["test_file"] = {
9494

9595
function dtutils_file.test_file(path, test)
9696
local cmd = "test -"
97-
local engine = os.execute
97+
local engine = dsys.os_execute
9898
local cmdstring = ""
9999

100100
if dt.configuration.running_os == "windows" then
@@ -167,7 +167,7 @@ local function _search_for_bin_windows(bin)
167167

168168
for _,arg in ipairs(args) do
169169
local cmd = "where " .. arg .. " " .. ds.sanitize(bin)
170-
local p = io.popen(cmd)
170+
local p = dsys.io_popen(cmd)
171171
local output = p:read("*a")
172172
p:close()
173173
local lines = du.split(output, "\n")
@@ -191,7 +191,7 @@ end
191191

192192
local function _search_for_bin_nix(bin)
193193
local result = false
194-
local p = io.popen("command -v " .. bin)
194+
local p = dsys.io_popen("command -v " .. bin)
195195
local output = p:read("*a")
196196
p:close()
197197
if string.len(output) > 0 then
@@ -220,7 +220,7 @@ local function _search_for_bin_macos(bin)
220220
search_start = "/Applications/" .. bin .. ".app"
221221
end
222222

223-
local p = io.popen("find " .. search_start .. " -type f -name " .. bin .. " -print")
223+
local p = dsys.io_popen("find " .. search_start .. " -type f -name " .. bin .. " -print")
224224
local output = p:read("*a")
225225
p:close()
226226
local lines = du.split(output, "\n")
@@ -445,7 +445,7 @@ function dtutils_file.check_if_file_exists(filepath)
445445
local result = false
446446
if (dt.configuration.running_os == 'windows') then
447447
filepath = string.gsub(filepath, '[\\/]+', '\\')
448-
local p = io.popen("if exist " .. dtutils_file.sanitize_filename(filepath) .. " (echo 'yes') else (echo 'no')")
448+
local p = dsys.io_popen("if exist " .. dtutils_file.sanitize_filename(filepath) .. " (echo 'yes') else (echo 'no')")
449449
local ans = p:read("*all")
450450
p:close()
451451
if string.match(ans, "yes") then
@@ -456,7 +456,7 @@ function dtutils_file.check_if_file_exists(filepath)
456456
-- result = false
457457
-- end
458458
elseif (dt.configuration.running_os == "linux") then
459-
result = os.execute('test -e ' .. dtutils_file.sanitize_filename(filepath))
459+
result = dsys.os_execute('test -e ' .. dtutils_file.sanitize_filename(filepath))
460460
if not result then
461461
result = false
462462
end
@@ -522,9 +522,9 @@ function dtutils_file.file_copy(fromFile, toFile)
522522
local result = nil
523523
-- if cp exists, use it
524524
if dt.configuration.running_os == "windows" then
525-
result = os.execute('copy "' .. fromFile .. '" "' .. toFile .. '"')
525+
result = dsys.os_execute('copy "' .. fromFile .. '" "' .. toFile .. '"')
526526
elseif dtutils_file.check_if_bin_exists("cp") then
527-
result = os.execute("cp '" .. fromFile .. "' '" .. toFile .. "'")
527+
result = dsys.os_execute("cp '" .. fromFile .. "' '" .. toFile .. "'")
528528
end
529529

530530
-- if cp was not present, or if cp failed, then a pure lua solution
@@ -575,7 +575,7 @@ function dtutils_file.file_move(fromFile, toFile)
575575
if not success then
576576
-- an error occurred, so let's try using the operating system function
577577
if dtutils_file.check_if_bin_exists("mv") then
578-
success = os.execute("mv '" .. fromFile .. "' '" .. toFile .. "'")
578+
success = dsys.os_execute("mv '" .. fromFile .. "' '" .. toFile .. "'")
579579
end
580580
-- if the mv didn't exist or succeed, then...
581581
if not success then

0 commit comments

Comments
 (0)