Skip to content

Commit 2d9c37d

Browse files
committed
use fs wrapper
1 parent b60bde1 commit 2d9c37d

5 files changed

Lines changed: 708 additions & 12 deletions

File tree

pkg/fs/fs.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright (c) 2012, Suryandaru Triandana <syndtr@gmail.com>
2+
// All rights reservefs.
3+
//
4+
// Use of this source code is governed by a BSD-style license that can be
5+
// found in the LICENSE file.
6+
7+
package fs
8+
9+
import (
10+
"io"
11+
"os"
12+
"path/filepath"
13+
)
14+
15+
// osFile is an interface for interacting with a file. In Go, the underlying
16+
// type of osFile is always simply *os.File. In JavaScript/Wasm, the underlying
17+
// type is a wrapper type which mimics the functionality of os.File.
18+
type OsFile interface {
19+
// Stat returns the FileInfo structure describing file. If there is an error,
20+
// it will be of type *PathError.
21+
Stat() (os.FileInfo, error)
22+
// Read reads up to len(b) bytes from the File. It returns the number of bytes
23+
// read and any error encountered. At end of file, Read returns 0, io.EOF.
24+
Read(b []byte) (n int, err error)
25+
// ReadAt reads len(b) bytes from the File starting at byte offset off. It
26+
// returns the number of bytes read and the error, if any. ReadAt always
27+
// returns a non-nil error when n < len(b). At end of file, that error is
28+
// io.EOF.
29+
ReadAt(b []byte, off int64) (n int, err error)
30+
// Write writes len(b) bytes to the File. It returns the number of bytes
31+
// written and an error, if any. Write returns a non-nil error when n !=
32+
// len(b).
33+
Write(b []byte) (n int, err error)
34+
// Seek sets the offset for the next Read or Write on file to offset,
35+
// interpreted according to whence: 0 means relative to the origin of the
36+
// file, 1 means relative to the current offset, and 2 means relative to the
37+
// end. It returns the new offset and an error, if any. The behavior of Seek
38+
// on a file opened with O_APPEND is not specified.
39+
Seek(offset int64, whence int) (ret int64, err error)
40+
// Sync commits the current contents of the file to stable storage. Typically,
41+
// this means flushing the file system's in-memory copy of recently written
42+
// data to disk.
43+
Sync() error
44+
// Close closes the File, rendering it unusable for I/O. On files that support
45+
// SetDeadline, any pending I/O operations will be canceled and return
46+
// immediately with an error.
47+
Close() error
48+
49+
WriteAt([]byte, int64) (int, error)
50+
Truncate(size int64) error
51+
WriteString(s string) (n int, err error)
52+
}
53+
54+
func ReadFile(filename string) ([]byte, error) {
55+
f, err := osOpen(filename)
56+
if err != nil {
57+
return nil, err
58+
}
59+
defer f.Close()
60+
61+
return io.ReadAll(f)
62+
}
63+
64+
func MkdirAll(path string, perm os.FileMode) error {
65+
return osMkdirAll(path, perm)
66+
}
67+
68+
func Open(name string) (OsFile, error) {
69+
return osOpen(name)
70+
}
71+
72+
func OpenFile(name string, flag int, perm os.FileMode) (OsFile, error) {
73+
return osOpenFile(name, flag, perm)
74+
}
75+
76+
func WriteFile(filename string, data []byte, perm os.FileMode) error {
77+
if err := MkdirAll(filepath.Dir(filename), 0o755); err != nil {
78+
return err
79+
}
80+
return writeFile(filename, data, perm)
81+
}
82+
83+
func Stat(name string) (os.FileInfo, error) {
84+
return fsStat(name)
85+
}
86+
87+
func Remove(name string) error {
88+
return osRemove(name)
89+
}

0 commit comments

Comments
 (0)