diff options
| author | Leah Rowe <leah@libreboot.org> | 2026-03-24 17:06:48 +0000 |
|---|---|---|
| committer | Leah Rowe <leah@libreboot.org> | 2026-03-24 17:18:47 +0000 |
| commit | e9c5da1a25641e09244d3f52d7bb90983e0e5550 (patch) | |
| tree | 2561158c2ff784bbde8e0373ef396e1eea3715c5 /util/libreboot-utils/lib/mkhtemp.c | |
| parent | 56ab5a18fee5257c3c875f2b8597b8379c7b959c (diff) | |
util/nvmutil: use new fs_open functions for gbe
this unifies nvmutil's file handling with the
handling used by mkhtemp. a special function
has been written for this. this allows greater
flexibility since we can more easily check the
integrity of a file at inode/dev level; this
complements nvmutil's existing content-based
verification.
(this also fixes nvmutil, so that gbe files can
be changed again. mkhtemp broke it while i was
writing it, but now everything works again)
Signed-off-by: Leah Rowe <leah@libreboot.org>
Diffstat (limited to 'util/libreboot-utils/lib/mkhtemp.c')
| -rw-r--r-- | util/libreboot-utils/lib/mkhtemp.c | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/util/libreboot-utils/lib/mkhtemp.c b/util/libreboot-utils/lib/mkhtemp.c index b30f6587..d1ebea25 100644 --- a/util/libreboot-utils/lib/mkhtemp.c +++ b/util/libreboot-utils/lib/mkhtemp.c @@ -39,6 +39,58 @@ new_tmpdir(int *fd, char **path) return new_tmp_common(fd, path, MKHTEMP_DIR); } +/* not used by mkhtemp util, but by nvmutil. + * it leaves an st variable set after return, + * so that verification can be done on a file + * being used, before writing it elsewhere + */ +int +new_tmpfile_at(int dirfd, struct stat *st_dir, + int *fd, char **name) +{ + struct stat st; + char suffix[] = "tmp.XXXXXXXXXX"; + size_t len; + char *buf = NULL; + char *fname; + int saved_errno = errno; + +#if defined(PATH_LEN) && \ + (PATH_LEN) >= 256 + size_t maxlen = PATH_LEN; +#else + size_t maxlen = 4096; +#endif + + if (if_err(fd == NULL || name == NULL || st_dir == NULL, EFAULT) || + if_err(*fd >= 0, EEXIST) || + if_err(dirfd < 0, EBADF) || + if_err_sys(slen(suffix, maxlen, &len) < 0) || + if_err_sys((malloc(len + 1)) == NULL)) + return -1; + + memcpy(buf, suffix, len + 1); + fname = buf; + + *fd = mkhtemp(fd, &st, + buf, dirfd, fname, + st_dir, MKHTEMP_FILE); + + if (*fd < 0) + goto err; + + *name = buf; + errno = saved_errno; + + return 0; + +err: + free_if_null(&buf); + close_no_err(fd); + + return -1; +} + /* WARNING: * on error, *path (at **path) may be NULL, or if the error pertains to |
