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 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
28 #include "replacements.h"
30 #include "configuration.h"
38 #include <sys/types.h>
43 int fileio_close(fileio_t *fileio);
44 int fileio_dispatch_read(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_read);
46 int fileio_open_local(fileio_t *fileio)
50 switch (fileio->access)
58 case FILEIO_READWRITE:
64 case FILEIO_APPENDREAD:
68 LOG_ERROR("BUG: access neither read, write nor readwrite");
69 return ERROR_INVALID_ARGUMENTS;
72 /* win32 always opens in binary mode */
74 if (fileio->type == FILEIO_BINARY)
80 if (!(fileio->file = open_file_from_path (fileio->url, access)))
82 LOG_ERROR("couldn't open %s", fileio->url);
83 return ERROR_FILEIO_OPERATION_FAILED;
86 if ((fileio->access != FILEIO_WRITE) || (fileio->access == FILEIO_READWRITE))
88 /* NB! Here we use fseek() instead of stat(), since stat is a
89 * more advanced operation that might not apply to e.g. a disk path
90 * that refers to e.g. a tftp client */
93 result = fseek(fileio->file, 0, SEEK_END);
95 fileio->size = ftell(fileio->file);
97 result2 = fseek(fileio->file, 0, SEEK_SET);
99 if ((fileio->size<0)||(result<0)||(result2<0))
101 fileio_close(fileio);
102 return ERROR_FILEIO_OPERATION_FAILED;
113 int fileio_open(fileio_t *fileio, char *url, enum fileio_access access, enum fileio_type type)
115 int retval = ERROR_OK;
118 fileio->access = access;
119 fileio->url = strdup(url);
121 retval = fileio_open_local(fileio);
126 int fileio_close_local(fileio_t *fileio)
129 if ((retval = fclose(fileio->file)) != 0)
133 LOG_ERROR("BUG: fileio_local->file not a valid file descriptor");
137 LOG_ERROR("couldn't close %s: %s", fileio->url, strerror(errno));
140 return ERROR_FILEIO_OPERATION_FAILED;
146 int fileio_close(fileio_t *fileio)
150 retval = fileio_close_local(fileio);
158 int fileio_seek(fileio_t *fileio, u32 position)
161 if ((retval = fseek(fileio->file, position, SEEK_SET)) != 0)
163 LOG_ERROR("couldn't seek file %s: %s", fileio->url, strerror(errno));
164 return ERROR_FILEIO_OPERATION_FAILED;
170 int fileio_local_read(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_read)
172 *size_read = fread(buffer, 1, size, fileio->file);
177 int fileio_read(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_read)
179 return fileio_local_read(fileio, size, buffer, size_read);
182 int fileio_read_u32(fileio_t *fileio, u32 *data)
188 if ((retval = fileio_local_read(fileio, 4, buf, &size_read)) != ERROR_OK)
190 *data = be_to_h_u32(buf);
195 int fileio_local_fgets(fileio_t *fileio, u32 size, char *buffer)
197 if( fgets(buffer, size, fileio->file) == NULL)
198 return ERROR_FILEIO_OPERATION_FAILED;
203 int fileio_fgets(fileio_t *fileio, u32 size, char *buffer)
205 return fileio_local_fgets(fileio, size, buffer);
208 int fileio_local_write(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_written)
210 *size_written = fwrite(buffer, 1, size, fileio->file);
215 int fileio_write(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_written)
219 retval = fileio_local_write(fileio, size, buffer, size_written);
221 if (retval == ERROR_OK)
222 fileio->size += *size_written;
227 int fileio_write_u32(fileio_t *fileio, u32 data)
233 h_u32_to_be(buf, data);
235 if ((retval = fileio_local_write(fileio, 4, buf, &size_written)) != ERROR_OK)