1 | #ifndef _theplu_yat_utility_fileutil_ |
---|
2 | #define _theplu_yat_utility_fileutil_ |
---|
3 | |
---|
4 | // $Id: FileUtil.h 711 2006-12-21 08:10:30Z jari $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2004 Jari Häkkinen |
---|
8 | Copyright (C) 2005 Peter Johansson |
---|
9 | Copyright (C) 2006 Jari Häkkinen |
---|
10 | |
---|
11 | This file is part of the yat library, http://lev.thep.lu.se/trac/yat |
---|
12 | |
---|
13 | The yat library is free software; you can redistribute it and/or |
---|
14 | modify it under the terms of the GNU General Public License as |
---|
15 | published by the Free Software Foundation; either version 2 of the |
---|
16 | License, or (at your option) any later version. |
---|
17 | |
---|
18 | The yat library is distributed in the hope that it will be useful, |
---|
19 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
21 | General Public License for more details. |
---|
22 | |
---|
23 | You should have received a copy of the GNU General Public License |
---|
24 | along with this program; if not, write to the Free Software |
---|
25 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
26 | 02111-1307, USA. |
---|
27 | */ |
---|
28 | |
---|
29 | #include "Exception.h" |
---|
30 | |
---|
31 | #include <string> |
---|
32 | #include <sys/stat.h> |
---|
33 | #include <unistd.h> |
---|
34 | |
---|
35 | namespace theplu { |
---|
36 | namespace yat { |
---|
37 | namespace utility { |
---|
38 | |
---|
39 | /// |
---|
40 | /// FileUtil is a utility class for checking file/directory |
---|
41 | /// existence and access permissions. FileUtil is a wrapper to |
---|
42 | /// access(2) and stat(2). |
---|
43 | /// |
---|
44 | class FileUtil { |
---|
45 | public: |
---|
46 | |
---|
47 | /// |
---|
48 | /// \brief Constructor, copies \a path only |
---|
49 | /// |
---|
50 | explicit FileUtil(const std::string& path); |
---|
51 | |
---|
52 | /// |
---|
53 | /// \brief Check file/directory permissions |
---|
54 | /// |
---|
55 | /// Check if access permissions match \a mode. \a mode must be |
---|
56 | /// given as r, w, x, or combinations of these letters. The check |
---|
57 | /// is performed att call time. |
---|
58 | /// |
---|
59 | /// \note Checking permissions on a non-existent file will match |
---|
60 | /// the permissions against the parent directory, but only the |
---|
61 | /// parent directory. This means that when the parent directory |
---|
62 | /// does not exist then fail is returned. |
---|
63 | /// |
---|
64 | /// \return On success (all requested permissions granted), zero |
---|
65 | /// is returned. On error (at least one bit in mode asked for a |
---|
66 | /// permission that is denied, or some other error occurred), -1 |
---|
67 | /// is returned, and errno is set appropriately. |
---|
68 | /// |
---|
69 | /// \exception Throws whatever exists() throws. |
---|
70 | /// |
---|
71 | int permissions(const std::string& bits) const; |
---|
72 | |
---|
73 | /// |
---|
74 | /// \brief Check whether \a file exists. |
---|
75 | /// |
---|
76 | /// The check for the file is done when calling this function. |
---|
77 | /// |
---|
78 | /// \return True if \a file exists, false otherwise. |
---|
79 | /// |
---|
80 | /// \exception IO_error if underlying stat(2) call fails in other |
---|
81 | /// ways than that file does not exist. |
---|
82 | /// |
---|
83 | /// \see stat(2) |
---|
84 | /// |
---|
85 | bool exists(void) const; |
---|
86 | |
---|
87 | /// |
---|
88 | /// \brief Get path associated with the object. |
---|
89 | /// |
---|
90 | const std::string& path(void) const; |
---|
91 | |
---|
92 | private: |
---|
93 | /// |
---|
94 | /// \brief The copy constructor, not implemented |
---|
95 | /// |
---|
96 | FileUtil(const FileUtil&); |
---|
97 | |
---|
98 | std::string path_; |
---|
99 | }; |
---|
100 | |
---|
101 | }}} // of namespace utility, yat, and theplu |
---|
102 | |
---|
103 | #endif |
---|