-
Notifications
You must be signed in to change notification settings - Fork 1
/
hostfs.c
2211 lines (1801 loc) · 59.7 KB
/
hostfs.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
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2005-2010 Matthew Howkins
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 2 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, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _MSC_VER
#define PATH_MAX 1024
#include "win/dirent.h" /* Thanks http://www.softagalleria.net/dirent.php! */
#include <direct.h>
#include <io.h>
#define rmdir _rmdir
#else
#include <dirent.h>
#include <unistd.h>
#endif
#if defined __unix || defined __MACH__ || defined __riscos__
#include <utime.h>
#else
#include <sys/utime.h>
#endif
#include <sys/stat.h>
#include <limits.h>
#include <stdint.h>
#ifdef AMIGA
#include <sys/syslimits.h>
#endif
#ifndef _MSC_VER
#include <stdbool.h>
#endif
#include <sys/types.h>
#ifdef __riscos__
#include "kernel.h"
#include "swis.h"
#ifdef __TARGET_UNIXLIB__ /* Set by GCC if we're using unixlib */
#include <unixlib/local.h>
int __riscosify_control = 0;
#endif
#define HOST_DIR_SEP_CHAR '.'
#define HOST_DIR_SEP_STR "."
#define NO_OPEN64
#else /* __riscos__ */
#define HOST_DIR_SEP_CHAR '/'
#define HOST_DIR_SEP_STR "/"
#endif /* !__riscos__ */
#include "hostfs.h"
#if defined NO_OPEN64 || defined __MACH__
/* ftello64/fseeko64 don't exist, but ftello/fseeko do. Likewise, we need to use regular fopen. */
#define ftello64 ftello
#define fseeko64 fseeko
#define fopen64 fopen
#define off64_t uint32_t
#endif
#ifdef HOSTFS_ARCEM
#include "arch/armarc.h"
#include "arch/ArcemConfig.h"
#include "arch/filecalls.h"
#include "ControlPane.h"
#define HOSTFS_ROOT hArcemConfig.sHostFSDirectory
#else /* HOSTFS_ARCEM */
#include "arm.h"
#include "mem.h"
static char HOSTFS_ROOT[512];
#endif /* !HOSTFS_ARCEM */
#define HOSTFS_PROTOCOL_VERSION 1
/* Windows mkdir() function only takes one argument name, and
name clashes with Posix mkdir() function taking two. This
macro allows us to use one API to work with both variants */
#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
# define mkdir(name, mode) _mkdir(name)
#endif
/* Visual Studio doesn't have ftruncate; use _chsize instead */
#if defined _MSC_VER
# define ftruncate _chsize
#endif
/** Registration states of HostFS module with backend code */
typedef enum {
HOSTFS_STATE_UNREGISTERED, /**< Module not yet registered */
HOSTFS_STATE_REGISTERED, /**< Module successfully registered */
HOSTFS_STATE_IGNORE, /**< Ignoring activity after failing to register */
} HostFSState;
enum OBJECT_TYPE {
OBJECT_TYPE_NOT_FOUND = 0,
OBJECT_TYPE_FILE = 1,
OBJECT_TYPE_DIRECTORY = 2,
OBJECT_TYPE_IMAGEFILE = 3,
};
enum OPEN_MODE {
OPEN_MODE_READ = 0,
OPEN_MODE_CREATE_OPEN_UPDATE = 1, /* Only used by RISC OS 2 */
OPEN_MODE_UPDATE = 2,
};
enum FILE_INFO_WORD {
FILE_INFO_WORD_WRITE_OK = 1U << 31,
FILE_INFO_WORD_READ_OK = 1U << 30,
FILE_INFO_WORD_IS_DIR = 1U << 29,
FILE_INFO_WORD_UNBUFFERED_OK = 1U << 28,
FILE_INFO_WORD_STREAM_INTERACTIVE = 1U << 27,
};
enum FILECORE_ERROR {
FILECORE_ERROR_BADRENAME = 0xb0,
FILECORE_ERROR_DIRNOTEMPTY = 0xb4,
FILECORE_ERROR_ACCESS = 0xbd,
FILECORE_ERROR_TOOMANYOPEN = 0xc0, /* Too many open files */
FILECORE_ERROR_OPEN = 0xc2, /* File open */
FILECORE_ERROR_LOCKED = 0xc3,
FILECORE_ERROR_EXISTS = 0xc4, /* Already exists */
FILECORE_ERROR_DISCFULL = 0xc6,
FILECORE_ERROR_NOTFOUND = 0xd6, /* Not found */
HOSTFS_ERROR_UNKNOWN = 0x100, /* Not a filecore error, but causes HostFS to return 'an unknown error occured' */
};
enum RISC_OS_FILE_TYPE {
RISC_OS_FILE_TYPE_OBEY = 0xfeb,
RISC_OS_FILE_TYPE_DATA = 0xffd,
RISC_OS_FILE_TYPE_TEXT = 0xfff,
};
typedef struct {
ARMword type;
ARMword load;
ARMword exec;
ARMword length;
ARMword attribs;
} risc_os_object_info;
/**
* Type used to cache information about a directory entry.
* Contains name and RISC OS object info
*/
typedef struct {
unsigned name_offset; /**< Offset within cache_names[] */
risc_os_object_info object_info;
} cache_directory_entry;
/* TODO Avoid duplicate macro with extnrom.c */
#define ROUND_UP_TO_4(x) (((x) + 3) & (~3))
#define STREQ(x,y) (strcmp(x,y) == 0)
#ifdef _MSC_VER
#define STRCASEEQ(x,y) (stricmp(x,y) == 0)
#else
#define STRCASEEQ(x,y) (strcasecmp(x,y) == 0)
#endif
#define MAX_OPEN_FILES 255
#define DEFAULT_ATTRIBUTES 0x03
#define DEFAULT_FILE_TYPE RISC_OS_FILE_TYPE_TEXT
#define MINIMUM_BUFFER_SIZE 32768
static FILE *open_file[MAX_OPEN_FILES + 1]; /* array subscript 0 is never used */
static unsigned char *buffer = NULL;
static size_t buffer_size = 0;
static cache_directory_entry *cache_entries = NULL;
static unsigned cache_entries_count = 0; /**< Number of valid entries in \a cache_entries */
static char *cache_names = NULL;
/** Current registration state of HostFS module with backend code */
static HostFSState hostfs_state = HOSTFS_STATE_UNREGISTERED;
#ifdef NDEBUG
static inline void dbug_hostfs(const char *format, ...) {}
#else
static void
dbug_hostfs(const char *format, ...)
{
va_list ap;
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
}
#endif
static void
path_construct(const char *old_path, const char *ro_path,
char *new_path, size_t len, ARMword load, ARMword exec);
static ARMword
errno_to_hostfs_error(const char *filename,const char *function,const char *op)
{
switch(errno) {
case ENOMEM: /* Out of memory */
hostfs_error(EXIT_FAILURE,"HostFS out of memory in %s: \'%s\'\n",function,strerror(errno));
break;
case ENOENT: /* Object not found */
return FILECORE_ERROR_NOTFOUND;
case EACCES: /* Access violation */
case EPERM:
return FILECORE_ERROR_ACCESS;
case EEXIST:
case ENOTEMPTY: /* POSIX permits either error for directory not empty */
return FILECORE_ERROR_DIRNOTEMPTY;
case ENOSPC: /* No space for the directory (either physical or quota) */
return FILECORE_ERROR_DISCFULL;
#ifdef __riscos__
case EOPSYS: /* RISC OS error */
{
_kernel_oserror *err = _kernel_last_oserror();
if(err && ((err->errnum >> 16) == 1) && ((err->errnum & 0xff) >= 0xb0))
{
/* Filesystem error, in the range HostFS will accept. Return it directly. */
return err->errnum & 0xff;
}
else
{
fprintf(stderr,"%s() could not %s '%s': %s %d\n",function,op,filename,strerror(errno),errno);
return HOSTFS_ERROR_UNKNOWN;
}
}
#endif
default:
fprintf(stderr,"%s() could not %s '%s': %s %d\n",function,op,filename,strerror(errno),errno);
return HOSTFS_ERROR_UNKNOWN;
}
}
/**
* @param buffer_size_needed Required buffer
*/
static void
hostfs_ensure_buffer_size(size_t buffer_size_needed)
{
if (buffer_size_needed > buffer_size) {
buffer = realloc(buffer, buffer_size_needed);
if (!buffer) {
hostfs_error(EXIT_FAILURE,"HostFS could not increase buffer size to %lu bytes\n",
(unsigned long) buffer_size_needed);
}
buffer_size = buffer_size_needed;
}
}
/**
* @param state Emulator state
* @param address Address in emulated memory
* @param buf Returned string (filled-in)
* @param bufsize Size of passed-in buffer
*/
static void
get_string(ARMul_State *state, ARMword address, char *buf, size_t bufsize)
{
assert(state);
assert(buf);
/* TODO Ensure we do not overrun the end of the passed-in space,
using the bufsize parameter */
while ((*buf = ARMul_LoadByte(state, address)) != '\0') {
buf++;
address++;
}
}
/**
* @param state Emulator state
* @param address Address in emulated memory
* @param str The string to store
* @return The length of the string (including terminator) rounded up to word
*/
static ARMword
put_string(ARMul_State *state, ARMword address, const char *str)
{
ARMword len = 0;
assert(state);
assert(str);
while (*str) {
ARMul_StoreByte(state, address++, *str++);
len++;
}
/* Write terminator */
ARMul_StoreByte(state, address, '\0');
return ROUND_UP_TO_4(len + 1);
}
#ifndef __riscos__
/**
* @param load RISC OS load address (assumed to be time-stamped)
* @param exec RISC OS exec address (assumed to be time-stamped)
* @return Time converted to time_t format
*
* Code adapted from fs/adfs/inode.c from Linux licensed under GPL2.
* Copyright (C) 1997-1999 Russell King
*/
static time_t
hostfs_adfs2host_time(ARMword load, ARMword exec)
{
ARMword high = load << 24;
ARMword low = exec;
high |= low >> 8;
low &= 0xff;
if (high < 0x3363996a) {
/* Too early */
return 0;
} else if (high >= 0x656e9969) {
/* Too late */
return 0x7ffffffd;
}
high -= 0x336e996a;
return (((high % 100) << 8) + low) / 100 + (high / 100 << 8);
}
#endif
/**
* Apply load/exec attributes to host file
*
* Note that on non-RISC OS hosts this only applies the time stamp; renaming
* due to filetype/load/exec changes is expected to be handled by the caller
*
* @param host_path Full path to object (file or dir) in host format
* @param load RISC OS load address (may contain time-stamp)
* @param exec RISC OS exec address (may contain time-stamp)
*/
static void
hostfs_object_set_loadexec(const char *host_path, ARMword load, ARMword exec)
{
#ifdef __riscos__
_swix(OS_File,_INR(0,2),2,host_path,load);
_swix(OS_File,_INR(0,1)|_IN(3),3,host_path,exec);
#else
/* Test if Load and Exec contain time-stamp */
if ((load & 0xfff00000u) == 0xfff00000u) {
struct utimbuf t;
t.actime = t.modtime = hostfs_adfs2host_time(load, exec);
utime(host_path, &t);
/* TODO handle error in utime() */
}
#endif
}
/**
* Apply attributes to host file
*
* @param ro_path RISC OS path to object
* @param host_path Full path to object (file or dir) in host format
* @param load RISC OS load address (may contain time-stamp)
* @param exec RISC OS exec address (may contain time-stamp)
* @param attribs RISC OS file attributes
*/
static void
hostfs_object_set_attribs(const char *ro_path, const char *host_path, ARMword load, ARMword exec,ARMword attribs)
{
#ifdef __riscos__
_swix(OS_File,_INR(0,3)|_IN(5),1,host_path,load,exec,attribs);
#else
char new_pathname[PATH_MAX];
path_construct(host_path, ro_path,
new_pathname, sizeof(new_pathname),
load, exec);
if (rename(host_path, new_pathname)) {
/* TODO handle error in renaming */
}
/* Update timestamp if necessary */
hostfs_object_set_loadexec(new_pathname, load, exec);
/* TODO handle attributes */
#endif
}
static void
riscos_path_to_host(const char *path, char *host_path)
{
assert(path);
assert(host_path);
while (*path) {
switch (*path) {
case '$':
strcpy(host_path, HOSTFS_ROOT);
host_path += strlen(host_path);
break;
#ifndef __riscos__ /* No translation needed on RISC OS */
case '.':
*host_path++ = HOST_DIR_SEP_CHAR;
break;
case HOST_DIR_SEP_CHAR:
*host_path++ = '.';
break;
#endif
default:
*host_path++ = *path;
break;
}
path++;
}
*host_path = '\0';
}
#ifndef __riscos__
/**
* @param object_name Name of Host object (file or directory)
* @param len Length of the part of the name to convert
* @param riscos_name Return object name in RISC OS format (filled-in)
*/
static void
name_host_to_riscos(const char *object_name, size_t len, char *riscos_name)
{
assert(object_name);
assert(riscos_name);
while (len--) {
switch (*object_name) {
case '.':
*riscos_name++ = HOST_DIR_SEP_CHAR;
break;
case HOST_DIR_SEP_CHAR:
*riscos_name++ = '.';
break;
case 32:
*riscos_name++ = 160;
break;
default:
*riscos_name++ = *object_name;
break;
}
object_name++;
}
*riscos_name = '\0';
}
#endif
/**
* Construct a new Host path based on an existing Host path,
* and modifying it using the leaf of the RISC OS path, and the
* load & exec addresses
*
* @param old_path Existing Host path
* @param ro_path New RISC OS path (of which the leaf will be extracted)
* @param new_path New Host path (filled-in)
* @param len Size of buffer for new Host path
* @param load RISC OS load address (may also be filetyped)
* @param exec RISC OS exec address (may also be filetyped)
*/
static void
path_construct(const char *old_path, const char *ro_path,
char *new_path, size_t len, ARMword load, ARMword exec)
{
const char *ro_leaf;
#ifndef __riscos__
char *new_suffix;
#endif
assert(old_path);
assert(ro_path);
assert(new_path);
/* TODO Ensure buffer safety is observed */
/* Start by basing new Host path on the old one */
strcpy(new_path, old_path);
/* Find the leaf of the RISC OS path */
{
const char *dot = strrchr(ro_path, '.');
/* A '.' must be present in the RISC OS path,
to prevent being passed "$" */
assert(dot);
ro_leaf = dot + 1;
}
/* Calculate where to place new leaf within the new host path */
{
char *slash, *new_leaf;
slash = strrchr(new_path, HOST_DIR_SEP_CHAR);
if (slash) {
/* New leaf immediately follows final slash */
new_leaf = slash + 1;
} else {
/* No slash currently in Host path, but we need one */
/* New leaf is then appended */
strcat(new_path, HOST_DIR_SEP_STR);
new_leaf = new_path + strlen(new_path);
}
/* Place new leaf */
riscos_path_to_host(ro_leaf, new_leaf);
}
#ifndef __riscos__
/* Calculate where to place new comma suffix */
/* New suffix appended onto existing path */
new_suffix = new_path + strlen(new_path);
if ((load & 0xfff00000u) == 0xfff00000u) {
ARMword filetype = (load >> 8) & 0xfff;
/* File has filetype and timestamp */
/* Don't set for default filetype */
if (filetype != DEFAULT_FILE_TYPE) {
sprintf(new_suffix, ",%03x", filetype);
}
} else {
/* File has load and exec addresses */
sprintf(new_suffix, ",%x-%x", load, exec);
}
#endif
}
/**
* @param host_pathname Full Host path to object
* @param ro_leaf Optionally return RISC OS leaf
* (filled-in if requested, and object found)
* @param object_info Return object info (filled-in)
*/
static void
hostfs_read_object_info(const char *host_pathname,
char *ro_leaf,
risc_os_object_info *object_info)
{
#ifdef __riscos__
assert(host_pathname);
assert(object_info);
_kernel_oserror *err = _swix(OS_File,_INR(0,1)|_OUT(0)|_OUTR(2,5),17,host_pathname,&object_info->type,&object_info->load,&object_info->exec,&object_info->length,&object_info->attribs);
if(err)
{
fprintf(stderr,"hostfs_read_object_info() could not stat '%s': %d %s\n",host_pathname,err->errnum,err->errmess);
object_info->type = OBJECT_TYPE_NOT_FOUND;
}
/* Handle image files as regular files
TODO - Have some whitelist of image file types which we want to pass through to the emulator as directories?
*/
if(object_info->type == OBJECT_TYPE_IMAGEFILE)
object_info->type = OBJECT_TYPE_FILE;
if((object_info->type != OBJECT_TYPE_NOT_FOUND) && ro_leaf)
strcpy(ro_leaf,strrchr(host_pathname,'.')+1);
#else /* __riscos__ */
struct stat info;
ARMword file_type;
bool is_timestamped = true; /* Assume initially it has timestamp/filetype */
bool truncate_name = false; /* Whether to truncate for leaf
(because filetype or load-exec found) */
const char *slash, *comma;
assert(host_pathname);
assert(object_info);
if (stat(host_pathname, &info)) {
/* Error reading info about the object */
switch (errno) {
case ENOENT: /* Object not found */
case ENOTDIR: /* A path component is not a directory */
object_info->type = OBJECT_TYPE_NOT_FOUND;
break;
default:
/* Other error */
fprintf(stderr,
"hostfs_read_object_info() could not stat() \'%s\': %s %d\n",
host_pathname, strerror(errno), errno);
object_info->type = OBJECT_TYPE_NOT_FOUND;
break;
}
return;
}
/* We were able to read about the object */
if (S_ISREG(info.st_mode)) {
object_info->type = OBJECT_TYPE_FILE;
} else if (S_ISDIR(info.st_mode)) {
object_info->type = OBJECT_TYPE_DIRECTORY;
} else {
/* Treat types other than file or directory as not found */
object_info->type = OBJECT_TYPE_NOT_FOUND;
return;
}
file_type = DEFAULT_FILE_TYPE;
/* Find where the leafname starts */
slash = strrchr(host_pathname, HOST_DIR_SEP_CHAR);
/* Find whether there is a comma in the leafname */
if (slash) {
/* Start search for comma after the slash */
comma = strrchr(slash + 1, ',');
} else {
comma = strrchr(host_pathname, ',');
}
/* Search for a filetype or load-exec after a comma */
if (comma) {
const char *dash = strrchr(comma + 1, '-');
/* Determine whether we have filetype or load-exec */
if (dash) {
/* Check the lengths of the portions before and after the dash */
if ((dash - comma - 1) >= 1 && (dash - comma - 1) <= 8 &&
strlen(dash + 1) >= 1 && strlen(dash + 1) <= 8)
{
/* Check there is no whitespace present, as sscanf() silently
ignores it */
const char *whitespace = strpbrk(comma + 1, " \f\n\r\t\v");
if (!whitespace) {
ARMword load, exec;
if (sscanf(comma + 1, "%8x-%8x", &load, &exec) == 2) {
object_info->load = load;
object_info->exec = exec;
is_timestamped = false;
truncate_name = true;
}
}
}
} else if (strlen(comma + 1) == 3) {
if (isxdigit(comma[1]) && isxdigit(comma[2]) && isxdigit(comma[3])) {
file_type = (ARMword) strtoul(comma + 1, NULL, 16);
truncate_name = true;
}
}
}
/* If the file has timestamp/filetype, instead of load-exec, then fill in */
if (is_timestamped) {
ARMword low = (ARMword) ((info.st_mtime & 255) * 100);
ARMword high = (ARMword) ((info.st_mtime / 256) * 100 + (low >> 8) + 0x336e996a);
object_info->load = 0xfff00000 | (file_type << 8) | (high >> 24);
object_info->exec = (low & 255) | (high << 8);
}
object_info->length = info.st_size;
object_info->attribs = DEFAULT_ATTRIBUTES;
if (ro_leaf) {
/* Allocate and return leafname for RISC OS */
size_t ro_leaf_len;
if (truncate_name) {
/* If a filetype or load-exec was found, we only want the part from after
the slash to before the comma */
ro_leaf_len = comma - slash - 1;
} else {
/* Return everything from after the slash to the end */
ro_leaf_len = strlen(slash + 1);
}
name_host_to_riscos(slash + 1, ro_leaf_len, ro_leaf);
}
#endif /* !__riscos__ */
}
/**
* @param host_dir_path Full Host path to directory to scan
* @param object Object name to search for
* @param host_name Return Host name of object (filled-in if object found)
* @param object_info Return object info (filled-in)
*/
static void
hostfs_path_scan(const char *host_dir_path,
const char *object,
char *host_name,
risc_os_object_info *object_info)
{
#ifdef __riscos__
char path[PATH_MAX];
assert(host_dir_path && object);
assert(host_name);
assert(object_info);
/* This is nice and easy */
sprintf(path,"%s.%s",host_dir_path,object);
hostfs_read_object_info(path,NULL,object_info);
if(object_info->type != OBJECT_TYPE_NOT_FOUND)
strcpy(host_name,object);
#else /* __riscos__ */
DIR *d;
struct dirent *entry;
size_t c;
assert(host_dir_path && object);
assert(host_name);
assert(object_info);
d = opendir(host_dir_path);
if (!d) {
switch (errno) {
case ENOENT: /* Object not found */
case ENOTDIR: /* Object not a directory */
object_info->type = OBJECT_TYPE_NOT_FOUND;
break;
default:
fprintf(stderr, "hostfs_path_scan() could not opendir() \'%s\': %s %d\n",
host_dir_path, strerror(errno), errno);
object_info->type = OBJECT_TYPE_NOT_FOUND;
}
return;
}
while ((entry = readdir(d)) != NULL) {
char entry_path[PATH_MAX], ro_leaf[PATH_MAX];
/* Ignore the current directory and it's parent */
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
continue;
}
strcpy(entry_path, host_dir_path);
strcat(entry_path, HOST_DIR_SEP_STR);
strcat(entry_path, entry->d_name);
hostfs_read_object_info(entry_path, ro_leaf, object_info);
for (c=0;c<strlen(ro_leaf);c++)
{
if (ro_leaf[c]==HOST_DIR_SEP_CHAR)
ro_leaf[c]='.';
}
/* Ignore entries we can not read information about,
or which are neither regular files or directories */
if (object_info->type == OBJECT_TYPE_NOT_FOUND) {
continue;
}
/* Compare leaf and object names in case-insensitive manner */
if (!STRCASEEQ(ro_leaf, object)) {
/* Names do not match */
continue;
}
/* A match has been found - exit the function early */
strcpy(host_name, entry->d_name);
closedir(d);
return;
}
closedir(d);
object_info->type = OBJECT_TYPE_NOT_FOUND;
#endif /* !__riscos__ */
}
/**
* @param ro_path Full RISC OS path to object
* @param host_pathname Return full Host path to object (filled-in)
* @param object_info Return object info (filled-in)
*/
static void
hostfs_path_process(const char *ro_path,
char *host_pathname,
risc_os_object_info *object_info)
{
char component_name[PATH_MAX]; /* working Host component */
char *component;
assert(ro_path);
assert(host_pathname);
assert(object_info);
assert(ro_path[0] == '$');
/* Initialise Host pathname */
host_pathname[0] = '\0';
/* Initialise working Host component */
component = &component_name[0];
*component = '\0';
while (*ro_path) {
switch (*ro_path) {
case '$':
strcat(host_pathname, HOSTFS_ROOT);
hostfs_read_object_info(host_pathname, NULL, object_info);
if (object_info->type == OBJECT_TYPE_NOT_FOUND) {
return;
}
break;
case '.':
if (component_name[0] != '\0') {
/* only if not first dot, i.e. "$." */
char host_name[PATH_MAX];
*component = '\0'; /* add terminator */
hostfs_path_scan(host_pathname, component_name,
host_name, object_info);
if (object_info->type == OBJECT_TYPE_NOT_FOUND) {
/* This component of the path is invalid */
/* Return what we have of the host_pathname */
return;
}
/* Append Host's name for this component to the working Host path */
strcat(host_pathname, HOST_DIR_SEP_STR);
strcat(host_pathname, host_name);
/* Reset component name ready for re-use */
component = &component_name[0];
*component = '\0';
}
break;
#ifndef __riscos__
case HOST_DIR_SEP_CHAR:
*component++ = '.';
break;
#endif
default:
*component++ = *ro_path;
break;
}
ro_path++;
}
if (component_name[0] != '\0') {
/* only if not first dot, i.e. "$." */
char host_name[PATH_MAX];
*component = '\0'; /* add terminator */
hostfs_path_scan(host_pathname, component_name,
host_name, object_info);
if (object_info->type == OBJECT_TYPE_NOT_FOUND) {
/* This component of the path is invalid */
/* Return what we have of the host_pathname */
return;
}
/* Append Host's name for this component to the working Host path */
strcat(host_pathname, HOST_DIR_SEP_STR);
strcat(host_pathname, host_name);
}
}
/* Search through the open_file[] array, and allocate an index.
A valid index will be >0 and <=MAX_OPEN_FILES
A return of 0 indicates that no array index could be allocated.
*/
static unsigned
hostfs_open_allocate_index(void)
{
unsigned i;
/* TODO Use the buffer in a circular manner for improved performance */
/* Start our search at array index 1.
Reserve a return of 0 for a special meaning: no free entry */
for (i = 1; i < (MAX_OPEN_FILES + 1); i++) {
if (open_file[i] == NULL) {
return i;
}
}
return 0;
}
static void
hostfs_open(ARMul_State *state)
{
char ro_path[PATH_MAX], host_pathname[PATH_MAX];
risc_os_object_info object_info;
unsigned idx;
assert(state);
dbug_hostfs("Open\n");
dbug_hostfs("\tr1 = 0x%08x (ptr to pathname)\n", state->Reg[1]);
dbug_hostfs("\tr3 = %u (FileSwitch handle)\n", state->Reg[3]);
dbug_hostfs("\tr6 = 0x%08x (pointer to special field if present)\n",
state->Reg[6]);
get_string(state, state->Reg[1], ro_path, sizeof(ro_path));
dbug_hostfs("\tPATH = %s\n", ro_path);
hostfs_path_process(ro_path, host_pathname, &object_info);
if (object_info.type == OBJECT_TYPE_NOT_FOUND) {
/* FIXME RISC OS uses this to create files - not therefore an error if not found */
state->Reg[1] = 0; /* Signal to RISC OS file not found */
return;
}
/* TODO Handle the case that a file exists to be replaced, (and the filetype is
not data - the recommeded default for new files) */
idx = hostfs_open_allocate_index();
if (idx == 0) {
/* No more space in the open_file[] array.
This should never occur, because RISC OS is constraining the max
number of open files */
abort();
}
switch (state->Reg[0]) {
case OPEN_MODE_READ:
dbug_hostfs("\tOpen for read\n");
open_file[idx] = fopen64(host_pathname, "rb");
state->Reg[0] = FILE_INFO_WORD_READ_OK;
break;
case OPEN_MODE_CREATE_OPEN_UPDATE:
dbug_hostfs("\tCreate and open for update (only RISC OS 2)\n");
return;
case OPEN_MODE_UPDATE:
dbug_hostfs("\tOpen for update\n");
open_file[idx] = fopen64(host_pathname, "rb+");
state->Reg[0] = (uint32_t) (FILE_INFO_WORD_READ_OK | FILE_INFO_WORD_WRITE_OK);
break;
}