OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [os/] [file_posix.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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
// syscallMode returns the syscall-specific mode bits from Go's portable mode bits.
85
func syscallMode(i FileMode) (o uint32) {
86
        o |= uint32(i.Perm())
87
        if i&ModeSetuid != 0 {
88
                o |= syscall.S_ISUID
89
        }
90
        if i&ModeSetgid != 0 {
91
                o |= syscall.S_ISGID
92
        }
93
        if i&ModeSticky != 0 {
94
                o |= syscall.S_ISVTX
95
        }
96
        // No mapping for Go's ModeTemporary (plan9 only).
97
        return
98
}
99
 
100
// Chmod changes the mode of the named file to mode.
101
// If the file is a symbolic link, it changes the mode of the link's target.
102
func Chmod(name string, mode FileMode) error {
103
        if e := syscall.Chmod(name, syscallMode(mode)); e != nil {
104
                return &PathError{"chmod", name, e}
105
        }
106
        return nil
107
}
108
 
109
// Chmod changes the mode of the file to mode.
110
func (f *File) Chmod(mode FileMode) error {
111
        if e := syscall.Fchmod(f.fd, syscallMode(mode)); e != nil {
112
                return &PathError{"chmod", f.name, e}
113
        }
114
        return nil
115
}
116
 
117
// Chown changes the numeric uid and gid of the named file.
118
// If the file is a symbolic link, it changes the uid and gid of the link's target.
119
func Chown(name string, uid, gid int) error {
120
        if e := syscall.Chown(name, uid, gid); e != nil {
121
                return &PathError{"chown", name, e}
122
        }
123
        return nil
124
}
125
 
126
// Lchown changes the numeric uid and gid of the named file.
127
// If the file is a symbolic link, it changes the uid and gid of the link itself.
128
func Lchown(name string, uid, gid int) error {
129
        if e := syscall.Lchown(name, uid, gid); e != nil {
130
                return &PathError{"lchown", name, e}
131
        }
132
        return nil
133
}
134
 
135
// Chown changes the numeric uid and gid of the named file.
136
func (f *File) Chown(uid, gid int) error {
137
        if e := syscall.Fchown(f.fd, uid, gid); e != nil {
138
                return &PathError{"chown", f.name, e}
139
        }
140
        return nil
141
}
142
 
143
// Truncate changes the size of the file.
144
// It does not change the I/O offset.
145
func (f *File) Truncate(size int64) error {
146
        if e := syscall.Ftruncate(f.fd, size); e != nil {
147
                return &PathError{"truncate", f.name, e}
148
        }
149
        return nil
150
}
151
 
152
// Sync commits the current contents of the file to stable storage.
153
// Typically, this means flushing the file system's in-memory copy
154
// of recently written data to disk.
155
func (f *File) Sync() (err error) {
156
        if f == nil {
157
                return EINVAL
158
        }
159
        if e := syscall.Fsync(f.fd); e != nil {
160
                return NewSyscallError("fsync", e)
161
        }
162
        return nil
163
}
164
 
165
// Chtimes changes the access and modification times of the named
166
// file, similar to the Unix utime() or utimes() functions.
167
//
168
// The underlying filesystem may truncate or round the values to a
169
// less precise time unit.
170
func Chtimes(name string, atime time.Time, mtime time.Time) error {
171
        var utimes [2]syscall.Timeval
172
        atime_ns := atime.Unix()*1e9 + int64(atime.Nanosecond())
173
        mtime_ns := mtime.Unix()*1e9 + int64(mtime.Nanosecond())
174
        utimes[0] = syscall.NsecToTimeval(atime_ns)
175
        utimes[1] = syscall.NsecToTimeval(mtime_ns)
176
        if e := syscall.Utimes(name, utimes[0:]); e != nil {
177
                return &PathError{"chtimes", name, e}
178
        }
179
        return nil
180
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.