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.
5 // +build aix darwin dragonfly freebsd linux nacl netbsd openbsd solaris windows
14 func sigpipe() // implemented in package runtime
16 // Readlink returns the destination of the named symbolic link.
17 // If there is an error, it will be of type *PathError.
18 func Readlink(name
string) (string, error
) {
19 for len := 128; ; len *= 2 {
20 b
:= make([]byte, len)
21 n
, e
:= fixCount(syscall
.Readlink(fixLongPath(name
), b
))
23 if e
== syscall
.ERANGE
{
27 return "", &PathError
{"readlink", name
, e
}
30 return string(b
[0:n
]), nil
35 // syscallMode returns the syscall-specific mode bits from Go's portable mode bits.
36 func syscallMode(i FileMode
) (o
uint32) {
38 if i
&ModeSetuid
!= 0 {
41 if i
&ModeSetgid
!= 0 {
44 if i
&ModeSticky
!= 0 {
47 // No mapping for Go's ModeTemporary (plan9 only).
51 // See docs in file.go:Chmod.
52 func chmod(name
string, mode FileMode
) error
{
53 if e
:= syscall
.Chmod(fixLongPath(name
), syscallMode(mode
)); e
!= nil {
54 return &PathError
{"chmod", name
, e
}
59 // See docs in file.go:(*File).Chmod.
60 func (f
*File
) chmod(mode FileMode
) error
{
61 if err
:= f
.checkValid("chmod"); err
!= nil {
64 if e
:= f
.pfd
.Fchmod(syscallMode(mode
)); e
!= nil {
65 return f
.wrapErr("chmod", e
)
70 // Chown changes the numeric uid and gid of the named file.
71 // If the file is a symbolic link, it changes the uid and gid of the link's target.
72 // If there is an error, it will be of type *PathError.
74 // On Windows, it always returns the syscall.EWINDOWS error, wrapped
76 func Chown(name
string, uid
, gid
int) error
{
77 if e
:= syscall
.Chown(name
, uid
, gid
); e
!= nil {
78 return &PathError
{"chown", name
, e
}
83 // Lchown changes the numeric uid and gid of the named file.
84 // If the file is a symbolic link, it changes the uid and gid of the link itself.
85 // If there is an error, it will be of type *PathError.
87 // On Windows, it always returns the syscall.EWINDOWS error, wrapped
89 func Lchown(name
string, uid
, gid
int) error
{
90 if e
:= syscall
.Lchown(name
, uid
, gid
); e
!= nil {
91 return &PathError
{"lchown", name
, e
}
96 // Chown changes the numeric uid and gid of the named file.
97 // If there is an error, it will be of type *PathError.
99 // On Windows, it always returns the syscall.EWINDOWS error, wrapped
101 func (f
*File
) Chown(uid
, gid
int) error
{
102 if err
:= f
.checkValid("chown"); err
!= nil {
105 if e
:= f
.pfd
.Fchown(uid
, gid
); e
!= nil {
106 return f
.wrapErr("chown", e
)
111 // Truncate changes the size of the file.
112 // It does not change the I/O offset.
113 // If there is an error, it will be of type *PathError.
114 func (f
*File
) Truncate(size
int64) error
{
115 if err
:= f
.checkValid("truncate"); err
!= nil {
118 if e
:= f
.pfd
.Ftruncate(size
); e
!= nil {
119 return f
.wrapErr("truncate", e
)
124 // Sync commits the current contents of the file to stable storage.
125 // Typically, this means flushing the file system's in-memory copy
126 // of recently written data to disk.
127 func (f
*File
) Sync() error
{
128 if err
:= f
.checkValid("sync"); err
!= nil {
131 if e
:= f
.pfd
.Fsync(); e
!= nil {
132 return f
.wrapErr("sync", e
)
137 // Chtimes changes the access and modification times of the named
138 // file, similar to the Unix utime() or utimes() functions.
140 // The underlying filesystem may truncate or round the values to a
141 // less precise time unit.
142 // If there is an error, it will be of type *PathError.
143 func Chtimes(name
string, atime time
.Time
, mtime time
.Time
) error
{
144 var utimes
[2]syscall
.Timespec
145 utimes
[0] = syscall
.NsecToTimespec(atime
.UnixNano())
146 utimes
[1] = syscall
.NsecToTimespec(mtime
.UnixNano())
147 if e
:= syscall
.UtimesNano(fixLongPath(name
), utimes
[0:]); e
!= nil {
148 return &PathError
{"chtimes", name
, e
}
153 // Chdir changes the current working directory to the file,
154 // which must be a directory.
155 // If there is an error, it will be of type *PathError.
156 func (f
*File
) Chdir() error
{
157 if err
:= f
.checkValid("chdir"); err
!= nil {
160 if e
:= f
.pfd
.Fchdir(); e
!= nil {
161 return f
.wrapErr("chdir", e
)
166 // setDeadline sets the read and write deadline.
167 func (f
*File
) setDeadline(t time
.Time
) error
{
168 if err
:= f
.checkValid("SetDeadline"); err
!= nil {
171 return f
.pfd
.SetDeadline(t
)
174 // setReadDeadline sets the read deadline.
175 func (f
*File
) setReadDeadline(t time
.Time
) error
{
176 if err
:= f
.checkValid("SetReadDeadline"); err
!= nil {
179 return f
.pfd
.SetReadDeadline(t
)
182 // setWriteDeadline sets the write deadline.
183 func (f
*File
) setWriteDeadline(t time
.Time
) error
{
184 if err
:= f
.checkValid("SetWriteDeadline"); err
!= nil {
187 return f
.pfd
.SetWriteDeadline(t
)
190 // checkValid checks whether f is valid for use.
191 // If not, it returns an appropriate error, perhaps incorporating the operation name op.
192 func (f
*File
) checkValid(op
string) error
{