OSDN Git Service

libgo: Update to weekly.2012-01-15.
[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 netbsd openbsd windows
6
7 package os
8
9 import (
10         "syscall"
11         "time"
12 )
13
14 func sigpipe() // implemented in package runtime
15
16 func epipecheck(file *File, e error) {
17         if e == syscall.EPIPE {
18                 file.nepipe++
19                 if file.nepipe >= 10 {
20                         sigpipe()
21                 }
22         } else {
23                 file.nepipe = 0
24         }
25 }
26
27 // LinkError records an error during a link or symlink or rename
28 // system call and the paths that caused it.
29 type LinkError struct {
30         Op  string
31         Old string
32         New string
33         Err error
34 }
35
36 func (e *LinkError) Error() string {
37         return e.Op + " " + e.Old + " " + e.New + ": " + e.Err.Error()
38 }
39
40 // Link creates a hard link.
41 func Link(oldname, newname string) error {
42         e := syscall.Link(oldname, newname)
43         if e != nil {
44                 return &LinkError{"link", oldname, newname, e}
45         }
46         return nil
47 }
48
49 // Symlink creates a symbolic link.
50 func Symlink(oldname, newname string) error {
51         e := syscall.Symlink(oldname, newname)
52         if e != nil {
53                 return &LinkError{"symlink", oldname, newname, e}
54         }
55         return nil
56 }
57
58 // Readlink reads the contents of a symbolic link: the destination of
59 // the link.  It returns the contents and an error, if any.
60 func Readlink(name string) (string, error) {
61         for len := 128; ; len *= 2 {
62                 b := make([]byte, len)
63                 n, e := syscall.Readlink(name, b)
64                 if e != nil {
65                         return "", &PathError{"readlink", name, e}
66                 }
67                 if n < len {
68                         return string(b[0:n]), nil
69                 }
70         }
71         // Silence 6g.
72         return "", nil
73 }
74
75 // Rename renames a file.
76 func Rename(oldname, newname string) error {
77         e := syscall.Rename(oldname, newname)
78         if e != nil {
79                 return &LinkError{"rename", oldname, newname, e}
80         }
81         return nil
82 }
83
84 // Chmod changes the mode of the named file to mode.
85 // If the file is a symbolic link, it changes the mode of the link's target.
86 func Chmod(name string, mode uint32) error {
87         if e := syscall.Chmod(name, mode); e != nil {
88                 return &PathError{"chmod", name, e}
89         }
90         return nil
91 }
92
93 // Chmod changes the mode of the file to mode.
94 func (f *File) Chmod(mode uint32) error {
95         if e := syscall.Fchmod(f.fd, mode); e != nil {
96                 return &PathError{"chmod", f.name, e}
97         }
98         return nil
99 }
100
101 // Chown changes the numeric uid and gid of the named file.
102 // If the file is a symbolic link, it changes the uid and gid of the link's target.
103 func Chown(name string, uid, gid int) error {
104         if e := syscall.Chown(name, uid, gid); e != nil {
105                 return &PathError{"chown", name, e}
106         }
107         return nil
108 }
109
110 // Lchown changes the numeric uid and gid of the named file.
111 // If the file is a symbolic link, it changes the uid and gid of the link itself.
112 func Lchown(name string, uid, gid int) error {
113         if e := syscall.Lchown(name, uid, gid); e != nil {
114                 return &PathError{"lchown", name, e}
115         }
116         return nil
117 }
118
119 // Chown changes the numeric uid and gid of the named file.
120 func (f *File) Chown(uid, gid int) error {
121         if e := syscall.Fchown(f.fd, uid, gid); e != nil {
122                 return &PathError{"chown", f.name, e}
123         }
124         return nil
125 }
126
127 // Truncate changes the size of the file.
128 // It does not change the I/O offset.
129 func (f *File) Truncate(size int64) error {
130         if e := syscall.Ftruncate(f.fd, size); e != nil {
131                 return &PathError{"truncate", f.name, e}
132         }
133         return nil
134 }
135
136 // Sync commits the current contents of the file to stable storage.
137 // Typically, this means flushing the file system's in-memory copy
138 // of recently written data to disk.
139 func (f *File) Sync() (err error) {
140         if f == nil {
141                 return EINVAL
142         }
143         if e := syscall.Fsync(f.fd); e != nil {
144                 return NewSyscallError("fsync", e)
145         }
146         return nil
147 }
148
149 // Chtimes changes the access and modification times of the named
150 // file, similar to the Unix utime() or utimes() functions.
151 //
152 // The underlying filesystem may truncate or round the values to a
153 // less precise time unit.
154 func Chtimes(name string, atime time.Time, mtime time.Time) error {
155         var utimes [2]syscall.Timeval
156         atime_ns := atime.Unix()*1e9 + int64(atime.Nanosecond())
157         mtime_ns := mtime.Unix()*1e9 + int64(mtime.Nanosecond())
158         utimes[0] = syscall.NsecToTimeval(atime_ns)
159         utimes[1] = syscall.NsecToTimeval(mtime_ns)
160         if e := syscall.Utimes(name, utimes[0:]); e != nil {
161                 return &PathError{"chtimes", name, e}
162         }
163         return nil
164 }