Verify output dir before transmission.
authorThadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
Fri, 18 Mar 2016 13:22:17 +0000 (10:22 -0300)
committerGabriel F. T. Gomes <gabriel@gftg.com.br>
Sun, 3 Apr 2016 20:14:09 +0000 (17:14 -0300)
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 <gabriel@gftg.com.br>
rnetclient.c

index 41e57b3..5d56ac1 100644 (file)
@@ -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));