-
Notifications
You must be signed in to change notification settings - Fork 354
/
filesystem.lua
273 lines (244 loc) · 9.26 KB
/
filesystem.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
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
--[[ Copyright (c) 2010 Peter "Corsix" Cawley
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. --]]
local pathsep = package.config:sub(1, 1)
local part_pattern = "[^" .. pathsep .. "]+"
local ISO_FS = require("TH").iso_fs
--! Layer for abstracting away differences in file systems.
--
-- In the traditional case, the FileSystem is associated with a path on the
-- actual filesystem, from which all other operations are considered relative.
--
-- The FileSystem is also able to delegate operations to the ISO_FS library
-- in the engine for reading / listing files off an ISO image instead of a
-- file system.
class "FileSystem"
---@type FileSystem
local FileSystem = _G["FileSystem"]
function FileSystem:FileSystem()
-- A mapping of normalized file names in the current directory to their
-- actual path names.
self.files = nil
-- A mapping of normalized directory names in the current directory to
-- an object containing their actual physical path.
self.sub_dirs = nil
-- The actual filesystem path that is the basis/root of this FileSystem. Not
-- used when a provider is specified.
self.physical_path = nil
-- The ISO_FS provider if we are reading from an ISO instead of a
-- filesystem, otherwise nil.
self.provider = nil
end
--! Convert a file name to a canonical, case insensitive format.
-- The format is based on the limitations of the ISO filesystem.
local function normalise(str)
return str:upper():gsub("_", "-")
end
--! Populate the files and sub_dirs values for the current FileSystem path.
function FileSystem:_enumerate()
self.sub_dirs = {}
self.files = {}
for item in lfs.dir(self.physical_path) do
local path = self.physical_path .. pathsep .. item
if lfs.attributes(path, "mode") == "directory" then
self.sub_dirs[normalise(item)] = {physical_path = path}
else
self.files[normalise(item)] = path
end
end
end
--! Test if file name has an .iso or .dmg extension
function FileSystem:isIso(name)
if name == nil then
return false
end
local ext = name:lower():match("%.(.+)$")
return ext == 'iso' or ext == 'iso9660$' or ext == 'dmg'
end
--! Set the root physical path for this FileSystem.
-- If the path is an ISO then set the provider. If the path is a directory
-- then set the physical_path and populate the files and sub_dirs.
--
--!param physical_path (string) a path on the filesystem to either a directory
-- or theme hospital ISO file.
function FileSystem:setRoot(physical_path)
if self:isIso(physical_path) then
self.provider = ISO_FS()
self.provider:setPathSeparator(pathsep)
self.physical_path = physical_path
return self.provider:setRoot(physical_path)
end
if physical_path:sub(-1) == pathsep then
-- Trim off the trailing separator (lfs doesn't like querying the mode of a
-- directory with a trailing slash on win32)
physical_path = physical_path:sub(1, -2)
end
if lfs.attributes(physical_path, "mode") ~= "directory" then
return nil, "Specified path ('" .. physical_path .. "') is not a directory"
end
self.provider = nil
self.physical_path = physical_path
self:_enumerate()
return true
end
--! list the files in the given path.
--
--!param virtual_path (string) a path relative to the FileSystem root.
--!param ... (string) the virtual_path may be split into separate arguments
-- for each level in the filesystem.
--!return (object) a map of normalized names to actual names of files.
function FileSystem:listFiles(virtual_path, ...)
if ... then
virtual_path = table.concat({virtual_path, ...}, pathsep)
end
if self.provider then
return self.provider:listFiles(virtual_path)
elseif not self.sub_dirs then
return nil, "Filesystem layer not initialised"
end
for part in virtual_path:gmatch(part_pattern) do
local part_u = normalise(part)
if self.sub_dirs[part_u] then
self = self.sub_dirs[part_u]
if not self.files then
FileSystem._enumerate(self)
end
elseif self.files[part_u] then
return nil, ("Attempt to access file '%s' as if it were a directory while trying to enumerate '%s'"):format(part, virtual_path)
else
return nil, ("Unable to find '%s' while trying to enumerate '%s'"):format(part, virtual_path)
end
end
return self.files
end
--! Combine the given path segments into a single path string.
--!param virtual_path (string) a path relative to the FileSystem root.
--!param ... (string) the virtual_path may be split into separate arguments
-- for each level in the filesystem.
local function getFullPath(virtual_path, ...)
if ... then
virtual_path = table.concat({virtual_path, ...}, pathsep)
end
return virtual_path
end
--! Return the contents of the file at the given path.
--
--!param virtual_path (string) a path relative to the FileSystem root.
--!param ... (string) the virtual_path may be split into separate arguments
-- for each level in the filesystem.
function FileSystem:readContents(virtual_path, ...)
virtual_path = getFullPath(virtual_path, ...)
if self.provider then
return self.provider:readContents(virtual_path)
end
local file, err = self:_getFilePath(virtual_path)
if not file then
return file, err
end
local f, e = io.open(file, "rb")
if not f then
return nil, e
end
local data = f:read("*a")
f:close()
return data
end
--! Determines if the given path points to a real file.
--
--!param virtual_path (string) a path relative to the FileSystem root.
--!param ... (string) the virtual_path may be split into separate arguments
-- for each level in the filesystem.
function FileSystem:fileExists(virtual_path, ...)
virtual_path = getFullPath(virtual_path, ...)
if self.provider then
local found, err = self.provider:fileExists(virtual_path)
if found then
return true
else
return nil, err
end
end
local s, e = self:_getFilePath(virtual_path)
return (not not s), e
end
--! Get the size of the file at the given path.
--
--!param virtual_path (string) a path relative to the FileSystem root.
--!param ... (string) the virtual_path may be split into separate arguments
-- for each level in the filesystem.
--!return (numeric) Number of bytes in the given file. Nil if the file doesn't
-- exist.
function FileSystem:fileSize(virtual_path, ...)
virtual_path = getFullPath(virtual_path, ...)
if self.provider then
return self.provider:fileSize(virtual_path)
end
local s, e = self:_getFilePath(virtual_path)
if not s then
return nil, e
end
return lfs.attributes(s, "size")
end
--! Get a URI for the file.
--
-- The returned URI formats match those described by <https://ffmpeg.org/ffmpeg-protocols.html>.
--
--!param virtual_path (string) a path relative to the FileSystem root.
--!param ... (string) the virtual_path may be split into separate arguments
-- for each level in the filesystem.
--!return (string) A uri representing the files location
function FileSystem:fileUri(virtual_path, ...)
virtual_path = getFullPath(virtual_path, ...)
if self.provider then
local fileStart, fileEnd = self.provider:fileOffsets(virtual_path)
return 'subfile,,start,' .. fileStart .. ',end,' .. fileEnd .. ',,:' .. self.physical_path
end
return 'file:' .. virtual_path
end
-- If the file exists and we are not using a provider then return a FileSystem
-- rooted in the directory that the directory the file is contained in.
-- Otherwise return nil and an error message.
function FileSystem:_getFilePath(virtual_path, ...)
virtual_path = getFullPath(virtual_path, ...)
if self.provider then
return nil, "This function is not supported for providers"
end
if not self.sub_dirs then
return nil, "Filesystem layer not initialised"
end
local is_file = false
for part in virtual_path:gmatch(part_pattern) do
if is_file then
return nil, ("Attempt to access file '%s' as if it were a directory while trying to read '%s'"):format(part, virtual_path)
end
local part_u = normalise(part)
if self.sub_dirs[part_u] then
self = self.sub_dirs[part_u]
if not self.files then
FileSystem._enumerate(self)
end
elseif self.files[part_u] then
self = self.files[part_u]
is_file = true
else
return nil, ("Unable to find '%s' while trying to read '%s'"):format(part, virtual_path)
end
end
if not is_file then
return nil, ("Attempt to access directory '%s' as if it were a file"):format(virtual_path)
end
return self
end