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

Last change on this file since 1207 was 1207, checked in by Peter, 14 years ago

fixes #282

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