2 // Copyright (C) 2007, 2008 Francesco Salvestrini
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this program; if not, write to the Free Software Foundation, Inc.,
16 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <sys/types.h>
29 #include "libs/misc/debug.h"
30 #include "libs/misc/exception.h"
31 #include "libs/fs/directory.h"
34 Directory::Directory(const std::string
& name
)
37 throw Exception("Missing directory name");
40 // Remove trailing '/'
41 std::string::size_type p
;
44 //TR_DBG("name = '%s', p = %d, size = %d\n",
45 // name.c_str(), p, name.size());
47 if (p
== (name
.size() - 1)) {
48 name_
= name
.substr(0, p
);
55 Directory::~Directory(void)
59 const std::string
& Directory::name(void) const
64 void Directory::create(void) const
66 // XXX FIXME: Consider using the gnulib replacement
67 if (::mkdir(name_
.c_str(), 0700) != 0) {
68 throw Exception("Cannot create "
72 std::string(strerror(errno
)) +
77 void Directory::remove(void) const
79 // XXX FIXME: Consider using the gnulib replacement
80 if (::rmdir(name_
.c_str()) != 0) {
81 throw Exception("Cannot remove "
85 std::string(strerror(errno
)) +
90 bool Directory::exists(void) const
92 // XXX FIXME: Consider using the gnulib replacement
93 DIR * tmp
= opendir(name_
.c_str());
95 BUG_ON(errno
== EBADF
);
97 if (errno
== ENOENT
) {
101 throw Exception("Cannot open "
105 std::string(strerror(errno
)) +
109 // XXX FIXME: Maybe a check on closedir() return value ...