2007-03-16 04:12:22 -05:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 2007 by Dominic Rath *
|
|
|
|
* Dominic.Rath@gmx.de *
|
|
|
|
* *
|
|
|
|
* 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 "types.h"
|
|
|
|
#include "replacements.h"
|
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
#include "fileio.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
int fileio_close(fileio_t *fileio);
|
|
|
|
int fileio_dispatch_read(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_read);
|
|
|
|
|
|
|
|
int fileio_open_local(fileio_t *fileio)
|
|
|
|
{
|
|
|
|
fileio_local_t *fileio_local = malloc(sizeof(fileio_local_t));
|
|
|
|
char access[4];
|
|
|
|
|
2007-12-19 15:14:50 -06:00
|
|
|
fileio->location_private = fileio_local;
|
2007-03-16 04:12:22 -05: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:
|
|
|
|
strcpy(access, "a");
|
|
|
|
break;
|
|
|
|
case FILEIO_APPENDREAD:
|
|
|
|
strcpy(access, "a+");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
free(fileio_local);
|
|
|
|
ERROR("BUG: access neither read, write nor readwrite");
|
|
|
|
return ERROR_INVALID_ARGUMENTS;
|
|
|
|
}
|
2007-07-31 04:15:59 -05:00
|
|
|
|
|
|
|
/* win32 always opens in binary mode */
|
|
|
|
#ifndef _WIN32
|
2007-05-29 06:23:42 -05:00
|
|
|
if (fileio->type == FILEIO_BINARY)
|
2007-07-31 04:15:59 -05:00
|
|
|
#endif
|
|
|
|
{
|
2007-03-16 04:12:22 -05:00
|
|
|
strcat(access, "b");
|
2007-07-31 04:15:59 -05:00
|
|
|
}
|
2007-03-16 04:12:22 -05:00
|
|
|
|
|
|
|
if (!(fileio_local->file = fopen(fileio->url, access)))
|
|
|
|
{
|
|
|
|
free(fileio_local);
|
|
|
|
snprintf(fileio->error_str, FILEIO_MAX_ERROR_STRING, "couldn't open %s", fileio->url);
|
|
|
|
return ERROR_FILEIO_OPERATION_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((fileio->access != FILEIO_WRITE) || (fileio->access == FILEIO_READWRITE))
|
|
|
|
{
|
2007-12-19 15:14:50 -06: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=fseek(fileio_local->file, 0, SEEK_END);
|
|
|
|
|
|
|
|
fileio->size = ftell(fileio_local->file);
|
|
|
|
|
|
|
|
int result2 = fseek(fileio_local->file, 0, SEEK_SET);
|
|
|
|
|
|
|
|
if ((fileio->size<0)||(result<0)||(result2<0))
|
|
|
|
{
|
|
|
|
fileio_close(fileio);
|
|
|
|
return ERROR_FILEIO_OPERATION_FAILED;
|
|
|
|
}
|
2007-03-16 04:12:22 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fileio->size = 0x0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ERROR_OK;
|
|
|
|
}
|
|
|
|
|
2007-05-29 06:23:42 -05:00
|
|
|
int fileio_open(fileio_t *fileio, char *url, enum fileio_access access, enum fileio_type type)
|
2007-03-16 04:12:22 -05:00
|
|
|
{
|
|
|
|
int retval = ERROR_OK;
|
2007-03-28 11:31:55 -05:00
|
|
|
char *resource_identifier = NULL;
|
|
|
|
|
2007-12-19 15:14:50 -06:00
|
|
|
/* try to identify file location. We only hijack the file paths we understand, the rest is
|
|
|
|
* passed on to the OS which might implement e.g. tftp via a mounted tftp device.
|
|
|
|
*/
|
2007-03-28 11:31:55 -05:00
|
|
|
if ((resource_identifier = strstr(url, "bootp://")) && (resource_identifier == url))
|
2007-03-16 04:12:22 -05:00
|
|
|
{
|
2007-03-28 11:31:55 -05:00
|
|
|
ERROR("bootp resource location isn't supported yet");
|
|
|
|
return ERROR_FILEIO_RESOURCE_TYPE_UNKNOWN;
|
2007-03-16 04:12:22 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-03-28 11:31:55 -05:00
|
|
|
/* default to local files */
|
|
|
|
fileio->location = FILEIO_LOCAL;
|
2007-03-16 04:12:22 -05:00
|
|
|
}
|
|
|
|
|
2007-05-29 06:23:42 -05:00
|
|
|
fileio->type = type;
|
2007-03-16 04:12:22 -05:00
|
|
|
fileio->access = access;
|
|
|
|
fileio->url = strdup(url);
|
|
|
|
|
|
|
|
switch (fileio->location)
|
|
|
|
{
|
|
|
|
case FILEIO_LOCAL:
|
|
|
|
retval = fileio_open_local(fileio);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ERROR("BUG: should never get here");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (retval != ERROR_OK)
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
return ERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fileio_close_local(fileio_t *fileio)
|
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
fileio_local_t *fileio_local = fileio->location_private;
|
|
|
|
|
2007-12-18 15:20:28 -06:00
|
|
|
if (fileio->location_private == NULL)
|
|
|
|
{
|
|
|
|
snprintf(fileio->error_str, FILEIO_MAX_ERROR_STRING, "couldn't close %s: ", fileio->url);
|
|
|
|
return ERROR_FILEIO_OPERATION_FAILED;
|
|
|
|
}
|
|
|
|
|
2007-03-16 04:12:22 -05:00
|
|
|
if ((retval = fclose(fileio_local->file)) != 0)
|
|
|
|
{
|
|
|
|
if (retval == EBADF)
|
|
|
|
{
|
|
|
|
snprintf(fileio->error_str, FILEIO_MAX_ERROR_STRING, "BUG: fileio_local->file not a valid file descriptor");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
snprintf(fileio->error_str, FILEIO_MAX_ERROR_STRING, "couldn't close %s: %s", fileio->url, strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
return ERROR_FILEIO_OPERATION_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(fileio->location_private);
|
2007-12-18 15:20:28 -06:00
|
|
|
fileio->location_private = NULL;
|
2007-03-16 04:12:22 -05:00
|
|
|
|
|
|
|
return ERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fileio_close(fileio_t *fileio)
|
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
switch (fileio->location)
|
|
|
|
{
|
|
|
|
case FILEIO_LOCAL:
|
|
|
|
retval = fileio_close_local(fileio);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ERROR("BUG: should never get here");
|
2007-03-26 16:47:26 -05:00
|
|
|
retval = ERROR_FILEIO_OPERATION_FAILED;
|
2007-03-16 04:12:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (retval != ERROR_OK)
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
free(fileio->url);
|
2007-12-18 15:20:28 -06:00
|
|
|
fileio->url = NULL;
|
2007-03-16 04:12:22 -05:00
|
|
|
|
|
|
|
return ERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fileio_seek_local(fileio_t *fileio, u32 position)
|
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
fileio_local_t *fileio_local = fileio->location_private;
|
|
|
|
|
|
|
|
if ((retval = fseek(fileio_local->file, position, SEEK_SET)) != 0)
|
|
|
|
{
|
|
|
|
snprintf(fileio->error_str, FILEIO_MAX_ERROR_STRING, "couldn't seek file %s: %s", fileio->url, strerror(errno));
|
|
|
|
return ERROR_FILEIO_OPERATION_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fileio_seek(fileio_t *fileio, u32 position)
|
|
|
|
{
|
|
|
|
switch (fileio->location)
|
|
|
|
{
|
|
|
|
case FILEIO_LOCAL:
|
|
|
|
return fileio_seek_local(fileio, position);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ERROR("BUG: should never get here");
|
|
|
|
}
|
|
|
|
|
|
|
|
return ERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fileio_local_read(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_read)
|
|
|
|
{
|
|
|
|
fileio_local_t *fileio_local = fileio->location_private;
|
|
|
|
|
|
|
|
*size_read = fread(buffer, 1, size, fileio_local->file);
|
|
|
|
|
|
|
|
return ERROR_OK;
|
|
|
|
}
|
|
|
|
|
2007-05-29 06:23:42 -05:00
|
|
|
int fileio_read(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_read)
|
2007-03-16 04:12:22 -05:00
|
|
|
{
|
|
|
|
switch (fileio->location)
|
|
|
|
{
|
|
|
|
case FILEIO_LOCAL:
|
|
|
|
return fileio_local_read(fileio, size, buffer, size_read);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ERROR("BUG: should never get here");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-14 04:47:00 -05:00
|
|
|
int fileio_read_u32(fileio_t *fileio, u32 *data)
|
|
|
|
{
|
|
|
|
u8 buf[4];
|
|
|
|
u32 size_read;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
switch (fileio->location)
|
|
|
|
{
|
|
|
|
case FILEIO_LOCAL:
|
|
|
|
if ((retval = fileio_local_read(fileio, 4, buf, &size_read)) != ERROR_OK)
|
|
|
|
return retval;
|
|
|
|
*data = be_to_h_u32(buf);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ERROR("BUG: should never get here");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ERROR_OK;
|
|
|
|
}
|
|
|
|
|
2007-10-01 03:31:30 -05:00
|
|
|
int fileio_local_fgets(fileio_t *fileio, u32 size, u8 *buffer)
|
|
|
|
{
|
|
|
|
fileio_local_t *fileio_local = fileio->location_private;
|
|
|
|
|
|
|
|
if( fgets(buffer, size, fileio_local->file) == NULL)
|
|
|
|
return ERROR_FILEIO_OPERATION_FAILED;
|
|
|
|
|
|
|
|
return ERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fileio_fgets(fileio_t *fileio, u32 size, u8 *buffer)
|
|
|
|
{
|
|
|
|
switch (fileio->location)
|
|
|
|
{
|
|
|
|
case FILEIO_LOCAL:
|
|
|
|
return fileio_local_fgets(fileio, size, buffer);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ERROR("BUG: should never get here");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-16 04:12:22 -05:00
|
|
|
int fileio_local_write(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_written)
|
|
|
|
{
|
|
|
|
fileio_local_t *fileio_local = fileio->location_private;
|
|
|
|
|
|
|
|
*size_written = fwrite(buffer, 1, size, fileio_local->file);
|
|
|
|
|
|
|
|
return ERROR_OK;
|
|
|
|
}
|
|
|
|
|
2007-05-29 06:23:42 -05:00
|
|
|
int fileio_write(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_written)
|
2007-03-16 04:12:22 -05:00
|
|
|
{
|
2007-08-20 05:08:08 -05:00
|
|
|
int retval;
|
|
|
|
|
2007-03-16 04:12:22 -05:00
|
|
|
switch (fileio->location)
|
|
|
|
{
|
|
|
|
case FILEIO_LOCAL:
|
2007-08-20 05:08:08 -05:00
|
|
|
retval = fileio_local_write(fileio, size, buffer, size_written);
|
2007-03-16 04:12:22 -05:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ERROR("BUG: should never get here");
|
2007-08-20 05:08:08 -05:00
|
|
|
exit(-1);
|
2007-03-16 04:12:22 -05:00
|
|
|
}
|
|
|
|
|
2007-08-20 05:08:08 -05:00
|
|
|
if (retval == ERROR_OK)
|
|
|
|
fileio->size += *size_written;
|
|
|
|
|
|
|
|
return retval;;
|
2007-03-16 04:12:22 -05:00
|
|
|
}
|
2007-06-14 04:47:00 -05:00
|
|
|
|
|
|
|
int fileio_write_u32(fileio_t *fileio, u32 data)
|
|
|
|
{
|
|
|
|
u8 buf[4];
|
|
|
|
u32 size_written;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
h_u32_to_be(buf, data);
|
|
|
|
|
|
|
|
switch (fileio->location)
|
|
|
|
{
|
|
|
|
case FILEIO_LOCAL:
|
|
|
|
if ((retval = fileio_local_write(fileio, 4, buf, &size_written)) != ERROR_OK)
|
|
|
|
return retval;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ERROR("BUG: should never get here");
|
|
|
|
}
|
|
|
|
|
|
|
|
return ERROR_OK;
|
|
|
|
}
|