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

Subversion Repositories openrisc

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2011 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
package os
6
 
7
import (
8
        "runtime"
9
        "syscall"
10
        "time"
11
)
12
 
13
// File represents an open file descriptor.
14
type File struct {
15
        *file
16
}
17
 
18
// file is the real representation of *File.
19
// The extra level of indirection ensures that no clients of os
20
// can overwrite this data, which could cause the finalizer
21
// to close the wrong file descriptor.
22
type file struct {
23
        fd      int
24
        name    string
25
        dirinfo *dirInfo // nil unless directory being read
26
}
27
 
28
// Fd returns the integer Unix file descriptor referencing the open file.
29
func (file *File) Fd() int {
30
        if file == nil {
31
                return -1
32
        }
33
        return file.fd
34
}
35
 
36
// NewFile returns a new File with the given file descriptor and name.
37
func NewFile(fd int, name string) *File {
38
        if fd < 0 {
39
                return nil
40
        }
41
        f := &File{&file{fd: fd, name: name}}
42
        runtime.SetFinalizer(f.file, (*file).close)
43
        return f
44
}
45
 
46
// Auxiliary information if the File describes a directory
47
type dirInfo struct {
48
        buf  [syscall.STATMAX]byte // buffer for directory I/O
49
        nbuf int                   // length of buf; return value from Read
50
        bufp int                   // location of next record in buf.
51
}
52
 
53
func epipecheck(file *File, e error) {
54
}
55
 
56
// DevNull is the name of the operating system's ``null device.''
57
// On Unix-like systems, it is "/dev/null"; on Windows, "NUL".
58
const DevNull = "/dev/null"
59
 
60
// syscallMode returns the syscall-specific mode bits from Go's portable mode bits.
61
func syscallMode(i FileMode) (o uint32) {
62
        o |= uint32(i.Perm())
63
        if i&ModeAppend != 0 {
64
                o |= syscall.DMAPPEND
65
        }
66
        if i&ModeExclusive != 0 {
67
                o |= syscall.DMEXCL
68
        }
69
        if i&ModeTemporary != 0 {
70
                o |= syscall.DMTMP
71
        }
72
        return
73
}
74
 
75
// OpenFile is the generalized open call; most users will use Open
76
// or Create instead.  It opens the named file with specified flag
77
// (O_RDONLY etc.) and perm, (0666 etc.) if applicable.  If successful,
78
// methods on the returned File can be used for I/O.
79
// It returns the File and an error, if any.
80
func OpenFile(name string, flag int, perm FileMode) (file *File, err error) {
81
        var (
82
                fd     int
83
                e      error
84
                create bool
85
                excl   bool
86
                trunc  bool
87
                append bool
88
        )
89
 
90
        if flag&O_CREATE == O_CREATE {
91
                flag = flag & ^O_CREATE
92
                create = true
93
        }
94
        if flag&O_EXCL == O_EXCL {
95
                excl = true
96
        }
97
        if flag&O_TRUNC == O_TRUNC {
98
                trunc = true
99
        }
100
        // O_APPEND is emulated on Plan 9
101
        if flag&O_APPEND == O_APPEND {
102
                flag = flag &^ O_APPEND
103
                append = true
104
        }
105
 
106
        syscall.ForkLock.RLock()
107
        if (create && trunc) || excl {
108
                fd, e = syscall.Create(name, flag, syscallMode(perm))
109
        } else {
110
                fd, e = syscall.Open(name, flag)
111
                if e != nil && create {
112
                        var e1 error
113
                        fd, e1 = syscall.Create(name, flag, syscallMode(perm))
114
                        if e1 == nil {
115
                                e = nil
116
                        }
117
                }
118
        }
119
        syscall.ForkLock.RUnlock()
120
 
121
        if e != nil {
122
                return nil, &PathError{"open", name, e}
123
        }
124
 
125
        if append {
126
                if _, e = syscall.Seek(fd, 0, SEEK_END); e != nil {
127
                        return nil, &PathError{"seek", name, e}
128
                }
129
        }
130
 
131
        return NewFile(fd, name), nil
132
}
133
 
134
// Close closes the File, rendering it unusable for I/O.
135
// It returns an error, if any.
136
func (file *File) Close() error {
137
        return file.file.close()
138
}
139
 
140
func (file *file) close() error {
141
        if file == nil || file.fd < 0 {
142
                return Ebadfd
143
        }
144
        var err error
145
        syscall.ForkLock.RLock()
146
        if e := syscall.Close(file.fd); e != nil {
147
                err = &PathError{"close", file.name, e}
148
        }
149
        syscall.ForkLock.RUnlock()
150
        file.fd = -1 // so it can't be closed again
151
 
152
        // no need for a finalizer anymore
153
        runtime.SetFinalizer(file, nil)
154
        return err
155
}
156
 
157
// Stat returns the FileInfo structure describing file.
158
// It returns the FileInfo and an error, if any.
159
func (f *File) Stat() (FileInfo, error) {
160
        d, err := dirstat(f)
161
        if err != nil {
162
                return nil, err
163
        }
164
        return fileInfoFromStat(d), nil
165
}
166
 
167
// Truncate changes the size of the file.
168
// It does not change the I/O offset.
169
func (f *File) Truncate(size int64) error {
170
        var d Dir
171
        d.Null()
172
 
173
        d.Length = uint64(size)
174
 
175
        if e := syscall.Fwstat(f.fd, pdir(nil, &d)); e != nil {
176
                return &PathError{"truncate", f.name, e}
177
        }
178
        return nil
179
}
180
 
181
const chmodMask = uint32(syscall.DMAPPEND | syscall.DMEXCL | syscall.DMTMP | ModePerm)
182
 
183
// Chmod changes the mode of the file to mode.
184
func (f *File) Chmod(mode FileMode) error {
185
        var d Dir
186
 
187
        odir, e := dirstat(f)
188
        if e != nil {
189
                return &PathError{"chmod", f.name, e}
190
        }
191
        d.Null()
192
        d.Mode = odir.Mode&^chmodMask | syscallMode(mode)&chmodMask
193
        if e := syscall.Fwstat(f.fd, pdir(nil, &d)); e != nil {
194
                return &PathError{"chmod", f.name, e}
195
        }
196
        return nil
197
}
198
 
199
// Sync commits the current contents of the file to stable storage.
200
// Typically, this means flushing the file system's in-memory copy
201
// of recently written data to disk.
202
func (f *File) Sync() (err error) {
203
        if f == nil {
204
                return EINVAL
205
        }
206
 
207
        var d Dir
208
        d.Null()
209
 
210
        if e := syscall.Fwstat(f.fd, pdir(nil, &d)); e != nil {
211
                return NewSyscallError("fsync", e)
212
        }
213
        return nil
214
}
215
 
216
// read reads up to len(b) bytes from the File.
217
// It returns the number of bytes read and an error, if any.
218
func (f *File) read(b []byte) (n int, err error) {
219
        return syscall.Read(f.fd, b)
220
}
221
 
222
// pread reads len(b) bytes from the File starting at byte offset off.
223
// It returns the number of bytes read and the error, if any.
224
// EOF is signaled by a zero count with err set to nil.
225
func (f *File) pread(b []byte, off int64) (n int, err error) {
226
        return syscall.Pread(f.fd, b, off)
227
}
228
 
229
// write writes len(b) bytes to the File.
230
// It returns the number of bytes written and an error, if any.
231
func (f *File) write(b []byte) (n int, err error) {
232
        return syscall.Write(f.fd, b)
233
}
234
 
235
// pwrite writes len(b) bytes to the File starting at byte offset off.
236
// It returns the number of bytes written and an error, if any.
237
func (f *File) pwrite(b []byte, off int64) (n int, err error) {
238
        return syscall.Pwrite(f.fd, b, off)
239
}
240
 
241
// seek sets the offset for the next Read or Write on file to offset, interpreted
242
// according to whence: 0 means relative to the origin of the file, 1 means
243
// relative to the current offset, and 2 means relative to the end.
244
// It returns the new offset and an error, if any.
245
func (f *File) seek(offset int64, whence int) (ret int64, err error) {
246
        return syscall.Seek(f.fd, offset, whence)
247
}
248
 
249
// Truncate changes the size of the named file.
250
// If the file is a symbolic link, it changes the size of the link's target.
251
func Truncate(name string, size int64) error {
252
        var d Dir
253
        d.Null()
254
 
255
        d.Length = uint64(size)
256
 
257
        if e := syscall.Wstat(name, pdir(nil, &d)); e != nil {
258
                return &PathError{"truncate", name, e}
259
        }
260
        return nil
261
}
262
 
263
// Remove removes the named file or directory.
264
func Remove(name string) error {
265
        if e := syscall.Remove(name); e != nil {
266
                return &PathError{"remove", name, e}
267
        }
268
        return nil
269
}
270
 
271
// Rename renames a file.
272
func Rename(oldname, newname string) error {
273
        var d Dir
274
        d.Null()
275
 
276
        d.Name = newname
277
 
278
        if e := syscall.Wstat(oldname, pdir(nil, &d)); e != nil {
279
                return &PathError{"rename", oldname, e}
280
        }
281
        return nil
282
}
283
 
284
// Chmod changes the mode of the named file to mode.
285
func Chmod(name string, mode FileMode) error {
286
        var d Dir
287
 
288
        odir, e := dirstat(name)
289
        if e != nil {
290
                return &PathError{"chmod", name, e}
291
        }
292
        d.Null()
293
        d.Mode = odir.Mode&^chmodMask | syscallMode(mode)&chmodMask
294
        if e := syscall.Wstat(name, pdir(nil, &d)); e != nil {
295
                return &PathError{"chmod", name, e}
296
        }
297
        return nil
298
}
299
 
300
// Chtimes changes the access and modification times of the named
301
// file, similar to the Unix utime() or utimes() functions.
302
//
303
// The underlying filesystem may truncate or round the values to a
304
// less precise time unit.
305
func Chtimes(name string, atime time.Time, mtime time.Time) error {
306
        var d Dir
307
        d.Null()
308
 
309
        d.Atime = uint32(atime.Unix())
310
        d.Mtime = uint32(mtime.Unix())
311
 
312
        if e := syscall.Wstat(name, pdir(nil, &d)); e != nil {
313
                return &PathError{"chtimes", name, e}
314
        }
315
        return nil
316
}
317
 
318
func Pipe() (r *File, w *File, err error) {
319
        var p [2]int
320
 
321
        syscall.ForkLock.RLock()
322
        if e := syscall.Pipe(p[0:]); e != nil {
323
                syscall.ForkLock.RUnlock()
324
                return nil, nil, NewSyscallError("pipe", e)
325
        }
326
        syscall.ForkLock.RUnlock()
327
 
328
        return NewFile(p[0], "|0"), NewFile(p[1], "|1"), nil
329
}
330
 
331
// not supported on Plan 9
332
 
333
// Link creates a hard link.
334
func Link(oldname, newname string) error {
335
        return EPLAN9
336
}
337
 
338
func Symlink(oldname, newname string) error {
339
        return EPLAN9
340
}
341
 
342
func Readlink(name string) (string, error) {
343
        return "", EPLAN9
344
}
345
 
346
func Chown(name string, uid, gid int) error {
347
        return EPLAN9
348
}
349
 
350
func Lchown(name string, uid, gid int) error {
351
        return EPLAN9
352
}
353
 
354
func (f *File) Chown(uid, gid int) error {
355
        return EPLAN9
356
}
357
 
358
// TempDir returns the default directory to use for temporary files.
359
func TempDir() string {
360
        return "/tmp"
361
}

powered by: WebSVN 2.1.0

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