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

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

it's just about style

  • 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 1475 2008-09-04 13:06:52Z 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 2 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 this program; if not, write to the Free Software
23  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24  02111-1307, USA.
25*/
26
27#ifdef HAVE_CONFIG_H
28#include <config.h>
29#endif
30
31#include "FileUtil.h"
32
33#include <cerrno>
34#include <cstddef>
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  int FileUtil::permissions(const std::string& bits) const
56  {
57    std::string tryme=path_;
58    if (!exists()) {
59      std::string::size_type pos=path_.find_last_of('/');
60      tryme = ( (pos!=std::string::npos) ? path_.substr(0,pos) : "." );
61    }
62
63    int mode=0;
64    bool ok = true;
65    for (size_t i=0; i<bits.length(); i++)
66      switch (bits[i]) {
67          case 'r':
68            mode|=R_OK;
69            break;
70          case 'w':
71            mode|=W_OK;
72            break;
73          case 'x':
74            mode|=X_OK;
75            break;
76          case 'd':
77            struct stat nodestat;
78            stat(tryme.c_str(),&nodestat);
79            if (!S_ISDIR(nodestat.st_mode))
80              ok = false;
81            break;
82          default:
83            throw std::invalid_argument("FileUtil::permission: "+bits);
84      }
85    if (!ok)
86      return -1;
87    return access(tryme.c_str(),mode);
88  }
89
90
91  bool FileUtil::exists(void) const
92  {
93    struct stat statt;
94    if ( stat(path_.c_str(),&statt) && (errno!=ENOENT) ) {
95      std::stringstream ss;
96      ss << "stat(2) call failed with errno: " << errno;
97      throw IO_error(ss.str());
98    }
99    if (errno) {
100      errno = 0; // don't leave errno in state of failure
101      return false;
102    }
103    return true;
104  }
105
106
107  const std::string& FileUtil::path(void) const
108  {
109    return path_;
110  }
111
112}}} // of namespace utility, yat, and theplu
Note: See TracBrowser for help on using the repository browser.