X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=file_write.c;h=6ba80194ae9362e5787a22d183c52f8f1072780f;hp=92777d7ddc4af81b173b5017559af6ea1a1af407;hb=cc2a6158eef6957769fd5739446916d3e5413ae1;hpb=318fbe90e08b6d9c5e781d9b517b670c5a1a04d7 diff --git a/file_write.c b/file_write.c index 92777d7d..6ba80194 100644 --- a/file_write.c +++ b/file_write.c @@ -6,13 +6,17 @@ /** \file file_write.c simple output plugin for testing purposes */ +#include #include #include +#include +#include #include "para.h" #include "list.h" #include "sched.h" #include "ggo.h" +#include "buffer_tree.h" #include "write.h" #include "string.h" #include "fd.h" @@ -27,20 +31,37 @@ struct private_file_write_data { int check_fd; }; +/* + * Get a random filename. + * + * This is by no means a secure way to create temporary files in a hostile + * directory like \p /tmp. However, we use it only for creating temp files in + * ~/.paraslash, for which it is OK. Result must be freed by the caller. + */ +__must_check __malloc static char *random_filename(void) +{ + char *result, *home = para_homedir(); + struct timeval tv; + + gettimeofday(&tv, NULL); + srandom(tv.tv_usec); + result = make_message("%s/.paraslash/%08lu", home, + para_random(99999999)); + free(home); + return result; +} + static int file_write_open(struct writer_node *wn) { struct private_file_write_data *pfwd = para_calloc( sizeof(struct private_file_write_data)); struct file_write_args_info *conf = wn->conf; char *filename; + if (conf->filename_given) filename = conf->filename_arg; - else { - char *tmp = para_tmpname(), *home = para_homedir(); - filename = make_message("%s/.paraslash/%s", home, tmp); - free(home); - free(tmp); - } + else + filename = random_filename(); wn->private_data = pfwd; pfwd->fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); if (!conf->filename_given) @@ -123,4 +144,5 @@ void file_write_init(struct writer *w) .short_help = file_write_args_info_help, .detailed_help = file_write_args_info_detailed_help }; + file_cmdline_parser_free(&dummy); }