Skip to content

Commit

Permalink
Deprecated bad rw funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Jun 23, 2024
1 parent ca2310b commit 01b7350
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 47 deletions.
57 changes: 45 additions & 12 deletions common/rw/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "/")]
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions common/rw/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
37 changes: 14 additions & 23 deletions common/rw/varint.go
Original file line number Diff line number Diff line change
@@ -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
}
Expand All @@ -15,56 +17,45 @@ 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
}
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
Expand Down
28 changes: 16 additions & 12 deletions common/rw/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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))
}

0 comments on commit 01b7350

Please sign in to comment.