From 454c7aa87f01ce790cf1c86f6dbcf29ccfd346b8 Mon Sep 17 00:00:00 2001 From: Alex Sharov Date: Tue, 7 Jun 2022 13:12:27 +0700 Subject: [PATCH] torrent: allow del db #4394 --- cmd/downloader/downloader/downloader.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/cmd/downloader/downloader/downloader.go b/cmd/downloader/downloader/downloader.go index b1f6c3042..ca085ca0f 100644 --- a/cmd/downloader/downloader/downloader.go +++ b/cmd/downloader/downloader/downloader.go @@ -63,7 +63,7 @@ func New(cfg *torrentcfg.Cfg) (*Downloader, error) { if common.FileExist(cfg.DataDir + "_tmp") { // migration from prev versions _ = os.Rename(cfg.DataDir+"_tmp", filepath.Join(cfg.DataDir, "tmp")) // ignore error, because maybe they are on different drive, or target folder already created manually, all is fine } - if !common.FileExist(filepath.Join(cfg.DataDir, "db")) { + if !common.FileExist(filepath.Join(cfg.DataDir, "db")) && !HasSegFile(cfg.DataDir) { // it's ok to remove "datadir/snapshots/db" dir or add .seg files manually cfg.DataDir = filepath.Join(cfg.DataDir, "tmp") } else { if err := copyFromTmp(cfg.DataDir); err != nil { @@ -367,3 +367,19 @@ func MainLoop(ctx context.Context, d *Downloader, silent bool) { } } } + +func HasSegFile(dir string) bool { + files, err := os.ReadDir(dir) + if err != nil { + return false + } + for _, f := range files { + if f.IsDir() { + continue + } + if filepath.Ext(f.Name()) == ".seg" { + return true + } + } + return false +}