Add os.Rename.

R=rsc
https://golang.org/cl/166058
This commit is contained in:
Ian Lance Taylor 2009-12-04 11:46:56 -08:00
parent d1740bb3a6
commit 0b5cc31693
2 changed files with 31 additions and 1 deletions

View File

@ -370,7 +370,7 @@ func Remove(name string) Error {
return &PathError{"remove", name, Errno(e)};
}
// LinkError records an error during a link or symlink
// LinkError records an error during a link or symlink or rename
// system call and the paths that caused it.
type LinkError struct {
Op string;
@ -418,6 +418,15 @@ func Readlink(name string) (string, Error) {
return "", nil;
}
// Rename renames a file.
func Rename(oldname, newname string) Error {
e := syscall.Rename(oldname, newname);
if e != 0 {
return &LinkError{"rename", oldname, newname, Errno(e)}
}
return nil;
}
// Chmod changes the mode of the named file to mode.
// If the file is a symbolic link, it changes the uid and gid of the link's target.
func Chmod(name string, mode int) Error {

View File

@ -315,6 +315,27 @@ func TestLongSymlink(t *testing.T) {
}
}
func TestRename(t *testing.T) {
from, to := "renamefrom", "renameto";
Remove(to); // Just in case.
file, err := Open(from, O_CREAT|O_WRONLY, 0666);
if err != nil {
t.Fatalf("open %q failed: %v", to, err)
}
if err = file.Close(); err != nil {
t.Errorf("close %q failed: %v", to, err)
}
err = Rename(from, to);
if err != nil {
t.Fatalf("rename %q, %q failed: %v", to, from, err)
}
defer Remove(to);
_, err = Stat(to);
if err != nil {
t.Errorf("stat %q failed: %v", to, err)
}
}
func TestForkExec(t *testing.T) {
r, w, err := Pipe();
if err != nil {