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

Last change on this file since 1207 was 1207, checked in by Peter, 14 years ago

fixes #282

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.3 KB
Line 
1// $Id: FileUtil.cc 1207 2008-03-05 23:56:42Z peter $
2
3/*
4  Copyright (C) 2004 Jari Häkkinen
5  Copyright (C) 2005 Peter Johansson
6  Copyright (C) 2006 Jari Häkkinen
7
8  This file is part of the yat library, http://trac.thep.lu.se/yat
9
10  The yat library is free software; you can redistribute it and/or
11  modify it under the terms of the GNU General Public License as
12  published by the Free Software Foundation; either version 2 of the
13  License, or (at your option) any later version.
14
15  The yat library is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  General Public License for more details.
19
20  You should have received a copy of the GNU General Public License
21  along with this program; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23  02111-1307, USA.
24*/
25
26#include "FileUtil.h"
27
28#include <cerrno>
29#include <iostream>
30#include <stdexcept>
31#include <string>
32
33#include <sys/stat.h>
34#include <unistd.h>
35
36
37namespace theplu {
38namespace yat {
39namespace utility {
40
41
42  FileUtil::FileUtil(const std::string& path)
43    : path_(path)
44  {
45  }
46
47
48  int FileUtil::permissions(const std::string& bits) const
49  {
50    std::string tryme=path_;
51    if (!exists()) {
52      std::string::size_type pos=path_.find_last_of('/');
53      tryme = ( (pos!=std::string::npos) ? path_.substr(0,pos) : "." );
54    }
55
56    int mode=0;
57    bool ok = true;
58    for (u_int i=0; i<bits.length(); i++)
59      switch (bits[i]) {
60          case 'r':
61            mode|=R_OK;
62            break;
63          case 'w':
64            mode|=W_OK;
65            break;
66          case 'x':
67            mode|=X_OK;
68            break;
69          case 'd':
70            struct stat nodestat;
71            stat(tryme.c_str(),&nodestat);
72            if (!S_ISDIR(nodestat.st_mode))
73              ok = false;
74            break;
75          default:
76            throw std::invalid_argument("FileUtil::permission: "+bits);
77      }
78    if (!ok)
79      return -1;
80    return access(tryme.c_str(),mode);
81  }
82
83
84  bool FileUtil::exists(void) const
85  {
86    struct stat statt;
87    if ( stat(path_.c_str(),&statt) && (errno!=ENOENT) )
88      throw IO_error(std::string("stat(2) call failed with errno: "+errno));
89    if (errno) {
90      errno = 0; // don't leave errno in state of failure
91      return false;
92    }
93    return true;
94  }
95
96
97  const std::string& FileUtil::path(void) const
98  {
99    return path_;
100  }
101
102}}} // of namespace utility, yat, and theplu
Note: See TracBrowser for help on using the repository browser.