Warehouse.FileExists return fileSize instead of fileInfo

This commit is contained in:
Kleissner
2022-02-21 22:35:20 +01:00
parent 8d62724d0c
commit ee22bf641a
6 changed files with 20 additions and 21 deletions

View File

@@ -249,18 +249,18 @@ func (peer *PeerInfo) cmdTransfer(msg *protocol.MessageTransfer, connection *Con
switch msg.Control {
case protocol.TransferControlRequestStart:
// First check if the file available in the warehouse.
_, fileInfo, status, _ := peer.Backend.UserWarehouse.FileExists(msg.Hash)
_, fileSize, status, _ := peer.Backend.UserWarehouse.FileExists(msg.Hash)
if status != warehouse.StatusOK {
// File not available.
peer.sendTransfer(nil, protocol.TransferControlNotAvailable, msg.TransferProtocol, msg.Hash, 0, 0, msg.Sequence, uuid.UUID{}, false)
return
} else if msg.Limit > 0 && fileInfo.Size() < int64(msg.Offset)+int64(msg.Limit) {
} else if msg.Limit > 0 && fileSize < msg.Offset+msg.Limit {
// If the read limit is out of bounds, this request is considered invalid and silently discarded.
return
}
// Create a local UDT client to connect to the remote UDT server and serve the file!
go peer.startFileTransferUDT(msg.Hash, uint64(fileInfo.Size()), msg.Offset, msg.Limit, msg.Sequence, msg.TransferID, msg.TransferProtocol)
go peer.startFileTransferUDT(msg.Hash, fileSize, msg.Offset, msg.Limit, msg.Sequence, msg.TransferID, msg.TransferProtocol)
case protocol.TransferControlActive:
if v, ok := msg.SequenceInfo.Data.(*virtualPacketConn); ok {

View File

@@ -19,10 +19,10 @@ import (
const merkleCompanionExt = ".merkle"
// MerkleFileExists checks if the merkle companion file exists. It returns StatusInvalidHash, StatusFileNotFound, or StatusOK.
func (wh *Warehouse) MerkleFileExists(hash []byte) (path string, fileInfo os.FileInfo, status int, err error) {
func (wh *Warehouse) MerkleFileExists(hash []byte) (path string, fileSize uint64, status int, err error) {
hashA, err := ValidateHash(hash)
if err != nil {
return "", nil, StatusInvalidHash, err
return "", 0, StatusInvalidHash, err
}
a, b := buildPath(wh.Directory, hashA)
@@ -30,10 +30,10 @@ func (wh *Warehouse) MerkleFileExists(hash []byte) (path string, fileInfo os.Fil
if fileInfo, err := os.Stat(path); err == nil {
// file exists
return path, fileInfo, StatusOK, nil
return path, uint64(fileInfo.Size()), StatusOK, nil
}
return "", nil, StatusFileNotFound, os.ErrNotExist
return "", 0, StatusFileNotFound, os.ErrNotExist
}
// createMerkleCompanionFile creates a merkle companion file. If the merkle companion file already exists, it is overwritten.

View File

@@ -210,10 +210,10 @@ func (wh *Warehouse) DeleteFile(hash []byte) (status int, err error) {
}
// FileExists checks if the file exists. It returns StatusInvalidHash, StatusFileNotFound, or StatusOK.
func (wh *Warehouse) FileExists(hash []byte) (path string, fileInfo os.FileInfo, status int, err error) {
func (wh *Warehouse) FileExists(hash []byte) (path string, fileSize uint64, status int, err error) {
hashA, err := ValidateHash(hash)
if err != nil {
return "", nil, StatusInvalidHash, err
return "", 0, StatusInvalidHash, err
}
a, b := buildPath(wh.Directory, hashA)
@@ -221,10 +221,10 @@ func (wh *Warehouse) FileExists(hash []byte) (path string, fileInfo os.FileInfo,
if fileInfo, err := os.Stat(path); err == nil {
// file exists
return path, fileInfo, StatusOK, nil
return path, uint64(fileInfo.Size()), StatusOK, nil
}
return "", nil, StatusFileNotFound, os.ErrNotExist
return "", 0, StatusFileNotFound, os.ErrNotExist
}
// DeleteWarehouse deletes all files in the warehouse

View File

@@ -178,17 +178,17 @@ func (info *downloadInfo) storeDownloadData(data []byte, offset uint64) (status
func (info *downloadInfo) DownloadSelf() {
// Check if the file is available in the local warehouse.
_, fileInfo, status, _ := info.backend.UserWarehouse.FileExists(info.hash)
_, fileSize, status, _ := info.backend.UserWarehouse.FileExists(info.hash)
if status != warehouse.StatusOK {
info.status = DownloadCanceled
return
}
info.file.Size = uint64(fileInfo.Size())
info.file.Size = fileSize
info.status = DownloadActive
// read the file
status, bytesRead, _ := info.backend.UserWarehouse.ReadFile(info.hash, 0, int64(fileInfo.Size()), info.DiskFile.Handle)
status, bytesRead, _ := info.backend.UserWarehouse.ReadFile(info.hash, 0, int64(info.file.Size), info.DiskFile.Handle)
info.DiskFile.StoredSize = uint64(bytesRead)

View File

@@ -107,11 +107,10 @@ func (api *WebapiInstance) apiFileRead(w http.ResponseWriter, r *http.Request) {
// Limit is optional, 0 means the entire file.
func serveFileFromWarehouse(backend *core.Backend, w http.ResponseWriter, fileHash []byte, offset, limit uint64, ranges []HTTPRange) (valid bool) {
// Check if the file is available in the local warehouse.
_, fileInfo, status, _ := backend.UserWarehouse.FileExists(fileHash)
_, fileSize, status, _ := backend.UserWarehouse.FileExists(fileHash)
if status != warehouse.StatusOK {
return false
}
fileSize := uint64(fileInfo.Size())
// validate offset and limit
if limit > 0 && offset+limit > fileSize {
@@ -124,7 +123,7 @@ func serveFileFromWarehouse(backend *core.Backend, w http.ResponseWriter, fileHa
limit = fileSize - offset
}
setContentLengthRangeHeader(w, offset, limit, uint64(fileInfo.Size()), ranges)
setContentLengthRangeHeader(w, offset, limit, fileSize, ranges)
status, _, _ = backend.UserWarehouse.ReadFile(fileHash, int64(offset), int64(limit), w)

View File

@@ -154,11 +154,11 @@ func (api *WebapiInstance) apiBlockchainFileAdd(w http.ResponseWriter, r *http.R
if _, err := warehouse.ValidateHash(file.Hash); err != nil {
http.Error(w, "", http.StatusBadRequest)
return
} else if _, fileInfo, status, _ := api.backend.UserWarehouse.FileExists(file.Hash); status != warehouse.StatusOK {
} else if _, fileSize, status, _ := api.backend.UserWarehouse.FileExists(file.Hash); status != warehouse.StatusOK {
EncodeJSON(api.backend, w, r, apiBlockchainBlockStatus{Status: blockchain.StatusNotInWarehouse})
return
} else {
file.Size = uint64(fileInfo.Size())
file.Size = fileSize
}
} else {
file.Hash = protocol.HashData(nil)
@@ -263,11 +263,11 @@ func (api *WebapiInstance) apiBlockchainFileUpdate(w http.ResponseWriter, r *htt
if _, err := warehouse.ValidateHash(file.Hash); err != nil {
http.Error(w, "", http.StatusBadRequest)
return
} else if _, fileInfo, status, _ := api.backend.UserWarehouse.FileExists(file.Hash); status != warehouse.StatusOK {
} else if _, fileSize, status, _ := api.backend.UserWarehouse.FileExists(file.Hash); status != warehouse.StatusOK {
EncodeJSON(api.backend, w, r, apiBlockchainBlockStatus{Status: blockchain.StatusNotInWarehouse})
return
} else {
file.Size = uint64(fileInfo.Size())
file.Size = fileSize
}
} else {
file.Hash = protocol.HashData(nil)