Skip to content

Commit

Permalink
修复通过socket上传文件时可能出现的问题,添加检查node是否存在的函数
Browse files Browse the repository at this point in the history
  • Loading branch information
littlefish12345 committed Dec 6, 2021
1 parent 898323f commit 8fb4843
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
24 changes: 16 additions & 8 deletions file-system-upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,24 +358,32 @@ func UploadProcessSocketThread(conn *net.Conn, fileLength int64, blockCount *int
var imageHash string
stop := false
errTimes := 0
var buffer []byte
blockCountLock.Lock()
nowBlock := *blockCount
*blockCount = *blockCount + 1
var recvSize int64 = 0
if (nowBlock+1)*singleImageMaxSize > int64(fileLength) {
if nowBlock*singleImageMaxSize > int64(fileLength) {
break
}
buffer = make([]byte, fileLength-nowBlock*singleImageMaxSize)
recvSize = fileLength - nowBlock*singleImageMaxSize
} else {
buffer = make([]byte, singleImageMaxSize)
recvSize = singleImageMaxSize
}
buffer := make([]byte, recvSize)

_, err = (*conn).Read(buffer)
if err != nil && err != io.EOF {
runtime.GC()
threadsWaitGroup.Done()
panic(err)
var totalBytes int64 = 0
for {
num, err := (*conn).Read(buffer[totalBytes:recvSize])
if err != nil && err != io.EOF {
runtime.GC()
threadsWaitGroup.Done()
panic(err)
}
totalBytes = totalBytes + int64(num)
if totalBytes == recvSize {
break
}
}
blockCountLock.Unlock()

Expand Down
26 changes: 26 additions & 0 deletions file-system.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,32 @@ func GetTempPath(path string) ([][]string, error) { //获取内部path列表
return tempPath, nil
}

func DoesNodeExist(path string) (bool, string, error) { //查看节点是否存在 path:要查询的路径 返回值:是否存在, node类型
if rootNodeHash == "" {
return false, "", NotSetARootNodeYet()
}
if path == "/" {
return true, "0", nil
}

nodeRWLock.Lock()
tempPath, err := GetTempPath(GetPathFolder(path))
if err != nil {
nodeRWLock.Unlock()
return false, "", err
}
nodeData, err := DecodeNode(tempPath[len(tempPath)-1][1], true)
nodeRWLock.Unlock()
if err != nil {
return false, "", err
}

if fileData, ok := nodeData[GetPathFileName(path)]; ok {
return true, fileData[0], nil
}
return false, "", nil
}

func ListFile(path string) ([][]string, error) { //获取当前文件夹下所有东西(ls) path:要获取的路径 返回值:[[文件名,node类型], ...]
if rootNodeHash == "" {
return nil, NotSetARootNodeYet()
Expand Down

0 comments on commit 8fb4843

Please sign in to comment.