yat/utility/FileUtil.cc

Code
Comments
Other
Rev Date Author Line
58 14 Apr 04 jari 1 // $Id$
58 14 Apr 04 jari 2
570 05 Apr 06 jari 3 /*
2119 12 Dec 09 peter 4   Copyright (C) 2004 Jari Häkkinen
570 05 Apr 06 jari 5   Copyright (C) 2005 Peter Johansson
2119 12 Dec 09 peter 6   Copyright (C) 2006 Jari Häkkinen
4359 23 Aug 23 peter 7   Copyright (C) 2007 Peter Johansson
4359 23 Aug 23 peter 8   Copyright (C) 2008 Jari Häkkinen, Peter Johansson
3670 27 Jul 17 peter 9   Copyright (C) 2009, 2010, 2017 Peter Johansson
570 05 Apr 06 jari 10
1437 25 Aug 08 peter 11   This file is part of the yat library, http://dev.thep.lu.se/yat
570 05 Apr 06 jari 12
675 10 Oct 06 jari 13   The yat library is free software; you can redistribute it and/or
675 10 Oct 06 jari 14   modify it under the terms of the GNU General Public License as
1486 09 Sep 08 jari 15   published by the Free Software Foundation; either version 3 of the
675 10 Oct 06 jari 16   License, or (at your option) any later version.
570 05 Apr 06 jari 17
675 10 Oct 06 jari 18   The yat library is distributed in the hope that it will be useful,
675 10 Oct 06 jari 19   but WITHOUT ANY WARRANTY; without even the implied warranty of
675 10 Oct 06 jari 20   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
570 05 Apr 06 jari 21   General Public License for more details.
570 05 Apr 06 jari 22
570 05 Apr 06 jari 23   You should have received a copy of the GNU General Public License
1487 10 Sep 08 jari 24   along with yat. If not, see <http://www.gnu.org/licenses/>.
570 05 Apr 06 jari 25 */
570 05 Apr 06 jari 26
1475 04 Sep 08 peter 27 #include <config.h>
1455 29 Aug 08 peter 28
710 20 Dec 06 jari 29 #include "FileUtil.h"
295 29 Apr 05 peter 30
2013 09 Jul 09 peter 31 #include "Exception.h"
3668 27 Jul 17 peter 32 #include "utility.h"
2013 09 Jul 09 peter 33
709 20 Dec 06 jari 34 #include <cerrno>
1261 08 Apr 08 peter 35 #include <cstddef>
2055 08 Sep 09 peter 36 #include <cstring>
58 14 Apr 04 jari 37 #include <iostream>
1207 05 Mar 08 peter 38 #include <stdexcept>
1437 25 Aug 08 peter 39 #include <sstream>
709 20 Dec 06 jari 40 #include <string>
709 20 Dec 06 jari 41
709 20 Dec 06 jari 42 #include <sys/stat.h>
58 14 Apr 04 jari 43 #include <unistd.h>
58 14 Apr 04 jari 44
58 14 Apr 04 jari 45
58 14 Apr 04 jari 46 namespace theplu {
680 11 Oct 06 jari 47 namespace yat {
301 30 Apr 05 peter 48 namespace utility {
58 14 Apr 04 jari 49
709 20 Dec 06 jari 50
710 20 Dec 06 jari 51   FileUtil::FileUtil(const std::string& path)
711 21 Dec 06 jari 52     : path_(path)
709 20 Dec 06 jari 53   {
709 20 Dec 06 jari 54   }
709 20 Dec 06 jari 55
709 20 Dec 06 jari 56
2125 22 Dec 09 peter 57   FileUtil::FileUtil(const FileUtil& other)
2125 22 Dec 09 peter 58     : path_(other.path_)
2125 22 Dec 09 peter 59   {
2125 22 Dec 09 peter 60   }
2125 22 Dec 09 peter 61
2125 22 Dec 09 peter 62
711 21 Dec 06 jari 63   int FileUtil::permissions(const std::string& bits) const
709 20 Dec 06 jari 64   {
709 20 Dec 06 jari 65     std::string tryme=path_;
711 21 Dec 06 jari 66     if (!exists()) {
3668 27 Jul 17 peter 67       tryme = dirname(path_);
58 14 Apr 04 jari 68     }
709 20 Dec 06 jari 69
58 14 Apr 04 jari 70     int mode=0;
1207 05 Mar 08 peter 71     bool ok = true;
1261 08 Apr 08 peter 72     for (size_t i=0; i<bits.length(); i++)
58 14 Apr 04 jari 73       switch (bits[i]) {
1207 05 Mar 08 peter 74           case 'r':
58 14 Apr 04 jari 75             mode|=R_OK;
58 14 Apr 04 jari 76             break;
58 14 Apr 04 jari 77           case 'w':
58 14 Apr 04 jari 78             mode|=W_OK;
58 14 Apr 04 jari 79             break;
58 14 Apr 04 jari 80           case 'x':
58 14 Apr 04 jari 81             mode|=X_OK;
58 14 Apr 04 jari 82             break;
1207 05 Mar 08 peter 83           case 'd':
1446 28 Aug 08 peter 84             struct stat nodestat;
1446 28 Aug 08 peter 85             stat(tryme.c_str(),&nodestat);
1207 05 Mar 08 peter 86             if (!S_ISDIR(nodestat.st_mode))
1207 05 Mar 08 peter 87               ok = false;
1207 05 Mar 08 peter 88             break;
1207 05 Mar 08 peter 89           default:
1207 05 Mar 08 peter 90             throw std::invalid_argument("FileUtil::permission: "+bits);
58 14 Apr 04 jari 91       }
1207 05 Mar 08 peter 92     if (!ok)
1207 05 Mar 08 peter 93       return -1;
709 20 Dec 06 jari 94     return access(tryme.c_str(),mode);
58 14 Apr 04 jari 95   }
58 14 Apr 04 jari 96
709 20 Dec 06 jari 97
711 21 Dec 06 jari 98   bool FileUtil::exists(void) const
709 20 Dec 06 jari 99   {
1446 28 Aug 08 peter 100     struct stat statt;
1627 17 Nov 08 peter 101     return exists_common(stat(path_.c_str(),&statt));
1627 17 Nov 08 peter 102   }
1627 17 Nov 08 peter 103
1627 17 Nov 08 peter 104
1627 17 Nov 08 peter 105   bool FileUtil::exists_common(bool failed) const
1627 17 Nov 08 peter 106   {
1627 17 Nov 08 peter 107     if ( failed && (errno!=ENOENT) ) {
1437 25 Aug 08 peter 108       std::stringstream ss;
1628 17 Nov 08 peter 109       ss << "stat(2) call failed with errno: " << errno << "\n"
1628 17 Nov 08 peter 110          << strerror(errno);
1437 25 Aug 08 peter 111        throw IO_error(ss.str());
1437 25 Aug 08 peter 112     }
1633 19 Nov 08 peter 113     return !failed;
709 20 Dec 06 jari 114   }
709 20 Dec 06 jari 115
709 20 Dec 06 jari 116
1627 17 Nov 08 peter 117   bool FileUtil::lexists(void) const
1627 17 Nov 08 peter 118   {
1627 17 Nov 08 peter 119     struct stat statt;
1627 17 Nov 08 peter 120     return exists_common(lstat(path_.c_str(),&statt));
1627 17 Nov 08 peter 121   }
1627 17 Nov 08 peter 122
1627 17 Nov 08 peter 123
710 20 Dec 06 jari 124   const std::string& FileUtil::path(void) const
709 20 Dec 06 jari 125   {
709 20 Dec 06 jari 126     return path_;
709 20 Dec 06 jari 127   }
709 20 Dec 06 jari 128
2125 22 Dec 09 peter 129
2125 22 Dec 09 peter 130   FileUtil& FileUtil::operator=(const FileUtil& rhs)
2125 22 Dec 09 peter 131   {
2125 22 Dec 09 peter 132     path_ = rhs.path_;
2125 22 Dec 09 peter 133     return *this;
2125 22 Dec 09 peter 134   }
2125 22 Dec 09 peter 135
687 16 Oct 06 jari 136 }}} // of namespace utility, yat, and theplu