good morning!!!!

Skip to content
Snippets Groups Projects
Unverified Commit f5a82300 authored by Alex Sharov's avatar Alex Sharov Committed by GitHub
Browse files

Snapshots: write hashes to file only if amount of them growth (#3784)

parent 3313eedb
No related branches found
No related tags found
No related merge requests found
...@@ -2,8 +2,8 @@ package main ...@@ -2,8 +2,8 @@ package main
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"io/ioutil"
"net" "net"
"os" "os"
"path/filepath" "path/filepath"
...@@ -35,13 +35,13 @@ import ( ...@@ -35,13 +35,13 @@ import (
var ( var (
datadir string datadir string
asJson bool
forceRebuild bool forceRebuild bool
forceVerify bool forceVerify bool
downloaderApiAddr string downloaderApiAddr string
torrentVerbosity string torrentVerbosity string
downloadRateStr, uploadRateStr string downloadRateStr, uploadRateStr string
torrentPort int torrentPort int
targetFile string
) )
func init() { func init() {
...@@ -57,9 +57,12 @@ func init() { ...@@ -57,9 +57,12 @@ func init() {
rootCmd.Flags().IntVar(&torrentPort, "torrent.port", 42069, "port to listen and serve BitTorrent protocol") rootCmd.Flags().IntVar(&torrentPort, "torrent.port", 42069, "port to listen and serve BitTorrent protocol")
withDataDir(printTorrentHashes) withDataDir(printTorrentHashes)
printTorrentHashes.PersistentFlags().BoolVar(&asJson, "json", false, "Print in json format (default: toml)")
printTorrentHashes.PersistentFlags().BoolVar(&forceRebuild, "rebuild", false, "Force re-create .torrent files") printTorrentHashes.PersistentFlags().BoolVar(&forceRebuild, "rebuild", false, "Force re-create .torrent files")
printTorrentHashes.PersistentFlags().BoolVar(&forceVerify, "verify", false, "Force verify data files if have .torrent files") printTorrentHashes.PersistentFlags().BoolVar(&forceVerify, "verify", false, "Force verify data files if have .torrent files")
printTorrentHashes.Flags().StringVar(&targetFile, "targetfile", "", "write output to file")
if err := printTorrentHashes.MarkFlagFilename("targetfile"); err != nil {
panic(err)
}
rootCmd.AddCommand(printTorrentHashes) rootCmd.AddCommand(printTorrentHashes)
} }
...@@ -205,19 +208,31 @@ var printTorrentHashes = &cobra.Command{ ...@@ -205,19 +208,31 @@ var printTorrentHashes = &cobra.Command{
} }
res[info.Name] = mi.HashInfoBytes().String() res[info.Name] = mi.HashInfoBytes().String()
} }
var serialized []byte serialized, err := toml.Marshal(res)
if asJson { if err != nil {
serialized, err = json.Marshal(res) return err
if err != nil { }
return err
} if targetFile == "" {
} else { fmt.Printf("%s\n", serialized)
serialized, err = toml.Marshal(res) return nil
if err != nil { }
return err
} oldContent, err := ioutil.ReadFile(targetFile)
if err != nil {
return err
}
oldLines := map[string]string{}
if err := toml.Unmarshal(oldContent, &oldLines); err != nil {
return fmt.Errorf("unmarshal: %w", err)
}
if len(oldLines) >= len(res) {
log.Info("amount of lines in target file is equal or greater than amount of lines in snapshot dir", "old", len(oldLines), "new", len(res))
return nil
}
if err := ioutil.WriteFile(targetFile, serialized, 0644); err != nil {
return err
} }
fmt.Printf("%s\n", serialized)
return nil return nil
}, },
} }
......
...@@ -14,7 +14,7 @@ git pull ...@@ -14,7 +14,7 @@ git pull
cd ../erigon cd ../erigon
# it will return only .seg of 500K (because Erigon send to Downloader only such files) # it will return only .seg of 500K (because Erigon send to Downloader only such files)
go run -trimpath ./cmd/downloader torrent_hashes --datadir="$datadir" >./../erigon-snapshot/"$network".toml go run -trimpath ./cmd/downloader torrent_hashes --datadir="$datadir" --targetfile=./../erigon-snapshot/"$network".toml
cd ./../erigon-snapshot cd ./../erigon-snapshot
git add "$network".toml git add "$network".toml
git commit -m "ci: $network" git commit -m "ci: $network"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment