summaryrefslogtreecommitdiff
path: root/src/paq.cc
blob: 2cbddd48ae8647864b7babf5972ebf45e3a4a952 (plain)
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
#include <dirent.h>
#include <unistd.h>
#include <iostream.h>
#include <list>
#include <Main.h>
#include <Input.h>
#include <Output.h>
#include <Regex.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifdef WORDS_BIGENDIAN 
#define SIGNATURE 0x4e504151
#else
#define SIGNATURE 0x5141504e
#endif

String current_dir;

extern "C" int sortdir(const void * d1, const void * d2) {
    struct stat fstats1, fstats2;
    String n;
    
    n = current_dir + "/" + (**((const dirent **)d1)).d_name;
    stat(n.to_charp(), &fstats1);
    
    n = current_dir + "/" + (**((const dirent **)d2)).d_name;
    stat(n.to_charp(), &fstats2);
    
    if (!((S_ISDIR(fstats1.st_mode) ? 1 : 0) ^ (S_ISDIR(fstats2.st_mode) ? 1 : 0))) {
	return alphasort(d1, d2);
    } else {
	if (S_ISDIR(fstats1.st_mode))
	    return -1;
	else
	    return 1;
    }
}

CODE_BEGINS

private:

struct couple {
    String p, v;
};

Output * Archive;

std::list<String> filelist;

void finalize(void) {
    int size;
    Input * file;
    for (std::list<String>::iterator i = filelist.begin(); i != filelist.end(); i++) {
	cerr << "Finalize file " << *i << endl;
	file = new Input(*i);
	size = file->GetSize();
	Archive->writeU32(size);
	delete file;
	file = new Input(*i + ".gz");
	copy(file, Archive);
	delete file;
	unlink((*i + ".gz").to_charp());
    }
}

void process_file(const String & filename) {
    char t;
    int size, old_size;
    cerr << "Processing file " << filename << "... ";
    
    Input * from = new Input(filename);
    Output * to = new Output(filename + ".gz");
    
    to->SetZ();
    
    old_size = from->GetSize();
    
    copy(from, to);
    
    delete to;
    delete from;
    
    filelist.push_back(filename);
    
    from = new Input(filename + ".gz");
    size = from->GetSize() + 4;
    cerr << old_size << " --> " << from->GetSize() << " (" << 100 * from->GetSize() / old_size << "%)\n";
    
    Archive->writeU32(size);
    
    delete from;
    
    t = 0;
    Archive->writeU8(t);
}

void process_directory(const String & dirname) throw (GeneralException) {
    struct dirent ** namelist;
    int n, i;
    Uint32 t;
    struct stat fstats;
    String fname;
    
    current_dir = dirname;
    n = scandir(dirname.to_charp(), &namelist, NULL, sortdir);
    cerr << "Processing directory " << dirname << endl;
    
    if (n < 0) {
	throw GeneralException("Unable to open directory " + dirname);
    }
    
    for (i = 0; i < n; i++) {
	fname = dirname + "/" + namelist[i]->d_name;
	stat(fname.to_charp(), &fstats);
	if (S_ISDIR(fstats.st_mode)) {
	    if (!Regex("^\\.{1,2}$").Match(namelist[i]->d_name)) {
		t = strlen(namelist[i]->d_name);
		Archive->writeU8(t);
		Archive->write(namelist[i]->d_name, t);
		t = 0;
		Archive->writeU32(t);
		t = 1;
		Archive->writeU8(t);
		process_directory(dirname + "/" + namelist[i]->d_name);
	    }
	} else {
	    if (!Regex("\\.gz$").Match(namelist[i]->d_name)) {
		t = strlen(namelist[i]->d_name);
		Archive->writeU8(t);
		Archive->write(namelist[i]->d_name, t);
		process_file(dirname + "/" + namelist[i]->d_name);
	    }
	}
	free((void *)namelist[i]);
    }
    
    free((void *)namelist);
    
    t = 0;
    Archive->writeU8(t);
}

void build_archive(const String & dirname) {
    char buff[4];
    
    *((int *) buff) = SIGNATURE;
    
    Archive->write(buff, 4);
    
    process_directory(dirname);
    
    finalize();
}

public:

virtual int startup(void) throw (GeneralException) {
    Archive = new Output("/tmp/bleh.paq");
    build_archive(".");
    return 0;
}
CODE_ENDS