1 /***************************************************************************
2 * Copyright (C) 2007 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
23 ***************************************************************************/
30 #include "configuration.h"
36 enum fileio_type type
;
37 enum fileio_access access
;
41 static inline int fileio_close_local(struct fileio
*fileio
)
43 int retval
= fclose(fileio
->file
);
46 LOG_ERROR("BUG: fileio->file not a valid file descriptor");
48 LOG_ERROR("couldn't close %s: %s", fileio
->url
, strerror(errno
));
50 return ERROR_FILEIO_OPERATION_FAILED
;
56 static inline int fileio_open_local(struct fileio
*fileio
)
61 switch (fileio
->access
) {
63 strcpy(file_access
, "r");
66 strcpy(file_access
, "w");
68 case FILEIO_READWRITE
:
69 strcpy(file_access
, "w+");
72 strcpy(file_access
, "a");
74 case FILEIO_APPENDREAD
:
75 strcpy(file_access
, "a+");
78 LOG_ERROR("BUG: access neither read, write nor readwrite");
79 return ERROR_COMMAND_SYNTAX_ERROR
;
82 /* win32 always opens in binary mode */
84 if (fileio
->type
== FILEIO_BINARY
)
86 strcat(file_access
, "b");
88 fileio
->file
= open_file_from_path(fileio
->url
, file_access
);
90 LOG_ERROR("couldn't open %s", fileio
->url
);
91 return ERROR_FILEIO_OPERATION_FAILED
;
96 if ((fileio
->access
!= FILEIO_WRITE
) || (fileio
->access
== FILEIO_READWRITE
)) {
97 /* NB! Here we use fseek() instead of stat(), since stat is a
98 * more advanced operation that might not apply to e.g. a disk path
99 * that refers to e.g. a tftp client */
102 result
= fseek(fileio
->file
, 0, SEEK_END
);
104 file_size
= ftell(fileio
->file
);
106 result2
= fseek(fileio
->file
, 0, SEEK_SET
);
108 if ((file_size
< 0) || (result
< 0) || (result2
< 0)) {
109 fileio_close_local(fileio
);
110 return ERROR_FILEIO_OPERATION_FAILED
;
114 fileio
->size
= file_size
;
119 int fileio_open(struct fileio
**fileio
, const char *url
,
120 enum fileio_access access_type
, enum fileio_type type
)
125 tmp
= malloc(sizeof(struct fileio
));
128 tmp
->access
= access_type
;
129 tmp
->url
= strdup(url
);
131 retval
= fileio_open_local(tmp
);
133 if (retval
!= ERROR_OK
) {
144 int fileio_close(struct fileio
*fileio
)
148 retval
= fileio_close_local(fileio
);
156 int fileio_feof(struct fileio
*fileio
)
158 return feof(fileio
->file
);
161 int fileio_seek(struct fileio
*fileio
, size_t position
)
165 retval
= fseek(fileio
->file
, position
, SEEK_SET
);
168 LOG_ERROR("couldn't seek file %s: %s", fileio
->url
, strerror(errno
));
169 return ERROR_FILEIO_OPERATION_FAILED
;
175 static int fileio_local_read(struct fileio
*fileio
, size_t size
, void *buffer
,
180 retval
= fread(buffer
, 1, size
, fileio
->file
);
181 *size_read
= (retval
>= 0) ? retval
: 0;
183 return (retval
< 0) ? retval
: ERROR_OK
;
186 int fileio_read(struct fileio
*fileio
, size_t size
, void *buffer
,
189 return fileio_local_read(fileio
, size
, buffer
, size_read
);
192 int fileio_read_u32(struct fileio
*fileio
, uint32_t *data
)
198 retval
= fileio_local_read(fileio
, sizeof(uint32_t), buf
, &size_read
);
200 if (ERROR_OK
== retval
&& sizeof(uint32_t) != size_read
)
202 if (ERROR_OK
== retval
)
203 *data
= be_to_h_u32(buf
);
208 static int fileio_local_fgets(struct fileio
*fileio
, size_t size
, void *buffer
)
210 if (fgets(buffer
, size
, fileio
->file
) == NULL
)
211 return ERROR_FILEIO_OPERATION_FAILED
;
216 int fileio_fgets(struct fileio
*fileio
, size_t size
, void *buffer
)
218 return fileio_local_fgets(fileio
, size
, buffer
);
221 static int fileio_local_write(struct fileio
*fileio
, size_t size
,
222 const void *buffer
, size_t *size_written
)
226 retval
= fwrite(buffer
, 1, size
, fileio
->file
);
227 *size_written
= (retval
>= 0) ? retval
: 0;
229 return (retval
< 0) ? retval
: ERROR_OK
;
232 int fileio_write(struct fileio
*fileio
, size_t size
, const void *buffer
,
233 size_t *size_written
)
237 retval
= fileio_local_write(fileio
, size
, buffer
, size_written
);
239 if (retval
== ERROR_OK
)
240 fileio
->size
+= *size_written
;
245 int fileio_write_u32(struct fileio
*fileio
, uint32_t data
)
249 h_u32_to_be(buf
, data
);
252 retval
= fileio_write(fileio
, 4, buf
, &size_written
);
254 if (ERROR_OK
== retval
&& size_written
!= sizeof(uint32_t))
263 * For now this can not fail, but that's because a seek was executed
266 * Avoiding the seek on startup opens up for using streams.
269 int fileio_size(struct fileio
*fileio
, size_t *size
)
271 *size
= fileio
->size
;