-
Notifications
You must be signed in to change notification settings - Fork 67
/
faSupport.c
597 lines (472 loc) · 14.7 KB
/
faSupport.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
/*
* faSupport.c - Unix support routines for the FileAttributesPlugin
*
*/
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <assert.h>
#include "sq.h"
#include "faCommon.h"
extern struct VirtualMachine * interpreterProxy;
sqInt faSetStDir(fapath *aFaPath, char *pathName, int len)
{
sqInt status;
/* Set the St encoded path and ensure trailing delimiter */
if (len+1 >= FA_PATH_MAX)
return interpreterProxy->primitiveFailForOSError(FA_STRING_TOO_LONG);
memcpy(aFaPath->path, pathName, len);
if (aFaPath->path[len-1] != PATH_SEPARATOR)
aFaPath->path[len++] = PATH_SEPARATOR;
aFaPath->path[len] = 0;
aFaPath->path_len = len;
aFaPath->path_file = aFaPath->path + len;
aFaPath->max_file_len = FA_PATH_MAX - len;
/* Convert to platform specific form */
status = sq2uxPath(aFaPath->path, len, aFaPath->uxpath, FA_PATH_MAX, 1);
if (!status)
return interpreterProxy->primitiveFailForOSError(FA_STRING_TOO_LONG);
/* Set aFaPath->uxpath_file and max_file_len to the buffer after the directory */
aFaPath->uxpath_len = strlen(aFaPath->uxpath);
aFaPath->uxpath_file = aFaPath->uxpath + aFaPath->uxpath_len;
aFaPath->uxmax_file_len = FA_PATH_MAX - aFaPath->uxpath_len;
return 0;
}
sqInt faSetStPath(fapath *aFaPath, char *pathName, int len)
{
sqInt status;
/* Set the St encoded path */
if (len >= FA_PATH_MAX)
return interpreterProxy->primitiveFailForOSError(FA_STRING_TOO_LONG);
memcpy(aFaPath->path, pathName, len);
aFaPath->path[len] = 0;
aFaPath->path_len = len;
aFaPath->path_file = 0;
aFaPath->max_file_len = 0;
/* Convert to platform specific form */
status = sq2uxPath(aFaPath->path, len, aFaPath->uxpath, FA_PATH_MAX, 1);
if (!status)
return interpreterProxy->primitiveFailForOSError(FA_STRING_TOO_LONG);
/* Set aFaPath->uxpath_file and max_file_len to the buffer after the directory */
aFaPath->uxpath_len = strlen(aFaPath->uxpath);
aFaPath->uxpath_file = 0;
aFaPath->uxmax_file_len = 0;
return 0;
}
sqInt faSetStFile(fapath *aFaPath, char *pathName)
{
int len;
sqInt status;
assert(aFaPath->path_file != 0);
/* Set the St encoded path */
len = strlen(pathName);
if (len >= aFaPath->max_file_len)
return interpreterProxy->primitiveFailForOSError(FA_STRING_TOO_LONG);
strcpy(aFaPath->path_file, pathName);
/* Convert to platform specific form */
status = sq2uxPath(aFaPath->path_file, len, aFaPath->uxpath_file, aFaPath->uxmax_file_len, 1);
if (!status)
return interpreterProxy->primitiveFailForOSError(FA_STRING_TOO_LONG);
return 0;
}
sqInt faSetPlatPath(fapath *aFaPath, char *pathName)
{
int len;
sqInt status;
/* Set the platform encoded path */
len = strlen(pathName);
if (len >= FA_PATH_MAX)
return interpreterProxy->primitiveFailForOSError(FA_STRING_TOO_LONG);
strcpy(aFaPath->uxpath, pathName);
aFaPath->uxpath[len] = 0;
aFaPath->uxpath_len = len;
aFaPath->uxpath_file = 0;
aFaPath->uxmax_file_len = 0;
/* Convert to St specific form */
status = ux2sqPath(aFaPath->uxpath, len, aFaPath->path, FA_PATH_MAX, 1);
if (!status)
return interpreterProxy->primitiveFailForOSError(FA_STRING_TOO_LONG);
/* Set aFaPath->uxpath_file and max_file_len to the buffer after the directory */
aFaPath->path_len = strlen(aFaPath->path);
aFaPath->path_file = 0;
aFaPath->max_file_len = 0;
return 0;
}
sqInt faSetPlatPathOop(fapath *aFaPath, sqInt pathNameOop)
{
int len;
char *bytePtr;
sqInt status;
len = interpreterProxy->stSizeOf(pathNameOop);
bytePtr = interpreterProxy->arrayValueOf(pathNameOop);
if (len >= FA_PATH_MAX)
return interpreterProxy->primitiveFailForOSError(FA_STRING_TOO_LONG);
memcpy(aFaPath->uxpath, bytePtr, len);
aFaPath->uxpath[len] = 0;
aFaPath->uxpath_len = len;
aFaPath->uxpath_file = 0;
aFaPath->uxmax_file_len = 0;
/* Convert to St specific form */
status = ux2sqPath(aFaPath->uxpath, len, aFaPath->path, FA_PATH_MAX, 1);
if (!status)
return interpreterProxy->primitiveFailForOSError(FA_STRING_TOO_LONG);
/* Set aFaPath->uxpath_file and max_file_len to the buffer after the directory */
aFaPath->path_len = strlen(aFaPath->path);
aFaPath->path_file = 0;
aFaPath->max_file_len = 0;
return 0;
}
sqInt faSetPlatFile(fapath *aFaPath, char *pathName)
{
int len;
sqInt status;
assert(aFaPath->uxpath_file != 0);
/* Set the platform encoded file name */
len = strlen(pathName);
if (len >= aFaPath->uxmax_file_len)
return interpreterProxy->primitiveFailForOSError(FA_STRING_TOO_LONG);
strcpy(aFaPath->uxpath_file, pathName);
/* Convert to St specific form */
status = ux2sqPath(aFaPath->uxpath_file, len, aFaPath->path_file, aFaPath->max_file_len, 1);
if (!status)
return interpreterProxy->primitiveFailForOSError(FA_STRING_TOO_LONG);
return 0;
}
/*
* Convert the supplied Unix (UTC) time to Squeak time.
* Squeak time has an epoch of 1901 and uses local time
* i.e. timezone + daylight savings
*
* Answer an sqLong which is guaranteed to be 64 bits on all platforms.
*/
sqLong faConvertUnixToLongSqueakTime(time_t unixTime)
{
sqLong squeakTime;
squeakTime = unixTime;
#if defined(HAVE_TM_GMTOFF)
squeakTime = squeakTime + localtime(&unixTime)->tm_gmtoff;
#elif defined(HAVE_TIMEZONE)
squeakTime = squeakTime + (daylight*60*60) - timezone;
#else
#error: cannot determine timezone correction
#endif
/* Squeak epoch is Jan 1, 1901. Unix epoch is Jan 1, 1970: 17 leap years
* and 52 non-leap years later than Squeak. */
squeakTime = squeakTime +
(52*365UL + 17*366UL) * 24*60*60UL;
return squeakTime;
}
/*
* pathNameToOop
*
* Convert the supplied platform encoded C string to a
* precomposed UTF8 ByteArray.
*/
sqInt pathNameToOop(char *pathName)
{
sqInt pathOop;
int status;
int len;
char uxName[FA_PATH_MAX];
len = strlen(pathName);
if (len >= FA_PATH_MAX)
return interpreterProxy->primitiveFailForOSError(FA_STRING_TOO_LONG);
status = ux2sqPath(pathName, len, uxName, FA_PATH_MAX, 1);
if (!status)
return interpreterProxy->primitiveFailForOSError(FA_INVALID_ARGUMENTS);
status = faCharToByteArray(uxName, &pathOop);
if (status)
return interpreterProxy->primitiveFailForOSError(status);
return pathOop;
}
/*
* faOpenDirectory
*
* Open the supplied directory for iteration and return the first entry.
*
* '.' and '..' are never returned.
*
* The root directory must be represented as '/', and not an empty string.
*
* If there are no entries, close the directory and return FA_NO_MORE_DATA
*/
sqInt faOpenDirectory(fapath *aFaPath)
{
sqInt rstatus, cstatus;
/* Open the directory */
aFaPath->platformDir = opendir(faGetPlatPath(aFaPath));
if (aFaPath->platformDir == NULL)
return FA_CANT_OPEN_DIR;
rstatus = faReadDirectory(aFaPath);
if (rstatus == FA_NO_MORE_DATA) {
cstatus = faCloseDirectory(aFaPath);
if (cstatus != FA_SUCCESS)
return cstatus; }
return rstatus;
}
/*
* faReadDirectory
*
* Read the next entry from the already opened directory (dirState)
*
* If there are no entries, return FA_NO_MORE_DATA
*/
sqInt faReadDirectory(fapath *aFaPath)
{
sqInt haveEntry;
struct dirent *entry;
sqInt status;
if (aFaPath->platformDir == NULL)
return FA_CORRUPT_VALUE;
haveEntry = 0;
errno = 0;
do {
entry = readdir(aFaPath->platformDir);
if (entry == NULL) {
if (errno == 0)
return FA_NO_MORE_DATA;
else
return FA_CANT_READ_DIR; }
if ((!(entry->d_name[0] == '.' && entry->d_name[1] == 0)) && strcmp(entry->d_name, ".."))
haveEntry = 1;
} while (!haveEntry);
status = faSetPlatFile(aFaPath, entry->d_name);
if (status) return status;
return FA_SUCCESS;
}
/*
* faCloseDirectory
*
* Close the supplied directory.
*/
sqInt faCloseDirectory(fapath *aFaPath)
{
sqInt status;
if (aFaPath->platformDir == NULL)
return FA_CORRUPT_VALUE;
status = closedir(aFaPath->platformDir);
if (status) return FA_UNABLE_TO_CLOSE_DIR;
aFaPath->platformDir = 0;
return FA_SUCCESS;
}
/*
* faRewindDirectory
*
* Rewind the supplied directory and answer the first entry.
*/
sqInt faRewindDirectory(fapath *aFaPath)
{
if (aFaPath->platformDir == NULL)
return FA_CORRUPT_VALUE;
rewinddir(aFaPath->platformDir);
return faReadDirectory(aFaPath);
}
/*
* faFileAttribute
*
* Answer a single attribute (OOP) for the supplied file name.
*
* Set the interpreterProxy primitiveFailure flag on error.
*
* The attributeNumber has previously been validated.
*
* See FileAttributesPlugin>>primitiveFileAttribute for the list of attribute
* numbers.
*/
sqInt faFileAttribute(fapath *aFaPath, sqInt attributeNumber)
{
faStatStruct statBuf;
int status;
sqInt resultOop = 0;
int mode;
if (attributeNumber <= 12) {
/* Requested attribute comes from stat() entry */
status = stat(faGetPlatPath(aFaPath), &statBuf);
if (status) {
interpreterProxy->primitiveFailForOSError(FA_CANT_STAT_PATH);
return 0; }
switch (attributeNumber) {
case 1: /* fileName, not supported for a single attribute */
resultOop = interpreterProxy->nilObject();
break;
case 2: /* Mode */
resultOop = interpreterProxy->positive32BitIntegerFor(statBuf.st_mode);
break;
case 3: /* inode */
resultOop = interpreterProxy->positive64BitIntegerFor(statBuf.st_ino);
break;
case 4: /* device id */
resultOop = interpreterProxy->positive64BitIntegerFor(statBuf.st_dev);
break;
case 5: /* nlink */
resultOop = interpreterProxy->positive64BitIntegerFor(statBuf.st_nlink);
break;
case 6: /* uid */
resultOop = interpreterProxy->positive32BitIntegerFor(statBuf.st_uid);
break;
case 7: /* gid */
resultOop = interpreterProxy->positive32BitIntegerFor(statBuf.st_gid);
break;
case 8: /* size (if file) */
if (S_ISDIR(statBuf.st_mode) == 0)
resultOop = interpreterProxy->positive64BitIntegerFor(statBuf.st_size);
else
resultOop = interpreterProxy->positive32BitIntegerFor(0);
break;
case 9: /* access time */
resultOop = interpreterProxy->signed64BitIntegerFor(
faConvertUnixToLongSqueakTime(statBuf.st_atime));
break;
case 10: /* modified time */
resultOop = interpreterProxy->signed64BitIntegerFor(
faConvertUnixToLongSqueakTime(statBuf.st_mtime));
break;
case 11: /* change time */
resultOop = interpreterProxy->signed64BitIntegerFor(
faConvertUnixToLongSqueakTime(statBuf.st_ctime));
break;
case 12: /* creation time */
resultOop = interpreterProxy->nilObject();
break;
}
} else if (attributeNumber < 16) {
switch (attributeNumber) {
case 13:
mode = R_OK;
break;
case 14:
mode = W_OK;
break;
case 15:
mode = X_OK;
break;
}
if (access(faGetPlatPath(aFaPath), mode) == 0)
resultOop = interpreterProxy->trueObject();
else
resultOop = interpreterProxy->falseObject();
} else if (attributeNumber == 16) {
/* isSymlink */
status = lstat(faGetPlatPath(aFaPath), &statBuf);
if (status) {
interpreterProxy->primitiveFailForOSError(FA_CANT_STAT_PATH);
return 0; }
if (S_ISLNK(statBuf.st_mode))
resultOop = interpreterProxy->trueObject();
else
resultOop = interpreterProxy->falseObject();
}
return resultOop;
}
/*
* faFileStatAttributes
*
* Populate the supplied array with the file attributes.
*
* On error answer the status.
*
*/
sqInt faFileStatAttributes(fapath *aFaPath, int lStat, sqInt attributeArray)
{
faStatStruct statBuf;
int status;
int mode;
sqInt targetOop;
char targetFile[FA_PATH_MAX];
targetOop = interpreterProxy->nilObject();
if (lStat) {
status = lstat(faGetPlatPath(aFaPath), &statBuf);
if (status)
return FA_CANT_STAT_PATH;
if (S_ISLNK(statBuf.st_mode)) {
/* This is a symbolic link, provide the target filename */
status = readlink(faGetPlatPath(aFaPath), targetFile, FA_PATH_MAX);
if (status >= 0) {
targetFile[status] = 0;
targetOop = pathNameToOop(targetFile); } } }
else {
status = stat(faGetPlatPath(aFaPath), &statBuf);
if (status)
return FA_CANT_STAT_PATH; }
interpreterProxy->storePointerofObjectwithValue(
0, attributeArray,
targetOop);
interpreterProxy->storePointerofObjectwithValue(
1, attributeArray,
interpreterProxy->positive32BitIntegerFor(statBuf.st_mode));
interpreterProxy->storePointerofObjectwithValue(
2, attributeArray,
interpreterProxy->positive64BitIntegerFor(statBuf.st_ino));
interpreterProxy->storePointerofObjectwithValue(
3, attributeArray,
interpreterProxy->positive64BitIntegerFor(statBuf.st_dev));
interpreterProxy->storePointerofObjectwithValue(
4, attributeArray,
interpreterProxy->positive32BitIntegerFor(statBuf.st_nlink));
interpreterProxy->storePointerofObjectwithValue(
5, attributeArray,
interpreterProxy->positive32BitIntegerFor(statBuf.st_uid));
interpreterProxy->storePointerofObjectwithValue(
6, attributeArray,
interpreterProxy->positive32BitIntegerFor(statBuf.st_gid));
interpreterProxy->storePointerofObjectwithValue(
7, attributeArray,
(S_ISDIR(statBuf.st_mode) == 0) ?
interpreterProxy->positive64BitIntegerFor(statBuf.st_size) :
interpreterProxy->positive32BitIntegerFor(0));
interpreterProxy->storePointerofObjectwithValue(
8, attributeArray,
interpreterProxy->signed64BitIntegerFor(
faConvertUnixToLongSqueakTime(statBuf.st_atime)));
interpreterProxy->storePointerofObjectwithValue(
9, attributeArray,
interpreterProxy->signed64BitIntegerFor(
faConvertUnixToLongSqueakTime(statBuf.st_mtime)));
interpreterProxy->storePointerofObjectwithValue(
10, attributeArray,
interpreterProxy->signed64BitIntegerFor(
faConvertUnixToLongSqueakTime(statBuf.st_ctime)));
interpreterProxy->storePointerofObjectwithValue(
11, attributeArray,
interpreterProxy->nilObject());
/* Windows file attribute flags - not supported on Unix */
interpreterProxy->storePointerofObjectwithValue(
12, attributeArray,
interpreterProxy->nilObject());
return FA_SUCCESS;
}
/*
* faExists
*
* Answer a boolean indicating whether the supplied path name exists.
*/
sqInt faExists(fapath *aFaPath)
{
if (access(faGetPlatPath(aFaPath), F_OK))
return interpreterProxy->falseObject();
else
return interpreterProxy->trueObject();
}
/*
* faAccessAttributes
*
* Call access() for each access type (R, W, X) on the supplied path,
* storing the results in the st array attributeArray.
*/
sqInt faAccessAttributes(fapath *aFaPath, sqInt attributeArray, sqInt offset)
{
sqInt index;
sqInt trueOop;
sqInt falseOop;
sqInt accessOop;
index = offset;
trueOop = interpreterProxy->trueObject();
falseOop = interpreterProxy->falseObject();
accessOop = access(faGetPlatPath(aFaPath), R_OK) ? falseOop : trueOop;
interpreterProxy->storePointerofObjectwithValue(index++, attributeArray, accessOop);
accessOop = access(faGetPlatPath(aFaPath), W_OK) ? falseOop : trueOop;
interpreterProxy->storePointerofObjectwithValue(index++, attributeArray, accessOop);
accessOop = access(faGetPlatPath(aFaPath), X_OK) ? falseOop : trueOop;
interpreterProxy->storePointerofObjectwithValue(index++, attributeArray, accessOop);
return 0;
}