-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfs_nametrans.c
executable file
·459 lines (427 loc) · 10.5 KB
/
fs_nametrans.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
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
/*-
* Copyright (c) 2010 Simon Tatham
* Copyright (c) 1998, 2010 Ben Harris
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* This is part of aund, an implementation of Acorn Universal
* Networking for Unix.
*/
/*
* fs_nametrans.c -- File-name translation (Unix<->Acorn)
*/
#include <sys/stat.h>
#include <sys/types.h>
#include <assert.h>
#include <ctype.h>
#include <dirent.h>
#include <err.h>
#include <errno.h>
#include <libgen.h>
#include <stdlib.h>
#include <string.h>
#include "extern.h"
#include "fileserver.h"
#include "fs_errors.h"
static char *fs_unhat_path(char *);
static void fs_match_path(char *);
static void fs_trans_simple(char *, char *);
/*
* Convert a leaf name to Acorn style for presenting to the client.
* Converts in place.
*/
char *
fs_acornify_name(char *name)
{
size_t len;
char *p, *q;
if (debug) printf("fs_acornify_name: [%s]", name);
p = q = name;
if (*p == '.' && !p[1])
p++; /* map "." to the empty string */
if (*p == '.' && p[1] == '.' && p[2] == '.')
p += 2; /* un-dot-stuff */
for (; *p; p++)
*q++ = (*p == '.' ? '/' : *p); /* un-slash-dot-swap */
len = q - name;
if (len >= 4 && name[len-4] == ',')
/* For now, assume all *,??? names are magic */
name[len-4] = '\0';
else
name[len] = '\0';
if (debug) printf("->[%s]\n", name);
return name;
}
/*
* Determine whether a leaf name describes a file that the file
* server should be showing. File names beginning with one or two
* dots are hidden (this covers '.', '..' and '.Acorn' in
* particular), and so are file names longer than 10 characters
* (after stripping two dots off dot-stuffed ones).
*/
bool
fs_hidden_name(char *name)
{
int len;
if (*name == '.') {
/*
* Check for, and skip, two extra dots.
*/
if (*++name != '.' || *++name != '.')
return true; /* dotfile; hidden */
/*
* Now the name has been un-dot-stuffed.
*/
}
len = strlen(name);
/* Ignore a ',???' suffix when finding name length */
if (len >= 4 && name[len-4] == ',')
len -= 4;
if (len > 10)
return true; /* long file: hidden */
return false;
}
/*
* Convert a path provided by a client into a Unix one. Note that the
* new path is in a freshly mallocked block, and the caller is
* responsible for freeing it.
*/
char *
fs_unixify_path(struct fs_context *c, char *path)
{
const char *base;
int nnames;
char *urd = NULL, *csd = NULL, *lib = NULL;
size_t disclen;
char *path2;
char *path3;
char *p, *q;
switch (c->req->function) {
default:
urd = c->req->urd ?
c->client->handles[c->req->urd]->path : NULL;
/* FALLTHROUGH */
case EC_FS_FUNC_LOAD:
case EC_FS_FUNC_LOAD_32:
case EC_FS_FUNC_LOAD_COMMAND:
case EC_FS_FUNC_SAVE:
case EC_FS_FUNC_GETBYTES:
case EC_FS_FUNC_PUTBYTES:
/* In these calls, the URD is replaced by a port number */
csd = c->req->csd ?
c->client->handles[c->req->csd]->path : NULL;
lib = c->req->lib ?
c->client->handles[c->req->lib]->path : NULL;
/* FALLTHROUGH */
case EC_FS_FUNC_GETBYTE:
case EC_FS_FUNC_PUTBYTE:
/* And these ones don't pass context at all. */
break;
}
/*
* Plenty of space.
*/
path2 = malloc((urd ? strlen(urd) : 0) + (csd ? strlen(csd) : 0) +
(lib ? strlen(lib) : 0) +
2 * strlen(path) + 100);
if (path == NULL) {
fs_err(c, EC_FS_E_NOMEM);
return NULL;
}
if (debug) printf("fs_unixify_path: [%s]", path);
/* By default, resolve things from the CSD. */
base = csd;
/*
* Disc names can start with either ':' or '$', the latter
* being an SJism. In either case, this means paths are
* resolved from the root of that disc.
*/
if ((path[0] == ':' || path[0] == '$') &&
(path[1] != '.' && path[1] != '\0')) {
path++;
disclen = strcspn(path, ".");
if (disclen != strlen(discname) ||
strncasecmp(path, discname, disclen) != 0) {
fs_err(c, EC_FS_E_NOTFOUND);
return NULL;
}
path += disclen;
if (*path) path++;
base = ".";
}
/*
* Decide what base path this pathname is relative to, by
* spotting magic characters at the front. Without any, of
* course, it'll be relative to the csd.
*/
if (path[0] && strchr("$:&%@", path[0]) &&
(!path[1] || path[1] == '.')) {
switch (path[0]) {
case '$':
case ':': /* SJ alias */
base = "."; break;
case '&':
base = urd; break;
case '@':
base = csd; break;
case '%':
base = lib; break;
}
path++;
if (*path) path++;
}
if (base == NULL) {
free(path2);
fs_err(c, EC_FS_E_CHANNEL);
return NULL;
}
sprintf(path2, "%s/", base);
/*
* Append the supplied pathname to that prefix, performing
* simple translations on the way.
*/
fs_trans_simple(path2 + strlen(path2), path);
if (debug) printf("->[%s]", path2);
/*
* Unhat.
*/
fs_unhat_path(path2);
if (debug) printf("->[%s]", path2);
/*
* References directly to the root dir: turn an empty name
* into ".".
*/
if (!*path2)
strcpy(path2, ".");
/*
* Process every path component through fs_match_path.
*/
for (p = path2, nnames = 1; *p; p++)
if (*p == '/')
nnames++;
path3 = malloc(20 * nnames + 10);
p = path2;
q = path3;
while (*p) {
char *r = p;
while (*p && *p != '/') p++;
sprintf(q, "%.*s", (int)(p-r), r);
fs_match_path(path3);
q += strlen(q);
if (*p) {
p++;
*q++ = '/';
}
}
*q = '\0';
if (debug) printf("->[%s]\n", path3);
free(path2);
path3 = realloc(path3, 1 + strlen(path3));
return path3;
}
/*
* Remove '/foo/^' constructs from a path
*/
static char *
fs_unhat_path(char *path)
{
char *p, *q;
/*
* p walks along the path as we read it; q walks along the
* same string as we write the transformed version.
*/
p = q = path;
while (*p) {
if (*p == '^' && (!p[1] || p[1] == '/')) {
/*
* Hat component. Skip it, and backtrack q.
*/
p++;
while (q > path && q[-1] != '/')
q--; /* backtrack over the previous word */
if (q > path)
q--; /* and over the slash before it */
} else {
/*
* Non-hat component. Just copy it in.
*/
if (q > path)
*q++ = '/';
while (*p && *p != '/')
*q++ = *p++;
}
if (*p) {
assert(*p == '/');
p++;
}
}
*q = '\0';
return path;
}
/*
* Case-insensitively match a file name against a potential
* wildcard.
*/
static bool
wcfrag(char *frag, char *file)
{
while (*frag && *frag != '*') {
if (*frag != '?' && (toupper((unsigned char)*file) !=
toupper((unsigned char)*frag)))
return false;
frag++;
file++;
}
return true;
}
static bool
wcmatch(char *wc, char *file, int len)
{
char *fragend;
char *filestart = file;
int at_start = 1;
while (*wc) {
for (fragend = wc; *fragend && *fragend != '*'; fragend++);
if (*fragend) {
/*
* This fragment isn't the end of the
* wildcard, so we match it at the first
* place we can.
*/
while (len >= fragend - wc &&
((at_start && file!=filestart) ||
!wcfrag(wc, file)))
file++, len--;
if (len < fragend - wc)
return false;
file += fragend - wc;
len -= fragend - wc;
wc = fragend;
} else {
/*
* This fragment is at the end, so we must
* match it at precisely the end or fail.
*/
if (len < fragend - wc)
return false;
file += len - (fragend - wc);
return ((!at_start || file==filestart) &&
wcfrag(wc, file));
}
while (*wc == '*') wc++;
at_start = 0;
}
return true;
}
/*
* Find the real file that matches the name in 'path'. This may
* involve:
*
* - truncating to 10 characters
* - case-insensitively matching
* - wildcard matching (we just return the first match)
* - appending ,??? for a RISC OS file type
*/
static void
fs_match_path(char *path)
{
struct stat st;
char *pathcopy, *parentpath, *leaf, *wc;
DIR *parent;
struct dirent *dp;
size_t leaflen;
leaf = strrchr(path, '/');
if (leaf)
leaf++;
else
leaf = path;
leaflen = strlen(leaf);
if (leaflen > 10) {
leaflen = 10;
leaf[leaflen] = '\0';
}
if (lstat(path, &st) == -1 && errno == ENOENT) {
pathcopy = strdup(path);
parentpath = dirname(pathcopy);
parent = opendir(parentpath);
if (parent == NULL) {
free(pathcopy);
return;
}
wc = leaf;
if (wc[0] == '.' && wc[1] == '.' && wc[2] == '.')
wc += 2; /* un-dot-stuff wildcard */
while ((dp = readdir(parent)) != NULL) {
char *name = dp->d_name;
int namelen = strlen(dp->d_name);
if (name[0] == '.') {
if (namelen >= 3 &&
name[1] == '.' && name[2] == '.') {
name += 2; /* un-dot-stuff */
} else
continue; /* hidden file */
}
if (namelen >= 4 && dp->d_name[namelen-4] == ',')
namelen -= 4;
if (namelen <= 10 &&
wcmatch(leaf, dp->d_name, namelen)) {
strcpy(leaf, dp->d_name);
break;
}
}
closedir(parent);
free(pathcopy);
}
}
/*
* Simple translations: exchange . and /, and stuff two extra dots
* at the front of any pathname starting with a dot. (That protects
* '.', '..' and '.Acorn'.)
*/
static void
fs_trans_simple(char *pathret, char *path)
{
/*
* Loop over each pathname component.
*/
while (*path) {
if (*path == '/') {
*pathret++ = '.';
*pathret++ = '.';
}
while (*path && *path != '.') {
if (*path == '/')
*pathret++ = '.';
else
*pathret++ = *path;
path++;
}
if (*path) {
path++;
*pathret++ = '/';
}
}
*pathret = '\0';
}