write: Add --buffer-tree option.
[paraslash.git] / stdout.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 stdout.c Functions that deal with writing to stdout. */
8
9 #include <dirent.h> /* readdir() */
10 #include <assert.h>
11 #include <stdbool.h>
12
13 #include "para.h"
14 #include "list.h"
15 #include "sched.h"
16 #include "fd.h"
17 #include "error.h"
18 #include "stdout.h"
19 #include "buffer_tree.h"
20
21 /**
22  * The pre_select function of the stdout task.
23  *
24  * \param s The scheduler this task was registered to.
25  * \param t The task structure of the stdout task.
26  *
27  * This function is always successful. If there is data available in the input
28  * buffer, it adds \p STDOUT_FILENO to the write fd set of \a s.
29  */
30 static void stdout_pre_select(struct sched *s, struct task *t)
31 {
32         struct stdout_task *sot = container_of(t, struct stdout_task, task);
33
34         t->error = 0;
35         sot->check_fd = 0;
36         if (!*sot->loaded) {
37                 if (*sot->input_error < 0) {
38                         t->error = *sot->input_error;
39                         s->timeout.tv_sec = 0;
40                         s->timeout.tv_usec = 1;
41                 }
42                 return;
43         }
44         sot->check_fd = 1;
45         para_fd_set(STDOUT_FILENO, &s->wfds, &s->max_fileno);
46 }
47
48 static void stdout_pre_select_btr(struct sched *s, struct task *t)
49 {
50         struct stdout_task *sot = container_of(t, struct stdout_task, task);
51         size_t sz = btr_get_input_queue_size(sot->btrn);
52
53         t->error = 0;
54         sot->check_fd = 0;
55         if (sz == 0) {
56                 if (btr_no_parent(sot->btrn)) {
57                         t->error = -E_ORPHAN;
58                         btr_del_node(sot->btrn);
59                         s->timeout.tv_sec = 0;
60                         s->timeout.tv_usec = 1;
61                 }
62                 return;
63         }
64         sot->check_fd = 1;
65         para_fd_set(STDOUT_FILENO, &s->wfds, &s->max_fileno);
66 }
67
68 /**
69  * The post select function of the stdout task.
70  *
71  * \param s The scheduler this task was registered to.
72  * \param t The task structure of the stdout task.
73  *
74  * This function checks if \p STDOUT_FILENO was included by in the write fd set
75  * of \a s during the previous pre_select call.  If yes, and \p STDOUT_FILENO
76  * appeears to be writable, the data loaded in the input buffer is written to
77  * stdout.
78  */
79 static void stdout_post_select(struct sched *s, struct task *t)
80 {
81         struct stdout_task *sot = container_of(t, struct stdout_task, task);
82         ssize_t ret;
83
84         t->error = 0;
85         if (!sot->check_fd) {
86                 if (!*sot->loaded && *sot->input_error < 0)
87                         t->error = *sot->input_error;
88                 return;
89         }
90         if (!FD_ISSET(STDOUT_FILENO, &s->wfds))
91                 return;
92         ret = write(STDOUT_FILENO, *sot->bufp, *sot->loaded);
93         if (ret < 0) {
94                 t->error = -ERRNO_TO_PARA_ERROR(errno);
95                 return;
96         }
97         *sot->loaded -= ret;
98         if (*sot->loaded)
99                 memmove(*sot->bufp, *sot->bufp + ret, *sot->loaded);
100 }
101
102 static void stdout_post_select_btr(struct sched *s, struct task *t)
103 {
104         struct stdout_task *sot = container_of(t, struct stdout_task, task);
105         ssize_t ret;
106         size_t sz = btr_get_input_queue_size(sot->btrn);
107         bool orphan = btr_no_parent(sot->btrn);
108         char *buf;
109
110         t->error = 0;
111         if (!sot->check_fd) {
112                 if (sz == 0 && orphan) {
113                         t->error = -E_ORPHAN;
114                         goto err;
115                 }
116                 return;
117         }
118         if (!FD_ISSET(STDOUT_FILENO, &s->wfds))
119                 return;
120         sz = btr_next_buffer(sot->btrn, &buf);
121         if (sz == 0)
122                 return;
123         ret = write(STDOUT_FILENO, buf, sz);
124         if (ret < 0) {
125                 t->error = -ERRNO_TO_PARA_ERROR(errno);
126                 goto err;
127         }
128         btr_consume(sot->btrn, ret);
129         return;
130 err:
131         btr_del_node(sot->btrn);
132 }
133 /**
134  * Initialize a stdout task structure with default values.
135  *
136  * \param sot The stdout task structure.
137  *
138  * This fills in the pre/post select function poinzters of the task structure
139  * given by \a sot.
140  */
141 void stdout_set_defaults(struct stdout_task *sot)
142 {
143         int ret;
144
145         if (sot->btrn) {
146                 sot->task.pre_select = stdout_pre_select_btr;
147                 sot->task.post_select = stdout_post_select_btr;
148         } else {
149                 sot->task.pre_select = stdout_pre_select;
150                 sot->task.post_select = stdout_post_select;
151         }
152         sprintf(sot->task.status, "stdout writer");
153         ret = mark_fd_nonblocking(STDOUT_FILENO);
154         if (ret >= 0)
155                 return;
156         PARA_EMERG_LOG("%s\n", para_strerror(-ret));
157         exit(EXIT_FAILURE);
158 }