-
Notifications
You must be signed in to change notification settings - Fork 65
/
sqUnixFile.c
341 lines (287 loc) · 9.73 KB
/
sqUnixFile.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
/* sqUnixFile.c -- directory operations for Unix
*
* Copyright (C) 1996-2004 by Ian Piumarta and other authors/contributors
* listed elsewhere in this file.
* All rights reserved.
*
* This file is part of Unix Squeak.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
/* Author: Ian.Piumarta@INRIA.Fr
*/
#include "sq.h"
#include "FilePlugin.h"
#include "sqUnixCharConv.h"
#ifdef HAVE_DIRENT_H
# include <dirent.h>
# define NAMLEN(dirent) strlen((dirent)->d_name)
#else
# define dirent direct
# define NAMLEN(dirent) (dirent)->d_namlen
# ifdef HAVE_SYS_NDIR_H
# include <sys/ndir.h>
# endif
# ifdef HAVE_SYS_DIR_H
# include <sys/dir.h>
# endif
# ifdef HAVE_NDIR_H
# include <ndir.h>
# endif
#endif
#ifdef HAVE_UNISTD_H
# include <sys/types.h>
# include <unistd.h>
#endif
#include <time.h>
#include <errno.h>
#include <string.h>
#include <sys/param.h>
#include <sys/stat.h>
#include "pharovm/debug.h"
/***
The interface to the directory primitive is path based.
That is, the client supplies a Squeak string describing
the path to the directory on every call. To avoid traversing
this path on every call, a cache is maintained of the last
path seen, along with the Mac volume and folder reference
numbers corresponding to that path.
***/
/*** Constants ***/
#define ENTRY_FOUND 0
#define NO_MORE_ENTRIES 1
#define BAD_PATH 2
#define DELIMITER '/'
/*** Variables ***/
char lastPath[MAXPATHLEN+1];
int lastPathValid = false;
int lastIndex= -1;
DIR *openDir= 0;
/*** Functions ***/
extern time_t convertToSqueakTime(time_t unixTime);
sqInt dir_Create(char *pathString, sqInt pathStringLength)
{
/* Create a new directory with the given path. By default, this
directory is created relative to the cwd. */
char name[MAXPATHLEN+1];
if (pathStringLength >= MAXPATHLEN)
return false;
if (!sq2uxPath(pathString, pathStringLength, name, MAXPATHLEN, 1))
return false;
return mkdir(name, 0777) == 0; /* rwxrwxrwx & ~umask */
}
sqInt dir_Delete(char *pathString, sqInt pathStringLength)
{
/* Delete the existing directory with the given path. */
char name[MAXPATHLEN+1];
if (pathStringLength >= MAXPATHLEN)
return false;
if (!sq2uxPath(pathString, pathStringLength, name, MAXPATHLEN, 1))
return false;
if (lastPathValid && !strcmp(lastPath, name))
{
closedir(openDir);
lastPathValid= false;
lastIndex= -1;
lastPath[0]= '\0';
}
return rmdir(name) == 0;
}
sqInt dir_Delimitor(void)
{
return DELIMITER;
}
static int maybeOpenDir(char *unixPath)
{
/* if the last opendir was to the same directory, re-use the directory
pointer from last time. Otherwise close the previous directory,
open the new one, and save its name. Return true if the operation
was successful, false if not. */
if (!lastPathValid || strcmp(lastPath, unixPath))
{
/* invalidate the old, open the new */
if (lastPathValid)
closedir(openDir);
lastPathValid= false;
strncpy(lastPath, unixPath, MAXPATHLEN);
if ((openDir= opendir(unixPath)) == 0)
return false;
lastPathValid= true;
lastIndex= 0; /* first entry is index 1 */
}
return true;
}
sqInt dir_Lookup(char *pathString, sqInt pathStringLength, sqInt index,
/* outputs: */ char *name, sqInt *nameLength, sqInt *creationDate, sqInt *modificationDate,
sqInt *isDirectory, squeakFileOffsetType *sizeIfFile, sqInt * posixPermissions, sqInt *isSymlink)
{
/* Lookup the index-th entry of the directory with the given path, starting
at the root of the file system. Set the name, name length, creation date,
creation time, directory flag, and file size (if the entry is a file).
Return: 0 if a entry is found at the given index
1 if the directory has fewer than index entries
2 if the given path has bad syntax or does not reach a directory
*/
int i;
int nameLen= 0;
struct dirent *dirEntry= 0;
char unixPath[MAXPATHLEN+1];
struct stat statBuf;
/* default return values */
*name = 0;
*nameLength = 0;
*creationDate = 0;
*modificationDate = 0;
*isDirectory = false;
*sizeIfFile = 0;
*posixPermissions = 0;
*isSymlink = false;
if (pathStringLength == 0)
strcpy(unixPath, ".");
else if (!sq2uxPath(pathString, pathStringLength, unixPath, MAXPATHLEN, 1))
return BAD_PATH;
/* get file or directory info */
if (!maybeOpenDir(unixPath))
return BAD_PATH;
if (++lastIndex == index)
index= 1; /* fake that the dir is rewound and we want the first entry */
else
{
rewinddir(openDir); /* really rewind it, and read to the index */
lastIndex= index;
}
for (i= 0; i < index; i++)
{
nextEntry:
do
{
errno= 0;
dirEntry= readdir(openDir);
}
while ((dirEntry == 0) && (errno == EINTR));
if (!dirEntry)
return NO_MORE_ENTRIES;
nameLen= NAMLEN(dirEntry);
/* ignore '.' and '..' (these are not *guaranteed* to be first) */
if (nameLen < 3 && dirEntry->d_name[0] == '.')
if (nameLen == 1 || dirEntry->d_name[1] == '.')
goto nextEntry;
}
*nameLength= ux2sqPath(dirEntry->d_name, nameLen, name, MAXPATHLEN, 0);
{
char terminatedName[MAXPATHLEN+1];
if(nameLen > MAXPATHLEN)
return BAD_PATH;
strncpy(terminatedName, dirEntry->d_name, nameLen);
terminatedName[nameLen]= '\0';
if(strlen(unixPath) + 1 + nameLen > MAXPATHLEN)
return BAD_PATH;
strcat(unixPath, "/");
strcat(unixPath, terminatedName);
if (stat(unixPath, &statBuf) && lstat(unixPath, &statBuf))
{
/* We can't stat the entry, but failing here would invalidate
the whole directory --bertf */
return ENTRY_FOUND;
}
}
/* last change time */
*creationDate= convertToSqueakTime(statBuf.st_ctime);
/* modification time */
*modificationDate= convertToSqueakTime(statBuf.st_mtime);
if (S_ISDIR(statBuf.st_mode))
*isDirectory= true;
else
*sizeIfFile= statBuf.st_size;
*isSymlink = S_ISLNK(statBuf.st_mode);
*posixPermissions = statBuf.st_mode & 0777;
return ENTRY_FOUND;
}
sqInt dir_EntryLookup(char *pathString, sqInt pathStringLength, char* nameString, sqInt nameStringLength,
/* outputs: */ char *name, sqInt *nameLength, sqInt *creationDate, sqInt *modificationDate,
sqInt *isDirectory, squeakFileOffsetType *sizeIfFile, sqInt *posixPermissions, sqInt *isSymlink)
{
/* Lookup the given name in the given directory,
Set the name, name length, creation date,
creation time, directory flag, and file size (if the entry is a file).
Return: 0 if a entry is found at the given index
1 if there is no such entry in the directory
2 if the given path has bad syntax or does not reach a directory
*/
char unixPath[MAXPATHLEN+1];
struct stat statBuf;
/* default return values */
*name = 0;
*nameLength = 0;
*creationDate = 0;
*modificationDate = 0;
*isDirectory = false;
*sizeIfFile = 0;
*posixPermissions = 0;
*isSymlink = false;
if (pathStringLength == 0)
strcpy(unixPath, ".");
else if (!sq2uxPath(pathString, pathStringLength, unixPath, MAXPATHLEN, 1))
return BAD_PATH;
char terminatedName[MAXPATHLEN+1];
if(nameStringLength > MAXPATHLEN)
return BAD_PATH;
strncpy(terminatedName, nameString, nameStringLength);
terminatedName[nameStringLength]= '\0';
if(strlen(unixPath) + 1 + nameStringLength > MAXPATHLEN)
return BAD_PATH;
strcat(unixPath, "/");
strcat(unixPath, terminatedName);
if (stat(unixPath, &statBuf) && lstat(unixPath, &statBuf)) {
return NO_MORE_ENTRIES;
}
/* To match the results of dir_Lookup, copy back the file name */
*nameLength = ux2sqPath(nameString, nameStringLength, name, 256, 0);
/* last change time */
*creationDate= convertToSqueakTime(statBuf.st_ctime);
/* modification time */
*modificationDate= convertToSqueakTime(statBuf.st_mtime);
if (S_ISDIR(statBuf.st_mode))
*isDirectory= true;
else
*sizeIfFile= statBuf.st_size;
*isSymlink = S_ISLNK(statBuf.st_mode);
*posixPermissions = statBuf.st_mode & 0777;
return ENTRY_FOUND;
}
/* unix files are untyped, and the creator is correct by default */
sqInt dir_SetMacFileTypeAndCreator(char *filename, sqInt filenameSize, char *fType, char *fCreator)
{
return true;
}
sqInt dir_GetMacFileTypeAndCreator(char *filename, sqInt filenameSize, char *fType, char *fCreator)
{
return true;
}
/*
* The following is useful in a debugging context when the VM's output has been
* directed to a log file. It binds stdout to /dev/tty, arranging that output
* of debugging print routines such as printOop appear on stdout.
*/
void
sqStdoutToDevTTY()
{
if (!freopen("/dev/tty","w",stdout))
logErrorFromErrno("sqStdoutToDevTTY freopen(\"/dev/tty\",\"w\",stdout):");
}