1 /***************************************************************************
2 * Copyright (C) 2007 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
25 #include "replacements.h"
27 #include "configuration.h"
35 #include <sys/types.h>
40 int fileio_close(fileio_t *fileio);
41 int fileio_dispatch_read(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_read);
43 int fileio_open_local(fileio_t *fileio)
47 switch (fileio->access)
55 case FILEIO_READWRITE:
61 case FILEIO_APPENDREAD:
65 LOG_ERROR("BUG: access neither read, write nor readwrite");
66 return ERROR_INVALID_ARGUMENTS;
69 /* win32 always opens in binary mode */
71 if (fileio->type == FILEIO_BINARY)
77 if (!(fileio->file = open_file_from_path (fileio->url, access)))
79 LOG_ERROR("couldn't open %s", fileio->url);
80 return ERROR_FILEIO_OPERATION_FAILED;
83 if ((fileio->access != FILEIO_WRITE) || (fileio->access == FILEIO_READWRITE))
85 /* NB! Here we use fseek() instead of stat(), since stat is a
86 * more advanced operation that might not apply to e.g. a disk path
87 * that refers to e.g. a tftp client */
90 result = fseek(fileio->file, 0, SEEK_END);
92 fileio->size = ftell(fileio->file);
94 result2 = fseek(fileio->file, 0, SEEK_SET);
96 if ((fileio->size<0)||(result<0)||(result2<0))
99 return ERROR_FILEIO_OPERATION_FAILED;
110 int fileio_open(fileio_t *fileio, char *url, enum fileio_access access, enum fileio_type type)
112 int retval = ERROR_OK;
115 fileio->access = access;
116 fileio->url = strdup(url);
118 retval = fileio_open_local(fileio);
123 int fileio_close_local(fileio_t *fileio)
126 if ((retval = fclose(fileio->file)) != 0)
130 LOG_ERROR("BUG: fileio_local->file not a valid file descriptor");
134 LOG_ERROR("couldn't close %s: %s", fileio->url, strerror(errno));
137 return ERROR_FILEIO_OPERATION_FAILED;
143 int fileio_close(fileio_t *fileio)
147 retval = fileio_close_local(fileio);
155 int fileio_seek(fileio_t *fileio, u32 position)
158 if ((retval = fseek(fileio->file, position, SEEK_SET)) != 0)
160 LOG_ERROR("couldn't seek file %s: %s", fileio->url, strerror(errno));
161 return ERROR_FILEIO_OPERATION_FAILED;
167 int fileio_local_read(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_read)
169 *size_read = fread(buffer, 1, size, fileio->file);
174 int fileio_read(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_read)
176 return fileio_local_read(fileio, size, buffer, size_read);
179 int fileio_read_u32(fileio_t *fileio, u32 *data)
185 if ((retval = fileio_local_read(fileio, 4, buf, &size_read)) != ERROR_OK)
187 *data = be_to_h_u32(buf);
192 int fileio_local_fgets(fileio_t *fileio, u32 size, char *buffer)
194 if( fgets(buffer, size, fileio->file) == NULL)
195 return ERROR_FILEIO_OPERATION_FAILED;
200 int fileio_fgets(fileio_t *fileio, u32 size, char *buffer)
202 return fileio_local_fgets(fileio, size, buffer);
205 int fileio_local_write(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_written)
207 *size_written = fwrite(buffer, 1, size, fileio->file);
212 int fileio_write(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_written)
216 retval = fileio_local_write(fileio, size, buffer, size_written);
218 if (retval == ERROR_OK)
219 fileio->size += *size_written;
224 int fileio_write_u32(fileio_t *fileio, u32 data)
230 h_u32_to_be(buf, data);
232 if ((retval = fileio_local_write(fileio, 4, buf, &size_written)) != ERROR_OK)