From ee22bf641a08e21afadab637dbfe9b6ea7737a4b Mon Sep 17 00:00:00 2001 From: Kleissner Date: Mon, 21 Feb 2022 22:35:20 +0100 Subject: [PATCH] Warehouse.FileExists return fileSize instead of fileInfo --- Commands.go | 6 +++--- warehouse/Merkle.go | 8 ++++---- warehouse/Store.go | 8 ++++---- webapi/Download Transfer.go | 6 +++--- webapi/File IO.go | 5 ++--- webapi/File.go | 8 ++++---- 6 files changed, 20 insertions(+), 21 deletions(-) diff --git a/Commands.go b/Commands.go index 07098f8..abf5120 100644 --- a/Commands.go +++ b/Commands.go @@ -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 { diff --git a/warehouse/Merkle.go b/warehouse/Merkle.go index 4d1f9da..d5a47dd 100644 --- a/warehouse/Merkle.go +++ b/warehouse/Merkle.go @@ -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. diff --git a/warehouse/Store.go b/warehouse/Store.go index 10471db..0f79bce 100644 --- a/warehouse/Store.go +++ b/warehouse/Store.go @@ -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 diff --git a/webapi/Download Transfer.go b/webapi/Download Transfer.go index a585da8..b3a59d2 100644 --- a/webapi/Download Transfer.go +++ b/webapi/Download Transfer.go @@ -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) diff --git a/webapi/File IO.go b/webapi/File IO.go index 0144b5c..cde24dc 100644 --- a/webapi/File IO.go +++ b/webapi/File IO.go @@ -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) diff --git a/webapi/File.go b/webapi/File.go index d7c1e26..a7da52f 100644 --- a/webapi/File.go +++ b/webapi/File.go @@ -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)