@@ -35,6 +35,112 @@ local function _(msgid)
35
35
return gettext .dgettext (" dtutils.file" , msgid )
36
36
end
37
37
38
+ dtutils_file .libdoc .functions [" is_dir" ] = {
39
+ Name = [[ is_dir]] ,
40
+ Synopsis = [[ check if a path is a directory]] ,
41
+ Usage = [[ local df = require "lib/dtutils.file"
42
+
43
+ local result = df.is_dir(path)
44
+ path - string - the path to check]] ,
45
+ Description = [[ is_dir checks a path to see if it is a directory]] ,
46
+ Return_Value = [[ result - boolean - true if path is a directory, nil if not]] ,
47
+ Limitations = [[ ]] ,
48
+ Example = [[ ]] ,
49
+ See_Also = [[ ]] ,
50
+ Reference = [[ ]] ,
51
+ License = [[ ]] ,
52
+ Copyright = [[ ]] ,
53
+ }
54
+
55
+ function dtutils_file .is_dir (path )
56
+ local cmd = nil
57
+
58
+ if dt .configuration .running_os == " windows" then
59
+ cmd = " if exist " .. ds .sanitize (path .. " \\ *" )
60
+ else
61
+ cmd = " test -d " .. ds .sanitize (path )
62
+ end
63
+
64
+ return os.execute (cmd )
65
+ end
66
+
67
+ dtutils_file .libdoc .functions [" is_file" ] = {
68
+ Name = [[ is_file]] ,
69
+ Synopsis = [[ check if a path is a file]] ,
70
+ Usage = [[ local df = require "lib/dtutils.file"
71
+
72
+ local result = df.is_file(path)
73
+ path - string - the path to check]] ,
74
+ Description = [[ is_file checks a path to see if it is a file]] ,
75
+ Return_Value = [[ result - boolean - true if path is a file, nil if not]] ,
76
+ Limitations = [[ ]] ,
77
+ Example = [[ ]] ,
78
+ See_Also = [[ ]] ,
79
+ Reference = [[ ]] ,
80
+ License = [[ ]] ,
81
+ Copyright = [[ ]] ,
82
+ }
83
+
84
+ function dtutils_file .is_file (path )
85
+ local cmd = nil
86
+
87
+ if dt .configuration .running_os == " windows" then
88
+ if not dtutils_file .is_dir (path ) then
89
+ cmd = " if exist " .. ds .sanitize (path )
90
+ else
91
+ return nil
92
+ end
93
+ else
94
+ cmd = " test -f " .. ds .sanitize (path )
95
+ end
96
+
97
+ return os.execute (cmd )
98
+ end
99
+
100
+ dtutils_file .libdoc .functions [" is_executable" ] = {
101
+ Name = [[ is_executable]] ,
102
+ Synopsis = [[ check if a path is a executable]] ,
103
+ Usage = [[ local df = require "lib/dtutils.file"
104
+
105
+ local result = df.is_executable(path)
106
+ path - string - the path to check]] ,
107
+ Description = [[ is_executable checks a path to see if it is an executable]] ,
108
+ Return_Value = [[ result - boolean - true if path is an executable, nil if not]] ,
109
+ Limitations = [[ ]] ,
110
+ Example = [[ ]] ,
111
+ See_Also = [[ ]] ,
112
+ Reference = [[ ]] ,
113
+ License = [[ ]] ,
114
+ Copyright = [[ ]] ,
115
+ }
116
+
117
+ local function _is_windows_executable (path )
118
+ local result = nil
119
+
120
+ if (string.match (path , " .exe$" ) or string.match (path , " .EXE$" )) or
121
+ (string.match (path , " .com$" ) or string.match (path , " .COM$" )) or
122
+ (string.match (path , " .bat$" ) or string.match (path , " .BAT$" )) or
123
+ (string.match (path , " .cmd$" ) or string.match (path , " .CMD$" )) then
124
+ result = true
125
+ end
126
+ return result
127
+ end
128
+
129
+
130
+ function dtutils_file .is_executable (path )
131
+
132
+ local result = nil
133
+
134
+ if dt .configuration .running_os == " windows" then
135
+ if _is_windows_executable (path ) then
136
+ result = true
137
+ end
138
+ else
139
+ result = os.execute (" test -x " .. ds .sanitize (path ))
140
+ end
141
+ return result
142
+ end
143
+
38
144
dtutils_file .libdoc .functions [" check_if_bin_exists" ] = {
39
145
Name = [[ check_if_bin_exists]] ,
40
146
Synopsis = [[ check if an executable exists]] ,
@@ -67,11 +173,9 @@ local function _check_if_bin_exists_windows(bin)
67
173
path = dtutils_file .get_executable_path_preference (bin )
68
174
end
69
175
70
- if (string.match (path , " .exe$" ) or string.match (path , " .EXE$" )) or
71
- (string.match (path , " .com$" ) or string.match (path , " .COM$" )) or
72
- (string.match (path , " .bat$" ) or string.match (path , " .BAT$" )) or
73
- (string.match (path , " .cmd$" ) or string.match (path , " .CMD$" )) then
74
- if dtutils_file .check_if_file_exists (path ) then
176
+
177
+ if dtutils_file .check_if_file_exists (path ) then
178
+ if dtutils_file .is - executable (path ) then
75
179
result = dtutils_file .sanitize_filename (path )
76
180
end
77
181
end
@@ -93,16 +197,14 @@ local function _check_if_bin_exists_nix(bin)
93
197
94
198
if string.len (path ) > 0 then
95
199
-- check for windows executable to run under wine
96
- if string.match (path , " .exe$ " ) or string.match ( path , " .EXE$ " ) then
200
+ if _is - windows_executable (path ) then
97
201
if dtutils_file .check_if_file_exists (path ) then
98
202
result = dtutils_file .sanitize_filename (path )
99
203
end
100
204
else
101
205
if dtutils_file .check_if_file_exists (path ) then
102
- local spath = dtutils_file .sanitize_filename (path )
103
- -- check that it's an executable file
104
- if os.execute (" test -f " .. spath .. " && test -x " .. spath ) then
105
- result = spath
206
+ if dtutils_file .is_file (path ) and dtutils_file .is_executable (path ) then
207
+ result = ds .sanitize (path )
106
208
end
107
209
end
108
210
end
@@ -112,8 +214,9 @@ local function _check_if_bin_exists_nix(bin)
112
214
local output = p :read (" *a" )
113
215
p :close ()
114
216
if string.len (output ) > 0 then
217
+
115
218
local spath = dtutils_file .sanitize_filename (output :sub (1 ,- 2 ))
116
- if os.execute ( " test -f " .. spath .. " && test -x " .. spath ) then
219
+ if dtutils_file . is_file ( spath ) and dtutils_file . is_executable ( spath ) then
117
220
result = spath
118
221
end
119
222
end
@@ -670,6 +773,61 @@ function dtutils_file.rmdir(path)
670
773
return dsys .external_command (rm_cmd .. " " .. path )
671
774
end
672
775
776
+ function dtutils_file .is_dir (path )
777
+ local cmd = nil
778
+
779
+ if dt .configuration .running_os == " windows" then
780
+ cmd = " if exist " .. ds .sanitize (path .. " \\ *" )
781
+ else
782
+ cmd = " test -d " .. ds .sanitize (path )
783
+ end
784
+
785
+ return os.execute (cmd )
786
+ end
787
+
788
+ function dtutils_file .is_file (path )
789
+ local cmd = nil
790
+
791
+ if dt .configuration .running_os == " windows" then
792
+ if not dtutils_file .is_dir (path ) then
793
+ cmd = " if exist " .. ds .sanitize (path )
794
+ else
795
+ return nil
796
+ end
797
+ else
798
+ cmd = " test -f " .. ds .sanitize (path )
799
+ end
800
+
801
+ return os.execute (cmd )
802
+ end
803
+
804
+ local function is_windows_executable (path )
805
+ local result = nil
806
+
807
+ if (string.match (path , " .exe$" ) or string.match (path , " .EXE$" )) or
808
+ (string.match (path , " .com$" ) or string.match (path , " .COM$" )) or
809
+ (string.match (path , " .bat$" ) or string.match (path , " .BAT$" )) or
810
+ (string.match (path , " .cmd$" ) or string.match (path , " .CMD$" )) then
811
+ result = true
812
+ end
813
+ return result
814
+ end
815
+
816
+
817
+ function dtutils_file .is_executable (path )
818
+
819
+ local result = nil
820
+
821
+ if dt .configuration .running_os == " windows" then
822
+ if _is_windows_executable (path ) then
823
+ result = true
824
+ end
825
+ else
826
+ result = os.execute (" test -x " .. ds .sanitize (path ))
827
+ end
828
+ return result
829
+ end
830
+
673
831
674
832
return dtutils_file
675
833
0 commit comments