file_write: Kill ->fd of struct private_file_write_data.
[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 <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
60 if (conf->filename_given)
61 filename = conf->filename_arg;
62 else
63 filename = random_filename();
64 wn->private_data = pfwd;
65 pfwd->fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
66 if (!conf->filename_given)
67 free(filename);
68 if (pfwd->fd >= 0)
69 return 1;
70 free(pfwd);
71 return -E_FW_OPEN;
72 }
73
74 static void file_write_pre_select_btr(struct sched *s, struct task *t)
75 {
76 struct writer_node *wn = container_of(t, struct writer_node, task);
77 struct private_file_write_data *pfwd = wn->private_data;
78 int ret;
79
80 t->error = 0;
81 ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
82 if (ret > 0) {
83 para_fd_set(pfwd->fd, &s->wfds, &s->max_fileno);
84 } else if (ret < 0) {
85 s->timeout.tv_sec = 0;
86 s->timeout.tv_usec = 1;
87 }
88 }
89
90 static void file_write_close(struct writer_node *wn)
91 {
92 struct private_file_write_data *pfwd = wn->private_data;
93
94 close(pfwd->fd);
95 free(pfwd);
96 }
97
98 static void file_write_post_select_btr(__a_unused struct sched *s,
99 struct task *t)
100 {
101 struct writer_node *wn = container_of(t, struct writer_node, task);
102 struct private_file_write_data *pfwd = wn->private_data;
103 struct btr_node *btrn = wn->btrn;
104 int ret;
105 char *buf;
106 size_t bytes;
107
108 t->error = 0;
109 ret = btr_node_status(btrn, wn->min_iqs, BTR_NT_LEAF);
110 if (ret <= 0)
111 goto out;
112 if (!FD_ISSET(pfwd->fd, &s->wfds))
113 return;
114 bytes = btr_next_buffer(btrn, &buf);
115 assert(bytes > 0);
116 //PARA_INFO_LOG("writing %zu\n", bytes);
117 ret = write(pfwd->fd, buf, bytes);
118 if (ret < 0)
119 goto out;
120 btr_consume(btrn, ret);
121 out:
122 if (ret < 0)
123 btr_remove_node(btrn);
124 t->error = ret;
125 }
126
127 __malloc static void *file_write_parse_config(const char *options)
128 {
129 struct file_write_args_info *conf
130 = para_calloc(sizeof(struct file_write_args_info));
131 int ret = file_cmdline_parser_string(options, conf, "file_write");
132
133 PARA_INFO_LOG("conf->filename_given: %d\n", conf->filename_given);
134 if (!ret)
135 return conf;
136 free(conf);
137 return NULL;
138 }
139
140 static void file_write_free_config(void *conf)
141 {
142 file_cmdline_parser_free(conf);
143 }
144
145 /** the init function of the file writer */
146 void file_write_init(struct writer *w)
147 {
148 struct file_write_args_info dummy;
149
150 file_cmdline_parser_init(&dummy);
151 w->open = file_write_open;
152 w->pre_select_btr = file_write_pre_select_btr;
153 w->post_select_btr = file_write_post_select_btr;
154 w->parse_config = file_write_parse_config;
155 w->free_config = file_write_free_config;
156 w->close = file_write_close;
157 w->shutdown = NULL; /* nothing to do */
158 w->help = (struct ggo_help) {
159 .short_help = file_write_args_info_help,
160 .detailed_help = file_write_args_info_detailed_help
161 };
162 file_cmdline_parser_free(&dummy);
163 }