2926
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
2926
|
21 |
|
22 */ |
|
23 |
|
24 #if !defined (octave_file_stat_h) |
|
25 #define octave_file_stat_h 1 |
|
26 |
|
27 #include <string> |
|
28 |
3260
|
29 #include "oct-time.h" |
|
30 |
2926
|
31 #ifdef HAVE_SYS_TYPES_H |
|
32 #include <sys/types.h> |
|
33 #endif |
|
34 |
|
35 class |
|
36 file_stat |
|
37 { |
|
38 public: |
|
39 |
3504
|
40 file_stat (const std::string& n = std::string (), bool fl = true) |
2926
|
41 : file_name (n), follow_links (fl), initialized (false) |
|
42 { |
|
43 if (! file_name.empty ()) |
|
44 update_internal (); |
|
45 } |
|
46 |
|
47 file_stat (const file_stat& f) { copy (f); } |
|
48 |
|
49 file_stat& operator = (const file_stat& f) |
|
50 { |
|
51 if (this != &f) |
|
52 copy (f); |
|
53 |
|
54 return *this; |
|
55 } |
|
56 |
|
57 ~file_stat (void) { } |
|
58 |
|
59 void get_stats (bool force = false) |
|
60 { |
|
61 if (! initialized || force) |
|
62 update_internal (force); |
|
63 } |
|
64 |
3504
|
65 void get_stats (const std::string& n, bool force = false) |
2926
|
66 { |
|
67 if (n != file_name || ! initialized || force) |
|
68 { |
|
69 initialized = false; |
|
70 |
|
71 file_name = n; |
|
72 |
|
73 update_internal (force); |
|
74 } |
|
75 } |
|
76 |
|
77 // File status and info. These should only be called for objects |
|
78 // that are already properly initialized. |
|
79 |
|
80 bool is_blk (void) const; |
|
81 bool is_chr (void) const; |
|
82 bool is_dir (void) const; |
|
83 bool is_fifo (void) const; |
|
84 bool is_lnk (void) const; |
|
85 bool is_reg (void) const; |
|
86 bool is_sock (void) const; |
|
87 |
|
88 ino_t ino (void) const { return fs_ino; } |
|
89 dev_t dev (void) const { return fs_dev; } |
|
90 |
|
91 nlink_t nlink (void) const { return fs_nlink; } |
|
92 |
|
93 uid_t uid (void) const { return fs_uid; } |
|
94 gid_t gid (void) const { return fs_gid; } |
|
95 |
|
96 off_t size (void) const { return fs_size; } |
|
97 |
3260
|
98 octave_time atime (void) const { return fs_atime; } |
|
99 octave_time mtime (void) const { return fs_mtime; } |
|
100 octave_time ctime (void) const { return fs_ctime; } |
2926
|
101 |
3887
|
102 #if defined (HAVE_STRUCT_STAT_ST_RDEV) |
2926
|
103 dev_t rdev (void) const { return fs_rdev; } |
|
104 #endif |
|
105 |
3887
|
106 #if defined (HAVE_STRUCT_STAT_ST_BLKSIZE) |
2926
|
107 long blksize (void) const { return fs_blksize; } |
|
108 #endif |
|
109 |
3887
|
110 #if defined (HAVE_STRUCT_STAT_ST_BLOCKS) |
2926
|
111 long blocks (void) const { return fs_blocks; } |
|
112 #endif |
|
113 |
3504
|
114 std::string mode_as_string (void) const; |
2926
|
115 |
|
116 bool ok (void) const { return initialized && ! fail; } |
|
117 |
3145
|
118 operator bool () const { return ok (); } |
2926
|
119 |
|
120 bool exists (void) const { return ok (); } |
|
121 |
3504
|
122 std::string error (void) const { return ok () ? std::string () : errmsg; } |
2926
|
123 |
|
124 // Has the file referenced by this object been modified since TIME? |
3260
|
125 bool is_newer (const octave_time& time) const { return fs_mtime > time; } |
2926
|
126 |
|
127 // It's nice to be able to hide the file_stat object if we don't |
|
128 // really care about it. |
3504
|
129 static int is_newer (const std::string&, const octave_time&); |
2926
|
130 |
|
131 private: |
|
132 |
|
133 // Name of the file. |
3504
|
134 std::string file_name; |
2926
|
135 |
|
136 // TRUE means follow symbolic links to the ultimate file (stat). |
|
137 // FALSE means get information about the link itself (lstat). |
|
138 bool follow_links; |
|
139 |
|
140 // TRUE means we have already called stat. |
|
141 bool initialized; |
|
142 |
|
143 // TRUE means the stat for this file failed. |
|
144 bool fail; |
|
145 |
|
146 // If a failure occurs, this contains the system error text. |
3504
|
147 std::string errmsg; |
2926
|
148 |
|
149 // file type and permissions |
|
150 mode_t fs_mode; |
|
151 |
|
152 // serial number |
|
153 ino_t fs_ino; |
|
154 |
|
155 // device number |
|
156 dev_t fs_dev; |
|
157 |
|
158 // number of links |
|
159 nlink_t fs_nlink; |
|
160 |
|
161 // user ID of owner |
|
162 uid_t fs_uid; |
|
163 |
|
164 // group ID of owner |
|
165 gid_t fs_gid; |
|
166 |
|
167 // size in bytes, for regular files |
|
168 off_t fs_size; |
|
169 |
|
170 // time of last access |
3260
|
171 octave_time fs_atime; |
2926
|
172 |
|
173 // time of last modification |
3260
|
174 octave_time fs_mtime; |
2926
|
175 |
|
176 // time of last file status change |
3260
|
177 octave_time fs_ctime; |
2926
|
178 |
3887
|
179 #if defined (HAVE_STRUCT_STAT_ST_RDEV) |
2926
|
180 // device number for special files |
|
181 dev_t fs_rdev; |
|
182 #endif |
|
183 |
3887
|
184 #if defined (HAVE_STRUCT_STAT_ST_BLKSIZE) |
2926
|
185 // best I/O block size |
|
186 long fs_blksize; |
|
187 #endif |
|
188 |
3887
|
189 #if defined (HAVE_STRUCT_STAT_ST_BLOCKS) |
2926
|
190 // number of 512-byte blocks allocated |
|
191 long fs_blocks; |
|
192 #endif |
|
193 |
|
194 void update_internal (bool force = false); |
|
195 |
|
196 void copy (const file_stat&); |
|
197 }; |
|
198 |
|
199 #endif |
|
200 |
|
201 /* |
|
202 ;;; Local Variables: *** |
|
203 ;;; mode: C++ *** |
|
204 ;;; End: *** |
|
205 */ |