]> Cypherpunks repositories - gostls13.git/commitdiff
Add os.Rename.
authorIan Lance Taylor <iant@golang.org>
Fri, 4 Dec 2009 19:46:56 +0000 (11:46 -0800)
committerIan Lance Taylor <iant@golang.org>
Fri, 4 Dec 2009 19:46:56 +0000 (11:46 -0800)
R=rsc
https://golang.org/cl/166058

src/pkg/os/file.go
src/pkg/os/os_test.go

index dc722055a53f39327bd8a3c1c7f3d2adb9de4951..03c6d57018cecc092892d4a7e91c6c7aac9bd8c3 100644 (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 {
index ed3d955cb30e702e9254f71a4b509c2a435f0c82..7ff2bddb3ccdbb58e4b8712984c2bdf3f53ed366 100644 (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 {