summaryrefslogtreecommitdiff
path: root/src/lib/os/file.go
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2009-04-13 16:50:42 -0700
committerRuss Cox <rsc@golang.org>2009-04-13 16:50:42 -0700
commit4948321bee7a9dfb635db68e40eacf5bed25ebd9 (patch)
treedfa3be7773ff2ad53a6a160053c27776f74dd2f2 /src/lib/os/file.go
parentba30f377d90dd9152ce2fcda4e4960bd80c01fb6 (diff)
downloadgolang-4948321bee7a9dfb635db68e40eacf5bed25ebd9.tar.gz
lib misc
* exec.LookPath * flag.Args * os.Remove * strings.HasPrefix * strings.HasSuffix * syscall.Rmdir TBR=r DELTA=100 (100 added, 0 deleted, 0 changed) OCL=27373 CL=27392
Diffstat (limited to 'src/lib/os/file.go')
-rw-r--r--src/lib/os/file.go30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/lib/os/file.go b/src/lib/os/file.go
index 48daf0bce..80f43bb59 100644
--- a/src/lib/os/file.go
+++ b/src/lib/os/file.go
@@ -267,3 +267,33 @@ func Chdir(dir string) *os.Error {
return ErrnoToError(e);
}
+// Remove removes the named file or directory.
+func Remove(name string) *os.Error {
+ // System call interface forces us to know
+ // whether name is a file or directory.
+ // Try both: it is cheaper on average than
+ // doing a Stat plus the right one.
+ r, e := syscall.Unlink(name);
+ if e == 0 {
+ return nil;
+ }
+ r1, e1 := syscall.Rmdir(name);
+ if e1 == 0 {
+ return nil;
+ }
+
+ // Both failed: figure out which error to return.
+ // OS X and Linux differ on whether unlink(dir)
+ // returns EISDIR, so can't use that. However,
+ // both agree that rmdir(file) returns ENOTDIR,
+ // so we can use that to decide which error is real.
+ // Rmdir might return ENOTDIR if given a bad
+ // file path, like /etc/passwd/foo, but in that case,
+ // both errors will be ENOTDIR, so it's okay to
+ // use the error from unlink.
+ if e1 != syscall.ENOTDIR {
+ e = e1;
+ }
+ return ErrnoToError(e1);
+}
+