github.com/xraypb/Xray-core@v1.8.1/common/platform/filesystem/file.go (about) 1 package filesystem 2 3 import ( 4 "io" 5 "os" 6 7 "github.com/xraypb/Xray-core/common/buf" 8 "github.com/xraypb/Xray-core/common/platform" 9 ) 10 11 type FileReaderFunc func(path string) (io.ReadCloser, error) 12 13 var NewFileReader FileReaderFunc = func(path string) (io.ReadCloser, error) { 14 return os.Open(path) 15 } 16 17 func ReadFile(path string) ([]byte, error) { 18 reader, err := NewFileReader(path) 19 if err != nil { 20 return nil, err 21 } 22 defer reader.Close() 23 24 return buf.ReadAllToBytes(reader) 25 } 26 27 func ReadAsset(file string) ([]byte, error) { 28 return ReadFile(platform.GetAssetLocation(file)) 29 } 30 31 func CopyFile(dst string, src string) error { 32 bytes, err := ReadFile(src) 33 if err != nil { 34 return err 35 } 36 f, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY, 0o644) 37 if err != nil { 38 return err 39 } 40 defer f.Close() 41 42 _, err = f.Write(bytes) 43 return err 44 }