forked from quic-go/quic-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
disk.go
56 lines (46 loc) · 1.12 KB
/
disk.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package storage
/*
功能一:为服务端提供数据文件的读取能力
功能二:提供基于文件名的解析服务
*/
import (
"errors"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
)
const (
DataFileExtension string = ".data"
CRCFileExtension string = ".crc"
)
var (
ErrorOpenDataFile = errors.New("error opening data file")
ErrorReadDataFile = errors.New("error opening data file")
)
var (
Path = "../../data/"
)
// ReadFile 读取一个磁盘文件
func ReadFile(path string) ([]byte, error) {
fmt.Println("path: ", path)
file, err := os.Open(path)
if err != nil {
return nil, ErrorOpenDataFile
}
defer file.Close()
content, err := ioutil.ReadAll(file)
if err != nil {
return nil, ErrorReadDataFile
}
return content, nil
}
// parseDataFilename 解析数据文件的文件名+扩展名部分,并返回 FID 和 文件的块序号
func ParseDataFilename(fileBaseName string) (fid string, blockIndex int) {
filename := strings.Split(fileBaseName, ".")[0]
filenameSlice := strings.Split(filename, ":")
fid = filenameSlice[0]
blockIndex, _ = strconv.Atoi(filenameSlice[1])
return fid, blockIndex
}