/
main_offline.cpp
468 lines (389 loc) · 11.1 KB
/
main_offline.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
(C) Copyright Milan Svoboda 2009 - 2015.
This file is part of FuseCompress.
FuseCompress 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.
FuseCompress 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 FuseCompress. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <stddef.h>
#include <ftw.h>
#include <cstdlib>
#include <limits.h>
#include "rlog/rlog.h"
#include "Compress.hpp"
#include "CompressedMagic.hpp"
#include "CompressionType.hpp"
#include "FileRememberXattrs.hpp"
#include <boost/version.hpp>
#if BOOST_VERSION >= 105600
#define BOOST_DISABLE_ASSERTS
#endif
#include <boost/scoped_array.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/program_options.hpp>
#include <boost/tokenizer.hpp>
#include "boost/filesystem/operations.hpp" // includes boost/filesystem/path.hpp
#include "boost/filesystem/fstream.hpp" // ditto
#include <iostream>
#include <vector>
#include <string>
namespace po = boost::program_options;
namespace fs = boost::filesystem;
bool g_DebugMode = false;
bool g_QuietMode = false;
unsigned int g_BufferedMemorySize;
CompressedMagic g_CompressedMagic;
CompressionType g_CompressionType;
std::string g_dirLower;
std::string g_dirMount;
rlog::RLog *g_RLog;
volatile sig_atomic_t g_BreakFlag = 0;
bool g_RawOutput = true;
void catch_kill(int signum)
{
g_BreakFlag = 1;
}
bool copy(const char *i, const char *o, const struct stat *i_st, const struct stat *o_st)
{
Compress input(i_st, i);
int i_fd = input.open(i, O_RDONLY);
if (i_fd == -1)
{
rError("File (%s) cannot be opened! (%s)", i, strerror(errno));
return false;
}
if (input.isCompressed() == false)
{
rInfo(" Not compressed");
if (g_RawOutput)
{
rInfo(" Skipped");
// Input file is not compressed an user wants
// to decomrpess file. Return now.
input.release(i);
return false;
}
}
else
{
rInfo(" Compressed");
if (g_RawOutput)
{
}
else
{
if (input.isCompressedOnlyWith(g_CompressionType))
{
rInfo(" All blocks compressed with the same compression method");
// Input file is compressed only with
// the same compression type as user wants.
// Return now.
input.release(i);
return false;
}
rInfo(" Some block(s) compressed with different compression method than others");
}
}
Compress output(o_st, o);
if (g_RawOutput)
{
output.setCompressed(false);
}
boost::scoped_array<char> buffer(new char[g_BufferedMemorySize]);
int o_fd = output.open(o, O_WRONLY);
if (o_fd == -1)
{
rError("File (%s) cannot be opened! (%s)", o, strerror(errno));
input.release(i);
return false;
}
// Get the apparent input file size.
struct stat st;
int r = input.getattr(i, &st);
if (r == -1)
{
rError("Cannot determine apparent size of input file (%s) (%s)", i, strerror(errno));
input.release(i);
return false;
}
rInfo(" Processing");
for (off_t off = 0; off < st.st_size; off += g_BufferedMemorySize)
{
if (g_BreakFlag)
{
rWarning("Interrupted when processing file (%s)", i);
rWarning("File is left untouched");
input.release(i);
output.release(o);
return false;
}
off_t r = input.read(buffer.get(), g_BufferedMemorySize, off);
if (r == -1)
{
rError("Read failed! (offset: %lld, size: %lld)", (unsigned long long) off ,(unsigned long long) g_BufferedMemorySize);
input.release(i);
output.release(o);
return false;
}
off_t rr = output.write(buffer.get(), r, off);
if (rr != r)
{
rError("Write failed! (offset: %lld, size: %lld)", (unsigned long long) off , (unsigned long long) r);
input.release(i);
output.release(o);
return false;
}
}
// Remember extended attributes.
FileRememberXattrs xattrs;
xattrs.read(i_fd);
xattrs.write(o_fd);
input.release(i);
output.release(o);
return true;
}
int compress(const char *i, const struct stat *i_st, int mode, struct FTW *n)
{
if (!((mode == FTW_F) && (S_ISREG(i_st->st_mode))))
return 0;
fs::path input(i
#if BOOST_VERSION <= 104600
, fs::native
#endif
);
fs::path input_directory(input.branch_path());
fs::path output(input_directory / "XXXXXX");
rInfo("Processing file (%s)", input.string().c_str());
// Remember times of the input file.
struct stat stbuf;
lstat(input.string().c_str(), &stbuf);
int o_fd = mkstemp(const_cast<char *>(output.string().c_str()));
if (o_fd < 0)
{
rError("Failed to create an temporary file in (%s) directory", input_directory.string().c_str());
return -1;
}
rInfo("Temporary file (%s)", output.string().c_str());
struct stat o_st;
if (fstat(o_fd, &o_st) == -1)
{
rError("Failed to read stat of temporary file (%s)", output.string().c_str());
close(o_fd);
return -1;
}
close(o_fd);
if (!copy(i, output.string().c_str(), i_st, &o_st))
{
unlink(output.string().c_str());
return -1;
}
if (rename(output.string().c_str(), i) == -1)
{
rError("Failed to rename temporary file (%s) to (%s) (%s)", output.string().c_str(), input.string().c_str(), strerror(errno));
return -1;
}
if (chmod(i, i_st->st_mode) == -1)
{
rError("Failed to change mode (%s) (%s)", input.string().c_str(), strerror(errno));
return -1;
}
// Write back original times.
struct timespec times[2];
times[0].tv_sec = stbuf.st_atime;
times[0].tv_nsec = stbuf.st_atim.tv_nsec;
times[1].tv_sec = stbuf.st_mtime;
times[1].tv_nsec = stbuf.st_mtim.tv_nsec;
utimensat(AT_FDCWD, i, times, AT_SYMLINK_NOFOLLOW);
return 0;
}
void print_license()
{
printf("%s version %s\n", PACKAGE_NAME, PACKAGE_VERSION);
printf("Copyright (C) 2007, 2008 Milan Svoboda.\n");
printf("This is free software; see the source for copying conditions. There is NO\n");
printf("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
}
void print_help(const po::options_description &rDesc)
{
std::cout << rDesc << std::endl;
std::cout << "Supported compression methods:" << std::endl;
std::cout << "\t"; CompressionType::printAllSupportedMethods(std::cout); std::cout << std::endl << std::endl;
std::cout << "Files with any of the following mime type will not be compressed:" << std::endl;
std::cout << g_CompressedMagic << std::endl;
}
int main(int argc, char **argv)
{
g_BufferedMemorySize = 100;
string compressorName;
string commandLineOptions;
std::vector<string> fuseOptions;
fuseOptions.push_back(argv[0]);
po::options_description desc("Usage: " PACKAGE "_offline [options] path\n"
"\nPath may be directory or file.\n"
"\nNo options mean decompression mode.\n\n"
"Allowed options");
desc.add_options()
("options,o", po::value<string>(&commandLineOptions),
"fc_c:arg - compression method (lzo/bzip2/zlib/lzma)\n"
" (default: gz)\n"
"fc_b:arg - size of blocks in kilobytes\n"
" (default: 100)\n"
"fc_d - run in debug mode\n"
"fc_ma:arg - files with passed mime types to be\n"
" always not compressed\n"
"fc_mr:arg - files with passed mime types to be\n"
" always compressed\n"
"\nOther options are passed directly to fuse library. See fuse documentation for full list of supported options.\n")
("dir_lower", po::value<string>(&g_dirLower), "path")
("help,h", "print this help")
("version,v", "print version")
("quiet,q", "quiet mode")
;
po::positional_options_description pdesc;
pdesc.add("dir_lower", 1);
po::variables_map vm;
try {
po::store(po::command_line_parser(argc, argv).options(desc).positional(pdesc).run(), vm);
} catch (...) {
print_help(desc);
exit(EXIT_FAILURE);
}
po::notify(vm);
if (vm.count("help"))
{
print_help(desc);
exit(EXIT_SUCCESS);
}
if (vm.count("version"))
{
print_license();
exit(EXIT_SUCCESS);
}
if (vm.count("quiet"))
{
g_QuietMode = true;
}
g_RLog = new rlog::RLog("FuseCompress_offline", g_QuietMode ? LOG_NOTICE : LOG_INFO, true);
if (vm.count("options"))
{
typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
boost::char_separator<char> sep(",");
tokenizer tokens(commandLineOptions, sep);
for (tokenizer::iterator tok_it = tokens.begin(); tok_it != tokens.end(); ++tok_it)
{
if ((*tok_it).find_first_of("fc_", 0, 3) == 0)
{
typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
boost::char_separator<char> sep(":");
tokenizer tokens(*tok_it, sep);
tokenizer::iterator key = tokens.begin();
tokenizer::iterator value = key; ++value;
if (*key == "fc_c")
{
if (value == tokens.end())
{
rError("Compression type not set!");
exit(EXIT_FAILURE);
}
compressorName = *value;
}
if (*key == "fc_b")
{
if (value == tokens.end())
{
rError("Block size not set!");
exit(EXIT_FAILURE);
}
g_BufferedMemorySize = boost::lexical_cast<unsigned int>(*value);
}
if (*key == "fc_d")
{
g_DebugMode = true;
g_RLog->setLevel(LOG_DEBUG);
}
if (*key == "fc_ma")
{
if (value == tokens.end())
{
rError("Mime type(s) not set!");
exit(EXIT_FAILURE);
}
g_CompressedMagic.Add(*value);
}
if (*key == "fc_mr")
{
if (value == tokens.end())
{
rError("Mime type(s) not set!");
exit(EXIT_FAILURE);
}
g_CompressedMagic.Remove(*value);
}
}
else
{
fuseOptions.push_back("-o");
fuseOptions.push_back(*tok_it);
}
}
}
if (!vm.count("dir_lower"))
{
print_help(desc);
exit(EXIT_FAILURE);
}
g_BufferedMemorySize *= 1024;
if (compressorName != "")
{
g_RawOutput = false;
if (g_CompressionType.parseType(compressorName) == false)
{
rError("Compressor %s not found!", compressorName.c_str());
exit(EXIT_FAILURE);
}
}
fs::path pathLower(g_dirLower
#if BOOST_VERSION <= 104600
, fs::native
#endif
);
if (!pathLower.is_complete())
{
char cwd[PATH_MAX];
// Transform relative path to absolute path.
if (getcwd(cwd, sizeof(cwd)) == NULL)
{
rError("Cannot determine current working directory!");
exit(EXIT_FAILURE);
}
pathLower = fs::path(cwd
#if BOOST_VERSION <= 104600
, fs::native
#endif
) / pathLower;
}
// Set signal handler to catch SIGINT (CTRL+C).
struct sigaction setup_kill;
memset(&setup_kill, 0, sizeof(setup_kill));
setup_kill.sa_handler = catch_kill;
sigaction(SIGINT, &setup_kill, NULL);
// Iterate over directory structure and execute compress
// for every files there.
if (nftw(const_cast<char *>(pathLower.string().c_str()), compress, 100, FTW_PHYS | FTW_CHDIR))
exit(EXIT_FAILURE);
exit(EXIT_SUCCESS);
}