From 046e3cc9023d4d14005b85be600fb21e1ab3d561 Mon Sep 17 00:00:00 2001 From: Thadeu Lima de Souza Cascardo Date: Fri, 18 Mar 2016 10:22:17 -0300 Subject: [PATCH] Verify output dir before transmission. In case the output dir is not valid, we should exit before transmitting the file. It's too late to realize the directory is not valid when the receipt is ready to be saved. Reviewed-by: Gabriel F. T. Gomes --- rnetclient.c | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/rnetclient.c b/rnetclient.c index 41e57b3..5d56ac1 100644 --- a/rnetclient.c +++ b/rnetclient.c @@ -346,27 +346,12 @@ static int rnet_recv(gnutls_session_t session, struct rnet_message **message) static void save_rec_file(char *cpf, char *buffer, int len, const struct rnetclient_args *args) { int fd; - char cwd[PATH_MAX]; char *path, *fname, *tmp; size_t fname_len; ssize_t r; - /* If the user provided the output directory where she wishes - to save the receipt, then we use it. Otherwise, we save - the file in the current working directory (CWD). */ - if (args->output_dir == NULL) - path = getcwd(cwd, PATH_MAX); - else { - struct stat st; - if (stat(args->output_dir, &st) < 0) { - fprintf(stderr, "Could not stat directory \"%s\": %s\n", args->output_dir, strerror(errno)); - return; - } - if (!S_ISDIR(st.st_mode)) { - fprintf(stderr, "Error: \"%s\" is a not a directory.\n", args->output_dir); - return; - } - path = args->output_dir; - } + + path = args->output_dir; + /* Now it's time to decide which filename to write. We use the declaration's filename as a base layout, because the proprietary version of the IRPF program only recognizes @@ -449,6 +434,7 @@ int main(int argc, char **argv) int finish = 0; char *cpf; error_t err; + char cwd[PATH_MAX]; /* Parsing the command line arguments. The argp_parse function calls exit() if there is some error during the @@ -463,6 +449,25 @@ int main(int argc, char **argv) if (err != 0) fprintf(stderr, "internal error while parsing command line arguments."); + /* If the user provided the output directory where she wishes + to save the receipt, then we use it. Otherwise, we save + the file in the current working directory (CWD). */ + if (rnet_args.output_dir == NULL) { + rnet_args.output_dir = getcwd(cwd, PATH_MAX); + } else { + struct stat st; + if (stat(rnet_args.output_dir, &st) < 0) { + fprintf(stderr, "Could not stat directory \"%s\": %s\n", + rnet_args.output_dir, strerror(errno)); + exit(1); + } + if (!S_ISDIR(st.st_mode)) { + fprintf(stderr, "Error: \"%s\" is a not a directory.\n", + rnet_args.output_dir); + exit(1); + } + } + decfile = rnet_decfile_open(rnet_args.input_file); if (!decfile) { fprintf(stderr, "could not parse file \"%s\": %s\n", rnet_args.input_file, strerror(errno)); -- 2.20.1