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

Last change on this file since 1486 was 1486, checked in by Jari Häkkinen, 13 years ago

Addresses #436.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.5 KB
RevLine 
[58]1// $Id: FileUtil.cc 1486 2008-09-09 21:17:19Z jari $
2
[570]3/*
[831]4  Copyright (C) 2004 Jari Häkkinen
[570]5  Copyright (C) 2005 Peter Johansson
[831]6  Copyright (C) 2006 Jari Häkkinen
[1260]7  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
[570]8
[1437]9  This file is part of the yat library, http://dev.thep.lu.se/yat
[570]10
[675]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
[1486]13  published by the Free Software Foundation; either version 3 of the
[675]14  License, or (at your option) any later version.
[570]15
[675]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
[570]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
[1455]27#ifdef HAVE_CONFIG_H
[1475]28#include <config.h>
[1455]29#endif
30
[710]31#include "FileUtil.h"
[295]32
[709]33#include <cerrno>
[1261]34#include <cstddef>
[58]35#include <iostream>
[1207]36#include <stdexcept>
[1437]37#include <sstream>
[709]38#include <string>
39
40#include <sys/stat.h>
[58]41#include <unistd.h>
42
43
44namespace theplu {
[680]45namespace yat {
[301]46namespace utility {
[58]47
[709]48
[710]49  FileUtil::FileUtil(const std::string& path)
[711]50    : path_(path)
[709]51  {
52  }
53
54
[711]55  int FileUtil::permissions(const std::string& bits) const
[709]56  {
57    std::string tryme=path_;
[711]58    if (!exists()) {
[709]59      std::string::size_type pos=path_.find_last_of('/');
60      tryme = ( (pos!=std::string::npos) ? path_.substr(0,pos) : "." );
[58]61    }
[709]62
[58]63    int mode=0;
[1207]64    bool ok = true;
[1261]65    for (size_t i=0; i<bits.length(); i++)
[58]66      switch (bits[i]) {
[1207]67          case 'r':
[58]68            mode|=R_OK;
69            break;
70          case 'w':
71            mode|=W_OK;
72            break;
73          case 'x':
74            mode|=X_OK;
75            break;
[1207]76          case 'd':
[1446]77            struct stat nodestat;
78            stat(tryme.c_str(),&nodestat);
[1207]79            if (!S_ISDIR(nodestat.st_mode))
80              ok = false;
81            break;
82          default:
83            throw std::invalid_argument("FileUtil::permission: "+bits);
[58]84      }
[1207]85    if (!ok)
86      return -1;
[709]87    return access(tryme.c_str(),mode);
[58]88  }
89
[709]90
[711]91  bool FileUtil::exists(void) const
[709]92  {
[1446]93    struct stat statt;
94    if ( stat(path_.c_str(),&statt) && (errno!=ENOENT) ) {
[1437]95      std::stringstream ss;
[1446]96      ss << "stat(2) call failed with errno: " << errno;
[1437]97      throw IO_error(ss.str());
98    }
[1001]99    if (errno) {
100      errno = 0; // don't leave errno in state of failure
101      return false;
102    }
103    return true;
[709]104  }
105
106
[710]107  const std::string& FileUtil::path(void) const
[709]108  {
109    return path_;
110  }
111
[687]112}}} // of namespace utility, yat, and theplu
Note: See TracBrowser for help on using the repository browser.