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

Last change on this file since 1446 was 1446, checked in by Peter, 15 years ago

refs #426 make it compile on Tiger - but removed the Large File Support

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.4 KB
Line 
1// $Id: FileUtil.cc 1446 2008-08-28 02:51:36Z 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#include "FileUtil.h"
28
29#include <cerrno>
30#include <cstddef>
31#include <iostream>
32#include <stdexcept>
33#include <sstream>
34#include <string>
35
36#include <sys/stat.h>
37#include <unistd.h>
38
39
40namespace theplu {
41namespace yat {
42namespace utility {
43
44
45  FileUtil::FileUtil(const std::string& path)
46    : path_(path)
47  {
48  }
49
50
51  int FileUtil::permissions(const std::string& bits) const
52  {
53    std::string tryme=path_;
54    if (!exists()) {
55      std::string::size_type pos=path_.find_last_of('/');
56      tryme = ( (pos!=std::string::npos) ? path_.substr(0,pos) : "." );
57    }
58
59    int mode=0;
60    bool ok = true;
61    for (size_t i=0; i<bits.length(); i++)
62      switch (bits[i]) {
63          case 'r':
64            mode|=R_OK;
65            break;
66          case 'w':
67            mode|=W_OK;
68            break;
69          case 'x':
70            mode|=X_OK;
71            break;
72          case 'd':
73            struct stat nodestat;
74            stat(tryme.c_str(),&nodestat);
75            if (!S_ISDIR(nodestat.st_mode))
76              ok = false;
77            break;
78          default:
79            throw std::invalid_argument("FileUtil::permission: "+bits);
80      }
81    if (!ok)
82      return -1;
83    return access(tryme.c_str(),mode);
84  }
85
86
87  bool FileUtil::exists(void) const
88  {
89    struct stat statt;
90    if ( stat(path_.c_str(),&statt) && (errno!=ENOENT) ) {
91      std::stringstream ss;
92      ss << "stat(2) call failed with errno: " << errno;
93      throw IO_error(ss.str());
94    }
95    if (errno) {
96      errno = 0; // don't leave errno in state of failure
97      return false;
98    }
99    return true;
100  }
101
102
103  const std::string& FileUtil::path(void) const
104  {
105    return path_;
106  }
107
108}}} // of namespace utility, yat, and theplu
Note: See TracBrowser for help on using the repository browser.