1 | #ifndef _theplu_yat_utility_fileutil_ |
---|
2 | #define _theplu_yat_utility_fileutil_ |
---|
3 | |
---|
4 | // $Id: FileUtil.h 2125 2009-12-22 20:19:41Z peter $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2004 Jari Häkkinen |
---|
8 | Copyright (C) 2005 Jari Häkkinen, Peter Johansson |
---|
9 | Copyright (C) 2006 Jari Häkkinen |
---|
10 | Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson |
---|
11 | Copyright (C) 2009 Peter Johansson |
---|
12 | |
---|
13 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
14 | |
---|
15 | The yat library is free software; you can redistribute it and/or |
---|
16 | modify it under the terms of the GNU General Public License as |
---|
17 | published by the Free Software Foundation; either version 3 of the |
---|
18 | License, or (at your option) any later version. |
---|
19 | |
---|
20 | The yat library is distributed in the hope that it will be useful, |
---|
21 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
22 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
23 | General Public License for more details. |
---|
24 | |
---|
25 | You should have received a copy of the GNU General Public License |
---|
26 | along with yat. If not, see <http://www.gnu.org/licenses/>. |
---|
27 | */ |
---|
28 | |
---|
29 | #include <string> |
---|
30 | #include <sys/stat.h> |
---|
31 | #include <unistd.h> |
---|
32 | |
---|
33 | namespace theplu { |
---|
34 | namespace yat { |
---|
35 | namespace utility { |
---|
36 | |
---|
37 | /// |
---|
38 | /// |
---|
39 | /// @brief Checking file/directory existence and access permissions. |
---|
40 | /// |
---|
41 | /// FileUtil is a wrapper to access(2) and stat(2). |
---|
42 | /// |
---|
43 | class FileUtil { |
---|
44 | public: |
---|
45 | |
---|
46 | /// |
---|
47 | /// \brief Constructor, copies \a path only |
---|
48 | /// |
---|
49 | explicit FileUtil(const std::string& path); |
---|
50 | |
---|
51 | /** |
---|
52 | \brief Copies the path |
---|
53 | |
---|
54 | \since New in yat 0.6 |
---|
55 | */ |
---|
56 | FileUtil(const FileUtil&); |
---|
57 | |
---|
58 | /// |
---|
59 | /// \brief Check file/directory permissions |
---|
60 | /// |
---|
61 | /// Check if access permissions match \a mode. \a mode must be |
---|
62 | /// given as r, w, x, d, or combinations of these letters. The check |
---|
63 | /// is performed at call time. |
---|
64 | /// |
---|
65 | /// \note Checking permissions on a non-existent file will match |
---|
66 | /// the permissions against the parent directory, but only the |
---|
67 | /// parent directory. This means that when the parent directory |
---|
68 | /// does not exist then fail is returned. |
---|
69 | /// |
---|
70 | /// \return On success (all requested permissions granted), zero |
---|
71 | /// is returned. On error (at least one bit in mode asked for a |
---|
72 | /// permission that is denied, or some other error occurred), -1 |
---|
73 | /// is returned, and errno is set appropriately. |
---|
74 | /// |
---|
75 | /// \exception IO_error if exists() throws. |
---|
76 | /// \exception std::invalid_argument if \a bits contain other |
---|
77 | /// character than r, w, x, or d. |
---|
78 | /// |
---|
79 | int permissions(const std::string& bits) const; |
---|
80 | |
---|
81 | /// |
---|
82 | /// \brief Check whether \a file exists. |
---|
83 | /// |
---|
84 | /// The check for the file is done when calling this function. |
---|
85 | /// |
---|
86 | /// \return True if \a file exists, false otherwise. |
---|
87 | /// |
---|
88 | /// \exception IO_error if underlying stat(2) call fails in other |
---|
89 | /// ways than that file does not exist. |
---|
90 | /// |
---|
91 | /// \see stat(2) |
---|
92 | /// |
---|
93 | bool exists(void) const; |
---|
94 | |
---|
95 | /** |
---|
96 | Same as exists(void) but uses \a lstat rather than \a |
---|
97 | stat. That implies that the function will return true also if |
---|
98 | \a path is a broken link. |
---|
99 | |
---|
100 | \return true if file exists |
---|
101 | |
---|
102 | \see lstat(2) |
---|
103 | |
---|
104 | \since New in yat 0.5 |
---|
105 | */ |
---|
106 | bool lexists(void) const; |
---|
107 | |
---|
108 | /// |
---|
109 | /// \brief Get path associated with the object. |
---|
110 | /// |
---|
111 | const std::string& path(void) const; |
---|
112 | |
---|
113 | /** |
---|
114 | \brief assignment operator |
---|
115 | |
---|
116 | \since New in yat 0.6 |
---|
117 | */ |
---|
118 | FileUtil& operator=(const FileUtil&); |
---|
119 | |
---|
120 | private: |
---|
121 | bool exists_common(bool) const; |
---|
122 | |
---|
123 | std::string path_; |
---|
124 | }; |
---|
125 | |
---|
126 | }}} // of namespace utility, yat, and theplu |
---|
127 | |
---|
128 | #endif |
---|