summaryrefslogtreecommitdiffhomepage
path: root/lib/fs.c
AgeCommit message (Collapse)Author
2024-06-17fs: add lock() file methodFelix Fietkau
Implements file based locking on a given file handle. Signed-off-by: Felix Fietkau <nbd@nbd.name>
2024-06-17fs: add truncate() file methodFelix Fietkau
Trunates the file referenced by a file handle Signed-off-by: Felix Fietkau <nbd@nbd.name>
2023-10-11lib: various documentation fixesJo-Philipp Wich
- Consistently use nullable instead of `type|null` expressions - Use @borrows to reduce some duplicated documentation blocks - Add typedef for timelocal()/timegm() TimeSpec value Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2023-08-09treewide: consolidate platform specific code in platform.cJo-Philipp Wich
Get rid of most __APPLE__ guards by introducing a central platform.c unit providing drop-in replacements for missing APIs. Also move system signal definitions into the new platform file to be able to share them with the upcoming debug library. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2023-07-25fs: explicitly compare isatty() resultJo-Philipp Wich
Reportedly, automatic conversion of the `isatty()` int result value to a bool does not work correctly on PPC. Explicitly compare the result value with `1` to infer the boolean result value. Fixes: #165 Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2023-07-13Merge pull request #164 from jow-/docs-improvementsJo-Philipp Wich
docs: various improvements
2023-07-13docs: various improvementsJo-Philipp Wich
- Switch JSDoc theme to "clean-jsdoc-theme" - Add some custom CSS and JS tweaks to the theme - Use a condensed README.md for the toplevel directory - Include a longer README.md in the documentation portal - Tweak JSDoc annotations for better output results - Register `ucode.mein.io` CNAME Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2023-07-12Merge pull request #163 from jow-/docs-improvementsJo-Philipp Wich
fs: complete function documentation coverage
2023-07-12fs: use `fseeko()` and `ftello()`Jo-Philipp Wich
Use `fseeko()` and `ftello()` instead of `fseek()` and `ftell()` respectively in order to be able to deal with large file offsets. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2023-07-12fs: complete function documentation coverageJo-Philipp Wich
Add missing function documentation and return value annotations. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2023-07-12fs: add JSDoc documentationJo-Philipp Wich
Add JSDoc documentation blocks to all exported filesystem functions. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2023-01-20fs: implement `fs.pipe()`Jo-Philipp Wich
The `pipe()` function takes no arguments and will return a two element array containing open read- and write file descriptors for a newly created pipe. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2023-01-09fs: add `isatty()` functionPetr Štetiar
Expose the `isatty(3)` libc function in the fs module to allow checking whether a file descriptor refers to a terminal. Signed-off-by: Petr Štetiar <ynezz@true.cz>
2022-11-29fs: add `realpath()` functionJo-Philipp Wich
Expose the `realpath(3)` libc function in the fs module to allow for canonicalizing file paths. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-10-18fs: expose `getdelim()` functionality through `fd.read()`Jo-Philipp Wich
When `fd.read()` is invoked with a single-character string argument, invoke `getdelim()` internally to read the input until the give character or EOF. This is useful for reading character delimited input data. For example `fd.read('\n')` will read any data up to the first newline (or EOF) while `fd.read('\0x00')` will read until the first null byte. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-10-07fs: use `getline()` for line wise read operationsJo-Philipp Wich
Use `getline()` instead of a custom `fgets()` wrapper logic to perform line wise reads from open file handles. This is required to properly deal with lines containing embedded null bytes. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-09-05fs: add optional third permission argument to fs.open()Jo-Philipp Wich
Rework the `fs.open()` implementation to accept an optional third file permission argument which is applied to newly created files. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-05-20fs: avoid input buffering with small limits in fs.readfile()Jo-Philipp Wich
If the read limit passed to fs.readfile() is smaller than BUFSIZ then disable stdio input buffering to avoid overreading the underlying file. This is useful when reading small amounts of data from special files such as /dev/urandom, where a readfile call limited to 16 bytes might actually read 4096 due to stdio buffering. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-04-07fs: implement `fs.readfile()` and `fs.writefile()`Jo-Philipp Wich
The `fs.readfile()` and `fs.writefile()` functions allow efficient reading and writing of whole files, reducing the required boilerplace code compared to using the `open()`/`read()`/`write()`/`close()` API. - `fs.readfile()` takes two arguments; the path to open and an optional limit value. If limit is omitted, `null` or negative, the entire file contents are read, otherwise the specified amount of bytes. Returns the read file contents as string or `null` on error. - `fs.writefile()` takes three arguments; the path to open/create, the contents to write and an optional limit value. If limit is omitted, the entire content is written, otherwise just the specified amount of bytes. Non-string content arguments are internally converted to strings, `null` is treated as empty string. Returns the amount of bytes written or `null` on error. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-03-31fs: fix off-by-one in fs.dirname() functionDaniel Golle
Make sure fs.dirname() doesn't truncate the last character of the returned path. Previously ucv_string_new_length was called with a length which no longer included the last character (which had just been tested not to be a '/' or '.' and hence broke the loop at that point). Signed-off-by: Daniel Golle <daniel@makrotopia.org> [testcase added] Signed-off-by: Paul Spooren <mail@aparcar.org> [testcase folded into this commit and fixed] Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-03-15fs: avoid Linux specific sys/sysmacros.h include on OS XJo-Philipp Wich
It should be enough to include `sys/types.h` to obtain `major()` and `minor()` definitions on OS X, so avoid including the the Linux specific `sys/sysmacros.h` header in this case. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
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>