From 01b735009c376a18ad7c1eeceb3257399f2694e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Sun, 23 Jun 2024 16:00:05 +0800 Subject: [PATCH] Deprecated bad rw funcs --- common/rw/file.go | 57 +++++++++++++++++++++++++++++++++++---------- common/rw/read.go | 3 +++ common/rw/varint.go | 37 +++++++++++------------------ common/rw/write.go | 28 ++++++++++++---------- 4 files changed, 78 insertions(+), 47 deletions(-) diff --git a/common/rw/file.go b/common/rw/file.go index 2bb85ac39..3600b1ece 100644 --- a/common/rw/file.go +++ b/common/rw/file.go @@ -9,33 +9,64 @@ import ( "github.com/sagernet/sing/common" ) -func FileExists(path string) bool { - return common.Error(os.Stat(path)) == nil +func IsFile(path string) bool { + stat, err := os.Stat(path) + if err != nil { + return false + } + return !stat.IsDir() } -func CopyFile(srcPath, dstPath string) error { - srcFile, err := os.Open(srcPath) +func IsDir(path string) bool { + stat, err := os.Stat(path) if err != nil { - return err + return false } - defer srcFile.Close() - if strings.Contains(dstPath, "/") { - parent := dstPath[:strings.LastIndex(dstPath, "/")] - if !FileExists(parent) { - err = os.MkdirAll(parent, 0o755) + return stat.IsDir() +} + +func MkdirParent(path string) error { + if strings.Contains(path, string(os.PathSeparator)) { + parent := path[:strings.LastIndex(path, string(os.PathSeparator))] + if !IsDir(parent) { + err := os.MkdirAll(parent, 0o755) if err != nil { return err } } } - dstFile, err := os.Create(dstPath) + return nil +} + +func CopyFile(srcPath, dstPath string) error { + srcFile, err := os.Open(srcPath) + if err != nil { + return err + } + defer srcFile.Close() + srcStat, err := srcFile.Stat() + if err != nil { + return err + } + err = MkdirParent(dstPath) + if err != nil { + return err + } + dstFile, err := os.OpenFile(dstPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, srcStat.Mode()) if err != nil { return err } defer dstFile.Close() - return common.Error(io.Copy(dstFile, srcFile)) + _, err = io.Copy(dstFile, srcFile) + return err +} + +// Deprecated: use IsFile and IsDir instead. +func FileExists(path string) bool { + return common.Error(os.Stat(path)) == nil } +// Deprecated: use MkdirParent and os.WriteFile instead. func WriteFile(path string, content []byte) error { if strings.Contains(path, "/") { parent := path[:strings.LastIndex(path, "/")] @@ -56,6 +87,7 @@ func WriteFile(path string, content []byte) error { return err } +// Deprecated: wtf is this? func ReadJSON(path string, data any) error { content, err := os.ReadFile(path) if err != nil { @@ -68,6 +100,7 @@ func ReadJSON(path string, data any) error { return nil } +// Deprecated: wtf is this? func WriteJSON(path string, data any) error { content, err := json.Marshal(data) if err != nil { diff --git a/common/rw/read.go b/common/rw/read.go index d536a710a..6bd9df35d 100644 --- a/common/rw/read.go +++ b/common/rw/read.go @@ -14,6 +14,7 @@ func SkipN(reader io.Reader, size int) error { return common.Error(io.CopyN(Discard, reader, int64(size))) } +// Deprecated: wtf is this? func ReadByte(reader io.Reader) (byte, error) { if br, isBr := reader.(io.ByteReader); isBr { return br.ReadByte() @@ -25,6 +26,7 @@ func ReadByte(reader io.Reader) (byte, error) { return b[0], nil } +// Deprecated: wtf is this? func ReadBytes(reader io.Reader, size int) ([]byte, error) { b := make([]byte, size) if err := common.Error(io.ReadFull(reader, b)); err != nil { @@ -33,6 +35,7 @@ func ReadBytes(reader io.Reader, size int) ([]byte, error) { return b, nil } +// Deprecated: wtf is this? func ReadString(reader io.Reader, size int) (string, error) { b, err := ReadBytes(reader, size) if err != nil { diff --git a/common/rw/varint.go b/common/rw/varint.go index cecd7ec47..4c4cb3c3c 100644 --- a/common/rw/varint.go +++ b/common/rw/varint.go @@ -1,12 +1,14 @@ package rw import ( - "encoding/binary" "io" "github.com/sagernet/sing/common" + "github.com/sagernet/sing/common/binary" + "github.com/sagernet/sing/common/varbin" ) +// Deprecated: create a *bufio.Reader instead. type stubByteReader struct { io.Reader } @@ -15,48 +17,35 @@ func (r stubByteReader) ReadByte() (byte, error) { return ReadByte(r.Reader) } +// Deprecated: create a *bufio.Reader instead. func ToByteReader(reader io.Reader) io.ByteReader { if byteReader, ok := reader.(io.ByteReader); ok { return byteReader } + //goland:noinspection GoDeprecation return &stubByteReader{reader} } +// Deprecated: Use binary.ReadUvarint instead. func ReadUVariant(reader io.Reader) (uint64, error) { + //goland:noinspection GoDeprecation return binary.ReadUvarint(ToByteReader(reader)) } +// Deprecated: Use binary.UvarintLen instead. func UVariantLen(x uint64) int { - switch { - case x < 1<<(7*1): - return 1 - case x < 1<<(7*2): - return 2 - case x < 1<<(7*3): - return 3 - case x < 1<<(7*4): - return 4 - case x < 1<<(7*5): - return 5 - case x < 1<<(7*6): - return 6 - case x < 1<<(7*7): - return 7 - case x < 1<<(7*8): - return 8 - case x < 1<<(7*9): - return 9 - default: - return 10 - } + return varbin.UvarintLen(x) } +// Deprecated: Use binary.WriteUvarint instead. func WriteUVariant(writer io.Writer, value uint64) error { var b [8]byte return common.Error(writer.Write(b[:binary.PutUvarint(b[:], value)])) } +// Deprecated: Use binary.WriteData instead. func WriteVString(writer io.Writer, value string) error { + //goland:noinspection GoDeprecation err := WriteUVariant(writer, uint64(len(value))) if err != nil { return err @@ -64,7 +53,9 @@ func WriteVString(writer io.Writer, value string) error { return WriteString(writer, value) } +// Deprecated: Use binary.ReadData instead. func ReadVString(reader io.Reader) (string, error) { + //goland:noinspection GoDeprecation length, err := binary.ReadUvarint(ToByteReader(reader)) if err != nil { return "", err diff --git a/common/rw/write.go b/common/rw/write.go index ad635b783..8691a5340 100644 --- a/common/rw/write.go +++ b/common/rw/write.go @@ -8,18 +8,6 @@ import ( var ZeroBytes = make([]byte, 1024) -func WriteByte(writer io.Writer, b byte) error { - return common.Error(writer.Write([]byte{b})) -} - -func WriteBytes(writer io.Writer, b []byte) error { - return common.Error(writer.Write(b)) -} - -func WriteZero(writer io.Writer) error { - return WriteByte(writer, 0) -} - func WriteZeroN(writer io.Writer, size int) error { var index int for index < size { @@ -38,6 +26,22 @@ func WriteZeroN(writer io.Writer, size int) error { return nil } +// Deprecated: wtf is this? +func WriteByte(writer io.Writer, b byte) error { + return common.Error(writer.Write([]byte{b})) +} + +// Deprecated: wtf is this? +func WriteBytes(writer io.Writer, b []byte) error { + return common.Error(writer.Write(b)) +} + +// Deprecated: wtf is this? +func WriteZero(writer io.Writer) error { + return WriteByte(writer, 0) +} + +// Deprecated: wtf is this? func WriteString(writer io.Writer, str string) error { return WriteBytes(writer, []byte(str)) }