Skip to content

Commit

Permalink
skip msync() if nothing was changed
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed Sep 30, 2024
1 parent b7217b2 commit d81c913
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions woof-code/rootfs-petbuilds/psnapcp/psnapcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

int main(int argc, char *argv[])
{
int src, dst;
int src, dst, sync = 0;
struct stat srcstat, dststat;
struct timeval times[2];
unsigned char *srcm, *dstm;
Expand Down Expand Up @@ -75,22 +75,26 @@ int main(int argc, char *argv[])
madvise(srcm, srcstat.st_size, MADV_SEQUENTIAL);
madvise(dstm, srcstat.st_size, MADV_SEQUENTIAL);

if (srcstat.st_size > 0 && dststat.st_size == 0)
if (srcstat.st_size > 0 && dststat.st_size == 0) {
memcpy(dstm, srcm, srcstat.st_size);
sync = 1;
}
else {
do {
chunk = srcstat.st_size - off >= CHUNK_SIZE ? CHUNK_SIZE : srcstat.st_size - off;

if (memcmp(&srcm[off], &dstm[off], chunk) != 0)
if (memcmp(&srcm[off], &dstm[off], chunk) != 0) {
memcpy(&dstm[off], &srcm[off], chunk);
sync = 1;
}

off += chunk;
} while (off < srcstat.st_size);
}

munmap(srcm, srcstat.st_size);

if (msync(dstm, srcstat.st_size, MS_SYNC) < 0) {
if (sync && msync(dstm, srcstat.st_size, MS_SYNC) < 0) {
fprintf(stderr, "Failed to flush changes to disk: %s\n", strerror(errno));
munmap(dstm, srcstat.st_size);
close(dst);
Expand Down

0 comments on commit d81c913

Please sign in to comment.