diff --git a/example/udf1.c b/example/udf1.c index 1f59bb86..70f1cd93 100644 --- a/example/udf1.c +++ b/example/udf1.c @@ -1,5 +1,5 @@ /* - $Id: udf1.c,v 1.6 2005/10/25 13:20:31 rocky Exp $ + $Id: udf1.c,v 1.7 2005/10/26 02:05:53 rocky Exp $ Copyright (C) 2005 Rocky Bernstein @@ -55,7 +55,11 @@ udf_file_t * list_files(const udf_t *p_udf, udf_file_t *p_udf_file, char *psz_token) { if (!p_udf_file) return NULL; - if (psz_token) printf("%s\n", psz_token); + if (psz_token) { + time_t mod_time = udf_get_modification_time(p_udf_file); + printf("%s %s\n", psz_token, ctime(&mod_time)); + } + while (udf_get_next(p_udf, p_udf_file)) { char *next_tok = strtok(psz_token, udf_PATH_DELIMITERS); diff --git a/include/cdio/Makefile.am b/include/cdio/Makefile.am index 2d815af3..b8f1c595 100644 --- a/include/cdio/Makefile.am +++ b/include/cdio/Makefile.am @@ -1,4 +1,4 @@ -# $Id: Makefile.am,v 1.25 2005/10/13 02:37:20 rocky Exp $ +# $Id: Makefile.am,v 1.26 2005/10/26 02:05:53 rocky Exp $ # # Copyright (C) 2003, 2004 Rocky Bernstein # @@ -48,6 +48,7 @@ libcdioinclude_HEADERS = \ track.h \ types.h \ udf.h \ + udf_time.h \ util.h \ version.h \ xa.h diff --git a/include/cdio/udf.h b/include/cdio/udf.h index c2190f79..d55a6392 100644 --- a/include/cdio/udf.h +++ b/include/cdio/udf.h @@ -1,5 +1,5 @@ /* - $Id: udf.h,v 1.9 2005/10/25 13:19:05 rocky Exp $ + $Id: udf.h,v 1.10 2005/10/26 02:05:54 rocky Exp $ Copyright (C) 2005 Rocky Bernstein This program is free software; you can redistribute it and/or modify @@ -56,6 +56,10 @@ typedef enum { expressions */ extern udf_enum1_t debug_udf_enums1; +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + /*! Seek to a position i_start and then read i_blocks. Number of blocks read is returned. One normally expects the return to be equal to i_blocks. @@ -132,7 +136,7 @@ udf_file_t *udf_get_next(const udf_t *p_udf, udf_file_t *p_udf_file); bool udf_close (udf_t *p_udf); /*! - free free resources associated with p_fe. + free free resources associated with p_udf_file. */ bool udf_file_free(udf_file_t *p_udf_file); @@ -141,5 +145,10 @@ bool udf_file_free(udf_file_t *p_udf_file); */ bool udf_is_dir(const udf_file_t *p_udf_file); +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#include #endif /*UDF_H*/ diff --git a/include/cdio/udf_time.h b/include/cdio/udf_time.h new file mode 100644 index 00000000..1be6ab5e --- /dev/null +++ b/include/cdio/udf_time.h @@ -0,0 +1,79 @@ +/* + $Id: udf_time.h,v 1.1 2005/10/26 02:05:54 rocky Exp $ + Copyright (C) 2005 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 + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +/*! + * \file udf_time.h + * + * \brief UDF time conversion and access files. + * +*/ + +#ifndef UDF_TIME_H +#define UDF_TIME_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/*! + Return the access time of the file + */ +time_t udf_get_access_time(const udf_file_t *p_udf_file); + +/*! + Return the modification time of the file + */ +time_t udf_get_modification_time(const udf_file_t *p_udf_file); + +/*! + Return the attr time of the file + */ +time_t udf_get_attr_time(const udf_file_t *p_udf_file); + +/*! + Return the access timestamp of the file + */ +udf_timestamp_t *udf_get_access_timestamp(const udf_file_t *p_udf_file); + +/*! + Return the modification timestamp of the file + */ +udf_timestamp_t *udf_get_modification_timestamp(const udf_file_t *p_udf_file); + +/*! + Return the attr timestamp of the file + */ +udf_timestamp_t *udf_get_attr_timestamp(const udf_file_t *p_udf_file); + +/*! + Convert a UDF timestamp to a time_t. If microseconds are desired, + use dest_usec. The return value is the same as dest. */ +time_t *udf_stamp_to_time(time_t *dest, long int *dest_usec, + const udf_timestamp_t src); + +udf_timestamp_t *udf_time_to_stamp(udf_timestamp_t *dest, + const struct timespec ts); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /*UDF_TIME_H*/ diff --git a/lib/udf/Makefile.am b/lib/udf/Makefile.am index 6ed82b61..160cc06c 100644 --- a/lib/udf/Makefile.am +++ b/lib/udf/Makefile.am @@ -34,7 +34,7 @@ noinst_HEADERS = udf_private.h lib_LTLIBRARIES = libudf.la -libudf_la_SOURCES = udf.c udf_fs.c +libudf_la_SOURCES = udf.c udf_fs.c udf_time.c libudf_la_LIBADD = @LIBCDIO_LIBS@ diff --git a/lib/udf/libudf.sym b/lib/udf/libudf.sym index a95913ed..aef51fba 100644 --- a/lib/udf/libudf.sym +++ b/lib/udf/libudf.sym @@ -16,3 +16,5 @@ udf_get_sub udf_is_dir udf_open udf_read_sectors +udf_stamp_to_time +udf_time_to_stamp diff --git a/lib/udf/udf_fs.c b/lib/udf/udf_fs.c index 33a9a1c1..9e73990f 100644 --- a/lib/udf/udf_fs.c +++ b/lib/udf/udf_fs.c @@ -1,5 +1,5 @@ /* - $Id: udf_fs.c,v 1.6 2005/10/25 13:19:05 rocky Exp $ + $Id: udf_fs.c,v 1.7 2005/10/26 02:05:54 rocky Exp $ Copyright (C) 2005 Rocky Bernstein @@ -279,6 +279,7 @@ udf_new_file(udf_file_entry_t *p_fe, uint32_t i_part_start, p_udf_file->i_part_start = i_part_start; p_udf_file->dir_left = uint64_from_le(p_fe->info_len); + memcpy(&(p_udf_file->fe), p_fe, sizeof(udf_file_entry_t)); udf_get_lba( p_fe, &(p_udf_file->dir_lba), &(p_udf_file->dir_end_lba) ); return p_udf_file; } @@ -530,6 +531,10 @@ udf_get_root (udf_t *p_udf, bool b_any_partition, partition_num_t i_partition) return NULL; } +#define free_and_null(x) \ + free(x); \ + x=NULL + /*! Close UDF and free resources associated with p_udf. */ @@ -545,7 +550,7 @@ udf_close (udf_t *p_udf) /* Get rid of root directory if allocated. */ - free(p_udf); + free_and_null(p_udf); return true; } @@ -585,6 +590,7 @@ udf_get_next(const udf_t *p_udf, udf_file_t *p_udf_file) if (p_udf_file->fid) { /* advance to next File Identifier Descriptor */ + /* FIXME: need to advance file entry (fe) as well. */ uint32_t ofs = 4 * ((sizeof(*(p_udf_file->fid)) + p_udf_file->fid->i_imp_use + p_udf_file->fid->i_file_id + 3) / 4); @@ -635,6 +641,7 @@ udf_get_next(const udf_t *p_udf, udf_file_t *p_udf_file) return NULL; } + /*! free free resources associated with p_fe. */ @@ -642,9 +649,9 @@ bool udf_file_free(udf_file_t *p_udf_file) { if (p_udf_file) { - free(p_udf_file->psz_name); - free(p_udf_file->sector); - free(p_udf_file); + free_and_null(p_udf_file->psz_name); + free_and_null(p_udf_file->sector); + free_and_null(p_udf_file); } return true; } diff --git a/lib/udf/udf_private.h b/lib/udf/udf_private.h index 98d88688..616645a1 100644 --- a/lib/udf/udf_private.h +++ b/lib/udf/udf_private.h @@ -1,5 +1,5 @@ /* - $Id: udf_private.h,v 1.1 2005/10/24 10:14:58 rocky Exp $ + $Id: udf_private.h,v 1.2 2005/10/26 02:05:54 rocky Exp $ Copyright (C) 2005 Rocky Bernstein @@ -51,6 +51,7 @@ struct udf_file_s uint32_t dir_lba, dir_end_lba; uint64_t dir_left; uint8_t *sector; + udf_file_entry_t fe; udf_fileid_desc_t *fid; }; diff --git a/lib/udf/udf_time.c b/lib/udf/udf_time.c new file mode 100644 index 00000000..020efd88 --- /dev/null +++ b/lib/udf/udf_time.c @@ -0,0 +1,206 @@ +/* + Copyright (C) 2005 Rocky Bernstein + Copyright (C) 1993, 1994, 1995, 1996, 1997 Free Software Foundation, Inc. + + Modified From part of the GNU C Library. + Contributed by Paul Eggert (eggert@twinsun.com). + + 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 + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +/* Some history from the GNU/Linux kernel from which this is also taken... + dgb 10/02/98: ripped this from glibc source to help convert + timestamps to unix time + + 10/04/98: added new table-based lookup after seeing how ugly the + gnu code is + + blf 09/27/99: ripped out all the old code and inserted new table from + John Brockmeyer (without leap second corrections) + rewrote udf_stamp_to_time and fixed timezone + accounting in udf_time_to_stamp. +*/ + +/* + * We don't take into account leap seconds. This may be correct or incorrect. + * For more NIST information (especially dealing with leap seconds), see: + * http://www.boulder.nist.gov/timefreq/pubs/bulletin/leapsecond.htm + */ + +#include +#include "udf_private.h" + +#define EPOCH_YEAR 1970 + +#ifndef __isleap +/* Nonzero if YEAR is a leap year (every 4 years, + except every 100th isn't, and every 400th is). */ +#define __isleap(year) \ + ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0)) +#endif + +/* How many days come before each month (0-12). */ +static const unsigned short int __mon_yday[2][13] = + { + /* Normal years. */ + { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }, + /* Leap years. */ + { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 } + }; + +#define MAX_YEAR_SECONDS 69 +#define SPD 0x15180 /* Seconds per day: 3600*24*/ +#define SPY(y,l,s) (SPD * (365*y+l)+s) /* Seconds per year */ + +static time_t year_seconds[MAX_YEAR_SECONDS]= { + /*1970*/ SPY( 0, 0,0), SPY( 1, 0,0), SPY( 2, 0,0), SPY( 3, 1,0), + /*1974*/ SPY( 4, 1,0), SPY( 5, 1,0), SPY( 6, 1,0), SPY( 7, 2,0), + /*1978*/ SPY( 8, 2,0), SPY( 9, 2,0), SPY(10, 2,0), SPY(11, 3,0), + /*1982*/ SPY(12, 3,0), SPY(13, 3,0), SPY(14, 3,0), SPY(15, 4,0), + /*1986*/ SPY(16, 4,0), SPY(17, 4,0), SPY(18, 4,0), SPY(19, 5,0), + /*1990*/ SPY(20, 5,0), SPY(21, 5,0), SPY(22, 5,0), SPY(23, 6,0), + /*1994*/ SPY(24, 6,0), SPY(25, 6,0), SPY(26, 6,0), SPY(27, 7,0), + /*1998*/ SPY(28, 7,0), SPY(29, 7,0), SPY(30, 7,0), SPY(31, 8,0), + /*2002*/ SPY(32, 8,0), SPY(33, 8,0), SPY(34, 8,0), SPY(35, 9,0), + /*2006*/ SPY(36, 9,0), SPY(37, 9,0), SPY(38, 9,0), SPY(39,10,0), + /*2010*/ SPY(40,10,0), SPY(41,10,0), SPY(42,10,0), SPY(43,11,0), + /*2014*/ SPY(44,11,0), SPY(45,11,0), SPY(46,11,0), SPY(47,12,0), + /*2018*/ SPY(48,12,0), SPY(49,12,0), SPY(50,12,0), SPY(51,13,0), + /*2022*/ SPY(52,13,0), SPY(53,13,0), SPY(54,13,0), SPY(55,14,0), + /*2026*/ SPY(56,14,0), SPY(57,14,0), SPY(58,14,0), SPY(59,15,0), + /*2030*/ SPY(60,15,0), SPY(61,15,0), SPY(62,15,0), SPY(63,16,0), + /*2034*/ SPY(64,16,0), SPY(65,16,0), SPY(66,16,0), SPY(67,17,0), + /*2038*/ SPY(68,17,0) +}; + +extern long timezone; + +#define SECS_PER_HOUR (60 * 60) +#define SECS_PER_DAY (SECS_PER_HOUR * 24) + +time_t * +udf_stamp_to_time(time_t *dest, long int *dest_usec, + const udf_timestamp_t src) +{ + int yday; + uint8_t type = src.type_tz >> 12; + int16_t offset; + + if (type == 1) { + offset = src.type_tz << 4; + /* sign extent offset */ + offset = (offset >> 4); + if (offset == -2047) /* unspecified offset */ + offset = 0; + } + else + offset = 0; + + if ((src.year < EPOCH_YEAR) || + (src.year >= EPOCH_YEAR+MAX_YEAR_SECONDS)) + { + *dest = -1; + *dest_usec = -1; + return NULL; + } + *dest = year_seconds[src.year - EPOCH_YEAR]; + *dest -= offset * 60; + + yday = ((__mon_yday[__isleap (src.year)] + [src.month-1]) + (src.day-1)); + *dest += ( ( (yday* 24) + src.hour ) * 60 + src.minute ) * 60 + src.second; + *dest_usec = src.microseconds + + (src.centiseconds * 10000) + + (src.hundreds_of_microseconds * 100); + return dest; +} + + +udf_timestamp_t * +udf_time_to_stamp(udf_timestamp_t *dest, struct timespec ts) +{ + long int days, rem, y; + const unsigned short int *ip; + int16_t offset; + + offset = -timezone; + + if (!dest) + return NULL; + + dest->type_tz = 0x1000 | (offset & 0x0FFF); + + ts.tv_sec += offset * 60; + days = ts.tv_sec / SECS_PER_DAY; + rem = ts.tv_sec % SECS_PER_DAY; + dest->hour = rem / SECS_PER_HOUR; + rem %= SECS_PER_HOUR; + dest->minute = rem / 60; + dest->second = rem % 60; + y = EPOCH_YEAR; + +#define DIV(a,b) ((a) / (b) - ((a) % (b) < 0)) +#define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400)) + + while (days < 0 || days >= (__isleap(y) ? 366 : 365)) { + long int yg = y + days / 365 - (days % 365 < 0); + + /* Adjust DAYS and Y to match the guessed year. */ + days -= ((yg - y) * 365 + + LEAPS_THRU_END_OF (yg - 1) + - LEAPS_THRU_END_OF (y - 1)); + y = yg; + } + dest->year = y; + ip = __mon_yday[__isleap(y)]; + for (y = 11; days < (long int) ip[y]; --y) + continue; + days -= ip[y]; + dest->month = y + 1; + dest->day = days + 1; + + dest->centiseconds = ts.tv_nsec / 10000000; + dest->hundreds_of_microseconds = ( (ts.tv_nsec / 1000) + - (dest->centiseconds * 10000) ) / 100; + dest->microseconds = ( (ts.tv_nsec / 1000) + - (dest->centiseconds * 10000) + - (dest->hundreds_of_microseconds * 100) ); + return dest; +} + +time_t +udf_get_modification_time(const udf_file_t *p_udf_file) +{ + if (p_udf_file) { + time_t ret_time; + long int usec; + udf_stamp_to_time(&ret_time, &usec, p_udf_file->fe.modification_time); + return ret_time; + } + return 0; +} + +time_t +udf_get_access_time(const udf_file_t *p_udf_file) +{ + if (p_udf_file) { + time_t ret_time; + long int usec; + udf_stamp_to_time(&ret_time, &usec, p_udf_file->fe.access_time); + return ret_time; + } + return 0; +} +