Swadhin Biswas

[email protected]
2026-03-05 13:56:37 +0600 committed NaN years ago
a3b9a64
commit a3b9a64
1 file changed
+23 -7
Showing 1 changed file+23-7
▼LUA/contexa/workspace.lua
+23-7
27
27
local attr = lfs.attributes(path)
28
28
return attr and attr.mode == "directory"
29
29
end
30
- -- fallback: try to open directory
30
+ -- fallback: use os.rename (works on most POSIX systems)
31
31
local ok, _, code = os.rename(path, path)
32
32
if ok then return true end
33
- -- code 13 = permission denied (exists but can't rename)
34
- return code == 13
33
+ if code == 13 then return true end
34
+ -- second fallback: try test -d
35
+ local ret = os.execute('test -d "' .. path .. '"')
36
+ -- Lua 5.1 returns number, 5.2+ returns boolean
37
+ if ret == true or ret == 0 then return true end
38
+ return false
35
39
end
36
40
37
41
local function file_exists(path)
42
46
43
47
local function mkdir_p(path)
44
48
if lfs_ok then
45
- -- split and create each segment
46
- local accum = ""
49
+ -- split and create each segment, preserving leading /
50
+ local accum = path:sub(1, 1) == "/" and "/" or ""
47
51
for seg in path:gmatch("[^/]+") do
48
- accum = accum == "" and seg or (accum .. "/" .. seg)
52
+ accum = accum == "" and seg
53
+ or accum == "/" and ("/" .. seg)
54
+ or (accum .. "/" .. seg)
49
55
if not dir_exists(accum) then
50
56
lfs.mkdir(accum)
51
57
end
52
58
end
53
59
return
54
60
end
55
- -- fallback: use os.execute
61
+ -- fallback: rely on system mkdir -p
56
62
os.execute('mkdir -p "' .. path .. '"')
63
+ -- verify, retry with segment-by-segment if needed
64
+ if not dir_exists(path) then
65
+ local accum = path:sub(1, 1) == "/" and "/" or ""
66
+ for seg in path:gmatch("[^/]+") do
67
+ accum = accum == "" and seg
68
+ or accum == "/" and ("/" .. seg)
69
+ or (accum .. "/" .. seg)
70
+ os.execute('mkdir "' .. accum .. '" 2>/dev/null')
71
+ end
72
+ end
57
73
end
58
74
59
75
local function read_file(path)
60
76