00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifdef HAVE_CONFIG_H
00024 #include <config.h>
00025 #endif
00026
00027 #include <cerrno>
00028 #include <cstring>
00029
00030 #ifdef HAVE_UNISTD_H
00031 #ifdef HAVE_SYS_TYPES_H
00032 #include <sys/types.h>
00033 #endif
00034 #include <unistd.h>
00035 #endif
00036
00037 #include "file-stat.h"
00038 #include "statdefs.h"
00039
00040 #if !defined (HAVE_LSTAT)
00041 static inline int
00042 lstat (const char *name, struct stat *buf)
00043 {
00044 return stat (name, buf);
00045 }
00046 #endif
00047
00048
00049
00050
00051
00052 bool
00053 file_stat::is_blk (void) const
00054 {
00055 #ifdef S_ISBLK
00056 return S_ISBLK (fs_mode);
00057 #else
00058 return false;
00059 #endif
00060 }
00061
00062 bool
00063 file_stat::is_chr (void) const
00064 {
00065 #ifdef S_ISCHR
00066 return S_ISCHR (fs_mode);
00067 #else
00068 return false;
00069 #endif
00070 }
00071
00072 bool
00073 file_stat::is_dir (void) const
00074 {
00075 #ifdef S_ISDIR
00076 return S_ISDIR (fs_mode);
00077 #else
00078 return false;
00079 #endif
00080 }
00081
00082 bool
00083 file_stat::is_fifo (void) const
00084 {
00085 #ifdef S_ISFIFO
00086 return S_ISFIFO (fs_mode);
00087 #else
00088 return false;
00089 #endif
00090 }
00091
00092 bool
00093 file_stat::is_lnk (void) const
00094 {
00095 #ifdef S_ISLNK
00096 return S_ISLNK (fs_mode);
00097 #else
00098 return false;
00099 #endif
00100 }
00101
00102 bool
00103 file_stat::is_reg (void) const
00104 {
00105 #ifdef S_ISREG
00106 return S_ISREG (fs_mode);
00107 #else
00108 return false;
00109 #endif
00110 }
00111
00112 bool
00113 file_stat::is_sock (void) const
00114 {
00115 #ifdef S_ISSOCK
00116 return S_ISSOCK (fs_mode);
00117 #else
00118 return false;
00119 #endif
00120 }
00121
00122 extern "C" void mode_string (unsigned short, char *);
00123
00124 std::string
00125 file_stat::mode_as_string (void) const
00126 {
00127 char buf[11];
00128
00129 mode_string (fs_mode, buf);
00130
00131 buf[10] = '\0';
00132
00133 return std::string (buf);
00134 }
00135
00136
00137
00138
00139 int
00140 file_stat::is_newer (const std::string& file, const octave_time& time)
00141 {
00142 file_stat fs (file);
00143
00144 return fs ? fs.is_newer (time) : -1;
00145 }
00146
00147
00148
00149 void
00150 file_stat::update_internal (bool force)
00151 {
00152 if (! initialized || force)
00153 {
00154 initialized = false;
00155 fail = false;
00156
00157 const char *cname = file_name.c_str ();
00158
00159 struct stat buf;
00160
00161 int status = follow_links ? stat (cname, &buf) : lstat (cname, &buf);
00162
00163 if (status < 0)
00164 {
00165 using namespace std;
00166
00167 fail = true;
00168 errmsg = strerror (errno);
00169 }
00170 else
00171 {
00172 fs_mode = buf.st_mode;
00173 fs_ino = buf.st_ino;
00174 fs_dev = buf.st_dev;
00175 fs_nlink = buf.st_nlink;
00176 fs_uid = buf.st_uid;
00177 fs_gid = buf.st_gid;
00178 fs_size = buf.st_size;
00179 fs_atime = buf.st_atime;
00180 fs_mtime = buf.st_mtime;
00181 fs_ctime = buf.st_ctime;
00182
00183 #if defined (HAVE_STRUCT_STAT_ST_RDEV)
00184 fs_rdev = buf.st_rdev;
00185 #endif
00186
00187 #if defined (HAVE_STRUCT_STAT_ST_BLKSIZE)
00188 fs_blksize = buf.st_blksize;
00189 #endif
00190
00191 #if defined (HAVE_STRUCT_STAT_ST_BLOCKS)
00192 fs_blocks = buf.st_blocks;
00193 #endif
00194 }
00195
00196 initialized = true;
00197 }
00198 }
00199
00200 void
00201 file_stat::copy (const file_stat& fs)
00202 {
00203 file_name = fs.file_name;
00204 follow_links = fs.follow_links;
00205 initialized = fs.initialized;
00206 fail = fs.fail;
00207 errmsg = fs.errmsg;
00208 fs_mode = fs.fs_mode;
00209 fs_ino = fs.fs_ino;
00210 fs_dev = fs.fs_dev;
00211 fs_nlink = fs.fs_nlink;
00212 fs_uid = fs.fs_uid;
00213 fs_gid = fs.fs_gid;
00214 fs_size = fs.fs_size;
00215 fs_atime = fs.fs_atime;
00216 fs_mtime = fs.fs_mtime;
00217 fs_ctime = fs.fs_ctime;
00218
00219 #if defined (HAVE_STRUCT_STAT_ST_RDEV)
00220 fs_rdev = fs.fs_rdev;
00221 #endif
00222
00223 #if defined (HAVE_STRUCT_STAT_ST_BLKSIZE)
00224 fs_blksize = fs.fs_blksize;
00225 #endif
00226
00227 #if defined (HAVE_STRUCT_STAT_ST_BLOCKS)
00228 fs_blocks = fs.fs_blocks;
00229 #endif
00230 }
00231
00232
00233
00234
00235
00236