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

Last change on this file since 1487 was 1487, checked in by Jari Häkkinen, 13 years ago

Addresses #436. GPL license copy reference should also be updated.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.9 KB
Line 
1#ifndef _theplu_yat_utility_fileutil_
2#define _theplu_yat_utility_fileutil_
3
4// $Id: FileUtil.h 1487 2008-09-10 08:41:36Z 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 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    /// \brief Get path associated with the object.
92    ///
93    const std::string& path(void) const;
94
95  private:
96    ///
97    /// \brief The copy constructor, not implemented
98    ///
99    FileUtil(const FileUtil&);
100
101    std::string path_;
102  };
103
104}}} // of namespace utility, yat, and theplu
105
106#endif
Note: See TracBrowser for help on using the repository browser.