yat/utility/FileUtil.h

Code
Comments
Other
Rev Date Author Line
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 4 // $Id$
58 14 Apr 04 jari 5
570 05 Apr 06 jari 6 /*
2119 12 Dec 09 peter 7   Copyright (C) 2004 Jari Häkkinen
2119 12 Dec 09 peter 8   Copyright (C) 2005 Jari Häkkinen, Peter Johansson
2119 12 Dec 09 peter 9   Copyright (C) 2006 Jari Häkkinen
4359 23 Aug 23 peter 10   Copyright (C) 2007 Peter Johansson
4359 23 Aug 23 peter 11   Copyright (C) 2008 Jari Häkkinen, Peter Johansson
4359 23 Aug 23 peter 12   Copyright (C) 2009 Peter Johansson
570 05 Apr 06 jari 13
1437 25 Aug 08 peter 14   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 16   The yat library is free software; you can redistribute it and/or
675 10 Oct 06 jari 17   modify it under the terms of the GNU General Public License as
1486 09 Sep 08 jari 18   published by the Free Software Foundation; either version 3 of the
675 10 Oct 06 jari 19   License, or (at your option) any later version.
570 05 Apr 06 jari 20
675 10 Oct 06 jari 21   The yat library is distributed in the hope that it will be useful,
675 10 Oct 06 jari 22   but WITHOUT ANY WARRANTY; without even the implied warranty of
675 10 Oct 06 jari 23   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
570 05 Apr 06 jari 24   General Public License for more details.
570 05 Apr 06 jari 25
570 05 Apr 06 jari 26   You should have received a copy of the GNU General Public License
1487 10 Sep 08 jari 27   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 40   /// @brief Checking file/directory existence and access permissions.
767 22 Feb 07 peter 41   ///
767 22 Feb 07 peter 42   /// 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 48     /// \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 53        \brief Copies the path
2125 22 Dec 09 peter 54
2125 22 Dec 09 peter 55        \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 60     /// \brief Check file/directory permissions
709 20 Dec 06 jari 61     ///
58 14 Apr 04 jari 62     /// Check if access permissions match \a mode. \a mode must be
1207 05 Mar 08 peter 63     /// given as r, w, x, d, or combinations of these letters. The check
1207 05 Mar 08 peter 64     /// is performed at call time.
58 14 Apr 04 jari 65     ///
711 21 Dec 06 jari 66     /// \note Checking permissions on a non-existent file will match
711 21 Dec 06 jari 67     /// the permissions against the parent directory, but only the
711 21 Dec 06 jari 68     /// parent directory. This means that when the parent directory
711 21 Dec 06 jari 69     /// does not exist then fail is returned.
709 20 Dec 06 jari 70     ///
711 21 Dec 06 jari 71     /// \return On success (all requested permissions granted), zero
58 14 Apr 04 jari 72     /// is returned. On error (at least one bit in mode asked for a
58 14 Apr 04 jari 73     /// permission that is denied, or some other error occurred), -1
58 14 Apr 04 jari 74     /// is returned, and errno is set appropriately.
58 14 Apr 04 jari 75     ///
1207 05 Mar 08 peter 76     /// \exception IO_error if exists() throws.
1207 05 Mar 08 peter 77     /// \exception std::invalid_argument if \a bits contain other
1207 05 Mar 08 peter 78     /// 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 83     /// \brief Check whether \a file exists.
360 04 Aug 05 jari 84     ///
711 21 Dec 06 jari 85     /// The check for the file is done when calling this function.
360 04 Aug 05 jari 86     ///
711 21 Dec 06 jari 87     /// \return True if \a file exists, false otherwise.
711 21 Dec 06 jari 88     ///
711 21 Dec 06 jari 89     /// \exception IO_error if underlying stat(2) call fails in other
711 21 Dec 06 jari 90     /// ways than that file does not exist.
711 21 Dec 06 jari 91     ///
711 21 Dec 06 jari 92     /// \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 97        Same as exists(void) but uses \a lstat rather than \a
1627 17 Nov 08 peter 98        stat. That implies that the function will return true also if
1627 17 Nov 08 peter 99        \a path is a broken link.
1627 17 Nov 08 peter 100
1627 17 Nov 08 peter 101        \return true if file exists
1627 17 Nov 08 peter 102
1627 17 Nov 08 peter 103        \see lstat(2)
4200 19 Aug 22 peter 104
1627 17 Nov 08 peter 105        \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 110     /// \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 115        \brief assignment operator
2125 22 Dec 09 peter 116
2125 22 Dec 09 peter 117        \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