Browse Source

Fix: geodata reader for multi-platform compatibility (#964)

rurirei 4 years ago
parent
commit
238b87d26a
2 changed files with 9 additions and 3 deletions
  1. 3 3
      common/geodata/decode.go
  2. 6 0
      common/platform/filesystem/file.go

+ 3 - 3
common/geodata/decode.go

@@ -11,13 +11,13 @@ package geodata
 
 
 import (
 import (
 	"io"
 	"io"
-	"os"
 	"runtime"
 	"runtime"
 	"strings"
 	"strings"
 
 
 	"google.golang.org/protobuf/encoding/protowire"
 	"google.golang.org/protobuf/encoding/protowire"
 
 
 	"github.com/v2fly/v2ray-core/v4/common/errors"
 	"github.com/v2fly/v2ray-core/v4/common/errors"
+	"github.com/v2fly/v2ray-core/v4/common/platform/filesystem"
 )
 )
 
 
 //go:generate go run github.com/v2fly/v2ray-core/v4/common/errors/errorgen
 //go:generate go run github.com/v2fly/v2ray-core/v4/common/errors/errorgen
@@ -30,7 +30,7 @@ var (
 	errCodeNotFound                 = errors.New("code not found")
 	errCodeNotFound                 = errors.New("code not found")
 )
 )
 
 
-func emitBytes(f *os.File, code string) ([]byte, error) {
+func emitBytes(f io.ReadSeeker, code string) ([]byte, error) {
 	count := 1
 	count := 1
 	isInner := false
 	isInner := false
 	tempContainer := make([]byte, 0, 5)
 	tempContainer := make([]byte, 0, 5)
@@ -107,7 +107,7 @@ Loop:
 }
 }
 
 
 func Decode(filename, code string) ([]byte, error) {
 func Decode(filename, code string) ([]byte, error) {
-	f, err := os.Open(filename)
+	f, err := filesystem.NewFileSeeker(filename)
 	if err != nil {
 	if err != nil {
 		return nil, newError("failed to open file: ", filename).Base(err)
 		return nil, newError("failed to open file: ", filename).Base(err)
 	}
 	}

+ 6 - 0
common/platform/filesystem/file.go

@@ -8,10 +8,16 @@ import (
 	"github.com/v2fly/v2ray-core/v4/common/platform"
 	"github.com/v2fly/v2ray-core/v4/common/platform"
 )
 )
 
 
+type FileSeekerFunc func(path string) (io.ReadSeekCloser, error)
+
 type FileReaderFunc func(path string) (io.ReadCloser, error)
 type FileReaderFunc func(path string) (io.ReadCloser, error)
 
 
 type FileWriterFunc func(path string) (io.WriteCloser, error)
 type FileWriterFunc func(path string) (io.WriteCloser, error)
 
 
+var NewFileSeeker FileSeekerFunc = func(path string) (io.ReadSeekCloser, error) {
+	return os.Open(path)
+}
+
 var NewFileReader FileReaderFunc = func(path string) (io.ReadCloser, error) {
 var NewFileReader FileReaderFunc = func(path string) (io.ReadCloser, error) {
 	return os.Open(path)
 	return os.Open(path)
 }
 }