forked from devttys0/sasquatch
-
Notifications
You must be signed in to change notification settings - Fork 10
/
sort.c
363 lines (313 loc) · 9.29 KB
/
sort.c
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
/*
* Create a squashfs filesystem. This is a highly compressed read only
* filesystem.
*
* Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012,
* 2013, 2014
* Phillip Lougher <[email protected]>
*
* 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,
* or (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* sort.c
*/
#define TRUE 1
#define FALSE 0
#define MAX_LINE 16384
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "squashfs_fs.h"
#include "mksquashfs.h"
#include "sort.h"
#include "error.h"
#include "progressbar.h"
int mkisofs_style = -1;
struct sort_info {
dev_t st_dev;
ino_t st_ino;
int priority;
struct sort_info *next;
};
struct sort_info *sort_info_list[65536];
struct priority_entry *priority_list[65536];
extern int silent;
extern void write_file(squashfs_inode *inode, struct dir_ent *dir_ent,
int *c_size);
extern char *pathname(struct dir_ent *dir_ent);
void add_priority_list(struct dir_ent *dir, int priority)
{
struct priority_entry *new_priority_entry;
priority += 32768;
new_priority_entry = malloc(sizeof(struct priority_entry));
if(new_priority_entry == NULL)
MEM_ERROR();
new_priority_entry->dir = dir;;
new_priority_entry->next = priority_list[priority];
priority_list[priority] = new_priority_entry;
}
int get_priority(char *filename, struct stat *buf, int priority)
{
int hash = buf->st_ino & 0xffff;
struct sort_info *s;
for(s = sort_info_list[hash]; s; s = s->next)
if((s->st_dev == buf->st_dev) && (s->st_ino == buf->st_ino)) {
TRACE("returning priority %d (%s)\n", s->priority,
filename);
return s->priority;
}
TRACE("returning priority %d (%s)\n", priority, filename);
return priority;
}
#define ADD_ENTRY(buf, priority) {\
int hash = buf.st_ino & 0xffff;\
struct sort_info *s;\
if((s = malloc(sizeof(struct sort_info))) == NULL) \
MEM_ERROR(); \
s->st_dev = buf.st_dev;\
s->st_ino = buf.st_ino;\
s->priority = priority;\
s->next = sort_info_list[hash];\
sort_info_list[hash] = s;\
}
int add_sort_list(char *path, int priority, int source, char *source_path[])
{
int i, n;
struct stat buf;
TRACE("add_sort_list: filename %s, priority %d\n", path, priority);
if(strlen(path) > 1 && strcmp(path + strlen(path) - 2, "/*") == 0)
path[strlen(path) - 2] = '\0';
TRACE("add_sort_list: filename %s, priority %d\n", path, priority);
re_read:
if(path[0] == '/' || strncmp(path, "./", 2) == 0 ||
strncmp(path, "../", 3) == 0 || mkisofs_style == 1) {
if(lstat(path, &buf) == -1)
goto error;
TRACE("adding filename %s, priority %d, st_dev %d, st_ino "
"%lld\n", path, priority, (int) buf.st_dev,
(long long) buf.st_ino);
ADD_ENTRY(buf, priority);
return TRUE;
}
for(i = 0, n = 0; i < source; i++) {
char *filename;
int res = asprintf(&filename, "%s/%s", source_path[i], path);
if(res == -1)
BAD_ERROR("asprintf failed in add_sort_list\n");
res = lstat(filename, &buf);
free(filename);
if(res == -1) {
if(!(errno == ENOENT || errno == ENOTDIR))
goto error;
continue;
}
ADD_ENTRY(buf, priority);
n ++;
}
if(n == 0 && mkisofs_style == -1 && lstat(path, &buf) != -1) {
ERROR("WARNING: Mkisofs style sortlist detected! This is "
"supported but please\n");
ERROR("convert to mksquashfs style sortlist! A sortlist entry");
ERROR(" should be\neither absolute (starting with ");
ERROR("'/') start with './' or '../' (taken to be\nrelative to "
"$PWD), otherwise it ");
ERROR("is assumed the entry is relative to one\nof the source "
"directories, i.e. with ");
ERROR("\"mksquashfs test test.sqsh\",\nthe sortlist ");
ERROR("entry \"file\" is assumed to be inside the directory "
"test.\n\n");
mkisofs_style = 1;
goto re_read;
}
mkisofs_style = 0;
if(n == 1)
return TRUE;
if(n > 1) {
ERROR(" Ambiguous sortlist entry \"%s\"\n\nIt maps to more "
"than one source entry! Please use an absolute path."
"\n", path);
return FALSE;
}
error:
ERROR_START("Cannot stat sortlist entry \"%s\"\n", path);
ERROR("This is probably because you're using the wrong file\n");
ERROR("path relative to the source directories.");
ERROR_EXIT(" Ignoring");
/*
* Historical note
* Failure to stat a sortlist entry is deliberately ignored, even
* though it is an error. Squashfs release 2.2 changed the behaviour
* to treat it as a fatal error, but it was changed back to
* the original behaviour to ignore it in release 2.2-r2 following
* feedback from users at the time.
*/
return TRUE;
}
void generate_file_priorities(struct dir_info *dir, int priority,
struct stat *buf)
{
struct dir_ent *dir_ent = dir->list;
priority = get_priority(dir->pathname, buf, priority);
for(; dir_ent; dir_ent = dir_ent->next) {
struct stat *buf = &dir_ent->inode->buf;
if(dir_ent->inode->root_entry)
continue;
switch(buf->st_mode & S_IFMT) {
case S_IFREG:
add_priority_list(dir_ent,
get_priority(pathname(dir_ent), buf,
priority));
break;
case S_IFDIR:
generate_file_priorities(dir_ent->dir,
priority, buf);
break;
}
}
}
int read_sort_file(char *filename, int source, char *source_path[])
{
FILE *fd;
char line_buffer[MAX_LINE + 1]; /* overflow safe */
char sort_filename[MAX_LINE + 1]; /* overflow safe */
char *line, *name;
int n, priority, res;
if((fd = fopen(filename, "r")) == NULL) {
ERROR("Failed to open sort file \"%s\" because %s\n",
filename, strerror(errno));
return FALSE;
}
while(fgets(line = line_buffer, MAX_LINE + 1, fd) != NULL) {
int len = strlen(line);
if(len == MAX_LINE && line[len - 1] != '\n') {
/* line too large */
ERROR("Line too long when reading "
"sort file \"%s\", larger than %d "
"bytes\n", filename, MAX_LINE);
goto failed;
}
/*
* Remove '\n' terminator if it exists (the last line
* in the file may not be '\n' terminated)
*/
if(len && line[len - 1] == '\n')
line[len - 1] = '\0';
/* Skip any leading whitespace */
while(isspace(*line))
line ++;
/* if comment line, skip */
if(*line == '#')
continue;
/*
* Scan for filename, don't use sscanf() and "%s" because
* that can't handle filenames with spaces
*/
for(name = sort_filename; !isspace(*line) && *line != '\0';) {
if(*line == '\\') {
line ++;
if (*line == '\0')
break;
}
*name ++ = *line ++;
}
*name = '\0';
/*
* if filename empty, then line was empty of anything but
* whitespace or a backslash character. Skip empy lines
*/
if(sort_filename[0] == '\0')
continue;
/*
* Scan the rest of the line, we expect a decimal number
* which is the filename priority
*/
errno = 0;
res = sscanf(line, "%d%n", &priority, &n);
if((res < 1 || errno) && errno != ERANGE) {
if(errno == 0)
/* No error, assume EOL or match failure */
ERROR("Sort file \"%s\", can't find priority "
"in entry \"%s\", EOL or match "
"failure\n", filename, line_buffer);
else
/* Some other failure not ERANGE */
ERROR("Sscanf failed reading sort file \"%s\" "
"because %s\n", filename,
strerror(errno));
goto failed;
} else if((errno == ERANGE) ||
(priority < -32768 || priority > 32767)) {
ERROR("Sort file \"%s\", entry \"%s\" has priority "
"outside range of -32767:32768.\n", filename,
line_buffer);
goto failed;
}
/* Skip any trailing whitespace */
line += n;
while(isspace(*line))
line ++;
if(*line != '\0') {
ERROR("Sort file \"%s\", trailing characters after "
"priority in entry \"%s\"\n", filename,
line_buffer);
goto failed;
}
res = add_sort_list(sort_filename, priority, source,
source_path);
if(res == FALSE)
goto failed;
}
if(ferror(fd)) {
ERROR("Reading sort file \"%s\" failed because %s\n", filename,
strerror(errno));
goto failed;
}
fclose(fd);
return TRUE;
failed:
fclose(fd);
return FALSE;
}
void sort_files_and_write(struct dir_info *dir)
{
int i;
struct priority_entry *entry;
squashfs_inode inode;
int duplicate_file;
for(i = 65535; i >= 0; i--)
for(entry = priority_list[i]; entry; entry = entry->next) {
TRACE("%d: %s\n", i - 32768, pathname(entry->dir));
if(entry->dir->inode->inode == SQUASHFS_INVALID_BLK) {
write_file(&inode, entry->dir, &duplicate_file);
INFO("file %s, uncompressed size %lld bytes %s"
"\n", pathname(entry->dir),
(long long)
entry->dir->inode->buf.st_size,
duplicate_file ? "DUPLICATE" : "");
entry->dir->inode->inode = inode;
entry->dir->inode->type = SQUASHFS_FILE_TYPE;
} else
INFO("file %s, uncompressed size %lld bytes "
"LINK\n", pathname(entry->dir),
(long long)
entry->dir->inode->buf.st_size);
}
}