aboutsummaryrefslogtreecommitdiff
path: root/src/helper/fileio.c
blob: e40e019a1e190f490287df3f2a3c81fe0f315e1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/***************************************************************************
 *   Copyright (C) 2007 by Dominic Rath                                    *
 *   Dominic.Rath@gmx.de                                                   *
 *                                                                         *
 *   Copyright (C) 2007,2008 Øyvind Harboe                                 *
 *   oyvind.harboe@zylin.com                                               *
 *                                                                         *
 *   Copyright (C) 2008 by Spencer Oliver                                  *
 *   spen@spen-soft.co.uk                                                  *
 *                                                                         *
 *   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"
#include "configuration.h"
#include "fileio.h"

static inline int fileio_open_local(fileio_t *fileio)
{
	char access[4];
	
	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:
			LOG_ERROR("BUG: access neither read, write nor readwrite");
			return ERROR_INVALID_ARGUMENTS;
	}
	
	/* win32 always opens in binary mode */
#ifndef _WIN32
	if (fileio->type == FILEIO_BINARY)
#endif
	{
		strcat(access, "b");
	}
	
	if (!(fileio->file = open_file_from_path (fileio->url, access)))
	{
		LOG_ERROR("couldn't open %s", fileio->url);
		return ERROR_FILEIO_OPERATION_FAILED;
	}
	
	if ((fileio->access != FILEIO_WRITE) || (fileio->access == FILEIO_READWRITE))
	{
		/* 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;
		
		result = fseek(fileio->file, 0, SEEK_END);

		fileio->size = ftell(fileio->file);
		
		result2 = fseek(fileio->file, 0, SEEK_SET); 
			
		if ((fileio->size<0)||(result<0)||(result2<0))
		{
			fileio_close(fileio);
			return ERROR_FILEIO_OPERATION_FAILED;
		}
	}
	else
	{
		fileio->size = 0x0;
	}
	
	return ERROR_OK;
}

int fileio_open(fileio_t *fileio, const char *url, enum fileio_access access,	enum fileio_type type)
{
	int retval = ERROR_OK;

	fileio->type = type;
	fileio->access = access;
	fileio->url = strdup(url);
	
	retval = fileio_open_local(fileio);

	return retval;
}

static inline int fileio_close_local(fileio_t *fileio)
{
	int retval;
	if ((retval = fclose(fileio->file)) != 0)
	{
		if (retval == EBADF)
		{
			LOG_ERROR("BUG: fileio_local->file not a valid file descriptor");
		}
		else
		{
			LOG_ERROR("couldn't close %s: %s", fileio->url, strerror(errno));
		}

		return ERROR_FILEIO_OPERATION_FAILED;
	}
	
	return ERROR_OK;
}

int fileio_close(fileio_t *fileio)
{
	int retval;
	
	retval = fileio_close_local(fileio);
	
	free(fileio->url);
	fileio->url = NULL;
	
	return retval;
}

int fileio_seek(fileio_t *fileio, uint32_t position)
{
	int retval;
	if ((retval = fseek(fileio->file, position, SEEK_SET)) != 0)
	{
		LOG_ERROR("couldn't seek file %s: %s", fileio->url, strerror(errno));
		return ERROR_FILEIO_OPERATION_FAILED;
	}
	
	return ERROR_OK;
}

static inline int fileio_local_read(fileio_t *fileio, uint32_t size, uint8_t *buffer, uint32_t *size_read)
{
	*size_read = fread(buffer, 1, size, fileio->file);
	
	return ERROR_OK;
}

int fileio_read(fileio_t *fileio, uint32_t size, uint8_t *buffer, uint32_t *size_read)
{
	return fileio_local_read(fileio, size, buffer, size_read);
}

int fileio_read_u32(fileio_t *fileio, uint32_t *data)
{
	uint8_t buf[4];
	uint32_t size_read;
	int retval;
	
	if ((retval = fileio_local_read(fileio, 4, buf, &size_read)) != ERROR_OK)
		return retval;
	*data = be_to_h_u32(buf);
	
	return ERROR_OK;
}

static inline int fileio_local_fgets(fileio_t *fileio, uint32_t size, char *buffer)
{
	if ( fgets(buffer, size, fileio->file) == NULL)
		return ERROR_FILEIO_OPERATION_FAILED;
	
	return ERROR_OK;
}

int fileio_fgets(fileio_t *fileio, uint32_t size, char *buffer)
{
	return fileio_local_fgets(fileio, size, buffer);
}

static inline int fileio_local_write(fileio_t *fileio, uint32_t size, const uint8_t *buffer, uint32_t *size_written)
{
	*size_written = fwrite(buffer, 1, size, fileio->file);
	
	return ERROR_OK;
}

int fileio_write(fileio_t *fileio, uint32_t size, const uint8_t *buffer, uint32_t *size_written)
{
	int retval;
	
	retval = fileio_local_write(fileio, size, buffer, size_written);
	
	if (retval == ERROR_OK)
		fileio->size += *size_written;
	
	return retval;;
}

int fileio_write_u32(fileio_t *fileio, uint32_t data)
{
	uint8_t buf[4];
	uint32_t size_written;
	int retval;
	
	h_u32_to_be(buf, data);
	
	if ((retval = fileio_local_write(fileio, 4, buf, &size_written)) != ERROR_OK)
		return retval;
	
	return ERROR_OK;
}