int64_t do_du (const char *path) { int r; int64_t rv; CLEANUP_FREE char *out = NULL, *err = NULL, *buf = NULL; /* Make the path relative to /sysroot. */ buf = sysroot_path (path); if (!buf) { reply_with_perror ("malloc"); return -1; } pulse_mode_start (); r = command (&out, &err, str_du, "-s", buf, NULL); if (r == -1) { pulse_mode_cancel (); reply_with_error ("%s: %s", path, err); return -1; } if (sscanf (out, "%"SCNi64, &rv) != 1) { pulse_mode_cancel (); reply_with_error ("%s: could not read output: %s", path, out); return -1; } pulse_mode_end (); return rv; }
static char * checksum (const char *csumtype, int fd) { const char *program; char *out; CLEANUP_FREE char *err = NULL; int flags, r; size_t len; program = program_of_csum (csumtype); if (program == NULL) return NULL; pulse_mode_start (); flags = COMMAND_FLAG_CHROOT_COPY_FILE_TO_STDIN | fd; r = commandf (&out, &err, flags, program, NULL); if (r == -1) { pulse_mode_cancel (); reply_with_error ("%s: %s", program, err); free (out); return NULL; } /* Split it at the first whitespace. */ len = strcspn (out, " \t\n"); out[len] = '\0'; pulse_mode_end (); return out; /* Caller frees. */ }
void pulse_mode_end (void) { pulse_mode_cancel (); /* Cancel the itimer. */ notify_progress (1, 1); }
static int cpmv_cmd (const char *cmd, const char *flags, const char *src, const char *dest) { CLEANUP_FREE char *srcbuf = NULL, *destbuf = NULL; CLEANUP_FREE char *err = NULL; int r; srcbuf = sysroot_path (src); if (srcbuf == NULL) { reply_with_perror ("malloc"); return -1; } destbuf = sysroot_path (dest); if (destbuf == NULL) { reply_with_perror ("malloc"); return -1; } pulse_mode_start (); if (flags) r = command (NULL, &err, cmd, flags, srcbuf, destbuf, NULL); else r = command (NULL, &err, cmd, srcbuf, destbuf, NULL); if (r == -1) { pulse_mode_cancel (); reply_with_error ("%s", err); return -1; } pulse_mode_end (); return 0; }
/* Takes optional arguments, consult optargs_bitmask. */ static int copy (const char *src, const char *src_display, const char *dest, const char *dest_display, int wrflags, int wrmode, int64_t srcoffset, int64_t destoffset, int64_t size, int sparse) { int64_t saved_size = size; int src_fd, dest_fd; char buf[BUFSIZ]; size_t n; ssize_t r; int err; if ((optargs_bitmask & GUESTFS_COPY_DEVICE_TO_DEVICE_SRCOFFSET_BITMASK)) { if (srcoffset < 0) { reply_with_error ("srcoffset is negative"); return -1; } } else srcoffset = 0; if ((optargs_bitmask & GUESTFS_COPY_DEVICE_TO_DEVICE_DESTOFFSET_BITMASK)) { if (destoffset < 0) { reply_with_error ("destoffset is negative"); return -1; } } else destoffset = 0; if ((optargs_bitmask & GUESTFS_COPY_DEVICE_TO_DEVICE_SIZE_BITMASK)) { if (size < 0) { reply_with_error ("size is negative"); return -1; } } else size = -1; if (! (optargs_bitmask & GUESTFS_COPY_DEVICE_TO_DEVICE_SPARSE_BITMASK)) sparse = 0; /* Open source and destination. */ src_fd = open (src, O_RDONLY|O_CLOEXEC); if (src_fd == -1) { reply_with_perror ("%s", src_display); return -1; } if (srcoffset > 0 && lseek (src_fd, srcoffset, SEEK_SET) == (off_t) -1) { reply_with_perror ("lseek: %s", src_display); close (src_fd); return -1; } dest_fd = open (dest, wrflags, wrmode); if (dest_fd == -1) { reply_with_perror ("%s", dest_display); close (src_fd); return -1; } if (destoffset > 0 && lseek (dest_fd, destoffset, SEEK_SET) == (off_t) -1) { reply_with_perror ("lseek: %s", dest_display); close (src_fd); close (dest_fd); return -1; } if (size == -1) pulse_mode_start (); while (size != 0) { /* Calculate bytes to copy. */ if (size == -1 || size > (int64_t) sizeof buf) n = sizeof buf; else n = size; r = read (src_fd, buf, n); if (r == -1) { err = errno; if (size == -1) pulse_mode_cancel (); errno = err; reply_with_perror ("read: %s", src_display); close (src_fd); close (dest_fd); return -1; } if (r == 0) { if (size == -1) /* if size == -1, this is normal end of loop */ break; reply_with_error ("%s: input too short", src_display); close (src_fd); close (dest_fd); return -1; } if (sparse && is_zero (buf, r)) { if (lseek (dest_fd, r, SEEK_CUR) == -1) { err = errno; if (size == -1) pulse_mode_cancel (); errno = err; reply_with_perror ("%s: seek (because of sparse flag)", dest_display); close (src_fd); close (dest_fd); return -1; } goto sparse_skip; } if (xwrite (dest_fd, buf, r) == -1) { err = errno; if (size == -1) pulse_mode_cancel (); errno = err; reply_with_perror ("%s: write", dest_display); close (src_fd); close (dest_fd); return -1; } sparse_skip: if (size != -1) { size -= r; notify_progress ((uint64_t) (saved_size - size), (uint64_t) saved_size); } } if (size == -1) pulse_mode_end (); if (close (src_fd) == -1) { reply_with_perror ("close: %s", src_display); close (dest_fd); return -1; } if (close (dest_fd) == -1) { reply_with_perror ("close: %s", dest_display); return -1; } return 0; }