Mercurial > octave
changeset 21935:2f33052c68ff
hide sys/stat.h header
* stat-wrappers.c, stat-wrappers.h (octave_stat_wrapper,
octave_lstat_wrapper, octave_fstat_wrapper, octave_is_blk_wrapper,
octave_is_chr_wrapper, octave_is_dir_wrapper, octave_is_fifo_wrapper,
octave_is_lnk_wrapper, octave_is_reg_wrapper, octave_is_sock_wrapper,
octave_have_struct_stat_st_rdev, octave_have_struct_stat_st_blksize,
octave_have_struct_stat_st_blocks): New functions.
(assign_stat_fields): New static function.
* statdefs.h: Delete
* liboctave/util/module.mk: Update.
* file-stat.cc, file-stat.h: Use new wrapper functions.
(have_struct_stat_st_rdev, have_struct_stat_st_blksize,
have_struct_stat_st_blocks): New functions.
* syscalls.cc: Use file_stat interface instead of config macros.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 16 Jun 2016 19:43:20 -0400 |
parents | b0e5173521b9 |
children | 45c5f4426289 |
files | libinterp/corefcn/syscalls.cc liboctave/system/file-stat.cc liboctave/system/file-stat.h liboctave/util/module.mk liboctave/util/statdefs.h liboctave/wrappers/stat-wrappers.c liboctave/wrappers/stat-wrappers.h |
diffstat | 7 files changed, 302 insertions(+), 168 deletions(-) [+] |
line wrap: on
line diff
--- a/libinterp/corefcn/syscalls.cc Thu Jun 16 17:17:42 2016 -0400 +++ b/libinterp/corefcn/syscalls.cc Thu Jun 16 19:43:20 2016 -0400 @@ -58,6 +58,12 @@ static octave_scalar_map mk_stat_map (const octave::sys::base_file_stat& fs) { + static bool have_rdev = file_stat::have_struct_stat_st_rdev (); + static bool have_blksize = file_stat::have_struct_stat_st_blksize (); + static bool have_blocks = file_stat::have_struct_stat_st_blocks (); + + static double nan = octave::numeric_limits<double>::NaN (); + octave_scalar_map m; m.assign ("dev", static_cast<double> (fs.dev ())); @@ -67,19 +73,21 @@ m.assign ("nlink", fs.nlink ()); m.assign ("uid", fs.uid ()); m.assign ("gid", fs.gid ()); -#if defined (HAVE_STRUCT_STAT_ST_RDEV) - m.assign ("rdev", static_cast<double> (fs.rdev ())); -#endif + m.assign ("rdev", have_rdev ? static_cast<double> (fs.rdev ()) : nan); m.assign ("size", fs.size ()); m.assign ("atime", fs.atime ()); m.assign ("mtime", fs.mtime ()); m.assign ("ctime", fs.ctime ()); -#if defined (HAVE_STRUCT_STAT_ST_BLKSIZE) - m.assign ("blksize", fs.blksize ()); -#endif -#if defined (HAVE_STRUCT_STAT_ST_BLOCKS) - m.assign ("blocks", fs.blocks ()); -#endif + + if (have_blksize) + m.assign ("blksize", fs.blksize ()); + else + m.assign ("blksize", nan); + + if (have_blocks) + m.assign ("blocks", fs.blocks ()); + else + m.assign ("blocks", nan); return m; }
--- a/liboctave/system/file-stat.cc Thu Jun 16 17:17:42 2016 -0400 +++ b/liboctave/system/file-stat.cc Thu Jun 16 19:43:20 2016 -0400 @@ -29,7 +29,7 @@ #include "file-ops.h" #include "file-stat.h" -#include "statdefs.h" +#include "stat-wrappers.h" #include "strmode-wrapper.h" namespace octave @@ -85,71 +85,61 @@ bool base_file_stat::is_blk (mode_t mode) { -#if defined (S_ISBLK) - return S_ISBLK (mode); -#else - return false; -#endif + return octave_is_blk_wrapper (mode); } bool base_file_stat::is_chr (mode_t mode) { -#if defined (S_ISCHR) - return S_ISCHR (mode); -#else - return false; -#endif + return octave_is_chr_wrapper (mode); } bool base_file_stat::is_dir (mode_t mode) { -#if defined (S_ISDIR) - return S_ISDIR (mode); -#else - return false; -#endif + return octave_is_dir_wrapper (mode); } bool base_file_stat::is_fifo (mode_t mode) { -#if defined (S_ISFIFO) - return S_ISFIFO (mode); -#else - return false; -#endif + return octave_is_fifo_wrapper (mode); } bool base_file_stat::is_lnk (mode_t mode) { -#if defined (S_ISLNK) - return S_ISLNK (mode); -#else - return false; -#endif + return octave_is_lnk_wrapper (mode); } bool base_file_stat::is_reg (mode_t mode) { -#if defined (S_ISREG) - return S_ISREG (mode); -#else - return false; -#endif + return octave_is_reg_wrapper (mode); } bool base_file_stat::is_sock (mode_t mode) { -#if defined (S_ISSOCK) - return S_ISSOCK (mode); -#else - return false; -#endif + return octave_is_sock_wrapper (mode); + } + + bool + base_file_stat::have_struct_stat_st_rdev (void) + { + return ::octave_have_struct_stat_st_rdev (); + } + + bool + base_file_stat::have_struct_stat_st_blksize (void) + { + return octave_have_struct_stat_st_blksize (); + } + + bool + base_file_stat::have_struct_stat_st_blocks (void) + { + return octave_have_struct_stat_st_blocks (); } std::string @@ -196,10 +186,18 @@ const char *cname = full_file_name.c_str (); - struct stat buf; + time_t sys_atime, sys_mtime, sys_ctime; - int status = follow_links - ? stat (cname, &buf) : gnulib::lstat (cname, &buf); + int status + = (follow_links + ? octave_stat_wrapper (cname, &m_mode, &m_ino, &m_dev, + &m_nlink, &m_uid, &m_gid, &m_size, + &sys_atime, &sys_mtime, &sys_ctime, + &m_rdev, &m_blksize, &m_blocks) + : octave_lstat_wrapper (cname, &m_mode, &m_ino, &m_dev, + &m_nlink, &m_uid, &m_gid, &m_size, + &sys_atime, &sys_mtime, &sys_ctime, + &m_rdev, &m_blksize, &m_blocks)); if (status < 0) { @@ -208,28 +206,9 @@ } else { - m_mode = buf.st_mode; - m_ino = buf.st_ino; - m_dev = buf.st_dev; - m_nlink = buf.st_nlink; - m_uid = buf.st_uid; - m_gid = buf.st_gid; - m_size = buf.st_size; - m_atime = buf.st_atime; - m_mtime = buf.st_mtime; - m_ctime = buf.st_ctime; - -#if defined (HAVE_STRUCT_STAT_ST_RDEV) - m_rdev = buf.st_rdev; -#endif - -#if defined (HAVE_STRUCT_STAT_ST_BLKSIZE) - m_blksize = buf.st_blksize; -#endif - -#if defined (HAVE_STRUCT_STAT_ST_BLOCKS) - m_blocks = buf.st_blocks; -#endif + m_atime = octave::sys::time (sys_atime); + m_mtime = octave::sys::time (sys_mtime); + m_ctime = octave::sys::time (sys_ctime); } initialized = true; @@ -244,9 +223,13 @@ initialized = false; fail = false; - struct stat buf; + time_t sys_atime, sys_mtime, sys_ctime; - int status = gnulib::fstat (fid, &buf); + int status + = octave_fstat_wrapper (fid, &m_mode, &m_ino, &m_dev, + &m_nlink, &m_uid, &m_gid, &m_size, + &sys_atime, &sys_mtime, &sys_ctime, + &m_rdev, &m_blksize, &m_blocks); if (status < 0) { @@ -255,28 +238,9 @@ } else { - m_mode = buf.st_mode; - m_ino = buf.st_ino; - m_dev = buf.st_dev; - m_nlink = buf.st_nlink; - m_uid = buf.st_uid; - m_gid = buf.st_gid; - m_size = buf.st_size; - m_atime = buf.st_atime; - m_mtime = buf.st_mtime; - m_ctime = buf.st_ctime; - -#if defined (HAVE_STRUCT_STAT_ST_RDEV) - m_rdev = buf.st_rdev; -#endif - -#if defined (HAVE_STRUCT_STAT_ST_BLKSIZE) - m_blksize = buf.st_blksize; -#endif - -#if defined (HAVE_STRUCT_STAT_ST_BLOCKS) - m_blocks = buf.st_blocks; -#endif + m_atime = octave::sys::time (sys_atime); + m_mtime = octave::sys::time (sys_mtime); + m_ctime = octave::sys::time (sys_ctime); } initialized = true;
--- a/liboctave/system/file-stat.h Thu Jun 16 17:17:42 2016 -0400 +++ b/liboctave/system/file-stat.h Thu Jun 16 19:43:20 2016 -0400 @@ -110,6 +110,10 @@ static bool is_reg (mode_t mode); static bool is_sock (mode_t mode); + static bool have_struct_stat_st_rdev (void); + static bool have_struct_stat_st_blksize (void); + static bool have_struct_stat_st_blocks (void); + ino_t ino (void) const { return m_ino; } dev_t dev (void) const { return m_dev; }
--- a/liboctave/util/module.mk Thu Jun 16 17:17:42 2016 -0400 +++ b/liboctave/util/module.mk Thu Jun 16 19:43:20 2016 -0400 @@ -43,8 +43,7 @@ NOINSTALL_UTIL_INC = \ liboctave/util/kpse.h \ - liboctave/util/oct-sparse.h \ - liboctave/util/statdefs.h + liboctave/util/oct-sparse.h UTIL_C_SRC = \ liboctave/util/f2c-main.c \
--- a/liboctave/util/statdefs.h Thu Jun 16 17:17:42 2016 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,72 +0,0 @@ -/* - -Copyright (C) 1993-2015 John W. Eaton - -This file is part of Octave. - -Octave 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 3 of the License, or (at your -option) any later version. - -Octave 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 Octave; see the file COPYING. If not, see -<http://www.gnu.org/licenses/>. - -*/ - -#if ! defined (octave_statdefs_h) -#define octave_statdefs_h 1 - -#include "octave-config.h" - -#include <sys/types.h> - -#if defined (HAVE_SYS_STAT_H) -# include <sys/stat.h> -#endif - -#if ! defined (S_ISREG) -# if ! defined (mode_t) -# define mode_t unsigned short -# endif -#endif -#if ! defined (S_ISBLK) && defined (S_IFBLK) -# define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK) -#endif -#if ! defined (S_ISCHR) && defined (S_IFCHR) -# define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR) -#endif -#if ! defined (S_ISDIR) && defined (S_IFDIR) -# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) -#endif -#if ! defined (S_ISREG) && defined (S_IFREG) -# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) -#endif -#if ! defined (S_ISFIFO) && defined (S_IFIFO) -# define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO) -#endif -#if ! defined (S_ISLNK) && defined (S_IFLNK) -# define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK) -#endif -#if ! defined (S_ISSOCK) && defined (S_IFSOCK) -# define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK) -#endif -#if ! defined (S_ISMPB) && defined (S_IFMPB) -# define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB) -# define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC) -#endif -#if ! defined (S_ISNWK) && defined (S_IFNWK) -# define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK) -#endif - -#if ! defined (S_ISLNK) -# undef HAVE_LSTAT -#endif - -#endif
--- a/liboctave/wrappers/stat-wrappers.c Thu Jun 16 17:17:42 2016 -0400 +++ b/liboctave/wrappers/stat-wrappers.c Thu Jun 16 19:43:20 2016 -0400 @@ -29,6 +29,9 @@ # include "config.h" #endif +#include <time.h> + +#include <sys/types.h> #include <sys/stat.h> #include "stat-wrappers.h" @@ -50,3 +53,189 @@ { return umask (mode); } + +static inline void +assign_stat_fields (struct stat *buf, mode_t *mode, ino_t *ino, + dev_t *dev, nlink_t *nlink, uid_t *uid, + gid_t *gid, off_t *size, time_t *atime, + time_t *mtime, time_t *ctime, dev_t *rdev, + long *blksize, long *blocks) +{ + *mode = buf->st_mode; + *ino = buf->st_ino; + *dev = buf->st_dev; + *nlink = buf->st_nlink; + *uid = buf->st_uid; + *gid = buf->st_gid; + *size = buf->st_size; + *atime = buf->st_atime; + *mtime = buf->st_mtime; + *ctime = buf->st_ctime; + +#if defined (HAVE_STRUCT_STAT_ST_RDEV) + *rdev = buf->st_rdev; +#else + *rdev = 0; +#endif + +#if defined (HAVE_STRUCT_STAT_ST_BLKSIZE) + *blksize = buf->st_blksize; + *blksize = 0; +#endif + +#if defined (HAVE_STRUCT_STAT_ST_BLOCKS) + *blocks = buf->st_blocks; + *blksize = 0; +#endif +} + +int +octave_stat_wrapper (const char *fname, mode_t *mode, ino_t *ino, + dev_t *dev, nlink_t *nlink, uid_t *uid, + gid_t *gid, off_t *size, time_t *atime, + time_t *mtime, time_t *ctime, dev_t *rdev, + long *blksize, long *blocks) +{ + struct stat buf; + + int status = stat (fname, &buf); + + assign_stat_fields (&buf, mode, ino, dev, nlink, uid, gid, size, + atime, mtime, ctime, rdev, blksize, blocks); + + return status; +} + +int +octave_lstat_wrapper (const char *lname, mode_t *mode, ino_t *ino, + dev_t *dev, nlink_t *nlink, uid_t *uid, + gid_t *gid, off_t *size, time_t *atime, + time_t *mtime, time_t *ctime, dev_t *rdev, + long *blksize, long *blocks) +{ + struct stat buf; + + int status = lstat (lname, &buf); + + assign_stat_fields (&buf, mode, ino, dev, nlink, uid, gid, size, + atime, mtime, ctime, rdev, blksize, blocks); + + return status; +} + +int +octave_fstat_wrapper (int fid, mode_t *mode, ino_t *ino, + dev_t *dev, nlink_t *nlink, uid_t *uid, + gid_t *gid, off_t *size, time_t *atime, + time_t *mtime, time_t *ctime, dev_t *rdev, + long *blksize, long *blocks) +{ + struct stat buf; + + int status = fstat (fid, &buf); + + assign_stat_fields (&buf, mode, ino, dev, nlink, uid, gid, size, + atime, mtime, ctime, rdev, blksize, blocks); + + return status; +} + +bool +octave_is_blk_wrapper (mode_t mode) +{ +#if defined (S_ISBLK) + return S_ISBLK (mode); +#else + return false; +#endif +} + +bool +octave_is_chr_wrapper (mode_t mode) +{ +#if defined (S_ISCHR) + return S_ISCHR (mode); +#else + return false; +#endif +} + +bool +octave_is_dir_wrapper (mode_t mode) +{ +#if defined (S_ISDIR) + return S_ISDIR (mode); +#else + return false; +#endif +} + +bool +octave_is_fifo_wrapper (mode_t mode) +{ +#if defined (S_ISFIFO) + return S_ISFIFO (mode); +#else + return false; +#endif +} + +bool +octave_is_lnk_wrapper (mode_t mode) +{ +#if defined (S_ISLNK) + return S_ISLNK (mode); +#else + return false; +#endif +} + +bool +octave_is_reg_wrapper (mode_t mode) +{ +#if defined (S_ISREG) + return S_ISREG (mode); +#else + return false; +#endif +} + +bool +octave_is_sock_wrapper (mode_t mode) +{ +#if defined (S_ISSOCK) + return S_ISSOCK (mode); +#else + return false; +#endif +} + +bool +octave_have_struct_stat_st_rdev (void) +{ +#if defined (HAVE_STRUCT_STAT_ST_RDEV) + return true; +#else + return false; +#endif +} + +bool +octave_have_struct_stat_st_blksize (void) +{ +#if defined (HAVE_STRUCT_STAT_ST_BLKSIZE) + return true; +#else + return false; +#endif +} + +bool +octave_have_struct_stat_st_blocks (void) +{ +#if defined (HAVE_STRUCT_STAT_ST_BLOCKS) + return true; +#else + return false; +#endif +}
--- a/liboctave/wrappers/stat-wrappers.h Thu Jun 16 17:17:42 2016 -0400 +++ b/liboctave/wrappers/stat-wrappers.h Thu Jun 16 19:43:20 2016 -0400 @@ -24,6 +24,15 @@ #define octave_stat_wrappers_h 1 #if defined __cplusplus +# include <ctime> +#else +# include <stdbool.h> +# include <time.h> +#endif + +#include <sys/types.h> + +#if defined __cplusplus extern "C" { #endif @@ -33,6 +42,39 @@ extern int octave_umask_wrapper (mode_t mode); +extern int +octave_stat_wrapper (const char *fname, mode_t *mode, ino_t *ino, + dev_t *dev, nlink_t *nlink, uid_t *uid, + gid_t *gid, off_t *size, time_t *atime, + time_t *mtime, time_t *ctime, dev_t *rdev, + long *blksize, long *blocks); + +extern int +octave_lstat_wrapper (const char *lname, mode_t *mode, ino_t *ino, + dev_t *dev, nlink_t *nlink, uid_t *uid, + gid_t *gid, off_t *size, time_t *atime, + time_t *mtime, time_t *ctime, dev_t *rdev, + long *blksize, long *blocks); + +extern int +octave_fstat_wrapper (int fid, mode_t *mode, ino_t *ino, + dev_t *dev, nlink_t *nlink, uid_t *uid, + gid_t *gid, off_t *size, time_t *atime, + time_t *mtime, time_t *ctime, dev_t *rdev, + long *blksize, long *blocks); + +extern bool octave_is_blk_wrapper (mode_t mode); +extern bool octave_is_chr_wrapper (mode_t mode); +extern bool octave_is_dir_wrapper (mode_t mode); +extern bool octave_is_fifo_wrapper (mode_t mode); +extern bool octave_is_lnk_wrapper (mode_t mode); +extern bool octave_is_reg_wrapper (mode_t mode); +extern bool octave_is_sock_wrapper (mode_t mode); + +extern bool octave_have_struct_stat_st_rdev (void); +extern bool octave_have_struct_stat_st_blksize (void); +extern bool octave_have_struct_stat_st_blocks (void); + #if defined __cplusplus } #endif