You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by cc...@apache.org on 2019/10/23 17:31:18 UTC

[mynewt-newt] branch master updated (98fa82b -> bc09bb3)

This is an automated email from the ASF dual-hosted git repository.

ccollins pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-newt.git.


    from 98fa82b  Add support for hardware encryption features
     new 07b3f92  util: MoveDir() and MoveFile(): try rename first
     new bc09bb3  Use `util.MoveDir()` instead of `os.Rename()`

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 newt/builder/extcmd.go    |  4 ++--
 newt/project/pkgwriter.go |  8 ++++----
 util/util.go              | 14 ++++++++++++++
 3 files changed, 20 insertions(+), 6 deletions(-)


[mynewt-newt] 01/02: util: MoveDir() and MoveFile(): try rename first

Posted by cc...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ccollins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-newt.git

commit 07b3f92fed29788201eecae5cedb22574ff942ac
Author: Christopher Collins <cc...@apache.org>
AuthorDate: Wed Oct 23 09:29:53 2019 -0700

    util: MoveDir() and MoveFile(): try rename first
    
    Before this commit, the `util.MoveDir()` and `util.MoveFile()` functions
    copied the source path to the destination, then deleted the source.
    This works, but is inefficient if both source and destination reside on
    the same disk.
    
    This commit changes these functions such that they attempt a rename
    first.  This should succeed if both source and destination are on the
    same disk.  The function falls back to the old behavior if the rename
    fails.
---
 util/util.go | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/util/util.go b/util/util.go
index 9800ea8..900afbb 100644
--- a/util/util.go
+++ b/util/util.go
@@ -552,6 +552,13 @@ func CopyDir(srcDirStr, dstDirStr string) error {
 }
 
 func MoveFile(srcFile string, destFile string) error {
+	// First, attempt a rename.  This will succeed if the source and
+	// destination are on the same disk.
+	if err := os.Rename(srcFile, destFile); err == nil {
+		return nil
+	}
+
+	// Otherwise, copy the file and delete the old path.
 	if err := CopyFile(srcFile, destFile); err != nil {
 		return err
 	}
@@ -564,6 +571,13 @@ func MoveFile(srcFile string, destFile string) error {
 }
 
 func MoveDir(srcDir string, destDir string) error {
+	// First, attempt a rename.  This will succeed if the source and
+	// destination are on the same disk.
+	if err := os.Rename(srcDir, destDir); err == nil {
+		return nil
+	}
+
+	// Otherwise, copy the directory and delete the old path.
 	if err := CopyDir(srcDir, destDir); err != nil {
 		return err
 	}


[mynewt-newt] 02/02: Use `util.MoveDir()` instead of `os.Rename()`

Posted by cc...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ccollins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-newt.git

commit bc09bb3b75e7d656869707846497e55f1b78de83
Author: Christopher Collins <cc...@apache.org>
AuthorDate: Wed Oct 23 09:32:46 2019 -0700

    Use `util.MoveDir()` instead of `os.Rename()`
    
    `os.Rename()` fails if the source and destination paths reside on
    different disks.  The `util.MoveDir()` function handles both cases (same
    disk and different disks).
    
    Before this commit, a cross-disk rename might cause the newt command to
    fail with an error like this:
    
        Error: rename /tmp/mynewt-user-pre-link391870512/src /home/me/repo/bin/targets/my_blinky_sim/user/pre_link/src: invalid cross-device link
---
 newt/builder/extcmd.go    | 4 ++--
 newt/project/pkgwriter.go | 8 ++++----
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/newt/builder/extcmd.go b/newt/builder/extcmd.go
index cddea76..0d106fb 100644
--- a/newt/builder/extcmd.go
+++ b/newt/builder/extcmd.go
@@ -28,8 +28,8 @@ func replaceArtifactsIfChanged(oldDir string, newDir string) error {
 
 	log.Debugf("changes detected; replacing %s with %s", oldDir, newDir)
 	os.RemoveAll(oldDir)
-	if err := os.Rename(newDir, oldDir); err != nil {
-		return util.ChildNewtError(err)
+	if err := util.MoveDir(newDir, oldDir); err != nil {
+		return err
 	}
 
 	return nil
diff --git a/newt/project/pkgwriter.go b/newt/project/pkgwriter.go
index 5a0f6db..0501599 100644
--- a/newt/project/pkgwriter.go
+++ b/newt/project/pkgwriter.go
@@ -201,8 +201,8 @@ func (pw *PackageWriter) fixupPkg() error {
 			if err := os.MkdirAll(filepath.Dir(d2), os.ModePerm); err != nil {
 				return util.ChildNewtError(err)
 			}
-			if err := os.Rename(d1, d2); err != nil {
-				return util.ChildNewtError(err)
+			if err := util.MoveDir(d1, d2); err != nil {
+				return err
 			}
 		}
 	}
@@ -224,8 +224,8 @@ func (pw *PackageWriter) fixupPkg() error {
 	for _, f1 := range files {
 		f2 := replaceText(f1, table)
 		if f2 != f1 {
-			if err := os.Rename(f1, f2); err != nil {
-				return util.ChildNewtError(err)
+			if err := util.MoveDir(f1, f2); err != nil {
+				return err
 			}
 		}
 	}