From af9d3bc7b305bcc1e2dedf800b632cf02341b542 Mon Sep 17 00:00:00 2001 From: Boris Melnik Date: Thu, 14 Dec 2023 12:52:10 +0300 Subject: [PATCH] fix(history-archive): Skip dowloading data from torrent if free space is not enought (#4449) --- protocol/communities/errors.go | 1 + protocol/communities/manager.go | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/protocol/communities/errors.go b/protocol/communities/errors.go index 3ab6fb343..1ef53202b 100644 --- a/protocol/communities/errors.go +++ b/protocol/communities/errors.go @@ -43,3 +43,4 @@ var ErrCannotBanOwnerOrAdmin = errors.New("not allowed to ban admin or owner") var ErrInvalidManageTokensPermission = errors.New("no privileges to manage tokens") var ErrRevealedAccountsAbsent = errors.New("revealed accounts is absent") var ErrNoRevealedAccountsSignature = errors.New("revealed accounts without the signature") +var ErrNoFreeSpaceForHistoryArchives = errors.New("history archive: No free space for downloading history archives") diff --git a/protocol/communities/manager.go b/protocol/communities/manager.go index 9b493d6f4..f64dfa3d0 100644 --- a/protocol/communities/manager.go +++ b/protocol/communities/manager.go @@ -11,6 +11,7 @@ import ( "sort" "strings" "sync" + "syscall" "time" "github.com/anacrolix/torrent" @@ -3972,6 +3973,22 @@ func (m *Manager) SeedHistoryArchiveTorrent(communityID types.HexBytes) error { if err != nil { return err } + + var stat syscall.Statfs_t + wd, _ := os.Getwd() + + err = syscall.Statfs(wd, &stat) + if err != nil { + return err + } + + // Available blocks * size per block = available space in bytes + freeSpace := stat.Bavail * uint64(stat.Bsize) + + if freeSpace <= uint64(torrent.Length()) { + return ErrNoFreeSpaceForHistoryArchives + } + torrent.DownloadAll() m.publish(&Subscription{