#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 = 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_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_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 %zu 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 } 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 %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_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; } 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 = 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] = 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 = 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; }