]> git.tuebingen.mpg.de Git - paraslash.git/blob - stdin.c
write: Add --buffer-tree option.
[paraslash.git] / stdin.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 stdin.c Functions that deal with reading from stdin. */
8
9 #include <dirent.h> /* readdir() */
10 #include <assert.h>
11 #include <stdbool.h>
12 #include <regex.h>
13
14 #include "para.h"
15 #include "list.h"
16 #include "sched.h"
17 #include "fd.h"
18 #include "error.h"
19 #include "stdin.h"
20 #include "buffer_tree.h"
21 #include "string.h"
22
23 /**
24  * The pre_select function of the stdin task.
25  *
26  * \param s The scheduler this task was registered to.
27  * \param t The task structure of the stdin task.
28  *
29  * This function is always successful. If there is space left in the
30  * buffer of the stdin task, it adds \p STDIN_FILENO to the read fd set
31  * of \a s.
32  */
33 static void stdin_pre_select(struct sched *s, struct task *t)
34 {
35         struct stdin_task *sit = container_of(t, struct stdin_task, task);
36
37         if (sit->output_error && *sit->output_error < 0) {
38                 t->error = *sit->output_error;
39                 return;
40         }
41         t->error = 0;
42         sit->check_fd = 0;
43         if (sit->loaded >= sit->bufsize)
44                 return;
45         sit->check_fd = 1;
46         para_fd_set(STDIN_FILENO, &s->rfds, &s->max_fileno);
47 }
48
49 #define STDIN_MAX_PENDING (1024 * 1024)
50
51 static void stdin_pre_select_btr(struct sched *s, struct task *t)
52 {
53         struct stdin_task *sit = container_of(t, struct stdin_task, task);
54
55         if (btr_no_children(sit->btrn)) {
56                 t->error = -E_STDIN_NO_CHILD;
57                 btr_del_node(sit->btrn);
58                 sit->btrn = NULL;
59                 return;
60         }
61         t->error = 0;
62         if (btr_bytes_pending(sit->btrn) > STDIN_MAX_PENDING)
63                 sit->check_fd = 0;
64         else {
65                 sit->check_fd = 1;
66                 para_fd_set(STDIN_FILENO, &s->rfds, &s->max_fileno);
67         }
68 }
69
70 /**
71  * The post select function of the stdin task.
72  *
73  * \param s The scheduler this task was registered to.
74  * \param t The task structure of the stdin task.
75  *
76  * This function checks if \p STDIN_FILENO was included by in the read fd set
77  * of \a s during the previous pre_select call.  If yes, and \p STDIN_FILENO
78  * appears to be readable, data is read from stdin into the buffer of the
79  * stdin task.
80  */
81 static void stdin_post_select(struct sched *s, struct task *t)
82 {
83         struct stdin_task *sit = container_of(t, struct stdin_task, task);
84         ssize_t ret;
85
86         if (sit->output_error && *sit->output_error < 0) {
87                 t->error = *sit->output_error;
88                 return;
89         }
90         t->error = 0;
91         if (!sit->check_fd)
92                 return;
93         if (!FD_ISSET(STDIN_FILENO, &s->rfds))
94                 return;
95         ret = read(STDIN_FILENO, sit->buf + sit->loaded, sit->bufsize - sit->loaded);
96         if (ret < 0)
97                 t->error = ERRNO_TO_PARA_ERROR(errno);
98         else if (ret > 0)
99                 sit->loaded += ret;
100         else
101                 t->error = -E_STDIN_EOF;
102 }
103
104 #define STDIN_INPUT_BUFFER_SIZE 4000
105 static void stdin_post_select_btr(struct sched *s, struct task *t)
106 {
107         struct stdin_task *sit = container_of(t, struct stdin_task, task);
108         ssize_t ret;
109         char *buf = NULL;
110
111         t->error = -E_STDIN_NO_CHILD;
112         if (btr_no_children(sit->btrn))
113                 goto err;
114
115         t->error = 0;
116         if (!sit->check_fd)
117                 return;
118         if (!FD_ISSET(STDIN_FILENO, &s->rfds))
119                 return;
120
121         buf = para_malloc(STDIN_INPUT_BUFFER_SIZE);
122         ret = read(STDIN_FILENO, buf, STDIN_INPUT_BUFFER_SIZE);
123         if (ret < 0)
124                 t->error = -ERRNO_TO_PARA_ERROR(errno);
125         if (ret == 0)
126                 t->error = -E_STDIN_EOF;
127         if (t->error < 0)
128                 goto err;
129         btr_add_output(buf, ret, sit->btrn);
130         return;
131 err:
132         free(buf);
133         btr_del_node(sit->btrn);
134         sit->btrn = NULL;
135 }
136
137 /**
138  * Initialize a stdin task structure with default values.
139  *
140  * \param sit The stdin task structure.
141  *
142  * This fills in the pre/post select function pointers of the task structure
143  * given by \a sit. Moreover, the stdin file desctiptor is set to nonblocking
144  * mode and \a bufsize is initialized to 16 KB (but no buffer is allocated).
145  */
146 void stdin_set_defaults(struct stdin_task *sit)
147 {
148         int ret;
149
150         sit->bufsize = 32 * 1024;
151         if (sit->btrn) {
152                 sit->task.pre_select = stdin_pre_select_btr;
153                 sit->task.post_select = stdin_post_select_btr;
154         } else {
155                 sit->task.pre_select = stdin_pre_select;
156                 sit->task.post_select = stdin_post_select;
157         }
158         sprintf(sit->task.status, "stdin reader");
159         ret = mark_fd_nonblocking(STDIN_FILENO);
160         if (ret >= 0)
161                 return;
162         sit->output_error = NULL;
163         PARA_EMERG_LOG("%s\n", para_strerror(-ret));
164         exit(EXIT_FAILURE);
165 }