-
Notifications
You must be signed in to change notification settings - Fork 11
/
MacOSXFiles.ns
234 lines (224 loc) · 7.36 KB
/
MacOSXFiles.ns
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
Newspeak3
'NSFiles-MacOSX'
class MacOSXFiles usingPlatform: p = (
(* This code was derived by converting the Strongtalk MacOSX file classes to Newspeak, which is why the Sun Microsystems copyright and BSD license below applies.
Copyright (c) 1995-2006 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
Copyright 2009 Stephen Rees
Copyright 2010 Ryan Macnak and other contributors.
Copyright 2012 Cadence Design Systems, Inc.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistribution in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/o other materials provided with the distribution.
Neither the name of Sun Microsystems or the names of contributors may
be used to endorse or promote products derived from this software without
specific prior written permission.
>>
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.>> *)
|
private UnixFilePath = p unixfiles UnixFilePath.
private UnixFilePattern = p unixfiles UnixFilePattern.
private UnixFileDescriptor = p unixfiles UnixFileDescriptor.
private MacOSXStatBuffer = p squeak MacOSXStatBuffer.
private Alien = p aliens Alien.
private NotFoundError = p squeak NotFoundError.
private __error_func
private strerror_func
private chdir_func
private chmod_func
private close_func
private fstat_func
private getcwd_func
private getegid_func
private geteuid_func
private glob_func
private lseek_func
private lstat_func
private mkdir_func
private open_func
private read_func
private readlink_func
private realpath_func
private rename_func
private rmdir_func
private stat_func
private symlink_func
private unlink_func
private write_func
|) (
class MacOSXFileDescriptor = UnixFileDescriptor (
) (
StatBuffer = (
^MacOSXStatBuffer
)
blockSize ^<Int> = (
(* Use the sector size *)
^self stat: [:buffer| buffer blockSize]
ifFail: [4096]
)
lseek: handle position: p <Int> whence: whence <Int> = (
^(lseek_func
primFFICallResult:
(Alien new: 8)
with: handle
with: (p bitAnd: 16rFFFFFFFF)
with: (p bitShift: -32)
with: whence) asSignedLongLong
)
public size ^<Int> = (
(* Use the sector size *)
^self stat: [:buffer| buffer fileSize]
ifFail: [0]
)
stat: testBlock ifFail: failBlock = (
| statBuffer <MacOSXStatBuffer> |
statBuffer:: MacOSXStatBuffer newC.
^[(fstat_func
primFFICallResult: (Alien new: 4)
with: self handle
with: statBuffer address)
asSignedLong == -1
ifTrue: [ failBlock value ]
ifFalse: [ testBlock value: statBuffer ] ]
ensure: [statBuffer free]
)
) : (
public for: obj <EOBJ> mode: m <Symbol> ^ <Instance> = (
(* Create a new instance, but don't open yet *)
^self new externalObject: obj; mode: m
)
)
public class MacOSXFilePath = UnixFilePath (
) (
O_CREAT = (
^16r200
)
O_TRUNC = (
^16r400
)
StatBuffer = (
^MacOSXStatBuffer
)
fileDescriptorClass = (
^MacOSXFileDescriptor
)
lstat: testBlock ifFail: failBlock = (
| statBuffer <MacOSXStatBuffer> name <CString> |
statBuffer:: MacOSXStatBuffer newC.
name:: Alien newCString: self name.
^[(lstat_func
primFFICallResult: (Alien new: 4)
with: name address
with: statBuffer address)
asSignedLong == -1
ifTrue: [ failBlock value ]
ifFalse: [ testBlock value: statBuffer ] ]
ensure: [ statBuffer free. name free]
)
stat: testBlock ifFail: failBlock = (
| statBuffer <MacOSXStatBuffer> name <CString> |
statBuffer:: MacOSXStatBuffer newC.
name:: Alien newCString: self name.
^[(stat_func
primFFICallResult: (Alien new: 4)
with: name address
with: statBuffer address)
asSignedLong == -1
ifTrue: [ failBlock value ]
ifFalse: [ testBlock value: statBuffer ] ]
ensure: [ statBuffer free. name free]
)
statName: name buffer: statBuffer = (
(* ^{{<libc ExternalProxy stat>
name: name
buffer: statBuffer}} *)
)
) : (
public currentDirectory ^<FilePath> = (
| cd |
cd:: Alien newC: 500.
^[getcwd_func
primFFICallResult: (Alien new: 4)
with: cd address
with: 500.
self new pattern: cd strcpy] ensure: [cd free]
)
public for: aString = (
^self new pattern: aString; yourself
)
)
public class MacOSXFilePattern = UnixFilePattern (
) (
vectorOffset = (
^17
)
) : (
public for: pat <Str> ^<FilePattern> = (
self assert: [ self validPattern: pat ].
^self new pattern: pat
)
public multipleMatchWildcardCharacter ^ <Character> = (
^"*"
)
pathSeparatorCharacter ^ <Character> = (
^'/'
)
public patternHasWildcards: pat <Str> ^<Boolean> = (
^(pat includes: multipleMatchWildcardCharacter)
or: [ pat includes: singleMatchWildcardCharacter ]
)
public singleMatchWildcardCharacter ^ <Character> = (
^"?"
)
validPattern: pat <Str> ^<Boolean> = (
(* Make sure this is a valid syntactic pattern (should be valid even if no *s) *)
self unimplemented
)
)
error: aString = (
| errno <Integer> result <Alien> msg <String> |
errno: self lastError.
result:: (strerror_func primFFICallResult: (Alien new: 4) with: errno).
msg:: Alien forPointer: (result unsignedLongAt: 1).
self Error signal: aString, ' ', msg strcpy
)
lastError = (
(* extern int * __error(void);
#define errno ( *__error()) *)
^(Alien forPointer: ((__error_func primFFICallResult: (Alien new: 4)) unsignedLongAt: 1)) signedLongAt: 1
)
public resetForNewImageSession = (
| libc |
libc:: Alien libcName.
chdir_func:: Alien lookup: 'chdir' inLibrary: libc.
chmod_func:: Alien lookup: 'chmod' inLibrary: libc.
close_func:: Alien lookup: 'close' inLibrary: libc.
__error_func:: Alien lookup: '__error' inLibrary: libc.
fstat_func:: Alien lookup: 'fstat$INODE64' inLibrary: libc.
getcwd_func:: Alien lookup: 'getcwd' inLibrary: libc.
getegid_func:: Alien lookup: 'getegid' inLibrary: libc.
geteuid_func:: Alien lookup: 'geteuid' inLibrary: libc.
glob_func:: Alien lookup: 'glob' inLibrary: libc.
lseek_func:: Alien lookup: 'lseek' inLibrary: libc.
lstat_func:: Alien lookup: 'lstat$INODE64' inLibrary: libc.
mkdir_func:: Alien lookup: 'mkdir' inLibrary: libc.
open_func:: Alien lookup: 'open' inLibrary: libc.
read_func:: Alien lookup: 'read' inLibrary: libc.
readlink_func:: Alien lookup: 'readlink' inLibrary: libc.
realpath_func:: Alien lookup: 'realpath' inLibrary: libc.
rename_func:: Alien lookup: 'rename' inLibrary: libc.
rmdir_func:: Alien lookup: 'rmdir' inLibrary: libc.
strerror_func:: Alien lookup: 'strerror' inLibrary: libc.
stat_func:: Alien lookup: 'stat$INODE64' inLibrary: libc.
symlink_func:: Alien lookup: 'symlink' inLibrary: libc.
unlink_func:: Alien lookup: 'unlink' inLibrary: libc.
write_func:: Alien lookup: 'write' inLibrary: libc
)
) : (
)