summaryrefslogtreecommitdiffhomepage
path: root/lib/fs.c
AgeCommit message (Collapse)Author
2022-02-15fs: implement access(), mkstemp(), file.flush() and proc.flush()Jo-Philipp Wich
The `access()` function allows testing the path given in the first argument for accessibility according to the permissions specified in the second mode string argument. The mode string characters may be `r`, `w`, `x` or `f` which correspond to `R_OK` - path is readable, `W_OK` - path is writable, `X_OK` - path is executable or `F_OK` - path exists respectively. The `mkstemp()` function creates a secure temporary file, unlinks it and returns the open file handle. The temporary path is constructed based on the optional template argument. If the template argument contains a slash, the path is taken as-is, if it contains no slashes, `/tmp/` is prepended. If the template does not end with `XXXXXX`, a `.XXXXXX` suffix is appended to the path. If the template is omitted, `/tmp/XXXXXX` is used. The `file.flush()` and `proc.flush()` functions call `fflush()` on the underlying file handle respectively. They take no arguments. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-01-04fs: implement fdopen(), file.fileno() and proc.fileno()Jo-Philipp Wich
Implement support for opening existing file descriptors as well as acquiring the descriptor number from open process and file handles. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-11-04fs: add utility functionsJo-Philipp Wich
Add three new functions `dirname()`, `basename()` and `lsdir()`. The `basename()` and `dirname()` functions behave like their libc counterparts and return the filename and directory portion of a given path respectively. If the path argument is missing or not a string, null is returned. Examples: dirname("/usr/lib") -> "/usr" dirname("/usr/") -> "/" dirname("usr") -> "." dirname("/") -> "/" dirname(".") -> "." dirname("..") -> "." basename("/usr/lib") -> "lib" basename("/usr/") -> "usr" basename("usr") -> "usr" basename("/") -> "/" basename(".") -> "." basename("..") -> ".." The `lsdir()` function returns a sorted array containing the names of all entries within the given directory path, without the common "." and ".." entries. If the given path is not a directory, cannot be opened or if another system level occurs, null is returned and `fs.error()` can be used to query details. The function takes an optional second argument which may be either a regular expression value or a string. In case a regular expression is given, each directory entry is matched against it. In case a string is provided, it is treated as wildcard (glob) pattern and only directory entries matching the pattern are considered. Examples: lsdir("/sys/class/net") -> [ "eth0", "lo", "wlan0" ] lsdir("/proc", /^[0-9]+$/) -> [ "1", "4", "12", ... ] lsdir("/sys/class/block/", "sd?3") -> [ "sde3", "sdf3" ] Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-11-01treewide: fix typo in exported function names and typesJo-Philipp Wich
Fix instances of misspelled "resource". This commit breaks the exported libucode ABI. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-07-28fs: fix chown() and rename() error return valuesJo-Philipp Wich
Fixes: dfb7379 ("fs: implement chmod(), chown(), rename() and glob() functions") Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-07-11treewide: harmonize function namingJo-Philipp Wich
- Ensure that most functions follow the subject_verb naming schema - Move type related function from value.c to types.c - Rename value.c to vallist.c Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-07-11treewide: move header files into dedicated directoryJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-07-11treewide: consolidate typedef namingJo-Philipp Wich
Ensure that all custom typedef and vector declaration type names end with a "_t" suffix. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-07-11lib: rename uc_add_proto_functions() to uc_add_functions()Jo-Philipp Wich
The naming is an artifact from before the introduction of the new type system. In the current code, there is nothing special about prototypes, they're simple object values. Also introduce a new singular uc_add_function() convenience macro while we're at it. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-07-11treewide: move ressource type registry into vm instanceJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-05-15fs: implement chmod(), chown(), rename() and glob() functionsJo-Philipp Wich
- The chmod() function expects a path string as first and an integer mode value as second argument. - The chown() function takes a path string as first argument, and either a string, an integer or null as second user and third group argument respectively. If either user or group are given as string, they're resolved to an uid/gid using getpwnam()/getgrnam() internally. If either lookup fails, the ownership change is not performed. If either user or group are null or -1, they're left unchanged. - The rename() function takes two path strings, the old path being the first argument and the new path the second one. - The glob() function takes an arbitrary number of glob patterns and resolves matching files for each one. In case of multiple patterns, no efforts are made to remove duplicates or to globally sort the combined match list. The list of matches for each individual pattern is sorted. Returns an array containing all matched file paths. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-04-25treewide: rework internal data type systemJo-Philipp Wich
Instead of relying on json_object values internally, use custom types to represent the different ucode value types which brings a number of advantages compared to the previous approach: - Due to the use of tagged pointers, small integer, string and bool values can be stored directly in the pointer addresses, vastly reducing required heap memory - Ability to create circular data structures such as `let o; o = { test: o };` - Ability to register custom `tostring()` function through prototypes - Initial mark/sweep GC implementation to tear down circular object graphs on VM deinit The change also paves the way for possible future extensions such as constant variables and meta methods for custom ressource types. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-04-24treewide: fix issues reported by clang code analyzerJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-03-08fs: make error function available on directory, process and file handlesJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-03-08fs: fix readlink() return valueJo-Philipp Wich
The fs.readlink() function incorrectly produced a JSON string containing trailing null bytes. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-02-17treewide: rewrite ucode interpreterJo-Philipp Wich
Replace the former AST walking interpreter implementation with a single pass bytecode compiler and a corresponding virtual machine. The rewrite lays the groundwork for a couple of improvements with will be subsequently implemented: - Ability to precompile ucode sources into binary byte code - Strippable debug information - Reduced runtime memory usage Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-11-19treewide: rebrand to ucodeJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-11-15fs: extend process close() function to return program exit codeJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-11-10fs: do not close stdio streams when gc'ing scopeJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-10-19eval: record correct source contexts in call stackJo-Philipp Wich
Also handle calls to C functions. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-10-06fs: add stdio handlesJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-20treewide: rework extended type handlingJo-Philipp Wich
Register prototype object directly together with the type instead of setting it manually whenever an extended type value is instantiated. This also allows freeing the various prototype objects in dlopened modules. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-11fs: implement getcwd() and chdir()Jo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-10treewide: eliminate unused function argumentsJo-Philipp Wich
Also introduce convenience macro for registering function arrays in modules. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-09fs: implement popen()Jo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-08fs: rename readdir() and closedir() to read() and close() respectivelyJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-08fs: fix file seek() return valueJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-08fs: fix crash when closing file handle twiceJo-Philipp Wich
We need to clear the file handle pointer after closing the file to avoid invoking the dtor later. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-08fs: implement seek() and tell() for directory handlesJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-06fs: use ARRAY_SIZE macroJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-06fs: add further filesystem functionsJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-06treewide: refactor internal AST structuresJo-Philipp Wich
- unify operand and value tag structures - use a contiguous array for storing opcodes - use relative offsets for next and children ops - defer function creation to runtime - rework "this" context handling by storing context pointer in scope tags Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-02lib: introduce filesystem moduleJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>