2023-09-24 06:42:53 +00:00
|
|
|
package chaindata
|
2023-09-01 11:54:39 +00:00
|
|
|
|
|
|
|
import (
|
2023-09-22 09:10:39 +00:00
|
|
|
"errors"
|
2023-09-25 18:15:25 +00:00
|
|
|
"io"
|
2023-09-01 11:54:39 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
)
|
|
|
|
|
2023-09-24 06:42:53 +00:00
|
|
|
type Paths struct {
|
|
|
|
ChainData, Ancient string
|
2023-09-22 09:10:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// List of names of chaindata fixtures accessible via ChainDataPaths
|
|
|
|
var FixtureChains = []string{
|
|
|
|
"small", "small2",
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsFixture(chain string) bool {
|
|
|
|
has := false
|
|
|
|
for _, fixture := range FixtureChains {
|
|
|
|
if chain == fixture {
|
|
|
|
has = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return has
|
|
|
|
}
|
|
|
|
|
2023-09-25 18:15:25 +00:00
|
|
|
// GetFixture returns the paths to the fixture chaindata for the given name. This copies the
|
|
|
|
// fixture data and returns paths to temp directories, due to the fact that Go modules are installed
|
|
|
|
// read-only and Leveldb will attempt to create lock files when opening a DB.
|
2023-09-24 06:42:53 +00:00
|
|
|
func GetFixture(chain string) (*Paths, error) {
|
2023-09-22 09:10:39 +00:00
|
|
|
if !IsFixture(chain) {
|
|
|
|
return nil, errors.New("no fixture named " + chain)
|
|
|
|
}
|
|
|
|
|
2023-09-01 11:54:39 +00:00
|
|
|
_, thisPath, _, ok := runtime.Caller(0)
|
|
|
|
if !ok {
|
2023-09-22 09:10:39 +00:00
|
|
|
return nil, errors.New("could not get function source path")
|
2023-09-01 11:54:39 +00:00
|
|
|
}
|
|
|
|
|
2023-09-24 06:42:53 +00:00
|
|
|
chaindataPath := filepath.Join(filepath.Dir(thisPath), "_data", chain)
|
2023-09-01 11:54:39 +00:00
|
|
|
if _, err := os.Stat(chaindataPath); err != nil {
|
2023-09-24 06:42:53 +00:00
|
|
|
return nil, errors.New("cannot access chaindata at " + chaindataPath)
|
2023-09-01 11:54:39 +00:00
|
|
|
}
|
|
|
|
|
2023-09-25 18:15:25 +00:00
|
|
|
// Copy chaindata directory to a temporary directory
|
|
|
|
// Note that we assume the ancient path is a subdirectory
|
|
|
|
copyTo := filepath.Join(os.TempDir(), chain)
|
|
|
|
if err := copyDir(chaindataPath, copyTo); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ancientCopy := filepath.Join(copyTo, "ancient")
|
|
|
|
|
|
|
|
return &Paths{copyTo, ancientCopy}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func copyDir(src, dest string) error {
|
|
|
|
err := os.MkdirAll(dest, 0755)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
srcDir, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer srcDir.Close()
|
|
|
|
|
|
|
|
fileInfos, err := srcDir.Readdir(-1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, fileInfo := range fileInfos {
|
|
|
|
srcPath := filepath.Join(src, fileInfo.Name())
|
|
|
|
destPath := filepath.Join(dest, fileInfo.Name())
|
|
|
|
|
|
|
|
if fileInfo.IsDir() {
|
|
|
|
if err := copyDir(srcPath, destPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := copyFile(srcPath, destPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func copyFile(src, dest string) error {
|
|
|
|
srcFile, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer srcFile.Close()
|
|
|
|
|
|
|
|
destFile, err := os.Create(dest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer destFile.Close()
|
|
|
|
|
|
|
|
_, err = io.Copy(destFile, srcFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2023-09-01 11:54:39 +00:00
|
|
|
}
|