summaryrefslogtreecommitdiff
path: root/core.c
blob: 1fe289153d0b9a9b5f7a680b00363355a54aa2c5 (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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#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;
	}
	ret = off_new;

	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;

	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 = (file_t *)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;

	uint8_t fn_sz = strlen( filename );
	char *fn = (char *)malloc( fn_sz );
	memcpy( fn, filename, fn_sz );
	ft->filename = fn;


	/* 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_seekp( file_t *ft, off_t offset )
{
	int ret = 0;
	off_t new_off = 0;

	ret = fd_seek( ft->fd, offset, FD_SEEK_SET );
	ft->offset = fd_seek( ft->fd, 0, FD_SEEK_CUR ); //cul be errors?
	
	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_resize( file_t *ft, size_t size )
{
	int ret = 0;
	off_t cur_pos;
	off_t cur_size;
	off_t resize;
	uint8_t *buf;

	cur_pos = fd_pos( ft->fd );
	cur_size = fd_size( ft->fd );
	if (cur_size < 0)
	{
		return -1;
	}
	buf = (uint8_t *)malloc( size );
	memset( buf, 0, size );
	fd_seek( ft->fd, cur_size, FD_SEEK_SET );
	resize = fd_write( ft->fd, buf, size );
	fd_seek( ft->fd, cur_pos, FD_SEEK_SET );

	ret = resize;

	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 sz )
{
	int ret = 0;

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

	file_pos( ft );
	int read_sz = sz;
	if (read_sz > ft->blk_size)
	{	
		read_sz = ft->blk_size;
	}
	ret = fd_read( ft->fd, buf, read_sz );
	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 %zu readed %d\n", count, ret);
	}

	return ret;
}


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

	file_pos( ft );
	if ( ft->position + ft->blk_size <= ft->size )
	{
		write_sz = ft->blk_size;
	} else
	{
		write_sz = ft->size - ft->position; //when pos 0 ans size 1 then will write 1 byte
	}

	if (write_sz > sz)
	{
		write_sz = sz;
	}

	ret = fd_write( ft->fd, buf, write_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 %zu 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_s_bufs( file_t *ft, unsigned int size )
{
	int ret = 0;

	ft->blk_size = size;

	return ret;
}


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

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

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

	return ret;
}


uint8_t **dir_list( char *path)
{
	uint8_t **ret = NULL, **new_ptr;
	int cnt=0;
	DIR *dp;
	struct dirent *ep;
	int str_sz = 0;

	/* lets use libc example. they know what they are doing */
	dp = opendir( path );
	if ( dp != NULL )
	{
		while ( (ep = readdir( dp )) != NULL )
		{
			//count one more in da list
			cnt += 1;
			//lets alloc pointer on pointer where we put pointer
			new_ptr = (uint8_t **)realloc( ret, sizeof(uint8_t*)*(cnt) );
			if ( new_ptr == NULL )
				goto failed_realloc;
			ret = new_ptr;
			str_sz = strlen(ep->d_name);
			ret[cnt-1] = (uint8_t *)malloc( str_sz+1 );
			memcpy( ret[cnt-1], ep->d_name, str_sz );
			ret[cnt-1][str_sz] = 0;
		}
		closedir( dp );
		dp = NULL;
	} else
	{
		perror("Couldnt list directory files");
		*ret = NULL;
	}

	//add NULL element at the end
	new_ptr = (uint8_t **)realloc( ret, sizeof(uint8_t*)*(cnt+1) );
	if ( new_ptr == NULL )
		goto failed_add_entry;
	ret = new_ptr;
	ret[cnt] = NULL;

	return ret;

failed_realloc:
	closedir( dp );
failed_add_entry:
	//forgot to free ret
	return NULL;
}