lib/fs: rename SymlinkRelative to MustSymlinkRelative

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.
This commit is contained in:
Aliaksandr Valialkin 2023-04-13 22:52:45 -07:00
parent 5f487ed996
commit 780abc3b3b
No known key found for this signature in database
GPG Key ID: A72BEC6CD3D0DED1
2 changed files with 12 additions and 13 deletions

View File

@ -288,14 +288,16 @@ func IsDirOrSymlink(de os.DirEntry) bool {
return de.IsDir() || (de.Type()&os.ModeSymlink == os.ModeSymlink)
}
// SymlinkRelative creates relative symlink for srcPath in dstPath.
func SymlinkRelative(srcPath, dstPath string) error {
// MustSymlinkRelative creates relative symlink for srcPath in dstPath.
func MustSymlinkRelative(srcPath, dstPath string) {
baseDir := filepath.Dir(dstPath)
srcPathRel, err := filepath.Rel(baseDir, srcPath)
if err != nil {
return fmt.Errorf("cannot make relative path for srcPath=%q: %w", srcPath, err)
logger.Panicf("FATAL: cannot make relative path for srcPath=%q: %s", srcPath, err)
}
if err := os.Symlink(srcPathRel, dstPath); err != nil {
logger.Panicf("FATAL: cannot make a symlink: %s", err)
}
return os.Symlink(srcPathRel, dstPath)
}
// CopyDirectory copies all the files in srcPath to dstPath.

View File

@ -330,14 +330,13 @@ func (s *Storage) CreateSnapshot(deadline uint64) (string, error) {
dstDataDir := filepath.Join(dstDir, dataDirname)
fs.MustMkdirFailIfExist(dstDataDir)
dstSmallDir := filepath.Join(dstDataDir, smallDirname)
if err := fs.SymlinkRelative(smallDir, dstSmallDir); err != nil {
return "", fmt.Errorf("cannot create symlink from %q to %q: %w", smallDir, dstSmallDir, err)
}
fs.MustSymlinkRelative(smallDir, dstSmallDir)
dstBigDir := filepath.Join(dstDataDir, bigDirname)
if err := fs.SymlinkRelative(bigDir, dstBigDir); err != nil {
return "", fmt.Errorf("cannot create symlink from %q to %q: %w", bigDir, dstBigDir, err)
}
fs.MustSymlinkRelative(bigDir, dstBigDir)
fs.MustSyncPath(dstDataDir)
srcMetadataDir := filepath.Join(srcDir, metadataDirname)
@ -362,9 +361,7 @@ func (s *Storage) CreateSnapshot(deadline uint64) (string, error) {
return "", fmt.Errorf("cannot create prev indexDB snapshot: %w", err)
}
dstIdbDir := filepath.Join(dstDir, indexdbDirname)
if err := fs.SymlinkRelative(idbSnapshot, dstIdbDir); err != nil {
return "", fmt.Errorf("cannot create symlink from %q to %q: %w", idbSnapshot, dstIdbDir, err)
}
fs.MustSymlinkRelative(idbSnapshot, dstIdbDir)
fs.MustSyncPath(dstDir)