# HG changeset patch # User file-io.cc (Ffstat): new function # Date 1232499763 18000 # Node ID 424ba638d8f1e5bdd45162af002689eaba2f3343 # Parent 193bf0e6c5671d054e88818f6729feca6053fe50 [mq]: fstat diff -r 193bf0e6c567 -r 424ba638d8f1 ChangeLog --- a/ChangeLog Tue Jan 20 22:39:29 2009 +0100 +++ b/ChangeLog Tue Jan 20 20:02:43 2009 -0500 @@ -1,3 +1,7 @@ +2009-01-20 John W. Eaton + + * configure.in: Check for fstat. + 2008-12-23 David Bateman * configure.in: Add configuration test for ARPACK. diff -r 193bf0e6c567 -r 424ba638d8f1 configure.in --- a/configure.in Tue Jan 20 22:39:29 2009 +0100 +++ b/configure.in Tue Jan 20 20:02:43 2009 -0500 @@ -1505,7 +1505,7 @@ ### Checks for functions and variables. AC_CHECK_FUNCS(atexit basename bcopy bzero canonicalize_file_name \ - chmod dup2 endgrent endpwent execvp expm1 expm1f fcntl fork getcwd \ + chmod dup2 endgrent endpwent execvp expm1 expm1f fcntl fork fstat getcwd \ getegid geteuid getgid getgrent getgrgid getgrnam getpgrp getpid \ getppid getpwent getpwuid gettimeofday getuid getwd _kbhit kill \ lgamma lgammaf lgamma_r lgammaf_r link localtime_r log1p log1pf lstat \ diff -r 193bf0e6c567 -r 424ba638d8f1 doc/ChangeLog --- a/doc/ChangeLog Tue Jan 20 22:39:29 2009 +0100 +++ b/doc/ChangeLog Tue Jan 20 20:02:43 2009 -0500 @@ -1,3 +1,8 @@ +2009-01-20 John W. Eaton + + * interpreter/system.txi (Filesystem Utilities): @DOCSTRING for + fstat. Delete @DOCSTRING for lstat. Add anchor for doc-lstat. + 2009-01-18 Thorsten Meyer * interpreter/numbers.txi (Numeric Data Types): Trivial fixes. diff -r 193bf0e6c567 -r 424ba638d8f1 doc/interpreter/system.txi --- a/doc/interpreter/system.txi Tue Jan 20 22:39:29 2009 +0100 +++ b/doc/interpreter/system.txi Tue Jan 20 20:02:43 2009 -0500 @@ -177,9 +177,10 @@ @DOCSTRING(umask) +@anchor{doc-lstat} @DOCSTRING(stat) -@DOCSTRING(lstat) +@DOCSTRING(fstat) @DOCSTRING(fileattrib) diff -r 193bf0e6c567 -r 424ba638d8f1 liboctave/ChangeLog --- a/liboctave/ChangeLog Tue Jan 20 22:39:29 2009 +0100 +++ b/liboctave/ChangeLog Tue Jan 20 20:02:43 2009 -0500 @@ -1,3 +1,9 @@ +2009-01-20 John W. Eaton + + * file-stat.h, file-stat.cc (class base_file_stat): New base class. + (class file_stat): Derive from base_file_stat. + (class file_fstat): New class, derived from base_file_stat. + 2009-01-17 Jaroslav Hajek * floatQR.h (FloatQR::update, FloatQR::insert_col, diff -r 193bf0e6c567 -r 424ba638d8f1 liboctave/file-stat.cc --- a/liboctave/file-stat.cc Tue Jan 20 22:39:29 2009 +0100 +++ b/liboctave/file-stat.cc Tue Jan 20 20:02:43 2009 -0500 @@ -52,49 +52,49 @@ // initialized, they should throw an exception. bool -file_stat::is_blk (void) const +base_file_stat::is_blk (void) const { return is_blk (fs_mode); } bool -file_stat::is_chr (void) const +base_file_stat::is_chr (void) const { return is_chr (fs_mode); } bool -file_stat::is_dir (void) const +base_file_stat::is_dir (void) const { return is_dir (fs_mode); } bool -file_stat::is_fifo (void) const +base_file_stat::is_fifo (void) const { return is_fifo (fs_mode); } bool -file_stat::is_lnk (void) const +base_file_stat::is_lnk (void) const { return is_lnk (fs_mode); } bool -file_stat::is_reg (void) const +base_file_stat::is_reg (void) const { return is_reg (fs_mode); } bool -file_stat::is_sock (void) const +base_file_stat::is_sock (void) const { return is_sock (fs_mode); } bool -file_stat::is_blk (mode_t mode) +base_file_stat::is_blk (mode_t mode) { #ifdef S_ISBLK return S_ISBLK (mode); @@ -104,7 +104,7 @@ } bool -file_stat::is_chr (mode_t mode) +base_file_stat::is_chr (mode_t mode) { #ifdef S_ISCHR return S_ISCHR (mode); @@ -114,7 +114,7 @@ } bool -file_stat::is_dir (mode_t mode) +base_file_stat::is_dir (mode_t mode) { #ifdef S_ISDIR return S_ISDIR (mode); @@ -124,7 +124,7 @@ } bool -file_stat::is_fifo (mode_t mode) +base_file_stat::is_fifo (mode_t mode) { #ifdef S_ISFIFO return S_ISFIFO (mode); @@ -134,7 +134,7 @@ } bool -file_stat::is_lnk (mode_t mode) +base_file_stat::is_lnk (mode_t mode) { #ifdef S_ISLNK return S_ISLNK (mode); @@ -144,7 +144,7 @@ } bool -file_stat::is_reg (mode_t mode) +base_file_stat::is_reg (mode_t mode) { #ifdef S_ISREG return S_ISREG (mode); @@ -154,7 +154,7 @@ } bool -file_stat::is_sock (mode_t mode) +base_file_stat::is_sock (mode_t mode) { #ifdef S_ISSOCK return S_ISSOCK (mode); @@ -166,7 +166,7 @@ extern "C" void mode_string (unsigned short, char *); std::string -file_stat::mode_as_string (void) const +base_file_stat::mode_as_string (void) const { char buf[11]; @@ -181,7 +181,7 @@ // and -1 for any error. int -file_stat::is_newer (const std::string& file, const octave_time& time) +base_file_stat::is_newer (const std::string& file, const octave_time& time) { file_stat fs (file); @@ -252,35 +252,61 @@ } void -file_stat::copy (const file_stat& fs) +file_fstat::update_internal (bool force) { - file_name = fs.file_name; - follow_links = fs.follow_links; - initialized = fs.initialized; - fail = fs.fail; - errmsg = fs.errmsg; - fs_mode = fs.fs_mode; - fs_ino = fs.fs_ino; - fs_dev = fs.fs_dev; - fs_nlink = fs.fs_nlink; - fs_uid = fs.fs_uid; - fs_gid = fs.fs_gid; - fs_size = fs.fs_size; - fs_atime = fs.fs_atime; - fs_mtime = fs.fs_mtime; - fs_ctime = fs.fs_ctime; + if (! initialized || force) + { + initialized = false; + fail = false; + +#if defined (HAVE_FSTAT) + + struct stat buf; + + int status = fstat (fid, &buf); + + if (status < 0) + { + using namespace std; + + fail = true; + errmsg = strerror (errno); + } + else + { + fs_mode = buf.st_mode; + fs_ino = buf.st_ino; + fs_dev = buf.st_dev; + fs_nlink = buf.st_nlink; + fs_uid = buf.st_uid; + fs_gid = buf.st_gid; + fs_size = buf.st_size; + fs_atime = buf.st_atime; + fs_mtime = buf.st_mtime; + fs_ctime = buf.st_ctime; #if defined (HAVE_STRUCT_STAT_ST_RDEV) - fs_rdev = fs.fs_rdev; + fs_rdev = buf.st_rdev; #endif #if defined (HAVE_STRUCT_STAT_ST_BLKSIZE) - fs_blksize = fs.fs_blksize; + fs_blksize = buf.st_blksize; #endif #if defined (HAVE_STRUCT_STAT_ST_BLOCKS) - fs_blocks = fs.fs_blocks; + fs_blocks = buf.st_blocks; #endif + } + +#else + + fail = true; + errmsg = "fstat not available on this system"; + +#endif + + initialized = true; + } } /* diff -r 193bf0e6c567 -r 424ba638d8f1 liboctave/file-stat.h --- a/liboctave/file-stat.h Tue Jan 20 22:39:29 2009 +0100 +++ b/liboctave/file-stat.h Tue Jan 20 20:02:43 2009 -0500 @@ -34,46 +34,50 @@ class OCTAVE_API -file_stat +base_file_stat { public: - file_stat (const std::string& n = std::string (), bool fl = true) - : file_name (n), follow_links (fl), initialized (false) + base_file_stat (void) + : initialized (false), fail (false), errmsg (), fs_mode (), + fs_ino (), fs_dev (), fs_nlink (), fs_uid (), fs_gid (), + fs_size (), fs_atime (), fs_mtime (), fs_ctime (), fs_rdev (), + fs_blksize (), fs_blocks () { } + + base_file_stat (const base_file_stat& fs) + : initialized (fs.initialized), fail (fs.fail), errmsg (fs.errmsg), + fs_mode (fs.fs_mode), fs_ino (fs.fs_ino), fs_dev (fs.fs_dev), + fs_nlink (fs.fs_nlink), fs_uid (fs.fs_uid), fs_gid (fs.fs_gid), + fs_size (fs.fs_size), fs_atime (fs.fs_atime), fs_mtime (fs.fs_mtime), + fs_ctime (fs.fs_ctime), fs_rdev (fs.fs_rdev), + fs_blksize (fs.fs_blksize), fs_blocks (fs.fs_blocks) { } + + base_file_stat& operator = (const base_file_stat& fs) + { + if (this != &fs) { - if (! file_name.empty ()) - update_internal (); + initialized = fs.initialized; + fail = fs.fail; + errmsg = fs.errmsg; + fs_mode = fs.fs_mode; + fs_ino = fs.fs_ino; + fs_dev = fs.fs_dev; + fs_nlink = fs.fs_nlink; + fs_uid = fs.fs_uid; + fs_gid = fs.fs_gid; + fs_size = fs.fs_size; + fs_atime = fs.fs_atime; + fs_mtime = fs.fs_mtime; + fs_ctime = fs.fs_ctime; + fs_rdev = fs.fs_rdev; + fs_blksize = fs.fs_blksize; + fs_blocks = fs.fs_blocks; } - file_stat (const file_stat& f) { copy (f); } - - file_stat& operator = (const file_stat& f) - { - if (this != &f) - copy (f); - - return *this; - } - - ~file_stat (void) { } + return *this; + } - void get_stats (bool force = false) - { - if (! initialized || force) - update_internal (force); - } - - void get_stats (const std::string& n, bool force = false) - { - if (n != file_name || ! initialized || force) - { - initialized = false; - - file_name = n; - - update_internal (force); - } - } + ~base_file_stat (void) { } // File status and info. These should only be called for objects // that are already properly initialized. @@ -108,17 +112,10 @@ octave_time mtime (void) const { return fs_mtime; } octave_time ctime (void) const { return fs_ctime; } -#if defined (HAVE_STRUCT_STAT_ST_RDEV) dev_t rdev (void) const { return fs_rdev; } -#endif -#if defined (HAVE_STRUCT_STAT_ST_BLKSIZE) long blksize (void) const { return fs_blksize; } -#endif - -#if defined (HAVE_STRUCT_STAT_ST_BLOCKS) long blocks (void) const { return fs_blocks; } -#endif mode_t mode (void) const { return fs_mode; } @@ -139,14 +136,7 @@ // really care about it. static int is_newer (const std::string&, const octave_time&); -private: - - // Name of the file. - std::string file_name; - - // TRUE means follow symbolic links to the ultimate file (stat). - // FALSE means get information about the link itself (lstat). - bool follow_links; +protected: // TRUE means we have already called stat. bool initialized; @@ -187,24 +177,130 @@ // time of last file status change octave_time fs_ctime; -#if defined (HAVE_STRUCT_STAT_ST_RDEV) // device number for special files dev_t fs_rdev; -#endif -#if defined (HAVE_STRUCT_STAT_ST_BLKSIZE) // best I/O block size long fs_blksize; -#endif -#if defined (HAVE_STRUCT_STAT_ST_BLOCKS) // number of 512-byte blocks allocated long fs_blocks; -#endif +}; + +class +OCTAVE_API +file_stat : public base_file_stat +{ +public: + + file_stat (const std::string& n = std::string (), bool fl = true) + : base_file_stat (), file_name (n), follow_links (fl) + { + if (! file_name.empty ()) + update_internal (); + } + + file_stat (const file_stat& fs) + : base_file_stat (fs), file_name (fs.file_name), + follow_links (fs.follow_links) { } + + file_stat& operator = (const file_stat& fs) + { + if (this != &fs) + { + base_file_stat::operator = (fs); + + file_name = fs.file_name; + follow_links = fs.follow_links; + } + + return *this; + } + + ~file_stat (void) { } + + void get_stats (bool force = false) + { + if (! initialized || force) + update_internal (force); + } + + void get_stats (const std::string& n, bool force = false) + { + if (n != file_name || ! initialized || force) + { + initialized = false; + + file_name = n; + + update_internal (force); + } + } + +private: + + // Name of the file. + std::string file_name; + + // TRUE means follow symbolic links to the ultimate file (stat). + // FALSE means get information about the link itself (lstat). + bool follow_links; void update_internal (bool force = false); +}; - void copy (const file_stat&); +class +OCTAVE_API +file_fstat : public base_file_stat +{ +public: + + file_fstat (int n) : base_file_stat (), fid (n) + { + update_internal (); + } + + file_fstat (const file_fstat& fs) + : base_file_stat (fs), fid (fs.fid) { } + + file_fstat& operator = (const file_fstat& fs) + { + if (this != &fs) + { + base_file_stat::operator = (fs); + + fid = fs.fid; + } + + return *this; + } + + ~file_fstat (void) { } + + void get_stats (bool force = false) + { + if (! initialized || force) + update_internal (force); + } + + void get_stats (int n, bool force = false) + { + if (n != fid || ! initialized || force) + { + initialized = false; + + fid = n; + + update_internal (force); + } + } + +private: + + // Open file descriptor. + int fid; + + void update_internal (bool force = false); }; #endif diff -r 193bf0e6c567 -r 424ba638d8f1 src/ChangeLog --- a/src/ChangeLog Tue Jan 20 22:39:29 2009 +0100 +++ b/src/ChangeLog Tue Jan 20 20:02:43 2009 -0500 @@ -1,3 +1,7 @@ +2009-01-20 John W. Eaton + + * file-io.cc (Ffstat): New function. + 2009-01-17 Jaroslav Hajek * DLD-FUNCTIONS/qr.cc (Fqrupdate, Fqrinsert, Fqrdelete, Fqrshift): diff -r 193bf0e6c567 -r 424ba638d8f1 src/syscalls.cc --- a/src/syscalls.cc Tue Jan 20 22:39:29 2009 +0100 +++ b/src/syscalls.cc Tue Jan 20 20:02:43 2009 -0500 @@ -63,7 +63,7 @@ #include "input.h" static Octave_map -mk_stat_map (const file_stat& fs) +mk_stat_map (const base_file_stat& fs) { Octave_map m; @@ -722,6 +722,43 @@ return retval; } +DEFUN (fstat, args, , + "-*- texinfo -*-\n\ +@deftypefn {Built-in Function} {[@var{info}, @var{err}, @var{msg}] =} fstat (@var{fid})\n\ +Return information about about the open file @var{fid}. See @code{stat}\n\ +for a description of the contents of @var{info}.\n\ +@end deftypefn") +{ + octave_value_list retval; + + if (args.length () == 1) + { + int fid = octave_stream_list::get_file_number (args(0)); + + if (! error_state) + { + file_fstat fs (fid); + + if (fs) + { + retval(2) = std::string (); + retval(1) = 0; + retval(0) = octave_value (mk_stat_map (fs)); + } + else + { + retval(2) = fs.error (); + retval(1) = -1; + retval(0) = Matrix (); + } + } + } + else + print_usage (); + + return retval; +} + DEFUN (lstat, args, , "-*- texinfo -*-\n\ @deftypefn {Built-in Function} {[@var{info}, @var{err}, @var{msg}] =} lstat (@var{file})\n\