00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #if !defined (octave_file_stat_h)
00025 #define octave_file_stat_h 1
00026
00027 #include <string>
00028
00029 #include "oct-time.h"
00030
00031 #ifdef HAVE_SYS_TYPES_H
00032 #include <sys/types.h>
00033 #endif
00034
00035 class
00036 OCTAVE_API
00037 base_file_stat
00038 {
00039 public:
00040
00041 base_file_stat (void)
00042 : initialized (false), fail (false), errmsg (), fs_mode (),
00043 fs_ino (), fs_dev (), fs_nlink (), fs_uid (), fs_gid (),
00044 fs_size (), fs_atime (), fs_mtime (), fs_ctime (), fs_rdev (),
00045 fs_blksize (), fs_blocks () { }
00046
00047 base_file_stat (const base_file_stat& fs)
00048 : initialized (fs.initialized), fail (fs.fail), errmsg (fs.errmsg),
00049 fs_mode (fs.fs_mode), fs_ino (fs.fs_ino), fs_dev (fs.fs_dev),
00050 fs_nlink (fs.fs_nlink), fs_uid (fs.fs_uid), fs_gid (fs.fs_gid),
00051 fs_size (fs.fs_size), fs_atime (fs.fs_atime), fs_mtime (fs.fs_mtime),
00052 fs_ctime (fs.fs_ctime), fs_rdev (fs.fs_rdev),
00053 fs_blksize (fs.fs_blksize), fs_blocks (fs.fs_blocks) { }
00054
00055 base_file_stat& operator = (const base_file_stat& fs)
00056 {
00057 if (this != &fs)
00058 {
00059 initialized = fs.initialized;
00060 fail = fs.fail;
00061 errmsg = fs.errmsg;
00062 fs_mode = fs.fs_mode;
00063 fs_ino = fs.fs_ino;
00064 fs_dev = fs.fs_dev;
00065 fs_nlink = fs.fs_nlink;
00066 fs_uid = fs.fs_uid;
00067 fs_gid = fs.fs_gid;
00068 fs_size = fs.fs_size;
00069 fs_atime = fs.fs_atime;
00070 fs_mtime = fs.fs_mtime;
00071 fs_ctime = fs.fs_ctime;
00072 fs_rdev = fs.fs_rdev;
00073 fs_blksize = fs.fs_blksize;
00074 fs_blocks = fs.fs_blocks;
00075 }
00076
00077 return *this;
00078 }
00079
00080 ~base_file_stat (void) { }
00081
00082
00083
00084
00085
00086
00087 bool is_blk (void) const;
00088 bool is_chr (void) const;
00089 bool is_dir (void) const;
00090 bool is_fifo (void) const;
00091 bool is_lnk (void) const;
00092 bool is_reg (void) const;
00093 bool is_sock (void) const;
00094
00095 static bool is_blk (mode_t mode);
00096 static bool is_chr (mode_t mode);
00097 static bool is_dir (mode_t mode);
00098 static bool is_fifo (mode_t mode);
00099 static bool is_lnk (mode_t mode);
00100 static bool is_reg (mode_t mode);
00101 static bool is_sock (mode_t mode);
00102
00103 ino_t ino (void) const { return fs_ino; }
00104 dev_t dev (void) const { return fs_dev; }
00105
00106 nlink_t nlink (void) const { return fs_nlink; }
00107
00108 uid_t uid (void) const { return fs_uid; }
00109 gid_t gid (void) const { return fs_gid; }
00110
00111 off_t size (void) const { return fs_size; }
00112
00113 octave_time atime (void) const { return fs_atime; }
00114 octave_time mtime (void) const { return fs_mtime; }
00115 octave_time ctime (void) const { return fs_ctime; }
00116
00117 dev_t rdev (void) const { return fs_rdev; }
00118
00119 long blksize (void) const { return fs_blksize; }
00120 long blocks (void) const { return fs_blocks; }
00121
00122 mode_t mode (void) const { return fs_mode; }
00123
00124 std::string mode_as_string (void) const;
00125
00126 bool ok (void) const { return initialized && ! fail; }
00127
00128 operator bool () const { return ok (); }
00129
00130 bool exists (void) const { return ok (); }
00131
00132 std::string error (void) const { return ok () ? std::string () : errmsg; }
00133
00134
00135 bool is_newer (const octave_time& time) const { return fs_mtime > time; }
00136
00137
00138
00139 static int is_newer (const std::string&, const octave_time&);
00140
00141 protected:
00142
00143
00144 bool initialized;
00145
00146
00147 bool fail;
00148
00149
00150 std::string errmsg;
00151
00152
00153 mode_t fs_mode;
00154
00155
00156 ino_t fs_ino;
00157
00158
00159 dev_t fs_dev;
00160
00161
00162 nlink_t fs_nlink;
00163
00164
00165 uid_t fs_uid;
00166
00167
00168 gid_t fs_gid;
00169
00170
00171 off_t fs_size;
00172
00173
00174 octave_time fs_atime;
00175
00176
00177 octave_time fs_mtime;
00178
00179
00180 octave_time fs_ctime;
00181
00182
00183 dev_t fs_rdev;
00184
00185
00186 long fs_blksize;
00187
00188
00189 long fs_blocks;
00190 };
00191
00192 class
00193 OCTAVE_API
00194 file_stat : public base_file_stat
00195 {
00196 public:
00197
00198 file_stat (const std::string& n = std::string (), bool fl = true)
00199 : base_file_stat (), file_name (n), follow_links (fl)
00200 {
00201 if (! file_name.empty ())
00202 update_internal ();
00203 }
00204
00205 file_stat (const file_stat& fs)
00206 : base_file_stat (fs), file_name (fs.file_name),
00207 follow_links (fs.follow_links) { }
00208
00209 file_stat& operator = (const file_stat& fs)
00210 {
00211 if (this != &fs)
00212 {
00213 base_file_stat::operator = (fs);
00214
00215 file_name = fs.file_name;
00216 follow_links = fs.follow_links;
00217 }
00218
00219 return *this;
00220 }
00221
00222 ~file_stat (void) { }
00223
00224 void get_stats (bool force = false)
00225 {
00226 if (! initialized || force)
00227 update_internal (force);
00228 }
00229
00230 void get_stats (const std::string& n, bool force = false)
00231 {
00232 if (n != file_name || ! initialized || force)
00233 {
00234 initialized = false;
00235
00236 file_name = n;
00237
00238 update_internal (force);
00239 }
00240 }
00241
00242 private:
00243
00244
00245 std::string file_name;
00246
00247
00248
00249 bool follow_links;
00250
00251 void update_internal (bool force = false);
00252 };
00253
00254 class
00255 OCTAVE_API
00256 file_fstat : public base_file_stat
00257 {
00258 public:
00259
00260 file_fstat (int n) : base_file_stat (), fid (n)
00261 {
00262 update_internal ();
00263 }
00264
00265 file_fstat (const file_fstat& fs)
00266 : base_file_stat (fs), fid (fs.fid) { }
00267
00268 file_fstat& operator = (const file_fstat& fs)
00269 {
00270 if (this != &fs)
00271 {
00272 base_file_stat::operator = (fs);
00273
00274 fid = fs.fid;
00275 }
00276
00277 return *this;
00278 }
00279
00280 ~file_fstat (void) { }
00281
00282 void get_stats (bool force = false)
00283 {
00284 if (! initialized || force)
00285 update_internal (force);
00286 }
00287
00288 void get_stats (int n, bool force = false)
00289 {
00290 if (n != fid || ! initialized || force)
00291 {
00292 initialized = false;
00293
00294 fid = n;
00295
00296 update_internal (force);
00297 }
00298 }
00299
00300 private:
00301
00302
00303 int fid;
00304
00305 void update_internal (bool force = false);
00306 };
00307
00308 #endif
00309
00310
00311
00312
00313
00314