-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfile-stream.c
214 lines (183 loc) · 5.47 KB
/
file-stream.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* -*- coding: utf-8 -*-
* ----------------------------------------------------------------------
* Copyright © 2012-2014, RedJack, LLC.
* All rights reserved.
*
* Please see the COPYING file in this distribution for license details.
* ----------------------------------------------------------------------
*/
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include "libcork/ds/stream.h"
#include "libcork/helpers/errors.h"
#include "libcork/helpers/posix.h"
#define BUFFER_SIZE 4096
/*-----------------------------------------------------------------------
* Producers
*/
int
cork_consume_fd(struct cork_stream_consumer *consumer, int fd)
{
char buf[BUFFER_SIZE];
ssize_t bytes_read;
bool first = true;
while (true) {
while ((bytes_read = read(fd, buf, BUFFER_SIZE)) > 0) {
rii_check(cork_stream_consumer_data
(consumer, buf, bytes_read, first));
first = false;
}
if (bytes_read == 0) {
return cork_stream_consumer_eof(consumer);
} else if (errno != EINTR) {
cork_system_error_set();
return -1;
}
}
}
int
cork_consume_file(struct cork_stream_consumer *consumer, FILE *fp)
{
char buf[BUFFER_SIZE];
size_t bytes_read;
bool first = true;
while (true) {
while ((bytes_read = fread(buf, 1, BUFFER_SIZE, fp)) > 0) {
rii_check(cork_stream_consumer_data
(consumer, buf, bytes_read, first));
first = false;
}
if (feof(fp)) {
return cork_stream_consumer_eof(consumer);
} else if (errno != EINTR) {
cork_system_error_set();
return -1;
}
}
}
int
cork_consume_file_from_path(struct cork_stream_consumer *consumer,
const char *path, int flags)
{
int fd;
rii_check_posix(fd = open(path, flags));
ei_check(cork_consume_fd(consumer, fd));
rii_check_posix(close(fd));
return 0;
error:
rii_check_posix(close(fd));
return -1;
}
/*-----------------------------------------------------------------------
* Consumers
*/
struct cork_file_consumer {
struct cork_stream_consumer parent;
FILE *fp;
};
static int
cork_file_consumer__data(struct cork_stream_consumer *vself,
const void *buf, size_t size, bool is_first)
{
struct cork_file_consumer *self =
cork_container_of(vself, struct cork_file_consumer, parent);
size_t bytes_written = fwrite(buf, 1, size, self->fp);
/* If there was an error writing to the file, then signal this to
* the producer */
if (bytes_written == size) {
return 0;
} else {
cork_system_error_set();
return -1;
}
}
static int
cork_file_consumer__eof(struct cork_stream_consumer *vself)
{
/* We never close the file with this version of the consumer, so there's
* nothing special to do at end-of-stream. */
return 0;
}
static void
cork_file_consumer__free(struct cork_stream_consumer *vself)
{
struct cork_file_consumer *self =
cork_container_of(vself, struct cork_file_consumer, parent);
cork_delete(struct cork_file_consumer, self);
}
struct cork_stream_consumer *
cork_file_consumer_new(FILE *fp)
{
struct cork_file_consumer *self = cork_new(struct cork_file_consumer);
self->parent.data = cork_file_consumer__data;
self->parent.eof = cork_file_consumer__eof;
self->parent.free = cork_file_consumer__free;
self->fp = fp;
return &self->parent;
}
struct cork_fd_consumer {
struct cork_stream_consumer parent;
int fd;
};
static int
cork_fd_consumer__data(struct cork_stream_consumer *vself,
const void *buf, size_t size, bool is_first)
{
struct cork_fd_consumer *self =
cork_container_of(vself, struct cork_fd_consumer, parent);
size_t bytes_left = size;
while (bytes_left > 0) {
ssize_t rc = write(self->fd, buf, bytes_left);
if (rc == -1 && errno != EINTR) {
cork_system_error_set();
return -1;
} else {
bytes_left -= rc;
buf += rc;
}
}
return 0;
}
static int
cork_fd_consumer__eof_close(struct cork_stream_consumer *vself)
{
int rc;
struct cork_fd_consumer *self =
cork_container_of(vself, struct cork_fd_consumer, parent);
rii_check_posix(rc = close(self->fd));
return 0;
}
static void
cork_fd_consumer__free(struct cork_stream_consumer *vself)
{
struct cork_fd_consumer *self =
cork_container_of(vself, struct cork_fd_consumer, parent);
cork_delete(struct cork_fd_consumer, self);
}
struct cork_stream_consumer *
cork_fd_consumer_new(int fd)
{
struct cork_fd_consumer *self = cork_new(struct cork_fd_consumer);
self->parent.data = cork_fd_consumer__data;
/* We don't want to close fd, so we reuse file_consumer's eof method */
self->parent.eof = cork_file_consumer__eof;
self->parent.free = cork_fd_consumer__free;
self->fd = fd;
return &self->parent;
}
struct cork_stream_consumer *
cork_file_from_path_consumer_new(const char *path, int flags)
{
int fd;
struct cork_fd_consumer *self;
rpi_check_posix(fd = open(path, flags));
self = cork_new(struct cork_fd_consumer);
self->parent.data = cork_fd_consumer__data;
self->parent.eof = cork_fd_consumer__eof_close;
self->parent.free = cork_fd_consumer__free;
self->fd = fd;
return &self->parent;
}