source: trunk/yat/utility/FileUtil.cc

Last change on this file was 3670, checked in by Peter, 4 years ago

update copyright years

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