{{ message }}
Repository navigation
Expand file tree
/
Copy pathplatform_riscos.c
More file actions
503 lines (458 loc) · 13.7 KB
/
Copy pathplatform_riscos.c
File metadata and controls
503 lines (458 loc) · 13.7 KB
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
#include "namespace.h"
#include "platform.h"
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <utime.h>
#ifdef __riscos__
#include "riscos_identity.h"
#include <kernel.h>
#include <swis.h>
#include <unixlib/local.h>
#endif
int platform_joinpath(char *dst, size_t size, const char *dir,
const char *name) {
#ifdef __riscos__
size_t dirlen;
if(!dst || !dir || !name || !size)
return -1;
dirlen = strlen(dir);
/* FileSwitch uses dots between native path components and slashes
* inside leaf names where Unix paths use full stops. */
if(strchr(dir, ':')) {
size_t used = dirlen;
if(used >= size)
return -1;
memcpy(dst, dir, used);
while(*name == '/')
name++;
if(*name && used && dst[used - 1] != ':') {
if(used + 1 >= size)
return -1;
dst[used++] = '.';
}
for(; *name; name++) {
if(used + 1 >= size)
return -1;
dst[used++] = *name == '/' ? '.' : *name == '.' ? '/' : *name;
}
dst[used] = '\0';
return 0;
}
#endif
return joinpath(dst, size, dir, name);
}
void platform_namespace_cleanup(Namespace *ns) {
(void)ns;
#ifdef __riscos__
riscos_identity_cleanup();
#endif
}
int platform_namespace_ready(Namespace *ns) {
#ifdef __riscos__
if(ns->synthetic)
return 0;
#endif
struct stat st;
if(ns->synthetic) {
errno = EINVAL;
return -1;
}
return stat(ns->native_root, &st) < 0 || !S_ISDIR(st.st_mode) ? -1 : 0;
}
int platform_namespace_init(Namespace *ns) {
#ifdef __riscos__
return namespace_use_synthetic(ns);
#else
return namespace_use_native(ns, "/");
#endif
}
#ifdef __riscos__
static int native_directory(const char *path) {
ResolvedPath resolved;
struct stat st;
memset(&resolved, 0, sizeof(resolved));
if(snprintf(resolved.native_path, sizeof(resolved.native_path), "%s", path) >=
(int)sizeof(resolved.native_path))
return 0;
return platform_lstat(&resolved, &st) == 0 && S_ISDIR(st.st_mode);
}
#endif
#ifdef __riscos__
static int add_drive_root(Namespace *ns, const char *name, unsigned drive) {
char path[S9_PATH_MAX];
char root_name[132];
if(snprintf(path, sizeof(path), "%s::%u.$", name, drive) >=
(int)sizeof(path) || !native_directory(path))
return 0;
if(snprintf(root_name, sizeof(root_name), "%s-%u", name, drive) >=
(int)sizeof(root_name))
return 0;
return namespace_add_root(ns, root_name, path);
}
#endif
int platform_namespace_discover(Namespace *ns) {
#ifdef __riscos__
unsigned number;
for(number = 0; number < 256; number++) {
char name[128];
char path[S9_PATH_MAX];
unsigned drive;
int direct;
int root_zero = 0;
if(_swix(OS_FSControl, _INR(0, 3), 33, number, name,
sizeof(name)) != NULL || !name[0])
continue;
if(snprintf(path, sizeof(path), "%s:", name) >= (int)sizeof(path))
continue;
direct = native_directory(path);
if(!direct) {
if(snprintf(path, sizeof(path), "%s::0.$", name) >=
(int)sizeof(path))
continue;
root_zero = native_directory(path);
}
if((direct || root_zero) && namespace_add_root(ns, name, path) < 0)
return -1;
if(strcmp(name, "SCSI") == 0) {
for(drive = 0; drive < 8; drive++) {
int ready = 0;
if(drive == 0 && root_zero)
continue;
if(drive >= 4 &&
(_swix(SCSIFS_TestReady, _IN(1)|_OUT(0), drive,
&ready) != NULL || ready != 2))
continue;
if(add_drive_root(ns, name, drive) < 0)
return -1;
}
} else if(strcmp(name, "CDFS") == 0) {
int configured = 0;
if(_swix(CDFS_GetNumberOfDrives, _OUT(0), &configured) == NULL) {
for(drive = 1; drive < (unsigned)configured; drive++) {
if(add_drive_root(ns, name, drive) < 0)
return -1;
}
}
}
}
#else
(void)ns;
#endif
return 0;
}
static int path_allowed(const ResolvedPath *path) {
if(!path || path->synthetic || !path->native_path[0]) {
errno = EINVAL;
return 0;
}
return 1;
}
int platform_lstat(const ResolvedPath *path, struct stat *st) {
if(!path_allowed(path))
return -1;
#ifdef __riscos__
/* UnixLib lstat can block on files supplied by non-FileCore filing
* systems. Query the filing system directly for 9P metadata instead. */
char native[S9_PATH_MAX];
int type, load, exec, length, attributes;
unsigned long long centiseconds;
_kernel_swi_regs regs;
if(!__riscosify_std(path->native_path, 0, native, sizeof(native), NULL)) {
errno = EILSEQ;
return -1;
}
memset(®s, 0, sizeof(regs));
regs.r[0] = 5;
regs.r[1] = (intptr_t)native;
if(_kernel_swi(OS_File, ®s, ®s) != NULL) {
/* Some filing systems reject OS_File on their volume root but
* enumerate that root through OS_GBPB. */
char first[S9_PATH_MAX];
memset(®s, 0, sizeof(regs));
regs.r[0] = 9;
regs.r[1] = (intptr_t)native;
regs.r[2] = (intptr_t)first;
regs.r[3] = 1;
regs.r[4] = 0;
regs.r[5] = (int)sizeof(first);
if(_kernel_swi(OS_GBPB, ®s, ®s) != NULL) {
errno = EIO;
return -1;
}
type = 3;
load = exec = length = attributes = 0;
} else {
type = (int)regs.r[0];
load = (int)regs.r[2];
exec = (int)regs.r[3];
length = (int)regs.r[4];
attributes = (int)regs.r[5];
}
if(type == 0) {
errno = ENOENT;
return -1;
}
memset(st, 0, sizeof(*st));
riscos_identity_stat(native, st);
st->st_mode = type == 2 || type == 3 ? S_IFDIR : S_IFREG;
if(attributes & 1) st->st_mode |= 0400;
if(attributes & 2) st->st_mode |= 0200;
if(attributes & 16) st->st_mode |= 0044;
if(attributes & 32) st->st_mode |= 0022;
if(S_ISDIR(st->st_mode)) {
/* Some filing systems report no attributes for a volume root. */
if(!(attributes & 0x33))
st->st_mode |= 0755;
else
st->st_mode |= (st->st_mode & 0444) >> 2;
}
st->st_size = (unsigned int)length;
if(((unsigned int)load & 0xfff00000u) == 0xfff00000u) {
centiseconds = (((unsigned long long)(unsigned int)load & 0xffu) << 32) |
(unsigned int)exec;
if(centiseconds >= 220898880000ULL)
st->st_mtime = (time_t)(centiseconds / 100 - 2208988800ULL);
}
st->st_atime = st->st_mtime;
return 0;
#else
return lstat(path->native_path, st);
#endif
}
int platform_fstat(int fd, const ResolvedPath *path,
const struct stat *opened, struct stat *st) {
#ifdef __riscos__
struct stat native;
dev_t device = opened->st_dev;
ino_t inode = opened->st_ino;
if(platform_lstat(path, &native) == 0 &&
native.st_dev == device && native.st_ino == inode) {
*st = native;
return 0;
}
/* The pathname disappeared or was replaced. The descriptor still names
* the original file, but UnixLib's inode fields are not our identity. */
if(fstat(fd, st) < 0)
return -1;
st->st_dev = device;
st->st_ino = inode;
return 0;
#else
(void)path;
(void)opened;
return fstat(fd, st);
#endif
}
int platform_lstat_child(PlatformDir *directory, const ResolvedPath *path,
const char *name, struct stat *st) {
#ifdef __riscos__
(void)directory;
(void)name;
return platform_lstat(path, st);
#else
(void)path;
return fstatat(dirfd(directory), name, st, AT_SYMLINK_NOFOLLOW);
#endif
}
int platform_open(const ResolvedPath *path, int flags, mode_t mode) {
return path_allowed(path) ? open(path->native_path, flags, mode) : -1;
}
#ifdef __riscos__
struct PlatformDir {
char native[S9_PATH_MAX];
long position;
struct dirent entry;
};
#endif
PlatformDir *platform_opendir(const ResolvedPath *path) {
#ifdef __riscos__
PlatformDir *directory;
struct stat st;
/* FileSwitch skips FAT directory metadata entries that UnixLib readdir
* exposes as repeated names. Use its directory cursor directly. */
if(!path_allowed(path))
return NULL;
if(platform_lstat(path, &st) < 0 || !S_ISDIR(st.st_mode)) {
errno = ENOTDIR;
return NULL;
}
directory = calloc(1, sizeof(*directory));
if(!directory)
return NULL;
if(!__riscosify_std(path->native_path, 0, directory->native,
sizeof(directory->native), NULL)) {
free(directory);
errno = ENOTDIR;
return NULL;
}
directory->position = 0;
return directory;
#else
return path_allowed(path) ? opendir(path->native_path) : NULL;
#endif
}
int platform_fstatdir(PlatformDir *directory, struct stat *st) {
#ifdef __riscos__
(void)directory;
(void)st;
errno = ENOTSUP;
return -1;
#else
return fstat(dirfd(directory), st);
#endif
}
struct dirent *platform_readdir(PlatformDir *directory) {
#ifdef __riscos__
char name[S9_PATH_MAX];
_kernel_swi_regs regs;
_kernel_oserror *error;
size_t length;
size_t i;
const char *end;
if(directory->position == -1) {
errno = 0;
return NULL;
}
memset(®s, 0, sizeof(regs));
regs.r[0] = 9;
regs.r[1] = (intptr_t)directory->native;
regs.r[2] = (intptr_t)name;
regs.r[3] = 1;
regs.r[4] = directory->position;
regs.r[5] = (int)sizeof(name);
error = _kernel_swi(OS_GBPB, ®s, ®s);
if(error) {
errno = EIO;
return NULL;
}
directory->position = regs.r[4];
if(regs.r[3] == 0) {
errno = 0;
return NULL;
}
end = memchr(name, 0, sizeof(name));
if(!end) {
errno = EIO;
return NULL;
}
length = (size_t)(end - name);
if(length >= sizeof(directory->entry.d_name)) {
errno = ENAMETOOLONG;
return NULL;
}
memcpy(directory->entry.d_name, name, length + 1);
/* A slash in a RISC OS leaf name represents a Unix full stop. */
for(i = 0; i < length; i++) {
if(directory->entry.d_name[i] == '/')
directory->entry.d_name[i] = '.';
}
return &directory->entry;
#else
return readdir(directory);
#endif
}
long platform_telldir(PlatformDir *directory) {
#ifdef __riscos__
return directory->position;
#else
return telldir(directory);
#endif
}
void platform_seekdir(PlatformDir *directory, long position) {
#ifdef __riscos__
directory->position = position;
#else
seekdir(directory, position);
#endif
}
void platform_rewinddir(PlatformDir *directory) {
#ifdef __riscos__
directory->position = 0;
#else
rewinddir(directory);
#endif
}
int platform_closedir(PlatformDir *directory) {
#ifdef __riscos__
free(directory);
return 0;
#else
return closedir(directory);
#endif
}
ssize_t platform_readlink(const ResolvedPath *path, char *buffer, size_t size) {
return path_allowed(path) ? readlink(path->native_path, buffer, size) : -1;
}
int platform_access_execute(const ResolvedPath *path) {
return path_allowed(path) ? access(path->native_path, X_OK) : -1;
}
int platform_mkdir(const ResolvedPath *path, mode_t mode) {
return path_allowed(path) ? mkdir(path->native_path, mode) : -1;
}
int platform_symlink(const char *target, const ResolvedPath *path) {
return path_allowed(path) ? symlink(target, path->native_path) : -1;
}
int platform_mknod(const ResolvedPath *path, mode_t mode, unsigned major,
unsigned minor) {
(void)major;
(void)minor;
if(!path_allowed(path))
return -1;
if(S_ISFIFO(mode))
return mkfifo(path->native_path, mode & 07777);
errno = EOPNOTSUPP;
return -1;
}
int platform_remove(const ResolvedPath *path, int directory) {
if(!path_allowed(path))
return -1;
return directory ? rmdir(path->native_path) : unlink(path->native_path);
}
int platform_rename(const ResolvedPath *old_path,
const ResolvedPath *new_path) {
if(!path_allowed(old_path) || !path_allowed(new_path))
return -1;
#ifdef __riscos__
char old_native[S9_PATH_MAX];
char new_native[S9_PATH_MAX];
if(!__riscosify_std(old_path->native_path, 0, old_native,
sizeof(old_native), NULL) ||
!__riscosify_std(new_path->native_path, 0, new_native,
sizeof(new_native), NULL)) {
errno = EILSEQ;
return -1;
}
return riscos_identity_rename(old_native, new_native);
#else
return rename(old_path->native_path, new_path->native_path);
#endif
}
int platform_chmod(const ResolvedPath *path, mode_t mode) {
return path_allowed(path) ? chmod(path->native_path, mode) : -1;
}
int platform_chown(const ResolvedPath *path, uid_t uid, gid_t gid) {
return path_allowed(path) ? chown(path->native_path, uid, gid) : -1;
}
int platform_device_spec(const struct stat *st, char *buffer, size_t size) {
(void)st;
(void)buffer;
(void)size;
errno = EINVAL;
return -1;
}
int platform_set_times(const ResolvedPath *path, time_t atime, time_t mtime) {
struct utimbuf times;
if(!path_allowed(path))
return -1;
times.actime = atime;
times.modtime = mtime;
return utime(path->native_path, ×);
}
int platform_truncate(const ResolvedPath *path, off_t length) {
return path_allowed(path) ? truncate(path->native_path, length) : -1;
}
You can’t perform that action at this time.
