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

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

Made the project to compile on my Intel Mac running Leopard.

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