Welcome to little lamb

Code » limb » commit 3536698

Add macros open_tmp() & open_read_close(), ..

author Olivier Brunel
2023-05-13 08:22:55 UTC
committer Olivier Brunel
2023-07-05 07:37:02 UTC
parent d8b445ac05369c30b295d53846896690340e04bf

Add macros open_tmp() & open_read_close(), ..

..open_slurp_close(), open_write_close() & open_writev_close()

src/doc/djbunix.h.0.md +19 -0
src/doc/djbunix.h/open_read_close.3.md +29 -0
src/doc/djbunix.h/open_tmp.3.md +40 -0
src/liblimb/include/limb/djbunix.h +8 -0

diff --git a/src/doc/djbunix.h.0.md b/src/doc/djbunix.h.0.md
index 0828df2..a33da9b 100644
--- a/src/doc/djbunix.h.0.md
+++ b/src/doc/djbunix.h.0.md
@@ -25,6 +25,25 @@ The following functions/macros are defined :
 : [open_parsed_name](3)
 :: Return the specified file descriptor, or that of the given file name.
 
+: [open_tmp](3)
+:: Open a new temporary file for writing.
+
+: [open_read_close](3)
+:: Same as [open_read_closeat](3) but relative path are based on current working
+:: directory.
+
+: [open_slurp_close](3)
+:: Same as [open_slurp_closeat](3) but relative path are based on current
+:: working directory.
+
+: [open_write_close](3)
+:: Same as [open_write_closeat](3) but relative path are based on current
+:: working directory.
+
+: [open_writev_close](3)
+:: Same as [open_writev_closeat](3) but relative path are based on current
+:: working directory.
+
 : [rm_rfat](3)
 :: Remove an entire directory entry, with relative path based on given file
 :: descriptor
diff --git a/src/doc/djbunix.h/open_read_close.3.md b/src/doc/djbunix.h/open_read_close.3.md
new file mode 100644
index 0000000..2fd2513
--- /dev/null
+++ b/src/doc/djbunix.h/open_read_close.3.md
@@ -0,0 +1,29 @@
+% limb manual
+% open_read_close(3)
+
+# NAME
+
+open\_read\_close, open\_slurp\_close,
+open\_write\_close, open\_writev\_close - open, read or write & close a file
+
+# SYNOPSIS
+
+    #include <limb/djbunix.h>
+
+```pre hl
+ssize_t open_read_close(char *<em>dst</em>, size_t <em>dlen</em>, const char *<em>file</em>)
+int open_slurp_close(stralloc *<em>sa</em>, const char *<em>file</em>)
+int open_write_close(const char *<em>file</em>, const char *<em>data</em>, size_t <em>dlen</em>)
+int open_writev_close(const char *<em>file</em>, const struct iovec *<em>v</em>, unsigned <em>n</em>)
+```
+
+# DESCRIPTION
+
+These functions are similar to their \*at counterparts, only when `file`
+describes a relative path it is always to the current working directory.
+
+! NOTE:
+! These are implemented as macros to their \*at counterparts.
+
+For more information, refer to [open_read_closeat](3), [open_slurp_closeat](3),
+[open_write_closeat](3) and [open_writev_closeat](3).
diff --git a/src/doc/djbunix.h/open_tmp.3.md b/src/doc/djbunix.h/open_tmp.3.md
new file mode 100644
index 0000000..b7e54ac
--- /dev/null
+++ b/src/doc/djbunix.h/open_tmp.3.md
@@ -0,0 +1,40 @@
+% limb manual
+% open_tmp(3)
+
+# NAME
+
+open\_tmp - open a new temporary file
+
+# SYNOPSIS
+
+    #include <limb/djbunix.h>
+
+```pre hl
+int open_tmp(char *<em>name</em>)
+```
+
+# DESCRIPTION
+
+The `open_tmp`() function opens a new temporary file for writing, using a name
+based on the template pointed by `name` - which must end with at least 6
+characters `X` that will be replaced with random (printable) characters.
+
+The memory pointed by `name` will be modified, replacing the trailing `X`
+characters so that `name` contains the name of the file actually opened.
+
+! NOTE:
+! It is actually implemented as a macro to [mkfiletemp](3).
+
+# RETURN VALUE
+
+The `open_tmp`() function returns the opened file descriptor on success.
+Otherwise it returns -1 and sets `errno` to indicate the error.
+
+# ERRORS
+
+The `open_tmp`() function may fail if :
+
+: *EINVAL*
+:: The template pointed by `name` does not end with at least 6 `X`.
+
+It may also fail and set `errno` for the errors described for [open_excl](3).
diff --git a/src/liblimb/include/limb/djbunix.h b/src/liblimb/include/limb/djbunix.h
index 18f21f1..6a276cd 100644
--- a/src/liblimb/include/limb/djbunix.h
+++ b/src/liblimb/include/limb/djbunix.h
@@ -6,11 +6,19 @@
 
 #include <skalibs/djbunix.h>
 #include <skalibs/stralloc.h>
+#include <limb/unix-transactional.h>
 
 typedef int (*open_fn) (const char *file);
 
 extern int open_parsed_name(const char *name, open_fn open);
 
+#define open_tmp(tmp)       mkfiletemp(tmp, (create_func) (void (*)(void)) open_excl, 0, NULL)
+
+#define open_read_close(dst,dlen,file)      open_read_closeat(dst, dlen, AT_FDCWD, file)
+#define open_slurp_close(sa,file)           open_slurp_closeat(sa, AT_FDCWD, file)
+#define open_write_close(file,data,dlen)    open_write_closeat(AT_FDCWD, file, data, dlen)
+#define open_writev_close(file,v,n)         open_writev_closeat(AT_FDCWD, file, v, n)
+
 extern int rm_rfat(int fd, const char *name);
 extern int rm_rf_tmpat(int fd, const char *name, stralloc *sa);
 extern int rm_rf_in_tmpat(int fd, stralloc *sa, size_t offset);