2019-02-10 13:04:11 -05:00
|
|
|
package filesystem
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
2021-02-16 15:31:50 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v4/common/buf"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/platform"
|
2019-02-10 13:04:11 -05:00
|
|
|
)
|
|
|
|
|
2021-05-03 12:12:47 -04:00
|
|
|
type FileSeekerFunc func(path string) (io.ReadSeekCloser, error)
|
|
|
|
|
2019-02-10 13:04:11 -05:00
|
|
|
type FileReaderFunc func(path string) (io.ReadCloser, error)
|
|
|
|
|
2021-04-06 15:08:03 -04:00
|
|
|
type FileWriterFunc func(path string) (io.WriteCloser, error)
|
|
|
|
|
2021-05-03 12:12:47 -04:00
|
|
|
var NewFileSeeker FileSeekerFunc = func(path string) (io.ReadSeekCloser, error) {
|
|
|
|
return os.Open(path)
|
|
|
|
}
|
|
|
|
|
2019-02-10 13:04:11 -05:00
|
|
|
var NewFileReader FileReaderFunc = func(path string) (io.ReadCloser, error) {
|
|
|
|
return os.Open(path)
|
|
|
|
}
|
|
|
|
|
2021-04-06 15:08:03 -04:00
|
|
|
var NewFileWriter FileWriterFunc = func(path string) (io.WriteCloser, error) {
|
|
|
|
return os.Create(path)
|
|
|
|
}
|
|
|
|
|
2019-02-10 13:04:11 -05:00
|
|
|
func ReadFile(path string) ([]byte, error) {
|
|
|
|
reader, err := NewFileReader(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer reader.Close()
|
|
|
|
|
|
|
|
return buf.ReadAllToBytes(reader)
|
|
|
|
}
|
|
|
|
|
2021-04-06 15:08:03 -04:00
|
|
|
func WriteFile(path string, payload []byte) error {
|
|
|
|
writer, err := NewFileWriter(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer writer.Close()
|
|
|
|
|
|
|
|
return buf.WriteAllBytes(writer, payload)
|
|
|
|
}
|
|
|
|
|
2019-02-10 13:04:11 -05:00
|
|
|
func ReadAsset(file string) ([]byte, error) {
|
|
|
|
return ReadFile(platform.GetAssetLocation(file))
|
|
|
|
}
|
|
|
|
|
|
|
|
func CopyFile(dst string, src string) error {
|
|
|
|
bytes, err := ReadFile(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-19 17:28:52 -04:00
|
|
|
f, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY, 0o644)
|
2019-02-10 13:04:11 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
_, err = f.Write(bytes)
|
|
|
|
return err
|
|
|
|
}
|