Files
ziran/std/file_tree_linux.zi
T

638 lines
24 KiB
Ziran

// Paths, folders and whole files for native Linux tools and build programs:
// what a build script needs beyond file_linux's positional I/O. Paths are
// UTF-8 text of at most 4095 bytes.
//
// CreateDirectories("build/site/pictures")
// CopyTree("site", "build/site/page")
// names: Vec(string)
// if ListDirectory("levels", *names) { for names { ... } }
// lock := LockPath("backup/drafts/approval.lock")
// ReplaceFile("levels/approved.json", text)
// UnlockPath(lock)
//
// PathStatus reads a path's kind, owner, permission bits, size and modification
// time with statx, whose record has one layout on every little-endian Linux
// architecture.
#import "c_string"
#import "byte_text_linux"
#import "sort"
using Files :: #import "file_linux"
using Vectors :: #import "vec"
#scope_file
libc :: #system_library "libc";
StatxRaw :: (directory: s32, path: *u8, flags: s32, mask: u32, output: *StatxRecord) -> s32 #foreign libc "statx";
MakeDirectoryRaw :: (path: *u8, mode: u32) -> s32 #foreign libc "mkdir";
RemoveDirectoryRaw :: (path: *u8) -> s32 #foreign libc "rmdir";
UnlinkRaw :: (path: *u8) -> s32 #foreign libc "unlink";
ChangeModeRaw :: (path: *u8, mode: u32) -> s32 #foreign libc "chmod";
ChangeFileModeRaw :: (fd: s32, mode: u32) -> s32 #foreign libc "fchmod";
OpenRaw :: (path: *u8, flags: s32, mode: s32) -> s32 #foreign libc "open";
CloseRaw :: (fd: s32) -> s32 #foreign libc "close";
FlockRaw :: (fd: s32, operation: s32) -> s32 #foreign libc "flock";
FsyncRaw :: (fd: s32) -> s32 #foreign libc "fsync";
WriteRaw :: (fd: s32, input: *u8, count: usize) -> isize #foreign libc "write";
RenameRaw :: (source: *u8, target: *u8) -> s32 #foreign libc "rename";
RealPathRaw :: (path: *u8, output: *u8) -> *u8 #foreign libc "realpath";
TemporaryDirectoryRaw :: (template: *u8) -> *u8 #foreign libc "mkdtemp";
EffectiveUserRaw :: () -> u32 #foreign libc "geteuid";
WorkingDirectoryRaw :: (output: *u8, size: usize) -> *u8 #foreign libc "getcwd";
ProcessIdRaw :: () -> s32 #foreign libc "getpid";
ErrnoRaw :: () -> *s32 #foreign libc "__errno_location";
FreeOwnedRaw :: (memory: *void) #foreign libc "free";
SymbolicLinkRaw :: (target: *u8, path: *u8) -> s32 #foreign libc "symlink";
ReadLinkRaw :: (path: *u8, output: *u8, capacity: usize) -> isize #foreign libc "readlink";
StatxRecord :: struct { words: [32]u64 }
CURRENT_DIRECTORY :: -100;
NO_FOLLOW :: 256;
BASIC_STATS :: 2047;
EXISTS :: 17;
INTERRUPTED :: 4;
#scope_export
PathKind :: enum u8 { Missing :: 0; File :: 1; Directory :: 2; Link :: 3; Other :: 4; }
PathInfo :: struct {
kind: PathKind
// Numeric user ID of the owner, including when not following a link.
owner_uid: u32
// Permission bits, such as 0o644 written 420.
mode: s32
size: s64
// Seconds since 1970 UTC.
modified: s64
// Fractional nanoseconds of modified, 0 through 999999999; not total ns.
modified_nanoseconds: u32
// Linux statx availability mask; identity_valid requires type, inode,
// size, ctime and mtime, with valid fractions and a representable size.
metadata_mask: u32
identity_valid: bool
inode: u64
changed: s64
changed_nanoseconds: u32
device_major: u32
device_minor: u32
}
// A failed query has zero info. Only ENOENT/ENOTDIR mean missing;
// error otherwise preserves the positive Linux errno (EINVAL for invalid text).
PathQueryResult :: struct {
info: PathInfo
valid: bool
missing: bool
error: s32
}
// Follows links by default. Invalid text contains NUL or exceeds 4095 bytes.
PathQuery :: (path: string, follow_links: bool = true) -> PathQueryResult {
result: PathQueryResult
name: [4096]u8
if path.count > 4095 || !CopyCString(path, name[:]) {
result.error = 22 // EINVAL
return result
}
record: StatxRecord
flags: s32 = ifx follow_links then 0 else NO_FOLLOW
if StatxRaw(CURRENT_DIRECTORY, *name[0], flags, BASIC_STATS, *record) != 0 {
result.error = <<ErrnoRaw()
result.missing = result.error == 2 || result.error == 20 // ENOENT / ENOTDIR
return result
}
info: PathInfo
info.metadata_mask = cast(u32)record.words[0]
mode := cast(s32)((record.words[3] >> 32) & 65535)
type := mode & 61440
info.kind = .Other
if type == 32768 { info.kind = .File }
if type == 16384 { info.kind = .Directory }
if type == 40960 { info.kind = .Link }
info.owner_uid = cast(u32)(record.words[2] >> 32)
info.mode = mode & 4095
if (info.metadata_mask & 512) != 0 { info.size = cast(s64)record.words[5] }
if (info.metadata_mask & 256) != 0 { info.inode = record.words[4] }
modified_fraction := cast(u32)record.words[15]
changed_fraction := cast(u32)record.words[13]
if (info.metadata_mask & 64) != 0 && modified_fraction < 1000000000 {
info.modified = cast(s64)record.words[14]
info.modified_nanoseconds = modified_fraction
}
if (info.metadata_mask & 128) != 0 && changed_fraction < 1000000000 {
info.changed = cast(s64)record.words[12]
info.changed_nanoseconds = changed_fraction
}
// Device identifiers are statx stx_dev fields (not the rdev fields).
info.device_major = cast(u32)record.words[17]
info.device_minor = cast(u32)(record.words[17] >> 32)
info.identity_valid = (info.metadata_mask & 961) == 961 && info.size >= 0 &&
modified_fraction < 1000000000 && changed_fraction < 1000000000
result.info = info
result.valid = true
return result
}
// The path's metadata, or zero info on any failure. Use PathQuery when the
// distinction between a missing path and another error matters.
PathStatus :: (path: string, follow_links: bool = true) -> PathInfo {
return PathQuery(path, follow_links).info
}
// A complete identity of a regular file without following its final symlink.
FileSnapshot :: (path: string) -> PathInfo {
result := PathStatus(path, false)
if result.kind != .File { result.identity_valid = false }
return result
}
SameFileSnapshot :: (left: PathInfo, right: PathInfo) -> bool {
return left.identity_valid && right.identity_valid && left.kind == .File && right.kind == .File &&
left.device_major == right.device_major && left.device_minor == right.device_minor &&
left.inode == right.inode && left.size == right.size && left.modified == right.modified &&
left.modified_nanoseconds == right.modified_nanoseconds && left.changed == right.changed &&
left.changed_nanoseconds == right.changed_nanoseconds
}
IsFile :: (path: string) -> bool { return PathStatus(path).kind == .File }
IsDirectory :: (path: string) -> bool { return PathStatus(path).kind == .Directory }
SetPathMode :: (path: string, mode: s32) -> bool {
name: [4096]u8
if !CopyCString(path, name[:]) { return false }
return ChangeModeRaw(*name[0], cast(u32)mode) == 0
}
// DIRECTORY/NAME, or NAME alone when it is absolute or DIRECTORY is empty.
JoinPath :: (directory: string, name: string) -> Text {
if directory.count == 0 || (name.count > 0 && name[0] == #char "/") { return TextCopy(name) }
result: Vec(u8)
BuilderAppend(result, directory)
if directory[directory.count - 1] != #char "/" { VecPush(result, #char "/") }
BuilderAppend(result, name)
return BuilderFinish(result)
}
// The last part of PATH, after its final slash.
PathName :: (path: string) -> string {
end := path.count
while end > 1 && path[end - 1] == #char "/" { end -= 1 }
at := end
while at > 0 && path[at - 1] != #char "/" { at -= 1 }
return path[at:end]
}
// PATH without its last part: "." for a bare name, "/" for a root entry.
PathParent :: (path: string) -> string {
end := path.count
while end > 1 && path[end - 1] == #char "/" { end -= 1 }
at := end
while at > 0 && path[at - 1] != #char "/" { at -= 1 }
if at == 0 { return "." }
while at > 1 && path[at - 1] == #char "/" { at -= 1 }
return path[0:at]
}
// The name's extension with its dot, as Python's Path.suffix: ".png".
PathSuffix :: (path: string) -> string {
name := PathName(path)
at := name.count - 1
while at > 0 {
if name[at] == #char "." { return name[at:name.count] }
at -= 1
}
return ""
}
// The name without its extension, as Python's Path.stem.
PathStem :: (path: string) -> string {
name := PathName(path)
suffix := PathSuffix(path)
return name[0:name.count - suffix.count]
}
// The absolute path with every link and dot resolved; empty when PATH does
// not exist.
ResolvePath :: (path: string) -> Text {
name: [4096]u8
output: [4096]u8
if !CopyCString(path, name[:]) { return .{} }
if RealPathRaw(*name[0], *output[0]) == null { return .{} }
return CopyText(output[:])
}
// Atomically creates a unique private child of an existing caller-owned
// parent. The parent and its ancestors must stay at stable paths during the
// call. PREFIX is a single name component; six random characters follow it.
// Returns the canonical absolute child path, or empty on failure. The caller
// owns the returned allocation and directory cleanup with RemoveTree.
// Permissions are 0700, restricted by umask.
CreateTemporaryDirectory :: (parent: string, prefix: string = "ziran-") -> Text {
for prefix { if it == #char "/" || it == 0 { return .{} } }
directory := ResolvePath(parent)
if directory.count == 0 { return .{} }
defer { ReleaseOwnedText(directory) }
info := PathStatus(directory, false)
if info.kind != .Directory || info.owner_uid != EffectiveUserRaw() { return .{} }
// Allocate the returned owner before creating anything. mkdtemp changes
// only the final six bytes, copied back without a further allocation.
output: Vec(u8)
if !BuilderAppend(output,directory) ||
(directory[directory.count-1] != #char "/" && !VecPush(output,#char "/")) ||
!BuilderAppend(output,prefix) || !BuilderAppend(output,"XXXXXX") { return .{} }
name: [4096]u8
if !CopyCString(TextView(VecSlice(output,0,output.count)),name[:]) { return .{} }
if TemporaryDirectoryRaw(*name[0]) == null { return .{} }
for at: 0..output.count-1 { output[at] = name[at] }
return BuilderFinish(output)
}
#scope_file
// Only exact owned results from ResolvePath/CopyText are passed here. Generic
// strings, JoinPath's absolute-name branch and caller views may be borrowed.
ReleaseOwnedText :: (text: string) {
if text.count == 0 { return }
descriptor := cast(*TextDescriptor)*text
FreeOwnedRaw(cast(*void)descriptor.data)
}
#scope_export
WorkingDirectory :: () -> Text {
output: [4096]u8
if WorkingDirectoryRaw(*output[0], 4096) == null { return .{} }
return CopyText(output[:])
}
#scope_file
CopyText :: (bytes: []u8) -> Text {
result: Vec(u8)
for bytes {
if it == 0 { break }
if !VecPush(result, it) { return .{} }
}
return BuilderFinish(result)
}
#scope_export
// Creates PATH and every missing parent folder; true when it exists as a
// folder afterwards.
CreateDirectories :: (path: string, mode: s32 = 493) -> bool {
if path.count == 0 { return false }
if IsDirectory(path) { return true }
parent := PathParent(path)
if parent != path && parent != "." && parent != "/" && !CreateDirectories(parent, mode) { return false }
name: [4096]u8
if !CopyCString(path, name[:]) { return false }
if MakeDirectoryRaw(*name[0], cast(u32)mode) != 0 && <<ErrnoRaw() != EXISTS { return false }
return IsDirectory(path)
}
#scope_file
ReleaseNames :: (names: *Vec(string), from: s64) {
owned: Vec(string)
VecSwap(owned,<<names)
index := from
while index < owned.count { ReleaseOwnedText(owned[index]); index += 1 }
VecSwap(owned,<<names)
}
#scope_export
// Release every independently owned name returned by ListDirectory and the
// vector storage, resetting the vector to empty. Do not mix borrowed strings
// or duplicate ownership into this vector. Empty, repeated and null calls are
// harmless; all borrowed views of its names expire at this call.
FreeDirectoryNames :: (names: *Vec(string)) {
if names == null { return }
ReleaseNames(names, 0)
owned: Vec(string)
VecSwap(owned, <<names)
VecFree(owned)
}
// The names in a folder, without "." and "..", sorted by their bytes.
// Appends independently owned entry names in sorted order. The caller owns
// each nonempty string as well as the Vec; release both with FreeDirectoryNames.
// Failed scans append nothing.
ListDirectory :: (path: string, names: *Vec(string)) -> bool {
if names == null { return false }
cursor := OpenDirectory(path)
if !cursor.valid { return false }
open := true
defer { if open { unused CloseDirectory(*cursor) } }
found: Vec(string)
transferred: s64
defer { ReleaseNames(*found,transferred) }
while true {
entry := NextDirectoryEntry(*cursor)
if !entry.valid { break }
view := TextFromBytes(entry.name[0:entry.name_count])
if view == "." || view == ".." { continue }
text := CopyText(entry.name[0:entry.name_count])
if text.count == 0 { return false }
if !VecPush(found,text) { ReleaseOwnedText(text); return false }
}
failed := cursor.failed
closed := CloseDirectory(*cursor)
open = false
if failed || !closed { return false }
SortText(found)
if names.count > 9223372036854775807-found.count || !VecReserve(<<names,names.count+found.count) { return false }
while transferred < found.count {
if !VecPush(<<names,found[transferred]) { return false }
transferred += 1
}
return true
}
// Removes PATH and, for a folder, everything in it. Links are removed, never
// followed. True when nothing remains at PATH. All traversal scratch is freed.
RemoveTree :: (path: string) -> bool {
info := PathStatus(path, false)
if info.kind == .Missing { return true }
name: [4096]u8
if !CopyCString(path, name[:]) { return false }
if info.kind != .Directory { return UnlinkRaw(*name[0]) == 0 }
names: Vec(string)
if !ListDirectory(path, *names) { return false }
defer { ReleaseNames(*names,0) }
ok := true
index: s64 = 0
while index < names.count {
child: Vec(u8)
if !BuilderAppend(child,path) || (path[path.count-1] != #char "/" && !VecPush(child,#char "/")) ||
!BuilderAppend(child,names[index]) { return false }
if !RemoveTree(TextView(VecSlice(child,0,child.count))) { ok = false }
index += 1
}
return ok && RemoveDirectoryRaw(*name[0]) == 0
}
// Copies a file's bytes and permission bits to TARGET, replacing it.
CopyFile :: (source: string, target: string) -> bool {
info := PathStatus(source)
if info.kind != .File { return false }
input := OpenRead(source)
if !input.valid { return false }
output := OpenReplace(target)
if !output.valid { CloseFile(input); return false }
chunk: [65536]u8
offset: s64 = 0
ok := true
while true {
count := ReadAt(input, chunk[:], offset)
if count < 0 { ok = false; break }
if count == 0 { break }
if WriteAt(output, chunk[0:count], offset) != count { ok = false; break }
offset += count
}
CloseFile(input)
if !CloseFile(output) { ok = false }
return ok && SetPathMode(target, info.mode)
}
// Copies a folder and everything in it to TARGET, which is created; links
// inside are copied as links.
CopyTree :: (source: string, target: string) -> bool {
info := PathStatus(source)
if info.kind != .Directory { return false }
if !CreateDirectories(target) { return false }
names: Vec(string)
if !ListDirectory(source, *names) { return false }
index: s64 = 0
while index < names.count {
from := JoinPath(source, names[index])
to := JoinPath(target, names[index])
kind := PathStatus(from, false).kind
ok := false
if kind == .Directory { ok = CopyTree(from, to) }
else if kind == .File { ok = CopyFile(from, to) }
else if kind == .Link { ok = CopyLink(from, to) }
if !ok { return false }
index += 1
}
return SetPathMode(target, info.mode)
}
// Copy the symbolic link itself, including a dangling or directory link.
// The native symlink operation fails when TARGET is occupied.
CopySymbolicLink :: (source: string, target: string) -> bool {
return CopyLink(source, target)
}
// Copy to an exclusively created private file, preserving permission bits on
// success. An incomplete file remains private. Links
// are copied as links. Failure may leave a partial new file; never removes
// a destination. Keep source and destination ancestors stable during copying.
CopyFileNew :: (source: string, target: string) -> bool {
info := PathStatus(source, false)
if info.kind == .Link { return CopySymbolicLink(source, target) }
if info.kind != .File { return false }
input := OpenRead(source)
if !input.valid { return false }
output := OpenExclusivePrivate(target)
if !output.valid { unused CloseFile(input); return false }
chunk: [65536]u8
offset: s64 = 0
ok := true
while true {
count := ReadAt(input, chunk[:], offset)
if count < 0 { ok = false; break }
if count == 0 { break }
if WriteAt(output, chunk[0:count], offset) != count { ok = false; break }
offset += count
}
if !CloseFile(input) { ok = false }
// Apply mode through the owned descriptor, never through a replaced path.
if ok && ChangeFileModeRaw(output.fd, cast(u32)info.mode) != 0 { ok = false }
if !CloseFile(output) { ok = false }
return ok
}
// Copy into a new directory under an existing parent, with exclusive child
// creation and links preserved. Reject self/descendant destinations, including
// aliases through symlink parents. Failure may leave a partial new tree, with
// no rollback deletion. Ancestors and source entries must remain stable.
CopyTreeNew :: (source: string, target: string) -> bool {
info := PathStatus(source, false)
if info.kind == .Link { return CopySymbolicLink(source, target) }
if info.kind != .Directory { return false }
canonical := ResolvePath(source)
defer { ReleaseOwnedText(canonical) }
parent := ResolvePath(PathParent(target))
defer { ReleaseOwnedText(parent) }
name := PathName(target)
if canonical.count == 0 || parent.count == 0 || name == "" || name == "." || name == ".." { return false }
if parent == canonical || (parent.count > canonical.count &&
parent[0:canonical.count] == canonical &&
(canonical == "/" || parent[canonical.count] == #char "/")) { return false }
destination := JoinPath(parent, name)
defer { ReleaseOwnedText(destination) }
return CopyTreeNewContents(canonical, destination, info.mode)
}
#scope_file
CopyTreeNewContents :: (source: string, target: string, mode: s32) -> bool {
name: [4096]u8
if !CopyCString(target, name[:]) || MakeDirectoryRaw(*name[0], 448) != 0 { return false }
names: Vec(string)
defer { ReleaseNames(*names, 0) }
if !ListDirectory(source, *names) { return false }
for index: 0..names.count-1 {
from := JoinPath(source, names[index])
defer { ReleaseOwnedText(from) }
to := JoinPath(target, names[index])
defer { ReleaseOwnedText(to) }
info := PathStatus(from, false)
ok := false
if info.kind == .Directory { ok = CopyTreeNewContents(from, to, info.mode) }
else if info.kind == .File { ok = CopyFileNew(from, to) }
else if info.kind == .Link { ok = CopySymbolicLink(from, to) }
if !ok { return false }
}
return SetPathMode(target, mode)
}
CopyLink :: (source: string, target: string) -> bool {
name: [4096]u8
destination: [4096]u8
link: [4096]u8
if !CopyCString(source, name[:]) || !CopyCString(target, link[:]) { return false }
count := cast(s64)ReadLinkRaw(*name[0], *destination[0], 4096)
if count < 0 || count >= 4096 { return false }
destination[count] = 0
return SymbolicLinkRaw(*destination[0], *link[0]) == 0
}
#scope_export
// Replaces the file at PATH with TEXT so that readers see either the old or
// the new whole file: a private sibling file is written, synced and renamed
// over it. The new file keeps the old one's permission bits.
ReplaceFile :: (path: string, text: string) -> bool {
temporary_text: Vec(u8)
BuilderAppend(temporary_text, path)
BuilderAppend(temporary_text, ".new-")
digits: [24]u8
at: s64 = 24
pid := ProcessIdRaw()
while true {
at -= 1
digits[at] = cast(u8)(48 + pid % 10)
pid = pid / 10
if pid == 0 { break }
}
while at < 24 { VecPush(temporary_text, digits[at]); at += 1 }
temporary := BuilderFinish(temporary_text)
previous := PathStatus(path)
name: [4096]u8
final: [4096]u8
if !CopyCString(temporary, name[:]) || !CopyCString(path, final[:]) { return false }
mode: s32 = ifx previous.kind == .File then previous.mode else 420
fd := OpenRaw(*name[0], 577 | 524288, mode)
if fd < 0 { return false }
chunk: [65536]u8
written: s64 = 0
ok := true
while ok && written < text.count {
size: s64 = 0
while size < 65536 && written + size < text.count { chunk[size] = text[written + size]; size += 1 }
sent: s64 = 0
while sent < size {
count := cast(s64)WriteRaw(fd, *chunk[sent], cast(usize)(size - sent))
if count < 0 && <<ErrnoRaw() == INTERRUPTED { continue }
if count <= 0 { ok = false; break }
sent += count
}
written += size
}
if ok && FsyncRaw(fd) != 0 { ok = false }
if CloseRaw(fd) != 0 { ok = false }
if ok && previous.kind == .File { ok = ChangeModeRaw(*name[0], cast(u32)mode) == 0 }
if ok && RenameRaw(*name[0], *final[0]) != 0 { ok = false }
if !ok { UnlinkRaw(*name[0]) }
return ok
}
// Opens (creating) the lock file at PATH and takes its flock: exclusive or
// shared, waiting or not. Returns the descriptor to UnlockPath, or -1 when
// the file cannot be opened or, without wait, another holder has it.
LockPath :: (path: string, exclusive: bool = true, wait: bool = true) -> s32 {
name: [4096]u8
if !CopyCString(path, name[:]) { return -1 }
fd := OpenRaw(*name[0], 1 | 64 | 1024 | 524288, 420)
if fd < 0 { return -1 }
operation: s32 = ifx exclusive then 2 else 1
if !wait { operation |= 4 }
while FlockRaw(fd, operation) != 0 {
if <<ErrnoRaw() != INTERRUPTED {
CloseRaw(fd)
return -1
}
}
return fd
}
UnlockPath :: (fd: s32) {
if fd < 0 { return }
FlockRaw(fd, 8)
CloseRaw(fd)
}
// Whether NAME matches a shell wildcard PATTERN: * any run, ? one byte,
// [abc], [a-z] and [!abc] one byte of a set. Nothing else is special.
MatchName :: (name: string, pattern: string) -> bool {
return MatchAt(name, 0, pattern, 0)
}
#scope_file
MatchAt :: (name: string, at: s64, pattern: string, from: s64) -> bool {
n := at
p := from
while p < pattern.count {
token := pattern[p]
if token == #char "*" {
while p < pattern.count && pattern[p] == #char "*" { p += 1 }
if p == pattern.count { return true }
start := n
while start <= name.count {
if MatchAt(name, start, pattern, p) { return true }
start += 1
}
return false
}
if n >= name.count { return false }
if token == #char "?" {
n += 1
p += 1
continue
}
if token == #char "[" {
close := p + 1
if close < pattern.count && pattern[close] == #char "!" { close += 1 }
if close < pattern.count && pattern[close] == #char "]" { close += 1 }
while close < pattern.count && pattern[close] != #char "]" { close += 1 }
if close < pattern.count {
negate := pattern[p + 1] == #char "!"
index := ifx negate then p + 2 else p + 1
found := false
while index < close {
low := pattern[index]
high := low
if index + 2 < close && pattern[index + 1] == #char "-" {
high = pattern[index + 2]
index += 2
}
if name[n] >= low && name[n] <= high { found = true }
index += 1
}
if found == negate { return false }
n += 1
p = close + 1
continue
}
}
if name[n] != token { return false }
n += 1
p += 1
}
return n == name.count
}