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