Xray-core/common/platform/filesystem/file.go

45 lines
832 B
Go
Raw Normal View History

2020-11-25 11:01:53 +00:00
package filesystem
import (
"io"
"os"
2020-12-04 01:36:16 +00:00
"github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/platform"
2020-11-25 11:01:53 +00:00
)
type FileReaderFunc func(path string) (io.ReadCloser, error)
var NewFileReader FileReaderFunc = func(path string) (io.ReadCloser, error) {
return os.Open(path)
2020-11-25 11:01:53 +00:00
}
func ReadFile(path string) ([]byte, error) {
reader, err := NewFileReader(path)
if err != nil {
return nil, err
}
defer reader.Close()
return buf.ReadAllToBytes(reader)
}
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-10-19 16:57:14 +00:00
f, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY, 0o644)
2020-11-25 11:01:53 +00:00
if err != nil {
return err
}
defer f.Close()
_, err = f.Write(bytes)
return err
}