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" |
1773
|
37 #include "safe-lstat.h" |
|
38 #include "safe-stat.h" |
1765
|
39 #include "statdefs.h" |
|
40 |
|
41 // XXX FIXME XXX -- the is_* and mode_as_string functions are only valid |
|
42 // for initialized objects. If called for an object that is not |
|
43 // initialized, they should throw an exception. |
|
44 |
|
45 bool |
|
46 file_stat::is_blk (void) const |
|
47 { |
|
48 return S_ISBLK (fs_mode); |
|
49 } |
|
50 |
|
51 bool |
|
52 file_stat::is_chr (void) const |
|
53 { |
|
54 return S_ISCHR (fs_mode); |
|
55 } |
|
56 |
|
57 bool |
|
58 file_stat::is_dir (void) const |
|
59 { |
|
60 return S_ISDIR (fs_mode); |
|
61 } |
|
62 |
|
63 bool |
|
64 file_stat::is_fifo (void) const |
|
65 { |
|
66 return S_ISFIFO (fs_mode); |
|
67 } |
|
68 |
|
69 bool |
|
70 file_stat::is_lnk (void) const |
|
71 { |
|
72 return S_ISLNK (fs_mode); |
|
73 } |
|
74 |
|
75 bool |
|
76 file_stat::is_reg (void) const |
|
77 { |
|
78 return S_ISREG (fs_mode); |
|
79 } |
|
80 |
|
81 bool |
|
82 file_stat::is_sock (void) const |
|
83 { |
|
84 return S_ISSOCK (fs_mode); |
|
85 } |
|
86 |
|
87 extern "C" void mode_string (); |
|
88 |
|
89 string |
|
90 file_stat::mode_as_string (void) const |
|
91 { |
|
92 char buf[11]; |
|
93 |
|
94 mode_string (fs_mode, buf); |
|
95 |
|
96 buf[10] = '\0'; |
|
97 |
|
98 return buf; |
|
99 } |
|
100 |
|
101 // Private stuff: |
|
102 |
|
103 void |
|
104 file_stat::update_internal (bool force) |
|
105 { |
|
106 if (! initialized || force) |
|
107 { |
|
108 initialized = false; |
|
109 fail = false; |
|
110 |
|
111 const char *cname = file_name.c_str (); |
|
112 |
|
113 struct stat buf; |
|
114 |
|
115 int status = follow_links |
1773
|
116 ? safe_stat (cname, &buf) : safe_lstat (cname, &buf); |
1765
|
117 |
|
118 if (status < 0) |
|
119 { |
|
120 fail = true; |
|
121 errmsg = strerror (errno); |
|
122 } |
|
123 else |
|
124 { |
|
125 fs_mode = buf.st_mode; |
|
126 fs_ino = buf.st_ino; |
|
127 fs_dev = buf.st_dev; |
|
128 fs_nlink = buf.st_nlink; |
|
129 fs_uid = buf.st_uid; |
|
130 fs_gid = buf.st_gid; |
|
131 fs_size = buf.st_size; |
|
132 fs_atime = buf.st_atime; |
|
133 fs_mtime = buf.st_mtime; |
|
134 fs_ctime = buf.st_ctime; |
|
135 |
|
136 #if defined (HAVE_ST_RDEV) |
|
137 fs_rdev = buf.st_rdev; |
|
138 #endif |
|
139 |
|
140 #if defined (HAVE_ST_BLKSIZE) |
|
141 fs_blksize = buf.st_blksize; |
|
142 #endif |
|
143 |
|
144 #if defined (HAVE_ST_BLOCKS) |
|
145 fs_blocks = buf.st_blocks; |
|
146 #endif |
|
147 } |
|
148 |
|
149 initialized = true; |
|
150 } |
|
151 } |
|
152 |
|
153 // Functions for octave. |
|
154 |
|
155 // Has FILE been modified since TIME? Returns 1 for yes, 0 for no, |
|
156 // and -1 for any error. |
|
157 int |
|
158 is_newer (const string& file, time_t time) |
|
159 { |
|
160 file_stat fs (file); |
|
161 |
|
162 return fs ? fs.is_newer (time) : -1; |
|
163 } |
|
164 |
|
165 int |
1772
|
166 oct_mkdir (const string& name, mode_t mode) |
1765
|
167 { |
|
168 return mkdir (name.c_str (), mode); |
|
169 } |
|
170 |
|
171 int |
1773
|
172 oct_mkfifo (const string& name, mode_t mode) |
1765
|
173 { |
1773
|
174 return mkfifo (name.c_str (), mode); |
1765
|
175 } |
|
176 |
|
177 int |
1772
|
178 oct_rename (const string& from, const string& to) |
1765
|
179 { |
|
180 return rename (from.c_str (), to.c_str ()); |
|
181 } |
|
182 |
|
183 int |
1773
|
184 oct_rmdir (const string& name) |
1765
|
185 { |
1773
|
186 return rmdir (name.c_str ()); |
1765
|
187 } |
|
188 |
|
189 int |
1772
|
190 oct_umask (mode_t mode) |
1765
|
191 { |
|
192 #if defined (HAVE_UMASK) |
|
193 return umask (mode); |
|
194 #else |
|
195 return 0; |
|
196 #endif |
|
197 } |
|
198 |
1773
|
199 int |
|
200 oct_unlink (const string& name) |
|
201 { |
|
202 return unlink (name.c_str ()); |
|
203 } |
|
204 |
1765
|
205 /* |
|
206 ;;; Local Variables: *** |
|
207 ;;; mode: C++ *** |
|
208 ;;; page-delimiter: "^/\\*" *** |
|
209 ;;; End: *** |
|
210 */ |