introduce best fseek() and offset management

This commit is contained in:
Laurent Vivier 2005-11-14 19:30:39 +00:00
parent 383b010988
commit 3e469d90e0
4 changed files with 50 additions and 44 deletions

View File

@ -5,16 +5,17 @@
TOP = $(shell pwd)
TARGET = native
CFLAGS = -nostdlib -nodefaultlibs -Wall -Werror -Wno-multichar -fpic
CPPFLAGS = -I$(TOP) -I$(TOP)/../libmacos -DARCH_M68K
CPPFLAGS =
LIBRARY = libiso9660.a
SOURCES = iso9660_mount.c iso9660_opendir.c \
iso9660_closedir.c iso9660_readdir.c \
iso9660_is_directory.c iso9660_open.c \
iso9660_read.c iso9660_close.c iso9660_init.c
iso9660_read.c iso9660_close.c iso9660_init.c \
iso9660_fseek.c
HEADERS = libiso9660.a
HEADERS = libiso9660.h
DISTFILES = $(SOURCES) $(HEADERS)
@ -56,9 +57,9 @@ dist:
for file in $(DISTFILES); do \
dir=$$(dirname $$file); \
if [ "$$dir" != "" ] ; then \
mkdir -p $(DISTDIR)/libunix/$$dir; \
mkdir -p $(DISTDIR)/libiso9660/$$dir; \
fi; \
cp -p $(TARGET)/$$file $(DISTDIR)/libunix/$$file; \
cp -p $(TARGET)/$$file $(DISTDIR)/libiso9660/$$file; \
done
clean:

View File

@ -26,9 +26,10 @@ iso9660_FILE* iso9660_open(char* pathname)
if (file == NULL)
return NULL;
file->extent = isonum_733((unsigned char *)idr->extent);
file->len = isonum_733((unsigned char *)idr->size);
file->index = sizeof (file->buffer);
file->base = isonum_733((unsigned char *)idr->extent);
file->size = isonum_733((unsigned char *)idr->size);
file->offset = 0;
file->current = -1;
free(idr);

View File

@ -15,55 +15,55 @@ ssize_t iso9660_read(iso9660_FILE *file, void *buf, size_t count)
{
size_t read = 0;
if ( count > (file->size - file->offset) )
count = file->size - file->offset;
while (count > 0)
{
size_t part;
int offset_extent;
int offset_index;
/* direct i/o */
offset_extent = file->base +
(file->offset / ISO9660_EXTENT_SIZE);
offset_index = file->offset % ISO9660_EXTENT_SIZE;
if ( (file->index == sizeof (file->buffer)) &&
(count > sizeof (file->buffer)) )
if (file->current != offset_extent)
{
int extents;
if ( (offset_index == 0) &&
(count >= ISO9660_EXTENT_SIZE) )
{
/* direct i/o */
if (file->len < count)
extents = file->len / sizeof(file->buffer);
else
extents = count / sizeof (file->buffer);
int extents_nb;
part = extents * sizeof (file->buffer);
extents_nb = count / ISO9660_EXTENT_SIZE;
__iso9660_device_read(file->extent,
buf + read, part);
file->len -= part;
file->extent += extents;
count -= part;
read += part;
part = extents_nb * ISO9660_EXTENT_SIZE;
continue;
}
__iso9660_device_read(offset_extent,
buf + read, part);
file->offset += part;
count -= part;
read += part;
if (file->index == sizeof (file->buffer))
{
if (file->len <= 0)
return read;
__iso9660_device_read(file->extent,
continue;
}
file->current = offset_extent;
__iso9660_device_read(offset_extent,
file->buffer,
sizeof (file->buffer));
file->len -= sizeof (file->buffer);
file->extent++;
file->index = 0;
ISO9660_EXTENT_SIZE);
}
if (count > sizeof(file->buffer) - file->index)
part = sizeof(file->buffer) - file->index;
else
part = ISO9660_EXTENT_SIZE - offset_index;
if (count < part)
part = count;
memcpy(buf + read, file->buffer + file->index, part);
memcpy(buf + read, file->buffer + offset_index, part);
file->offset += part;
count -= part;
file->index += part;
read += part;
}

View File

@ -12,18 +12,21 @@
typedef void (*iso9660_read_t)(off_t offset, void* buffer, size_t size);
#define ISO9660_EXTENT_SIZE (2048)
typedef struct iso9660_DIR {
int extent;
int len;
int index;
unsigned char buffer[2048];
unsigned char buffer[ISO9660_EXTENT_SIZE];
} iso9660_DIR;
typedef struct iso9660_FILE {
int extent;
int len;
int index;
unsigned char buffer[2048];
int base; /* first extent of the file */
int size; /* size of the file */
int offset;
int current;
unsigned char buffer[ISO9660_EXTENT_SIZE];
} iso9660_FILE;
static inline int isonum_721(char *p)
@ -56,5 +59,6 @@ extern iso9660_FILE* iso9660_open(char* pathname);
extern ssize_t iso9660_read(iso9660_FILE *file, void *buf, size_t count);
extern void iso9660_close(iso9660_FILE *file);
extern void iso9660_init(iso9660_read_t func);
extern int iso9660_fseek(iso9660_FILE *file, long offset, int whence);
#endif /* __LIBISO9660_H__ */