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

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

updating copyrights to uniform encoding in them.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.6 KB
Line 
1// $Id: FileUtil.cc 2121 2009-12-13 16:17:07Z 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  int FileUtil::permissions(const std::string& bits) const
58  {
59    std::string tryme=path_;
60    if (!exists()) {
61      std::string::size_type pos=path_.find_last_of('/');
62      tryme = ( (pos!=std::string::npos) ? path_.substr(0,pos) : "." );
63    }
64
65    int mode=0;
66    bool ok = true;
67    for (size_t i=0; i<bits.length(); i++)
68      switch (bits[i]) {
69          case 'r':
70            mode|=R_OK;
71            break;
72          case 'w':
73            mode|=W_OK;
74            break;
75          case 'x':
76            mode|=X_OK;
77            break;
78          case 'd':
79            struct stat nodestat;
80            stat(tryme.c_str(),&nodestat);
81            if (!S_ISDIR(nodestat.st_mode))
82              ok = false;
83            break;
84          default:
85            throw std::invalid_argument("FileUtil::permission: "+bits);
86      }
87    if (!ok)
88      return -1;
89    return access(tryme.c_str(),mode);
90  }
91
92
93  bool FileUtil::exists(void) const
94  {
95    struct stat statt;
96    return exists_common(stat(path_.c_str(),&statt));
97  }
98
99
100  bool FileUtil::exists_common(bool failed) const
101  {
102    if ( failed && (errno!=ENOENT) ) {
103      std::stringstream ss;
104      ss << "stat(2) call failed with errno: " << errno << "\n"
105         << strerror(errno);
106      throw IO_error(ss.str());
107    }
108    return !failed;
109  }
110
111
112  bool FileUtil::lexists(void) const
113  {
114    struct stat statt;
115    return exists_common(lstat(path_.c_str(),&statt));
116  }
117
118
119  const std::string& FileUtil::path(void) const
120  {
121    return path_;
122  }
123
124}}} // of namespace utility, yat, and theplu
Note: See TracBrowser for help on using the repository browser.