-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.lua
208 lines (188 loc) · 5.1 KB
/
fs.lua
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
local common = require 'lithium.common'
local fs = {}
function fs.chdir()
return nil, 'changing current directory is not implemented'
end
if common.isWindows then
local function escapePath(path)
return (path:gsub('/', '\\'))
end
function fs.dir(filepath)
if filepath == '' then
error('could not list files', 2)
end
local commandLine = common.formCommand('dir', '/b', escapePath(filepath) .. '\\'):sub(2, -2)
local nextLine = common.commandLines('"echo .& echo ..& ' .. commandLine .. ' 2>nul && echo //0|| echo //1"')
return function()
local line = nextLine()
if not line then
return nil
end
if line:match('^//%d+$') then
if line ~= '//0' then
error('could not list files', 2)
end
return nil
end
return line
end
end
function fs.mkdir(dirname)
local commandLine = common.formCommand('mkdir', escapePath(dirname)):sub(2, -2)
local status = 1
for line in common.commandLines(commandLine .. ' 2>nul && echo 0') do
status = tonumber(line) or 1
break
end
if status ~= 0 then
return nil, 'could not create directory', status
end
return true
end
function fs.rmdir(dirname)
local commandLine = common.formCommand('rd', escapePath(dirname)):sub(2, -2)
local status = 1
for line in common.commandLines(commandLine .. ' 2>nul && echo 0') do
status = tonumber(line) or 1
break
end
if status ~= 0 then
return nil, 'could not remove directory', status
end
return true
end
function fs.currentdir()
local path = ''
for line in common.commandLines('echo(%CD%') do
path = line
break
end
if path == '' then
return nil, 'could not get current directory'
end
return path
end
else
local function escapePath(path)
if path:match('^%-') then
return './' .. path
end
return path
end
local modeMap = {
[ 1] = 'named pipe',
[ 2] = 'char device',
[ 4] = 'directory',
[ 6] = 'block device',
[ 8] = 'file',
[10] = 'link',
[12] = 'socket',
}
local function attributes(follow, filepath, request)
filepath = escapePath(filepath)
local commandParams = {'stat', '-c', '%d\t%i\t%f\t%h\t%u\t%g\t%t\t%X\t%Y\t%Z\t%s\t%A\t%b\t%o', filepath}
if follow then
table.insert(commandParams, 2, '-L')
end
local commandLine = common.formCommand(common.unpack(commandParams))
local params = {}
for line in common.commandLines(commandLine .. ' 2>/dev/null') do
for match in line:gmatch('[^\t]+') do
table.insert(params, match)
end
break
end
if #params ~= 14 then
return nil, 'could not get file attributes'
end
local info = (type(request) == 'table') and request or {}
info.dev = tonumber(params[1])
info.ino = tonumber(params[2])
info.mode = modeMap[math.floor(tonumber(params[3], 16) / 2 ^ 12)] or 'other'
info.nlink = tonumber(params[4])
info.uid = tonumber(params[5])
info.gid = tonumber(params[6])
info.rdef = tonumber(params[7])
info.access = tonumber(params[8])
info.modification = tonumber(params[9])
info.change = tonumber(params[10])
info.size = tonumber(params[11])
info.permissions = params[12]
info.blocks = tonumber(params[13])
info.blksize = tonumber(params[14])
info.permissions = info.permissions:sub(-9) -- for compatibility with lfs
if not follow and info.mode == 'link' then
local commandLine = common.formCommand('readlink', filepath)
for line in common.commandLines(commandLine .. ' 2>/dev/null') do
info.target = line
break
end
end
if type(request) == 'string' then
return assert(info[request])
end
return info
end
function fs.attributes(filepath, request)
return attributes(true, filepath, request)
end
function fs.symlinkattributes(filepath, request)
return attributes(false, filepath, request)
end
function fs.dir(filepath)
if filepath == '' then
error('could not list files', 2)
end
local commandLine = common.formCommand('ls', '-a', escapePath(filepath) .. '/')
local nextLine = common.commandLines(commandLine .. ' 2>/dev/null ; echo "//$?"')
return function()
local line = nextLine()
if not line then
return nil
end
if line:match('^//%d+$') then
if line ~= '//0' then
error('could not list files', 2)
end
return nil
end
return line
end
end
function fs.mkdir(dirname)
local commandLine = common.formCommand('mkdir', escapePath(dirname))
local status = 1
for line in common.commandLines(commandLine .. ' 2>/dev/null ; echo $?') do
status = tonumber(line) or 1
break
end
if status ~= 0 then
return nil, 'could not create directory', status
end
return true
end
function fs.rmdir(dirname)
local commandLine = common.formCommand('rmdir', escapePath(dirname))
local status = 1
for line in common.commandLines(commandLine .. ' 2>/dev/null ; echo $?') do
status = tonumber(line) or 1
break
end
if status ~= 0 then
return nil, 'could not remove directory', status
end
return true
end
function fs.currentdir()
local path = ''
for line in common.commandLines('ls -d "$PWD" 2>/dev/null') do
path = line
break
end
if path == '' then
return nil, 'could not get current directory'
end
return path
end
end
return fs