summaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorLeah Rowe <leah@libreboot.org>2026-03-16 16:00:49 +0000
committerLeah Rowe <leah@libreboot.org>2026-03-16 16:00:49 +0000
commitd34571217265cc3ee3d5bfe9f5e4016fcc42dfd5 (patch)
tree04576d0d2caab4554be46ad13f5ea8f9278521fa /util
parent7d5ada2272c5145f88d9312e8c3742cccaa1e4f7 (diff)
util/nvmutil: more portable functtions
Signed-off-by: Leah Rowe <leah@libreboot.org>
Diffstat (limited to 'util')
-rw-r--r--util/nvmutil/nvmutil.c72
1 files changed, 67 insertions, 5 deletions
diff --git a/util/nvmutil/nvmutil.c b/util/nvmutil/nvmutil.c
index 2ca27ade..4a5e48df 100644
--- a/util/nvmutil/nvmutil.c
+++ b/util/nvmutil/nvmutil.c
@@ -475,6 +475,8 @@ static const char *getnvmprogname(void);
*/
static char *new_tmpfile(int *fd, int local, const char *path);
static int x_i_mkstemp(char *template);
+static char *x_c_strrchr(const char *s, int c);
+static int x_i_rename(const char *src, const char *dst);
/*
* Sizes in bytes:
@@ -1992,7 +1994,7 @@ gbe_mv(void)
saved_errno = errno;
- r = rename(tname, fname);
+ r = x_i_rename(tname, fname);
if (r > -1) {
/*
@@ -2045,7 +2047,7 @@ gbe_mv(void)
if (close(dest_fd) == -1)
goto ret_gbe_mv;
- if (rename(dest_tmp, fname) == -1)
+ if (x_i_rename(dest_tmp, fname) == -1)
goto ret_gbe_mv;
if (fsync_dir(fname) < 0)
@@ -2098,7 +2100,7 @@ ret_gbe_mv:
}
/*
- * Ensure rename() is durable by syncing the
+ * Ensure x_i_rename() is durable by syncing the
* directory containing the target file.
*/
static int
@@ -2132,7 +2134,7 @@ fsync_dir(const char *path)
goto err_fsync_dir;
memcpy(dirbuf, path, pathlen + 1);
- slash = strrchr(dirbuf, '/');
+ slash = x_c_strrchr(dirbuf, '/');
if (slash != NULL) {
*slash = '\0';
@@ -2801,7 +2803,7 @@ getnvmprogname(void)
if (argv0 == NULL || *argv0 == '\0')
return "";
- p = strrchr(argv0, '/');
+ p = x_c_strrchr(argv0, '/');
if (p)
return p + 1;
@@ -3025,3 +3027,63 @@ x_i_mkstemp(char *template)
fd = open(template, O_RDWR | O_CREAT | O_EXCL, 0600);
return fd;
}
+
+static char *
+x_c_strrchr(const char *s, int c)
+{
+ const char *p = NULL;
+
+ while (*s) {
+ if (*s == (char)c)
+ p = s;
+ s++;
+ }
+
+ if (c == '\0')
+ return (char *)s;
+
+ return (char *)p;
+}
+
+static int
+x_i_rename(const char *src, const char *dst)
+{
+ int sfd, dfd;
+ ssize_t r;
+ char buf[8192];
+
+ sfd = open(src, O_RDONLY);
+ if (sfd < 0)
+ return -1;
+
+ dfd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, 0600);
+ if (dfd < 0) {
+ close(sfd);
+ return -1;
+ }
+
+ while ((r = read(sfd, buf, sizeof(buf))) > 0) {
+ ssize_t w = write(dfd, buf, r);
+ if (w != r) {
+ close(sfd);
+ close(dfd);
+ return -1;
+ }
+ }
+
+ if (r < 0) {
+ close(sfd);
+ close(dfd);
+ return -1;
+ }
+
+ fsync(dfd);
+
+ close(sfd);
+ close(dfd);
+
+ if (unlink(src) < 0)
+ return -1;
+
+ return 0;
+}