From 22608b594ed9a54c4bfe18b7af721d003ccbb080 Mon Sep 17 00:00:00 2001 From: rocky Date: Sat, 15 Apr 2006 03:05:14 +0000 Subject: [PATCH] stream: add getpos routine udf: save last read position. --- include/cdio/udf_file.h | 5 +- lib/driver/_cdio_stream.c | 255 ++++++++++++++++++++------------------ lib/driver/_cdio_stream.h | 37 ++++-- lib/driver/libcdio.sym | 1 + lib/udf/udf_file.c | 14 ++- lib/udf/udf_fs.c | 4 +- lib/udf/udf_private.h | 6 +- 7 files changed, 179 insertions(+), 143 deletions(-) diff --git a/include/cdio/udf_file.h b/include/cdio/udf_file.h index 1a245f2e..3fc9b13f 100644 --- a/include/cdio/udf_file.h +++ b/include/cdio/udf_file.h @@ -1,5 +1,5 @@ /* - $Id: udf_file.h,v 1.10 2006/04/14 21:09:55 rocky Exp $ + $Id: udf_file.h,v 1.11 2006/04/15 03:05:14 rocky Exp $ Copyright (C) 2005, 2006 Rocky Bernstein This program is free software; you can redistribute it and/or modify @@ -71,7 +71,8 @@ extern "C" { /** Attempts to read up to count bytes from UDF directory entry p_udf_dirent into the buffer starting at buf. buf should be a - multiple of UDF_BLOCKSIZE bytes. + multiple of UDF_BLOCKSIZE bytes. Reading continues after the + point at which we last read or from the beginning the first time. If count is zero, read() returns zero and has no other results. If count is greater than SSIZE_MAX, the result is unspecified. diff --git a/lib/driver/_cdio_stream.c b/lib/driver/_cdio_stream.c index dd01090e..f6e0233f 100644 --- a/lib/driver/_cdio_stream.c +++ b/lib/driver/_cdio_stream.c @@ -1,8 +1,8 @@ /* - $Id: _cdio_stream.c,v 1.7 2005/03/18 12:56:00 rocky Exp $ + $Id: _cdio_stream.c,v 1.8 2006/04/15 03:05:14 rocky Exp $ Copyright (C) 2000, 2004, 2005 Herbert Valerio Riedel - Copyright (C) 2005 Rocky Bernstein + Copyright (C) 2005, 2006 Rocky Bernstein This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -35,7 +35,7 @@ #include #include "_cdio_stream.h" -static const char _rcsid[] = "$Id: _cdio_stream.c,v 1.7 2005/03/18 12:56:00 rocky Exp $"; +static const char _rcsid[] = "$Id: _cdio_stream.c,v 1.8 2006/04/15 03:05:14 rocky Exp $"; /* * DataSource implementations @@ -48,123 +48,6 @@ struct _CdioDataSource { long position; }; -/* - Open if not already open. - Return false if we hit an error. Errno should be set for that error. -*/ -static bool -_cdio_stream_open_if_necessary(CdioDataSource_t *p_obj) -{ - if (!p_obj) return false; - - if (!p_obj->is_open) { - if (p_obj->op.open(p_obj->user_data)) { - cdio_warn ("could not open input stream..."); - return false; - } else { - cdio_debug ("opened source..."); - p_obj->is_open = 1; - p_obj->position = 0; - } - } - return true; -} - -/*! - Like 3 fseek and in fact may be the same. - - This function sets the file position indicator for the stream - pointed to by stream. The new position, measured in bytes, is obtained - by adding offset bytes to the position specified by whence. If whence - is set to SEEK_SET, SEEK_CUR, or SEEK_END, the offset is relative to - the start of the file, the current position indicator, or end-of-file, - respectively. A successful call to the fseek function clears the end- - of-file indicator for the stream and undoes any effects of the - ungetc(3) function on the same stream. - - @return unpon successful completion, DRIVER_OP_SUCCESS, else, - DRIVER_OP_ERROR is returned and the global variable errno is set to - indicate the error. -*/ -driver_return_code_t -cdio_stream_seek(CdioDataSource_t* p_obj, long int offset, int whence) -{ - if (!p_obj) return DRIVER_OP_UNINIT; - - if (!_cdio_stream_open_if_necessary(p_obj)) - /* errno is set by _cdio_stream_open_if necessary. */ - return DRIVER_OP_ERROR; - - if (offset < 0) return DRIVER_OP_ERROR; - - if (p_obj->position != offset) { -#ifdef STREAM_DEBUG - cdio_warn("had to reposition DataSource from %ld to %ld!", obj->position, offset); -#endif - p_obj->position = offset; - return p_obj->op.seek(p_obj->user_data, offset, whence); - } - - return 0; -} - -CdioDataSource_t * -cdio_stream_new(void *user_data, const cdio_stream_io_functions *funcs) -{ - CdioDataSource_t *new_obj; - - new_obj = calloc (1, sizeof (CdioDataSource_t)); - - new_obj->user_data = user_data; - memcpy(&(new_obj->op), funcs, sizeof(cdio_stream_io_functions)); - - return new_obj; -} - -/*! - Like fread(3) and in fact may be the same. - - DESCRIPTION: - The function fread reads nmemb elements of data, each size bytes long, - from the stream pointed to by stream, storing them at the location - given by ptr. - - RETURN VALUE: - return the number of items successfully read or written (i.e., - not the number of characters). If an error occurs, or the - end-of-file is reached, the return value is a short item count - (or zero). - - We do not distinguish between end-of-file and error, and callers - must use feof(3) and ferror(3) to determine which occurred. -*/ -long -cdio_stream_read(CdioDataSource_t* p_obj, void *ptr, long size, long nmemb) -{ - long read_bytes; - - if (!p_obj) return 0; - if (!_cdio_stream_open_if_necessary(p_obj)) return 0; - - read_bytes = (p_obj->op.read)(p_obj->user_data, ptr, size*nmemb); - p_obj->position += read_bytes; - - return read_bytes; -} - -/*! - Return whatever size of stream reports, I guess unit size is bytes. - On error return -1; - */ -long int -cdio_stream_stat(CdioDataSource_t *p_obj) -{ - if (!p_obj) return -1; - if (!_cdio_stream_open_if_necessary(p_obj)) return -1; - - return p_obj->op.stat(p_obj->user_data); -} - void cdio_stream_close(CdioDataSource_t *p_obj) { @@ -190,6 +73,138 @@ cdio_stream_destroy(CdioDataSource_t *p_obj) free(p_obj); } +/** + Like 3 fgetpos. + + This function gets the current file position indicator for the stream + pointed to by stream. + + @return unpon successful completion, return value is positive, else, + the global variable errno is set to indicate the error. +*/ +ssize_t +cdio_stream_getpos(CdioDataSource_t* p_obj, /*out*/ ssize_t *i_offset) +{ + if (!p_obj || !p_obj->is_open) return DRIVER_OP_UNINIT; + return *i_offset = p_obj->position; +} + +CdioDataSource_t * +cdio_stream_new(void *user_data, const cdio_stream_io_functions *funcs) +{ + CdioDataSource_t *new_obj; + + new_obj = calloc (1, sizeof (CdioDataSource_t)); + + new_obj->user_data = user_data; + memcpy(&(new_obj->op), funcs, sizeof(cdio_stream_io_functions)); + + return new_obj; +} + +/* + Open if not already open. + Return false if we hit an error. Errno should be set for that error. +*/ +static bool +_cdio_stream_open_if_necessary(CdioDataSource_t *p_obj) +{ + if (!p_obj) return false; + + if (!p_obj->is_open) { + if (p_obj->op.open(p_obj->user_data)) { + cdio_warn ("could not open input stream..."); + return false; + } else { + cdio_debug ("opened source..."); + p_obj->is_open = 1; + p_obj->position = 0; + } + } + return true; +} + +/** + Like fread(3) and in fact may be the same. + + DESCRIPTION: + The function fread reads nmemb elements of data, each size bytes long, + from the stream pointed to by stream, storing them at the location + given by ptr. + + RETURN VALUE: + return the number of items successfully read or written (i.e., + not the number of characters). If an error occurs, or the + end-of-file is reached, the return value is a short item count + (or zero). + + We do not distinguish between end-of-file and error, and callers + must use feof(3) and ferror(3) to determine which occurred. +*/ +ssize_t +cdio_stream_read(CdioDataSource_t* p_obj, void *ptr, long size, long nmemb) +{ + long read_bytes; + + if (!p_obj) return 0; + if (!_cdio_stream_open_if_necessary(p_obj)) return 0; + + read_bytes = (p_obj->op.read)(p_obj->user_data, ptr, size*nmemb); + p_obj->position += read_bytes; + + return read_bytes; +} + +/** + Like 3 fseek and in fact may be the same. + + This function sets the file position indicator for the stream + pointed to by stream. The new position, measured in bytes, is obtained + by adding offset bytes to the position specified by whence. If whence + is set to SEEK_SET, SEEK_CUR, or SEEK_END, the offset is relative to + the start of the file, the current position indicator, or end-of-file, + respectively. A successful call to the fseek function clears the end- + of-file indicator for the stream and undoes any effects of the + ungetc(3) function on the same stream. + + @return unpon successful completion, return value is positive, else, + the global variable errno is set to indicate the error. +*/ +ssize_t +cdio_stream_seek(CdioDataSource_t* p_obj, ssize_t offset, int whence) +{ + if (!p_obj) return DRIVER_OP_UNINIT; + + if (!_cdio_stream_open_if_necessary(p_obj)) + /* errno is set by _cdio_stream_open_if necessary. */ + return DRIVER_OP_ERROR; + + if (offset < 0) return DRIVER_OP_ERROR; + + if (p_obj->position != offset) { +#ifdef STREAM_DEBUG + cdio_warn("had to reposition DataSource from %ld to %ld!", obj->position, offset); +#endif + p_obj->position = offset; + return p_obj->op.seek(p_obj->user_data, offset, whence); + } + + return 0; +} + +/** + Return whatever size of stream reports, I guess unit size is bytes. + On error return -1; + */ +ssize_t +cdio_stream_stat(CdioDataSource_t *p_obj) +{ + if (!p_obj) return -1; + if (!_cdio_stream_open_if_necessary(p_obj)) return -1; + + return p_obj->op.stat(p_obj->user_data); +} + /* * Local variables: diff --git a/lib/driver/_cdio_stream.h b/lib/driver/_cdio_stream.h index 51ffabcb..aa58b7c4 100644 --- a/lib/driver/_cdio_stream.h +++ b/lib/driver/_cdio_stream.h @@ -1,8 +1,9 @@ /* - $Id: _cdio_stream.h,v 1.3 2005/02/05 04:25:14 rocky Exp $ + $Id: _cdio_stream.h,v 1.4 2006/04/15 03:05:14 rocky Exp $ Copyright (C) 2000 Herbert Valerio Riedel - Copyright (C) 2003, 2004, 2005 Rocky Bernstein + Copyright (C) 2003, 2004, 2005, 2006 Rocky Bernstein + This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -57,10 +58,22 @@ extern "C" { cdio_data_free_t free; } cdio_stream_io_functions; + /** + Like 3 fgetpos. + + This function gets the current file position indicator for the stream + pointed to by stream. + + @return unpon successful completion, return value is positive, else, + the global variable errno is set to indicate the error. + */ + ssize_t cdio_stream_getpos(CdioDataSource_t* p_obj, + /*out*/ ssize_t *i_offset); + CdioDataSource_t * cdio_stream_new(void *user_data, const cdio_stream_io_functions *funcs); - /*! + /** Like fread(3) and in fact may be the same. DESCRIPTION: @@ -77,10 +90,10 @@ extern "C" { We do not distinguish between end-of-file and error, and callers must use feof(3) and ferror(3) to determine which occurred. */ - long cdio_stream_read(CdioDataSource_t* p_obj, void *ptr, long i_size, - long nmemb); + ssize_t cdio_stream_read(CdioDataSource_t* p_obj, void *ptr, long i_size, + long nmemb); - /*! + /** Like fseek(3) and in fact may be the same. This function sets the file position indicator for the stream @@ -96,17 +109,17 @@ extern "C" { DRIVER_OP_ERROR is returned and the global variable errno is set to indicate the error. */ - driver_return_code_t cdio_stream_seek(CdioDataSource_t *p_obj, long offset, - int whence); + ssize_t cdio_stream_seek(CdioDataSource_t *p_obj, ssize_t i_offset, + int whence); - /*! + /** Return whatever size of stream reports, I guess unit size is bytes. On error return -1; */ - long int cdio_stream_stat(CdioDataSource_t *p_obj); + ssize_t cdio_stream_stat(CdioDataSource_t *p_obj); - /*! - Deallocate resources assocaited with p_obj. After this p_obj is unusable. + /** + Deallocate resources associated with p_obj. After this p_obj is unusable. */ void cdio_stream_destroy(CdioDataSource_t *p_obj); diff --git a/lib/driver/libcdio.sym b/lib/driver/libcdio.sym index f9a0d619..3e2ca4b0 100644 --- a/lib/driver/libcdio.sym +++ b/lib/driver/libcdio.sym @@ -157,6 +157,7 @@ cdio_set_drive_speed cdio_set_speed cdio_stdio_destroy cdio_stdio_new +cdio_stream_getpos cdio_stream_read cdio_stream_seek cdio_to_bcd8 diff --git a/lib/udf/udf_file.c b/lib/udf/udf_file.c index 612b310e..a1ac66a3 100644 --- a/lib/udf/udf_file.c +++ b/lib/udf/udf_file.c @@ -1,5 +1,5 @@ /* - $Id: udf_file.c,v 1.9 2006/04/11 05:47:58 rocky Exp $ + $Id: udf_file.c,v 1.10 2006/04/15 03:05:14 rocky Exp $ Copyright (C) 2005, 2006 Rocky Bernstein @@ -99,7 +99,8 @@ udf_is_dir(const udf_dirent_t *p_udf_dirent) /** Attempts to read up to count bytes from UDF directory entry p_udf_dirent into the buffer starting at buf. buf should be a - multiple of UDF_BLOCKSIZE bytes. + multiple of UDF_BLOCKSIZE bytes. Reading continues after the point + at which we last read or from the beginning the first time. If count is zero, read() returns zero and has no other results. If count is greater than SSIZE_MAX, the result is unspecified. @@ -112,7 +113,7 @@ udf_read_block(const udf_dirent_t *p_udf_dirent, void * buf, size_t count) { if (count == 0) return 0; else { - const udf_t *p_udf = p_udf_dirent->p_udf; + udf_t *p_udf = p_udf_dirent->p_udf; uint8_t data[UDF_BLOCKSIZE]; const udf_file_entry_t *p_udf_fe = (udf_file_entry_t *) data; driver_return_code_t ret; @@ -123,6 +124,9 @@ udf_read_block(const udf_dirent_t *p_udf_dirent, void * buf, size_t count) if (!udf_checktag(&p_udf_fe->tag, TAGID_FILE_ENTRY)) { uint32_t i_lba_start, i_lba_end; udf_get_lba( p_udf_fe, &i_lba_start, &i_lba_end); + + /* set i_lba_start to position of where we last left off. */ + i_lba_start += (p_udf->i_position / UDF_BLOCKSIZE); if ( (i_lba_end - i_lba_start+1) < count ) { printf("Warning: don't know how to handle yet\n" ); @@ -131,7 +135,9 @@ udf_read_block(const udf_dirent_t *p_udf_dirent, void * buf, size_t count) const uint32_t i_lba = p_udf->i_part_start+i_lba_start; ret = udf_read_sectors(p_udf, buf, i_lba, count); if (DRIVER_OP_SUCCESS == ret) { - return MIN(i_file_length, count * UDF_BLOCKSIZE); + ssize_t i_read_len = MIN(i_file_length, count * UDF_BLOCKSIZE); + p_udf->i_position += i_read_len; + return i_read_len; } } diff --git a/lib/udf/udf_fs.c b/lib/udf/udf_fs.c index f32f7862..b02d428a 100644 --- a/lib/udf/udf_fs.c +++ b/lib/udf/udf_fs.c @@ -1,7 +1,7 @@ /* - $Id: udf_fs.c,v 1.18 2006/04/11 06:46:29 rocky Exp $ + $Id: udf_fs.c,v 1.19 2006/04/15 03:05:14 rocky Exp $ - Copyright (C) 2005 Rocky Bernstein + Copyright (C) 2005, 2006 Rocky Bernstein This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by diff --git a/lib/udf/udf_private.h b/lib/udf/udf_private.h index a34d7652..d665be63 100644 --- a/lib/udf/udf_private.h +++ b/lib/udf/udf_private.h @@ -1,5 +1,5 @@ /* - $Id: udf_private.h,v 1.8 2006/04/11 05:47:58 rocky Exp $ + $Id: udf_private.h,v 1.9 2006/04/15 03:05:14 rocky Exp $ Copyright (C) 2005, 2006 Rocky Bernstein @@ -34,8 +34,8 @@ struct udf_s { bool b_stream; /* Use stream pointer, else use - p_cdio. - */ + p_cdio. */ + ssize_t i_position; /* Position in file if positive. */ CdioDataSource_t *stream; /* Stream pointer if stream */ CdIo_t *cdio; /* Cdio pointer if read device */ anchor_vol_desc_ptr_t anchor_vol_desc_ptr;