-
Notifications
You must be signed in to change notification settings - Fork 0
/
lisp_fileio.go
695 lines (643 loc) · 18.6 KB
/
lisp_fileio.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
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
//go:build fileio
// +build fileio
package z3s5
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"sync"
"github.com/adrg/xdg"
"github.com/nukata/goarith"
)
var ErrPortNotWritable = errors.New("port not writable")
var ErrPortNotReadable = errors.New("port not readable")
var ErrPortNotSeekable = errors.New("port not seekable")
var ErrNoPort = errors.New("invalid port or operation not supported")
var BoxedFilePort = NewSym("file-port")
var BoxedStringPort = NewSym("str-port")
var BoxedURIPort = NewSym("uri-port")
var PortTypes []*Sym
var fileIOMutex sync.Mutex
const DefaultOpenFlags = os.O_RDWR | os.O_APPEND | os.O_CREATE
const DefaultFilePermissions = 0o640
func init() {
PortTypes = make([]*Sym, 0)
AddPortType(BoxedFilePort)
AddPortType(BoxedStringPort)
AddPortType(BoxedURIPort)
}
// AddPortType adds a foreign port type to the system so port? knows about it.
// This symbol needs to be the one in boxed.Sort of the new BoxedPort created and added with DefBoxed, like
// in the examples of BoxedFilePort and BoxedStringPort.
func AddPortType(sym *Sym) {
fileIOMutex.Lock()
defer fileIOMutex.Unlock()
PortTypes = append(PortTypes, sym)
}
// Define_FileIO defines file-i/o-related functions that allow for direct filesystem access
// without safeguards. For security reasons this module is "opt-in" by using the build-tag "io".
// Even if it is included, the functions will only work if permissions are given for
// AllowFileRead and AllowFileWrite respectively.
func (interp *Interp) Define_FileIO() {
// register this module
reflect, ok := interp.GetGlobalVar(ReflectSym)
if !ok {
reflect = Nil
}
interp.SetGlobalVar(ReflectSym, &Cell{NewSym("fileio"), reflect})
// register boxed values
interp.DefBoxed(BoxedFilePort)
interp.DefBoxed(BoxedStringPort)
interp.DefBoxed(BoxedURIPort)
// (open file [flags] [permissions]) => port open a file for reading or writing.
// If the file does not exist, it is created. The given file path must be a slash path
// and is converted to a local path.
interp.Def("open", -1, func(a []any) any {
args := ListToArray(a[0].(*Cell))
le := len(args)
if le < 1 {
panic(`open: missing argument, expected file path as first argument`)
}
file := args[0].(string)
var flags, perm int
flags = DefaultOpenFlags
perm = DefaultFilePermissions
if le > 1 {
flags = FileFlagsToInt("open", args[1].(*Cell))
if le > 2 {
perm = FilePermissionToInt("open", args[2])
}
}
var err error
var fi *os.File
path := filepath.FromSlash(file)
fi, err = os.OpenFile(path, flags, os.FileMode(perm))
if err != nil {
panic(fmt.Errorf("open: %w", err))
}
port := &Boxed{Datum: fi, Sort: BoxedFilePort, Valid: true}
ioReader, _ := port.Datum.(io.Reader)
iowriter, _ := port.Datum.(io.Writer)
ioseeker, _ := port.Datum.(io.Seeker)
iowriterat, _ := port.Datum.(io.WriterAt)
stream := NewStream(NewFileSource(path), ioReader, iowriter, ioseeker, iowriterat)
interp.Streams().Store(port, stream)
return port
})
// (close port) close the given port
interp.Def("close", 1, func(a []any) any {
port, ok := a[0].(*Boxed)
if !ok {
panic(fmt.Sprintf("close: expected valid port, given %v", Str(a[0])))
}
if !port.Valid {
return Void // do nothing since it is already closed or never was valid
}
fileIOMutex.Lock()
defer fileIOMutex.Unlock()
fi := port.Datum
switch fi.(type) {
case *bytes.Buffer:
port.Valid = false
fi.(*bytes.Buffer).Reset()
case io.ReadWriteCloser:
port.Valid = false
fi.(io.ReadWriteCloser).Close()
default:
panic(ErrNoPort)
}
interp.Streams().Delete(port)
return Void
})
// (port? x) => bool return t if x is a port, nil otherwise
interp.Def("port?", 1, func(a []any) any {
port, ok := a[0].(*Boxed)
if !ok {
return Nil
}
fileIOMutex.Lock()
defer fileIOMutex.Unlock()
found := false
for i := range PortTypes {
if PortTypes[i] == port.Sort {
found = true
break
}
}
return AsLispBool(found)
})
// (file-exists? fi) => bool return true if the file exists at given path, nil otherwise
interp.Def("file-exists?", 1, func(a []any) any {
path := a[0].(string)
if _, err := os.Stat(path); err == nil {
return true
}
return Nil
})
// (dir? fi) => bool returns true if the given file exists and is a directory, nil otherwise
interp.Def("dir?", 1, func(a []any) any {
path := a[0].(string)
if fileInfo, err := os.Stat(path); err == nil {
return AsLispBool(fileInfo.IsDir())
}
return Nil
})
// (readable? port) => bool return true if the stream can be read from (but it may be empty), nil otherwise
interp.Def("readable?", 1, func(a []any) any {
obj, ok := a[0].(*Boxed)
if !ok {
return Nil
}
s, ok := interp.Streams().Load(obj)
if !ok {
return Nil
}
stream, ok := s.(*Stream)
if !ok {
return Nil
}
if stream.LispReader.IOReader() == nil {
return Nil
}
return true
})
// (writable? id) => bool return true if the stream can be written to, nil otherwise
interp.Def("writable?", 1, func(a []any) any {
obj, ok := a[0].(*Boxed)
if !ok {
return Nil
}
s, ok := interp.Streams().Load(obj)
if !ok {
return Nil
}
stream, ok := s.(*Stream)
if !ok {
return Nil
}
if stream.LispWriter.IOWriter() == nil {
return Nil
}
return true
})
// (seekable? id) => bool return true if the stream supports seek, nil otherwise
interp.Def("seekable?", 1, func(a []any) any {
obj, ok := a[0].(*Boxed)
if !ok {
return Nil
}
s, ok := interp.Streams().Load(obj)
if !ok {
return Nil
}
stream, ok := s.(*Stream)
if !ok {
return Nil
}
if stream.LispSeeker.IOSeeker() == nil {
return Nil
}
return true
})
// (read port) => any read one expression from port, EOF if no more to read
interp.Def("read", 1, func(a []any) any {
obj, ok := a[0].(*Boxed)
if !ok {
panic(fmt.Sprintf("read: expected valid port, given %v", Str(a[0])))
}
s, ok := interp.Streams().Load(obj)
if !ok {
panic("read: stream not valid")
}
stream, ok := s.(*Stream)
if !ok {
panic("read: stream not valid (internal error)")
}
result, errObj := stream.LispReader.Read()
if err, ok := errObj.(error); ok && err != nil {
if err == EofToken {
return EofToken
}
panic(fmt.Errorf("read: %w", err.Error()))
}
return result
})
// (write port datum) => int write datum to port, return number of bytes written
interp.Def("write", 2, func(a []any) any {
obj, ok := a[0].(*Boxed)
if !ok {
panic(fmt.Sprintf("write: expected valid port, given %v", Str(a[0])))
}
s, ok := interp.Streams().Load(obj)
if !ok {
panic("write: stream not valid")
}
stream, ok := s.(*Stream)
if !ok {
panic("write: stream not valid (internal error)")
}
k, err := stream.LispWriter.Write(a[1])
if err != nil {
panic(fmt.Sprintf("write: %v", err))
}
return goarith.AsNumber(k)
})
// (write-string port s) => int writes s to port
interp.Def("write-string", 2, func(a []any) any {
obj, ok := a[0].(*Boxed)
if !ok {
panic(fmt.Sprintf("write-string: expected valid port, given %v", Str(a[0])))
}
s, ok := interp.Streams().Load(obj)
if !ok {
panic("write-string: stream not valid")
}
stream, ok := s.(*Stream)
if !ok {
panic("write-string: stream not valid (internal error)")
}
k, err := io.WriteString(stream.LispWriter.IOWriter(), a[1].(string))
if err != nil {
panic(fmt.Sprintf("write-string: %v", err))
}
return goarith.AsNumber(k)
})
// (read-string p del)
interp.Def("read-string", 2, func(a []any) any {
obj, ok := a[0].(*Boxed)
if !ok {
panic(fmt.Sprintf("read-string: expected valid port, given %v", Str(a[0])))
}
s, ok := interp.Streams().Load(obj)
if !ok {
panic("read-string: stream not valid")
}
stream, ok := s.(*Stream)
if !ok {
panic("read-string: stream not valid (internal error)")
}
del := []byte(a[1].(string))
if len(del) == 0 {
panic("read-string: empty delimiter string, the string must contain one single-byte delimiter character")
}
if len(del) > 1 {
panic("read-string: delimiter string too long, read-string currenly only supports single-byte characters")
}
b, err := stream.BuffReader.BuffIOReader().ReadString(del[0])
if err != nil && err != io.EOF {
panic(fmt.Errorf("read-string: %w", err))
}
return string(b)
})
// (seek id pos sel) => offset seek to pos in stream id, depending on selector sel. Return the new offset.
interp.Def("seek", 3, func(a []any) any {
obj, ok := a[0].(*Boxed)
if !ok {
panic(fmt.Sprintf(`seek: not a valid port port, given %v`, Str(a[0])))
}
s, ok := interp.Streams().Load(obj)
if !ok {
panic(`seek: stream not valid`)
}
stream, ok := s.(*Stream)
if !ok {
panic("seek: stream not valid (internal error)")
}
if stream.LispSeeker.IOSeeker() == nil {
panic(fmt.Sprintf(`seek: the stream port %v is not seekable (no random access supported)`, Str(a[0])))
}
pos := ToInt64("seek", a[1])
var whence int
switch Str(a[2]) {
case "start":
whence = io.SeekStart
case "current":
whence = io.SeekCurrent
case "end":
whence = io.SeekEnd
default:
panic(fmt.Sprintf(`seek: selector must be one of '(start current end), given %v`, Str(a[2])))
}
offset, err := stream.LispSeeker.IOSeeker().Seek(pos, whence)
if err != nil {
panic(fmt.Errorf(`seek: %w`, err.Error()))
}
return goarith.AsNumber(offset)
})
// (read-binary id buff n) => int read n bytes of data from stream id into buff, return the number of bytes read
interp.Def("read-binary", 3, func(a []any) any {
obj, ok := a[0].(*Boxed)
if !ok {
panic(fmt.Sprintf("read-binary: expected valid port, given %v", Str(a[0])))
}
s, ok := interp.Streams().Load(obj)
if !ok {
panic("read-binary: stream not valid")
}
stream, ok := s.(*Stream)
if !ok {
panic("read-binary: stream not valid (internal error)")
}
blob := MustGetBoxed("read-binary", a[1], BoxedBlob)
buff := blob.Datum.([]byte)
n, exact := goarith.AsNumber(a[2]).Int()
if !exact {
panic(fmt.Sprintf("read-binary: expected integer, given %v", n))
}
k, err := io.ReadAtLeast(stream.LispReader.IOReader(), buff, n)
if err == EofToken {
return goarith.AsNumber(0)
}
if err == io.ErrUnexpectedEOF {
return goarith.AsNumber(k)
}
if err != nil {
panic(fmt.Errorf(`read-binary: %w`, err))
}
return goarith.AsNumber(k)
})
// (write-binary id buff n offset) => int write n bytes from buff at offset into stream id,
// return the number of bytes written
interp.Def("write-binary", 4, func(a []any) any {
obj, ok := a[0].(*Boxed)
if !ok {
panic(fmt.Sprintf("write-binary: expected valid port, given %v", Str(a[0])))
}
s, ok := interp.Streams().Load(obj)
if !ok {
panic("write-binary: stream not valid")
}
stream, ok := s.(*Stream)
if !ok {
panic("write-binary: stream not valid (internal error)")
}
blob := MustGetBoxed("write-binary", a[1], BoxedBlob)
buff := blob.Datum.([]byte)
n, exact := goarith.AsNumber(a[2]).Int()
if !exact {
panic(fmt.Sprintf("write-binary: expected integer for number of bytes to write, given %v", n))
}
k, exact := goarith.AsNumber(a[3]).Int()
if !exact {
panic(fmt.Sprintf("write-binary: expected integer for offset, given %v", k))
}
if len(buff) < n+k {
panic(`write-binary: buffer overflow`)
}
i, err := stream.LispWriter.IOWriter().Write(buff[k : n+k])
if err != nil {
panic(fmt.Errorf(`write-binary: %w`, err))
}
return goarith.AsNumber(i)
})
// (write-binary-at id buff n offset fpos) write datum to seekable stream id from 0-indexed offset into position fpos
interp.Def("write-binary-at", 5, func(a []any) any {
obj, ok := a[0].(*Boxed)
if !ok {
panic(fmt.Sprintf("write-binary-at: expected valid port, given %v", Str(a[0])))
}
s, ok := interp.Streams().Load(obj)
if !ok {
panic("write-binary-at: stream not valid")
}
stream, ok := s.(*Stream)
if !ok {
panic("write-binary-at: stream not valid (internal error)")
}
blob := MustGetBoxed("write-binary-at", a[1], BoxedBlob)
buff := blob.Datum.([]byte)
n, exact := goarith.AsNumber(a[2]).Int()
if !exact {
panic(fmt.Sprintf("write-binary-at: expected integer for number of bytes to write, given %v", n))
}
k, exact := goarith.AsNumber(a[3]).Int()
if !exact {
panic(fmt.Sprintf("write-binary-at: expected integer for buffer offset, given %v", k))
}
if len(buff) < n+k {
panic(`write-binary-at: buffer overflow`)
}
fpos := ToInt64("write-binary-at", a[4])
i, err := stream.LispWriterAt.IOWriterAt().WriteAt(buff[k:n+k], fpos)
if err != nil {
panic(fmt.Errorf(`write-binary-at: %w`, err))
}
return goarith.AsNumber(i)
})
// (stropen s) => p open a string as a stream
interp.Def("stropen", 1, func(a []any) any {
s := a[0].(string)
buff := bytes.NewBufferString(s)
obj := &Boxed{Datum: buff, Sort: BoxedStringPort, Valid: true}
in, ok := obj.Datum.(io.Reader)
if !ok {
panic(fmt.Errorf(`stropen:%w`, ErrPortNotReadable))
}
out, ok := obj.Datum.(io.Writer)
if !ok {
panic(fmt.Errorf(`stropen:%w`, ErrPortNotWritable))
}
stream := NewStream(NewInternalSource("stropen", s), in, out, nil, nil)
interp.Streams().Store(obj, stream)
return obj
})
// (dir [path]) => li obtain a list of files, if no path is specified the current directory is listed
interp.Def("dir", -1, func(a []any) any {
var current string
li := a[0].(*Cell)
if li == Nil {
current = "."
} else {
current = filepath.FromSlash(li.Car.(string))
}
file, err := os.Open(current)
if err != nil {
panic(fmt.Errorf(`dir: %w`, err))
}
defer file.Close()
names, err := file.Readdirnames(0)
if err != nil {
panic(fmt.Errorf(`dir: %w`, err))
}
arr := make([]any, len(names))
for i := range names {
arr[i] = names[i]
}
return ArrayToList(arr)
})
// (sysdir sel) => str obtain a special system path based on the given selector
interp.Def("sysdir", 1, func(a []any) any {
sym := a[0].(*Sym)
s := sym.String()
switch s {
case "data":
return xdg.DataHome
case "data-dirs":
return StrArrayToList(xdg.DataDirs)
case "config":
return xdg.ConfigHome
case "config-dirs":
return StrArrayToList(xdg.ConfigDirs)
case "state":
return xdg.StateHome
case "cache":
return xdg.CacheHome
case "runtime":
return xdg.RuntimeDir
case "home":
return xdg.Home
case "application-dirs":
return StrArrayToList(xdg.ApplicationDirs)
case "font-dirs":
return StrArrayToList(xdg.FontDirs)
case "desktop":
return xdg.UserDirs.Desktop
case "downloads":
return xdg.UserDirs.Download
case "documents":
return xdg.UserDirs.Documents
case "music":
return xdg.UserDirs.Music
case "pictures":
return xdg.UserDirs.Pictures
case "videos":
return xdg.UserDirs.Videos
case "templates":
return xdg.UserDirs.Templates
case "public", "shared":
return xdg.UserDirs.PublicShare
case "z3s5-data":
d := xdg.DataHome + "/z3s5"
os.MkdirAll(filepath.FromSlash(d), 0777)
return d
default:
panic(fmt.Sprintf("sysdir: unknown directory '%v, the selector must be one of '(data data-dirs config config-dirs state cache runtime home application-dirs font-dirs desktop downloads documents music pictures videos templates public shared z3s5-data)",
Str(sym)))
}
})
// (mkdir path [permissions]) creates all directories in path that don't exist yet
interp.Def("mkdir", -1, func(a []any) any {
c := a[0].(*Cell)
if c == Nil {
panic("mkdir: missing path argument")
}
perm := 0777
if c.CdrCell() != Nil {
perm = FilePermissionToInt("mkdir", c.CdrCell().Car)
}
os.MkdirAll(filepath.FromSlash(c.Car.(string)), os.FileMode(perm))
return Void
})
// (cd path) change the working directory to path
interp.Def("cd", 1, func(a []any) any {
if err := os.Chdir(filepath.FromSlash(a[0].(string))); err != nil {
panic(fmt.Errorf(`cd: %w`, err))
}
return Void
})
// (file-name file)
interp.Def("file-name", 1, func(a []any) any {
s := a[0].(string)
return filepath.Base(s)
})
// (file-display-name file)
interp.Def("file-display-name", 1, func(a []any) any {
s := a[0].(string)
return strings.TrimSuffix(filepath.Base(s), filepath.Ext(s))
})
// (file-suffix file)
interp.Def("file-suffix", 1, func(a []any) any {
s := a[0].(string)
s = filepath.Ext(s)
if len(s) > 0 {
s = s[1:]
}
return s
})
// (file-path file)
interp.Def("file-path", 1, func(a []any) any {
s := a[0].(string)
return filepath.Dir(s)
})
// (fdelete path) remove the file or directory at path
interp.Def("fdelete", 1, func(a []any) any {
if err := os.RemoveAll(a[0].(string)); err != nil {
panic(fmt.Errorf(`fdelete: %w`, err))
}
return Void
})
}
// FileFlagsToInt converts a Lisp list of symbolic flags to the corresponding host os flag int
// of the modes for opening and creating files.
func FileFlagsToInt(caller string, a *Cell) int {
fail := func() {
panic(fmt.Sprintf("%v: only one of read, write, read-write can be specified in file flags, given %v",
caller, Str(a)))
}
arr := ListToArray(a)
flag := 0
excl := false
for _, s := range arr {
sym, ok := s.(*Sym)
if !ok {
panic(fmt.Sprintf("%v: expected file mode flag symbol, given %v", caller, Str(s)))
}
switch sym.Name {
case "read", "r", "rdonly":
if excl {
fail()
}
flag = flag | os.O_RDONLY
excl = true
case "write", "w", "wronly":
if excl {
fail()
}
flag = flag | os.O_WRONLY
excl = true
case "read-write", "rw", "rdwr":
if excl {
fail()
}
flag = flag | os.O_RDWR
excl = true
case "append", "a":
flag = flag | os.O_APPEND
case "create", "c":
flag = flag | os.O_CREATE
case "excl", "exclusive", "e":
flag = flag | os.O_EXCL
case "sync", "s":
flag = flag | os.O_SYNC
case "trunc", "truncate", "t":
flag = flag | os.O_TRUNC
default:
panic(fmt.Sprintf(`%v: unknown file mode flag '%v in '%v`, caller, s, Str(a)))
}
}
return flag
}
// FilePermissionsToInt returns the file permissions as an integer value.
func FilePermissionToInt(caller string, a any) int {
n, exact := goarith.AsNumber(a).Int()
if !exact {
panic(fmt.Sprintf("%v: expected integer for file permissions, given %v", caller, Str(a)))
}
return n
}
/*
Copyright (c) 2019-2024 Erich Rast
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.
*/