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

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

do not rely on errno=0 on success

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