44 "fmt"
55 "os"
66 "path/filepath"
7+ "strings"
78 "sync"
89
910 "github.com/anacrolix/torrent"
@@ -28,8 +29,10 @@ func (tf *TorrentFiles) Exists(name string) bool {
2829}
2930
3031func (tf * TorrentFiles ) exists (name string ) bool {
31- fPath := filepath .Join (tf .dir , name )
32- return dir2 .FileExist (fPath + ".torrent" )
32+ if ! strings .HasSuffix (name , ".torrent" ) {
33+ name += ".torrent"
34+ }
35+ return dir2 .FileExist (filepath .Join (tf .dir , name ))
3336}
3437func (tf * TorrentFiles ) Delete (name string ) error {
3538 tf .lock .Lock ()
@@ -38,8 +41,10 @@ func (tf *TorrentFiles) Delete(name string) error {
3841}
3942
4043func (tf * TorrentFiles ) delete (name string ) error {
41- fPath := filepath .Join (tf .dir , name )
42- return os .Remove (fPath + ".torrent" )
44+ if ! strings .HasSuffix (name , ".torrent" ) {
45+ name += ".torrent"
46+ }
47+ return os .Remove (filepath .Join (tf .dir , name ))
4348}
4449
4550func (tf * TorrentFiles ) Create (torrentFilePath string , res []byte ) error {
@@ -91,11 +96,10 @@ func (tf *TorrentFiles) createTorrentFromMetaInfo(fPath string, mi *metainfo.Met
9196 return nil
9297}
9398
94- func (tf * TorrentFiles ) LoadByName (fName string ) (* torrent.TorrentSpec , error ) {
99+ func (tf * TorrentFiles ) LoadByName (name string ) (* torrent.TorrentSpec , error ) {
95100 tf .lock .Lock ()
96101 defer tf .lock .Unlock ()
97- fPath := filepath .Join (tf .dir , fName + ".torrent" )
98- return tf .load (fPath )
102+ return tf .load (filepath .Join (tf .dir , name ))
99103}
100104
101105func (tf * TorrentFiles ) LoadByPath (fPath string ) (* torrent.TorrentSpec , error ) {
@@ -105,10 +109,41 @@ func (tf *TorrentFiles) LoadByPath(fPath string) (*torrent.TorrentSpec, error) {
105109}
106110
107111func (tf * TorrentFiles ) load (fPath string ) (* torrent.TorrentSpec , error ) {
112+ if ! strings .HasSuffix (fPath , ".torrent" ) {
113+ fPath += ".torrent"
114+ }
108115 mi , err := metainfo .LoadFromFile (fPath )
109116 if err != nil {
110117 return nil , fmt .Errorf ("LoadFromFile: %w, file=%s" , err , fPath )
111118 }
112119 mi .AnnounceList = Trackers
113120 return torrent .TorrentSpecFromMetaInfoErr (mi )
114121}
122+
123+ const ProhibitNewDownloadsFileName = "prohibit_new_downloads.lock"
124+
125+ // Erigon "download once" - means restart/upgrade/downgrade will not download files (and will be fast)
126+ // After "download once" - Erigon will produce and seed new files
127+ // Downloader will able: seed new files (already existing on FS), download uncomplete parts of existing files (if Verify found some bad parts)
128+ func (tf * TorrentFiles ) prohibitNewDownloads () error {
129+ tf .lock .Lock ()
130+ defer tf .lock .Unlock ()
131+ return CreateProhibitNewDownloadsFile (tf .dir )
132+ }
133+ func (tf * TorrentFiles ) newDownloadsAreProhibited () bool {
134+ tf .lock .Lock ()
135+ defer tf .lock .Unlock ()
136+ return dir2 .FileExist (filepath .Join (tf .dir , ProhibitNewDownloadsFileName ))
137+ }
138+ func CreateProhibitNewDownloadsFile (dir string ) error {
139+ fPath := filepath .Join (dir , ProhibitNewDownloadsFileName )
140+ f , err := os .Create (fPath )
141+ if err != nil {
142+ return err
143+ }
144+ defer f .Close ()
145+ if err := f .Sync (); err != nil {
146+ return err
147+ }
148+ return nil
149+ }
0 commit comments