-
Notifications
You must be signed in to change notification settings - Fork 4
/
fileutils.cpp
178 lines (154 loc) · 4.44 KB
/
fileutils.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
////////////////////////////////////////////////////////////////////////
// FILE: fileutils.cpp
// AUTHOR: Johannes Winkelmann, [email protected]
// COPYRIGHT: (c) 2002-2005 by Johannes Winkelmann
// ---------------------------------------------------------------------
// This program 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 2 of the License, or
// (at your option) any later version.
////////////////////////////////////////////////////////////////////////
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include "md5.h"
#include "httpup.h"
#include "fileutils.h"
using namespace std;
int FileUtils::deltree(const char* directory)
{
int ret = 0;
struct stat info;
if (stat(directory, &info)) {
// already removed
return 0;
}
if (!S_ISDIR(info.st_mode)) {
return unlink(directory);
}
DIR* dir = opendir(directory);
struct dirent* entry;
while ((entry = readdir(dir)) != 0) {
if (entry->d_name[0] == '.' &&
(entry->d_name[1] == '.' || entry->d_name[1] == '\0')) {
continue;
}
struct stat info;
string pathName = string(directory) + "/" + string(entry->d_name);
if (stat(pathName.c_str(), &info) != 0) {
cout << entry->d_name << endl;
return -1;
}
if (S_ISDIR(info.st_mode)) {
if (deltree(pathName.c_str())) {
ret = -1;
}
rmdir(pathName.c_str());
} else {
if (unlink(pathName.c_str())) {
ret = -1;
}
}
}
closedir(dir);
if (rmdir(directory)) {
ret = -1;
}
return ret;
}
int FileUtils::mktree(const string& directory)
{
int ret = 0;
size_t pos = 0;
string fName;
while ((pos = directory.find( '/', pos+1)) != string::npos ) {
fName = directory.substr(0, pos);
struct stat info;
if (stat(fName.c_str(), &info)) {
if (mkdir(fName.c_str(), 0755)) {
ret = -1;
}
}
}
return ret;
}
bool FileUtils::fmd5sum(const string& fileName, unsigned char* result)
{
struct md5_context ctx;
unsigned char buffer[1000];
FILE* f = fopen(fileName.c_str(), "r");
if (!f) {
return false;
}
md5_starts( &ctx );
int i = 0;
while( ( i = fread( buffer, 1, sizeof( buffer ), f ) ) > 0 ) {
md5_update( &ctx, buffer, i );
}
fclose(f);
md5_finish( &ctx, result );
return true;
}
void FileUtils::listFiles(const string& target)
{
list<string> files;
string newTarget = target;
if (newTarget != ".") {
if (newTarget[newTarget.length()-1] != '/') {
newTarget += "/";
}
}
string repoFile = newTarget + "/" + HttpUp::REPOCURRENTFILE;
FILE* fp = fopen(repoFile.c_str(), "r");
if (fp) {
char line[512];
while (fgets(line, 512, fp)) {
line[strlen(line)-1] = '\0';
files.push_back(line);
}
fclose(fp);
listFilesRec(newTarget, "", files);
} else {
cerr << "Failed to open " << repoFile << endl;
}
}
void FileUtils::listFilesRec(const string& base,
const string& offset,
list<string>& files)
{
string newOff = offset;
if (newOff.length() > 0) {
newOff += "/";
}
DIR* dir = opendir((base + newOff).c_str());
if (dir) {
struct dirent* d;
string name;
while ((d = readdir(dir))) {
name = d->d_name;
if (name == HttpUp::REPOCURRENTFILE ||
name == HttpUp::URLINFO ||
name == "." || name == "..") {
continue;
}
if (find(files.begin(), files.end(),
newOff + d->d_name) == files.end()) {
cout << "? ";
} else {
cout << "= ";
}
cout << newOff
<< d->d_name << endl;
struct stat buf;
if (stat(((base + newOff) + d->d_name).c_str(), &buf) == 0 &&
S_ISDIR(buf.st_mode)) {
listFilesRec(base, newOff + d->d_name, files);
}
}
closedir(dir);
}
}