source: trunk/yat/utility/FileUtil.cc @ 2217

Last change on this file since 2217 was 2217, checked in by Peter, 13 years ago

merged release 0.6.1

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