From 5f487ed996dba8e3ce40132952d36f684a411dd4 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Thu, 13 Apr 2023 22:48:05 -0700 Subject: [PATCH] lib/fs: rename HardLinkFiles to MustHardLinkFiles Callers of this function log the returned error and then exit. Let's log the error with the call stack inside the function itself. This simplifies the code at callers' side, while leaving the same level of debuggability in case of errors. --- lib/fs/fs.go | 9 ++++----- lib/mergeset/table.go | 4 +--- lib/storage/partition.go | 4 +--- 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/lib/fs/fs.go b/lib/fs/fs.go index c86eef233..726a123de 100644 --- a/lib/fs/fs.go +++ b/lib/fs/fs.go @@ -259,13 +259,13 @@ func MustRemoveTemporaryDirs(dir string) { MustSyncPath(dir) } -// HardLinkFiles makes hard links for all the files from srcDir in dstDir. -func HardLinkFiles(srcDir, dstDir string) error { +// MustHardLinkFiles makes hard links for all the files from srcDir in dstDir. +func MustHardLinkFiles(srcDir, dstDir string) { mustMkdirSync(dstDir) des, err := os.ReadDir(srcDir) if err != nil { - return fmt.Errorf("cannot read files in scrDir: %w", err) + logger.Panicf("FATAL: cannot read files in scrDir: %s", err) } for _, de := range des { if IsDirOrSymlink(de) { @@ -276,12 +276,11 @@ func HardLinkFiles(srcDir, dstDir string) error { srcPath := filepath.Join(srcDir, fn) dstPath := filepath.Join(dstDir, fn) if err := os.Link(srcPath, dstPath); err != nil { - return err + logger.Panicf("FATAL: cannot link files: %s", err) } } MustSyncPath(dstDir) - return nil } // IsDirOrSymlink returns true if de is directory or symlink. diff --git a/lib/mergeset/table.go b/lib/mergeset/table.go index 2c57add62..96a02ce7f 100644 --- a/lib/mergeset/table.go +++ b/lib/mergeset/table.go @@ -1477,9 +1477,7 @@ func (tb *Table) CreateSnapshotAt(dstDir string, deadline uint64) error { } srcPartPath := pw.p.path dstPartPath := filepath.Join(dstDir, filepath.Base(srcPartPath)) - if err := fs.HardLinkFiles(srcPartPath, dstPartPath); err != nil { - return fmt.Errorf("cannot create hard links from %q to %q: %w", srcPartPath, dstPartPath, err) - } + fs.MustHardLinkFiles(srcPartPath, dstPartPath) } fs.MustSyncPath(dstDir) diff --git a/lib/storage/partition.go b/lib/storage/partition.go index 096eeb593..5761a5473 100644 --- a/lib/storage/partition.go +++ b/lib/storage/partition.go @@ -1897,9 +1897,7 @@ func (pt *partition) createSnapshot(srcDir, dstDir string, pws []*partWrapper) e for _, pw := range pws { srcPartPath := pw.p.path dstPartPath := filepath.Join(dstDir, filepath.Base(srcPartPath)) - if err := fs.HardLinkFiles(srcPartPath, dstPartPath); err != nil { - return fmt.Errorf("cannot create hard links from %q to %q: %w", srcPartPath, dstPartPath, err) - } + fs.MustHardLinkFiles(srcPartPath, dstPartPath) } // Copy the appliedRetentionFilename to dstDir.