aboutsummaryrefslogtreecommitdiffstats
path: root/core.c
blob: 2711111c5e3776aaac0bacf4dbbbc74b64293ab0 (plain) (blame)
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include "core.h"

int fd_open( const char *filename, int flags )
{
	int ret;

	ret = open( filename, flags );

	return ret;
}


int fd_close( int fd )
{
	int ret;

	ret = close( fd );

	return ret;
}


int fd_seek( int fd, off_t offset, int whence )
{
	int ret = 0;
	off_t off_new=0;

	off_new = lseek( fd, offset, whence );
	if ( errno != 0)
	{
		printf("Cannot seek %s\n", strerror(errno));
		errno = 0; //why i need to reset it?
		return -1;
	}

	return ret;
}


off_t fd_pos( int fd )
{
	off_t cur = 0;
	cur = lseek( fd, 0, SEEK_CUR );
	return cur;
}


off_t fd_set_pos( int fd, off_t offset )
{
	off_t ret;

	ret = lseek( fd, offset, SEEK_SET );

	return ret;
}


off_t fd_size( int fd )
{
	off_t ret;
	off_t old;

	old = lseek( fd, 0, FD_SEEK_CUR );
	ret = lseek( fd, 0, FD_SEEK_END );
	lseek( fd, old, FD_SEEK_SET );

	return ret;
}


ssize_t fd_read( int fd, void *buf, size_t count )
{
	ssize_t ret;
	
	ret = read( fd, buf, count );

	return ret;
}


ssize_t fd_write( int fd, void *buf, size_t count )
{
	ssize_t ret;

	printf("%d %x %d\n", fd, buf, count);
	ret = write( fd, buf, count );
	if ( errno != 0)
	{
		printf("Cannot write %s\n", strerror(errno));
		errno = 0;
	}

	return ret;
}


file_t *file_init()
{
	file_t *ret = NULL;

	ret = malloc( sizeof(file_t) );
	memset( ret, 0, sizeof(file_t) );

	ret->flags = FD_RO; //di we really need that?

	return ret;
}


int file_open_fn( file_t *ft, const char *filename, int flags )
{
	int ret = 0;
	int fd;

	fd = fd_open( filename, flags );
	if ( fd < 1 )
	{
		printf("Couldnt open %s\n", filename);
		return -1;
	}

	ft->fd = fd;
	ft->flags = flags;
	ft->filename = filename;


	/* set current file cursor postion and file size */
	ft->size = fd_size( fd );    /* if it RO mode - could be important */
	ft->position = fd_pos( fd ); /* file cursor position could be bigger 
	                                then file size*/
	ft->blk_size = DEFAULT_BLK_SIZE;

	return ret;
}


int file_seek( file_t *ft, off_t offset )
{
	int ret = 0;
	off_t new_off=0;

	
	ret = fd_seek( ft->fd, offset, FD_SEEK_CUR );
	if (ret == 0) 
	{
		new_off = fd_pos( ft->fd );
		if (new_off > ft->size)
		{
			fd_seek( ft->fd, ft->size, FD_SEEK_SET );
			new_off = fd_pos( ft->fd );
		}
		ft->position = new_off;
		ft->offset = offset;
		if ( offset != new_off )
		{
			printf("Offset set to %zd requested %zd\n", new_off, offset);
		}
	} else
	{
		printf("Cannot set offset\n");
		ret = -1;
	}

	return ret;
}


int file_pos( file_t *ft )
{
	int ret = 0;
	long pos = fd_pos( ft->fd );
	if (pos < 0)
	{
		printf("Cannot get file cursor position\n");
		ret = -1;
	}

	ft->position = pos;
	ret = pos;

	return ret;
}


int file_size( file_t *ft )
{
	int ret = 0;
	off_t size;

	size = fd_size( ft->fd );
	if (size < 0)
	{
		printf("Cannot get file size\n");
		return -1;
	}
	ft->size = size;
	ret = size;

	return ret;
}


int file_open( file_t *ft, const char *filename, int flags, int mode )
{
	int ret = 0;
	printf("Not implemented\n");
	ret = -1;
	return ret;
}


int file_read_blk( file_t *ft, uint8_t *buf )
{
	int ret = 0;

	if ( ft->fd < 0 )
		return -1;

	file_pos( ft );
	ret = fd_read( ft->fd, buf, ft->blk_size );
	fd_set_pos( ft->fd, ft->position ); //chck err?

	if ( ret < 0)
	{
		printf("Error while reading from file\n");
	}

	return ret;
}


int file_read( file_t *ft, uint8_t *buf, size_t count )
{
	int ret = 0;

	ret = fd_read( ft->fd, buf, count );
	if ( ret < 0 )
	{
		printf("Cannot read\n");
	} else if ( ret < count )
	{
		printf("Requestd %d readed %d\n", count, ret);
	}

	return ret;
}


int file_write_blk( file_t *ft, uint8_t *buf )
{
	int ret = 0;
	unsigned int sz;

	file_pos( ft );
	if ( ft->position + ft->blk_size <= ft->size )
	{
		sz = ft->blk_size;
	} else
	{
		sz = ft->size - ft->position; //when pos 0 ans size 1 then will write 1 byte
	}
	printf(" %d %x %u \n", ft->fd, buf, sz);
	ret = fd_write( ft->fd, buf, sz );
	if ( ret < 0 )
	{
		printf("Error while writing block to file\n");
	}
	fd_set_pos( ft->fd, ft->position );

	

	return ret;
}


int file_write( file_t *ft, uint8_t *buf, size_t count )
{
	int ret = 0;

	ret = fd_write( ft->fd, buf, count );
	if ( ret < 0 )
	{
		printf("Cannot write\n");
	} else if ( ret < count )
	{
		printf("Requested %d written %d\n", count, ret);
	}

	return ret;
}


int file_s_mode( file_t *ft, int mode )
{
	int ret = 0;

	if ( ft == NULL)
		return -1;

	ft->mode = mode;

	return ret;
}


int file_close( file_t *ft )
{
	int ret = 0;

	if ( ft->fd < 1 )
	{
		printf("File descriptor <1\n");
		ret = -1;
	}

	if ( ft->filename == NULL )
	{
		printf("File name is empty\n");
		ret = -1;
	}
	
	fd_close( ft->fd );
	memset( ft, 0, sizeof(file_t) );

	return ret;
}