2007-03-16 09:12:22 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 2007 by Dominic Rath *
|
|
|
|
* Dominic.Rath@gmx.de *
|
|
|
|
* *
|
2009-07-17 19:54:25 +00:00
|
|
|
* Copyright (C) 2007,2008 Øyvind Harboe *
|
2008-07-25 06:54:17 +00:00
|
|
|
* oyvind.harboe@zylin.com *
|
|
|
|
* *
|
2008-09-20 10:50:53 +00:00
|
|
|
* Copyright (C) 2008 by Spencer Oliver *
|
|
|
|
* spen@spen-soft.co.uk *
|
|
|
|
* *
|
2007-03-16 09:12:22 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the *
|
|
|
|
* Free Software Foundation, Inc., *
|
|
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
|
|
***************************************************************************/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "log.h"
|
2008-02-29 11:42:37 +00:00
|
|
|
#include "configuration.h"
|
2007-03-16 09:12:22 +00:00
|
|
|
#include "fileio.h"
|
|
|
|
|
2009-11-13 11:08:29 +00:00
|
|
|
static inline int fileio_open_local(struct fileio *fileio)
|
2007-03-16 09:12:22 +00:00
|
|
|
{
|
|
|
|
char access[4];
|
2009-06-23 22:50:08 +00:00
|
|
|
|
2007-03-16 09:12:22 +00:00
|
|
|
switch (fileio->access)
|
|
|
|
{
|
|
|
|
case FILEIO_READ:
|
|
|
|
strcpy(access, "r");
|
|
|
|
break;
|
|
|
|
case FILEIO_WRITE:
|
|
|
|
strcpy(access, "w");
|
|
|
|
break;
|
|
|
|
case FILEIO_READWRITE:
|
|
|
|
strcpy(access, "w+");
|
|
|
|
break;
|
|
|
|
case FILEIO_APPEND:
|
2009-06-23 22:50:08 +00:00
|
|
|
strcpy(access, "a");
|
2007-03-16 09:12:22 +00:00
|
|
|
break;
|
|
|
|
case FILEIO_APPENDREAD:
|
2009-06-23 22:50:08 +00:00
|
|
|
strcpy(access, "a+");
|
2007-03-16 09:12:22 +00:00
|
|
|
break;
|
|
|
|
default:
|
2008-03-25 15:45:17 +00:00
|
|
|
LOG_ERROR("BUG: access neither read, write nor readwrite");
|
2007-03-16 09:12:22 +00:00
|
|
|
return ERROR_INVALID_ARGUMENTS;
|
|
|
|
}
|
2009-06-23 22:50:08 +00:00
|
|
|
|
2007-07-31 09:15:59 +00:00
|
|
|
/* win32 always opens in binary mode */
|
|
|
|
#ifndef _WIN32
|
2007-05-29 11:23:42 +00:00
|
|
|
if (fileio->type == FILEIO_BINARY)
|
2007-07-31 09:15:59 +00:00
|
|
|
#endif
|
|
|
|
{
|
2007-03-16 09:12:22 +00:00
|
|
|
strcat(access, "b");
|
2007-07-31 09:15:59 +00:00
|
|
|
}
|
2009-06-23 22:50:08 +00:00
|
|
|
|
2008-02-29 11:42:37 +00:00
|
|
|
if (!(fileio->file = open_file_from_path (fileio->url, access)))
|
2007-03-16 09:12:22 +00:00
|
|
|
{
|
2008-03-25 15:45:17 +00:00
|
|
|
LOG_ERROR("couldn't open %s", fileio->url);
|
2007-03-16 09:12:22 +00:00
|
|
|
return ERROR_FILEIO_OPERATION_FAILED;
|
|
|
|
}
|
2009-06-23 22:50:08 +00:00
|
|
|
|
2007-03-16 09:12:22 +00:00
|
|
|
if ((fileio->access != FILEIO_WRITE) || (fileio->access == FILEIO_READWRITE))
|
|
|
|
{
|
2007-12-20 16:19:10 +00:00
|
|
|
/* NB! Here we use fseek() instead of stat(), since stat is a
|
|
|
|
* more advanced operation that might not apply to e.g. a disk path
|
|
|
|
* that refers to e.g. a tftp client */
|
|
|
|
int result, result2;
|
2009-06-23 22:50:08 +00:00
|
|
|
|
2008-02-29 11:42:37 +00:00
|
|
|
result = fseek(fileio->file, 0, SEEK_END);
|
|
|
|
|
|
|
|
fileio->size = ftell(fileio->file);
|
2009-06-23 22:50:08 +00:00
|
|
|
|
|
|
|
result2 = fseek(fileio->file, 0, SEEK_SET);
|
|
|
|
|
2009-06-23 22:45:47 +00:00
|
|
|
if ((fileio->size < 0)||(result < 0)||(result2 < 0))
|
2007-12-19 21:14:50 +00:00
|
|
|
{
|
|
|
|
fileio_close(fileio);
|
|
|
|
return ERROR_FILEIO_OPERATION_FAILED;
|
|
|
|
}
|
2007-03-16 09:12:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fileio->size = 0x0;
|
|
|
|
}
|
2009-06-23 22:50:08 +00:00
|
|
|
|
2007-03-16 09:12:22 +00:00
|
|
|
return ERROR_OK;
|
|
|
|
}
|
|
|
|
|
2009-11-13 11:08:29 +00:00
|
|
|
int fileio_open(struct fileio *fileio, const char *url, enum fileio_access access, enum fileio_type type)
|
2007-03-16 09:12:22 +00:00
|
|
|
{
|
|
|
|
int retval = ERROR_OK;
|
2007-03-28 16:31:55 +00:00
|
|
|
|
2007-05-29 11:23:42 +00:00
|
|
|
fileio->type = type;
|
2007-03-16 09:12:22 +00:00
|
|
|
fileio->access = access;
|
|
|
|
fileio->url = strdup(url);
|
2009-06-23 22:50:08 +00:00
|
|
|
|
2008-02-29 11:42:37 +00:00
|
|
|
retval = fileio_open_local(fileio);
|
|
|
|
|
|
|
|
return retval;
|
2007-03-16 09:12:22 +00:00
|
|
|
}
|
|
|
|
|
2009-11-13 11:08:29 +00:00
|
|
|
static inline int fileio_close_local(struct fileio *fileio)
|
2007-03-16 09:12:22 +00:00
|
|
|
{
|
|
|
|
int retval;
|
2008-02-29 11:42:37 +00:00
|
|
|
if ((retval = fclose(fileio->file)) != 0)
|
2007-03-16 09:12:22 +00:00
|
|
|
{
|
|
|
|
if (retval == EBADF)
|
|
|
|
{
|
2008-03-25 15:45:17 +00:00
|
|
|
LOG_ERROR("BUG: fileio_local->file not a valid file descriptor");
|
2007-03-16 09:12:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-03-25 15:45:17 +00:00
|
|
|
LOG_ERROR("couldn't close %s: %s", fileio->url, strerror(errno));
|
2007-03-16 09:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ERROR_FILEIO_OPERATION_FAILED;
|
|
|
|
}
|
2009-06-23 22:50:08 +00:00
|
|
|
|
2007-03-16 09:12:22 +00:00
|
|
|
return ERROR_OK;
|
|
|
|
}
|
|
|
|
|
2009-11-13 11:08:29 +00:00
|
|
|
int fileio_close(struct fileio *fileio)
|
2007-03-16 09:12:22 +00:00
|
|
|
{
|
|
|
|
int retval;
|
2009-06-23 22:50:08 +00:00
|
|
|
|
2008-02-29 11:42:37 +00:00
|
|
|
retval = fileio_close_local(fileio);
|
2009-06-23 22:50:08 +00:00
|
|
|
|
2009-11-13 22:22:21 +00:00
|
|
|
free((void*)fileio->url);
|
2007-12-18 21:20:28 +00:00
|
|
|
fileio->url = NULL;
|
2009-06-23 22:50:08 +00:00
|
|
|
|
2008-02-29 11:42:37 +00:00
|
|
|
return retval;
|
2007-03-16 09:12:22 +00:00
|
|
|
}
|
|
|
|
|
2009-11-13 11:08:29 +00:00
|
|
|
int fileio_seek(struct fileio *fileio, uint32_t position)
|
2007-03-16 09:12:22 +00:00
|
|
|
{
|
|
|
|
int retval;
|
2008-02-29 11:42:37 +00:00
|
|
|
if ((retval = fseek(fileio->file, position, SEEK_SET)) != 0)
|
2007-03-16 09:12:22 +00:00
|
|
|
{
|
2008-03-25 15:45:17 +00:00
|
|
|
LOG_ERROR("couldn't seek file %s: %s", fileio->url, strerror(errno));
|
2007-03-16 09:12:22 +00:00
|
|
|
return ERROR_FILEIO_OPERATION_FAILED;
|
|
|
|
}
|
2009-06-23 22:50:08 +00:00
|
|
|
|
2007-03-16 09:12:22 +00:00
|
|
|
return ERROR_OK;
|
|
|
|
}
|
|
|
|
|
2009-11-13 11:08:29 +00:00
|
|
|
static inline int fileio_local_read(struct fileio *fileio, uint32_t size, uint8_t *buffer, uint32_t *size_read)
|
2007-03-16 09:12:22 +00:00
|
|
|
{
|
2008-02-29 11:42:37 +00:00
|
|
|
*size_read = fread(buffer, 1, size, fileio->file);
|
2009-06-23 22:50:08 +00:00
|
|
|
|
2007-03-16 09:12:22 +00:00
|
|
|
return ERROR_OK;
|
|
|
|
}
|
|
|
|
|
2009-11-13 11:08:29 +00:00
|
|
|
int fileio_read(struct fileio *fileio, uint32_t size, uint8_t *buffer, uint32_t *size_read)
|
2007-03-16 09:12:22 +00:00
|
|
|
{
|
2008-02-29 11:42:37 +00:00
|
|
|
return fileio_local_read(fileio, size, buffer, size_read);
|
2007-03-16 09:12:22 +00:00
|
|
|
}
|
|
|
|
|
2009-11-13 11:08:29 +00:00
|
|
|
int fileio_read_u32(struct fileio *fileio, uint32_t *data)
|
2007-06-14 09:47:00 +00:00
|
|
|
{
|
2009-06-18 07:07:12 +00:00
|
|
|
uint8_t buf[4];
|
2009-06-18 07:11:11 +00:00
|
|
|
uint32_t size_read;
|
2007-06-14 09:47:00 +00:00
|
|
|
int retval;
|
2009-06-23 22:50:08 +00:00
|
|
|
|
2008-02-29 11:42:37 +00:00
|
|
|
if ((retval = fileio_local_read(fileio, 4, buf, &size_read)) != ERROR_OK)
|
|
|
|
return retval;
|
|
|
|
*data = be_to_h_u32(buf);
|
2009-06-23 22:50:08 +00:00
|
|
|
|
2007-06-14 09:47:00 +00:00
|
|
|
return ERROR_OK;
|
|
|
|
}
|
|
|
|
|
2009-11-13 11:08:29 +00:00
|
|
|
static inline int fileio_local_fgets(struct fileio *fileio, uint32_t size, char *buffer)
|
2007-10-01 08:31:30 +00:00
|
|
|
{
|
2009-06-23 22:46:23 +00:00
|
|
|
if (fgets(buffer, size, fileio->file) == NULL)
|
2007-10-01 08:31:30 +00:00
|
|
|
return ERROR_FILEIO_OPERATION_FAILED;
|
2009-06-23 22:50:08 +00:00
|
|
|
|
2007-10-01 08:31:30 +00:00
|
|
|
return ERROR_OK;
|
|
|
|
}
|
|
|
|
|
2009-11-13 11:08:29 +00:00
|
|
|
int fileio_fgets(struct fileio *fileio, uint32_t size, char *buffer)
|
2007-10-01 08:31:30 +00:00
|
|
|
{
|
2008-02-29 11:42:37 +00:00
|
|
|
return fileio_local_fgets(fileio, size, buffer);
|
2007-10-01 08:31:30 +00:00
|
|
|
}
|
|
|
|
|
2009-11-13 11:08:29 +00:00
|
|
|
static inline int fileio_local_write(struct fileio *fileio, uint32_t size, const uint8_t *buffer, uint32_t *size_written)
|
2007-03-16 09:12:22 +00:00
|
|
|
{
|
2008-02-29 11:42:37 +00:00
|
|
|
*size_written = fwrite(buffer, 1, size, fileio->file);
|
2009-06-23 22:50:08 +00:00
|
|
|
|
2007-03-16 09:12:22 +00:00
|
|
|
return ERROR_OK;
|
|
|
|
}
|
|
|
|
|
2009-11-13 11:08:29 +00:00
|
|
|
int fileio_write(struct fileio *fileio, uint32_t size, const uint8_t *buffer, uint32_t *size_written)
|
2007-03-16 09:12:22 +00:00
|
|
|
{
|
2007-08-20 10:08:08 +00:00
|
|
|
int retval;
|
2009-06-23 22:50:08 +00:00
|
|
|
|
2008-02-29 11:42:37 +00:00
|
|
|
retval = fileio_local_write(fileio, size, buffer, size_written);
|
2009-06-23 22:50:08 +00:00
|
|
|
|
2007-08-20 10:08:08 +00:00
|
|
|
if (retval == ERROR_OK)
|
|
|
|
fileio->size += *size_written;
|
2009-06-23 22:50:08 +00:00
|
|
|
|
2007-08-20 10:08:08 +00:00
|
|
|
return retval;;
|
2007-03-16 09:12:22 +00:00
|
|
|
}
|
2007-06-14 09:47:00 +00:00
|
|
|
|
2009-11-13 11:08:29 +00:00
|
|
|
int fileio_write_u32(struct fileio *fileio, uint32_t data)
|
2007-06-14 09:47:00 +00:00
|
|
|
{
|
2009-06-18 07:07:12 +00:00
|
|
|
uint8_t buf[4];
|
2009-06-18 07:11:11 +00:00
|
|
|
uint32_t size_written;
|
2007-06-14 09:47:00 +00:00
|
|
|
int retval;
|
2009-06-23 22:50:08 +00:00
|
|
|
|
2007-06-14 09:47:00 +00:00
|
|
|
h_u32_to_be(buf, data);
|
2009-06-23 22:50:08 +00:00
|
|
|
|
2008-02-29 11:42:37 +00:00
|
|
|
if ((retval = fileio_local_write(fileio, 4, buf, &size_written)) != ERROR_OK)
|
|
|
|
return retval;
|
2009-06-23 22:50:08 +00:00
|
|
|
|
2007-06-14 09:47:00 +00:00
|
|
|
return ERROR_OK;
|
|
|
|
}
|