1
2
3
13
14
15
16
17
18
19
20
21
22
23
30
31
45
46
47
48
49
50
51
71
72
73
74
75
76
77
78
79
83
84
85
86
87
90
91
92
93
94
95
96
97
98
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
150
151
152
153
154
155
156
157
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
205
206
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "log.h"
#include "configuration.h"
#include "fileio.h"
#include "replacements.h"
struct fileio {
char *url;
size_t size;
enum fileio_type type;
enum fileio_access access;
FILE *file;
...};
static inline int fileio_close_local(struct fileio *fileio)
{
int retval = fclose(fileio->file);
if (retval != 0) {
if (retval == EBADF)
LOG_ERROR("BUG: fileio->file not a valid file descriptor");
else
LOG_ERROR("couldn't close %s: %s", fileio->url, strerror(errno));
return ERROR_FILEIO_OPERATION_FAILED;
}if (retval != 0) { ... }
return ERROR_OK;
}{ ... }
static inline int fileio_open_local(struct fileio *fileio)
{
char file_access[4];
ssize_t file_size;
switch (fileio->access) {
case FILEIO_READ:
strcpy(file_access, "r");
break;case FILEIO_READ:
case FILEIO_WRITE:
strcpy(file_access, "w");
break;case FILEIO_WRITE:
case FILEIO_READWRITE:
strcpy(file_access, "w+");
break;case FILEIO_READWRITE:
case FILEIO_APPEND:
strcpy(file_access, "a");
break;case FILEIO_APPEND:
case FILEIO_APPENDREAD:
strcpy(file_access, "a+");
break;case FILEIO_APPENDREAD:
default:
LOG_ERROR("BUG: access neither read, write nor readwrite");
return ERROR_COMMAND_SYNTAX_ERROR;default
}switch (fileio->access) { ... }
#ifndef _WIN32
if (fileio->type == FILEIO_BINARY)
#endif
strcat(file_access, "b");
fileio->file = open_file_from_path(fileio->url, file_access);
if (!fileio->file) {
LOG_ERROR("couldn't open %s", fileio->url);
return ERROR_FILEIO_OPERATION_FAILED;
}if (!fileio->file) { ... }
file_size = 0;
if ((fileio->access != FILEIO_WRITE) || (fileio->access == FILEIO_READWRITE)) {
/* ... */
int result, result2;
result = fseek(fileio->file, 0, SEEK_END);
file_size = ftell(fileio->file);
result2 = fseek(fileio->file, 0, SEEK_SET);
if ((file_size < 0) || (result < 0) || (result2 < 0)) {
fileio_close_local(fileio);
return ERROR_FILEIO_OPERATION_FAILED;
}if ((file_size < 0) || (result < 0) || (result2 < 0)) { ... }
}if ((fileio->access != FILEIO_WRITE) || (fileio->access == FILEIO_READWRITE)) { ... }
fileio->size = file_size;
return ERROR_OK;
}{ ... }
int fileio_open(struct fileio **fileio, const char *url,
enum fileio_access access_type, enum fileio_type type)
{
int retval;
struct fileio *tmp;
tmp = malloc(sizeof(struct fileio));
tmp->type = type;
tmp->access = access_type;
tmp->url = strdup(url);
retval = fileio_open_local(tmp);
if (retval != ERROR_OK) {
free(tmp->url);
free(tmp);
return retval;
}if (retval != ERROR_OK) { ... }
*fileio = tmp;
return ERROR_OK;
}{ ... }
int fileio_close(struct fileio *fileio)
{
int retval;
retval = fileio_close_local(fileio);
free(fileio->url);
free(fileio);
return retval;
}{ ... }
int fileio_feof(struct fileio *fileio)
{
return feof(fileio->file);
}{ ... }
int fileio_seek(struct fileio *fileio, size_t position)
{
int retval;
retval = fseek(fileio->file, position, SEEK_SET);
if (retval != 0) {
LOG_ERROR("couldn't seek file %s: %s", fileio->url, strerror(errno));
return ERROR_FILEIO_OPERATION_FAILED;
}if (retval != 0) { ... }
return ERROR_OK;
}{ ... }
static int fileio_local_read(struct fileio *fileio, size_t size, void *buffer,
size_t *size_read)
{
ssize_t retval;
retval = fread(buffer, 1, size, fileio->file);
*size_read = (retval >= 0) ? retval : 0;
return (retval < 0) ? retval : ERROR_OK;
}{ ... }
int fileio_read(struct fileio *fileio, size_t size, void *buffer,
size_t *size_read)
{
return fileio_local_read(fileio, size, buffer, size_read);
}{ ... }
int fileio_read_u32(struct fileio *fileio, uint32_t *data)
{
int retval;
uint8_t buf[4];
size_t size_read;
retval = fileio_local_read(fileio, sizeof(uint32_t), buf, &size_read);
if (retval == ERROR_OK && sizeof(uint32_t) != size_read)
retval = -EIO;
if (retval == ERROR_OK)
*data = be_to_h_u32(buf);
return retval;
}{ ... }
static int fileio_local_fgets(struct fileio *fileio, size_t size, void *buffer)
{
if (!fgets(buffer, size, fileio->file))
return ERROR_FILEIO_OPERATION_FAILED;
return ERROR_OK;
}{ ... }
int fileio_fgets(struct fileio *fileio, size_t size, void *buffer)
{
return fileio_local_fgets(fileio, size, buffer);
}{ ... }
static int fileio_local_write(struct fileio *fileio, size_t size,
const void *buffer, size_t *size_written)
{
ssize_t retval;
retval = fwrite(buffer, 1, size, fileio->file);
*size_written = (retval >= 0) ? retval : 0;
return (retval < 0) ? retval : ERROR_OK;
}{ ... }
int fileio_write(struct fileio *fileio, size_t size, const void *buffer,
size_t *size_written)
{
int retval;
retval = fileio_local_write(fileio, size, buffer, size_written);
if (retval == ERROR_OK)
fileio->size += *size_written;
return retval;
}{ ... }
int fileio_write_u32(struct fileio *fileio, uint32_t data)
{
int retval;
uint8_t buf[4];
h_u32_to_be(buf, data);
size_t size_written;
retval = fileio_write(fileio, 4, buf, &size_written);
if (retval == ERROR_OK && size_written != sizeof(uint32_t))
retval = -EIO;
return retval;
}{ ... }
/* ... */
int fileio_size(struct fileio *fileio, size_t *size)
{
*size = fileio->size;
return ERROR_OK;
}{ ... }