Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backward compatibility of .lock #10006

Merged
merged 18 commits into from
Apr 25, 2024
11 changes: 6 additions & 5 deletions erigon-lib/common/dir/rw_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,16 @@ func WriteFileWithFsync(name string, data []byte, perm os.FileMode) error {
return err
}
defer f.Close()
_, err = f.Write(data)
if err != nil {
if _, err = f.Write(data); err != nil {
return err
}
err = f.Sync()
if err != nil {
if err = f.Sync(); err != nil {
return err
}
if err = f.Close(); err != nil {
return err
}
return err
return nil
}

func Recreate(dir string) {
Expand Down
47 changes: 23 additions & 24 deletions erigon-lib/downloader/torrent_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,43 +177,33 @@ func (tf *AtomicTorrentFS) ProhibitNewDownloads(t string) error {
}

func (tf *AtomicTorrentFS) prohibitNewDownloads(t string) error {
// open or create file ProhibitNewDownloadsFileName
f, err := os.OpenFile(filepath.Join(tf.dir, ProhibitNewDownloadsFileName), os.O_CREATE|os.O_RDONLY, 0644)
if err != nil {
return fmt.Errorf("open file: %w", err)
}
defer f.Close()
fPath := filepath.Join(tf.dir, ProhibitNewDownloadsFileName)
exist := dir.FileExist(fPath)
var prohibitedList []string
torrentListJsonBytes, err := io.ReadAll(f)
if err != nil {
return fmt.Errorf("read file: %w", err)
}
if len(torrentListJsonBytes) > 0 {
if err := json.Unmarshal(torrentListJsonBytes, &prohibitedList); err != nil {
return fmt.Errorf("unmarshal: %w", err)
if exist {
torrentListJsonBytes, err := os.ReadFile(fPath)
if err != nil {
return fmt.Errorf("read file: %w", err)
}
if len(torrentListJsonBytes) > 0 {
if err := json.Unmarshal(torrentListJsonBytes, &prohibitedList); err != nil {
return fmt.Errorf("unmarshal: %w", err)
}
}
}
if slices.Contains(prohibitedList, t) {
return nil
}
prohibitedList = append(prohibitedList, t)
f.Close()

// write new prohibited list by opening the file in truncate mode
f, err = os.OpenFile(filepath.Join(tf.dir, ProhibitNewDownloadsFileName), os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("open file for writing: %w", err)
}
defer f.Close()
prohibitedListJsonBytes, err := json.Marshal(prohibitedList)
if err != nil {
return fmt.Errorf("marshal: %w", err)
}
if _, err := f.Write(prohibitedListJsonBytes); err != nil {
if err := dir.WriteFileWithFsync(fPath, prohibitedListJsonBytes, 0644); err != nil {
return fmt.Errorf("write: %w", err)
}

return f.Sync()
return nil
}

func (tf *AtomicTorrentFS) NewDownloadsAreProhibited(name string) (bool, error) {
Expand All @@ -223,7 +213,13 @@ func (tf *AtomicTorrentFS) NewDownloadsAreProhibited(name string) (bool, error)
}

func (tf *AtomicTorrentFS) newDownloadsAreProhibited(name string) (bool, error) {
f, err := os.OpenFile(filepath.Join(tf.dir, ProhibitNewDownloadsFileName), os.O_CREATE|os.O_APPEND|os.O_RDONLY, 0644)
fPath := filepath.Join(tf.dir, ProhibitNewDownloadsFileName)
exists := dir.FileExist(fPath)
if !exists {
return false, nil
}

f, err := os.OpenFile(fPath, os.O_RDONLY, 0644)
if err != nil {
return false, err
}
Expand All @@ -233,6 +229,9 @@ func (tf *AtomicTorrentFS) newDownloadsAreProhibited(name string) (bool, error)
if err != nil {
return false, fmt.Errorf("NewDownloadsAreProhibited: read file: %w", err)
}
if exists && len(torrentListJsonBytes) == 0 { // backward compatibility: if .lock exists and empty - it means everything is prohibited
AskAlexSharov marked this conversation as resolved.
Show resolved Hide resolved
return true, nil
}
if len(torrentListJsonBytes) > 0 {
if err := json.Unmarshal(torrentListJsonBytes, &prohibitedList); err != nil {
return false, fmt.Errorf("NewDownloadsAreProhibited: unmarshal: %w", err)
Expand Down
54 changes: 54 additions & 0 deletions erigon-lib/downloader/torrent_files_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package downloader

import (
"os"
"path/filepath"
"testing"

"github.com/ledgerwatch/erigon-lib/common/datadir"
"github.com/stretchr/testify/require"
)

func TestFSProhibitBackwardCompat(t *testing.T) {
require := require.New(t)
dirs := datadir.New(t.TempDir())

//prev version of .lock - is empty .lock file which exitence prohibiting everything
t.Run("no prev version .lock", func(t *testing.T) {
tf := NewAtomicTorrentFS(dirs.Snap)
prohibited, err := tf.NewDownloadsAreProhibited("v1-004900-005000-headers.seg")
require.NoError(err)
require.False(prohibited)
prohibited, err = tf.NewDownloadsAreProhibited("v1-004900-005000-headers.seg.torrent")
require.NoError(err)
require.False(prohibited)
})
t.Run("prev version .lock support", func(t *testing.T) {
err := os.WriteFile(filepath.Join(dirs.Snap, ProhibitNewDownloadsFileName), nil, 0644)
require.NoError(err)

tf := NewAtomicTorrentFS(dirs.Snap)
prohibited, err := tf.NewDownloadsAreProhibited("v1-004900-005000-headers.seg")
require.NoError(err)
require.True(prohibited)
prohibited, err = tf.NewDownloadsAreProhibited("v1-004900-005000-headers.seg.torrent")
require.NoError(err)
require.True(prohibited)
})
t.Run("prev version .lock upgrade", func(t *testing.T) {
//old lock
err := os.WriteFile(filepath.Join(dirs.Snap, ProhibitNewDownloadsFileName), nil, 0644)
require.NoError(err)

tf := NewAtomicTorrentFS(dirs.Snap)
err = tf.prohibitNewDownloads("transactions") //upgrade
require.NoError(err)

prohibited, err := tf.NewDownloadsAreProhibited("v1-004900-005000-headers.seg")
require.NoError(err)
require.False(prohibited)
prohibited, err = tf.NewDownloadsAreProhibited("v1-004900-005000-headers.seg.torrent")
require.NoError(err)
require.False(prohibited)
})
}
Loading