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
|
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <libsec.h>
#include "iso9660.h"
void
mkdirec(Direc *direc, XDir *d)
{
memset(direc, 0, sizeof(Direc));
direc->name = atom(d->name);
direc->uid = atom(d->uid);
direc->gid = atom(d->gid);
direc->uidno = d->uidno;
direc->gidno = d->gidno;
direc->mode = d->mode;
direc->length = d->length;
direc->mtime = d->mtime;
direc->atime = d->atime;
direc->ctime = d->ctime;
direc->symlink = d->symlink;
}
static int
strecmp(char *a, char *ea, char *b)
{
int r;
if((r = strncmp(a, b, ea-a)) != 0)
return r;
if(b[ea-a] == '\0')
return 0;
return 1;
}
/*
* Binary search a list of directories for the
* entry with name name.
* If no entry is found, return a pointer to
* where a new such entry would go.
*/
static Direc*
dbsearch(char *name, int nname, Direc *d, int n)
{
int i;
while(n > 0) {
i = strecmp(name, name+nname, d[n/2].name);
if(i < 0)
n = n/2;
else if(i > 0) {
d += n/2+1;
n -= (n/2+1);
} else
return &d[n/2];
}
return d;
}
/*
* Walk to name, starting at d.
*/
Direc*
walkdirec(Direc *d, char *name)
{
char *p, *nextp, *slashp;
Direc *nd;
for(p=name; p && *p; p=nextp) {
if((slashp = strchr(p, '/')) != nil)
nextp = slashp+1;
else
nextp = slashp = p+strlen(p);
nd = dbsearch(p, slashp-p, d->child, d->nchild);
if(nd >= d->child+d->nchild || strecmp(p, slashp, nd->name) != 0)
return nil;
d = nd;
}
return d;
}
/*
* Add the file ``name'' with attributes d to the
* directory ``root''. Name may contain multiple
* elements; all but the last must exist already.
*
* The child lists are kept sorted by utfname.
*/
Direc*
adddirec(Direc *root, char *name, XDir *d)
{
char *p;
Direc *nd;
int off;
if(name[0] == '/')
name++;
if((p = strrchr(name, '/')) != nil) {
*p = '\0';
root = walkdirec(root, name);
if(root == nil) {
sysfatal("error in proto file: no entry for /%s but /%s/%s\n", name, name, p+1);
return nil;
}
*p = '/';
p++;
} else
p = name;
nd = dbsearch(p, strlen(p), root->child, root->nchild);
off = nd - root->child;
if(off < root->nchild && strcmp(nd->name, p) == 0) {
if ((d->mode & DMDIR) == 0)
fprint(2, "warning: proto lists %s twice\n", name);
return nil;
}
if(root->nchild%Ndirblock == 0) {
root->child = erealloc(root->child, (root->nchild+Ndirblock)*sizeof(Direc));
nd = root->child + off;
}
memmove(nd+1, nd, (root->nchild - off)*sizeof(Direc));
mkdirec(nd, d);
nd->name = atom(p);
root->nchild++;
return nd;
}
/*
* Copy the tree src into dst.
*/
void
copydirec(Direc *dst, Direc *src)
{
int i, n;
*dst = *src;
if((src->mode & DMDIR) == 0)
return;
n = (src->nchild + Ndirblock - 1);
n -= n%Ndirblock;
dst->child = emalloc(n*sizeof(Direc));
n = dst->nchild;
for(i=0; i<n; i++)
copydirec(&dst->child[i], &src->child[i]);
}
/*
* Turn the Dbadname flag on for any entries
* that have non-conforming names.
*/
static void
_checknames(Direc *d, int (*isbadname)(char*), int isroot)
{
int i;
if(!isroot && isbadname(d->name))
d->flags |= Dbadname;
if(strcmp(d->name, "_conform.map") == 0)
d->flags |= Dbadname;
for(i=0; i<d->nchild; i++)
_checknames(&d->child[i], isbadname, 0);
}
void
checknames(Direc *d, int (*isbadname)(char*))
{
_checknames(d, isbadname, 1);
}
/*
* Set the names to conform to 8.3
* by changing them to numbers.
* Plan 9 gets the right names from its
* own directory entry.
*
* We used to write a _conform.map file to translate
* names. Joliet should take care of most of the
* interoperability with other systems now.
*/
void
convertnames(Direc *d, char* (*cvt)(char*, char*))
{
int i;
char new[1024];
if(d->flags & Dbadname)
cvt(new, conform(d->name, d->mode & DMDIR));
else
cvt(new, d->name);
d->confname = atom(new);
for(i=0; i<d->nchild; i++)
convertnames(&d->child[i], cvt);
}
/*
* Sort a directory with a given comparison function.
* After this is called on a tree, adddirec should not be,
* since the entries may no longer be sorted as adddirec expects.
*/
void
dsort(Direc *d, int (*cmp)(const void*, const void*))
{
int i, n;
n = d->nchild;
qsort(d->child, n, sizeof(d[0]), cmp);
for(i=0; i<n; i++)
dsort(&d->child[i], cmp);
}
|