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

Last change on this file since 1706 was 1627, checked in by Peter, 13 years ago

adding a function FileUtil::lexists that uses lstat rather than stat.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.3 KB
Line 
1#ifndef _theplu_yat_utility_fileutil_
2#define _theplu_yat_utility_fileutil_
3
4// $Id: FileUtil.h 1627 2008-11-17 01:27:46Z 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 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 yat. If not, see <http://www.gnu.org/licenses/>.
27*/
28
29#include "Exception.h"
30
31#include <string>
32#include <sys/stat.h>
33#include <unistd.h>
34
35namespace theplu {
36namespace yat {
37namespace utility {
38
39  ///
40  ///
41  /// @brief Checking file/directory existence and access permissions.
42  ///
43  /// FileUtil is a wrapper to access(2) and stat(2).
44  ///
45  class FileUtil {
46  public:
47
48    ///
49    /// \brief Constructor, copies \a path only
50    ///
51    explicit FileUtil(const std::string& path);
52
53    ///
54    /// \brief Check file/directory permissions
55    ///
56    /// Check if access permissions match \a mode. \a mode must be
57    /// given as r, w, x, d, or combinations of these letters. The check
58    /// is performed at call time.
59    ///
60    /// \note Checking permissions on a non-existent file will match
61    /// the permissions against the parent directory, but only the
62    /// parent directory. This means that when the parent directory
63    /// does not exist then fail is returned.
64    ///
65    /// \return On success (all requested permissions granted), zero
66    /// is returned. On error (at least one bit in mode asked for a
67    /// permission that is denied, or some other error occurred), -1
68    /// is returned, and errno is set appropriately.
69    ///
70    /// \exception IO_error if exists() throws.
71    /// \exception std::invalid_argument if \a bits contain other
72    /// character than r, w, x, or d.
73    ///
74    int permissions(const std::string& bits) const;
75
76    ///
77    /// \brief Check whether \a file exists.
78    ///
79    /// The check for the file is done when calling this function.
80    ///
81    /// \return True if \a file exists, false otherwise.
82    ///
83    /// \exception IO_error if underlying stat(2) call fails in other
84    /// ways than that file does not exist.
85    ///
86    /// \see stat(2)
87    ///
88    bool exists(void) const;
89
90    /**
91       Same as exists(void) but uses \a lstat rather than \a
92       stat. That implies that the function will return true also if
93       \a path is a broken link.
94
95       \return true if file exists
96
97       \see lstat(2)
98       
99       \since New in yat 0.5
100     */
101    bool lexists(void) const;
102
103    ///
104    /// \brief Get path associated with the object.
105    ///
106    const std::string& path(void) const;
107
108  private:
109    ///
110    /// \brief The copy constructor, not implemented
111    ///
112    FileUtil(const FileUtil&);
113    FileUtil& operator=(const FileUtil&);
114
115    bool exists_common(bool) const;
116
117    std::string path_;
118  };
119
120}}} // of namespace utility, yat, and theplu
121
122#endif
Note: See TracBrowser for help on using the repository browser.