Files
ziran/bootstrap/compiler_backend_process/file_tree_linux.c
T
waoziandClaude Opus 5.5 931fed4c86 Run ZX natively by default and integrate the finished assembly lanes
One commit, because the parts below only build together.

Allocator: an interval needed at the current instruction is never
evicted, and the allocated-program verifier checks that every operand sits
where the allocation says.

AArch64 encoder: compiler_aarch64.zi encodes allocated machine IR with the
shared ARM byte helpers, maps the function-address relocation,
sign-extends signed 8- and 16-bit loads and rejects what it cannot encode
with a named diagnostic. It has no AOT ELF writer yet.

Native default with checked pointers and no fallback: on linux-x86_64,
`ziran run app.zx`, `zi2zx run`, the embedding API's Auto and the Kryon
players run a bundle's verified program as native code in W^X memory.
Stray and freed pointers, double free, bounds and read-only stores trap. A
native gap is a named preflight error, nothing falls back to the engine,
and `--backend engine` picks it explicitly. zx2native uses the same
preflight. Native code exchanges module state for hot reload, checks the
stack, zeroes fresh locals, spends fuel at the engine's checkpoints and
returns records, arrays and strings from host calls. `zi2zx --debug` source
origins name lines in native traps and `zx2native --emit map`. The host
value API drops its VM prefix (HostValue, HOST_*). A call with a record
result loads the hidden result address into rax only after its stack
arguments are stored, and an indirect target stays in r10: rax could hold
a stack argument (Kryon's text-field clip guard arrived as an address) or
the target (indirect calls returning records crashed).

Self-hosting ports: compiler_backend_process, compiler_backend_cache,
compiler_native_names and compiler_runtime_emit replace the zir_c_build,
zir_c_cache and zir_c_process C, with checked bootstrap C and Makefile
rules. zir_c.toolchain again reports "compiling OUT failed:" with the head
of the compiler output and a truncation marker. The parallel scheduler no
longer stalls on a held compiler after reaping a finished job, and poll or
read errors now fail the build. A fresh BUILD_DIR (Kryon's runtime build)
builds: generated files have pattern rules, objects wait for the headers
they include, and engine flags are per object instead of leaking into zi2c.

Legacy removals F1-F10 and N1-N4: the duplicate LirX64 encoder and its
spec test, zir_c_*, zir_runtime and zir_native, nine zero-reference spec
tests, vec_capacity, the zx_metadata and native_cache_names benchmarks,
build-android-host.sh and two shell wrappers are deleted; zx2native is
machine-only; `zi2zx` rejects `--opt` on ZX input; the bootstrap is
regenerated. ASSEMBLY docs, the legacy-removal plan, the milestones and the
remaining-work plan record the current position.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-15 14:04:48 -03:00

1961 lines
69 KiB
C

/* Generated by zi2c from file_tree_linux.zi. */
#define ZIRAN_FILE_TREE_LINUX_H_SOURCE 1
#include <stdbool.h>
#include "zir_bounds.h"
#include "zir_string.h"
#include "zir_vec.h"
#define ZIRAN_FILE_TREE_LINUX_H_PRIVATE 1
#include "file_tree_linux.h"
#undef ZIRAN_FILE_TREE_LINUX_H_PRIVATE
#include "c_string.h"
#include "byte_text_linux.h"
#include "sort.h"
#ifndef ZIR_FOREIGN_zir_f_0ee210116da2a0c9_StatxRaw_bab3b2e7
#define ZIR_FOREIGN_zir_f_0ee210116da2a0c9_StatxRaw_bab3b2e7
#ifdef __APPLE__
int32_t zir_foreign_zir_f_0ee210116da2a0c9_StatxRaw(int32_t directory, uint8_t* path, int32_t flags, uint32_t mask, StatxRecord* output) __asm__("_statx");
#else
int32_t zir_foreign_zir_f_0ee210116da2a0c9_StatxRaw(int32_t directory, uint8_t* path, int32_t flags, uint32_t mask, StatxRecord* output) __asm__("statx");
#endif
static int32_t
zir_f_0ee210116da2a0c9_StatxRaw(int32_t directory, uint8_t* path, int32_t flags, uint32_t mask, StatxRecord* output)
{
return zir_foreign_zir_f_0ee210116da2a0c9_StatxRaw(directory, path, flags, mask, output);
}
#endif
#ifndef ZIR_FOREIGN_zir_f_0ee210116da2a0c9_MakeDirectoryRaw_9266eb24
#define ZIR_FOREIGN_zir_f_0ee210116da2a0c9_MakeDirectoryRaw_9266eb24
#ifdef __APPLE__
int32_t zir_foreign_zir_f_0ee210116da2a0c9_MakeDirectoryRaw(uint8_t* path, uint32_t mode) __asm__("_mkdir");
#else
int32_t zir_foreign_zir_f_0ee210116da2a0c9_MakeDirectoryRaw(uint8_t* path, uint32_t mode) __asm__("mkdir");
#endif
static int32_t
zir_f_0ee210116da2a0c9_MakeDirectoryRaw(uint8_t* path, uint32_t mode)
{
return zir_foreign_zir_f_0ee210116da2a0c9_MakeDirectoryRaw(path, mode);
}
#endif
#ifndef ZIR_FOREIGN_zir_f_0ee210116da2a0c9_RemoveDirectoryRaw_494daf1e
#define ZIR_FOREIGN_zir_f_0ee210116da2a0c9_RemoveDirectoryRaw_494daf1e
#ifdef __APPLE__
int32_t zir_foreign_zir_f_0ee210116da2a0c9_RemoveDirectoryRaw(uint8_t* path) __asm__("_rmdir");
#else
int32_t zir_foreign_zir_f_0ee210116da2a0c9_RemoveDirectoryRaw(uint8_t* path) __asm__("rmdir");
#endif
static int32_t
zir_f_0ee210116da2a0c9_RemoveDirectoryRaw(uint8_t* path)
{
return zir_foreign_zir_f_0ee210116da2a0c9_RemoveDirectoryRaw(path);
}
#endif
#ifndef ZIR_FOREIGN_zir_f_0ee210116da2a0c9_UnlinkRaw_1f92ad9b
#define ZIR_FOREIGN_zir_f_0ee210116da2a0c9_UnlinkRaw_1f92ad9b
#ifdef __APPLE__
int32_t zir_foreign_zir_f_0ee210116da2a0c9_UnlinkRaw(uint8_t* path) __asm__("_unlink");
#else
int32_t zir_foreign_zir_f_0ee210116da2a0c9_UnlinkRaw(uint8_t* path) __asm__("unlink");
#endif
static int32_t
zir_f_0ee210116da2a0c9_UnlinkRaw(uint8_t* path)
{
return zir_foreign_zir_f_0ee210116da2a0c9_UnlinkRaw(path);
}
#endif
#ifndef ZIR_FOREIGN_zir_f_0ee210116da2a0c9_ChangeModeRaw_5e6069f2
#define ZIR_FOREIGN_zir_f_0ee210116da2a0c9_ChangeModeRaw_5e6069f2
#ifdef __APPLE__
int32_t zir_foreign_zir_f_0ee210116da2a0c9_ChangeModeRaw(uint8_t* path, uint32_t mode) __asm__("_chmod");
#else
int32_t zir_foreign_zir_f_0ee210116da2a0c9_ChangeModeRaw(uint8_t* path, uint32_t mode) __asm__("chmod");
#endif
static int32_t
zir_f_0ee210116da2a0c9_ChangeModeRaw(uint8_t* path, uint32_t mode)
{
return zir_foreign_zir_f_0ee210116da2a0c9_ChangeModeRaw(path, mode);
}
#endif
#ifndef ZIR_FOREIGN_zir_f_0ee210116da2a0c9_ChangeFileModeRaw_d5737c88
#define ZIR_FOREIGN_zir_f_0ee210116da2a0c9_ChangeFileModeRaw_d5737c88
#ifdef __APPLE__
int32_t zir_foreign_zir_f_0ee210116da2a0c9_ChangeFileModeRaw(int32_t fd, uint32_t mode) __asm__("_fchmod");
#else
int32_t zir_foreign_zir_f_0ee210116da2a0c9_ChangeFileModeRaw(int32_t fd, uint32_t mode) __asm__("fchmod");
#endif
static int32_t
zir_f_0ee210116da2a0c9_ChangeFileModeRaw(int32_t fd, uint32_t mode)
{
return zir_foreign_zir_f_0ee210116da2a0c9_ChangeFileModeRaw(fd, mode);
}
#endif
#ifndef ZIR_FOREIGN_zir_f_0ee210116da2a0c9_OpenRaw_6d3762f4
#define ZIR_FOREIGN_zir_f_0ee210116da2a0c9_OpenRaw_6d3762f4
#ifdef __APPLE__
int32_t zir_foreign_zir_f_0ee210116da2a0c9_OpenRaw(uint8_t* path, int32_t flags, int32_t mode) __asm__("_open");
#else
int32_t zir_foreign_zir_f_0ee210116da2a0c9_OpenRaw(uint8_t* path, int32_t flags, int32_t mode) __asm__("open");
#endif
static int32_t
zir_f_0ee210116da2a0c9_OpenRaw(uint8_t* path, int32_t flags, int32_t mode)
{
return zir_foreign_zir_f_0ee210116da2a0c9_OpenRaw(path, flags, mode);
}
#endif
#ifndef ZIR_FOREIGN_zir_f_0ee210116da2a0c9_CloseRaw_d6ec0c1e
#define ZIR_FOREIGN_zir_f_0ee210116da2a0c9_CloseRaw_d6ec0c1e
#ifdef __APPLE__
int32_t zir_foreign_zir_f_0ee210116da2a0c9_CloseRaw(int32_t fd) __asm__("_close");
#else
int32_t zir_foreign_zir_f_0ee210116da2a0c9_CloseRaw(int32_t fd) __asm__("close");
#endif
static int32_t
zir_f_0ee210116da2a0c9_CloseRaw(int32_t fd)
{
return zir_foreign_zir_f_0ee210116da2a0c9_CloseRaw(fd);
}
#endif
#ifndef ZIR_FOREIGN_zir_f_0ee210116da2a0c9_FlockRaw_d74aed45
#define ZIR_FOREIGN_zir_f_0ee210116da2a0c9_FlockRaw_d74aed45
#ifdef __APPLE__
int32_t zir_foreign_zir_f_0ee210116da2a0c9_FlockRaw(int32_t fd, int32_t operation) __asm__("_flock");
#else
int32_t zir_foreign_zir_f_0ee210116da2a0c9_FlockRaw(int32_t fd, int32_t operation) __asm__("flock");
#endif
static int32_t
zir_f_0ee210116da2a0c9_FlockRaw(int32_t fd, int32_t operation)
{
return zir_foreign_zir_f_0ee210116da2a0c9_FlockRaw(fd, operation);
}
#endif
#ifndef ZIR_FOREIGN_zir_f_0ee210116da2a0c9_FsyncRaw_6212288d
#define ZIR_FOREIGN_zir_f_0ee210116da2a0c9_FsyncRaw_6212288d
#ifdef __APPLE__
int32_t zir_foreign_zir_f_0ee210116da2a0c9_FsyncRaw(int32_t fd) __asm__("_fsync");
#else
int32_t zir_foreign_zir_f_0ee210116da2a0c9_FsyncRaw(int32_t fd) __asm__("fsync");
#endif
static int32_t
zir_f_0ee210116da2a0c9_FsyncRaw(int32_t fd)
{
return zir_foreign_zir_f_0ee210116da2a0c9_FsyncRaw(fd);
}
#endif
#ifndef ZIR_FOREIGN_zir_f_0ee210116da2a0c9_WriteRaw_06f26813
#define ZIR_FOREIGN_zir_f_0ee210116da2a0c9_WriteRaw_06f26813
#ifdef __APPLE__
ptrdiff_t zir_foreign_zir_f_0ee210116da2a0c9_WriteRaw(int32_t fd, uint8_t* input, size_t count) __asm__("_write");
#else
ptrdiff_t zir_foreign_zir_f_0ee210116da2a0c9_WriteRaw(int32_t fd, uint8_t* input, size_t count) __asm__("write");
#endif
static ptrdiff_t
zir_f_0ee210116da2a0c9_WriteRaw(int32_t fd, uint8_t* input, size_t count)
{
return zir_foreign_zir_f_0ee210116da2a0c9_WriteRaw(fd, input, count);
}
#endif
#ifndef ZIR_FOREIGN_zir_f_0ee210116da2a0c9_RenameRaw_c18b8a8c
#define ZIR_FOREIGN_zir_f_0ee210116da2a0c9_RenameRaw_c18b8a8c
#ifdef __APPLE__
int32_t zir_foreign_zir_f_0ee210116da2a0c9_RenameRaw(uint8_t* source, uint8_t* target) __asm__("_rename");
#else
int32_t zir_foreign_zir_f_0ee210116da2a0c9_RenameRaw(uint8_t* source, uint8_t* target) __asm__("rename");
#endif
static int32_t
zir_f_0ee210116da2a0c9_RenameRaw(uint8_t* source, uint8_t* target)
{
return zir_foreign_zir_f_0ee210116da2a0c9_RenameRaw(source, target);
}
#endif
#ifndef ZIR_FOREIGN_zir_f_0ee210116da2a0c9_RealPathRaw_20916715
#define ZIR_FOREIGN_zir_f_0ee210116da2a0c9_RealPathRaw_20916715
#ifdef __APPLE__
uint8_t* zir_foreign_zir_f_0ee210116da2a0c9_RealPathRaw(uint8_t* path, uint8_t* output) __asm__("_realpath");
#else
uint8_t* zir_foreign_zir_f_0ee210116da2a0c9_RealPathRaw(uint8_t* path, uint8_t* output) __asm__("realpath");
#endif
static uint8_t*
zir_f_0ee210116da2a0c9_RealPathRaw(uint8_t* path, uint8_t* output)
{
return zir_foreign_zir_f_0ee210116da2a0c9_RealPathRaw(path, output);
}
#endif
#ifndef ZIR_FOREIGN_zir_f_0ee210116da2a0c9_TemporaryDirectoryRaw_bdb0155a
#define ZIR_FOREIGN_zir_f_0ee210116da2a0c9_TemporaryDirectoryRaw_bdb0155a
#ifdef __APPLE__
uint8_t* zir_foreign_zir_f_0ee210116da2a0c9_TemporaryDirectoryRaw(uint8_t* template) __asm__("_mkdtemp");
#else
uint8_t* zir_foreign_zir_f_0ee210116da2a0c9_TemporaryDirectoryRaw(uint8_t* template) __asm__("mkdtemp");
#endif
static uint8_t*
zir_f_0ee210116da2a0c9_TemporaryDirectoryRaw(uint8_t* template)
{
return zir_foreign_zir_f_0ee210116da2a0c9_TemporaryDirectoryRaw(template);
}
#endif
#ifndef ZIR_FOREIGN_zir_f_0ee210116da2a0c9_EffectiveUserRaw_f88904d0
#define ZIR_FOREIGN_zir_f_0ee210116da2a0c9_EffectiveUserRaw_f88904d0
#ifdef __APPLE__
uint32_t zir_foreign_zir_f_0ee210116da2a0c9_EffectiveUserRaw(void) __asm__("_geteuid");
#else
uint32_t zir_foreign_zir_f_0ee210116da2a0c9_EffectiveUserRaw(void) __asm__("geteuid");
#endif
static uint32_t
zir_f_0ee210116da2a0c9_EffectiveUserRaw(void)
{
return zir_foreign_zir_f_0ee210116da2a0c9_EffectiveUserRaw();
}
#endif
#ifndef ZIR_FOREIGN_zir_f_0ee210116da2a0c9_WorkingDirectoryRaw_a9b77685
#define ZIR_FOREIGN_zir_f_0ee210116da2a0c9_WorkingDirectoryRaw_a9b77685
#ifdef __APPLE__
uint8_t* zir_foreign_zir_f_0ee210116da2a0c9_WorkingDirectoryRaw(uint8_t* output, size_t size) __asm__("_getcwd");
#else
uint8_t* zir_foreign_zir_f_0ee210116da2a0c9_WorkingDirectoryRaw(uint8_t* output, size_t size) __asm__("getcwd");
#endif
static uint8_t*
zir_f_0ee210116da2a0c9_WorkingDirectoryRaw(uint8_t* output, size_t size)
{
return zir_foreign_zir_f_0ee210116da2a0c9_WorkingDirectoryRaw(output, size);
}
#endif
#ifndef ZIR_FOREIGN_zir_f_0ee210116da2a0c9_ProcessIdRaw_bd88b581
#define ZIR_FOREIGN_zir_f_0ee210116da2a0c9_ProcessIdRaw_bd88b581
#ifdef __APPLE__
int32_t zir_foreign_zir_f_0ee210116da2a0c9_ProcessIdRaw(void) __asm__("_getpid");
#else
int32_t zir_foreign_zir_f_0ee210116da2a0c9_ProcessIdRaw(void) __asm__("getpid");
#endif
static int32_t
zir_f_0ee210116da2a0c9_ProcessIdRaw(void)
{
return zir_foreign_zir_f_0ee210116da2a0c9_ProcessIdRaw();
}
#endif
#ifndef ZIR_FOREIGN_zir_f_0ee210116da2a0c9_ErrnoRaw_40ae2072
#define ZIR_FOREIGN_zir_f_0ee210116da2a0c9_ErrnoRaw_40ae2072
#ifdef __APPLE__
int32_t* zir_foreign_zir_f_0ee210116da2a0c9_ErrnoRaw(void) __asm__("___errno_location");
#else
int32_t* zir_foreign_zir_f_0ee210116da2a0c9_ErrnoRaw(void) __asm__("__errno_location");
#endif
static int32_t*
zir_f_0ee210116da2a0c9_ErrnoRaw(void)
{
return zir_foreign_zir_f_0ee210116da2a0c9_ErrnoRaw();
}
#endif
#ifndef ZIR_FOREIGN_zir_f_0ee210116da2a0c9_FreeOwnedRaw_c75930da
#define ZIR_FOREIGN_zir_f_0ee210116da2a0c9_FreeOwnedRaw_c75930da
#ifdef __APPLE__
void zir_foreign_zir_f_0ee210116da2a0c9_FreeOwnedRaw(void* memory) __asm__("_free");
#else
void zir_foreign_zir_f_0ee210116da2a0c9_FreeOwnedRaw(void* memory) __asm__("free");
#endif
static void
zir_f_0ee210116da2a0c9_FreeOwnedRaw(void* memory)
{
zir_foreign_zir_f_0ee210116da2a0c9_FreeOwnedRaw(memory);
}
#endif
#ifndef ZIR_FOREIGN_zir_f_0ee210116da2a0c9_SymbolicLinkRaw_c233d8d7
#define ZIR_FOREIGN_zir_f_0ee210116da2a0c9_SymbolicLinkRaw_c233d8d7
#ifdef __APPLE__
int32_t zir_foreign_zir_f_0ee210116da2a0c9_SymbolicLinkRaw(uint8_t* target, uint8_t* path) __asm__("_symlink");
#else
int32_t zir_foreign_zir_f_0ee210116da2a0c9_SymbolicLinkRaw(uint8_t* target, uint8_t* path) __asm__("symlink");
#endif
static int32_t
zir_f_0ee210116da2a0c9_SymbolicLinkRaw(uint8_t* target, uint8_t* path)
{
return zir_foreign_zir_f_0ee210116da2a0c9_SymbolicLinkRaw(target, path);
}
#endif
#ifndef ZIR_FOREIGN_zir_f_0ee210116da2a0c9_ReadLinkRaw_133b334a
#define ZIR_FOREIGN_zir_f_0ee210116da2a0c9_ReadLinkRaw_133b334a
#ifdef __APPLE__
ptrdiff_t zir_foreign_zir_f_0ee210116da2a0c9_ReadLinkRaw(uint8_t* path, uint8_t* output, size_t capacity) __asm__("_readlink");
#else
ptrdiff_t zir_foreign_zir_f_0ee210116da2a0c9_ReadLinkRaw(uint8_t* path, uint8_t* output, size_t capacity) __asm__("readlink");
#endif
static ptrdiff_t
zir_f_0ee210116da2a0c9_ReadLinkRaw(uint8_t* path, uint8_t* output, size_t capacity)
{
return zir_foreign_zir_f_0ee210116da2a0c9_ReadLinkRaw(path, output, capacity);
}
#endif
#ifndef ZIRAN_MAYBE_UNUSED
#if defined(__GNUC__) || defined(__clang__)
#define ZIRAN_MAYBE_UNUSED __attribute__((unused))
#else
#define ZIRAN_MAYBE_UNUSED
#endif
#endif
static void file_tree_linux_ReleaseOwnedText(String text) ZIRAN_MAYBE_UNUSED;
static Text file_tree_linux_CopyText(Slice bytes) ZIRAN_MAYBE_UNUSED;
static void file_tree_linux_ReleaseNames(__type_c53c2d7c4913dc02* names, int64_t from) ZIRAN_MAYBE_UNUSED;
static bool file_tree_linux_CopyTreeNewContents(String source, String target, int32_t mode) ZIRAN_MAYBE_UNUSED;
static bool file_tree_linux_CopyLink(String source, String target) ZIRAN_MAYBE_UNUSED;
static bool file_tree_linux_MatchAt(String name, int64_t at, String pattern, int64_t from) ZIRAN_MAYBE_UNUSED;
PathQueryResult
file_tree_linux_PathQuery(String path, bool follow_links)
{
PathQueryResult result = {0};
uint8_t name[4096] = {0};
bool value_0 = (int64_t)(path).length > 4095LL;
if (!value_0) {
Slice value_1 = {name, 4096};
bool value_2 = CopyCString(path, value_1);
value_0 = !value_2;
}
if (value_0) {
int32_t* value_3 = &(result.error);
(*value_3) = 22;
return result;
}
StatxRecord record = {0};
int32_t value_4 = 0;
if (follow_links) {
value_4 = 0;
} else {
value_4 = 256;
}
int32_t flags = value_4;
if (zir_f_0ee210116da2a0c9_StatxRaw(-100, &(name[0]), flags, 2047, &(record)) != 0) {
int32_t* value_5 = &(result.error);
int32_t* value_6 = zir_f_0ee210116da2a0c9_ErrnoRaw();
(*value_5) = *(value_6);
bool* value_7 = &(result.missing);
(*value_7) = result.error == 2 || result.error == 20;
return result;
}
PathInfo info = {0};
uint32_t* value_8 = &(info.metadata_mask);
(*value_8) = (uint32_t)((record.words)[0]);
int32_t mode = (int32_t)(((uint64_t)((uint64_t)(((record.words)[3] >> 32)) & UINT64_C(65535))));
int32_t type = (int32_t)((uint32_t)mode & 61440u);
PathKind* value_9 = &(info.kind);
(*value_9) = PathKind_Other;
if (type == 32768) {
PathKind* value_10 = &(info.kind);
(*value_10) = PathKind_File;
}
if (type == 16384) {
PathKind* value_11 = &(info.kind);
(*value_11) = PathKind_Directory;
}
if (type == 40960) {
PathKind* value_12 = &(info.kind);
(*value_12) = PathKind_Link;
}
uint32_t* value_13 = &(info.owner_uid);
(*value_13) = (uint32_t)(((record.words)[2] >> 32));
int32_t* value_14 = &(info.mode);
(*value_14) = (int32_t)((uint32_t)mode & 4095u);
if (((uint32_t)((uint32_t)(info.metadata_mask) & 512u)) != 0) {
int64_t* value_15 = &(info.size);
(*value_15) = (int64_t)((record.words)[5]);
}
if (((uint32_t)((uint32_t)(info.metadata_mask) & 256u)) != 0) {
uint64_t* value_16 = &(info.inode);
(*value_16) = (record.words)[4];
}
uint32_t modified_fraction = (uint32_t)((record.words)[15]);
uint32_t changed_fraction = (uint32_t)((record.words)[13]);
if (((uint32_t)((uint32_t)(info.metadata_mask) & 64u)) != 0 && modified_fraction < 1000000000) {
int64_t* value_17 = &(info.modified);
(*value_17) = (int64_t)((record.words)[14]);
uint32_t* value_18 = &(info.modified_nanoseconds);
(*value_18) = modified_fraction;
}
if (((uint32_t)((uint32_t)(info.metadata_mask) & 128u)) != 0 && changed_fraction < 1000000000) {
int64_t* value_19 = &(info.changed);
(*value_19) = (int64_t)((record.words)[12]);
uint32_t* value_20 = &(info.changed_nanoseconds);
(*value_20) = changed_fraction;
}
uint32_t* value_21 = &(info.device_major);
(*value_21) = (uint32_t)((record.words)[17]);
uint32_t* value_22 = &(info.device_minor);
(*value_22) = (uint32_t)(((record.words)[17] >> 32));
bool* value_23 = &(info.identity_valid);
bool value_24 = ((uint32_t)((uint32_t)(info.metadata_mask) & 961u)) == 961 && info.size >= 0LL && modified_fraction < 1000000000;
(*value_23) = value_24 && changed_fraction < 1000000000;
PathInfo* value_25 = &(result.info);
(*value_25) = info;
bool* value_26 = &(result.valid);
(*value_26) = true;
return result;
}
PathInfo
file_tree_linux_PathStatus(String path, bool follow_links)
{
PathQueryResult value_0 = file_tree_linux_PathQuery(path, follow_links);
return value_0.info;
}
PathInfo
file_tree_linux_FileSnapshot(String path)
{
String value_0 = path;
PathInfo value_1 = {0};
{
String path = value_0;
bool follow_links = false;
PathQueryResult value_3 = file_tree_linux_PathQuery(path, follow_links);
value_1 = value_3.info;
}
PathInfo result = value_1;
if (((PathKind)(result.kind)) != PathKind_File) {
bool* value_4 = &(result.identity_valid);
(*value_4) = false;
}
return result;
}
bool
file_tree_linux_SameFileSnapshot(PathInfo left, PathInfo right)
{
bool value_0 = left.identity_valid && right.identity_valid && ((PathKind)(left.kind)) == PathKind_File && ((PathKind)(right.kind)) == PathKind_File;
bool value_1 = value_0 && left.device_major == right.device_major && left.device_minor == right.device_minor && left.inode == right.inode;
bool value_2 = value_1 && left.size == right.size && left.modified == right.modified && left.modified_nanoseconds == right.modified_nanoseconds;
return value_2 && left.changed == right.changed && left.changed_nanoseconds == right.changed_nanoseconds;
}
bool
file_tree_linux_IsFile(String path)
{
String value_0 = path;
PathInfo value_1 = {0};
{
String path = value_0;
bool follow_links = true;
PathQueryResult value_3 = file_tree_linux_PathQuery(path, follow_links);
value_1 = value_3.info;
}
return ((PathKind)(value_1.kind)) == PathKind_File;
}
bool
file_tree_linux_IsDirectory(String path)
{
String value_0 = path;
PathInfo value_1 = {0};
{
String path = value_0;
bool follow_links = true;
PathQueryResult value_3 = file_tree_linux_PathQuery(path, follow_links);
value_1 = value_3.info;
}
return ((PathKind)(value_1.kind)) == PathKind_Directory;
}
bool
file_tree_linux_SetPathMode(String path, int32_t mode)
{
uint8_t name[4096] = {0};
Slice value_0 = {name, 4096};
bool value_1 = CopyCString(path, value_0);
if (!value_1) {
return false;
}
return zir_f_0ee210116da2a0c9_ChangeModeRaw(&(name[0]), (uint32_t)(mode)) == 0;
}
Text
file_tree_linux_JoinPath(String directory, String name)
{
if ((int64_t)(directory).length == 0LL || ((int64_t)(name).length > 0LL && (uint8_t)ZIRAN_INDEX(name.data, name.length, 0) == 47)) {
return vec_TextCopy(name);
}
__type_838711075b90279a result = {0};
__type_838711075b90279a* value_0 = &result;
bool value_1 = false;
if ((directory).length == 0) {
value_1 = true;
} else {
void *value_2 = ZirVecReserve((void *)(result).data, &(result).capacity, (result).count, (int64_t)(directory).length, sizeof(*(result).data));
if (value_2 != NULL) {
(result).data = (uint8_t *)value_2;
memcpy((result).data + (result).count, (directory).data, (size_t)(directory).length);
(result).count += (int64_t)(directory).length;
value_1 = true;
}
}
(void)value_1;
if ((uint8_t)directory.data[((int64_t)((uint64_t)((int64_t)(directory).length) - UINT64_C(1)))] != 47) {
uint8_t value_3 = 47;
bool value_4 = false;
void *value_5 = ZirVecGrow((void *)(result).data, &(result).capacity, (result).count, sizeof(*(result).data));
if (value_5 != NULL) {
(result).data = (uint8_t *)value_5;
(result).data[(result).count++] = value_3;
value_4 = true;
}
(void)value_4;
}
bool value_6 = false;
if ((name).length == 0) {
value_6 = true;
} else {
void *value_7 = ZirVecReserve((void *)(result).data, &(result).capacity, (result).count, (int64_t)(name).length, sizeof(*(result).data));
if (value_7 != NULL) {
(result).data = (uint8_t *)value_7;
memcpy((result).data + (result).count, (name).data, (size_t)(name).length);
(result).count += (int64_t)(name).length;
value_6 = true;
}
}
(void)value_6;
__type_838711075b90279a value_8 = result;
result.data = NULL;
result.capacity = 0;
result.count = 0;
Text value_9 = vec_TextFromBuilder(value_8);
free((*value_0).data);
(*value_0).data = NULL;
(*value_0).capacity = 0;
(*value_0).count = 0;
return value_9;
}
String
file_tree_linux_PathName(String path)
{
int64_t end = (int64_t)(path).length;
while (end > 1LL && (uint8_t)ZIRAN_INDEX(path.data, path.length, ((int64_t)((uint64_t)end - UINT64_C(1)))) == 47) {
end = (int64_t)((uint64_t)end - UINT64_C(1));
}
int64_t at = end;
while (at > 0LL && (uint8_t)ZIRAN_INDEX(path.data, path.length, ((int64_t)((uint64_t)at - UINT64_C(1)))) != 47) {
at = (int64_t)((uint64_t)at - UINT64_C(1));
}
return StringRange(path, (int64_t)at, (int64_t)end);
}
String
file_tree_linux_PathParent(String path)
{
int64_t end = (int64_t)(path).length;
while (end > 1LL && (uint8_t)ZIRAN_INDEX(path.data, path.length, ((int64_t)((uint64_t)end - UINT64_C(1)))) == 47) {
end = (int64_t)((uint64_t)end - UINT64_C(1));
}
int64_t at = end;
while (at > 0LL && (uint8_t)ZIRAN_INDEX(path.data, path.length, ((int64_t)((uint64_t)at - UINT64_C(1)))) != 47) {
at = (int64_t)((uint64_t)at - UINT64_C(1));
}
if (at == 0LL) {
return StringLiteral(".");
}
while (at > 1LL && (uint8_t)ZIRAN_INDEX(path.data, path.length, ((int64_t)((uint64_t)at - UINT64_C(1)))) == 47) {
at = (int64_t)((uint64_t)at - UINT64_C(1));
}
return StringRange(path, (int64_t)0LL, (int64_t)at);
}
String
file_tree_linux_PathSuffix(String path)
{
String name = file_tree_linux_PathName(path);
int64_t at = (int64_t)((uint64_t)((int64_t)(name).length) - UINT64_C(1));
while (at > 0LL) {
if ((uint8_t)ZIRAN_INDEX(name.data, name.length, at) == 46) {
return StringRange(name, (int64_t)at, (int64_t)(int64_t)(name).length);
}
at = (int64_t)((uint64_t)at - UINT64_C(1));
}
return StringLiteral("");
}
String
file_tree_linux_PathStem(String path)
{
String name = file_tree_linux_PathName(path);
String suffix = file_tree_linux_PathSuffix(path);
return StringRange(name, (int64_t)0LL, (int64_t)((int64_t)((uint64_t)((int64_t)(name).length) - (uint64_t)((int64_t)(suffix).length))));
}
Text
file_tree_linux_ResolvePath(String path)
{
uint8_t name[4096] = {0};
uint8_t output[4096] = {0};
Slice value_0 = {name, 4096};
bool value_1 = CopyCString(path, value_0);
if (!value_1) {
return (Text){0};
}
uint8_t* value_2 = zir_f_0ee210116da2a0c9_RealPathRaw(&(name[0]), &(output[0]));
if (value_2 == ((void *)0)) {
return (Text){0};
}
Slice value_3 = {output, 4096};
return file_tree_linux_CopyText(value_3);
}
Text
file_tree_linux_CreateTemporaryDirectory(String parent, String prefix)
{
for (int64_t it_index = 0LL; it_index < (int64_t)(prefix).length; it_index++) {
uint8_t it = (uint8_t)prefix.data[it_index];
if (it == 47 || it == 0) {
return (Text){0};
}
}
String directory = vec_TextDetach(file_tree_linux_ResolvePath(parent));
if ((int64_t)(directory).length == 0LL) {
return (Text){0};
}
PathInfo value_0 = {0};
{
String path = directory;
bool follow_links = false;
PathQueryResult value_2 = file_tree_linux_PathQuery(path, follow_links);
value_0 = value_2.info;
}
PathInfo info = value_0;
if (((PathKind)(info.kind)) != PathKind_Directory || info.owner_uid != zir_f_0ee210116da2a0c9_EffectiveUserRaw()) {
Text cleanup_return_0 = {0};
{
file_tree_linux_ReleaseOwnedText(directory);
}
Text value_3 = cleanup_return_0;
(cleanup_return_0).bytes.data = NULL;
(cleanup_return_0).bytes.capacity = 0;
(cleanup_return_0).bytes.count = 0;
Text value_4 = value_3;
free((cleanup_return_0).bytes.data);
(cleanup_return_0).bytes.data = NULL;
(cleanup_return_0).bytes.capacity = 0;
(cleanup_return_0).bytes.count = 0;
return value_4;
}
__type_838711075b90279a output = {0};
__type_838711075b90279a* value_5 = &output;
bool value_6 = false;
if ((directory).length == 0) {
value_6 = true;
} else {
void *value_7 = ZirVecReserve((void *)(output).data, &(output).capacity, (output).count, (int64_t)(directory).length, sizeof(*(output).data));
if (value_7 != NULL) {
(output).data = (uint8_t *)value_7;
memcpy((output).data + (output).count, (directory).data, (size_t)(directory).length);
(output).count += (int64_t)(directory).length;
value_6 = true;
}
}
bool value_8 = !value_6;
if (!value_8) {
bool value_9 = (uint8_t)directory.data[((int64_t)((uint64_t)((int64_t)(directory).length) - UINT64_C(1)))] != 47;
bool value_10 = value_9;
bool value_11 = value_10;
if (value_11) {
uint8_t value_12 = 47;
bool value_13 = false;
void *value_14 = ZirVecGrow((void *)(output).data, &(output).capacity, (output).count, sizeof(*(output).data));
if (value_14 != NULL) {
(output).data = (uint8_t *)value_14;
(output).data[(output).count++] = value_12;
value_13 = true;
}
value_11 = !value_13;
}
value_8 = value_11;
}
bool value_15 = value_8;
bool value_16 = value_15;
if (!value_16) {
bool value_17 = false;
if ((prefix).length == 0) {
value_17 = true;
} else {
void *value_18 = ZirVecReserve((void *)(output).data, &(output).capacity, (output).count, (int64_t)(prefix).length, sizeof(*(output).data));
if (value_18 != NULL) {
(output).data = (uint8_t *)value_18;
memcpy((output).data + (output).count, (prefix).data, (size_t)(prefix).length);
(output).count += (int64_t)(prefix).length;
value_17 = true;
}
}
value_16 = !value_17;
}
bool value_19 = value_16;
bool value_20 = value_19;
if (!value_20) {
bool value_21 = false;
if ((StringLiteral("XXXXXX")).length == 0) {
value_21 = true;
} else {
void *value_22 = ZirVecReserve((void *)(output).data, &(output).capacity, (output).count, (int64_t)(StringLiteral("XXXXXX")).length, sizeof(*(output).data));
if (value_22 != NULL) {
(output).data = (uint8_t *)value_22;
memcpy((output).data + (output).count, (StringLiteral("XXXXXX")).data, (size_t)(StringLiteral("XXXXXX")).length);
(output).count += (int64_t)(StringLiteral("XXXXXX")).length;
value_21 = true;
}
}
value_20 = !value_21;
}
if (value_20) {
Text cleanup_return_1 = {0};
{
file_tree_linux_ReleaseOwnedText(directory);
}
Text value_23 = cleanup_return_1;
(cleanup_return_1).bytes.data = NULL;
(cleanup_return_1).bytes.capacity = 0;
(cleanup_return_1).bytes.count = 0;
Text value_24 = value_23;
free((cleanup_return_1).bytes.data);
(cleanup_return_1).bytes.data = NULL;
(cleanup_return_1).bytes.capacity = 0;
(cleanup_return_1).bytes.count = 0;
free((*value_5).data);
(*value_5).data = NULL;
(*value_5).capacity = 0;
(*value_5).count = 0;
return value_24;
}
uint8_t name[4096] = {0};
Slice value_25 = {(output).data, (output).count};
Slice value_26 = SliceRange(value_25, 0LL, output.count, sizeof(*(output).data));
Slice value_27 = {name, 4096};
bool value_28 = CopyCString(StringView((const char *)(value_26).data, (size_t)(value_26).length), value_27);
if (!value_28) {
Text cleanup_return_2 = {0};
{
file_tree_linux_ReleaseOwnedText(directory);
}
Text value_29 = cleanup_return_2;
(cleanup_return_2).bytes.data = NULL;
(cleanup_return_2).bytes.capacity = 0;
(cleanup_return_2).bytes.count = 0;
Text value_30 = value_29;
free((cleanup_return_2).bytes.data);
(cleanup_return_2).bytes.data = NULL;
(cleanup_return_2).bytes.capacity = 0;
(cleanup_return_2).bytes.count = 0;
free((*value_5).data);
(*value_5).data = NULL;
(*value_5).capacity = 0;
(*value_5).count = 0;
return value_30;
}
uint8_t* value_31 = zir_f_0ee210116da2a0c9_TemporaryDirectoryRaw(&(name[0]));
if (value_31 == ((void *)0)) {
Text cleanup_return_3 = {0};
{
file_tree_linux_ReleaseOwnedText(directory);
}
Text value_32 = cleanup_return_3;
(cleanup_return_3).bytes.data = NULL;
(cleanup_return_3).bytes.capacity = 0;
(cleanup_return_3).bytes.count = 0;
Text value_33 = value_32;
free((cleanup_return_3).bytes.data);
(cleanup_return_3).bytes.data = NULL;
(cleanup_return_3).bytes.capacity = 0;
(cleanup_return_3).bytes.count = 0;
free((*value_5).data);
(*value_5).data = NULL;
(*value_5).capacity = 0;
(*value_5).count = 0;
return value_33;
}
{
int64_t range_first_40 = 0LL;
int64_t range_last_40 = (int64_t)((uint64_t)(output.count) - UINT64_C(1));
int64_t at = range_first_40;
if (at <= range_last_40) for (;;) {
uint8_t* value_34 = &(ZIRAN_VEC_INDEX((output).data, (output).count, at));
(*value_34) = ZIRAN_INDEX(name, 4096, at);
if (at == range_last_40) {
break;
}
at = (int64_t)((uint64_t)at + UINT64_C(1));
}
}
__type_838711075b90279a value_35 = output;
output.data = NULL;
output.capacity = 0;
output.count = 0;
Text cleanup_return_4 = vec_TextFromBuilder(value_35);
{
file_tree_linux_ReleaseOwnedText(directory);
}
Text value_36 = cleanup_return_4;
(cleanup_return_4).bytes.data = NULL;
(cleanup_return_4).bytes.capacity = 0;
(cleanup_return_4).bytes.count = 0;
Text value_37 = value_36;
free((cleanup_return_4).bytes.data);
(cleanup_return_4).bytes.data = NULL;
(cleanup_return_4).bytes.capacity = 0;
(cleanup_return_4).bytes.count = 0;
free((*value_5).data);
(*value_5).data = NULL;
(*value_5).capacity = 0;
(*value_5).count = 0;
return value_37;
}
static void
file_tree_linux_ReleaseOwnedText(String text)
{
if ((int64_t)(text).length == 0LL) {
return;
}
TextDescriptor* descriptor = (TextDescriptor*)(&(text));
zir_f_0ee210116da2a0c9_FreeOwnedRaw((void*)(descriptor->data));
}
Text
file_tree_linux_WorkingDirectory(void)
{
uint8_t output[4096] = {0};
uint8_t* value_0 = zir_f_0ee210116da2a0c9_WorkingDirectoryRaw(&(output[0]), 4096);
if (value_0 == ((void *)0)) {
return (Text){0};
}
Slice value_1 = {output, 4096};
return file_tree_linux_CopyText(value_1);
}
static Text
file_tree_linux_CopyText(Slice bytes)
{
__type_838711075b90279a result = {0};
__type_838711075b90279a* value_0 = &result;
for (int64_t it_index = 0LL; it_index < (int64_t)(bytes).length; it_index++) {
uint8_t it = ((uint8_t *)bytes.data)[it_index];
if (it == 0) {
break;
}
uint8_t value_1 = it;
bool value_2 = false;
void *value_3 = ZirVecGrow((void *)(result).data, &(result).capacity, (result).count, sizeof(*(result).data));
if (value_3 != NULL) {
(result).data = (uint8_t *)value_3;
(result).data[(result).count++] = value_1;
value_2 = true;
}
if (!value_2) {
Text value_4 = (Text){0};
free((*value_0).data);
(*value_0).data = NULL;
(*value_0).capacity = 0;
(*value_0).count = 0;
return value_4;
}
}
__type_838711075b90279a value_5 = result;
result.data = NULL;
result.capacity = 0;
result.count = 0;
Text value_6 = vec_TextFromBuilder(value_5);
free((*value_0).data);
(*value_0).data = NULL;
(*value_0).capacity = 0;
(*value_0).count = 0;
return value_6;
}
bool
file_tree_linux_CreateDirectories(String path, int32_t mode)
{
if ((int64_t)(path).length == 0LL) {
return false;
}
String value_0 = path;
bool value_1 = false;
{
String path = value_0;
String value_3 = path;
PathInfo value_4 = {0};
{
String path = value_3;
bool follow_links = true;
PathQueryResult value_6 = file_tree_linux_PathQuery(path, follow_links);
value_4 = value_6.info;
}
value_1 = ((PathKind)(value_4.kind)) == PathKind_Directory;
}
if (value_1) {
return true;
}
String parent = file_tree_linux_PathParent(path);
bool value_7 = !StringEqual(parent, path) && !StringEqual(parent, StringLiteral(".")) && !StringEqual(parent, StringLiteral("/"));
bool value_8 = value_7;
if (value_8) {
bool value_9 = file_tree_linux_CreateDirectories(parent, mode);
value_8 = !value_9;
}
if (value_8) {
return false;
}
uint8_t name[4096] = {0};
Slice value_10 = {name, 4096};
bool value_11 = CopyCString(path, value_10);
if (!value_11) {
return false;
}
bool value_12 = zir_f_0ee210116da2a0c9_MakeDirectoryRaw(&(name[0]), (uint32_t)(mode)) != 0;
bool value_13 = value_12;
if (value_13) {
int32_t* value_14 = zir_f_0ee210116da2a0c9_ErrnoRaw();
value_13 = (*(value_14) != 17);
}
if (value_13) {
return false;
}
String value_15 = path;
bool value_16 = false;
{
String path = value_15;
String value_18 = path;
PathInfo value_19 = {0};
{
String path = value_18;
bool follow_links = true;
PathQueryResult value_21 = file_tree_linux_PathQuery(path, follow_links);
value_19 = value_21.info;
}
value_16 = ((PathKind)(value_19.kind)) == PathKind_Directory;
}
return value_16;
}
static void
file_tree_linux_ReleaseNames(__type_c53c2d7c4913dc02* names, int64_t from)
{
__type_c53c2d7c4913dc02 owned = {0};
__type_c53c2d7c4913dc02* value_0 = &owned;
__type_c53c2d7c4913dc02 value_1 = owned;
owned = (*(names));
(*(names)) = value_1;
int64_t index = from;
while (index < owned.count) {
file_tree_linux_ReleaseOwnedText(ZIRAN_VEC_INDEX((owned).data, (owned).count, index));
index = (int64_t)((uint64_t)index + UINT64_C(1));
}
__type_c53c2d7c4913dc02 value_2 = owned;
owned = (*(names));
(*(names)) = value_2;
free((*value_0).data);
(*value_0).data = NULL;
(*value_0).capacity = 0;
(*value_0).count = 0;
}
void
file_tree_linux_FreeDirectoryNames(__type_c53c2d7c4913dc02* names)
{
if (names == ((void *)0)) {
return;
}
file_tree_linux_ReleaseNames(names, 0LL);
__type_c53c2d7c4913dc02 owned = {0};
__type_c53c2d7c4913dc02* value_0 = &owned;
__type_c53c2d7c4913dc02 value_1 = owned;
owned = (*(names));
(*(names)) = value_1;
free((owned).data);
(owned).data = NULL;
owned.capacity = 0;
owned.count = 0;
free((*value_0).data);
(*value_0).data = NULL;
(*value_0).capacity = 0;
(*value_0).count = 0;
}
bool
file_tree_linux_ListDirectory(String path, __type_c53c2d7c4913dc02* names)
{
if (names == ((void *)0)) {
return false;
}
DirectoryCursor cursor = OpenDirectory(path);
if (!cursor.valid) {
return false;
}
bool open = true;
__type_c53c2d7c4913dc02 found = {0};
__type_c53c2d7c4913dc02* value_0 = &found;
int64_t transferred = 0;
while (true) {
DirectoryEntry entry = NextDirectoryEntry(&(cursor));
if (!entry.valid) {
break;
}
Slice value_1 = {entry.name, 256};
String view = TextFromBytes(SliceRange(value_1, (int64_t)0LL, (int64_t)entry.name_count, sizeof(uint8_t)));
if (StringEqual(view, StringLiteral(".")) || StringEqual(view, StringLiteral(".."))) {
continue;
}
Slice value_2 = {entry.name, 256};
String text = vec_TextDetach(file_tree_linux_CopyText(SliceRange(value_2, (int64_t)0LL, (int64_t)entry.name_count, sizeof(uint8_t))));
if ((int64_t)(text).length == 0LL) {
bool cleanup_return_0 = false;
{
file_tree_linux_ReleaseNames(&(found), transferred);
}
{
if (open) {
CloseDirectory(&(cursor));
}
}
bool value_3 = cleanup_return_0;
free((*value_0).data);
(*value_0).data = NULL;
(*value_0).capacity = 0;
(*value_0).count = 0;
return value_3;
}
String value_4 = text;
bool value_5 = false;
void *value_6 = ZirVecGrow((void *)(found).data, &(found).capacity, (found).count, sizeof(*(found).data));
if (value_6 != NULL) {
(found).data = (String *)value_6;
(found).data[(found).count++] = value_4;
value_5 = true;
}
if (!value_5) {
file_tree_linux_ReleaseOwnedText(text);
bool cleanup_return_1 = false;
{
file_tree_linux_ReleaseNames(&(found), transferred);
}
{
if (open) {
CloseDirectory(&(cursor));
}
}
bool value_7 = cleanup_return_1;
free((*value_0).data);
(*value_0).data = NULL;
(*value_0).capacity = 0;
(*value_0).count = 0;
return value_7;
}
}
bool failed = cursor.failed;
bool closed = CloseDirectory(&(cursor));
open = false;
if (failed || !closed) {
bool cleanup_return_2 = false;
{
file_tree_linux_ReleaseNames(&(found), transferred);
}
{
if (open) {
CloseDirectory(&(cursor));
}
}
bool value_8 = cleanup_return_2;
free((*value_0).data);
(*value_0).data = NULL;
(*value_0).capacity = 0;
(*value_0).count = 0;
return value_8;
}
Slice value_9 = {(found).data, (found).count};
sort_SortText(SliceRange(value_9, 0LL, found.count, sizeof(*(found).data)));
int64_t value_10 = names->count;
int64_t value_11 = (int64_t)(UINT64_C(9223372036854775807) - (uint64_t)(found.count));
bool value_12 = value_10 > value_11;
bool value_13 = value_12;
if (!value_13) {
int64_t value_15 = (int64_t)((uint64_t)(names->count) + (uint64_t)(found.count));
bool value_16 = false;
if (value_15 >= 0) {
value_16 = value_15 <= ((*(names))).capacity;
if (!value_16) {
void *value_17 = ZirVecReserve((void *)((*(names))).data, &((*(names))).capacity, ((*(names))).count, value_15 - ((*(names))).count, sizeof(*((*(names))).data));
if (value_17 != NULL) { ((*(names))).data = (String *)value_17; value_16 = true; }
}
}
value_13 = !value_16;
}
if (value_13) {
bool cleanup_return_3 = false;
{
file_tree_linux_ReleaseNames(&(found), transferred);
}
{
if (open) {
CloseDirectory(&(cursor));
}
}
bool value_19 = cleanup_return_3;
free((*value_0).data);
(*value_0).data = NULL;
(*value_0).capacity = 0;
(*value_0).count = 0;
return value_19;
}
while (transferred < found.count) {
String value_20 = ZIRAN_VEC_INDEX((found).data, (found).count, transferred);
bool value_21 = false;
void *value_22 = ZirVecGrow((void *)((*(names))).data, &((*(names))).capacity, ((*(names))).count, sizeof(*((*(names))).data));
if (value_22 != NULL) {
((*(names))).data = (String *)value_22;
((*(names))).data[((*(names))).count++] = value_20;
value_21 = true;
}
if (!value_21) {
bool cleanup_return_4 = false;
{
file_tree_linux_ReleaseNames(&(found), transferred);
}
{
if (open) {
CloseDirectory(&(cursor));
}
}
bool value_23 = cleanup_return_4;
free((*value_0).data);
(*value_0).data = NULL;
(*value_0).capacity = 0;
(*value_0).count = 0;
return value_23;
}
transferred = (int64_t)((uint64_t)transferred + UINT64_C(1));
}
bool cleanup_return_5 = true;
{
file_tree_linux_ReleaseNames(&(found), transferred);
}
{
if (open) {
CloseDirectory(&(cursor));
}
}
bool value_24 = cleanup_return_5;
free((*value_0).data);
(*value_0).data = NULL;
(*value_0).capacity = 0;
(*value_0).count = 0;
return value_24;
}
bool
file_tree_linux_RemoveTree(String path)
{
String value_0 = path;
PathInfo value_1 = {0};
{
String path = value_0;
bool follow_links = false;
PathQueryResult value_3 = file_tree_linux_PathQuery(path, follow_links);
value_1 = value_3.info;
}
PathInfo info = value_1;
if (((PathKind)(info.kind)) == PathKind_Missing) {
return true;
}
uint8_t name[4096] = {0};
Slice value_4 = {name, 4096};
bool value_5 = CopyCString(path, value_4);
if (!value_5) {
return false;
}
if (((PathKind)(info.kind)) != PathKind_Directory) {
return zir_f_0ee210116da2a0c9_UnlinkRaw(&(name[0])) == 0;
}
__type_c53c2d7c4913dc02 names = {0};
__type_c53c2d7c4913dc02* value_6 = &names;
bool value_7 = file_tree_linux_ListDirectory(path, &(names));
if (!value_7) {
bool value_8 = false;
free((*value_6).data);
(*value_6).data = NULL;
(*value_6).capacity = 0;
(*value_6).count = 0;
return value_8;
}
bool ok = true;
int64_t index = 0LL;
while (index < names.count) {
__type_838711075b90279a child = {0};
__type_838711075b90279a* value_9 = &child;
bool value_10 = false;
if ((path).length == 0) {
value_10 = true;
} else {
void *value_11 = ZirVecReserve((void *)(child).data, &(child).capacity, (child).count, (int64_t)(path).length, sizeof(*(child).data));
if (value_11 != NULL) {
(child).data = (uint8_t *)value_11;
memcpy((child).data + (child).count, (path).data, (size_t)(path).length);
(child).count += (int64_t)(path).length;
value_10 = true;
}
}
bool value_12 = !value_10;
if (!value_12) {
bool value_13 = (uint8_t)ZIRAN_INDEX(path.data, path.length, ((int64_t)((uint64_t)((int64_t)(path).length) - UINT64_C(1)))) != 47;
bool value_14 = value_13;
bool value_15 = value_14;
if (value_15) {
uint8_t value_16 = 47;
bool value_17 = false;
void *value_18 = ZirVecGrow((void *)(child).data, &(child).capacity, (child).count, sizeof(*(child).data));
if (value_18 != NULL) {
(child).data = (uint8_t *)value_18;
(child).data[(child).count++] = value_16;
value_17 = true;
}
value_15 = !value_17;
}
value_12 = value_15;
}
bool value_19 = value_12;
bool value_20 = value_19;
if (!value_20) {
bool value_21 = false;
if ((ZIRAN_VEC_INDEX((names).data, (names).count, index)).length == 0) {
value_21 = true;
} else {
void *value_22 = ZirVecReserve((void *)(child).data, &(child).capacity, (child).count, (int64_t)(ZIRAN_VEC_INDEX((names).data, (names).count, index)).length, sizeof(*(child).data));
if (value_22 != NULL) {
(child).data = (uint8_t *)value_22;
memcpy((child).data + (child).count, (ZIRAN_VEC_INDEX((names).data, (names).count, index)).data, (size_t)(ZIRAN_VEC_INDEX((names).data, (names).count, index)).length);
(child).count += (int64_t)(ZIRAN_VEC_INDEX((names).data, (names).count, index)).length;
value_21 = true;
}
}
value_20 = !value_21;
}
if (value_20) {
bool cleanup_return_0 = false;
{
file_tree_linux_ReleaseNames(&(names), 0LL);
}
bool value_23 = cleanup_return_0;
free((*value_9).data);
(*value_9).data = NULL;
(*value_9).capacity = 0;
(*value_9).count = 0;
free((*value_6).data);
(*value_6).data = NULL;
(*value_6).capacity = 0;
(*value_6).count = 0;
return value_23;
}
Slice value_24 = {(child).data, (child).count};
Slice value_25 = SliceRange(value_24, 0LL, child.count, sizeof(*(child).data));
bool value_26 = file_tree_linux_RemoveTree(StringView((const char *)(value_25).data, (size_t)(value_25).length));
if (!value_26) {
ok = false;
}
index = (int64_t)((uint64_t)index + UINT64_C(1));
free((*value_9).data);
(*value_9).data = NULL;
(*value_9).capacity = 0;
(*value_9).count = 0;
}
bool cleanup_return_1 = ok && zir_f_0ee210116da2a0c9_RemoveDirectoryRaw(&(name[0])) == 0;
{
file_tree_linux_ReleaseNames(&(names), 0LL);
}
bool value_27 = cleanup_return_1;
free((*value_6).data);
(*value_6).data = NULL;
(*value_6).capacity = 0;
(*value_6).count = 0;
return value_27;
}
bool
file_tree_linux_CopyFile(String source, String target)
{
PathInfo value_0 = {0};
{
String path = source;
bool follow_links = true;
PathQueryResult value_2 = file_tree_linux_PathQuery(path, follow_links);
value_0 = value_2.info;
}
PathInfo info = value_0;
if (((PathKind)(info.kind)) != PathKind_File) {
return false;
}
FileHandle input = OpenRead(source);
if (!input.valid) {
return false;
}
FileHandle output = OpenReplace(target);
if (!output.valid) {
CloseFile(input);
return false;
}
uint8_t chunk[65536] = {0};
int64_t offset = 0LL;
bool ok = true;
while (true) {
Slice value_3 = {chunk, 65536};
int64_t count = ReadAt(input, value_3, offset);
if (count < 0LL) {
ok = false;
break;
}
if (count == 0LL) {
break;
}
Slice value_4 = {chunk, 65536};
int64_t value_5 = WriteAt(output, SliceRange(value_4, (int64_t)0LL, (int64_t)count, sizeof(uint8_t)), offset);
if (value_5 != count) {
ok = false;
break;
}
offset = (int64_t)((uint64_t)offset + (uint64_t)count);
}
CloseFile(input);
bool value_6 = CloseFile(output);
if (!value_6) {
ok = false;
}
return ok && file_tree_linux_SetPathMode(target, info.mode);
}
bool
file_tree_linux_CopyTree(String source, String target)
{
PathInfo value_0 = {0};
{
String path = source;
bool follow_links = true;
PathQueryResult value_2 = file_tree_linux_PathQuery(path, follow_links);
value_0 = value_2.info;
}
PathInfo info = value_0;
if (((PathKind)(info.kind)) != PathKind_Directory) {
return false;
}
bool value_3 = file_tree_linux_CreateDirectories(target, 493);
if (!value_3) {
return false;
}
__type_c53c2d7c4913dc02 names = {0};
__type_c53c2d7c4913dc02* value_4 = &names;
bool value_5 = file_tree_linux_ListDirectory(source, &(names));
if (!value_5) {
bool value_6 = false;
free((*value_4).data);
(*value_4).data = NULL;
(*value_4).capacity = 0;
(*value_4).count = 0;
return value_6;
}
int64_t index = 0LL;
while (index < names.count) {
Text from = file_tree_linux_JoinPath(source, ZIRAN_VEC_INDEX((names).data, (names).count, index));
String to = vec_TextDetach(file_tree_linux_JoinPath(target, ZIRAN_VEC_INDEX((names).data, (names).count, index)));
String value_7 = StringView((from.bytes).count > 0 ? (const char *)(from.bytes).data : "", (size_t)(from.bytes).count);
PathInfo value_8 = {0};
{
String path = value_7;
bool follow_links = false;
PathQueryResult value_10 = file_tree_linux_PathQuery(path, follow_links);
value_8 = value_10.info;
}
PathKind kind = (PathKind)(value_8.kind);
bool ok = false;
if (kind == PathKind_Directory) {
ok = file_tree_linux_CopyTree(StringView((from.bytes).count > 0 ? (const char *)(from.bytes).data : "", (size_t)(from.bytes).count), to);
} else if (kind == PathKind_File) {
ok = file_tree_linux_CopyFile(StringView((from.bytes).count > 0 ? (const char *)(from.bytes).data : "", (size_t)(from.bytes).count), to);
} else if (kind == PathKind_Link) {
ok = file_tree_linux_CopyLink(StringView((from.bytes).count > 0 ? (const char *)(from.bytes).data : "", (size_t)(from.bytes).count), to);
}
if (!ok) {
bool value_11 = false;
free((from).bytes.data);
(from).bytes.data = NULL;
(from).bytes.capacity = 0;
(from).bytes.count = 0;
free((*value_4).data);
(*value_4).data = NULL;
(*value_4).capacity = 0;
(*value_4).count = 0;
return value_11;
}
index = (int64_t)((uint64_t)index + UINT64_C(1));
free((from).bytes.data);
(from).bytes.data = NULL;
(from).bytes.capacity = 0;
(from).bytes.count = 0;
}
bool value_12 = file_tree_linux_SetPathMode(target, info.mode);
free((*value_4).data);
(*value_4).data = NULL;
(*value_4).capacity = 0;
(*value_4).count = 0;
return value_12;
}
bool
file_tree_linux_CopySymbolicLink(String source, String target)
{
return file_tree_linux_CopyLink(source, target);
}
bool
file_tree_linux_CopyFileNew(String source, String target)
{
PathInfo value_0 = {0};
{
String path = source;
bool follow_links = false;
PathQueryResult value_2 = file_tree_linux_PathQuery(path, follow_links);
value_0 = value_2.info;
}
PathInfo info = value_0;
if (((PathKind)(info.kind)) == PathKind_Link) {
String value_3 = source;
String value_4 = target;
bool value_5 = false;
{
String source = value_3;
String target = value_4;
value_5 = file_tree_linux_CopyLink(source, target);
}
return value_5;
}
if (((PathKind)(info.kind)) != PathKind_File) {
return false;
}
FileHandle input = OpenRead(source);
if (!input.valid) {
return false;
}
FileHandle output = OpenExclusivePrivate(target);
if (!output.valid) {
CloseFile(input);
return false;
}
uint8_t chunk[65536] = {0};
int64_t offset = 0LL;
bool ok = true;
while (true) {
Slice value_7 = {chunk, 65536};
int64_t count = ReadAt(input, value_7, offset);
if (count < 0LL) {
ok = false;
break;
}
if (count == 0LL) {
break;
}
Slice value_8 = {chunk, 65536};
int64_t value_9 = WriteAt(output, SliceRange(value_8, (int64_t)0LL, (int64_t)count, sizeof(uint8_t)), offset);
if (value_9 != count) {
ok = false;
break;
}
offset = (int64_t)((uint64_t)offset + (uint64_t)count);
}
bool value_10 = CloseFile(input);
if (!value_10) {
ok = false;
}
if (ok && zir_f_0ee210116da2a0c9_ChangeFileModeRaw(output.fd, (uint32_t)(info.mode)) != 0) {
ok = false;
}
bool value_11 = CloseFile(output);
if (!value_11) {
ok = false;
}
return ok;
}
bool
file_tree_linux_CopyTreeNew(String source, String target)
{
PathInfo value_0 = {0};
{
String path = source;
bool follow_links = false;
PathQueryResult value_2 = file_tree_linux_PathQuery(path, follow_links);
value_0 = value_2.info;
}
PathInfo info = value_0;
if (((PathKind)(info.kind)) == PathKind_Link) {
String value_3 = source;
String value_4 = target;
bool value_5 = false;
{
String source = value_3;
String target = value_4;
value_5 = file_tree_linux_CopyLink(source, target);
}
return value_5;
}
if (((PathKind)(info.kind)) != PathKind_Directory) {
return false;
}
String canonical = vec_TextDetach(file_tree_linux_ResolvePath(source));
String parent = vec_TextDetach(file_tree_linux_ResolvePath(file_tree_linux_PathParent(target)));
String name = file_tree_linux_PathName(target);
bool value_7 = (int64_t)(canonical).length == 0LL || (int64_t)(parent).length == 0LL || StringEqual(name, StringLiteral(""));
if (value_7 || StringEqual(name, StringLiteral(".")) || StringEqual(name, StringLiteral(".."))) {
bool cleanup_return_0 = false;
{
file_tree_linux_ReleaseOwnedText(parent);
}
{
file_tree_linux_ReleaseOwnedText(canonical);
}
return cleanup_return_0;
}
bool value_8 = StringEqual(parent, canonical);
if (!value_8) {
bool value_9 = (int64_t)(parent).length > (int64_t)(canonical).length && StringEqual(StringRange(parent, (int64_t)0LL, (int64_t)(int64_t)(canonical).length), canonical);
bool value_10 = value_9 && (StringEqual(canonical, StringLiteral("/")) || (uint8_t)ZIRAN_INDEX(parent.data, parent.length, (int64_t)(canonical).length) == 47);
value_8 = value_10;
}
if (value_8) {
bool cleanup_return_1 = false;
{
file_tree_linux_ReleaseOwnedText(parent);
}
{
file_tree_linux_ReleaseOwnedText(canonical);
}
return cleanup_return_1;
}
String destination = vec_TextDetach(file_tree_linux_JoinPath(parent, name));
bool cleanup_return_2 = file_tree_linux_CopyTreeNewContents(canonical, destination, info.mode);
{
file_tree_linux_ReleaseOwnedText(destination);
}
{
file_tree_linux_ReleaseOwnedText(parent);
}
{
file_tree_linux_ReleaseOwnedText(canonical);
}
return cleanup_return_2;
}
static bool
file_tree_linux_CopyTreeNewContents(String source, String target, int32_t mode)
{
uint8_t name[4096] = {0};
Slice value_0 = {name, 4096};
bool value_1 = CopyCString(target, value_0);
if (!value_1 || zir_f_0ee210116da2a0c9_MakeDirectoryRaw(&(name[0]), 448) != 0) {
return false;
}
__type_c53c2d7c4913dc02 names = {0};
__type_c53c2d7c4913dc02* value_2 = &names;
bool value_3 = file_tree_linux_ListDirectory(source, &(names));
if (!value_3) {
bool cleanup_return_0 = false;
{
file_tree_linux_ReleaseNames(&(names), 0LL);
}
bool value_4 = cleanup_return_0;
free((*value_2).data);
(*value_2).data = NULL;
(*value_2).capacity = 0;
(*value_2).count = 0;
return value_4;
}
{
int64_t range_first_12 = 0LL;
int64_t range_last_12 = (int64_t)((uint64_t)(names.count) - UINT64_C(1));
int64_t index = range_first_12;
if (index <= range_last_12) for (;;) {
String from = vec_TextDetach(file_tree_linux_JoinPath(source, ZIRAN_VEC_INDEX((names).data, (names).count, index)));
String to = vec_TextDetach(file_tree_linux_JoinPath(target, ZIRAN_VEC_INDEX((names).data, (names).count, index)));
PathInfo value_5 = {0};
{
String path = from;
bool follow_links = false;
PathQueryResult value_7 = file_tree_linux_PathQuery(path, follow_links);
value_5 = value_7.info;
}
PathInfo info = value_5;
bool ok = false;
if (((PathKind)(info.kind)) == PathKind_Directory) {
ok = file_tree_linux_CopyTreeNewContents(from, to, info.mode);
} else if (((PathKind)(info.kind)) == PathKind_File) {
ok = file_tree_linux_CopyFileNew(from, to);
} else if (((PathKind)(info.kind)) == PathKind_Link) {
bool value_8 = false;
{
String source = from;
String target = to;
value_8 = file_tree_linux_CopyLink(source, target);
}
ok = value_8;
}
if (!ok) {
bool cleanup_return_1 = false;
{
file_tree_linux_ReleaseOwnedText(to);
}
{
file_tree_linux_ReleaseOwnedText(from);
}
{
file_tree_linux_ReleaseNames(&(names), 0LL);
}
bool value_10 = cleanup_return_1;
free((*value_2).data);
(*value_2).data = NULL;
(*value_2).capacity = 0;
(*value_2).count = 0;
return value_10;
}
{
file_tree_linux_ReleaseOwnedText(to);
}
{
file_tree_linux_ReleaseOwnedText(from);
}
if (index == range_last_12) {
break;
}
index = (int64_t)((uint64_t)index + UINT64_C(1));
}
}
bool cleanup_return_2 = file_tree_linux_SetPathMode(target, mode);
{
file_tree_linux_ReleaseNames(&(names), 0LL);
}
bool value_11 = cleanup_return_2;
free((*value_2).data);
(*value_2).data = NULL;
(*value_2).capacity = 0;
(*value_2).count = 0;
return value_11;
}
static bool
file_tree_linux_CopyLink(String source, String target)
{
uint8_t name[4096] = {0};
uint8_t destination[4096] = {0};
uint8_t link[4096] = {0};
Slice value_0 = {name, 4096};
bool value_1 = CopyCString(source, value_0);
bool value_2 = !value_1;
if (!value_2) {
Slice value_3 = {link, 4096};
bool value_4 = CopyCString(target, value_3);
value_2 = !value_4;
}
if (value_2) {
return false;
}
ptrdiff_t value_5 = zir_f_0ee210116da2a0c9_ReadLinkRaw(&(name[0]), &(destination[0]), 4096);
int64_t count = (int64_t)(value_5);
if (count < 0LL || count >= 4096LL) {
return false;
}
uint8_t* value_6 = &(destination[count]);
(*value_6) = 0;
return zir_f_0ee210116da2a0c9_SymbolicLinkRaw(&(destination[0]), &(link[0])) == 0;
}
bool
file_tree_linux_ReplaceFile(String path, String text)
{
__type_838711075b90279a temporary_text = {0};
__type_838711075b90279a* value_0 = &temporary_text;
bool value_1 = false;
if ((path).length == 0) {
value_1 = true;
} else {
void *value_2 = ZirVecReserve((void *)(temporary_text).data, &(temporary_text).capacity, (temporary_text).count, (int64_t)(path).length, sizeof(*(temporary_text).data));
if (value_2 != NULL) {
(temporary_text).data = (uint8_t *)value_2;
memcpy((temporary_text).data + (temporary_text).count, (path).data, (size_t)(path).length);
(temporary_text).count += (int64_t)(path).length;
value_1 = true;
}
}
(void)value_1;
bool value_3 = false;
if ((StringLiteral(".new-")).length == 0) {
value_3 = true;
} else {
void *value_4 = ZirVecReserve((void *)(temporary_text).data, &(temporary_text).capacity, (temporary_text).count, (int64_t)(StringLiteral(".new-")).length, sizeof(*(temporary_text).data));
if (value_4 != NULL) {
(temporary_text).data = (uint8_t *)value_4;
memcpy((temporary_text).data + (temporary_text).count, (StringLiteral(".new-")).data, (size_t)(StringLiteral(".new-")).length);
(temporary_text).count += (int64_t)(StringLiteral(".new-")).length;
value_3 = true;
}
}
(void)value_3;
uint8_t digits[24] = {0};
int64_t at = 24LL;
int32_t pid = zir_f_0ee210116da2a0c9_ProcessIdRaw();
while (true) {
at = (int64_t)((uint64_t)at - UINT64_C(1));
uint8_t* value_5 = &(ZIRAN_INDEX(digits, sizeof(digits) / sizeof(digits[0]), at));
(*value_5) = (uint8_t)(((int32_t)(48u + (uint32_t)((pid % 10)))));
pid = pid / 10;
if (pid == 0) {
break;
}
}
while (at < 24LL) {
uint8_t value_6 = ZIRAN_INDEX(digits, 24, at);
bool value_7 = false;
void *value_8 = ZirVecGrow((void *)(temporary_text).data, &(temporary_text).capacity, (temporary_text).count, sizeof(*(temporary_text).data));
if (value_8 != NULL) {
(temporary_text).data = (uint8_t *)value_8;
(temporary_text).data[(temporary_text).count++] = value_6;
value_7 = true;
}
(void)value_7;
at = (int64_t)((uint64_t)at + UINT64_C(1));
}
__type_838711075b90279a value_9 = temporary_text;
temporary_text.data = NULL;
temporary_text.capacity = 0;
temporary_text.count = 0;
Text temporary = vec_TextFromBuilder(value_9);
String value_10 = path;
PathInfo value_11 = {0};
{
String path = value_10;
bool follow_links = true;
PathQueryResult value_13 = file_tree_linux_PathQuery(path, follow_links);
value_11 = value_13.info;
}
PathInfo previous = value_11;
uint8_t name[4096] = {0};
uint8_t final[4096] = {0};
Slice value_14 = {name, 4096};
bool value_15 = CopyCString(StringView((temporary.bytes).count > 0 ? (const char *)(temporary.bytes).data : "", (size_t)(temporary.bytes).count), value_14);
bool value_16 = !value_15;
if (!value_16) {
Slice value_17 = {final, 4096};
bool value_18 = CopyCString(path, value_17);
value_16 = !value_18;
}
if (value_16) {
bool value_19 = false;
free((temporary).bytes.data);
(temporary).bytes.data = NULL;
(temporary).bytes.capacity = 0;
(temporary).bytes.count = 0;
free((*value_0).data);
(*value_0).data = NULL;
(*value_0).capacity = 0;
(*value_0).count = 0;
return value_19;
}
int32_t value_20 = 0;
if (((PathKind)(previous.kind)) == PathKind_File) {
value_20 = previous.mode;
} else {
value_20 = 420;
}
int32_t mode = value_20;
int32_t fd = zir_f_0ee210116da2a0c9_OpenRaw(&(name[0]), 524865, mode);
if (fd < 0) {
bool value_21 = false;
free((temporary).bytes.data);
(temporary).bytes.data = NULL;
(temporary).bytes.capacity = 0;
(temporary).bytes.count = 0;
free((*value_0).data);
(*value_0).data = NULL;
(*value_0).capacity = 0;
(*value_0).count = 0;
return value_21;
}
uint8_t chunk[65536] = {0};
int64_t written = 0LL;
bool ok = true;
while (ok && written < (int64_t)(text).length) {
int64_t size = 0LL;
while (size < 65536LL && (int64_t)((uint64_t)written + (uint64_t)size) < (int64_t)(text).length) {
uint8_t* value_22 = &(chunk[size]);
(*value_22) = (uint8_t)ZIRAN_INDEX(text.data, text.length, ((int64_t)((uint64_t)written + (uint64_t)size)));
size = (int64_t)((uint64_t)size + UINT64_C(1));
}
int64_t sent = 0LL;
while (sent < size) {
ptrdiff_t value_23 = zir_f_0ee210116da2a0c9_WriteRaw(fd, &(ZIRAN_INDEX(chunk, sizeof(chunk) / sizeof(chunk[0]), sent)), (size_t)(((int64_t)((uint64_t)size - (uint64_t)sent))));
int64_t count = (int64_t)(value_23);
bool value_24 = count < 0LL;
if (value_24) {
int32_t* value_25 = zir_f_0ee210116da2a0c9_ErrnoRaw();
value_24 = (*(value_25) == 4);
}
if (value_24) {
continue;
}
if (count <= 0LL) {
ok = false;
break;
}
sent = (int64_t)((uint64_t)sent + (uint64_t)count);
}
written = (int64_t)((uint64_t)written + (uint64_t)size);
}
if (ok && zir_f_0ee210116da2a0c9_FsyncRaw(fd) != 0) {
ok = false;
}
if (zir_f_0ee210116da2a0c9_CloseRaw(fd) != 0) {
ok = false;
}
if (ok && ((PathKind)(previous.kind)) == PathKind_File) {
ok = zir_f_0ee210116da2a0c9_ChangeModeRaw(&(name[0]), (uint32_t)(mode)) == 0;
}
if (ok && zir_f_0ee210116da2a0c9_RenameRaw(&(name[0]), &(final[0])) != 0) {
ok = false;
}
if (!ok) {
zir_f_0ee210116da2a0c9_UnlinkRaw(&(name[0]));
}
bool value_26 = ok;
free((temporary).bytes.data);
(temporary).bytes.data = NULL;
(temporary).bytes.capacity = 0;
(temporary).bytes.count = 0;
free((*value_0).data);
(*value_0).data = NULL;
(*value_0).capacity = 0;
(*value_0).count = 0;
return value_26;
}
int32_t
file_tree_linux_LockPath(String path, bool exclusive, bool wait)
{
uint8_t name[4096] = {0};
Slice value_0 = {name, 4096};
bool value_1 = CopyCString(path, value_0);
if (!value_1) {
return -1;
}
int32_t fd = zir_f_0ee210116da2a0c9_OpenRaw(&(name[0]), 525377, 420);
if (fd < 0) {
return -1;
}
int32_t value_2 = 0;
if (exclusive) {
value_2 = 2;
} else {
value_2 = 1;
}
int32_t operation = value_2;
if (!wait) {
operation = (int32_t)((uint32_t)operation | 4u);
}
while (zir_f_0ee210116da2a0c9_FlockRaw(fd, operation) != 0) {
int32_t* value_3 = zir_f_0ee210116da2a0c9_ErrnoRaw();
if (*(value_3) != 4) {
zir_f_0ee210116da2a0c9_CloseRaw(fd);
return -1;
}
}
return fd;
}
void
file_tree_linux_UnlockPath(int32_t fd)
{
if (fd < 0) {
return;
}
zir_f_0ee210116da2a0c9_FlockRaw(fd, 8);
zir_f_0ee210116da2a0c9_CloseRaw(fd);
}
bool
file_tree_linux_MatchName(String name, String pattern)
{
return file_tree_linux_MatchAt(name, 0LL, pattern, 0LL);
}
static bool
file_tree_linux_MatchAt(String name, int64_t at, String pattern, int64_t from)
{
int64_t n = at;
int64_t p = from;
while (p < (int64_t)(pattern).length) {
uint8_t token = (uint8_t)ZIRAN_INDEX(pattern.data, pattern.length, p);
if (token == 42) {
while (p < (int64_t)(pattern).length && (uint8_t)ZIRAN_INDEX(pattern.data, pattern.length, p) == 42) {
p = (int64_t)((uint64_t)p + UINT64_C(1));
}
if (p == (int64_t)(pattern).length) {
return true;
}
int64_t start = n;
while (start <= (int64_t)(name).length) {
if (file_tree_linux_MatchAt(name, start, pattern, p)) {
return true;
}
start = (int64_t)((uint64_t)start + UINT64_C(1));
}
return false;
}
if (n >= (int64_t)(name).length) {
return false;
}
if (token == 63) {
n = (int64_t)((uint64_t)n + UINT64_C(1));
p = (int64_t)((uint64_t)p + UINT64_C(1));
continue;
}
if (token == 91) {
int64_t close = (int64_t)((uint64_t)p + UINT64_C(1));
if (close < (int64_t)(pattern).length && (uint8_t)ZIRAN_INDEX(pattern.data, pattern.length, close) == 33) {
close = (int64_t)((uint64_t)close + UINT64_C(1));
}
if (close < (int64_t)(pattern).length && (uint8_t)ZIRAN_INDEX(pattern.data, pattern.length, close) == 93) {
close = (int64_t)((uint64_t)close + UINT64_C(1));
}
while (close < (int64_t)(pattern).length && (uint8_t)ZIRAN_INDEX(pattern.data, pattern.length, close) != 93) {
close = (int64_t)((uint64_t)close + UINT64_C(1));
}
if (close < (int64_t)(pattern).length) {
bool negate = (uint8_t)ZIRAN_INDEX(pattern.data, pattern.length, ((int64_t)((uint64_t)p + UINT64_C(1)))) == 33;
int64_t value_0 = 0;
if (negate) {
value_0 = (int64_t)((uint64_t)p + UINT64_C(2));
} else {
value_0 = (int64_t)((uint64_t)p + UINT64_C(1));
}
int64_t index = value_0;
bool found = false;
while (index < close) {
uint8_t low = (uint8_t)ZIRAN_INDEX(pattern.data, pattern.length, index);
uint8_t high = low;
if ((int64_t)((uint64_t)index + UINT64_C(2)) < close && (uint8_t)ZIRAN_INDEX(pattern.data, pattern.length, ((int64_t)((uint64_t)index + UINT64_C(1)))) == 45) {
high = (uint8_t)ZIRAN_INDEX(pattern.data, pattern.length, ((int64_t)((uint64_t)index + UINT64_C(2))));
index = (int64_t)((uint64_t)index + UINT64_C(2));
}
if ((uint8_t)ZIRAN_INDEX(name.data, name.length, n) >= low && (uint8_t)ZIRAN_INDEX(name.data, name.length, n) <= high) {
found = true;
}
index = (int64_t)((uint64_t)index + UINT64_C(1));
}
if (found == negate) {
return false;
}
n = (int64_t)((uint64_t)n + UINT64_C(1));
p = (int64_t)((uint64_t)close + UINT64_C(1));
continue;
}
}
if ((uint8_t)ZIRAN_INDEX(name.data, name.length, n) != token) {
return false;
}
n = (int64_t)((uint64_t)n + UINT64_C(1));
p = (int64_t)((uint64_t)p + UINT64_C(1));
}
return n == (int64_t)(name).length;
}