introduce input_eof and ouput_eof
[paraslash.git] / file_writer.c
1 /*
2  * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
3  *
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  *
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  *
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */
18
19 /** \file file_writer.c simple output plugin for testing purposes */
20
21 #include "para.h"
22 #include "list.h"
23 #include "sched.h"
24 #include "write.h"
25 #include "string.h"
26 #include "fd.h"
27 #include "error.h"
28
29 /** data specific to the file writer */
30 struct private_file_writer_data {
31 /** the file descriptor of the output file */
32 int fd;
33 int check_fd;
34 };
35
36 static int file_writer_open(struct writer_node *wn)
37 {
38         struct private_file_writer_data *pfwd = para_calloc(
39                 sizeof(struct private_file_writer_data));
40         char *tmp = para_tmpname(), *home = para_homedir(),
41                 *filename = make_message("%s/.paraslash/%s", home, tmp);
42
43         free(home);
44         free(tmp);
45         wn->private_data = pfwd;
46         pfwd->fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
47         free(filename);
48         if (pfwd->fd >= 0)
49                 return 8192;
50         free(pfwd);
51         return -E_FW_OPEN;
52 }
53
54 static int file_writer_write(char *data, size_t nbytes, struct writer_node *wn)
55 {
56         struct private_file_writer_data *pfwd = wn->private_data;
57         int ret = write(pfwd->fd, data, nbytes);
58         if (ret < 0)
59                 ret = -E_FW_WRITE;
60         return ret;
61 }
62
63 static void file_writer_pre_select(struct sched *s, struct task *t)
64 {
65         struct writer_node *wn = t->private_data;
66         struct private_file_writer_data *pfwd = wn->private_data;
67         struct writer_node_group *wng = wn->wng;
68
69 //      PARA_INFO_LOG("task %p check_fd: %d\n", t, pfwd->check_fd);
70         pfwd->check_fd = 0;
71         t->ret = -E_FW_NO_FILE;
72         if (pfwd->fd <= 0)
73                 return;
74         t->ret = 0;
75         if (!*wng->loaded)
76                 return;
77         t->ret = 1;
78         para_fd_set(pfwd->fd, &s->wfds, &s->max_fileno);
79         pfwd->check_fd = 1;
80 }
81
82 static void file_writer_post_select(struct sched *s, struct task *t)
83 {
84         struct writer_node *wn = t->private_data;
85         struct private_file_writer_data *pfwd = wn->private_data;
86         struct writer_node_group *wng = wn->wng;
87
88         t->ret = 0;
89         if (!pfwd->check_fd)
90                 return;
91         if (!*wng->loaded)
92                 return;
93         if (!FD_ISSET(pfwd->fd, &s->wfds))
94                 return;
95 //      PARA_INFO_LOG("writing %zd\n", *wng->loaded);
96         t->ret = write(pfwd->fd, wng->buf, *wng->loaded);
97         if (t->ret < 0)
98                 t->ret = -E_FW_WRITE;
99 }
100
101 static void file_writer_close(struct writer_node *wn)
102 {
103         struct private_file_writer_data *pfwd = wn->private_data;
104         close(pfwd->fd);
105         free(pfwd);
106 }
107
108 /** the init function of the file writer */
109 void file_writer_init(struct writer *w)
110 {
111         w->open = file_writer_open;
112         w->write = file_writer_write;
113         w->pre_select = file_writer_pre_select;
114         w->post_select = file_writer_post_select;
115         w->close = file_writer_close;
116         w->shutdown = NULL; /* nothing to do */
117 }