gui: Avoid unnecessary strdup's and replace newlines by spaces in curses output.
[paraslash.git] / file_write.c
1 /*
2  * Copyright (C) 2006-2009 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file file_write.c simple output plugin for testing purposes */
8
9 #include <sys/types.h>
10 #include <dirent.h>
11 #include <sys/time.h>
12
13 #include "para.h"
14 #include "list.h"
15 #include "sched.h"
16 #include "ggo.h"
17 #include "write.h"
18 #include "string.h"
19 #include "fd.h"
20 #include "file_write.cmdline.h"
21 #include "error.h"
22
23 /** data specific to the file writer */
24 struct private_file_write_data {
25         /** the file descriptor of the output file */
26         int fd;
27         /** non-zero if \a fd was added to the write fd set */
28         int check_fd;
29 };
30
31 /*
32  * Get a random filename.
33  *
34  * This is by no means a secure way to create temporary files in a hostile
35  * directory like \p /tmp. However, we use it only for creating temp files in
36  * ~/.paraslash, for which it is OK. Result must be freed by the caller.
37  */
38 __must_check __malloc static char *random_filename(void)
39 {
40         char *result, *home = para_homedir();
41         struct timeval tv;
42
43         gettimeofday(&tv, NULL);
44         srandom(tv.tv_usec);
45         result = make_message("%s/.paraslash/%08lu", home,
46                 para_random(99999999));
47         free(home);
48         return result;
49 }
50
51 static int file_write_open(struct writer_node *wn)
52 {
53         struct private_file_write_data *pfwd = para_calloc(
54                 sizeof(struct private_file_write_data));
55         struct file_write_args_info *conf = wn->conf;
56         char *filename;
57
58         if (conf->filename_given)
59                 filename = conf->filename_arg;
60         else
61                 filename = random_filename();
62         wn->private_data = pfwd;
63         pfwd->fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
64         if (!conf->filename_given)
65                 free(filename);
66         if (pfwd->fd >= 0)
67                 return 1;
68         free(pfwd);
69         return -E_FW_OPEN;
70 }
71
72 static int file_write_pre_select(struct sched *s, struct writer_node *wn)
73 {
74         struct private_file_write_data *pfwd = wn->private_data;
75         struct writer_node_group *wng = wn->wng;
76
77         pfwd->check_fd = 0;
78         if (pfwd->fd <= 0)
79                 return -E_FW_NO_FILE;
80         if (!*wng->loaded)
81                 return 1;
82         para_fd_set(pfwd->fd, &s->wfds, &s->max_fileno);
83         pfwd->check_fd = 1;
84         return 1;
85 }
86
87 static int file_write_post_select(struct sched *s, struct writer_node *wn)
88 {
89         struct private_file_write_data *pfwd = wn->private_data;
90         struct writer_node_group *wng = wn->wng;
91         int ret;
92
93         if (!pfwd->check_fd)
94                 return 1;
95         if (*wng->loaded <= wn->written)
96                 return 1;
97         if (!FD_ISSET(pfwd->fd, &s->wfds))
98                 return 1;
99 //      PARA_INFO_LOG("writing %zd\n", *wng->loaded);
100         ret = write(pfwd->fd, *wng->bufp + wn->written,
101                 *wng->loaded - wn->written);
102         if (ret < 0)
103                 return -E_FW_WRITE;
104         wn->written += ret;
105         return 1;
106 }
107
108 static void file_write_close(struct writer_node *wn)
109 {
110         struct private_file_write_data *pfwd = wn->private_data;
111         close(pfwd->fd);
112         free(pfwd);
113 }
114
115 __malloc static void *file_write_parse_config(const char *options)
116 {
117         struct file_write_args_info *conf
118                 = para_calloc(sizeof(struct file_write_args_info));
119         int ret = file_cmdline_parser_string(options, conf, "file_write");
120
121         PARA_INFO_LOG("conf->filename_given: %d\n", conf->filename_given);
122         if (!ret)
123                 return conf;
124         free(conf);
125         return NULL;
126 }
127
128 /** the init function of the file writer */
129 void file_write_init(struct writer *w)
130 {
131         struct file_write_args_info dummy;
132
133         file_cmdline_parser_init(&dummy);
134         w->open = file_write_open;
135         w->pre_select = file_write_pre_select;
136         w->post_select = file_write_post_select;
137         w->parse_config = file_write_parse_config;
138         w->close = file_write_close;
139         w->shutdown = NULL; /* nothing to do */
140         w->help = (struct ggo_help) {
141                 .short_help = file_write_args_info_help,
142                 .detailed_help = file_write_args_info_detailed_help
143         };
144 }