-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
226 lines (176 loc) · 5.14 KB
/
main.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
*
* Copyright (c) 2015. Lukas Dresel, Julius Lohmann, Benedikt Lorch, Burkhard Ringlein, Andreas Rupp, Yuriy Sulima, Carola Touchy
*
* This file is part of GarbageCollector.
*
* GarbageCollector 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.
*
* GarbageCollector 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 GarbageCollector. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <cassert>
#include <iostream>
#include <ostream>
#include <stdlib.h>
#include <cstdlib>
#include "jbuffer.h"
#include "ThreadPool.h"
#include "SewagePlant.h"
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/syscall.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <string.h>
#include <errno.h>
#include <thread>
#include <vector>
#include <sstream>
#include <time.h>
#include <algorithm>
#include <limits.h>
#include "util.h"
typedef unsigned int uint;
using namespace std;
#define THREADFACTOR 2
#define BUFFERFACTOR 3
#define OUTPUTFOLDER_NAME "output"
void printUsageAndExit(char *argv_0 )
{
cerr << "Usage: " << argv_0 << " image_file output_destination [-N] [-T] " << endl;
exit(1);
}
int main(int argc, char** argv)
{
#ifdef DEBUG
cout << "GarbageCollector started! \n with " << argc << " arguments: ";
for(int i = 0; i<argc; i++)
{
cout << argv[i] << " ";
}
cout << endl;
#endif
if(argc < 3 || argc > 5)
{
printUsageAndExit(argv[0]);
}
//init SewagePlant
stringstream outputDestination;
outputDestination << argv[2];
if(argv[2][ strlen(argv[2]) -1] != '/')
{
outputDestination << "/" ;
}
outputDestination << OUTPUTFOLDER_NAME;
string timestamp_option = "-T";
string naming_option = "-N";
if((argc == 4 || argc == 5) && (naming_option.compare(argv[3]) == 0 ) )
{
string imageFileName(argv[1]);
replace(imageFileName.begin(), imageFileName.end(), '/','-');
replace(imageFileName.begin(), imageFileName.end(), '.','_');
outputDestination << "_" << imageFileName;
}
if( (argc == 4 && (timestamp_option.compare(argv[3])==0) ) || (argc == 5 && (timestamp_option.compare(argv[4])==0) ) )
{
time_t rawtime;
struct tm * timeinfo;
char timeBuffer [30];
time (&rawtime);
timeinfo = localtime (&rawtime);
strftime (timeBuffer,30,"_%F_%H-%M-%S",timeinfo);
/*if(argc == 5 )
{
outputDestination << "_";
}*/
outputDestination << timeBuffer;
} else if(argc == 4 || argc == 5)
{
printUsageAndExit(argv[0]);
}
int ret = mkdir(outputDestination.str().c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
if(ret != 0)
{
cerr << "failed to create directories at output_destination: " << strerror(errno) << endl;
exit(1);
}
outputDestination << "/";
vector<string> targetDirectories;
targetDirectories.push_back("pdf");
targetDirectories.push_back("jpg");
targetDirectories.push_back("png");
targetDirectories.push_back("sqlite");
for(uint i = 0; i< targetDirectories.size(); i++)
{
stringstream nextFolder;
nextFolder << outputDestination.str() << targetDirectories[i];
ret += mkdir(nextFolder.str().c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
}
if(ret != 0)
{
cerr << "failed to create subdirectories at output_destination: " << strerror(errno) << endl;
exit(1);
}
// Resolve path
char* realPath = realpath(argv[1], NULL);
if (NULL == realPath) {
perror("realpath");
exit(1);
}
int fd = open(realPath, O_RDONLY, 0);
if(fd == -1)
{
cerr << "GarbageMan open failed: " << strerror(errno) << endl;
exit(1);
}
uint64_t imageSize = getFileOrDeviceSize(realPath, fd);
if (0 == imageSize) {
cerr << "GarbageMan getFilesize returned 0" << endl;
close(fd);
exit(1);
}
// We don't need realPath any more
free(realPath);
close(fd);
SewagePlant swp(outputDestination.str(), 0,imageSize);
cout << "Recovered Files are saved in : " << outputDestination.str() << endl;
//starting ThreadPool
int numberOfThreads = thread::hardware_concurrency();
cout << numberOfThreads << " concurrent threads are supported.\n\n";
if(numberOfThreads == 0 ) // If the value is not well defined or not computable, returns 0.
{
numberOfThreads = 4;
}
numberOfThreads = numberOfThreads * THREADFACTOR;
ThreadPool tp(numberOfThreads,argv[1]);
int numberOfPlaces = numberOfThreads * BUFFERFACTOR; // Anzahl der Plaetze in Jbuffer
BNDBUF *jbuf = bbCreate(numberOfPlaces);
assert(jbuf != NULL);
#ifdef DEBUG
cout << numberOfThreads << " should be started. " << endl;
#endif
tp.start(jbuf, &swp);
//wait thrads for join
tp.waitForJoin();
cout << "all threads joined " << endl;
swp.condense();
//Cleanup
//TODO: Destructors??
bbDestroy(jbuf);
//Greetings ;)
cout << "\n To help is our mission! Bye" << endl;
return 0;
}