1765
|
1 /* |
|
2 |
|
3 Copyright (C) 1996 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 |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #if !defined (octave_file_ops_h) |
|
24 #define octave_file_ops_h 1 |
|
25 |
|
26 #include <string> |
|
27 |
|
28 class |
|
29 file_stat |
|
30 { |
|
31 public: |
|
32 |
|
33 file_stat (const string& n = string (), bool fl = 1) |
|
34 : file_name (n), follow_links (fl), initialized (false) |
|
35 { |
|
36 if (! file_name.empty ()) |
|
37 update_internal (); |
|
38 } |
|
39 |
1778
|
40 file_stat (const file_stat& f) { copy (f); } |
1765
|
41 |
|
42 file_stat& operator = (const file_stat& f) |
|
43 { |
1879
|
44 if (this != &f) |
|
45 copy (f); |
|
46 |
1765
|
47 return *this; |
|
48 } |
|
49 |
|
50 ~file_stat (void) { } |
|
51 |
|
52 void get_stats (bool force = false) |
|
53 { |
|
54 if (! initialized || force) |
|
55 update_internal (force); |
|
56 } |
|
57 |
|
58 void get_stats (const string& n, bool force = false) |
|
59 { |
|
60 if (n != file_name || ! initialized || force) |
|
61 { |
|
62 initialized = false; |
|
63 |
|
64 file_name = n; |
|
65 |
|
66 update_internal (force); |
|
67 } |
|
68 } |
|
69 |
|
70 // File status and info. These should only be called for objects |
|
71 // that are already properly initialized. |
|
72 |
|
73 bool is_blk (void) const; |
|
74 bool is_chr (void) const; |
|
75 bool is_dir (void) const; |
|
76 bool is_fifo (void) const; |
|
77 bool is_lnk (void) const; |
|
78 bool is_reg (void) const; |
|
79 bool is_sock (void) const; |
|
80 |
|
81 ino_t ino (void) const { return fs_ino; } |
|
82 dev_t dev (void) const { return fs_dev; } |
|
83 |
|
84 nlink_t nlink (void) const { return fs_nlink; } |
|
85 |
|
86 uid_t uid (void) const { return fs_uid; } |
|
87 gid_t gid (void) const { return fs_gid; } |
|
88 |
|
89 off_t size (void) const { return fs_size; } |
|
90 |
|
91 time_t atime (void) const { return fs_atime; } |
|
92 time_t mtime (void) const { return fs_mtime; } |
|
93 time_t ctime (void) const { return fs_ctime; } |
|
94 |
|
95 #if defined (HAVE_ST_RDEV) |
|
96 dev_t rdev (void) const { return fs_rdev; } |
|
97 #endif |
|
98 |
|
99 #if defined (HAVE_ST_BLKSIZE) |
|
100 long blksize (void) const { return fs_blksize; } |
|
101 #endif |
|
102 |
|
103 #if defined (HAVE_ST_BLOCKS) |
|
104 long blocks (void) const { return fs_blocks; } |
|
105 #endif |
|
106 |
|
107 string mode_as_string (void) const; |
|
108 |
|
109 bool ok (void) const { return initialized && ! fail; } |
|
110 |
|
111 operator void* () const { return ok () ? (void *) -1 : (void *) 0; } |
|
112 |
|
113 bool exists (void) const { return ok (); } |
|
114 |
|
115 string error (void) const { return ok () ? string () : errmsg; } |
|
116 |
|
117 // Has the file referenced by this object been modified since TIME? |
|
118 bool is_newer (time_t time) const { return fs_mtime > time; } |
|
119 |
|
120 private: |
|
121 |
|
122 // Name of the file. |
|
123 string file_name; |
|
124 |
|
125 // TRUE means follow symbolic links to the ultimate file (stat). |
|
126 // FALSE means get information about the link itself (lstat). |
|
127 bool follow_links; |
|
128 |
|
129 // TRUE means we have already called stat. |
|
130 bool initialized; |
|
131 |
|
132 // TRUE means the stat for this file failed. |
|
133 bool fail; |
|
134 |
|
135 // If a failure occurs, this contains the system error text. |
|
136 string errmsg; |
|
137 |
|
138 // file type and permissions |
|
139 mode_t fs_mode; |
|
140 |
|
141 // serial number |
|
142 ino_t fs_ino; |
|
143 |
|
144 // device number |
|
145 dev_t fs_dev; |
|
146 |
|
147 // number of links |
|
148 nlink_t fs_nlink; |
|
149 |
|
150 // user ID of owner |
|
151 uid_t fs_uid; |
|
152 |
|
153 // group ID of owner |
|
154 gid_t fs_gid; |
|
155 |
|
156 // size in bytes, for regular files |
|
157 off_t fs_size; |
|
158 |
|
159 // time of last access |
|
160 time_t fs_atime; |
|
161 |
|
162 // time of last modification |
|
163 time_t fs_mtime; |
|
164 |
|
165 // time of last file status change |
|
166 time_t fs_ctime; |
|
167 |
|
168 #if defined (HAVE_ST_RDEV) |
|
169 // device number for special files |
|
170 dev_t fs_rdev; |
|
171 #endif |
|
172 |
|
173 #if defined (HAVE_ST_BLKSIZE) |
|
174 // best I/O block size |
|
175 long fs_blksize; |
|
176 #endif |
|
177 |
|
178 #if defined (HAVE_ST_BLOCKS) |
|
179 // number of 512-byte blocks allocated |
|
180 long fs_blocks; |
|
181 #endif |
|
182 |
|
183 void update_internal (bool force = false); |
1778
|
184 |
|
185 void copy (const file_stat&); |
1765
|
186 }; |
|
187 |
|
188 extern int is_newer (const string&, time_t); |
|
189 |
1772
|
190 extern int oct_mkdir (const string&, mode_t); |
1773
|
191 extern int oct_mkfifo (const string&, mode_t); |
1772
|
192 extern int oct_rename (const string&, const string&); |
1773
|
193 extern int oct_rmdir (const string&); |
1801
|
194 extern string oct_tempnam (void); |
1772
|
195 extern int oct_umask (mode_t); |
1773
|
196 extern int oct_unlink (const string&); |
1765
|
197 |
|
198 #endif |
|
199 |
|
200 /* |
|
201 ;;; Local Variables: *** |
|
202 ;;; mode: C++ *** |
|
203 ;;; End: *** |
|
204 */ |