1 | #ifndef _theplu_yat_utility_fileutil_ |
---|
2 | #define _theplu_yat_utility_fileutil_ |
---|
3 | |
---|
4 | // $Id: FileUtil.h 1486 2008-09-09 21:17:19Z jari $ |
---|
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 Jari Häkkinen, Peter Johansson |
---|
11 | Copyright (C) 2008 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 this program; if not, write to the Free Software |
---|
27 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
28 | 02111-1307, USA. |
---|
29 | */ |
---|
30 | |
---|
31 | #include "Exception.h" |
---|
32 | |
---|
33 | #include <string> |
---|
34 | #include <sys/stat.h> |
---|
35 | #include <unistd.h> |
---|
36 | |
---|
37 | namespace theplu { |
---|
38 | namespace yat { |
---|
39 | namespace utility { |
---|
40 | |
---|
41 | /// |
---|
42 | /// |
---|
43 | /// @brief Checking file/directory existence and access permissions. |
---|
44 | /// |
---|
45 | /// FileUtil is a wrapper to access(2) and stat(2). |
---|
46 | /// |
---|
47 | class FileUtil { |
---|
48 | public: |
---|
49 | |
---|
50 | /// |
---|
51 | /// \brief Constructor, copies \a path only |
---|
52 | /// |
---|
53 | explicit FileUtil(const std::string& path); |
---|
54 | |
---|
55 | /// |
---|
56 | /// \brief Check file/directory permissions |
---|
57 | /// |
---|
58 | /// Check if access permissions match \a mode. \a mode must be |
---|
59 | /// given as r, w, x, d, or combinations of these letters. The check |
---|
60 | /// is performed at call time. |
---|
61 | /// |
---|
62 | /// \note Checking permissions on a non-existent file will match |
---|
63 | /// the permissions against the parent directory, but only the |
---|
64 | /// parent directory. This means that when the parent directory |
---|
65 | /// does not exist then fail is returned. |
---|
66 | /// |
---|
67 | /// \return On success (all requested permissions granted), zero |
---|
68 | /// is returned. On error (at least one bit in mode asked for a |
---|
69 | /// permission that is denied, or some other error occurred), -1 |
---|
70 | /// is returned, and errno is set appropriately. |
---|
71 | /// |
---|
72 | /// \exception IO_error if exists() throws. |
---|
73 | /// \exception std::invalid_argument if \a bits contain other |
---|
74 | /// character than r, w, x, or d. |
---|
75 | /// |
---|
76 | int permissions(const std::string& bits) const; |
---|
77 | |
---|
78 | /// |
---|
79 | /// \brief Check whether \a file exists. |
---|
80 | /// |
---|
81 | /// The check for the file is done when calling this function. |
---|
82 | /// |
---|
83 | /// \return True if \a file exists, false otherwise. |
---|
84 | /// |
---|
85 | /// \exception IO_error if underlying stat(2) call fails in other |
---|
86 | /// ways than that file does not exist. |
---|
87 | /// |
---|
88 | /// \see stat(2) |
---|
89 | /// |
---|
90 | bool exists(void) const; |
---|
91 | |
---|
92 | /// |
---|
93 | /// \brief Get path associated with the object. |
---|
94 | /// |
---|
95 | const std::string& path(void) const; |
---|
96 | |
---|
97 | private: |
---|
98 | /// |
---|
99 | /// \brief The copy constructor, not implemented |
---|
100 | /// |
---|
101 | FileUtil(const FileUtil&); |
---|
102 | |
---|
103 | std::string path_; |
---|
104 | }; |
---|
105 | |
---|
106 | }}} // of namespace utility, yat, and theplu |
---|
107 | |
---|
108 | #endif |
---|