Skip to content

Commit fba639e

Browse files
authored
Merge pull request darktable-org#496 from wpferguson/desanitize_libs
Desanitize libs
2 parents dc81c0b + 3e37c06 commit fba639e

17 files changed

+62
-97
lines changed

contrib/autostyle.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ script_data.show = nil -- only required for libs since the destroy_method only h
6969
-- run command and retrieve stdout
7070
local function get_stdout(cmd)
7171
-- Open the command, for reading
72-
local fd = assert(syslib.io_popen(cmd, 'r'))
72+
local fd = assert(io.popen(cmd, 'r'))
7373
darktable.control.read(fd)
7474
-- slurp the whole file
7575
local data = assert(fd:read('*a'))
@@ -188,4 +188,4 @@ darktable.register_event("autostyle", "post-import-image",
188188

189189

190190
script_data.destroy = destroy
191-
return script_data
191+
return script_data

contrib/color_profile_manager.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ end
107107

108108
local function list_profiles(dir)
109109
local files = {}
110-
local p = dtsys.io_popen(DIR_CMD .. " " .. dir)
110+
local p = io.popen(DIR_CMD .. " " .. dir)
111111
if p then
112112
for line in p:lines() do
113113
table.insert(files, line)

contrib/fujifilm_dynamic_range.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ local function detect_dynamic_range(event, image)
106106
-- without -n flag, exiftool will round to the nearest tenth
107107
command = command .. " -RawExposureBias -n -t " .. RAF_filename
108108
dt.print_log(command)
109-
output = dtsys.io_popen(command)
109+
output = io.popen(command)
110110
local raf_result = output:read("*all")
111111
output:close()
112112
if #raf_result == 0 then

contrib/fujifilm_ratings.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ local function detect_rating(event, image)
6565
local JPEG_filename = string.gsub(RAF_filename, "%.RAF$", ".JPG")
6666
local command = "exiftool -Rating " .. JPEG_filename
6767
dt.print_log(command)
68-
local output = dtsys.io_popen(command)
68+
local output = io.popen(command)
6969
local jpeg_result = output:read("*all")
7070
output:close()
7171
if string.len(jpeg_result) > 0 then
@@ -76,7 +76,7 @@ local function detect_rating(event, image)
7676
end
7777
command = "exiftool -Rating " .. RAF_filename
7878
dt.print_log(command)
79-
output = dtsys.io_popen(command)
79+
output = io.popen(command)
8080
local raf_result = output:read("*all")
8181
output:close()
8282
if string.len(raf_result) > 0 then

contrib/geoJSON_export.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ dt.preferences.register("geoJSON_export",
331331
_("opens the geoJSON file after the export with the standard program for geoJSON files"),
332332
false )
333333

334-
local handle = dtsys.io_popen("xdg-user-dir DESKTOP")
334+
local handle = io.popen("xdg-user-dir DESKTOP")
335335
local result = handle:read()
336336
handle:close()
337337
if (result == nil) then

contrib/geoToolbox.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ local function reverse_geocode()
412412
-- jq could be replaced with a Lua JSON parser
413413
startCommand = string.format("curl --silent \"https://api.mapbox.com/geocoding/v5/mapbox.places/%s,%s.json?types=%s&access_token=%s\" | jq '.features | .[0] | '.text''", lon1, lat1, types, tokan)
414414

415-
local handle = dtsys.io_popen(startCommand)
415+
local handle = io.popen(startCommand)
416416
local result = trim12(handle:read("*a"))
417417
handle:close()
418418

contrib/image_stack.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ local function list_files(search_string)
348348
search_string = string.gsub(search_string, "/", "\\\\")
349349
end
350350

351-
local f = dtsys.io_popen(ls .. search_string)
351+
local f = io.popen(ls .. search_string)
352352
if f then
353353
local found_file = f:read()
354354
while found_file do

contrib/image_time.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ local function get_image_taken_time(image)
226226

227227
local exiv2 = df.check_if_bin_exists("exiv2")
228228
if exiv2 then
229-
p = dtsys.io_popen(exiv2 .. " -K Exif.Image.DateTime " .. image.path .. PS .. image.filename)
229+
p = io.popen(exiv2 .. " -K Exif.Image.DateTime " .. image.path .. PS .. image.filename)
230230
if p then
231231
for line in p:lines() do
232232
if string.match(line, "Exif.Image.DateTime") then
@@ -244,7 +244,7 @@ end
244244

245245
local function _get_windows_image_file_creation_time(image)
246246
local datetime = nil
247-
local p = dtsys.io_popen("dir " .. image.path .. PS .. image.filename)
247+
local p = io.popen("dir " .. image.path .. PS .. image.filename)
248248
if p then
249249
for line in p:lines() do
250250
if string.match(line, ds.sanitize_lua(image.filename)) then
@@ -265,7 +265,7 @@ end
265265

266266
local function _get_nix_image_file_creation_time(image)
267267
local datetime = nil
268-
local p = dtsys.io_popen("ls -lL --time-style=full-iso " .. image.path .. PS .. image.filename)
268+
local p = io.popen("ls -lL --time-style=full-iso " .. image.path .. PS .. image.filename)
269269
if p then
270270
for line in p:lines() do
271271
if string.match(line, ds.sanitize_lua(image.filename)) then

contrib/kml_export.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ if dt.configuration.running_os == "windows" then
343343
elseif dt.configuration.running_os == "macos" then
344344
defaultDir = os.getenv("HOME")
345345
else
346-
local handle = dsys.io_popen("xdg-user-dir DESKTOP")
346+
local handle = io.popen("xdg-user-dir DESKTOP")
347347
defaultDir = handle:read()
348348
handle:close()
349349
end

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/string.lua

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -298,16 +298,32 @@ local function _sanitize_windows(str)
298298
end
299299
end
300300

301+
local function _should_be_sanitized(str)
302+
local old_log_level = log.log_level()
303+
local result = false
304+
log.log_level(dtutils_string.log_level)
305+
if string.match(str, "[^%g]") then
306+
result = true
307+
end
308+
log.log_level(old_log_level)
309+
return result
310+
end
311+
301312
function dtutils_string.sanitize(str)
302313
local old_log_level = log.log_level()
314+
local sanitized_str = nil
303315
log.log_level(dtutils_string.log_level)
304-
if dt.configuration.running_os == "windows" then
305-
log.log_level(old_log_level)
306-
return _sanitize_windows(str)
316+
if _should_be_sanitized(str) then
317+
if dt.configuration.running_os == "windows" then
318+
sanitized_str = _sanitize_windows(str)
319+
else
320+
sanitized_str = _sanitize_posix(str)
321+
end
307322
else
308-
log.log_level(old_log_level)
309-
return _sanitize_posix(str)
323+
sanitized_str = str
310324
end
325+
log.log_level(old_log_level)
326+
return sanitized_str
311327
end
312328

313329
dtutils_string.libdoc.functions["sanitize_lua"] = {

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

official/enfuse.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ if enfuse_installed then
116116

117117
local version = nil
118118

119-
local p = dtsys.io_popen(enfuse_installed .. " --version")
119+
local p = io.popen(enfuse_installed .. " --version")
120120
local f = p:read("all")
121121
p:close()
122122
version = string.match(f, "enfuse (%d.%d)")

tools/executable_manager.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ local function grep(file, pattern)
7676
if dt.configuration.running_os == "windows" then
7777
-- use find to get the matches
7878
local command = "\\windows\\system32\\find.exe " .. "\"" .. pattern .. "\"" .. " " .. file
79-
local f = dtsys.io_popen(command)
79+
local f = io.popen(command)
8080
local output = f:read("all")
8181
f:close()
8282
-- strip out the first line
@@ -85,7 +85,7 @@ local function grep(file, pattern)
8585
else
8686
-- use grep and just return the answers
8787
local command = "grep " .. pattern .. " " .. file
88-
local f = dtsys.io_popen(command)
88+
local f = io.popen(command)
8989
local output = f:read("all")
9090
f:close()
9191
result = du.split(output, "\n")

tools/get_lib_manpages.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ local function output_man(d)
4646
mf:close()
4747
if df.check_if_bin_exists("groff") then
4848
if df.check_if_bin_exists("ps2pdf") then
49-
dtsys.os_execute("groff -man " .. fname .. " | ps2pdf - " .. fname .. ".pdf")
49+
os.execute("groff -man " .. fname .. " | ps2pdf - " .. fname .. ".pdf")
5050
else
5151
log.msg(log.error, "Missing ps2pdf. Can't generate pdf man pages.")
5252
end
@@ -60,7 +60,7 @@ end
6060

6161
-- find the libraries
6262

63-
local output = dtsys.io_popen("cd "..dt.configuration.config_dir.."/lua/lib ;find . -name \\*.lua -print | sort")
63+
local output = io.popen("cd "..dt.configuration.config_dir.."/lua/lib ;find . -name \\*.lua -print | sort")
6464

6565
-- loop through the libraries
6666

tools/get_libdoc.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ end
3737

3838
-- find the libraries
3939

40-
local output = dtsys.io_popen("cd "..dt.configuration.config_dir.."/lua/lib ;find . -name \\*.lua -print | sort")
40+
local output = io.popen("cd "..dt.configuration.config_dir.."/lua/lib ;find . -name \\*.lua -print | sort")
4141

4242
-- loop through the libraries
4343

0 commit comments

Comments
 (0)