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