-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathosFile.go
341 lines (297 loc) · 9.2 KB
/
osFile.go
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
package main
import (
"errors"
"fmt"
"os"
"strconv"
"strings"
"github.com/mitchellh/go-homedir"
)
const (
osNotFound uint8 = 0
osFileFound uint8 = 1
osDirectoryFound uint8 = 2
cbLoadAddress uint16 = 0x2
cbExecutionAddress uint16 = 0x6
cbStartAddressOrSize uint16 = 0xa
cbEndAddressOrAttributes uint16 = 0xe
addressNull uint32 = ^uint32(0)
)
type fileAttributes struct {
fileType uint8
fileSize uint32
hasMetadata bool
loadAddress uint32
executionAddress uint32
attributes uint32
}
func execOSFILE(env *environment) {
a, x, y, p := env.cpu.GetAXYP()
// See: http://beebwiki.mdfs.net/OSFILE
controlBlock := uint16(x) + uint16(y)<<8
filenameAddress := env.mem.peekWord(controlBlock)
loadAddress := env.mem.peekDoubleWord(controlBlock + cbLoadAddress)
executionAddress := env.mem.peekDoubleWord(controlBlock + cbExecutionAddress)
startAddress := env.mem.peekDoubleWord(controlBlock + cbStartAddressOrSize)
endAddress := env.mem.peekDoubleWord(controlBlock + cbEndAddressOrAttributes)
filename := env.mem.peekString(filenameAddress, 0x0d)
filename = processPath(filename)
newA := uint8(0) // Nothing found
option := ""
switch a {
case 0:
option = "Save file"
/*
Save a block of memory as a file using the
information provided in the parameter block.
*/
attr := saveFile(env, filename, startAddress, endAddress, executionAddress, loadAddress, false)
if attr.fileType != osNotFound {
updateControlBlock(env, controlBlock, attr)
}
newA = attr.fileType
case 1:
option = "Write file metadata"
attr := getFileAttributes(env, filename)
if attr.fileType != osNotFound {
attr.loadAddress = loadAddress
attr.executionAddress = executionAddress
attr.attributes = endAddress
writeMetadata(env, filename, attr)
}
newA = attr.fileType
case 2:
option = "Write file reload address"
attr := getFileAttributes(env, filename)
if attr.fileType != osNotFound {
attr.loadAddress = loadAddress
writeMetadata(env, filename, attr)
}
newA = attr.fileType
case 3:
option = "Write file execution address"
attr := getFileAttributes(env, filename)
if attr.fileType != osNotFound {
attr.executionAddress = executionAddress
writeMetadata(env, filename, attr)
}
newA = attr.fileType
case 4:
option = "Write file attributes"
attr := getFileAttributes(env, filename)
if attr.fileType != osNotFound {
attr.attributes = endAddress
writeMetadata(env, filename, attr)
}
newA = attr.fileType
case 5:
option = "File info"
/*
Read a file’s catalogue information, with the file
type returned in the accumulator. The information
is written to the parameter block.
*/
attr := getFileAttributes(env, filename)
if attr.fileType != osNotFound {
updateControlBlock(env, controlBlock, attr)
}
newA = attr.fileType
case 6:
option = "Delete file"
/*
Delete object. If the object does not exist, A returned
as &00. If the object is locked, or is not owned, or is
a directory that is not empty, or is open, then an error
is generated.
*/
newA = deleteFile(env, filename)
case 7:
option = "Create an empty file of defined length"
/*
Create an empty file of defined length. Block as for
SAVE. The supplied start address is usually passed as &0
and the end address as the required length. No data is
transfered, and the file does not necessarily contain
zeros. Some file systems may deliberately overwrite any
existing data in the file. If a file already exists with
the same name, it is overwritten, with the file access
and the case of the name staying the same. If the file
is locked, or a directory exists with the same name, or
the file is open, then an error is generated.
*/
attr := saveFile(env, filename, startAddress, endAddress, executionAddress, loadAddress, true)
if attr.fileType != osNotFound {
updateControlBlock(env, controlBlock, attr)
}
newA = attr.fileType
case 0xff: // Load file into memory
option = "Load file"
/*
Load the named file, the address to which the file is
loaded being determined by the lowest byte of the
execution address in the control block (XY+6). If
this byte is zero, the address given in the control
block is used, otherwise the file's own load address
is used.
*/
useLoadAddress := (executionAddress & 0xff) == 0
if !useLoadAddress {
loadAddress = addressNull
}
attr := loadFile(env, filename, loadAddress)
//env.mem.pokeDoubleWord(controlBlock+cbStartAddressOrSize, attr.fileSize)
if attr.fileType == osNotFound {
env.raiseError(214, "File not found")
}
updateControlBlock(env, controlBlock, attr)
newA = attr.fileType
default:
env.notImplemented(fmt.Sprintf("OSFILE(A=%02x)", a))
}
if a != 1 && a != 5 {
env.mem.pokeWord(controlBlock+cbEndAddressOrAttributes, 0x00 /*attributes*/)
}
env.cpu.SetAXYP(newA, x, y, p)
env.log(fmt.Sprintf("OSFILE('%s',A=%02x,FCB=%04x,FILE=%s) => %v",
option, a, controlBlock, filename, newA))
}
func updateControlBlock(env *environment, controlBlock uint16, attr *fileAttributes) {
if attr.fileType != osNotFound {
env.mem.pokeDoubleWord(controlBlock+cbStartAddressOrSize, attr.fileSize)
if attr.hasMetadata {
env.mem.pokeDoubleWord(controlBlock+cbLoadAddress, attr.loadAddress)
env.mem.pokeDoubleWord(controlBlock+cbExecutionAddress, attr.executionAddress)
env.mem.pokeDoubleWord(controlBlock+cbEndAddressOrAttributes, attr.attributes)
}
}
}
func loadFile(env *environment, filename string, loadAddress uint32) *fileAttributes {
attr := getFileAttributes(env, filename)
if attr.fileType == osNotFound {
return attr
}
if attr.fileType == osDirectoryFound {
env.raiseError(errorTodo, "Directory found")
return attr
}
if loadAddress == addressNull {
if !attr.hasMetadata {
env.raiseError(errorTodo, "Missing metadata file")
attr.fileType = osNotFound
return attr
}
loadAddress = attr.loadAddress
} else {
attr.loadAddress = loadAddress
}
data, err := os.ReadFile(filename)
if err != nil {
env.raiseError(errorTodo, err.Error())
attr.fileType = osNotFound
return attr
}
env.mem.pokeSlice(uint16(loadAddress), uint16(len(data)), data)
return attr
}
func saveFile(env *environment, filename string,
startAddress uint32, endAddress uint32, executionAddress uint32, loadAddress uint32, blank bool) *fileAttributes {
var attr fileAttributes
attr.loadAddress = loadAddress
attr.executionAddress = executionAddress
attr.fileSize = endAddress - startAddress
// attr.attributes = ?
var data []uint8
if blank {
data = make([]uint8, attr.fileSize)
} else {
data = env.mem.peekSlice(uint16(startAddress), uint16(attr.fileSize))
}
err := os.WriteFile(filename, data, 0644)
if err != nil {
env.raiseError(errorTodo, err.Error())
attr.fileType = osNotFound
} else {
attr.fileType = osFileFound
}
writeMetadata(env, filename, &attr)
return &attr
}
func deleteFile(env *environment, filename string) uint8 {
err := os.Remove(filename)
var pathError *os.PathError
if errors.As(err, &pathError) {
return osNotFound
}
if err != nil {
env.raiseError(errorTodo, err.Error())
return osNotFound
}
os.Remove(filename + metadataExtension)
return osFileFound
}
func getFileAttributes(env *environment, filename string) *fileAttributes {
var attr fileAttributes
fileInfo, err := os.Stat(filename)
if errors.Is(err, os.ErrNotExist) {
attr.fileType = osNotFound
return &attr
}
if err != nil {
attr.fileType = osNotFound
return &attr
}
attr.fileSize = uint32(fileInfo.Size())
attr.fileType = osFileFound
if fileInfo.IsDir() {
attr.fileType = osDirectoryFound
}
/*
Search metadata file "{filename}.inf" looking like:
$.BasObj 003000 003100 005000 00 CRC32=614721E1
*/
attr.hasMetadata = false
metadata := filename + metadataExtension
data, err := os.ReadFile(metadata)
if errors.Is(err, os.ErrNotExist) {
return &attr
}
parts := strings.Fields(string(data))
if len(parts) < 5 {
env.log(fmt.Sprintf("Invalid format for metadata file for %s, missing fields", metadata))
return &attr
}
i, err := strconv.ParseUint(parts[1], 16, 64)
if err != nil {
env.log(fmt.Sprintf("Invalid format for metadata file %s, bad load address '%s'", metadata, err.Error()))
return &attr
}
attr.loadAddress = uint32(i)
i, err = strconv.ParseUint(parts[2], 16, 64)
if err != nil {
env.log(fmt.Sprintf("Invalid format for metadata file %s, bad exec address '%s'", metadata, err.Error()))
return &attr
}
attr.executionAddress = uint32(i)
i, err = strconv.ParseUint(parts[4], 16, 64)
if err != nil {
env.log(fmt.Sprintf("Invalid format for metadata file %s, bad sttributes '%s'", metadata, err.Error()))
return &attr
}
attr.attributes = uint32(i)
attr.hasMetadata = true
return &attr
}
func writeMetadata(env *environment, filename string, attr *fileAttributes) {
// $.BasObj 003000 003100 005000 00 CRC32=614721E1
metadata := fmt.Sprintf("$.FILE %08X %08X %08X %02X",
attr.loadAddress, attr.executionAddress, attr.fileSize, attr.attributes)
os.WriteFile(filename+metadataExtension, []byte(metadata), 0644)
}
func processPath(path string) string {
newPath, err := homedir.Expand(path)
if err != nil {
// Ignore the error and use the original version
return path
}
return newPath
}