forked from hasse69/rar2fs
/
dirlist.c
165 lines (149 loc) · 6.01 KB
/
dirlist.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
/*
Copyright (C) 2009-2014 Hans Beckerus (hans.beckerus#AT#gmail.com)
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 3 of the License, 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, see <http://www.gnu.org/licenses/>.
This program take use of the freeware "Unrar C++ Library" (libunrar)
by Alexander Roshal and some extensions to it.
Unrar source may be used in any software to handle RAR archives
without limitations free of charge, but cannot be used to re-create
the RAR compression algorithm, which is proprietary. Distribution
of modified Unrar source in separate form or as a part of other
software is permitted, provided that it is clearly stated in
the documentation and source comments that the code may not be used
to develop a RAR (WinRAR) compatible archiver.
*/
#include <platform.h>
#include <string.h>
#include "dirlist.h"
#define DIR_LIST_HEAD_ ((void*)-1)
/*!
*****************************************************************************
*
****************************************************************************/
static inline int swap(struct dir_entry_list *A, struct dir_entry_list *B)
{
int swap = strcmp(A->entry.name, B->entry.name);
swap = !swap ? A->entry.type > B->entry.type : swap;
if (swap > 0) {
const struct dir_entry TMP = B->entry;
B->entry = A->entry;
A->entry = TMP;
return 1;
}
return 0;
}
/*!
*****************************************************************************
*
****************************************************************************/
void dir_list_open(struct dir_entry_list *root)
{
root->next = NULL;
root->entry.name = NULL;
root->entry.head_flag = DIR_LIST_HEAD_;
}
/*!
*****************************************************************************
*
****************************************************************************/
void dir_list_close(struct dir_entry_list *root)
{
/* Simple bubble sort of directory entries in alphabetical order */
if (root && root->next) {
int n;
struct dir_entry_list *next;
do {
n = 0;
next = root->next;
while (next->next) {
n += swap(next, next->next);
next = next->next;
}
} while (n != 0); /* while swaps performed */
/* Make sure entries are unique. Duplicates will be removed. */
next = root->next;
while (next->next) {
if ((next->entry.type == DIR_E_NRM || /* no hash */
next->entry.hash == next->next->entry.hash) &&
!strcmp(next->entry.name, next->next->entry.name)) {
/*
* A duplicate. Rare but possible.
* Make sure the current entry is kept marked
* as valid since regular fs entries should
* always have priority.
*/
next->next->entry.valid = 0;
}
next = next->next;
}
}
}
/*!
*****************************************************************************
*
****************************************************************************/
void dir_list_free(struct dir_entry_list *root)
{
struct dir_entry_list *next = root->next;
while (next) {
struct dir_entry_list *tmp = next;
next = next->next;
free(tmp->entry.name);
free(tmp);
}
}
/*!
*****************************************************************************
*
****************************************************************************/
struct dir_entry_list *dir_entry_add_hash(struct dir_entry_list *l,
const char *key, struct stat *st, uint32_t hash, int type)
{
int dir_entry_add_skip_ = 0;
if (l->entry.head_flag != DIR_LIST_HEAD_) {
if (hash == l->entry.hash)
if (!strcmp(key, l->entry.name))
dir_entry_add_skip_ = 1;
};
l->next = !dir_entry_add_skip_
? malloc(sizeof(struct dir_entry_list))
: NULL;
if (l->next) {
l=l->next;
l->entry.head_flag = 0;
l->entry.name = strdup(key);
l->entry.hash = hash;
l->entry.st = st;
l->entry.type = type;
l->entry.valid = 1; /* assume entry is valid */
l->next = NULL;
}
return l;
}
/*!
*****************************************************************************
*
****************************************************************************/
struct dir_entry_list *dir_entry_add(struct dir_entry_list *l, const char *key,
struct stat *st, int type)
{
l->next = malloc(sizeof(struct dir_entry_list));
if (l->next) {
l=l->next;
l->entry.name = strdup(key);
l->entry.hash = 0;
l->entry.st = st;
l->entry.type = type;
l->entry.valid = 1; /* assume entry is valid */
l->next = NULL;
}
return l;
}