mirror of https://github.com/v2ray/v2ray-core
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
809 B
45 lines
809 B
6 years ago
|
package filesystem
|
||
|
|
||
|
import (
|
||
|
"io"
|
||
|
"os"
|
||
|
|
||
|
"v2ray.com/core/common/buf"
|
||
|
"v2ray.com/core/common/platform"
|
||
|
)
|
||
|
|
||
|
type FileReaderFunc func(path string) (io.ReadCloser, error)
|
||
|
|
||
|
var NewFileReader FileReaderFunc = func(path string) (io.ReadCloser, error) {
|
||
|
return os.Open(path)
|
||
|
}
|
||
|
|
||
|
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
|
||
|
}
|
||
|
f, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY, 0644)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
defer f.Close()
|
||
|
|
||
|
_, err = f.Write(bytes)
|
||
|
return err
|
||
|
}
|