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 |
6108
|
36 OCTAVE_API |
2926
|
37 file_stat |
|
38 { |
|
39 public: |
|
40 |
3504
|
41 file_stat (const std::string& n = std::string (), bool fl = true) |
2926
|
42 : file_name (n), follow_links (fl), initialized (false) |
|
43 { |
|
44 if (! file_name.empty ()) |
|
45 update_internal (); |
|
46 } |
|
47 |
|
48 file_stat (const file_stat& f) { copy (f); } |
|
49 |
|
50 file_stat& operator = (const file_stat& f) |
|
51 { |
|
52 if (this != &f) |
|
53 copy (f); |
|
54 |
|
55 return *this; |
|
56 } |
|
57 |
|
58 ~file_stat (void) { } |
|
59 |
|
60 void get_stats (bool force = false) |
|
61 { |
|
62 if (! initialized || force) |
|
63 update_internal (force); |
|
64 } |
|
65 |
3504
|
66 void get_stats (const std::string& n, bool force = false) |
2926
|
67 { |
|
68 if (n != file_name || ! initialized || force) |
|
69 { |
|
70 initialized = false; |
|
71 |
|
72 file_name = n; |
|
73 |
|
74 update_internal (force); |
|
75 } |
|
76 } |
|
77 |
|
78 // File status and info. These should only be called for objects |
|
79 // that are already properly initialized. |
|
80 |
|
81 bool is_blk (void) const; |
|
82 bool is_chr (void) const; |
|
83 bool is_dir (void) const; |
|
84 bool is_fifo (void) const; |
|
85 bool is_lnk (void) const; |
|
86 bool is_reg (void) const; |
|
87 bool is_sock (void) const; |
|
88 |
5476
|
89 static bool is_blk (mode_t mode); |
|
90 static bool is_chr (mode_t mode); |
|
91 static bool is_dir (mode_t mode); |
|
92 static bool is_fifo (mode_t mode); |
|
93 static bool is_lnk (mode_t mode); |
|
94 static bool is_reg (mode_t mode); |
|
95 static bool is_sock (mode_t mode); |
|
96 |
2926
|
97 ino_t ino (void) const { return fs_ino; } |
|
98 dev_t dev (void) const { return fs_dev; } |
|
99 |
|
100 nlink_t nlink (void) const { return fs_nlink; } |
|
101 |
|
102 uid_t uid (void) const { return fs_uid; } |
|
103 gid_t gid (void) const { return fs_gid; } |
|
104 |
|
105 off_t size (void) const { return fs_size; } |
|
106 |
3260
|
107 octave_time atime (void) const { return fs_atime; } |
|
108 octave_time mtime (void) const { return fs_mtime; } |
|
109 octave_time ctime (void) const { return fs_ctime; } |
2926
|
110 |
3887
|
111 #if defined (HAVE_STRUCT_STAT_ST_RDEV) |
2926
|
112 dev_t rdev (void) const { return fs_rdev; } |
|
113 #endif |
|
114 |
3887
|
115 #if defined (HAVE_STRUCT_STAT_ST_BLKSIZE) |
2926
|
116 long blksize (void) const { return fs_blksize; } |
|
117 #endif |
|
118 |
3887
|
119 #if defined (HAVE_STRUCT_STAT_ST_BLOCKS) |
2926
|
120 long blocks (void) const { return fs_blocks; } |
|
121 #endif |
|
122 |
5476
|
123 mode_t mode (void) const { return fs_mode; } |
|
124 |
3504
|
125 std::string mode_as_string (void) const; |
2926
|
126 |
|
127 bool ok (void) const { return initialized && ! fail; } |
|
128 |
3145
|
129 operator bool () const { return ok (); } |
2926
|
130 |
|
131 bool exists (void) const { return ok (); } |
|
132 |
3504
|
133 std::string error (void) const { return ok () ? std::string () : errmsg; } |
2926
|
134 |
|
135 // Has the file referenced by this object been modified since TIME? |
3260
|
136 bool is_newer (const octave_time& time) const { return fs_mtime > time; } |
2926
|
137 |
|
138 // It's nice to be able to hide the file_stat object if we don't |
|
139 // really care about it. |
3504
|
140 static int is_newer (const std::string&, const octave_time&); |
2926
|
141 |
|
142 private: |
|
143 |
|
144 // Name of the file. |
3504
|
145 std::string file_name; |
2926
|
146 |
|
147 // TRUE means follow symbolic links to the ultimate file (stat). |
|
148 // FALSE means get information about the link itself (lstat). |
|
149 bool follow_links; |
|
150 |
|
151 // TRUE means we have already called stat. |
|
152 bool initialized; |
|
153 |
|
154 // TRUE means the stat for this file failed. |
|
155 bool fail; |
|
156 |
|
157 // If a failure occurs, this contains the system error text. |
3504
|
158 std::string errmsg; |
2926
|
159 |
|
160 // file type and permissions |
|
161 mode_t fs_mode; |
|
162 |
|
163 // serial number |
|
164 ino_t fs_ino; |
|
165 |
|
166 // device number |
|
167 dev_t fs_dev; |
|
168 |
|
169 // number of links |
|
170 nlink_t fs_nlink; |
|
171 |
|
172 // user ID of owner |
|
173 uid_t fs_uid; |
|
174 |
|
175 // group ID of owner |
|
176 gid_t fs_gid; |
|
177 |
|
178 // size in bytes, for regular files |
|
179 off_t fs_size; |
|
180 |
|
181 // time of last access |
3260
|
182 octave_time fs_atime; |
2926
|
183 |
|
184 // time of last modification |
3260
|
185 octave_time fs_mtime; |
2926
|
186 |
|
187 // time of last file status change |
3260
|
188 octave_time fs_ctime; |
2926
|
189 |
3887
|
190 #if defined (HAVE_STRUCT_STAT_ST_RDEV) |
2926
|
191 // device number for special files |
|
192 dev_t fs_rdev; |
|
193 #endif |
|
194 |
3887
|
195 #if defined (HAVE_STRUCT_STAT_ST_BLKSIZE) |
2926
|
196 // best I/O block size |
|
197 long fs_blksize; |
|
198 #endif |
|
199 |
3887
|
200 #if defined (HAVE_STRUCT_STAT_ST_BLOCKS) |
2926
|
201 // number of 512-byte blocks allocated |
|
202 long fs_blocks; |
|
203 #endif |
|
204 |
|
205 void update_internal (bool force = false); |
|
206 |
|
207 void copy (const file_stat&); |
|
208 }; |
|
209 |
|
210 #endif |
|
211 |
|
212 /* |
|
213 ;;; Local Variables: *** |
|
214 ;;; mode: C++ *** |
|
215 ;;; End: *** |
|
216 */ |