fs
File access restricted to the C:\xiaohook folder, available at xiao.fs. Paths are relative to C:\xiaohook, / and \ both work, and .. is allowed as long as the resolved path stays inside C:\xiaohook.
Every failure raises an error, use pcall to handle it.
Refused paths
- absolute paths and drive letters (
C:/...,c:file), and any:(NTFS streams such asfile.txt:stream) - network paths (
\\server\share,//server/share) - paths that leave
C:\xiaohook, including through a junction or a symbolic link pointing outside - Windows device names in any component (
CON,PRN,AUX,NUL,COM1toCOM9,LPT1toLPT9, with or without extension) - components ending with a dot or a space
Functions (disk)
xiao.fs.read(path: string): string
xiao.fs.read(path: string): string
- Returns the whole file as a string (binary safe).
xiao.fs.write(path: string, data: string)
xiao.fs.write(path: string, data: string)
- Creates or replaces the file.
- The folder must exist.
xiao.fs.append(path: string, data: string)
xiao.fs.append(path: string, data: string)
- Appends to the file, creating it if needed.
xiao.fs.isfile(path: string): boolean
xiao.fs.isfile(path: string): boolean
- Returns a boolean.
xiao.fs.isfolder(path: string): boolean
xiao.fs.isfolder(path: string): boolean
- Returns a boolean.
""or"."isC:\xiaohookitself.
xiao.fs.listfiles(path?: string): string[]
xiao.fs.listfiles(path?: string): string[]
- Returns the files and folders inside
path, sorted, as paths relative toC:\xiaohookwith/.
xiao.fs.makefolder(path: string)
xiao.fs.makefolder(path: string)
- Creates the folder and every missing parent.
xiao.fs.delfolder(path: string)
xiao.fs.delfolder(path: string)
- Deletes the folder and its content.
C:\xiaohookitself cannot be deleted.
xiao.fs.delfile(path: string)
xiao.fs.delfile(path: string)
- Deletes a file.
Path helpers (strings only)
The path helpers (forward to sanitize) only transform strings and never touch the disk.
xiao.fs.forward(path: string): string
xiao.fs.forward(path: string): string
- Replaces every
\with/.
xiao.fs.backward(path: string): string
xiao.fs.backward(path: string): string
- Replaces every
/with\.
xiao.fs.join(a: string, b: string): string
xiao.fs.join(a: string, b: string): string
- Joins two paths with a single separator.
xiao.fs.extname(path: string, replace?: string): string
xiao.fs.extname(path: string, replace?: string): string
- Returns the extension without the dot (
"gz"fora.tar.gz,""for.hidden). - With
replace, returns the path with that extension,""removes it.
xiao.fs.filename(path: string, replace?: string): string
xiao.fs.filename(path: string, replace?: string): string
- Returns the file name.
- With
replace, returns the path with that file name.
xiao.fs.dirname(path: string, replace?: string): string
xiao.fs.dirname(path: string, replace?: string): string
- Returns the folder part.
- With
replace, returns the path moved to that folder.
xiao.fs.sanitize(path: string): string
xiao.fs.sanitize(path: string): string
- Replaces control characters and
< > : " | ? *with_.
Example
local fs = xiao.fs
fs.makefolder("cache/session")
fs.write("cache/session/log.txt", os.date("%x %X") .. "\n")
fs.append("cache/session/log.txt", "started\n")
for _, path in ipairs(fs.listfiles("cache/session")) do
print(path, fs.extname(path))
end
local font = fs.read("lua/fonts/proggy.ttf")
xiao.paint.text.bake("Proggy", font, 14)