diff options
-rw-r--r-- | src/luarocks/fs/win32.lua | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/luarocks/fs/win32.lua b/src/luarocks/fs/win32.lua index 9f85ac05..b6ea25e4 100644 --- a/src/luarocks/fs/win32.lua +++ b/src/luarocks/fs/win32.lua | |||
@@ -182,3 +182,36 @@ function replace_file(old_file, new_file) | |||
182 | return os.rename(new_file, old_file) | 182 | return os.rename(new_file, old_file) |
183 | end | 183 | end |
184 | 184 | ||
185 | --- Test is file/dir is writable. | ||
186 | -- Warning: testing if a file/dir is writable does not guarantee | ||
187 | -- that it will remain writable and therefore it is no replacement | ||
188 | -- for checking the result of subsequent operations. | ||
189 | -- @param file string: filename to test | ||
190 | -- @return boolean: true if file exists, false otherwise. | ||
191 | function is_writable(file) | ||
192 | assert(file) | ||
193 | file = dir.normalize(file) | ||
194 | local result | ||
195 | local tmpname = 'tmpluarockstestwritable.deleteme' | ||
196 | if fs.is_dir(file) then | ||
197 | local file2 = dir.path(file, tmpname) | ||
198 | local fh = io.open(file2, 'wb') | ||
199 | result = fh ~= nil | ||
200 | if fh then fh:close() end | ||
201 | if result then | ||
202 | -- the above test might give a false positive when writing to | ||
203 | -- c:\program files\ because of VirtualStore redirection on Vista and up | ||
204 | -- So get a directory listing and check whether it's really there | ||
205 | local pipe = io.popen("dir "..fs.Q(file)) | ||
206 | local dir_list = pipe:read("*a") | ||
207 | pipe:close() | ||
208 | result = (nil ~= string.find(dir_list, tmpname, 1, true)) | ||
209 | end | ||
210 | os.remove(file2) | ||
211 | else | ||
212 | local fh = io.open(file, 'r+b') | ||
213 | result = fh ~= nil | ||
214 | if fh then fh:close() end | ||
215 | end | ||
216 | return result | ||
217 | end | ||