Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions lib/commonio.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ static int create_backup (const char *name, FILE * fp)
struct stat sb;
struct utimbuf ub;
FILE *bkfp;
int c;

stprintf_a(tmpf, "%s.cioXXXXXX", name);
if (fstat (fileno (fp), &sb) != 0) {
Expand All @@ -259,16 +258,29 @@ static int create_backup (const char *name, FILE * fp)
return -1;
}

/* TODO: faster copy, not one-char-at-a-time. --marekm */
c = 0;
if (fseek (fp, 0, SEEK_SET) == 0) {
while ((c = getc (fp)) != EOF) {
if (putc (c, bkfp) == EOF) {
break;
if (fseek (fp, 0, SEEK_SET) != 0) {
(void) fclose (bkfp);
unlink(tmpf);
return -1;
}
{
char buf[BUFSIZ];
size_t n;

while ((n = fread (buf, 1, sizeof buf, fp)) > 0) {
if (fwrite (buf, 1, n, bkfp) != n) {
(void) fclose (bkfp);
unlink(tmpf);
return -1;
}
}
if (ferror (fp) != 0) {
(void) fclose (bkfp);
unlink(tmpf);
return -1;
}
}
if ((c != EOF) || (ferror (fp) != 0) || (fflush (bkfp) != 0)) {
if (fflush (bkfp) != 0) {
Comment on lines -262 to +283
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you notice any performance issues or issues of other kind? Otherwise, I see this as a readability pessimization, which has a high chance of having bugs in there.

Copy link
Copy Markdown
Collaborator

@alejandro-colomar alejandro-colomar Mar 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(void) fclose (bkfp);
unlink(tmpf);
return -1;
Expand Down