source: trunk/yat/FileUtil.cc @ 1512

Last change on this file since 1512 was 1512, checked in by Peter Johansson, 7 years ago

fetch latest yat sources

  • Property svn:eol-style set to native
File size: 2.9 KB
Line 
1// $Id: FileUtil.cc 2837 2012-09-16 23:07:47Z 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 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
32#include <cerrno>
33#include <cstddef>
34#include <cstring>
35#include <iostream>
36#include <stdexcept>
37#include <sstream>
38#include <string>
39
40#include <sys/stat.h>
41#include <unistd.h>
42
43
44namespace theplu {
45namespace yat {
46namespace utility {
47
48
49  FileUtil::FileUtil(const std::string& path)
50    : path_(path)
51  {
52  }
53
54
55  FileUtil::FileUtil(const FileUtil& other)
56    : path_(other.path_)
57  {
58  }
59
60
61  int FileUtil::permissions(const std::string& bits) const
62  {
63    std::string tryme=path_;
64    if (!exists()) {
65      std::string::size_type pos=path_.find_last_of('/');
66      if (pos == std::string::npos)
67        tryme = ".";
68      else if (pos == 0)
69        tryme = "/";
70      else
71        tryme.resize(pos);
72    }
73
74    int mode=0;
75    bool ok = true;
76    for (size_t i=0; i<bits.length(); i++)
77      switch (bits[i]) {
78          case 'r':
79            mode|=R_OK;
80            break;
81          case 'w':
82            mode|=W_OK;
83            break;
84          case 'x':
85            mode|=X_OK;
86            break;
87          case 'd':
88            struct stat nodestat;
89            stat(tryme.c_str(),&nodestat);
90            if (!S_ISDIR(nodestat.st_mode))
91              ok = false;
92            break;
93          default:
94            throw std::invalid_argument("FileUtil::permission: "+bits);
95      }
96    if (!ok)
97      return -1;
98    return access(tryme.c_str(),mode);
99  }
100
101
102  bool FileUtil::exists(void) const
103  {
104    struct stat statt;
105    return exists_common(stat(path_.c_str(),&statt));
106  }
107
108
109  bool FileUtil::exists_common(bool failed) const
110  {
111    if ( failed && (errno!=ENOENT) ) {
112      std::stringstream ss;
113      ss << "stat(2) call failed with errno: " << errno << "\n"
114         << strerror(errno);
115      throw IO_error(ss.str());
116    }
117    return !failed;
118  }
119
120
121  bool FileUtil::lexists(void) const
122  {
123    struct stat statt;
124    return exists_common(lstat(path_.c_str(),&statt));
125  }
126
127
128  const std::string& FileUtil::path(void) const
129  {
130    return path_;
131  }
132
133
134  FileUtil& FileUtil::operator=(const FileUtil& rhs)
135  {
136    path_ = rhs.path_;
137    return *this;
138  }
139
140}}} // of namespace utility, yat, and theplu
Note: See TracBrowser for help on using the repository browser.