-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFilesystem.cpp
91 lines (69 loc) · 2.41 KB
/
Filesystem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
Copyright (C) 2004,2005,2006,2007,2008,2009,2010,2011,2012,2013 Cyrus Shaoul and Geoff Hollis
This file is part of HiDEx.
HiDEx is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
HiDEx is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with HiDEx in the COPYING.txt file.
If not, see <http://www.gnu.org/licenses/>.
*/
#define _LARGE_FILES
/*
This is a collection of functions that handle filesystem operations needed by
other parts of HiDEx.
*/
#include "sys/stat.h"
#include "Filesystem.h"
#include "string.h"
using namespace std;
bool file_exists(const string& fname)
{
struct stat sbuf;
// exists && is a regular file
return stat(fname, &sbuf) == 0 && S_ISREG(sbuf.st_mode);
}
bool dir_exists(const string& fname)
{
struct stat sbuf;
// exists && is a dir
return stat(fname, &sbuf) == 0 && S_ISDIR(sbuf.st_mode);
}
int rmdir(const string& filename)
{
// Check filename exists and is actually a directory
struct stat sb;
if (stat(filename, &sb) != 0 || !S_ISDIR(sb.st_mode)) {
cerr << "Could not delete " << filename << ". It does not exist or is not a directory" << endl;
return -1;
}
string safefile = filename;
string::size_type p = 0;
while (p < safefile.size()) {
// Don't escape a few safe characters which are common in filenames
if (!isalnum(safefile[p]) && strchr("/._-", safefile[p]) == NULL) {
safefile.insert(p, "\\");
++p;
}
++p;
}
system("rm -rf " + safefile);
return 0;
}
int mkdir(const string& filename, mode_t mode) {
return mkdir(filename.c_str(), mode);
}
int stat(const string& filename, struct stat *buf) {
return stat(filename.c_str(), buf);
}
void touch(const string& filename) {
int fd = open(filename.c_str(), O_CREAT|O_WRONLY, 0644);
if (fd >= 0) close(fd);
}
int unlink(const string& filename) { return unlink(filename.c_str()); }
int system(const string& command) { return system(command.c_str()); }