int main(int argc, char *argv[]) { _cleanup_close_ int sfd = -1, dfd = -1; int r; if (argc != 3) { log_error("Needs two arguments."); return EXIT_FAILURE; } sfd = open(argv[1], O_RDONLY|O_CLOEXEC|O_NOCTTY); if (sfd < 0) { log_error_errno(errno, "Can't open source file: %m"); return EXIT_FAILURE; } dfd = open(argv[2], O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, 0666); if (dfd < 0) { log_error_errno(errno, "Can't open destination file: %m"); return EXIT_FAILURE; } r = qcow2_convert(sfd, dfd); if (r < 0) { log_error_errno(r, "Failed to unpack: %m"); return EXIT_FAILURE; } return EXIT_SUCCESS; }
static int raw_pull_maybe_convert_qcow2(RawPull *i) { _cleanup_close_ int converted_fd = -1; _cleanup_free_ char *t = NULL; int r; assert(i); assert(i->raw_job); r = qcow2_detect(i->raw_job->disk_fd); if (r < 0) return log_error_errno(r, "Failed to detect whether this is a QCOW2 image: %m"); if (r == 0) return 0; /* This is a QCOW2 image, let's convert it */ r = tempfn_random(i->final_path, NULL, &t); if (r < 0) return log_oom(); converted_fd = open(t, O_RDWR|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0664); if (converted_fd < 0) return log_error_errno(errno, "Failed to create %s: %m", t); r = chattr_fd(converted_fd, FS_NOCOW_FL, FS_NOCOW_FL); if (r < 0) log_warning_errno(errno, "Failed to set file attributes on %s: %m", t); log_info("Unpacking QCOW2 file."); r = qcow2_convert(i->raw_job->disk_fd, converted_fd); if (r < 0) { unlink(t); return log_error_errno(r, "Failed to convert qcow2 image: %m"); } (void) unlink(i->temp_path); free(i->temp_path); i->temp_path = t; t = NULL; safe_close(i->raw_job->disk_fd); i->raw_job->disk_fd = converted_fd; converted_fd = -1; return 1; }