1 | // |
---|
2 | // File_VMS.cpp |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/src/File_VMS.cpp#5 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Filesystem |
---|
8 | // Module: File |
---|
9 | // |
---|
10 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
11 | // and Contributors. |
---|
12 | // |
---|
13 | // Permission is hereby granted, free of charge, to any person or organization |
---|
14 | // obtaining a copy of the software and accompanying documentation covered by |
---|
15 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
16 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
17 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
18 | // do so, all subject to the following: |
---|
19 | // |
---|
20 | // The copyright notices in the Software and this entire statement, including |
---|
21 | // the above license grant, this restriction and the following disclaimer, |
---|
22 | // must be included in all copies of the Software, in whole or in part, and |
---|
23 | // all derivative works of the Software, unless such copies or derivative |
---|
24 | // works are solely in the form of machine-executable object code generated by |
---|
25 | // a source language processor. |
---|
26 | // |
---|
27 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
28 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
29 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
30 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
31 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
32 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
33 | // DEALINGS IN THE SOFTWARE. |
---|
34 | // |
---|
35 | |
---|
36 | |
---|
37 | #include "Poco/File_VMS.h" |
---|
38 | #include "Poco/Exception.h" |
---|
39 | #include "Poco/Path.h" |
---|
40 | #include "Poco/String.h" |
---|
41 | #include <stdio.h> |
---|
42 | #include <unistd.h> |
---|
43 | #include <unixio.h> |
---|
44 | #include <file.h> |
---|
45 | #include <stat.h> |
---|
46 | #include <errno.h> |
---|
47 | #include <utime.h> |
---|
48 | #include <lib$routines.h> |
---|
49 | #include <rmsdef.h> |
---|
50 | #include <starlet.h> |
---|
51 | #include <lnmdef.h> |
---|
52 | #include <ssdef.h> |
---|
53 | #include <descrip.h> |
---|
54 | |
---|
55 | |
---|
56 | namespace Poco { |
---|
57 | |
---|
58 | |
---|
59 | FileImpl::FileImpl() |
---|
60 | { |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | FileImpl::FileImpl(const std::string& path): _path(path) |
---|
65 | { |
---|
66 | if (!_path.empty()) |
---|
67 | { |
---|
68 | Path p(_path); |
---|
69 | p.makeFile(); |
---|
70 | _path = p.toString(); |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | |
---|
75 | FileImpl::~FileImpl() |
---|
76 | { |
---|
77 | } |
---|
78 | |
---|
79 | |
---|
80 | void FileImpl::swapImpl(FileImpl& file) |
---|
81 | { |
---|
82 | std::swap(_path, file._path); |
---|
83 | } |
---|
84 | |
---|
85 | |
---|
86 | void FileImpl::setPathImpl(const std::string& path) |
---|
87 | { |
---|
88 | _path = path; |
---|
89 | } |
---|
90 | |
---|
91 | |
---|
92 | bool FileImpl::existsImpl() const |
---|
93 | { |
---|
94 | poco_assert (!_path.empty()); |
---|
95 | |
---|
96 | return access(_path.c_str(), F_OK) == 0; |
---|
97 | } |
---|
98 | |
---|
99 | |
---|
100 | bool FileImpl::canReadImpl() const |
---|
101 | { |
---|
102 | poco_assert (!_path.empty()); |
---|
103 | |
---|
104 | struct stat st; |
---|
105 | if (stat(_path.c_str(), &st) == 0) |
---|
106 | { |
---|
107 | if (st.st_uid == geteuid()) |
---|
108 | return (st.st_mode & S_IRUSR) != 0; |
---|
109 | else if (st.st_gid == getegid()) |
---|
110 | return (st.st_mode & S_IRGRP) != 0; |
---|
111 | else |
---|
112 | return (st.st_mode & S_IROTH) != 0; |
---|
113 | } |
---|
114 | else handleLastErrorImpl(_path); |
---|
115 | return false; |
---|
116 | } |
---|
117 | |
---|
118 | |
---|
119 | bool FileImpl::canWriteImpl() const |
---|
120 | { |
---|
121 | poco_assert (!_path.empty()); |
---|
122 | |
---|
123 | struct stat st; |
---|
124 | if (stat(_path.c_str(), &st) == 0) |
---|
125 | { |
---|
126 | if (st.st_uid == geteuid()) |
---|
127 | return (st.st_mode & S_IWUSR) != 0; |
---|
128 | else if (st.st_gid == getegid()) |
---|
129 | return (st.st_mode & S_IWGRP) != 0; |
---|
130 | else |
---|
131 | return (st.st_mode & S_IWOTH) != 0; |
---|
132 | } |
---|
133 | else handleLastErrorImpl(_path); |
---|
134 | return false; |
---|
135 | } |
---|
136 | |
---|
137 | |
---|
138 | bool FileImpl::canExecuteImpl() const |
---|
139 | { |
---|
140 | Path p(_path); |
---|
141 | return icompare(p.getExtension(), "exe") == 0; |
---|
142 | } |
---|
143 | |
---|
144 | |
---|
145 | bool FileImpl::isFileImpl() const |
---|
146 | { |
---|
147 | poco_assert (!_path.empty()); |
---|
148 | |
---|
149 | struct stat st; |
---|
150 | if (stat(_path.c_str(), &st) == 0) |
---|
151 | return S_ISREG(st.st_mode); |
---|
152 | else |
---|
153 | handleLastErrorImpl(_path); |
---|
154 | return false; |
---|
155 | } |
---|
156 | |
---|
157 | |
---|
158 | bool FileImpl::isDirectoryImpl() const |
---|
159 | { |
---|
160 | poco_assert (!_path.empty()); |
---|
161 | |
---|
162 | struct stat st; |
---|
163 | if (stat(_path.c_str(), &st) == 0) |
---|
164 | return S_ISDIR(st.st_mode); |
---|
165 | else |
---|
166 | handleLastErrorImpl(_path); |
---|
167 | return false; |
---|
168 | } |
---|
169 | |
---|
170 | |
---|
171 | bool FileImpl::isLinkImpl() const |
---|
172 | { |
---|
173 | return false; |
---|
174 | } |
---|
175 | |
---|
176 | |
---|
177 | bool FileImpl::isLinkImpl() const |
---|
178 | { |
---|
179 | return false; |
---|
180 | } |
---|
181 | |
---|
182 | |
---|
183 | bool FileImpl::isHiddenImpl() const |
---|
184 | { |
---|
185 | return false; |
---|
186 | } |
---|
187 | |
---|
188 | |
---|
189 | Timestamp FileImpl::createdImpl() const |
---|
190 | { |
---|
191 | poco_assert (!_path.empty()); |
---|
192 | |
---|
193 | struct stat st; |
---|
194 | if (stat(_path.c_str(), &st) == 0) |
---|
195 | return Timestamp(st.st_ctime); |
---|
196 | else |
---|
197 | handleLastErrorImpl(_path); |
---|
198 | return 0; |
---|
199 | } |
---|
200 | |
---|
201 | |
---|
202 | Timestamp FileImpl::getLastModifiedImpl() const |
---|
203 | { |
---|
204 | poco_assert (!_path.empty()); |
---|
205 | |
---|
206 | struct stat st; |
---|
207 | if (stat(_path.c_str(), &st) == 0) |
---|
208 | return Timestamp(st.st_mtime); |
---|
209 | else |
---|
210 | handleLastErrorImpl(_path); |
---|
211 | return 0; |
---|
212 | } |
---|
213 | |
---|
214 | |
---|
215 | void FileImpl::setLastModifiedImpl(const Timestamp& ts) |
---|
216 | { |
---|
217 | poco_assert (!_path.empty()); |
---|
218 | |
---|
219 | struct utimbuf tb; |
---|
220 | tb.actime = ts.epochTime(); |
---|
221 | tb.modtime = ts.epochTime(); |
---|
222 | if (utime(_path.c_str(), &tb) != 0) |
---|
223 | handleLastErrorImpl(_path); |
---|
224 | } |
---|
225 | |
---|
226 | |
---|
227 | FileImpl::FileSizeImpl FileImpl::getSizeImpl() const |
---|
228 | { |
---|
229 | poco_assert (!_path.empty()); |
---|
230 | |
---|
231 | struct stat st; |
---|
232 | if (stat(_path.c_str(), &st) == 0) |
---|
233 | return st.st_size; |
---|
234 | else |
---|
235 | handleLastErrorImpl(_path); |
---|
236 | return 0; |
---|
237 | } |
---|
238 | |
---|
239 | |
---|
240 | void FileImpl::setSizeImpl(FileSizeImpl size) |
---|
241 | { |
---|
242 | poco_assert (!_path.empty()); |
---|
243 | |
---|
244 | if (truncate(_path.c_str(), size) != 0) |
---|
245 | handleLastErrorImpl(_path); |
---|
246 | } |
---|
247 | |
---|
248 | |
---|
249 | void FileImpl::setWriteableImpl(bool flag) |
---|
250 | { |
---|
251 | poco_assert (!_path.empty()); |
---|
252 | |
---|
253 | struct stat st; |
---|
254 | if (stat(_path.c_str(), &st) != 0) |
---|
255 | handleLastErrorImpl(_path); |
---|
256 | mode_t mode; |
---|
257 | if (flag) |
---|
258 | { |
---|
259 | mode = st.st_mode | S_IWUSR; |
---|
260 | } |
---|
261 | else |
---|
262 | { |
---|
263 | mode_t wmask = S_IWUSR | S_IWGRP | S_IWOTH; |
---|
264 | mode = st.st_mode & ~wmask; |
---|
265 | } |
---|
266 | if (chmod(_path.c_str(), mode) != 0) |
---|
267 | handleLastErrorImpl(_path); |
---|
268 | } |
---|
269 | |
---|
270 | |
---|
271 | void FileImpl::setExecutableImpl(bool flag) |
---|
272 | { |
---|
273 | // not supported |
---|
274 | } |
---|
275 | |
---|
276 | |
---|
277 | void FileImpl::copyToImpl(const std::string& path) const |
---|
278 | { |
---|
279 | poco_assert (!_path.empty()); |
---|
280 | |
---|
281 | // copying a file correctly under OpenVMS is non-trivial, |
---|
282 | // so we just invoke the COPY program. |
---|
283 | std::string cmd = "COPY "; |
---|
284 | cmd.append(_path); |
---|
285 | cmd.append(" "); |
---|
286 | cmd.append(path); |
---|
287 | if (system(cmd.c_str()) != 0) |
---|
288 | throw FileException("COPY command failed", _path); |
---|
289 | } |
---|
290 | |
---|
291 | |
---|
292 | void FileImpl::renameToImpl(const std::string& path) |
---|
293 | { |
---|
294 | poco_assert (!_path.empty()); |
---|
295 | |
---|
296 | POCO_DESCRIPTOR_STRING(oldNameDsc, _path); |
---|
297 | POCO_DESCRIPTOR_STRING(newNameDsc, path); |
---|
298 | |
---|
299 | int res; |
---|
300 | if ((res = lib$rename_file(&oldNameDsc, &newNameDsc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)) != 1) |
---|
301 | { |
---|
302 | switch (res & 0x0FFFFFFF) |
---|
303 | { |
---|
304 | case RMS$_FNF: |
---|
305 | throw FileNotFoundException(_path); |
---|
306 | case RMS$_DEV: |
---|
307 | case RMS$_DNF: |
---|
308 | throw PathNotFoundException(_path); |
---|
309 | case RMS$_SYN: |
---|
310 | throw PathSyntaxException(path); |
---|
311 | case RMS$_RMV: |
---|
312 | throw FileAccessDeniedException(_path); |
---|
313 | case RMS$_PRV: |
---|
314 | throw FileAccessDeniedException("insufficient privileges", _path); |
---|
315 | default: |
---|
316 | throw FileException(path); |
---|
317 | } |
---|
318 | } |
---|
319 | } |
---|
320 | |
---|
321 | |
---|
322 | void FileImpl::removeImpl() |
---|
323 | { |
---|
324 | poco_assert (!_path.empty()); |
---|
325 | |
---|
326 | int rc; |
---|
327 | if (isDirectoryImpl()) |
---|
328 | { |
---|
329 | setWriteableImpl(true); |
---|
330 | rc = rmdir(_path.c_str()); |
---|
331 | } |
---|
332 | else |
---|
333 | { |
---|
334 | rc = unlink(_path.c_str()); |
---|
335 | } |
---|
336 | if (rc) handleLastErrorImpl(_path); |
---|
337 | } |
---|
338 | |
---|
339 | |
---|
340 | |
---|
341 | bool FileImpl::createFileImpl() |
---|
342 | { |
---|
343 | poco_assert (!_path.empty()); |
---|
344 | |
---|
345 | int n = open(_path.c_str(), O_WRONLY | O_CREAT | O_EXCL); |
---|
346 | if (n != -1) |
---|
347 | { |
---|
348 | close(n); |
---|
349 | return true; |
---|
350 | } |
---|
351 | if (n == -1 && errno == EEXIST) |
---|
352 | return false; |
---|
353 | else |
---|
354 | handleLastErrorImpl(_path); |
---|
355 | return false; |
---|
356 | } |
---|
357 | |
---|
358 | |
---|
359 | bool FileImpl::createDirectoryImpl() |
---|
360 | { |
---|
361 | poco_assert (!_path.empty()); |
---|
362 | |
---|
363 | if (existsImpl() && isDirectoryImpl()) |
---|
364 | return false; |
---|
365 | Path p(_path); |
---|
366 | p.makeDirectory(); |
---|
367 | if (mkdir(p.toString().c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) |
---|
368 | handleLastErrorImpl(_path); |
---|
369 | return true; |
---|
370 | } |
---|
371 | |
---|
372 | |
---|
373 | void FileImpl::handleLastErrorImpl(const std::string& path) |
---|
374 | { |
---|
375 | switch (errno) |
---|
376 | { |
---|
377 | case EIO: |
---|
378 | throw IOException(path); |
---|
379 | case EPERM: |
---|
380 | throw FileAccessDeniedException("insufficient permissions", path); |
---|
381 | case EACCES: |
---|
382 | throw FileAccessDeniedException(path); |
---|
383 | case ENOENT: |
---|
384 | throw FileNotFoundException(path); |
---|
385 | case ENOTDIR: |
---|
386 | throw OpenFileException("not a directory", path); |
---|
387 | case EISDIR: |
---|
388 | throw OpenFileException("not a file", path); |
---|
389 | case EROFS: |
---|
390 | throw FileReadOnlyException(path); |
---|
391 | case EEXIST: |
---|
392 | throw FileExistsException(path); |
---|
393 | case ENOSPC: |
---|
394 | throw FileException("no space left on device", path); |
---|
395 | case EDQUOT: |
---|
396 | throw FileException("disk quota exceeded", path); |
---|
397 | case ENOTEMPTY: |
---|
398 | throw FileException("directory not empty", path); |
---|
399 | case ENAMETOOLONG: |
---|
400 | throw PathSyntaxException(path); |
---|
401 | default: |
---|
402 | throw FileException(strerror(errno), path); |
---|
403 | } |
---|
404 | } |
---|
405 | |
---|
406 | |
---|
407 | } // namespace Poco |
---|