From 0db7640ed55e48e41922c3343654669ba2a20ab7 Mon Sep 17 00:00:00 2001 From: Laurent Vivier Date: Mon, 21 Nov 2005 23:55:56 +0000 Subject: [PATCH] First revision --- libstream/Makefile | 23 +++++ libstream/stream_close.c | 21 +++++ libstream/stream_lseek.c | 12 +++ libstream/stream_open.c | 192 +++++++++++++++++++++++++++++++++++++++ libstream/stream_read.c | 12 +++ 5 files changed, 260 insertions(+) create mode 100644 libstream/Makefile create mode 100644 libstream/stream_close.c create mode 100644 libstream/stream_lseek.c create mode 100644 libstream/stream_open.c create mode 100644 libstream/stream_read.c diff --git a/libstream/Makefile b/libstream/Makefile new file mode 100644 index 0000000..d029ee9 --- /dev/null +++ b/libstream/Makefile @@ -0,0 +1,23 @@ +# +# (c) 2005 Laurent Vivier +# + +TOP = $(shell pwd) +VPATH=$(TOP) + +CFLAGS = -nostdlib -nodefaultlibs -Wall -Werror -Wno-multichar -fpic -g +CPPFLAGS = -I$(TOP)/../libfloppy -I$(TOP)/../libscsi -I$(TOP)/../libblock \ + -I$(TOP)/../libblock -I$(TOP)/../libiso9660 -I$(TOP)/../libmacos \ + -I$(TOP)/../libcontainer -I$(TOP) + +LIBRARY = libstream.a + +SOURCES = stream_close.c stream_open.c stream_read.c stream_lseek.c + +HEADERS = libstream.h + +DISTFILES = $(SOURCES) $(HEADERS) + +all: $(LIBRARY) + +include $(TOP)/../Rules.mk diff --git a/libstream/stream_close.c b/libstream/stream_close.c new file mode 100644 index 0000000..4fbdc3d --- /dev/null +++ b/libstream/stream_close.c @@ -0,0 +1,21 @@ +/* + * + * (c) 2005 Laurent Vivier + * + */ + +#include + +#include "libstream.h" + +int stream_close(stream_t *stream) +{ + if (stream->fs.close(stream->fs.file) != 0) + return -1; + if (stream->fs.umount(stream->fs.volume) != 0) + return -1; + if (stream->device.close(stream->device.data) != 0) + return -1; + free(stream); + return 0; +} diff --git a/libstream/stream_lseek.c b/libstream/stream_lseek.c new file mode 100644 index 0000000..454b9fc --- /dev/null +++ b/libstream/stream_lseek.c @@ -0,0 +1,12 @@ +/* + * + * (c) 2005 Laurent Vivier + * + */ + +#include "libstream.h" + +int stream_lseek(stream_t *stream, long offset, int whence) +{ + return stream->fs.lseek(stream->fs.file, offset, whence); +} diff --git a/libstream/stream_open.c b/libstream/stream_open.c new file mode 100644 index 0000000..fe20e8e --- /dev/null +++ b/libstream/stream_open.c @@ -0,0 +1,192 @@ +/* + * + * (c) 2005 Laurent Vivier + * + */ + +#include +#include + +#include "libstream.h" +#include +#include +#include +#include +#include + +typedef enum { + device_FLOPPY, + device_SCSI, +} device_t; + +typedef enum { + fs_BLOCK, + fs_CONTAINER, + fs_ISO9660, +} fs_t; + +static char* get_fs(char *path, fs_t *fs) +{ + if (strncmp("block:", path, 6) == 0) + { + *fs = fs_BLOCK; + return path + 6; + } else if (strncmp("container:", path, 10) == 0) + { + *fs = fs_CONTAINER; + return path + 10; + } else if (strncmp("iso9660:", path, 8) == 0) + { + *fs = fs_ISO9660; + return path + 8; + } + return NULL; +} + +static char *get_device(char* path, + device_t *device, int *unit, int* partition) +{ + int nb; + + if (*path != '(') + return NULL; + path++; + + if (strncmp("fd", path, 2) == 0) { + *device = device_FLOPPY; + path += 2; + } else if (strncmp("sd", path, 2) == 0) { + *device = device_SCSI; + path += 2; + } else + return NULL; + + nb = 0; + while ( (*path >= '0') && (*path <= '9') ) { + nb = (nb * 10) + (*path - '0'); + path++; + } + *unit = nb; + + *partition = -1; + if ( (*path == 0) || (*path == ')') ) + { + path++; + return path; + } + + if (*path != ',') + return NULL; + path++; + + nb = 0; + while ( (*path >= '0') && (*path <= '9') ) { + nb = (nb * 10) + (*path - '0'); + path++; + } + *partition = nb; + if ( (*path == 0) || (*path == ')') ) + { + path++; + return path; + } + + return NULL; +} + +stream_t *stream_open(char *dev) +{ + stream_t *stream; + fs_t fs; + device_t device; + int unit, partition; + char *current; + + current = get_fs(dev, &fs); + if (current == NULL) + return NULL; + current = get_device(current, &device, &unit, &partition); + if (current == NULL) + return NULL; + + stream = (stream_t*)malloc(sizeof(stream_t)); + + switch(device) + { +#if 0 + case device_FLOPPY: + if (partition != -1) + { + free(stream); + return NULL; + } + stream->device.data = floppy_open(unit); + if (stream->device.data == NULL) + { + free(stream); + return NULL; + } + stream->device.read_sector = (stream_read_sector_t)floppy_read_sector; + stream->device.close = (stream_close_t)floppy_close; + break; +#endif + + case device_SCSI: + stream->device.data = scsi_open(unit); + if (stream->device.data == NULL) + { + free(stream); + return NULL; + } + stream->device.read_sector = (stream_read_sector_t)scsi_read_sector; + stream->device.close = (stream_close_t)scsi_close; + break; + default: + free(stream); + stream = NULL; + break; + } + + switch(fs) + { +#if 0 + case fs_BLOCK: + if (stream->fs.data == NULL) + goto outfs; + stream->fs.read = block_read; + stream->fs.seek = block_seek; + stream->fs.close = block_close; + break; + case fs_CONTAINER: + stream->fs.data = container_open(&stream->device, current); + if (stream->fs.data == NULL) + goto outfs; + stream->fs.read = container_read; + stream->fs.seek = container_seek; + stream->fs.close = container_close; + break; +#endif + case fs_ISO9660: + stream->fs.volume = iso9660_mount(&stream->device); + if (stream->fs.volume == NULL) + goto outfs; + stream->fs.file = iso9660_open(stream->fs.volume, current); + if (stream->fs.file == NULL) + { + iso9660_umount(stream->fs.volume); + goto outfs; + } + stream->fs.read = (stream_read_t)iso9660_read; + stream->fs.lseek = (stream_lseek_t)iso9660_lseek; + stream->fs.close = (stream_close_t)iso9660_close; + stream->fs.umount = (stream_umount_t)iso9660_umount; + break; + default: +outfs: + stream->device.close(stream->device.data); + free(stream); + return NULL; + } + + return stream; +} diff --git a/libstream/stream_read.c b/libstream/stream_read.c new file mode 100644 index 0000000..d74a967 --- /dev/null +++ b/libstream/stream_read.c @@ -0,0 +1,12 @@ +/* + * + * (c) 2005 Laurent Vivier + * + */ + +#include "libstream.h" + +int stream_read(stream_t *stream, void *buf, size_t count) +{ + return stream->fs.read(stream->fs.file, buf, count); +}