Skip to content

lib/commonio.c: Use block I/O for backup copies#1565

Closed
cuiweixie wants to merge 1 commit intoshadow-maint:masterfrom
cuiweixie:opt
Closed

lib/commonio.c: Use block I/O for backup copies#1565
cuiweixie wants to merge 1 commit intoshadow-maint:masterfrom
cuiweixie:opt

Conversation

@cuiweixie
Copy link

Replace byte-by-byte backup copying in create_backup() with fread/fwrite buffering. This removes the long-standing TODO and keeps explicit cleanup paths for seek/read/write failures before flushing and syncing.

Replace byte-by-byte backup copying in create_backup() with fread/fwrite buffering. This removes the long-standing TODO and keeps explicit cleanup paths for seek/read/write failures before flushing and syncing.
Comment on lines -262 to +283
/* 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) {
Copy link
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
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.

@cuiweixie cuiweixie closed this Mar 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants