From 1af238a4f95c0c4eed717aaa4876ae0e982f697b Mon Sep 17 00:00:00 2001 From: Andrew Bettison Date: Fri, 25 May 2012 11:52:49 +0930 Subject: [PATCH] Rhizome extract commands handle missing pathnames properly --- commandline.c | 6 +++--- rhizome_database.c | 33 +++++++++++++++++---------------- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/commandline.c b/commandline.c index 061799d8..15ebad18 100644 --- a/commandline.c +++ b/commandline.c @@ -1236,7 +1236,7 @@ int app_rhizome_extract_manifest(int argc, const char *const *argv, struct comma { const char *manifestid, *manifestpath; if (cli_arg(argc, argv, o, "manifestid", &manifestid, cli_manifestid, NULL) - || cli_arg(argc, argv, o, "manifestpath", &manifestpath, NULL, "") == -1) + || cli_arg(argc, argv, o, "manifestpath", &manifestpath, NULL, NULL) == -1) return -1; /* Ensure the Rhizome database exists and is open */ if (create_serval_instance_dir() == -1) @@ -1249,14 +1249,14 @@ int app_rhizome_extract_manifest(int argc, const char *const *argv, struct comma switch (ret) { case 0: ret = 1; break; case 1: ret = 0; - if (manifestpath[0]) { + if (manifestpath) { /* If the manifest has been read in from database, the blob is there, and we can lie and say we are finalised and just want to write it out. XXX really should have a dirty/clean flag, so that write works is clean but not finalised. */ m->finalised=1; if (rhizome_write_manifest_file(m, manifestpath) == -1) - ret = WHY("Could not overwrite manifest file."); + ret = -1; } break; case -1: break; diff --git a/rhizome_database.c b/rhizome_database.c index 88f77b6e..0ca90018 100644 --- a/rhizome_database.c +++ b/rhizome_database.c @@ -1066,22 +1066,23 @@ int rhizome_retrieve_file(const char *fileid, const char *filepath) if (fileblobsize != length) ret = WHY("File length does not match blob size"); else { - int fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0775); - if (fd == -1) { - ret = WHYF("Cannot open %s for write/create", filepath); - WHY_perror("open"); - } else if (write(fd, fileblob, length) != length) { - ret = WHYF("Error writing %lld bytes to %s ", (long long) length, filepath); - WHY_perror("write"); - } else if (close(fd) == -1) { - ret = WHYF("Error flushing to %s ", filepath); - WHY_perror("close"); - } else { - ret = 1; - cli_puts("filehash"); cli_delim(":"); - cli_puts((const char *)sqlite3_column_text(statement, 0)); cli_delim("\n"); - cli_puts("filesize"); cli_delim(":"); - cli_printf("%lld", length); cli_delim("\n"); + cli_puts("filehash"); cli_delim(":"); + cli_puts((const char *)sqlite3_column_text(statement, 0)); cli_delim("\n"); + cli_puts("filesize"); cli_delim(":"); + cli_printf("%lld", length); cli_delim("\n"); + ret = 1; + if (filepath) { + int fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0775); + if (fd == -1) { + ret = WHYF("Cannot open %s for write/create", filepath); + WHY_perror("open"); + } else if (write(fd, fileblob, length) != length) { + ret = WHYF("Error writing %lld bytes to %s ", (long long) length, filepath); + WHY_perror("write"); + } else if (close(fd) == -1) { + ret = WHYF("Error flushing to %s ", filepath); + WHY_perror("close"); + } } } break;