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, write to the *
23 * Free Software Foundation, Inc., *
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 ***************************************************************************/
31 #include "replacements.h"
33 #include "configuration.h"
41 #include <sys/types.h>
46 int fileio_dispatch_read(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_read);
48 int fileio_open_local(fileio_t *fileio)
52 switch (fileio->access)
60 case FILEIO_READWRITE:
66 case FILEIO_APPENDREAD:
70 LOG_ERROR("BUG: access neither read, write nor readwrite");
71 return ERROR_INVALID_ARGUMENTS;
74 /* win32 always opens in binary mode */
76 if (fileio->type == FILEIO_BINARY)
82 if (!(fileio->file = open_file_from_path (fileio->url, access)))
84 LOG_ERROR("couldn't open %s", fileio->url);
85 return ERROR_FILEIO_OPERATION_FAILED;
88 if ((fileio->access != FILEIO_WRITE) || (fileio->access == FILEIO_READWRITE))
90 /* NB! Here we use fseek() instead of stat(), since stat is a
91 * more advanced operation that might not apply to e.g. a disk path
92 * that refers to e.g. a tftp client */
95 result = fseek(fileio->file, 0, SEEK_END);
97 fileio->size = ftell(fileio->file);
99 result2 = fseek(fileio->file, 0, SEEK_SET);
101 if ((fileio->size<0)||(result<0)||(result2<0))
103 fileio_close(fileio);
104 return ERROR_FILEIO_OPERATION_FAILED;
115 int fileio_open(fileio_t *fileio, char *url, enum fileio_access access, enum fileio_type type)
117 int retval = ERROR_OK;
120 fileio->access = access;
121 fileio->url = strdup(url);
123 retval = fileio_open_local(fileio);
128 int fileio_close_local(fileio_t *fileio)
131 if ((retval = fclose(fileio->file)) != 0)
135 LOG_ERROR("BUG: fileio_local->file not a valid file descriptor");
139 LOG_ERROR("couldn't close %s: %s", fileio->url, strerror(errno));
142 return ERROR_FILEIO_OPERATION_FAILED;
148 int fileio_close(fileio_t *fileio)
152 retval = fileio_close_local(fileio);
160 int fileio_seek(fileio_t *fileio, u32 position)
163 if ((retval = fseek(fileio->file, position, SEEK_SET)) != 0)
165 LOG_ERROR("couldn't seek file %s: %s", fileio->url, strerror(errno));
166 return ERROR_FILEIO_OPERATION_FAILED;
172 int fileio_local_read(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_read)
174 *size_read = fread(buffer, 1, size, fileio->file);
179 int fileio_read(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_read)
181 return fileio_local_read(fileio, size, buffer, size_read);
184 int fileio_read_u32(fileio_t *fileio, u32 *data)
190 if ((retval = fileio_local_read(fileio, 4, buf, &size_read)) != ERROR_OK)
192 *data = be_to_h_u32(buf);
197 int fileio_local_fgets(fileio_t *fileio, u32 size, char *buffer)
199 if( fgets(buffer, size, fileio->file) == NULL)
200 return ERROR_FILEIO_OPERATION_FAILED;
205 int fileio_fgets(fileio_t *fileio, u32 size, char *buffer)
207 return fileio_local_fgets(fileio, size, buffer);
210 int fileio_local_write(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_written)
212 *size_written = fwrite(buffer, 1, size, fileio->file);
217 int fileio_write(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_written)
221 retval = fileio_local_write(fileio, size, buffer, size_written);
223 if (retval == ERROR_OK)
224 fileio->size += *size_written;
229 int fileio_write_u32(fileio_t *fileio, u32 data)
235 h_u32_to_be(buf, data);
237 if ((retval = fileio_local_write(fileio, 4, buf, &size_written)) != ERROR_OK)