nvmutil: remove memcmp/memcpy/strrchr/rename

i had this idea in my head of later porting this
to k&r c for fun. but screw it.

compiling on everything since 1989 is enough

Signed-off-by: Leah Rowe <leah@libreboot.org>
This commit is contained in:
Leah Rowe
2026-03-19 07:32:46 +00:00
parent f1fda8b43e
commit 846cb23585
6 changed files with 18 additions and 128 deletions

View File

@@ -11,14 +11,6 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <limits.h> #include <limits.h>
/* keep SYS_RENAME 1 to
* use libc rename()
* recommended
*/
#ifndef SYS_RENAME
#define SYS_RENAME 1
#endif
#define items(x) (sizeof((x)) / sizeof((x)[0])) #define items(x) (sizeof((x)) / sizeof((x)[0]))
/* system prototypes /* system prototypes
@@ -434,14 +426,8 @@ const char *getnvmprogname(void);
char *new_tmpfile(int *fd, int local, const char *path); char *new_tmpfile(int *fd, int local, const char *path);
int x_i_mkstemp(char *template); int x_i_mkstemp(char *template);
char *x_c_strrchr(const char *s, int c);
int x_i_rename(const char *src, const char *dst);
char *x_c_tmpdir(void); char *x_c_tmpdir(void);
int x_i_close(int fd); int x_i_close(int fd);
void *x_v_memcpy(void *dst,
const void *src, unsigned long n);
int x_i_memcmp(const void *a,
const void *b, unsigned long n);
int x_i_fsync(int fd); int x_i_fsync(int fd);
/* asserts */ /* asserts */

View File

@@ -415,17 +415,17 @@ cmd_helper_swap(void)
check_cmd(cmd_helper_swap, "swap"); check_cmd(cmd_helper_swap, "swap");
x_v_memcpy( memcpy(
f->buf + (unsigned long)GBE_WORK_SIZE, f->buf + (unsigned long)GBE_WORK_SIZE,
f->buf, f->buf,
GBE_PART_SIZE); GBE_PART_SIZE);
x_v_memcpy( memcpy(
f->buf, f->buf,
f->buf + (unsigned long)GBE_PART_SIZE, f->buf + (unsigned long)GBE_PART_SIZE,
GBE_PART_SIZE); GBE_PART_SIZE);
x_v_memcpy( memcpy(
f->buf + (unsigned long)GBE_PART_SIZE, f->buf + (unsigned long)GBE_PART_SIZE,
f->buf + (unsigned long)GBE_WORK_SIZE, f->buf + (unsigned long)GBE_WORK_SIZE,
GBE_PART_SIZE); GBE_PART_SIZE);
@@ -442,7 +442,7 @@ cmd_helper_copy(void)
check_cmd(cmd_helper_copy, "copy"); check_cmd(cmd_helper_copy, "copy");
x_v_memcpy( memcpy(
f->buf + (unsigned long)((f->part ^ 1) * GBE_PART_SIZE), f->buf + (unsigned long)((f->part ^ 1) * GBE_PART_SIZE),
f->buf + (unsigned long)(f->part * GBE_PART_SIZE), f->buf + (unsigned long)(f->part * GBE_PART_SIZE),
GBE_PART_SIZE); GBE_PART_SIZE);

View File

@@ -5,6 +5,7 @@
* Safe file handling. * Safe file handling.
*/ */
#include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <errno.h> #include <errno.h>
@@ -66,7 +67,7 @@ xopen(int *fd_ptr, const char *path, int flags, struct stat *st)
err(errno, "%s: file not seekable", path); err(errno, "%s: file not seekable", path);
} }
/* Ensure x_i_rename() is durable by syncing the /* Ensure rename() is durable by syncing the
* directory containing the target file. * directory containing the target file.
*/ */
@@ -112,8 +113,8 @@ fsync_dir(const char *path)
if (dirbuf == NULL) if (dirbuf == NULL)
goto err_fsync_dir; goto err_fsync_dir;
x_v_memcpy(dirbuf, path, pathlen + 1); memcpy(dirbuf, path, pathlen + 1);
slash = x_c_strrchr(dirbuf, '/'); slash = strrchr(dirbuf, '/');
if (slash != NULL) { if (slash != NULL) {
*slash = '\0'; *slash = '\0';
@@ -294,17 +295,17 @@ new_tmpfile(int *fd, int local, const char *path)
*dest = '.'; /* hidden file */ *dest = '.'; /* hidden file */
x_v_memcpy(dest + (unsigned long)1, tmpname, tmpname_len); memcpy(dest + (unsigned long)1, tmpname, tmpname_len);
x_v_memcpy(dest + (unsigned long)1 + tmpname_len, memcpy(dest + (unsigned long)1 + tmpname_len,
default_tmpname, tmpdir_len); default_tmpname, tmpdir_len);
} else { } else {
x_v_memcpy(dest, base, tmpdir_len); memcpy(dest, base, tmpdir_len);
dest[tmpdir_len] = '/'; dest[tmpdir_len] = '/';
x_v_memcpy(dest + tmpdir_len + 1, tmpname, tmpname_len); memcpy(dest + tmpdir_len + 1, tmpname, tmpname_len);
} }
dest[tmppath_len] = '\0'; dest[tmppath_len] = '\0';
@@ -906,63 +907,6 @@ try_err(int loop_err, int errval)
return -1; return -1;
} }
/* portable rename(). WARNING:
* not powercut-safe. do this to
* use system rename:
* #define SYS_RENAME 1
*
* written academically, but in reality,
* nearly all unix systems have rename()
*/
int
x_i_rename(const char *src, const char *dst)
{
#if defined(SYS_RENAME) &&\
SYS_RENAME > 0
return rename(src, dst);
#else
int sfd, dirfd;
ssize_t r;
char buf[8192];
sfd = open(src, O_RDONLY);
if (sfd < 0)
return -1;
dirfd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, 0600);
if (dirfd < 0) {
x_i_close(sfd);
return -1;
}
while ((r = read(sfd, buf, sizeof(buf))) > 0) {
ssize_t w = write(dirfd, buf, r);
if (w != r) {
x_i_close(sfd);
x_i_close(dirfd);
return -1;
}
}
if (r < 0) {
x_i_close(sfd);
x_i_close(dirfd);
return -1;
}
x_i_fsync(dirfd);
x_i_close(sfd);
x_i_close(dirfd);
if (unlink(src) < 0)
return -1;
return 0;
#endif
}
int int
x_i_close(int fd) x_i_close(int fd)
{ {

View File

@@ -79,7 +79,7 @@ copy_gbe(void)
if (f->gbe_file_size == SIZE_8KB) if (f->gbe_file_size == SIZE_8KB)
return; return;
x_v_memcpy(f->buf + (unsigned long)GBE_PART_SIZE, memcpy(f->buf + (unsigned long)GBE_PART_SIZE,
f->buf + (unsigned long)(f->gbe_file_size >> 1), f->buf + (unsigned long)(f->gbe_file_size >> 1),
(unsigned long)GBE_PART_SIZE); (unsigned long)GBE_PART_SIZE);
} }
@@ -135,7 +135,7 @@ read_file(void)
if (_r < 0) if (_r < 0)
err(errno, "%s: read failed (cmp)", f->tname); err(errno, "%s: read failed (cmp)", f->tname);
if (x_i_memcmp(f->buf, f->bufcmp, f->gbe_file_size) != 0) if (memcmp(f->buf, f->bufcmp, f->gbe_file_size) != 0)
err(errno, "%s: %s: read contents differ (pre-test)", err(errno, "%s: %s: read contents differ (pre-test)",
f->fname, f->tname); f->fname, f->tname);
} }
@@ -331,7 +331,7 @@ check_written_part(unsigned long p)
f->rw_check_err_read[p] = f->io_err_gbe = 1; f->rw_check_err_read[p] = f->io_err_gbe = 1;
else if ((unsigned long)rval != gbe_rw_size) else if ((unsigned long)rval != gbe_rw_size)
f->rw_check_partial_read[p] = f->io_err_gbe = 1; f->rw_check_partial_read[p] = f->io_err_gbe = 1;
else if (x_i_memcmp(mem_offset, f->pad, gbe_rw_size) != 0) else if (memcmp(mem_offset, f->pad, gbe_rw_size) != 0)
f->rw_check_bad_part[p] = f->io_err_gbe = 1; f->rw_check_bad_part[p] = f->io_err_gbe = 1;
if (f->rw_check_err_read[p] || if (f->rw_check_err_read[p] ||
@@ -435,7 +435,7 @@ gbe_mv(void)
saved_errno = errno; saved_errno = errno;
rval = x_i_rename(f->tname, f->fname); rval = rename(f->tname, f->fname);
if (rval > -1) { if (rval > -1) {
/* /*
@@ -490,7 +490,7 @@ gbe_mv(void)
if (x_i_close(dest_fd) == -1) if (x_i_close(dest_fd) == -1)
goto ret_gbe_mv; goto ret_gbe_mv;
if (x_i_rename(dest_tmp, f->fname) == -1) if (rename(dest_tmp, f->fname) == -1)
goto ret_gbe_mv; goto ret_gbe_mv;
if (fsync_dir(f->fname) < 0) { if (fsync_dir(f->fname) < 0) {

View File

@@ -236,7 +236,7 @@ getnvmprogname(void)
rval = x->argv0; rval = x->argv0;
} }
p = x_c_strrchr(rval, '/'); p = strrchr(rval, '/');
if (p) if (p)
return p + 1; return p + 1;

View File

@@ -81,43 +81,3 @@ xstrxlen(const char *scmp, unsigned long maxlen)
return xstr_index; return xstr_index;
} }
char *
x_c_strrchr(const char *s, int c)
{
const char *p = NULL;
for ( ; *s; s++)
if (*s == (char)c)
p = s;
if (c == '\0')
return (char *)s;
return (char *)p;
}
void *
x_v_memcpy(void *dst, const void *src, unsigned long n)
{
unsigned char *d = (unsigned char *)dst;
const unsigned char *s = (const unsigned char *)src;
while (n--)
*d++ = *s++;
return dst;
}
int
x_i_memcmp(const void *a, const void *b, unsigned long n)
{
const unsigned char *pa = (const unsigned char *)a;
const unsigned char *pb = (const unsigned char *)b;
for ( ; n--; ++pa, ++pb)
if (*pa != *pb)
return *pa - *pb;
return 0;
}