source: trunk/yat/utility/FileUtil.h @ 2119

Last change on this file since 2119 was 2119, checked in by Peter, 12 years ago

converted files to utf-8. fixes #577

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.2 KB
Line 
1#ifndef _theplu_yat_utility_fileutil_
2#define _theplu_yat_utility_fileutil_
3
4// $Id: FileUtil.h 2119 2009-12-12 23:11:43Z 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
12  This file is part of the yat library, http://dev.thep.lu.se/yat
13
14  The yat library is free software; you can redistribute it and/or
15  modify it under the terms of the GNU General Public License as
16  published by the Free Software Foundation; either version 3 of the
17  License, or (at your option) any later version.
18
19  The yat library is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  General Public License for more details.
23
24  You should have received a copy of the GNU General Public License
25  along with yat. If not, see <http://www.gnu.org/licenses/>.
26*/
27
28#include <string>
29#include <sys/stat.h>
30#include <unistd.h>
31
32namespace theplu {
33namespace yat {
34namespace utility {
35
36  ///
37  ///
38  /// @brief Checking file/directory existence and access permissions.
39  ///
40  /// FileUtil is a wrapper to access(2) and stat(2).
41  ///
42  class FileUtil {
43  public:
44
45    ///
46    /// \brief Constructor, copies \a path only
47    ///
48    explicit FileUtil(const std::string& path);
49
50    ///
51    /// \brief Check file/directory permissions
52    ///
53    /// Check if access permissions match \a mode. \a mode must be
54    /// given as r, w, x, d, or combinations of these letters. The check
55    /// is performed at call time.
56    ///
57    /// \note Checking permissions on a non-existent file will match
58    /// the permissions against the parent directory, but only the
59    /// parent directory. This means that when the parent directory
60    /// does not exist then fail is returned.
61    ///
62    /// \return On success (all requested permissions granted), zero
63    /// is returned. On error (at least one bit in mode asked for a
64    /// permission that is denied, or some other error occurred), -1
65    /// is returned, and errno is set appropriately.
66    ///
67    /// \exception IO_error if exists() throws.
68    /// \exception std::invalid_argument if \a bits contain other
69    /// character than r, w, x, or d.
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       Same as exists(void) but uses \a lstat rather than \a
89       stat. That implies that the function will return true also if
90       \a path is a broken link.
91
92       \return true if file exists
93
94       \see lstat(2)
95       
96       \since New in yat 0.5
97     */
98    bool lexists(void) const;
99
100    ///
101    /// \brief Get path associated with the object.
102    ///
103    const std::string& path(void) const;
104
105  private:
106    ///
107    /// \brief The copy constructor, not implemented
108    ///
109    FileUtil(const FileUtil&);
110    FileUtil& operator=(const FileUtil&);
111
112    bool exists_common(bool) const;
113
114    std::string path_;
115  };
116
117}}} // of namespace utility, yat, and theplu
118
119#endif
Note: See TracBrowser for help on using the repository browser.