aboutsummaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2024-02-19 23:04:50 +0100
committerXavier Del Campo Romero <xavi.dcr@tutanota.com>2024-02-19 23:35:08 +0100
commita578ad6537320e562ce936bf70426ab2820ddaad (patch)
treee6bceccbe2e6b5bd1523904da8a8bf5586e4c231 /main.c
parentf6b84b765d6fa4d95aae5501fedca5cd8903e224 (diff)
downloadslcl-a578ad6537320e562ce936bf70426ab2820ddaad.tar.gz
main.c: Use fstat(2) on move_file
This allows to reuse the same file descriptor to both open(2) and fstat(2) the file.
Diffstat (limited to 'main.c')
-rw-r--r--main.c54
1 files changed, 30 insertions, 24 deletions
diff --git a/main.c b/main.c
index d76b225..cdb1aed 100644
--- a/main.c
+++ b/main.c
@@ -1057,23 +1057,23 @@ end:
static int move_file(const char *const old, const char *const new)
{
int ret = -1;
- FILE *const f = fopen(old, "rb");
- const int fd = open(new, O_WRONLY | O_CREAT, 0600);
+ const int fd_old = open(old, O_RDONLY),
+ fd_new = open(new, O_WRONLY | O_CREAT, 0600);
struct stat sb;
- if (!f)
+ if (fd_old < 0)
{
- fprintf(stderr, "%s: fopen(3): %s\n", __func__, strerror(errno));
+ fprintf(stderr, "%s: open(2) fd_old: %s\n", __func__, strerror(errno));
goto end;
}
- else if (fd < 0)
+ else if (fd_new < 0)
{
fprintf(stderr, "%s: open(2): %s\n", __func__, strerror(errno));
goto end;
}
- else if (stat(old, &sb))
+ else if (fstat(fd_old, &sb))
{
- fprintf(stderr, "%s: stat(2): %s\n", __func__, strerror(errno));
+ fprintf(stderr, "%s: fstat(2): %s\n", __func__, strerror(errno));
goto end;
}
@@ -1082,24 +1082,30 @@ static int move_file(const char *const old, const char *const new)
char buf[BUFSIZ];
const off_t left = sb.st_size - i;
const size_t rem = left > sizeof buf ? sizeof buf : left;
- ssize_t w;
+ const ssize_t r = read(fd_old, buf, rem);
- if (!fread(buf, rem, 1, f))
- {
- fprintf(stderr, "%s: fread(3) failed, feof=%d, ferror=%d\n",
- __func__, feof(f), ferror(f));
- goto end;
- }
- else if ((w = write(fd, buf, rem)) < 0)
+ if (r < 0)
{
- fprintf(stderr, "%s: write(2): %s\n", __func__, strerror(errno));
+ fprintf(stderr, "%s: read(2): %s\n", __func__, strerror(errno));
goto end;
}
- else if (w != rem)
+
+ size_t wrem = r;
+ const void *p = buf;
+
+ while (wrem)
{
- fprintf(stderr, "%s: write(2): expected to write %zu bytes, "
- "only %ju written\n", __func__, rem, (intmax_t)w);
- goto end;
+ const ssize_t w = write(fd_new, p, wrem);
+
+ if (w < 0)
+ {
+ fprintf(stderr, "%s: write(2): %s\n",
+ __func__, strerror(errno));
+ goto end;
+ }
+
+ p = (const char *)p + w;
+ wrem -= w;
}
i += rem;
@@ -1108,15 +1114,15 @@ static int move_file(const char *const old, const char *const new)
ret = 0;
end:
- if (fd >= 0 && close(fd))
+ if (fd_old >= 0 && close(fd_old))
{
- fprintf(stderr, "%s: close(2): %s\n", __func__, strerror(errno));
+ fprintf(stderr, "%s: close(2) fd_old: %s\n", __func__, strerror(errno));
ret = -1;
}
- if (f && fclose(f))
+ if (fd_new >= 0 && close(fd_new))
{
- fprintf(stderr, "%s: fclose(3): %s\n", __func__, strerror(errno));
+ fprintf(stderr, "%s: close(2) fd_new: %s\n", __func__, strerror(errno));
ret = -1;
}