summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>2016-03-18 10:22:17 -0300
committerGabriel F. T. Gomes <gabriel@gftg.com.br>2016-04-03 17:14:09 -0300
commit046e3cc9023d4d14005b85be600fb21e1ab3d561 (patch)
tree6c9e6a2909596b35bae165c0a96357598ffb60d4
parent6742a53bd88c11e2f8c3231fdaf0f86ba133646d (diff)
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 <gabriel@gftg.com.br>
-rw-r--r--rnetclient.c41
1 files 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));