Skip to content

Commit 9f423ee

Browse files
committed
lib/dtutils/file - reverted io_popen and os_execute wrappers as
lib/dtutils/system they don't work in this situation (no control over the input strings)
1 parent 7c93868 commit 9f423ee

File tree

2 files changed

+15
-66
lines changed

2 files changed

+15
-66
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 = dsys.io_popen(cmd)
45+
local p = 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 = dsys.os_execute
97+
local engine = 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 = dsys.io_popen(cmd)
170+
local p = 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 = dsys.io_popen("command -v " .. bin)
194+
local p = 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 = dsys.io_popen("find " .. search_start .. " -type f -name " .. bin .. " -print")
223+
local p = 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 = dsys.io_popen("if exist " .. dtutils_file.sanitize_filename(filepath) .. " (echo 'yes') else (echo 'no')")
448+
local p = 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 = dsys.os_execute('test -e ' .. dtutils_file.sanitize_filename(filepath))
459+
result = 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 = dsys.os_execute('copy "' .. fromFile .. '" "' .. toFile .. '"')
525+
result = os.execute('copy "' .. fromFile .. '" "' .. toFile .. '"')
526526
elseif dtutils_file.check_if_bin_exists("cp") then
527-
result = dsys.os_execute("cp '" .. fromFile .. "' '" .. toFile .. "'")
527+
result = 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 = dsys.os_execute("mv '" .. fromFile .. "' '" .. toFile .. "'")
578+
success = os.execute("mv '" .. fromFile .. "' '" .. toFile .. "'")
579579
end
580580
-- if the mv didn't exist or succeed, then...
581581
if not success then

lib/dtutils/system.lua

Lines changed: 5 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function dtutils_system.external_command(command)
5151
local result = nil
5252

5353
if dt.configuration.running_os == "windows" then
54-
result = dtutils_system.windows_command(ds.sanitize(command))
54+
result = dtutils_system.windows_command(command)
5555
else
5656
result = dt.control.execute(command)
5757
end
@@ -78,20 +78,20 @@ dtutils_system.libdoc.functions["windows_command"] = {
7878
Copyright = [[]],
7979
}
8080

81-
local function quote_windows_command(command)
81+
local function _quote_windows_command(command)
8282
return "\"" .. command .. "\""
8383
end
8484

8585
function dtutils_system.windows_command(command)
8686
local result = 1
8787

88-
local fname = ds.sanitize(dt.configuration.tmp_dir .. "/run_command.bat")
88+
local fname = dt.configuration.tmp_dir .. "/run_command.bat"
8989

9090
local file = io.open(fname, "w")
9191
if file then
9292
dt.print_log("opened file")
9393
command = string.gsub(command, "%%", "%%%%") -- escape % from windows shell
94-
command = quote_windows_command(command)
94+
command = _quote_windows_command(command)
9595
file:write(command)
9696
file:close()
9797

@@ -124,6 +124,7 @@ dtutils_system.libdoc.functions["launch_default_app"] = {
124124
License = [[]],
125125
Copyright = [[]],
126126
}
127+
127128
function dtutils_system.launch_default_app(path)
128129
local open_cmd = "xdg-open "
129130
if (dt.configuration.running_os == "windows") then
@@ -134,56 +135,4 @@ function dtutils_system.launch_default_app(path)
134135
return dtutils_system.external_command(open_cmd .. path)
135136
end
136137

137-
138-
dtutils_system.libdoc.functions["os_execute"] = {
139-
Name = [[os_execute]],
140-
Synopsis = [[wrapper around the lua os.execute function]],
141-
Usage = [[local dsys = require "lib/dtutils.file"
142-
143-
result = dsys.os_execute(cmd)
144-
cmd - string - a command to execute on the operating system]],
145-
Description = [[os_execute wraps the lua os.execute system call to provide
146-
correct sanitization of windows commands]],
147-
Return_Value = [[see the lua os.execute documentation]],
148-
Limitations = [[]],
149-
Example = [[]],
150-
See_Also = [[]],
151-
Reference = [[]],
152-
License = [[]],
153-
Copyright = [[]],
154-
}
155-
156-
function dtutils_system.os_execute(cmd)
157-
if dt.configuration.running_os == "windows" then
158-
cmd = quote_windows_command(cmd)
159-
end
160-
return os.execute(cmd)
161-
end
162-
163-
dtutils_system.libdoc.functions["io_popen"] = {
164-
Name = [[io_popen]],
165-
Synopsis = [[wrapper around the lua io.popen function]],
166-
Usage = [[local dsys = require "lib/dtutils.file"
167-
168-
result = dsys.io_popen(cmd)
169-
cmd - string - a command to execute and attach to]],
170-
Description = [[io_popen wraps the lua io.popen system call to provide
171-
correct sanitization of windows commands]],
172-
Return_Value = [[see the lua io.popen documentation]],
173-
Limitations = [[]],
174-
Example = [[]],
175-
See_Also = [[]],
176-
Reference = [[]],
177-
License = [[]],
178-
Copyright = [[]],
179-
}
180-
181-
function dtutils_system.io_popen(cmd)
182-
if dt.configuration.running_os == "windows" then
183-
cmd = quote_windows_command(cmd)
184-
end
185-
return io.popen(cmd)
186-
end
187-
188-
189138
return dtutils_system

0 commit comments

Comments
 (0)