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

Last change on this file since 2125 was 2125, checked in by Peter, 12 years ago

fixes #575 - allow copy of FileUtil?

  • 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 2125 2009-12-22 20:19:41Z 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 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      tryme = ( (pos!=std::string::npos) ? path_.substr(0,pos) : "." );
69    }
70
71    int mode=0;
72    bool ok = true;
73    for (size_t i=0; i<bits.length(); i++)
74      switch (bits[i]) {
75          case 'r':
76            mode|=R_OK;
77            break;
78          case 'w':
79            mode|=W_OK;
80            break;
81          case 'x':
82            mode|=X_OK;
83            break;
84          case 'd':
85            struct stat nodestat;
86            stat(tryme.c_str(),&nodestat);
87            if (!S_ISDIR(nodestat.st_mode))
88              ok = false;
89            break;
90          default:
91            throw std::invalid_argument("FileUtil::permission: "+bits);
92      }
93    if (!ok)
94      return -1;
95    return access(tryme.c_str(),mode);
96  }
97
98
99  bool FileUtil::exists(void) const
100  {
101    struct stat statt;
102    return exists_common(stat(path_.c_str(),&statt));
103  }
104
105
106  bool FileUtil::exists_common(bool failed) const
107  {
108    if ( failed && (errno!=ENOENT) ) {
109      std::stringstream ss;
110      ss << "stat(2) call failed with errno: " << errno << "\n"
111         << strerror(errno);
112      throw IO_error(ss.str());
113    }
114    return !failed;
115  }
116
117
118  bool FileUtil::lexists(void) const
119  {
120    struct stat statt;
121    return exists_common(lstat(path_.c_str(),&statt));
122  }
123
124
125  const std::string& FileUtil::path(void) const
126  {
127    return path_;
128  }
129
130
131  FileUtil& FileUtil::operator=(const FileUtil& rhs)
132  {
133    path_ = rhs.path_;
134    return *this;
135  }
136
137}}} // of namespace utility, yat, and theplu
Note: See TracBrowser for help on using the repository browser.