1765
|
1 // file-ops.cc -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1996 John W. Eaton |
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include <cerrno> |
|
29 #include <cstring> |
|
30 |
|
31 #ifdef HAVE_UNISTD_H |
|
32 #include <sys/types.h> |
|
33 #include <unistd.h> |
|
34 #endif |
|
35 |
|
36 #include "file-ops.h" |
1775
|
37 #include "statdefs.h" |
|
38 |
|
39 // These must come after <sys/types.h> and <sys/stat.h>. |
|
40 |
1773
|
41 #include "safe-lstat.h" |
|
42 #include "safe-stat.h" |
1765
|
43 |
|
44 // XXX FIXME XXX -- the is_* and mode_as_string functions are only valid |
|
45 // for initialized objects. If called for an object that is not |
|
46 // initialized, they should throw an exception. |
|
47 |
|
48 bool |
|
49 file_stat::is_blk (void) const |
|
50 { |
|
51 return S_ISBLK (fs_mode); |
|
52 } |
|
53 |
|
54 bool |
|
55 file_stat::is_chr (void) const |
|
56 { |
|
57 return S_ISCHR (fs_mode); |
|
58 } |
|
59 |
|
60 bool |
|
61 file_stat::is_dir (void) const |
|
62 { |
|
63 return S_ISDIR (fs_mode); |
|
64 } |
|
65 |
|
66 bool |
|
67 file_stat::is_fifo (void) const |
|
68 { |
|
69 return S_ISFIFO (fs_mode); |
|
70 } |
|
71 |
|
72 bool |
|
73 file_stat::is_lnk (void) const |
|
74 { |
|
75 return S_ISLNK (fs_mode); |
|
76 } |
|
77 |
|
78 bool |
|
79 file_stat::is_reg (void) const |
|
80 { |
|
81 return S_ISREG (fs_mode); |
|
82 } |
|
83 |
|
84 bool |
|
85 file_stat::is_sock (void) const |
|
86 { |
|
87 return S_ISSOCK (fs_mode); |
|
88 } |
|
89 |
|
90 extern "C" void mode_string (); |
|
91 |
|
92 string |
|
93 file_stat::mode_as_string (void) const |
|
94 { |
|
95 char buf[11]; |
|
96 |
|
97 mode_string (fs_mode, buf); |
|
98 |
|
99 buf[10] = '\0'; |
|
100 |
|
101 return buf; |
|
102 } |
|
103 |
|
104 // Private stuff: |
|
105 |
|
106 void |
|
107 file_stat::update_internal (bool force) |
|
108 { |
|
109 if (! initialized || force) |
|
110 { |
|
111 initialized = false; |
|
112 fail = false; |
|
113 |
|
114 const char *cname = file_name.c_str (); |
|
115 |
|
116 struct stat buf; |
|
117 |
|
118 int status = follow_links |
1773
|
119 ? safe_stat (cname, &buf) : safe_lstat (cname, &buf); |
1765
|
120 |
|
121 if (status < 0) |
|
122 { |
|
123 fail = true; |
|
124 errmsg = strerror (errno); |
|
125 } |
|
126 else |
|
127 { |
|
128 fs_mode = buf.st_mode; |
|
129 fs_ino = buf.st_ino; |
|
130 fs_dev = buf.st_dev; |
|
131 fs_nlink = buf.st_nlink; |
|
132 fs_uid = buf.st_uid; |
|
133 fs_gid = buf.st_gid; |
|
134 fs_size = buf.st_size; |
|
135 fs_atime = buf.st_atime; |
|
136 fs_mtime = buf.st_mtime; |
|
137 fs_ctime = buf.st_ctime; |
|
138 |
|
139 #if defined (HAVE_ST_RDEV) |
|
140 fs_rdev = buf.st_rdev; |
|
141 #endif |
|
142 |
|
143 #if defined (HAVE_ST_BLKSIZE) |
|
144 fs_blksize = buf.st_blksize; |
|
145 #endif |
|
146 |
|
147 #if defined (HAVE_ST_BLOCKS) |
|
148 fs_blocks = buf.st_blocks; |
|
149 #endif |
|
150 } |
|
151 |
|
152 initialized = true; |
|
153 } |
|
154 } |
|
155 |
|
156 // Functions for octave. |
|
157 |
|
158 // Has FILE been modified since TIME? Returns 1 for yes, 0 for no, |
|
159 // and -1 for any error. |
|
160 int |
|
161 is_newer (const string& file, time_t time) |
|
162 { |
|
163 file_stat fs (file); |
|
164 |
|
165 return fs ? fs.is_newer (time) : -1; |
|
166 } |
|
167 |
|
168 int |
1772
|
169 oct_mkdir (const string& name, mode_t mode) |
1765
|
170 { |
|
171 return mkdir (name.c_str (), mode); |
|
172 } |
|
173 |
|
174 int |
1773
|
175 oct_mkfifo (const string& name, mode_t mode) |
1765
|
176 { |
1773
|
177 return mkfifo (name.c_str (), mode); |
1765
|
178 } |
|
179 |
|
180 int |
1772
|
181 oct_rename (const string& from, const string& to) |
1765
|
182 { |
|
183 return rename (from.c_str (), to.c_str ()); |
|
184 } |
|
185 |
|
186 int |
1773
|
187 oct_rmdir (const string& name) |
1765
|
188 { |
1773
|
189 return rmdir (name.c_str ()); |
1765
|
190 } |
|
191 |
|
192 int |
1772
|
193 oct_umask (mode_t mode) |
1765
|
194 { |
|
195 #if defined (HAVE_UMASK) |
|
196 return umask (mode); |
|
197 #else |
|
198 return 0; |
|
199 #endif |
|
200 } |
|
201 |
1773
|
202 int |
|
203 oct_unlink (const string& name) |
|
204 { |
|
205 return unlink (name.c_str ()); |
|
206 } |
|
207 |
1765
|
208 /* |
|
209 ;;; Local Variables: *** |
|
210 ;;; mode: C++ *** |
|
211 ;;; page-delimiter: "^/\\*" *** |
|
212 ;;; End: *** |
|
213 */ |