From d81c9133fa5b40a33b0295c3f9d29add4b78476f Mon Sep 17 00:00:00 2001 From: Dima Krasner Date: Mon, 30 Sep 2024 20:06:27 +0300 Subject: [PATCH] skip msync() if nothing was changed --- woof-code/rootfs-petbuilds/psnapcp/psnapcp.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/woof-code/rootfs-petbuilds/psnapcp/psnapcp.c b/woof-code/rootfs-petbuilds/psnapcp/psnapcp.c index c01e89f967b..09f08d1db1b 100644 --- a/woof-code/rootfs-petbuilds/psnapcp/psnapcp.c +++ b/woof-code/rootfs-petbuilds/psnapcp/psnapcp.c @@ -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; @@ -75,14 +75,18 @@ 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); @@ -90,7 +94,7 @@ int main(int argc, char *argv[]) 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);