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