-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathlfsx.lua
More file actions
220 lines (197 loc) · 6.58 KB
/
lfsx.lua
File metadata and controls
220 lines (197 loc) · 6.58 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
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)
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)
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
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