fix(filesystem): mkdir handle multiple parents

This commit is contained in:
Aire-One 2022-12-19 22:47:47 +01:00
parent fcad0b33f9
commit 0c4b2a84f3
5 changed files with 108 additions and 2 deletions

View File

@ -7,8 +7,8 @@
"editor.acceptSuggestionOnEnter": "off" "editor.acceptSuggestionOnEnter": "off"
}, },
"cSpell.words": [ "cSpell.words": [
"aireone",
"aire-one", "aire-one",
"aireone",
"ansicolors", "ansicolors",
"awesomewm", "awesomewm",
"buildx", "buildx",
@ -21,6 +21,7 @@
"lldebugger", "lldebugger",
"Luacheck", "Luacheck",
"luacheckrc", "luacheckrc",
"luafilesystem",
"lualogging", "lualogging",
"Luarocks", "Luarocks",
"luasec", "luasec",

View File

@ -16,6 +16,7 @@ dependencies = {
"penlight 1.13.1", "penlight 1.13.1",
"luasocket 3.1.0-1", "luasocket 3.1.0-1",
"luasec 1.2.0-1", "luasec 1.2.0-1",
"luafilesystem 1.8.0-1",
} }
build = { build = {
type = "builtin", type = "builtin",

View File

@ -1,14 +1,35 @@
local file = require "pl.file" local file = require "pl.file"
local lfs = require "lfs"
local logger = require "logger" local logger = require "logger"
local path = require "pl.path" local path = require "pl.path"
local log = logger.log("file_writer") local log = logger.log("file_writer")
local function mkdir(dir_path: string)
local sep, parent_dir = "/", ""
for dir in dir_path:gmatch("[^" .. sep .. "]+") do
parent_dir = parent_dir .. dir .. sep
if not path.isdir(parent_dir) then
local success, error_message = lfs.mkdir(parent_dir)
if not success then
log:error(logger.message_with_metadata(
"Failed to create directory",
{
directory = parent_dir,
error = error_message,
}
))
error("Failed to create directory " .. parent_dir)
end
end
end
end
local function write_file(file_content: string, file_path: string): boolean, nil | string local function write_file(file_content: string, file_path: string): boolean, nil | string
-- Make sure the directory we want to write the file to exists -- Make sure the directory we want to write the file to exists
local directory = path.dirname(file_path) local directory = path.dirname(file_path)
if not path.isdir(directory) then if not path.isdir(directory) then
path.mkdir(directory) mkdir(directory)
end end
return file.write(file_path, file_content, false) return file.write(file_path, file_content, false)

View File

@ -4,5 +4,6 @@ return {
include_dir = { include_dir = {
"src/awesomewm.d.tl", "src/awesomewm.d.tl",
"types", "types",
"generated", -- used to remove require error when visualizing the generated type definitions
}, },
} }

82
types/lfs.d.tl Normal file
View File

@ -0,0 +1,82 @@
local record lfs
enum FileMode
"file"
"directory"
"link"
"socket"
"named pipe"
"char device"
"block device"
"other"
end
record Attributes
dev: number
ino: number
mode: FileMode
nlink: number
uid: number
gid: number
rdev: number
access: number
modification: number
change: number
size: number
permissions: string
blocks: number
blksize: number
end
enum OpenFileMode
"binary"
"text"
end
enum LockMode
"r"
"w"
end
record Lock
free: function()
end
dir: function(string): function(): string
chdir: function(string): boolean, string
lock_dir: function(string, number): Lock, string
-- returns number on success, really!? this should be fixed in the lfs library
link: function(string, string, boolean): number, string
mkdir: function(string): boolean, string
rmdir: function(string): boolean, string
setmode: function(string, OpenFileMode): boolean, string
currentdir: function(): string
attributes: function(string): Attributes
attributes: function(string, string): string
attributes: function(string, string): number
attributes: function(string, string): FileMode
attributes: function(string, Attributes): Attributes
symlinkattributes: function(string): Attributes
symlinkattributes: function(string, string): string
symlinkattributes: function(string, string): number
symlinkattributes: function(string, string): FileMode
symlinkattributes: function(string, Attributes): Attributes
touch: function(string, number, number): boolean, string
-- TODO: FILE needs to be renamed to io.FILE in tl itself
lock: function(FILE, LockMode, number, number): boolean, string
unlock: function(FILE, number, number): boolean, string
end
return lfs