Skip to content

Commit d8ec9c4

Browse files
committed
Deprecated bad rw funcs
1 parent a333493 commit d8ec9c4

File tree

5 files changed

+82
-78
lines changed

5 files changed

+82
-78
lines changed

common/rw/count.go

Lines changed: 0 additions & 27 deletions
This file was deleted.

common/rw/file.go

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,64 @@ import (
99
"github.com/sagernet/sing/common"
1010
)
1111

12-
func FileExists(path string) bool {
13-
return common.Error(os.Stat(path)) == nil
12+
func IsFile(path string) bool {
13+
stat, err := os.Stat(path)
14+
if err != nil {
15+
return false
16+
}
17+
return !stat.IsDir()
1418
}
1519

16-
func CopyFile(srcPath, dstPath string) error {
17-
srcFile, err := os.Open(srcPath)
20+
func IsDir(path string) bool {
21+
stat, err := os.Stat(path)
1822
if err != nil {
19-
return err
23+
return false
2024
}
21-
defer srcFile.Close()
22-
if strings.Contains(dstPath, "/") {
23-
parent := dstPath[:strings.LastIndex(dstPath, "/")]
24-
if !FileExists(parent) {
25-
err = os.MkdirAll(parent, 0o755)
25+
return stat.IsDir()
26+
}
27+
28+
func MkdirParent(path string) error {
29+
if strings.Contains(path, string(os.PathSeparator)) {
30+
parent := path[:strings.LastIndex(path, string(os.PathSeparator))]
31+
if !IsDir(parent) {
32+
err := os.MkdirAll(parent, 0o755)
2633
if err != nil {
2734
return err
2835
}
2936
}
3037
}
31-
dstFile, err := os.Create(dstPath)
38+
return nil
39+
}
40+
41+
func CopyFile(srcPath, dstPath string) error {
42+
srcFile, err := os.Open(srcPath)
43+
if err != nil {
44+
return err
45+
}
46+
defer srcFile.Close()
47+
srcStat, err := srcFile.Stat()
48+
if err != nil {
49+
return err
50+
}
51+
err = MkdirParent(dstPath)
52+
if err != nil {
53+
return err
54+
}
55+
dstFile, err := os.OpenFile(dstPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, srcStat.Mode())
3256
if err != nil {
3357
return err
3458
}
3559
defer dstFile.Close()
36-
return common.Error(io.Copy(dstFile, srcFile))
60+
_, err = io.Copy(dstFile, srcFile)
61+
return err
62+
}
63+
64+
// Deprecated: use IsFile and IsDir instead.
65+
func FileExists(path string) bool {
66+
return common.Error(os.Stat(path)) == nil
3767
}
3868

69+
// Deprecated: use MkdirParent and os.WriteFile instead.
3970
func WriteFile(path string, content []byte) error {
4071
if strings.Contains(path, "/") {
4172
parent := path[:strings.LastIndex(path, "/")]
@@ -56,6 +87,7 @@ func WriteFile(path string, content []byte) error {
5687
return err
5788
}
5889

90+
// Deprecated: wtf is this?
5991
func ReadJSON(path string, data any) error {
6092
content, err := os.ReadFile(path)
6193
if err != nil {
@@ -68,6 +100,7 @@ func ReadJSON(path string, data any) error {
68100
return nil
69101
}
70102

103+
// Deprecated: wtf is this?
71104
func WriteJSON(path string, data any) error {
72105
content, err := json.Marshal(data)
73106
if err != nil {

common/rw/read.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ import (
66
"github.com/sagernet/sing/common"
77
)
88

9-
func Skip(reader io.Reader) error {
10-
return SkipN(reader, 1)
11-
}
12-
139
func SkipN(reader io.Reader, size int) error {
1410
return common.Error(io.CopyN(Discard, reader, int64(size)))
1511
}
1612

13+
// Deprecated: wtf is this?
14+
func Skip(reader io.Reader) error {
15+
return SkipN(reader, 1)
16+
}
17+
18+
// Deprecated: wtf is this?
1719
func ReadByte(reader io.Reader) (byte, error) {
1820
if br, isBr := reader.(io.ByteReader); isBr {
1921
return br.ReadByte()
@@ -25,6 +27,7 @@ func ReadByte(reader io.Reader) (byte, error) {
2527
return b[0], nil
2628
}
2729

30+
// Deprecated: wtf is this?
2831
func ReadBytes(reader io.Reader, size int) ([]byte, error) {
2932
b := make([]byte, size)
3033
if err := common.Error(io.ReadFull(reader, b)); err != nil {
@@ -33,6 +36,7 @@ func ReadBytes(reader io.Reader, size int) ([]byte, error) {
3336
return b, nil
3437
}
3538

39+
// Deprecated: wtf is this?
3640
func ReadString(reader io.Reader, size int) (string, error) {
3741
b, err := ReadBytes(reader, size)
3842
if err != nil {

common/rw/varint.go

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package rw
22

33
import (
4-
"encoding/binary"
54
"io"
65

76
"github.com/sagernet/sing/common"
7+
"github.com/sagernet/sing/common/binary"
8+
"github.com/sagernet/sing/common/varbin"
89
)
910

11+
// Deprecated: create a *bufio.Reader instead.
1012
type stubByteReader struct {
1113
io.Reader
1214
}
@@ -15,47 +17,32 @@ func (r stubByteReader) ReadByte() (byte, error) {
1517
return ReadByte(r.Reader)
1618
}
1719

20+
// Deprecated: create a *bufio.Reader instead.
1821
func ToByteReader(reader io.Reader) io.ByteReader {
1922
if byteReader, ok := reader.(io.ByteReader); ok {
2023
return byteReader
2124
}
2225
return &stubByteReader{reader}
2326
}
2427

28+
// Deprecated: Use binary.ReadUvarint instead.
2529
func ReadUVariant(reader io.Reader) (uint64, error) {
30+
//goland:noinspection GoDeprecation
2631
return binary.ReadUvarint(ToByteReader(reader))
2732
}
2833

34+
// Deprecated: Use varbin.UvarintLen instead.
2935
func UVariantLen(x uint64) int {
30-
switch {
31-
case x < 1<<(7*1):
32-
return 1
33-
case x < 1<<(7*2):
34-
return 2
35-
case x < 1<<(7*3):
36-
return 3
37-
case x < 1<<(7*4):
38-
return 4
39-
case x < 1<<(7*5):
40-
return 5
41-
case x < 1<<(7*6):
42-
return 6
43-
case x < 1<<(7*7):
44-
return 7
45-
case x < 1<<(7*8):
46-
return 8
47-
case x < 1<<(7*9):
48-
return 9
49-
default:
50-
return 10
51-
}
36+
return varbin.UvarintLen(x)
5237
}
5338

39+
// Deprecated: Use varbin.WriteUvarint instead.
5440
func WriteUVariant(writer io.Writer, value uint64) error {
5541
var b [8]byte
5642
return common.Error(writer.Write(b[:binary.PutUvarint(b[:], value)]))
5743
}
5844

45+
// Deprecated: Use varbin.Write instead.
5946
func WriteVString(writer io.Writer, value string) error {
6047
err := WriteUVariant(writer, uint64(len(value)))
6148
if err != nil {
@@ -64,6 +51,7 @@ func WriteVString(writer io.Writer, value string) error {
6451
return WriteString(writer, value)
6552
}
6653

54+
// Deprecated: Use varbin.ReadValue instead.
6755
func ReadVString(reader io.Reader) (string, error) {
6856
length, err := binary.ReadUvarint(ToByteReader(reader))
6957
if err != nil {

common/rw/write.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,10 @@ import (
66
"github.com/sagernet/sing/common"
77
)
88

9+
// Deprecated: wtf is this?
910
var ZeroBytes = make([]byte, 1024)
1011

11-
func WriteByte(writer io.Writer, b byte) error {
12-
return common.Error(writer.Write([]byte{b}))
13-
}
14-
15-
func WriteBytes(writer io.Writer, b []byte) error {
16-
return common.Error(writer.Write(b))
17-
}
18-
19-
func WriteZero(writer io.Writer) error {
20-
return WriteByte(writer, 0)
21-
}
22-
12+
// Deprecated: wtf is this?
2313
func WriteZeroN(writer io.Writer, size int) error {
2414
var index int
2515
for index < size {
@@ -38,6 +28,22 @@ func WriteZeroN(writer io.Writer, size int) error {
3828
return nil
3929
}
4030

31+
// Deprecated: wtf is this?
32+
func WriteByte(writer io.Writer, b byte) error {
33+
return common.Error(writer.Write([]byte{b}))
34+
}
35+
36+
// Deprecated: wtf is this?
37+
func WriteBytes(writer io.Writer, b []byte) error {
38+
return common.Error(writer.Write(b))
39+
}
40+
41+
// Deprecated: wtf is this?
42+
func WriteZero(writer io.Writer) error {
43+
return WriteByte(writer, 0)
44+
}
45+
46+
// Deprecated: wtf is this?
4147
func WriteString(writer io.Writer, str string) error {
4248
return WriteBytes(writer, []byte(str))
4349
}

0 commit comments

Comments
 (0)