e03cf8f65acea814d5dc62a9e450b056527b06f4
[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
12 #include "para.h"
13 #include "list.h"
14 #include "sched.h"
15 #include "ggo.h"
16 #include "write.h"
17 #include "string.h"
18 #include "fd.h"
19 #include "file_write.cmdline.h"
20 #include "error.h"
21
22 /** data specific to the file writer */
23 struct private_file_write_data {
24 /** the file descriptor of the output file */
25 int fd;
26 /** non-zero if \a fd was added to the write fd set */
27 int check_fd;
28 };
29
30 static int file_write_open(struct writer_node *wn)
31 {
32 struct private_file_write_data *pfwd = para_calloc(
33 sizeof(struct private_file_write_data));
34 struct file_write_args_info *conf = wn->conf;
35 char *filename;
36 if (conf->filename_given)
37 filename = conf->filename_arg;
38 else {
39 char *tmp = para_tmpname(), *home = para_homedir();
40 filename = make_message("%s/.paraslash/%s", home, tmp);
41 free(home);
42 free(tmp);
43 }
44 wn->private_data = pfwd;
45 pfwd->fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
46 if (!conf->filename_given)
47 free(filename);
48 if (pfwd->fd >= 0)
49 return 1;
50 free(pfwd);
51 return -E_FW_OPEN;
52 }
53
54 static int file_write_pre_select(struct sched *s, struct writer_node *wn)
55 {
56 struct private_file_write_data *pfwd = wn->private_data;
57 struct writer_node_group *wng = wn->wng;
58
59 pfwd->check_fd = 0;
60 if (pfwd->fd <= 0)
61 return -E_FW_NO_FILE;
62 if (!*wng->loaded)
63 return 1;
64 para_fd_set(pfwd->fd, &s->wfds, &s->max_fileno);
65 pfwd->check_fd = 1;
66 return 1;
67 }
68
69 static int file_write_post_select(struct sched *s, struct writer_node *wn)
70 {
71 struct private_file_write_data *pfwd = wn->private_data;
72 struct writer_node_group *wng = wn->wng;
73 int ret;
74
75 if (!pfwd->check_fd)
76 return 1;
77 if (*wng->loaded <= wn->written)
78 return 1;
79 if (!FD_ISSET(pfwd->fd, &s->wfds))
80 return 1;
81 // PARA_INFO_LOG("writing %zd\n", *wng->loaded);
82 ret = write(pfwd->fd, *wng->bufp + wn->written,
83 *wng->loaded - wn->written);
84 if (ret < 0)
85 return -E_FW_WRITE;
86 wn->written += ret;
87 return 1;
88 }
89
90 static void file_write_close(struct writer_node *wn)
91 {
92 struct private_file_write_data *pfwd = wn->private_data;
93 close(pfwd->fd);
94 free(pfwd);
95 }
96
97 __malloc static void *file_write_parse_config(const char *options)
98 {
99 struct file_write_args_info *conf
100 = para_calloc(sizeof(struct file_write_args_info));
101 int ret = file_cmdline_parser_string(options, conf, "file_write");
102
103 PARA_INFO_LOG("conf->filename_given: %d\n", conf->filename_given);
104 if (!ret)
105 return conf;
106 free(conf);
107 return NULL;
108 }
109
110 /** the init function of the file writer */
111 void file_write_init(struct writer *w)
112 {
113 struct file_write_args_info dummy;
114
115 file_cmdline_parser_init(&dummy);
116 w->open = file_write_open;
117 w->pre_select = file_write_pre_select;
118 w->post_select = file_write_post_select;
119 w->parse_config = file_write_parse_config;
120 w->close = file_write_close;
121 w->shutdown = NULL; /* nothing to do */
122 w->help = (struct ggo_help) {
123 .short_help = file_write_args_info_help,
124 .detailed_help = file_write_args_info_detailed_help
125 };
126 file_cmdline_parser_free(&dummy);
127 }