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 // Chmod changes the mode of the named file to mode.
52 // If the file is a symbolic link, it changes the mode of the link's target.
53 // If there is an error, it will be of type *PathError.
54 func Chmod(name
string, mode FileMode
) error
{
55 if e
:= syscall
.Chmod(name
, syscallMode(mode
)); e
!= nil {
56 return &PathError
{"chmod", name
, e
}
61 // Chmod changes the mode of the file to mode.
62 // If there is an error, it will be of type *PathError.
63 func (f
*File
) Chmod(mode FileMode
) error
{
64 if err
:= f
.checkValid("chmod"); err
!= nil {
67 if e
:= syscall
.Fchmod(f
.fd
, syscallMode(mode
)); e
!= nil {
68 return &PathError
{"chmod", f
.name
, e
}
73 // Chown changes the numeric uid and gid of the named file.
74 // If the file is a symbolic link, it changes the uid and gid of the link's target.
75 // If there is an error, it will be of type *PathError.
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.
86 func Lchown(name
string, uid
, gid
int) error
{
87 if e
:= syscall
.Lchown(name
, uid
, gid
); e
!= nil {
88 return &PathError
{"lchown", name
, e
}
93 // Chown changes the numeric uid and gid of the named file.
94 // If there is an error, it will be of type *PathError.
95 func (f
*File
) Chown(uid
, gid
int) error
{
96 if err
:= f
.checkValid("chown"); err
!= nil {
99 if e
:= syscall
.Fchown(f
.fd
, uid
, gid
); e
!= nil {
100 return &PathError
{"chown", f
.name
, e
}
105 // Truncate changes the size of the file.
106 // It does not change the I/O offset.
107 // If there is an error, it will be of type *PathError.
108 func (f
*File
) Truncate(size
int64) error
{
109 if err
:= f
.checkValid("truncate"); err
!= nil {
112 if e
:= syscall
.Ftruncate(f
.fd
, size
); e
!= nil {
113 return &PathError
{"truncate", f
.name
, e
}
118 // Sync commits the current contents of the file to stable storage.
119 // Typically, this means flushing the file system's in-memory copy
120 // of recently written data to disk.
121 func (f
*File
) Sync() error
{
122 if err
:= f
.checkValid("sync"); err
!= nil {
125 if e
:= syscall
.Fsync(f
.fd
); e
!= nil {
126 return &PathError
{"sync", f
.name
, e
}
131 // Chtimes changes the access and modification times of the named
132 // file, similar to the Unix utime() or utimes() functions.
134 // The underlying filesystem may truncate or round the values to a
135 // less precise time unit.
136 // If there is an error, it will be of type *PathError.
137 func Chtimes(name
string, atime time
.Time
, mtime time
.Time
) error
{
138 var utimes
[2]syscall
.Timespec
139 utimes
[0] = syscall
.NsecToTimespec(atime
.UnixNano())
140 utimes
[1] = syscall
.NsecToTimespec(mtime
.UnixNano())
141 if e
:= syscall
.UtimesNano(fixLongPath(name
), utimes
[0:]); e
!= nil {
142 return &PathError
{"chtimes", name
, e
}