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

Fix moving files between different drives and filesystems #322

Merged
merged 1 commit into from
Apr 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pkg/util/pathutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"io/ioutil"
"os"
"path/filepath"

"github.com/SkycoinProject/skywire-mainnet/pkg/util/rename"
)

const (
Expand Down Expand Up @@ -43,7 +45,7 @@ func AtomicWriteFile(filename string, data []byte) error {
return err
}

if err := os.Rename(tempFilePath, filename); err != nil {
if err := rename.Rename(tempFilePath, filename); err != nil {
return err
}

Expand Down
73 changes: 73 additions & 0 deletions pkg/util/rename/rename.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package rename

import (
"fmt"
"io"
"log"
"os"
)

const crossDeviceError = "invalid cross-device link"

// Rename renames (moves) oldPath to newPath using os.Rename.
// If paths are located on different drives or filesystems, os.Rename fails.
// In that case, Rename uses a workaround by copying oldPath to newPath and removing oldPath thereafter.
func Rename(oldPath, newPath string) error {
if err := os.Rename(oldPath, newPath); err == nil || err.Error() != crossDeviceError {
return err
}

stat, err := os.Stat(oldPath)
if err != nil {
return fmt.Errorf("stat: %w", err)
}

if !stat.Mode().IsRegular() {
return fmt.Errorf("is regular: %w", err)
}

// Paths are located on different devices.
if err := move(oldPath, newPath); err != nil {
return fmt.Errorf("move: %w", err)
}

if err := os.Chmod(newPath, stat.Mode()); err != nil {
return fmt.Errorf("chmod: %w", err)
}

if err := os.Remove(oldPath); err != nil {
return fmt.Errorf("remove: %w", err)
}

return nil
}

func move(oldPath string, newPath string) error {
inputFile, err := os.Open(oldPath) // nolint:gosec
if err != nil {
return fmt.Errorf("open: %w", err)
}

defer func() {
if err := inputFile.Close(); err != nil {
log.Printf("Failed to close file %q: %v", inputFile.Name(), err)
}
}()

outputFile, err := os.Create(newPath)
if err != nil {
return fmt.Errorf("create: %w", err)
}

defer func() {
if err := outputFile.Close(); err != nil {
log.Printf("Failed to close file %q: %v", outputFile.Name(), err)
}
}()

if _, err = io.Copy(outputFile, inputFile); err != nil {
return fmt.Errorf("copy: %w", err)
}

return nil
}
11 changes: 6 additions & 5 deletions pkg/util/updater/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"github.com/SkycoinProject/skywire-mainnet/pkg/restart"
"github.com/SkycoinProject/skywire-mainnet/pkg/util/buildinfo"
"github.com/SkycoinProject/skywire-mainnet/pkg/util/rename"
)

const (
Expand Down Expand Up @@ -180,13 +181,13 @@ func (u *Updater) updateBinary(downloadedBinariesPath, basePath, binary string)
}
}

if err := os.Rename(currentBinaryPath, oldBinaryPath); err != nil {
if err := rename.Rename(currentBinaryPath, oldBinaryPath); err != nil {
return fmt.Errorf("rename %s to %s: %w", currentBinaryPath, oldBinaryPath, err)
}

if err := os.Rename(downloadedBinaryPath, currentBinaryPath); err != nil {
// Try to revert previous os.Rename
if err := os.Rename(oldBinaryPath, currentBinaryPath); err != nil {
if err := rename.Rename(downloadedBinaryPath, currentBinaryPath); err != nil {
// Try to revert previous rename.
if err := rename.Rename(oldBinaryPath, currentBinaryPath); err != nil {
u.log.Errorf("Failed to rename file %q to %q: %v", oldBinaryPath, currentBinaryPath, err)
}

Expand All @@ -201,7 +202,7 @@ func (u *Updater) updateBinary(downloadedBinariesPath, basePath, binary string)
func (u *Updater) restore(currentBinaryPath string, toBeRemoved string) {
u.removeFiles(currentBinaryPath)

if err := os.Rename(toBeRemoved, currentBinaryPath); err != nil {
if err := rename.Rename(toBeRemoved, currentBinaryPath); err != nil {
u.log.Errorf("Failed to rename file %q to %q: %v", toBeRemoved, currentBinaryPath, err)
}
}
Expand Down