-
Notifications
You must be signed in to change notification settings - Fork 67
/
FileAttributesPlugin.c
1249 lines (1081 loc) · 34.2 KB
/
FileAttributesPlugin.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
/* Automatically generated by
VMPluginCodeGenerator VMMaker.oscog-eem.2504 uuid: a00b0fad-c04c-47a6-8a11-5dbff110ac11
from
FileAttributesPlugin FileAttributesPlugin.oscog-akg.49 uuid: 00b4eed5-b2a8-454a-90b7-c3662d7c5423
*/
static char __buildInfo[] = "FileAttributesPlugin FileAttributesPlugin.oscog-akg.49 uuid: 00b4eed5-b2a8-454a-90b7-c3662d7c5423 " __DATE__ ;
#include "config.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <limits.h>
#include <sys/types.h>
#if !defined(_WIN32)
#include <unistd.h>
#endif
#include <dirent.h>
#if _WIN32 || _WIN64
# include <windows.h>
# include <winbase.h>
# define FAIL() { return -1; }
# include "sqWin32File.h"
# if !defined(PATH_MAX) /* work around bug in 64-bit cygwin; sigh :-( */
# define PATH_MAX 4096
# endif
# define HAVE_CHMOD 0
# define HAVE_CHOWN 0
#else
#define HAVE_CHMOD 1
#define HAVE_CHOWN 1
#endif
#include <sys/stat.h>
#if !defined(HAVE_LSTAT) && !defined(_WIN32) && !defined(_WIN64)
# define HAVE_LSTAT 1
#endif
#if !defined(_WIN32)
#include <unistd.h>
#endif
#include <pharovm/debug.h>
/* AKG 2018 - FileAttributesPlugin.c translated from class FileAttributesPlugin */
/* Do not include the entire sq.h file but just those parts needed. */
#include "sqConfig.h" /* Configuration options */
#include "sqVirtualMachine.h" /* The virtual machine proxy definition */
#include "sqPlatformSpecific.h" /* Platform specific definitions */
#define true 1
#define false 0
#define null 0 /* using 'null' because nil is predefined in Think C */
#ifdef SQUEAK_BUILTIN_PLUGIN
# undef EXPORT
# define EXPORT(returnType) static returnType
#endif
#include "faCommon.h"
#include "sqMemoryAccess.h"
/*** Constants ***/
#define PrimErrBadArgument 3
#define PrimErrNoCMemory 10
#define PrimErrNoMemory 9
/*** Function Prototypes ***/
static sqInt addressObjectFor(void *aMachineAddress);
static sqInt attributeArrayformask(sqInt *attributeArrayPtr, fapath *faPath, sqInt attributeMask);
static sqInt badSessionId(void);
#if _WIN32
static sqLong convertWinToSqueakTime(SYSTEMTIME st);
#endif /* _WIN32 */
static sqInt faSuccess(void);
EXPORT(const char*) getModuleName(void);
EXPORT(sqInt) initialiseModule(void);
static sqInt objectFromStructsize(void *aMachineAddress, sqInt structSize);
static int pathOoptoBuffermaxLen(sqInt pathNameOop, char *cPathName, sqInt maxLen);
static void * pointerFrom(sqInt directoryPointerBytes);
EXPORT(sqInt) primitiveChangeMode(void);
EXPORT(sqInt) primitiveChangeOwner(void);
EXPORT(sqInt) primitiveClosedir(void);
EXPORT(sqInt) primitiveFileAttribute(void);
EXPORT(sqInt) primitiveFileAttributes(void);
EXPORT(sqInt) primitiveFileExists(void);
EXPORT(sqInt) primitiveFileMasks(void);
EXPORT(sqInt) primitiveLogicalDrives(void);
EXPORT(sqInt) primitiveOpendir(void);
EXPORT(sqInt) primitivePathMax(void);
EXPORT(sqInt) primitivePlatToStPath(void);
EXPORT(sqInt) primitiveReaddir(void);
EXPORT(sqInt) primitiveRewinddir(void);
EXPORT(sqInt) primitiveStToPlatPath(void);
EXPORT(sqInt) primitiveSymlinkChangeOwner(void);
EXPORT(sqInt) primitiveVersionString(void);
static sqInt processDirectory(fapath *faPath);
static sqInt readLinkintomaxLength(char *cPathName, char *cLinkPtr, size_t maxLength);
EXPORT(sqInt) setInterpreter(struct VirtualMachine *anInterpreter);
static sqInt sizeOfFaPath(void);
static sqInt sizeOfFaPathPtr(void);
static sqInt stringFromCString(const char *aCString);
#if !defined(SQUEAK_BUILTIN_PLUGIN)
static void * (*arrayValueOf)(sqInt oop);
static sqInt (*classArray)(void);
static sqInt (*classByteArray)(void);
static sqInt (*classString)(void);
static sqInt (*failed)(void);
static sqInt (*instantiateClassindexableSize)(sqInt classPointer, sqInt size);
static sqInt (*integerObjectOf)(sqInt value);
static void * (*ioLoadFunctionFrom)(char *functionName, char *moduleName);
static sqInt (*isKindOf)(sqInt oop, char *aString);
static sqInt (*isBytes)(sqInt oop);
static sqInt (*methodReturnValue)(sqInt oop);
static sqInt (*nilObject)(void);
static sqInt (*popthenPush)(sqInt nItems, sqInt oop);
static sqInt (*popRemappableOop)(void);
static sqInt (*positive32BitIntegerFor)(unsigned int integerValue);
static sqInt (*primitiveFail)(void);
static sqInt (*primitiveFailFor)(sqInt reasonCode);
static sqInt (*primitiveFailForOSError)(sqLong osError);
static sqInt (*primitiveFailureCode)(void);
static sqInt (*pushRemappableOop)(sqInt oop);
static sqInt (*stSizeOf)(sqInt oop);
static sqInt (*stackIntegerValue)(sqInt offset);
static sqInt (*stackObjectValue)(sqInt offset);
static sqInt (*stackValue)(sqInt offset);
static sqInt (*storePointerofObjectwithValue)(sqInt index, sqInt oop, sqInt valuePointer);
#else /* !defined(SQUEAK_BUILTIN_PLUGIN) */
extern void * arrayValueOf(sqInt oop);
extern sqInt classArray(void);
extern sqInt classByteArray(void);
extern sqInt classString(void);
extern sqInt failed(void);
extern sqInt instantiateClassindexableSize(sqInt classPointer, sqInt size);
extern sqInt integerObjectOf(sqInt value);
extern void * ioLoadFunctionFrom(char *functionName, char *moduleName);
extern sqInt isKindOf(sqInt oop, char *aString);
extern sqInt isBytes(sqInt oop);
extern sqInt methodReturnValue(sqInt oop);
extern sqInt nilObject(void);
extern sqInt popthenPush(sqInt nItems, sqInt oop);
extern sqInt popRemappableOop(void);
extern sqInt positive32BitIntegerFor(unsigned int integerValue);
extern sqInt primitiveFail(void);
extern sqInt primitiveFailFor(sqInt reasonCode);
#if VM_PROXY_MAJOR > 1 || (VM_PROXY_MAJOR == 1 && VM_PROXY_MINOR >= 14)
extern sqInt primitiveFailForOSError(sqLong osError);
#else
# define primitiveFailForOSError(osError) 0
#endif
extern sqInt primitiveFailureCode(void);
extern sqInt pushRemappableOop(sqInt oop);
extern sqInt stSizeOf(sqInt oop);
extern sqInt stackIntegerValue(sqInt offset);
extern sqInt stackObjectValue(sqInt offset);
extern sqInt stackValue(sqInt offset);
extern sqInt storePointerofObjectwithValue(sqInt index, sqInt oop, sqInt valuePointer);
extern
#endif
struct VirtualMachine* interpreterProxy;
static const char *moduleName =
#ifdef SQUEAK_BUILTIN_PLUGIN
"FileAttributesPlugin FileAttributesPlugin.oscog-akg.49 (i)"
#else
"FileAttributesPlugin FileAttributesPlugin.oscog-akg.49 (e)"
#endif
;
/* Answer an ExternalAddress object which represents aMachineAddress */
/* FileAttributesPlugin>>#addressObjectFor: */
static sqInt
addressObjectFor(void *aMachineAddress)
{
sqInt addressOop;
unsigned char *addressOopArrayPointer;
union {void *address; unsigned char bytes[sizeof(void *)];} addressUnion;
sqInt idx;
addressOop = instantiateClassindexableSize(classByteArray(), sizeof(void *));
if (!(addressOop)) {
return primitiveFailFor(PrimErrNoMemory);
}
addressOopArrayPointer = arrayValueOf(addressOop);
addressUnion.address = aMachineAddress;
idx = 0;
while (idx < (sizeof(void *))) {
addressOopArrayPointer[idx] = addressUnion.bytes[idx];
idx += 1;
}
return addressOop;
}
/* Create the attributes array for the specified file (faPath) and set
attributeArrayPtr. Which attributes are retrieved are specified in
attributeMask. On error, set the error in interpreterProxy and answer the
appropriate status (some callers check the status, others
interpreterProxy)
*/
/* FileAttributesPlugin>>#attributeArray:for:mask: */
static sqInt
attributeArrayformask(sqInt *attributeArrayPtr, fapath *faPath, sqInt attributeMask)
{
sqInt accessArray;
sqInt attributeArray;
int getAccess;
int getLinkStats;
int getStats;
sqInt resultOop;
sqInt status;
/* Determine which attributes to retrieve */
accessArray = 0;
attributeArray = 0;
resultOop = 0;
getStats = attributeMask & 1;
getAccess = attributeMask & 2;
if (!(getStats
|| (getAccess))) {
/* No information has been requested, which doesn't make sense */
primitiveFailForOSError(-6 /* invalidArguments */);
return -6 /* invalidArguments */;
}
getLinkStats = attributeMask & 4;
if (getStats) {
attributeArray = instantiateClassindexableSize(classArray(), 13);
if (!(attributeArray)) {
primitiveFailFor(PrimErrNoMemory);
return -15 /* interpreterError */;
}
#if SPURVM
status = faFileStatAttributes(faPath, getLinkStats, attributeArray);
#else /* SPURVM */
pushRemappableOop(attributeArray);
status = faFileStatAttributes(faPath, getLinkStats, attributeArray);
attributeArray = popRemappableOop()
#endif /* SPURVM */
;
if (status != 0) {
return status;
}
resultOop = attributeArray;
}
if (getAccess) {
#if SPURVM
accessArray = instantiateClassindexableSize(classArray(), 3);
#else /* SPURVM */
pushRemappableOop(attributeArray);
accessArray = instantiateClassindexableSize(classArray(), 3);
attributeArray = popRemappableOop()
#endif /* SPURVM */
;
if (!(accessArray)) {
primitiveFailFor(PrimErrNoMemory);
/* interpreterError */
}
faAccessAttributes(faPath, accessArray, 0);
if (failed()) {
return -15 /* interpreterError */;
}
resultOop = accessArray;
}
if (getStats
&& (getAccess)) {
#if SPURVM
resultOop = instantiateClassindexableSize(classArray(), 2);
#else /* SPURVM */
pushRemappableOop(attributeArray);
pushRemappableOop(accessArray);
resultOop = instantiateClassindexableSize(classArray(), 2);
accessArray = popRemappableOop();
attributeArray = popRemappableOop()
#endif /* SPURVM */
;
if (!(resultOop)) {
primitiveFailFor(PrimErrNoMemory);
/* interpreterError */
}
storePointerofObjectwithValue(0, resultOop, attributeArray);
storePointerofObjectwithValue(1, resultOop, accessArray);
}
attributeArrayPtr[0] = resultOop;
return 0 /* faSuccess */;
}
/* The supplied session Id doesn't match the current VM session Id */
/* FileAttributesPlugin>>#badSessionId */
static sqInt
badSessionId(void)
{
return -17;
}
/* Convert the supplied Windows SYSTEMTIME to Squeak time */
/* FileAttributesPlugin>>#convertWinToSqueakTime: */
#if _WIN32
static sqLong
convertWinToSqueakTime(SYSTEMTIME st)
{
sqLong dy;
static sqLong nDaysPerMonth[14] = {
0, 0, 31, 59, 90, 120, 151,
181, 212, 243, 273, 304, 334, 365 };
sqLong secs;
dy = (st.wYear) - 1901;
/* base seconds */
/* seconds of leap years */
/* check if month > 2 and current year is a leap year */
secs = ((((dy * 365) * 24) * 60) * 60) + ((((((usqInt) dy >> 2)) * 24) * 60) * 60);
if (((st.wMonth) > 2)
&& ((dy & 3) == 3)) {
/* add one day */
secs += (24 * 60) * 60;
}
/* add the hours, minutes, and seconds */
secs += (nDaysPerMonth[st.wMonth] + st.wDay - 1) * 24 * 60 * 60;
secs += st.wSecond + 60*(st.wMinute + 60*st.wHour);
return secs;
}
#endif /* _WIN32 */
/* FileAttributesPlugin>>#faSuccess */
static sqInt
faSuccess(void)
{
return 0;
}
/* Note: This is hardcoded so it can be run from Squeak.
The module name is used for validating a module *after*
it is loaded to check if it does really contain the module
we're thinking it contains. This is important! */
/* InterpreterPlugin>>#getModuleName */
EXPORT(const char*)
getModuleName(void)
{
return moduleName;
}
/* FileAttributesPlugin>>#initialiseModule */
EXPORT(sqInt)
initialiseModule(void)
{
return 1;
}
/* Answer a ByteArray object which copies the structure at aMachineAddress */
/* FileAttributesPlugin>>#objectFromStruct:size: */
static sqInt
objectFromStructsize(void *aMachineAddress, sqInt structSize)
{
sqInt addressOop;
unsigned char *addressOopArrayPointer;
addressOop = instantiateClassindexableSize(classByteArray(), structSize);
if (!(addressOop)) {
return primitiveFailFor(PrimErrNoMemory);
}
addressOopArrayPointer = arrayValueOf(addressOop);
memcpy(addressOopArrayPointer, aMachineAddress, structSize);
return addressOop;
}
/* Copy the supplied path name string object to the supplied c string buffer */
/* FileAttributesPlugin>>#pathOop:toBuffer:maxLen: */
static int
pathOoptoBuffermaxLen(sqInt pathNameOop, char *cPathName, sqInt maxLen)
{
sqInt len;
char *sPtr;
len = stSizeOf(pathNameOop);
if (len >= maxLen) {
return -1 /* stringTooLong */;
}
sPtr = arrayValueOf(pathNameOop);
memcpy(cPathName, sPtr, len);
cPathName[len] = 0;
return 0;
}
/* Answer the machine address contained in anExternalAddressOop. */
/* FileAttributesPlugin>>#pointerFrom: */
static void *
pointerFrom(sqInt directoryPointerBytes)
{
union {void *address; unsigned char bytes[sizeof(void *)];} addressUnion;
sqInt idx;
unsigned char *ptr;
if (!((isKindOf(directoryPointerBytes, "ByteArray"))
&& ((stSizeOf(directoryPointerBytes)) == (sizeof(void *))))) {
return null;
}
ptr = arrayValueOf(directoryPointerBytes);
idx = 0;
while (idx < (sizeof(void *))) {
addressUnion.bytes[idx] = ptr[idx];
idx += 1;
}
return addressUnion.address;
}
/* Set the mode of the supplied file using chmod(). */
/* FileAttributesPlugin>>#primitiveChangeMode */
EXPORT(sqInt)
primitiveChangeMode(void)
{
fapath faPath;
sqInt fileNameOop;
sqInt newMode;
sqInt status;
fileNameOop = stackObjectValue(1);
newMode = stackIntegerValue(0);
if ((failed())
|| (!(isBytes(fileNameOop)))) {
return primitiveFailFor(PrimErrBadArgument);
}
# if HAVE_CHMOD
faSetStPathOop((&faPath), fileNameOop);
if (failed()) {
return primitiveFailureCode();
}
status = chmod(faGetPlatPath((&faPath)), newMode);
if (status != 0) {
return primitiveFailForOSError(errno);
}
return methodReturnValue(nilObject());
# endif /* HAVE_CHMOD */
return primitiveFailForOSError(-13 /* unsupportedOperation */);
}
/* Set the owner of the supplied file using chown(). */
/* FileAttributesPlugin>>#primitiveChangeOwner */
EXPORT(sqInt)
primitiveChangeOwner(void)
{
fapath faPath;
sqInt fileNameOop;
sqInt groupId;
sqInt ownerId;
sqInt status;
fileNameOop = stackObjectValue(2);
ownerId = stackIntegerValue(1);
groupId = stackIntegerValue(0);
if ((failed())
|| (!(isBytes(fileNameOop)))) {
return primitiveFailFor(PrimErrBadArgument);
}
# if HAVE_CHOWN
faSetStPathOop((&faPath), fileNameOop);
if (failed()) {
return primitiveFailureCode();
}
status = chown(faGetPlatPath((&faPath)), ownerId, groupId);
if (status != 0) {
return primitiveFailForOSError(errno);
}
return methodReturnValue(nilObject());
# endif /* HAVE_CHOWN */
return primitiveFailForOSError(-13 /* unsupportedOperation */);
}
/* Close the directory stream for dirPointerOop. Answer dirPointerOop on
success. Raise PrimErrBadArgument if the parameter is not a ByteArray
length size(void *).
If closedir() returns an error raise PrimitiveOSError.
*/
/* FileAttributesPlugin>>#primitiveClosedir */
EXPORT(sqInt)
primitiveClosedir(void)
{
void *buffer;
sqInt dirPointerOop;
fapath *faPath;
FAPathPtr *faPathPtr;
sqInt result;
sqInt structSize;
dirPointerOop = stackValue(0);
/* begin structFromObject:size: */
structSize = sizeof(FAPathPtr);
if (!((stSizeOf(dirPointerOop)) == structSize)) {
primitiveFailFor(PrimErrBadArgument);
faPathPtr = ((void *) 0);
goto l1;
}
buffer = alloca(structSize);
if (buffer == 0) {
primitiveFailFor(PrimErrNoCMemory);
}
else {
memcpy(buffer, arrayValueOf(dirPointerOop), structSize);
}
faPathPtr = buffer;
l1: /* end structFromObject:size: */;
if (faPathPtr == 0) {
return primitiveFailFor(PrimErrBadArgument);
}
if (!(faValidateSessionId((faPathPtr->sessionId)))) {
return primitiveFailForOSError(-17 /* badSessionId */);
}
faPath = (faPathPtr->faPath);
result = faCloseDirectory(faPath);
faInvalidateSessionId(&faPathPtr->sessionId);
if (!(result == 0)) {
return primitiveFailForOSError(result);
}
free(faPath);
methodReturnValue(dirPointerOop);
return 0;
}
/* Answer a single file attribute.
primFileAttributes: aString attributeNumber: attributeNumber
aString is the path to the file
attributeNumber identifies which attribute to return:
1 - 12: stat(): name, mode, ino, dev, nlink, uid, gid, size, accessDate,
modifiedDate, changeDate, creationDate
13 - 15: access(): R_OK, W_OK, X_OK
16: isSymlink
On error, answer a single element array containing the appropriate error
code.
*/
/* FileAttributesPlugin>>#primitiveFileAttribute */
EXPORT(sqInt)
primitiveFileAttribute(void)
{
sqInt attributeNumber;
fapath faPath;
sqInt fileName;
sqInt resultOop;
fileName = stackObjectValue(1);
attributeNumber = stackIntegerValue(0);
if ((failed())
|| ((!(((attributeNumber >= 1) && (attributeNumber <= 16))))
|| (!(isBytes(fileName))))) {
return primitiveFailFor(PrimErrBadArgument);
}
faSetStPathOop((&faPath), fileName);
if (failed()) {
return primitiveFailureCode();
}
resultOop = faFileAttribute((&faPath), attributeNumber);
if (failed()) {
return primitiveFailureCode();
}
if (resultOop == 0) {
/* It shouldn't be possible to get here */
primitiveFailForOSError(-14 /* unexpectedError */);
}
else {
methodReturnValue(resultOop);
}
return 0;
}
/* Answer an array of file attributes.
primFileAttributes: aString mask: attributeMask
aString is the path to the file
attributeMask indicates which attributes to retrieve:
bit 0 - get stat() attributes
bit 1 - get access() attributes
bit 2 - use lstat() instead of stat()
On error answer the appropriate error code (Integer) */
/* FileAttributesPlugin>>#primitiveFileAttributes */
EXPORT(sqInt)
primitiveFileAttributes(void)
{
sqInt attributeMask;
fapath faPath;
sqInt fileName;
sqInt resultOop;
sqInt status;
sqInt val;
resultOop = 0;
val = 0;
fileName = stackObjectValue(1);
attributeMask = stackIntegerValue(0);
if ((failed())
|| (!(isBytes(fileName)))) {
return primitiveFailFor(PrimErrBadArgument);
}
faSetStPathOop((&faPath), fileName);
if (failed()) {
return primitiveFailureCode();
}
status = attributeArrayformask((&resultOop), (&faPath), attributeMask);
if (status != 0) {
return primitiveFailForOSError(status);
}
return methodReturnValue(resultOop);
}
/* Check for existence of a file with a call to access(). */
/* FileAttributesPlugin>>#primitiveFileExists */
EXPORT(sqInt)
primitiveFileExists(void)
{
fapath faPath;
sqInt fileNameOop;
sqInt resultOop;
fileNameOop = stackObjectValue(0);
if (!(isBytes(fileNameOop))) {
return primitiveFailFor(PrimErrBadArgument);
}
faSetStPathOop((&faPath), fileNameOop);
if (failed()) {
return primitiveFailureCode();
}
resultOop = faExists((&faPath));
return methodReturnValue(resultOop);
}
/* Answer an array of well known file masks */
/* FileAttributesPlugin>>#primitiveFileMasks */
EXPORT(sqInt)
primitiveFileMasks(void)
{
sqInt masks;
masks = instantiateClassindexableSize(classArray(), 8);
if (!(masks)) {
return primitiveFailFor(PrimErrNoMemory);
}
storePointerofObjectwithValue(0, masks, positive32BitIntegerFor(S_IFMT));
# if !(defined(_WIN32))
storePointerofObjectwithValue(1, masks, positive32BitIntegerFor(S_IFSOCK));
storePointerofObjectwithValue(2, masks, positive32BitIntegerFor(S_IFLNK));
# endif /* !(defined(_WIN32)) */
storePointerofObjectwithValue(3, masks, positive32BitIntegerFor(S_IFREG));
storePointerofObjectwithValue(4, masks, positive32BitIntegerFor(S_IFBLK));
storePointerofObjectwithValue(5, masks, positive32BitIntegerFor(S_IFDIR));
storePointerofObjectwithValue(6, masks, positive32BitIntegerFor(S_IFCHR));
storePointerofObjectwithValue(7, masks, positive32BitIntegerFor(S_IFIFO));
popthenPush(1, masks);
return 0;
}
/* Answer the logical drive mask on windows */
/* FileAttributesPlugin>>#primitiveLogicalDrives */
EXPORT(sqInt)
primitiveLogicalDrives(void)
{
unsigned int mask;
# if defined(_WIN32)
mask = GetLogicalDrives();
if (mask != 0) {
return popthenPush(1, positive32BitIntegerFor(mask));
}
# endif /* defined(_WIN32) */
primitiveFail();
return 0;
}
/* Open the supplied directory and answer the first entry and directory
pointer. If the directory is empty, answer nil as the first entry.
If the directory can't be opened, answer an error (cantOpenDir) */
/* self primOpendir: '/etc' */
/* FileAttributesPlugin>>#primitiveOpendir */
EXPORT(sqInt)
primitiveOpendir(void)
{
sqInt addressOop;
unsigned char *addressOopArrayPointer;
void *aMachineAddress;
sqInt dirName;
sqInt dirOop;
fapath *faPath;
FAPathPtr faPathPtr;
sqInt resultOop;
sqInt status;
sqInt structSize;
/* Process the parameters */
dirName = stackObjectValue(0);
if (!(isBytes(dirName))) {
return primitiveFailFor(PrimErrBadArgument);
}
faPath = (fapath *) calloc(1, sizeof(fapath));
if (faPath == null) {
return primitiveFailForOSError(-10 /* cantAllocateMemory */);
}
faSetStDirOop(faPath, dirName);
if (failed()) {
return primitiveFailureCode();
}
status = faOpenDirectory(faPath);
if (status == 1 /* noMoreData */) {
free(faPath);
return methodReturnValue(nilObject());
}
if (status < 0) {
free(faPath);
return primitiveFailForOSError(status);
}
resultOop = processDirectory(faPath);
if (failed()) {
free(faPath);
return primitiveFailureCode();
}
faInitSessionId(&faPathPtr.sessionId);
(faPathPtr.faPath = faPath);
#if SPURVM
/* begin objectFromStruct:size: */
aMachineAddress = (&faPathPtr);
structSize = sizeOfFaPathPtr();
addressOop = instantiateClassindexableSize(classByteArray(), structSize);
if (!(addressOop)) {
dirOop = primitiveFailFor(PrimErrNoMemory);
goto l1;
}
addressOopArrayPointer = arrayValueOf(addressOop);
memcpy(addressOopArrayPointer, aMachineAddress, structSize);
dirOop = addressOop;
l1: /* end objectFromStruct:size: */;
#else /* SPURVM */
pushRemappableOop(resultOop);
/* begin objectFromStruct:size: */
aMachineAddress = (&faPathPtr);
structSize = sizeOfFaPathPtr();
addressOop = instantiateClassindexableSize(classByteArray(), structSize);
if (!(addressOop)) {
dirOop = primitiveFailFor(PrimErrNoMemory);
goto l1;
}
addressOopArrayPointer = arrayValueOf(addressOop);
memcpy(addressOopArrayPointer, aMachineAddress, structSize);
dirOop = addressOop;
l1: /* end objectFromStruct:size: */;
resultOop = popRemappableOop()
#endif /* SPURVM */
;
return (storePointerofObjectwithValue(2, resultOop, dirOop),
methodReturnValue(resultOop));
}
/* Answer the value of PATH_MAX for the current VM */
/* FileAttributesPlugin>>#primitivePathMax */
EXPORT(sqInt)
primitivePathMax(void)
{
return popthenPush(1, integerObjectOf(FA_PATH_MAX));
}
/* Convert the supplied file name (platform encoded) to the St UTF8 encoded
byte array
*/
/* FileAttributesPlugin>>#primitivePlatToStPath */
EXPORT(sqInt)
primitivePlatToStPath(void)
{
unsigned char *byteArrayPtr;
fapath faPath;
sqInt fileName;
sqInt resultOop;
fileName = stackObjectValue(0);
if ((failed())
|| (!(isBytes(fileName)))) {
return primitiveFailFor(PrimErrBadArgument);
}
faSetPlatPathOop((&faPath), fileName);
if (failed()) {
return primitiveFailureCode();
}
resultOop = instantiateClassindexableSize(classByteArray(), faGetStPathLen((&faPath)));
if (!(resultOop)) {
return primitiveFailFor(PrimErrNoMemory);
}
byteArrayPtr = arrayValueOf(resultOop);
memcpy(byteArrayPtr, faGetStPath((&faPath)), faGetStPathLen((&faPath)));
return methodReturnValue(resultOop);
}
/* Get the next entry in the directory stream. Answer the name of the entry,
or nil for the end of the directory stream.
Arguments:
- directoryPointer (ByteArray) */
/* FileAttributesPlugin>>#primitiveReaddir */
EXPORT(sqInt)
primitiveReaddir(void)
{
void *buffer;
sqInt dirPointerOop;
fapath *faPath;
FAPathPtr *faPathPtr;
sqInt resultArray;
sqInt status;
sqInt structSize;
dirPointerOop = stackValue(0);
/* begin structFromObject:size: */
structSize = sizeof(FAPathPtr);
if (!((stSizeOf(dirPointerOop)) == structSize)) {
primitiveFailFor(PrimErrBadArgument);
faPathPtr = ((void *) 0);
goto l1;
}
buffer = alloca(structSize);
if (buffer == 0) {
primitiveFailFor(PrimErrNoCMemory);
}
else {
memcpy(buffer, arrayValueOf(dirPointerOop), structSize);
}
faPathPtr = buffer;
l1: /* end structFromObject:size: */;
if (faPathPtr == 0) {
return primitiveFailFor(PrimErrBadArgument);
}
if (!(faValidateSessionId((faPathPtr->sessionId)))) {
return primitiveFailForOSError(-17 /* badSessionId */);
}
faPath = (faPathPtr->faPath);
status = faReadDirectory(faPath);
if (status == 1 /* noMoreData */) {
return methodReturnValue(nilObject());
}
if (status < 0) {
return primitiveFailForOSError(status);
}
/* no need to check the status of #processDirectory: as it will have flagged an error with interpreterProxy */
resultArray = processDirectory(faPath);
return methodReturnValue(resultArray);
}
/* Set directoryStream to first entry. Answer dirPointerOop. */
/* FileAttributesPlugin>>#primitiveRewinddir */
EXPORT(sqInt)
primitiveRewinddir(void)
{
void *buffer;
sqInt dirPointerOop;
fapath *faPath;
FAPathPtr *faPathPtr;
sqInt resultOop;
sqInt status;
sqInt structSize;
dirPointerOop = stackValue(0);
/* begin structFromObject:size: */
structSize = sizeof(FAPathPtr);
if (!((stSizeOf(dirPointerOop)) == structSize)) {
primitiveFailFor(PrimErrBadArgument);
faPathPtr = ((void *) 0);
goto l1;
}
buffer = alloca(structSize);
if (buffer == 0) {
primitiveFailFor(PrimErrNoCMemory);
}
else {
memcpy(buffer, arrayValueOf(dirPointerOop), structSize);
}
faPathPtr = buffer;
l1: /* end structFromObject:size: */;
if (faPathPtr == 0) {
return primitiveFailFor(PrimErrBadArgument);
}
if (!(faValidateSessionId((faPathPtr->sessionId)))) {
return primitiveFailForOSError(-17 /* badSessionId */);
}
faPath = (faPathPtr->faPath);
status = faRewindDirectory(faPath);
if (status < 0) {
return primitiveFailForOSError(status);
}
/* no need to check the status of #processDirectory: as it will have flagged an error with interpreterProxy */
resultOop = processDirectory(faPath);
return methodReturnValue(resultOop);
}
/* Convert the supplied file name (UTF8 encoded) to the platform encoded byte
array
*/
/* FileAttributesPlugin>>#primitiveStToPlatPath */
EXPORT(sqInt)
primitiveStToPlatPath(void)
{
unsigned char *byteArrayPtr;
fapath faPath;
sqInt fileName;
sqInt resultOop;
fileName = stackObjectValue(0);
if ((failed())
|| (!(isBytes(fileName)))) {
return primitiveFailFor(PrimErrBadArgument);
}
faSetStPathOop((&faPath), fileName);
if (failed()) {
return primitiveFailureCode();
}
resultOop = instantiateClassindexableSize(classByteArray(), faGetPlatPathByteCount((&faPath)));
if (!(resultOop)) {
return primitiveFailFor(PrimErrNoMemory);
}
byteArrayPtr = arrayValueOf(resultOop);
memcpy(byteArrayPtr, faGetPlatPath((&faPath)), faGetPlatPathByteCount((&faPath)));
return methodReturnValue(resultOop);
}
/* Set the owner of the supplied file using chown(). */
/* FileAttributesPlugin>>#primitiveSymlinkChangeOwner */
EXPORT(sqInt)
primitiveSymlinkChangeOwner(void)
{
fapath faPath;
sqInt fileNameOop;
sqInt groupId;
sqInt ownerId;
sqInt status;
fileNameOop = stackObjectValue(2);