Skip to content

Commit e427294

Browse files
committed
fix library problems found during unit testing
1 parent 18283cc commit e427294

File tree

2 files changed

+65
-18
lines changed

2 files changed

+65
-18
lines changed

lib/dtutils/file.lua

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ dtutils_file.libdoc.functions["check_if_bin_exists"] = {
4848
quoted and returned. If no preference is specified and the operating system is
4949
linux then the which command is used to check for a binary in the path. If found
5050
that path is returned. If no binary is found, false is returned.]],
51-
Return_Value = [[result - string - the path of the binary, false if not found]],
51+
Return_Value = [[result - string - the sanitized path of the binary, false if not found]],
5252
Limitations = [[]],
5353
Example = [[]],
5454
See_Also = [[]],
@@ -70,17 +70,17 @@ function dtutils_file.check_if_bin_exists(bin)
7070
if string.len(path) > 0 then
7171
if dtutils_file.check_if_file_exists(path) then
7272
if (string.match(path, ".exe$") or string.match(path, ".EXE$")) and dt.configuration.running_os ~= "windows" then
73-
result = "wine " .. "\"" .. path .. "\""
73+
result = dtutils_file.sanitize_filename("wine " .. path)
7474
else
75-
result = "\"" .. path .. "\""
75+
result = dtutils_file.sanitize_filename(path)
7676
end
7777
end
7878
elseif dt.configuration.running_os == "linux" then
7979
local p = io.popen("which " .. bin)
8080
local output = p:read("*a")
8181
p:close()
8282
if string.len(output) > 0 then
83-
result = output:sub(1,-2)
83+
result = dtutils_file.sanitize_filename(output:sub(1,-2))
8484
end
8585
end
8686
return result
@@ -107,9 +107,14 @@ dtutils_file.libdoc.functions["split_filepath"] = {
107107
function dtutils_file.split_filepath(str)
108108
-- strip out single quotes from quoted pathnames
109109
str = string.gsub(str, "'", "")
110+
str = string.gsub(str, '"', '')
110111
local result = {}
111112
-- Thank you Tobias Jakobs for the awesome regular expression, which I tweaked a little
112113
result["path"], result["filename"], result["basename"], result["filetype"] = string.match(str, "(.-)(([^\\/]-)%.?([^%.\\/]*))$")
114+
if result["basename"] == "" and result["filetype"]:len() > 1 then
115+
result["basename"] = result["filetype"]
116+
result["filetype"] = ""
117+
end
113118
return result
114119
end
115120

@@ -222,15 +227,21 @@ dtutils_file.libdoc.functions["check_if_file_exists"] = {
222227
}
223228

224229
function dtutils_file.check_if_file_exists(filepath)
225-
local result
230+
local result = false
226231
if (dt.configuration.running_os == 'windows') then
227232
filepath = string.gsub(filepath, '[\\/]+', '\\')
228-
result = os.execute('if exist "'..filepath..'" (cmd /c exit 0) else (cmd /c exit 1)')
229-
if not result then
230-
result = false
233+
local p = io.popen("if exist " .. filepath .. " (echo 'yes') else (echo 'no')")
234+
local ans = p:read("*all")
235+
p:close()
236+
if string.match(ans, "yes") then
237+
result = true
231238
end
239+
-- result = os.execute('if exist "'..filepath..'" (cmd /c exit 0) else (cmd /c exit 1)')
240+
-- if not result then
241+
-- result = false
242+
-- end
232243
elseif (dt.configuration.running_os == "linux") then
233-
result = os.execute('test -e ' .. "\"" .. filepath .. "\"")
244+
result = os.execute('test -e ' .. dtutils_file.sanitize_filename(filepath))
234245
if not result then
235246
result = false
236247
end
@@ -266,7 +277,11 @@ dtutils_file.libdoc.functions["chop_filetype"] = {
266277

267278
function dtutils_file.chop_filetype(path)
268279
local length = dtutils_file.get_filetype(path):len() + 2
269-
return string.sub(path, 1, -length)
280+
if length > 2 then
281+
return string.sub(path, 1, -length)
282+
else
283+
return path
284+
end
270285
end
271286

272287
dtutils_file.libdoc.functions["file_copy"] = {
@@ -373,7 +388,7 @@ dtutils_file.libdoc.functions["filename_increment"] = {
373388
"01" is added to the basename. If the filename already has an increment, then
374389
1 is added to it and the filename returned.]],
375390
Return_Value = [[result - string - the incremented filename]],
376-
Limitations = [[]],
391+
Limitations = [[The filename will be incremented to 99]],
377392
Example = [[]],
378393
See_Also = [[]],
379394
Reference = [[]],
@@ -396,6 +411,9 @@ function dtutils_file.filename_increment(filepath)
396411
if string.len(increment) > 2 then
397412
-- we got the filename so set the increment to 01
398413
increment = "01"
414+
elseif increment == "99" then
415+
dt.print_error("not incrementing, filename has already been incremented 99 times.")
416+
return filepath
399417
else
400418
increment = string.format("%02d", tonumber(increment) + 1)
401419
basename = string.gsub(basename, "_(%d-)$", "")
@@ -433,8 +451,9 @@ function dtutils_file.create_unique_filename(filepath)
433451
while dtutils_file.check_if_file_exists(filepath) do
434452
filepath = dtutils_file.filename_increment(filepath)
435453
-- limit to 99 more exports of the original export
436-
if string.match(dtutils_file.get_basename(filepath), "_(d-)$") == "99" then
437-
break
454+
local increment = string.match(dtutils_file.get_basename(filepath), "_(%d-)$")
455+
if increment == "99" then
456+
break
438457
end
439458
end
440459
return filepath
@@ -576,6 +595,7 @@ dtutils_file.libdoc.functions["mkdir"] = {
576595
License = [[]],
577596
Copyright = [[]],
578597
}
598+
579599
function dtutils_file.mkdir(path)
580600
if not dtutils_file.check_if_file_exists(path) then
581601
local mkdir_cmd = dt.configuration.running_os == "windows" and "mkdir" or "mkdir -p"

lib/dtutils/string.lua

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ dtutils_string.libdoc.functions["urlencode"] = {
167167
function dtutils_string.urlencode(str)
168168
if (str) then
169169
str = string.gsub (str, "\n", "\r\n")
170-
str = string.gsub (str, "([^%w ])", function () return string.format ("%%%02X", string.byte()) end)
170+
str = string.gsub (str, "([^%w ])", function (c) return string.format ("%%%02X", string.byte(c)) end)
171171
str = string.gsub (str, " ", "+")
172172
end
173173
return str
@@ -194,14 +194,41 @@ dtutils_string.libdoc.functions["sanitize"] = {
194194

195195
function dtutils_string.sanitize(str)
196196
local result = ""
197+
local os_quote = dt.configuration.running_os == "windows" and '"' or "'"
197198

198-
if dt.configuration.running_os == "windows" then
199-
result = '"' .. str .. '"'
200-
else
201-
result = "'" .. str .. "'"
199+
if dtutils_string.is_not_sanitized(str) then
200+
result = os_quote .. str .. os_quote
202201
end
203202

204203
return result
205204
end
206205

206+
dtutils_string.libdoc.functions["is_not_sanitized"] = {
207+
Name = [[is_not_sanitized]],
208+
Synopsis = [[Check if a string has been sanitized]],
209+
Usage = [[local ds = require "lib/dtutils.string"
210+
local result = ds.is_not_sanitized(str)
211+
str - string - the string that needs to be made safe]],
212+
Description = [[is_not_sanitized checks a string to see if it
213+
has been made safe use passing as an argument in a system command.]],
214+
Return_Value = [[result - boolean - true if the string is not sanitized otherwise false]],
215+
Limitations = [[]],
216+
Example = [[]],
217+
See_Also = [[]],
218+
Reference = [[]],
219+
License = [[]],
220+
Copyright = [[]],
221+
}
222+
223+
function dtutils_string.is_not_sanitized(str)
224+
local os_quote = dt.configuration.running_os == "windows" and '"' or "'"
225+
226+
if string.match(str, os_quote .. ".*" .. os_quote) then
227+
return false
228+
else
229+
return true
230+
end
231+
end
232+
233+
207234
return dtutils_string

0 commit comments

Comments
 (0)