-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add os.Rename wrapper for Plan 9 (#87)
os.Rename documentation says: "OS-specific restrictions may apply when oldpath and newpath are in different directories." On Unix, this means we can't rename across devices. On Plan 9 however, the functionality is even more limited: cross-directory renames are not allowed at all. Add a wrapper around os.Rename for Plan 9, which will copy the file if we're renaming across directory. All tests seems to pass. (Aside: I also had to write this wrapper to get go-git working on Plan 9: https://github.com/go-git/go-billy/blob/v5.0.0/osfs/os_plan9.go#L27 but I notice few issues with that one.) Fixes #86
- Loading branch information
Showing
4 changed files
with
88 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// +build !plan9 | ||
|
||
package flatfs | ||
|
||
import "os" | ||
|
||
var rename = os.Rename |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package flatfs | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"syscall" | ||
) | ||
|
||
// rename behaves like os.Rename but can rename files across directories. | ||
func rename(oldpath, newpath string) error { | ||
err := os.Rename(oldpath, newpath) | ||
if le, ok := err.(*os.LinkError); !ok || le.Err != os.ErrInvalid { | ||
return err | ||
} | ||
if filepath.Dir(oldpath) == filepath.Dir(newpath) { | ||
// We should not get here, but just in case | ||
// os.ErrInvalid is used for something else in the future. | ||
return err | ||
} | ||
|
||
src, err := os.Open(oldpath) | ||
if err != nil { | ||
return &os.LinkError{"rename", oldpath, newpath, err} | ||
} | ||
defer src.Close() | ||
|
||
fi, err := src.Stat() | ||
if err != nil { | ||
return &os.LinkError{"rename", oldpath, newpath, err} | ||
} | ||
if fi.Mode().IsDir() { | ||
return &os.LinkError{"rename", oldpath, newpath, syscall.EISDIR} | ||
} | ||
|
||
dst, err := os.OpenFile(newpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, fi.Mode()) | ||
if err != nil { | ||
return &os.LinkError{"rename", oldpath, newpath, err} | ||
} | ||
|
||
if _, err := io.Copy(dst, src); err != nil { | ||
dst.Close() | ||
os.Remove(newpath) | ||
return &os.LinkError{"rename", oldpath, newpath, err} | ||
} | ||
if err := dst.Close(); err != nil { | ||
os.Remove(newpath) | ||
return &os.LinkError{"rename", oldpath, newpath, err} | ||
} | ||
|
||
// Copy mtime and mode from original file. | ||
// We need only one syscall if we avoid os.Chmod and os.Chtimes. | ||
dir := fi.Sys().(*syscall.Dir) | ||
var d syscall.Dir | ||
d.Null() | ||
d.Mtime = dir.Mtime | ||
d.Mode = dir.Mode | ||
_ = dirwstat(newpath, &d) // ignore error, as per mv(1) | ||
|
||
if err := os.Remove(oldpath); err != nil { | ||
return &os.LinkError{"rename", oldpath, newpath, err} | ||
} | ||
return nil | ||
} | ||
|
||
func dirwstat(name string, d *syscall.Dir) error { | ||
var buf [syscall.STATFIXLEN]byte | ||
|
||
n, err := d.Marshal(buf[:]) | ||
if err != nil { | ||
return &os.PathError{"dirwstat", name, err} | ||
} | ||
if err = syscall.Wstat(name, buf[:n]); err != nil { | ||
return &os.PathError{"dirwstat", name, err} | ||
} | ||
return nil | ||
} |