diff --git a/tools/device.c b/tools/device.c index b27c9d6..3eda40b 100644 --- a/tools/device.c +++ b/tools/device.c @@ -6,38 +6,53 @@ #include #include +#include #include +#include #include "device.h" -#define SECTOR_SIZE (2048) -#define ISO_BLOCKS(X) (((X) / SECTOR_SIZE) + (((X)%SECTOR_SIZE)?1:0)) +int device_sector_size = 2048; + +#define BLOCKS(X) (((X)/device_sector_size)+(((X)%device_sector_size)?1:0)) int device_read_sector(void *data,off_t offset, void* buffer, size_t size) { - FILE* file = (FILE*)data; + int fd = (int)data; + int ret; - lseek(fileno(file), offset << 11, SEEK_SET); - return read(fileno(file), buffer, ISO_BLOCKS(size) << 11); + lseek(fd, offset << 11, SEEK_SET); + ret = read(fd, buffer, BLOCKS(size) << 11); + if (ret != BLOCKS(size) << 11) + return -1; + return 0; +} + +int device_write_sector(void *data,off_t offset, void* buffer, size_t size) +{ + int fd = (int)data; + int ret; + + lseek(fd, offset << 11, SEEK_SET); + ret = write(fd, buffer, BLOCKS(size) << 11); + if (ret != BLOCKS(size) << 11) + return -1; + return 0; } void device_close(void *data) { - FILE* file = (FILE*)data; - if (file) - fclose(file); + close((int)data); } -FILE *device_open(char *device) +int device_open(char *device, int flags) { - FILE* file; + int fd; if (device == NULL) - return NULL; + return -1; - file = fopen(device, "rb"); - if (file == NULL) - return NULL; + fd = open(device, flags); - return file; + return fd; } diff --git a/tools/device.h b/tools/device.h index 045ca44..7440b99 100644 --- a/tools/device.h +++ b/tools/device.h @@ -1,6 +1,6 @@ /* * - * (c) 2005 Laurent Vivier + * (c) 2005-2007 Laurent Vivier * */ @@ -10,6 +10,9 @@ #include -extern FILE *device_open(char *device); +extern int device_sector_size; + +extern int device_open(char *device, int flags); extern void device_close(void *data); +extern int device_write_sector(void *data,off_t offset, void* buffer, size_t size); extern int device_read_sector(void *data, off_t offset, void* buffer, size_t size);