Repository navigation
Expand file tree
/
Copy pathlfsx.lua
More file actions
338 lines (297 loc) · 10.2 KB
/
Copy pathlfsx.lua
File metadata and controls
338 lines (297 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
local lfs = require 'lfs'
-- deletes a non-empty directory
function lfs.rmdir_r(dir)
for file in lfs.dir(dir) do
local file_path = dir .. '/' .. file
if file ~= '.' and file ~= '..' then
local mode = lfs.attributes(file_path, 'mode')
if mode == 'file' then
os.remove(file_path)
elseif mode == 'directory' then
lfs.rmdir_r(file_path)
end
end
end
return lfs.rmdir(dir)
end
function lfs.pathsanitize(p)
if lfs.pathsep() == '\\' then
p = p:gsub('/', '\\')
end
return p
end
function lfs.pathsep()
return package.config:sub(1,1)
end
-- Returns the directory part and the file part, similar to Python's os.path.split
function lfs.pathsplit(p)
p = lfs.pathsanitize(p)
local sep = lfs.pathsep()
-- Normalize trailing separator (unless it's root '/')
if #p > 1 and p:sub(-1) == sep then
p = p:sub(1, -2)
end
local head, tail = p:match("^(.*"..sep..")([^"..sep.."]+)$")
if not head then
return "", p
else
-- Remove trailing separator from head unless it's root
if #head > 1 and head:sub(-1) == sep then
head = head:sub(1, -2)
end
return head, tail
end
end
-- Joins paths, taking into account absolute paths
function lfs.pathjoin(...)
local sep = lfs.pathsep()
local args = {...}
local result = ""
for i, part in ipairs(args) do
if part:sub(1, 1) == sep then
-- Absolute path resets the result
result = part
else
if result == "" or result:sub(-1) == sep then
result = result .. part
else
result = result .. sep .. part
end
end
end
return result
end
-- similar to pathlib.Path(...).parts
function lfs.pathparts(p)
p = lfs.pathsanitize(p)
local result = {}
local head, tail
head = p
while true do
if head == '' then break end
head, tail = lfs.pathsplit(head)
table.insert(result, 1, tail)
end
return result
end
function lfs.basename(path)
path = lfs.pathsanitize(path)
local name = string.gsub(path, '(.*' .. lfs.pathsep() .. ')(.*)', '%2')
return name
end
function lfs.dirname(path)
path = lfs.pathsanitize(path)
if path:match('.-' .. lfs.pathsep() .. '.-') then
local name = string.gsub(path, '(.*)(' .. lfs.pathsep() .. ')(.*)', '%1')
return name
else
return ''
end
end
function lfs.isfile(p)
return lfs.attributes(p, 'mode') == 'file'
end
function lfs.isdir(p)
return lfs.attributes(p, 'mode') == 'directory'
end
function lfs.ispathabsolute(path)
-- Handle empty or nil paths
if not path or path == '' then return false end
if lfs.pathsep() == '\\' then
-- Windows absolute path: starts with drive letter (e.g., C:\ or C:/)
if path:match('^%a:[/\\]') then return true end
-- Windows UNC path: starts with double backslashes (e.g., \\Server\Share)
if path:match('^\\\\') then return true end
else
-- Unix absolute path: starts with a slash
if path:sub(1, 1) == '/' then return true end
end
return false
end
function lfs.makedirs(path, exist_ok)
exist_ok = exist_ok or false
local sep = lfs.pathsep()
local parts = lfs.pathparts(path)
local current = ""
-- Windows drive letters or UNC paths need special handling
if lfs.ispathabsolute(path) then
-- If the first part is a drive letter or UNC root, preserve it
if parts[1]:match("^[A-Za-z]:$") or parts[1]:match("^\\\\") then
current = parts[1]
table.remove(parts, 1)
elseif sep == "/" then
current = sep
end
end
for _, part in ipairs(parts) do
if current == "" or current:sub(-1) == sep then
current = current .. part
else
current = current .. sep .. part
end
if not lfs.isdir(current) then
local ok, err = lfs.mkdir(current)
if not ok then
error('Could not create directory: ' .. current .. ' (' .. err .. ')')
end
elseif not exist_ok and not lfs.isdir(current) then
error('Path exists and is not a directory: ' .. current)
end
end
end
function lfs.gettempdir()
local temp = os.getenv("TMP") or os.getenv("TEMP") or os.getenv("TMPDIR")
if not temp and package.config:sub(1,1) == '/' then
-- On Unix, fallback to /tmp
temp = "/tmp"
elseif not temp then
-- On Windows, fallback to C:\Temp
temp = "C:\\Temp"
end
return temp
end
function lfs.realpath(path)
local dir, file = lfs.pathsplit(path)
local lower = file:lower()
for entry in lfs.dir(dir) do
if entry:lower() == lower then
return lfs.pathjoin(dir, entry)
end
end
end
local function getWalkAttributes(path, follow)
path = lfs.pathsanitize(path)
if follow then
return lfs.attributes(path)
end
if not lfs.symlinkattributes then
return nil, "symlinkattributes is unavailable; cannot honor follow=false"
end
local mode, err = lfs.symlinkattributes(path, "mode")
if not mode then
return nil, err
end
if mode == "link" then
-- Do not retrieve target attributes or follow the link.
return {mode = mode}
end
return lfs.attributes(path)
end
--- Recursively visit every file under `dir`.
-- @param dir string starting directory
-- @param fn function(path, attr) called for each regular file
-- @param opts table optional:
-- onDir (function(path)) called for each directory (before descending)
-- onOther (function(path, mode)) called for symlinks, sockets, etc.
-- follow (bool) follow symlinks (default false → avoids loops)
-- sort (bool) sort entries per directory (default false)
function lfs.walk(dir, fn, opts)
opts = opts or {}
dir = dir or '.'
-- collect first: safer than iterating while recursing on some platforms
local entries = {}
for entry in lfs.dir(dir) do
if entry ~= '.' and entry ~= '..' then
entries[#entries + 1] = entry
end
end
if opts.sort then table.sort(entries) end
for _, entry in ipairs(entries) do
local path = lfs.pathjoin(dir, entry)
local ok, attr, err = pcall(getWalkAttributes, path, opts.follow)
if not ok then
-- On an exception, the error message is in attr.
error(("Cannot inspect %q: %s"):format(path, tostring(attr)), 0)
end
if not attr then
error(("Cannot inspect %q: %s"):format(path, tostring(err)), 0)
end
local mode = attr.mode
if mode == 'directory' then
if opts.onDir then opts.onDir(path) end
lfs.walk(path, fn, opts)
elseif mode == 'file' then
fn(path, attr)
else
if opts.onOther then opts.onOther(path, mode) end
end
end
end
-- iterator-style walk, e.g.:
-- for path, attr in lfs.iwalk('src') do … end
function lfs.iwalk(root, opts)
opts = opts or {}
local function walk(dir)
local entries = {}
for entry in lfs.dir(dir) do
if entry ~= '.' and entry ~= '..' then
entries[#entries + 1] = entry
end
end
if opts.sort then table.sort(entries) end
for _, entry in ipairs(entries) do
local path = lfs.pathjoin(dir, entry)
local ok, attr, err = pcall(getWalkAttributes, path, opts.follow)
if not ok then
-- On an exception, the error message is in attr.
error(("Cannot inspect %q: %s"):format(path, tostring(attr)), 0)
end
if not attr then
error(("Cannot inspect %q: %s"):format(path, tostring(err)), 0)
end
local mode = attr.mode
if mode == 'directory' then
walk(path)
elseif mode == 'file' then
coroutine.yield(path, attr)
elseif opts.onOther then
opts.onOther(path, mode)
end
end
end
local co = coroutine.create(function() walk(root or '.') end)
return function()
local ok, path, attr = coroutine.resume(co)
if not ok then error(path, 0) end
if coroutine.status(co) == 'dead' then return nil end
return path, attr
end
end
function lfs.unittest()
require 'tablex'
if lfs.pathsep() == '\\' then
assert(lfs.pathsanitize('c:\\tmp\\foo.txt') == 'c:\\tmp\\foo.txt')
assert(lfs.pathsanitize('c:/tmp/foo.txt') == 'c:\\tmp\\foo.txt')
assert(lfs.pathjoin('c:', 'tmp', 'foo.txt') == 'c:\\tmp\\foo.txt')
assert(table.eq({lfs.pathsplit 'c:\\tmp\\foo.txt'}, {'c:\\tmp', 'foo.txt'}))
assert(table.eq(lfs.pathparts 'c:\\tmp\\foo.txt', {'c:', 'tmp', 'foo.txt'}))
assert(lfs.basename 'c:\\tmp\\foo.txt' == 'foo.txt')
assert(lfs.dirname 'c:\\tmp\\foo.txt' == 'c:\\tmp')
assert(lfs.dirname 'c:\\tmp1\\tmp2\\foo.txt' == 'c:\\tmp1\\tmp2')
assert(lfs.ispathabsolute 'c:\\tmp\\foo.txt')
assert(not lfs.ispathabsolute 'tmp\\foo.txt')
assert(lfs.pathsanitize 'c:/tmp/foo.txt', 'c:\\tmp\\foo.txt')
else
assert(table.eq({lfs.pathsplit '/usr/bin/foo'}, {'/usr/bin', 'foo'}))
assert(table.eq(lfs.pathparts '/usr/bin/foo', {'/', 'usr', 'bin', 'foo'}))
assert(lfs.pathjoin('/usr', 'bin', 'foo') == '/usr/bin/foo')
assert(lfs.basename '/usr/bin/foo' == 'foo')
assert(lfs.dirname '/usr/bin/foo' == '/usr/bin')
assert(lfs.ispathabsolute '/usr/bin/foo')
assert(not lfs.ispathabsolute 'bin/foo')
assert(lfs.pathsanitize '/usr/bin/foo', '/usr/bin/foo')
end
local tmp1 = lfs.pathjoin(lfs.gettempdir(), 'foo')
local tmp = lfs.pathjoin(tmp1, 'bar', 'baz')
if lfs.isdir(tmp) then
print('warning: ' .. tmp .. ' already exists')
print('removing ' .. tmp1 .. ' first...')
lfs.rmdir_r(tmp1)
end
assert(not lfs.isdir(tmp))
lfs.makedirs(tmp)
assert(lfs.isdir(tmp))
lfs.rmdir_r(tmp1)
print(debug.getinfo(1, 'S').source, 'tests passed')
end
return lfs