-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfs_util.c
executable file
·423 lines (375 loc) · 10.8 KB
/
fs_util.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
/*-
* 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.
*/
#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <sys/errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include <dirent.h>
#include <errno.h>
#include <fts.h>
#include <libgen.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "aun.h"
#include "extern.h"
#include "fs_proto.h"
#include "fileserver.h"
char *
strpad(char *s, int c, size_t len)
{
int i;
for (i = strlen(s); i < len; i++)
s[i] = c;
return s;
}
uint8_t
fs_mode_to_type(mode_t mode)
{
if (S_ISDIR(mode)) return EC_FS_TYPE_DIR;
return EC_FS_TYPE_FILE;
/* Old clients may prefer EC_FS_TYPE_SOME */
}
/*
* Conversions between Acorn access and Unix modes. Acorn 'L'
* prevents an object being deleted and has no Unix equivalent.
* usergroup determines whether group permissions follow user or
* public permissions when changed by an Acorn client.
*/
// Now using user execute permission to hold the 'L' flag,
// fs_delete will check if this is set and wont delete if
// the flag is set.
uint8_t
fs_mode_to_access(mode_t mode)
{
unsigned char access;
access = 0;
if (!S_ISDIR(mode))
{
if (mode & S_IXUSR) access |= EC_FS_ACCESS_L;
}
if (mode & S_IRUSR) access |= EC_FS_ACCESS_UR;
if (mode & S_IWUSR) access |= EC_FS_ACCESS_UW;
if (mode & S_IROTH) access |= EC_FS_ACCESS_OR;
if (mode & S_IWOTH) access |= EC_FS_ACCESS_OW;
if (S_ISDIR(mode)) access |= EC_FS_ACCESS_D;
return access;
}
mode_t
fs_access_to_mode(unsigned char access, int usergroup)
{
mode_t mode;
mode = 0;
if (access & EC_FS_ACCESS_UR)
mode |= S_IRUSR | (usergroup ? S_IRGRP : 0);
if (access & EC_FS_ACCESS_UW)
mode |= S_IWUSR | (usergroup ? S_IWGRP : 0);
if (access & EC_FS_ACCESS_L) {
mode |= S_IXUSR | (usergroup ? S_IXGRP : 0);
}
if (access & EC_FS_ACCESS_OR)
mode |= S_IROTH | (usergroup ? 0 : S_IRGRP);
if (access & EC_FS_ACCESS_OW)
mode |= S_IWOTH | (usergroup ? 0 : S_IWGRP);
return mode;
}
char *
fs_access_to_string(char *buf, uint8_t access)
{
buf[0] = '\0';
if (access & EC_FS_ACCESS_D) strcat(buf, "D");
if (access & EC_FS_ACCESS_L) strcat(buf, "L");
if (access & EC_FS_ACCESS_UW) strcat(buf, "W");
if (access & EC_FS_ACCESS_UR) strcat(buf, "R");
strcat(buf, "/");
if (access & EC_FS_ACCESS_OW) strcat(buf, "w");
if (access & EC_FS_ACCESS_OR) strcat(buf, "r");
return buf;
}
uint64_t
fs_read_val(uint8_t *p, size_t len)
{
uint64_t value;
value = 0;
p += len - 1;
while(len) {
value <<= 8;
value |= *p;
p--;
len--;
}
return value;
}
void
fs_write_val(uint8_t *p, uint64_t value, size_t len)
{
uint64_t max;
max = (1ULL << (len * 8)) - 1;
if (value > max) value = max;
while (len) {
/* LINTED no loss of accuracy */
*p = value & 0xff;
p++;
len--;
value >>= 8;
}
}
/*
* Construct path to Acorn metadata for a file. The caller must free the
* returned string itself.
*/
static char *
fs_metapath(FTSENT *f)
{
char *lastslash, *metapath;
lastslash = strrchr(f->fts_accpath, '/');
if (lastslash)
lastslash++;
else
lastslash = f->fts_accpath;
metapath = malloc((lastslash - f->fts_accpath) +
f->fts_namelen + 8 + 1);
if (metapath != NULL) {
sprintf(metapath, "%.*s.Acorn/%s",
(int)(lastslash - f->fts_accpath),
f->fts_accpath, f->fts_name);
}
return metapath;
}
void
fs_get_meta(FTSENT *f, struct ec_fs_meta *meta)
{
struct stat *st;
char *metapath, rawinfo[24];
uint64_t stamp;
int type, i, ret;
metapath = fs_metapath(f);
if (metapath != NULL) {
rawinfo[23] = '\0';
ret = readlink(metapath, rawinfo, 23);
if (ret == 23) {
for (i = 0; i < 4; i++)
/* LINTED strtoul result < 0x100 */
meta->load_addr[i] =
strtoul(rawinfo+i*3, NULL, 16);
for (i = 0; i < 4; i++)
/* LINTED strtoul result < 0x100 */
meta->exec_addr[i] =
strtoul(rawinfo+12+i*3, NULL, 16);
return;
} else if (ret == 17) {
fs_write_val(meta->load_addr,
strtoul(rawinfo, NULL, 16),
sizeof(meta->load_addr));
fs_write_val(meta->exec_addr,
strtoul(rawinfo + 9, NULL, 16),
sizeof(meta->load_addr));
return;
}
free(metapath);
}
st = f->fts_statp;
if (st != NULL) {
stamp = fs_riscos_date(st->st_mtime,
#if HAVE_STRUCT_STAT_ST_MTIMENSEC
st->st_mtimensec / 10000000
#elif HAVE_STRUCT_STAT_ST_MTIM
st->st_mtim.tv_nsec / 10000000
#else
0
#endif
);
type = fs_guess_type(f);
fs_write_val(meta->load_addr,
0xfff00000 | (type << 8) | (stamp >> 32), 4);
fs_write_val(meta->exec_addr, stamp & 0x00ffffffffULL, 4);
} else {
fs_write_val(meta->load_addr, 0xdeaddead, 4);
fs_write_val(meta->exec_addr, 0xdeaddead, 4);
}
}
bool
fs_set_meta(FTSENT *f, struct ec_fs_meta *meta)
{
char *lastslash, *metapath, rawinfo[24];
int ret;
metapath = fs_metapath(f);
if (metapath == NULL) {
errno = ENOMEM;
return false;
}
lastslash = strrchr(metapath, '/');
*lastslash = '\0'; /* metapath now points to the .Acorn directory. */
ret = rmdir(metapath);
if (ret < 0 && errno != ENOENT && errno != ENOTEMPTY)
goto fail;
if ((ret < 0 && errno == ENOENT) || ret == 0) {
if (mkdir(metapath, 0777) < 0)
goto fail;
}
*lastslash = '/'; /* metapath now points to the metadata again. */
sprintf(rawinfo, "%08lX %08lX",
(unsigned long)
fs_read_val(meta->load_addr, sizeof(meta->load_addr)),
(unsigned long)
fs_read_val(meta->exec_addr, sizeof(meta->exec_addr)));
if (unlink(metapath) < 0 && errno != ENOENT)
goto fail;
if (symlink(rawinfo, metapath) < 0)
goto fail;
free(metapath);
return true;
fail:
free(metapath);
return false;
}
void
fs_del_meta(FTSENT *f)
{
char *metapath;
metapath = fs_metapath(f);
if (metapath != NULL) {
unlink(metapath);
*strrchr(metapath, '/') = '\0';
rmdir(metapath); /* Don't worry if it fails. */
free(metapath);
}
}
/*
* Return the System Internal Name for a file. This is only 24 bits long
* but is expected to be unique across the whole disk. For now, we fake
* it with the bottom 24 bits of the inode number, which is less than
* optimal.
*/
int
fs_get_sin(FTSENT *f)
{
return f->fts_statp->st_ino & 0xFFFFFF;
}
/*
* Get the creation time of a file, or the best approximation we can
* manage. Various bits of protocol return this as a fileserver date
* or as a string.
*/
time_t
fs_get_birthtime(FTSENT *f)
{
#if HAVE_STRUCT_STAT_ST_BIRTHTIME
/*
* NetBSD 5.0 seems to be confused over whether an unknown
* birthtime should be 0 or VNOVAL (-1).
*/
if (f->fts_statp->st_birthtime &&
f->fts_statp->st_birthtime != (time_t)(-1))
return f->fts_statp->st_birthtime;
#endif
/* Ah well, mtime will have to do. */
return f->fts_statp->st_mtime;
}
/*
* Convert a Unix time_t (non-leap seconds since 1970-01-01) and odd
* centiseconds to a RISC OS time (non-leap(?) centiseconds since
* 1900-01-01(?)).
*/
uint64_t
fs_riscos_date(time_t time, unsigned csec)
{
uint64_t base;
base = 31536000ULL * 70 + 86400 * 17;
return (((uint64_t)time) + base)*100;
}
/*
* Convert a date stamp from Unix to Acorn fileserver.
*/
void
fs_write_date(struct ec_fs_date *date, time_t time)
{
struct tm *t;
int year81;
t = localtime(&time);
if (t->tm_year < 81) {
/* Too early -- return lowest date possible */
date->day = 1;
date->year_month = 1;
} else {
year81 = t->tm_year - 81;
date->day = t->tm_mday | ((year81 & 0xf0) << 1);
date->year_month = (t->tm_mon + 1) | (year81 << 4);
}
}
/*
* Mostly like stat(2), but if called on a broken symlink, returns
* information on the symlink itself.
*/
int
fs_stat(const char *path, struct stat *sb)
{
int rc;
rc = stat(path, sb);
if (rc == -1 && errno == ENOENT)
/* Could be a broken symlink */
rc = lstat(path, sb);
return rc;
}
const char *
fs_leafname(const char *path)
{
char *leaf;
if ((leaf = strrchr(path, '/')) != NULL)
return leaf+1;
else
return path;
}
bool
fs_is_owner(struct fs_context *c, char *path) {
char *oururd;
int match;
bool is_owner; /* True - Owner, False - Public */
oururd = userfuncs->urd(c->client->login);
fs_acornify_name(oururd);
match = strncmp(userfuncs->urd(c->client->login), path,
strlen(userfuncs->urd(c->client->login)));
if (match == 0) {
is_owner = true;
} else {
is_owner = false;
}
/* now we check if they have privilege as that gives
ownership access - Can still have no permission to write */
if (c->client->priv == EC_FS_PRIV_SYST)
{
is_owner = true; /* Privilege gives owner access */
}
return is_owner;
}