gui: Combine open_stat_pipe() and para_open_stat_pipe().
[paraslash.git] / file_write.c
1 /*
2 * Copyright (C) 2006-2010 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 <regex.h>
10 #include <sys/types.h>
11 #include <dirent.h>
12 #include <sys/time.h>
13 #include <stdbool.h>
14
15 #include "para.h"
16 #include "list.h"
17 #include "sched.h"
18 #include "ggo.h"
19 #include "buffer_tree.h"
20 #include "write.h"
21 #include "write_common.h"
22 #include "string.h"
23 #include "fd.h"
24 #include "file_write.cmdline.h"
25 #include "error.h"
26
27 /** Data specific to the file writer. */
28 struct private_file_write_data {
29 /** The file descriptor of the output file. */
30 int fd;
31 };
32
33 /*
34 * Get a random filename.
35 *
36 * This is by no means a secure way to create temporary files in a hostile
37 * directory like \p /tmp. However, we use it only for creating temp files in
38 * ~/.paraslash, for which it is OK. Result must be freed by the caller.
39 */
40 __must_check __malloc static char *random_filename(void)
41 {
42 char *result, *home = para_homedir();
43 struct timeval tv;
44
45 gettimeofday(&tv, NULL);
46 srandom(tv.tv_usec);
47 result = make_message("%s/.paraslash/%08lu", home,
48 para_random(99999999));
49 free(home);
50 return result;
51 }
52
53 static int file_write_open(struct writer_node *wn)
54 {
55 struct private_file_write_data *pfwd = para_calloc(
56 sizeof(struct private_file_write_data));
57 struct file_write_args_info *conf = wn->conf;
58 char *filename;
59 int ret;
60
61 if (conf->filename_given)
62 filename = conf->filename_arg;
63 else
64 filename = random_filename();
65 wn->private_data = pfwd;
66 ret = para_open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
67 if (!conf->filename_given)
68 free(filename);
69 if (ret < 0)
70 goto out;
71 pfwd->fd = ret;
72 ret = mark_fd_blocking(pfwd->fd);
73 if (ret >= 0)
74 return 1;
75 close(pfwd->fd);
76 out:
77 free(pfwd);
78 return ret;
79 }
80
81 static void file_write_pre_select(struct sched *s, struct task *t)
82 {
83 struct writer_node *wn = container_of(t, struct writer_node, task);
84 struct private_file_write_data *pfwd = wn->private_data;
85 int ret;
86
87 t->error = 0;
88 ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
89 if (ret > 0)
90 para_fd_set(pfwd->fd, &s->wfds, &s->max_fileno);
91 else if (ret < 0)
92 sched_min_delay(s);
93 }
94
95 static void file_write_close(struct writer_node *wn)
96 {
97 struct private_file_write_data *pfwd = wn->private_data;
98
99 close(pfwd->fd);
100 free(pfwd);
101 }
102
103 static void file_write_post_select(__a_unused struct sched *s,
104 struct task *t)
105 {
106 struct writer_node *wn = container_of(t, struct writer_node, task);
107 struct private_file_write_data *pfwd = wn->private_data;
108 struct btr_node *btrn = wn->btrn;
109 int ret;
110 char *buf;
111 size_t bytes;
112
113 t->error = 0;
114 ret = btr_node_status(btrn, wn->min_iqs, BTR_NT_LEAF);
115 if (ret <= 0)
116 goto out;
117 if (!FD_ISSET(pfwd->fd, &s->wfds))
118 return;
119 bytes = btr_next_buffer(btrn, &buf);
120 assert(bytes > 0);
121 //PARA_INFO_LOG("writing %zu\n", bytes);
122 ret = write(pfwd->fd, buf, bytes);
123 if (ret < 0)
124 goto out;
125 btr_consume(btrn, ret);
126 out:
127 if (ret < 0)
128 btr_remove_node(btrn);
129 t->error = ret;
130 }
131
132 __malloc static void *file_write_parse_config(const char *options)
133 {
134 struct file_write_args_info *conf
135 = para_calloc(sizeof(struct file_write_args_info));
136 int ret = file_cmdline_parser_string(options, conf, "file_write");
137
138 PARA_INFO_LOG("conf->filename_given: %d\n", conf->filename_given);
139 if (!ret)
140 return conf;
141 free(conf);
142 return NULL;
143 }
144
145 static void file_write_free_config(void *conf)
146 {
147 file_cmdline_parser_free(conf);
148 }
149
150 /** the init function of the file writer */
151 void file_write_init(struct writer *w)
152 {
153 struct file_write_args_info dummy;
154
155 file_cmdline_parser_init(&dummy);
156 w->open = file_write_open;
157 w->pre_select = file_write_pre_select;
158 w->post_select = file_write_post_select;
159 w->parse_config = file_write_parse_config;
160 w->free_config = file_write_free_config;
161 w->close = file_write_close;
162 w->shutdown = NULL; /* nothing to do */
163 w->help = (struct ggo_help) {
164 .short_help = file_write_args_info_help,
165 .detailed_help = file_write_args_info_detailed_help
166 };
167 file_cmdline_parser_free(&dummy);
168 }