OSDN Git Service

libgo: Update to weekly.2011-11-02.
[pf3gnuchains/gcc-fork.git] / libgo / go / os / file_posix.go
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // +build darwin freebsd linux openbsd windows
6
7 package os
8
9 import (
10         "syscall"
11 )
12
13 func sigpipe() // implemented in package runtime
14
15 func epipecheck(file *File, e int) {
16         if e == syscall.EPIPE {
17                 file.nepipe++
18                 if file.nepipe >= 10 {
19                         sigpipe()
20                 }
21         } else {
22                 file.nepipe = 0
23         }
24 }
25
26 // Remove removes the named file or directory.
27 func Remove(name string) error {
28         // System call interface forces us to know
29         // whether name is a file or directory.
30         // Try both: it is cheaper on average than
31         // doing a Stat plus the right one.
32         e := syscall.Unlink(name)
33         if !iserror(e) {
34                 return nil
35         }
36         e1 := syscall.Rmdir(name)
37         if !iserror(e1) {
38                 return nil
39         }
40
41         // Both failed: figure out which error to return.
42         // OS X and Linux differ on whether unlink(dir)
43         // returns EISDIR, so can't use that.  However,
44         // both agree that rmdir(file) returns ENOTDIR,
45         // so we can use that to decide which error is real.
46         // Rmdir might also return ENOTDIR if given a bad
47         // file path, like /etc/passwd/foo, but in that case,
48         // both errors will be ENOTDIR, so it's okay to
49         // use the error from unlink.
50         // For windows syscall.ENOTDIR is set
51         // to syscall.ERROR_PATH_NOT_FOUND, hopefully it should
52         // do the trick.
53         if e1 != syscall.ENOTDIR {
54                 e = e1
55         }
56         return &PathError{"remove", name, Errno(e)}
57 }
58
59 // LinkError records an error during a link or symlink or rename
60 // system call and the paths that caused it.
61 type LinkError struct {
62         Op  string
63         Old string
64         New string
65         Err error
66 }
67
68 func (e *LinkError) Error() string {
69         return e.Op + " " + e.Old + " " + e.New + ": " + e.Err.Error()
70 }
71
72 // Link creates a hard link.
73 func Link(oldname, newname string) error {
74         e := syscall.Link(oldname, newname)
75         if iserror(e) {
76                 return &LinkError{"link", oldname, newname, Errno(e)}
77         }
78         return nil
79 }
80
81 // Symlink creates a symbolic link.
82 func Symlink(oldname, newname string) error {
83         e := syscall.Symlink(oldname, newname)
84         if iserror(e) {
85                 return &LinkError{"symlink", oldname, newname, Errno(e)}
86         }
87         return nil
88 }
89
90 // Readlink reads the contents of a symbolic link: the destination of
91 // the link.  It returns the contents and an error, if any.
92 func Readlink(name string) (string, error) {
93         for len := 128; ; len *= 2 {
94                 b := make([]byte, len)
95                 n, e := syscall.Readlink(name, b)
96                 if iserror(e) {
97                         return "", &PathError{"readlink", name, Errno(e)}
98                 }
99                 if n < len {
100                         return string(b[0:n]), nil
101                 }
102         }
103         // Silence 6g.
104         return "", nil
105 }
106
107 // Rename renames a file.
108 func Rename(oldname, newname string) error {
109         e := syscall.Rename(oldname, newname)
110         if iserror(e) {
111                 return &LinkError{"rename", oldname, newname, Errno(e)}
112         }
113         return nil
114 }
115
116 // Chmod changes the mode of the named file to mode.
117 // If the file is a symbolic link, it changes the mode of the link's target.
118 func Chmod(name string, mode uint32) error {
119         if e := syscall.Chmod(name, mode); iserror(e) {
120                 return &PathError{"chmod", name, Errno(e)}
121         }
122         return nil
123 }
124
125 // Chmod changes the mode of the file to mode.
126 func (f *File) Chmod(mode uint32) error {
127         if e := syscall.Fchmod(f.fd, mode); iserror(e) {
128                 return &PathError{"chmod", f.name, Errno(e)}
129         }
130         return nil
131 }
132
133 // Chown changes the numeric uid and gid of the named file.
134 // If the file is a symbolic link, it changes the uid and gid of the link's target.
135 func Chown(name string, uid, gid int) error {
136         if e := syscall.Chown(name, uid, gid); iserror(e) {
137                 return &PathError{"chown", name, Errno(e)}
138         }
139         return nil
140 }
141
142 // Lchown changes the numeric uid and gid of the named file.
143 // If the file is a symbolic link, it changes the uid and gid of the link itself.
144 func Lchown(name string, uid, gid int) error {
145         if e := syscall.Lchown(name, uid, gid); iserror(e) {
146                 return &PathError{"lchown", name, Errno(e)}
147         }
148         return nil
149 }
150
151 // Chown changes the numeric uid and gid of the named file.
152 func (f *File) Chown(uid, gid int) error {
153         if e := syscall.Fchown(f.fd, uid, gid); iserror(e) {
154                 return &PathError{"chown", f.name, Errno(e)}
155         }
156         return nil
157 }
158
159 // Truncate changes the size of the file.
160 // It does not change the I/O offset.
161 func (f *File) Truncate(size int64) error {
162         if e := syscall.Ftruncate(f.fd, size); iserror(e) {
163                 return &PathError{"truncate", f.name, Errno(e)}
164         }
165         return nil
166 }
167
168 // Sync commits the current contents of the file to stable storage.
169 // Typically, this means flushing the file system's in-memory copy
170 // of recently written data to disk.
171 func (file *File) Sync() (err error) {
172         if file == nil {
173                 return EINVAL
174         }
175         if e := syscall.Fsync(file.fd); iserror(e) {
176                 return NewSyscallError("fsync", e)
177         }
178         return nil
179 }
180
181 // Chtimes changes the access and modification times of the named
182 // file, similar to the Unix utime() or utimes() functions.
183 //
184 // The argument times are in nanoseconds, although the underlying
185 // filesystem may truncate or round the values to a more
186 // coarse time unit.
187 func Chtimes(name string, atime_ns int64, mtime_ns int64) error {
188         var utimes [2]syscall.Timeval
189         utimes[0] = syscall.NsecToTimeval(atime_ns)
190         utimes[1] = syscall.NsecToTimeval(mtime_ns)
191         if e := syscall.Utimes(name, utimes[0:]); iserror(e) {
192                 return &PathError{"chtimes", name, Errno(e)}
193         }
194         return nil
195 }