LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package file | 5 package file |
6 | 6 |
7 import ( | 7 import ( |
8 "os" | 8 "os" |
9 "syscall" | 9 "syscall" |
10 ) | 10 ) |
11 | 11 |
12 type File struct { | 12 type File struct { |
13 fd syscall.Handle // file descriptor number | 13 fd syscall.Handle // file descriptor number |
14 name string // file name at Open time | 14 name string // file name at Open time |
15 } | 15 } |
16 | 16 |
17 func newFile(fd syscall.Handle, name string) *File { | 17 func newFile(fd syscall.Handle, name string) *File { |
18 if fd == ^syscall.Handle(0) { | 18 if fd == ^syscall.Handle(0) { |
19 return nil | 19 return nil |
20 } | 20 } |
21 return &File{fd, name} | 21 return &File{fd, name} |
22 } | 22 } |
23 | 23 |
24 var ( | 24 var ( |
25 Stdin = newFile(syscall.Stdin, "/dev/stdin") | 25 Stdin = newFile(syscall.Stdin, "/dev/stdin") |
26 Stdout = newFile(syscall.Stdout, "/dev/stdout") | 26 Stdout = newFile(syscall.Stdout, "/dev/stdout") |
27 Stderr = newFile(syscall.Stderr, "/dev/stderr") | 27 Stderr = newFile(syscall.Stderr, "/dev/stderr") |
28 ) | 28 ) |
29 | 29 |
30 func OpenFile(name string, mode int, perm uint32) (file *File, err os.Error) { | 30 func OpenFile(name string, mode int, perm uint32) (file *File, err error) { |
31 r, e := syscall.Open(name, mode, perm) | 31 r, e := syscall.Open(name, mode, perm) |
32 if e != 0 { | 32 if e != 0 { |
33 err = os.Errno(e) | 33 err = os.Errno(e) |
34 } | 34 } |
35 return newFile(r, name), err | 35 return newFile(r, name), err |
36 } | 36 } |
37 | 37 |
38 const ( | 38 const ( |
39 O_RDONLY = syscall.O_RDONLY | 39 O_RDONLY = syscall.O_RDONLY |
40 O_RDWR = syscall.O_RDWR | 40 O_RDWR = syscall.O_RDWR |
41 O_CREATE = syscall.O_CREAT | 41 O_CREATE = syscall.O_CREAT |
42 O_TRUNC = syscall.O_TRUNC | 42 O_TRUNC = syscall.O_TRUNC |
43 ) | 43 ) |
44 | 44 |
45 func Open(name string) (file *File, err os.Error) { | 45 func Open(name string) (file *File, err error) { |
46 return OpenFile(name, O_RDONLY, 0) | 46 return OpenFile(name, O_RDONLY, 0) |
47 } | 47 } |
48 | 48 |
49 func Create(name string) (file *File, err os.Error) { | 49 func Create(name string) (file *File, err error) { |
50 return OpenFile(name, O_RDWR|O_CREATE|O_TRUNC, 0666) | 50 return OpenFile(name, O_RDWR|O_CREATE|O_TRUNC, 0666) |
51 } | 51 } |
52 | 52 |
53 func (file *File) Close() os.Error { | 53 func (file *File) Close() error { |
54 if file == nil { | 54 if file == nil { |
55 return os.EINVAL | 55 return os.EINVAL |
56 } | 56 } |
57 e := syscall.Close(file.fd) | 57 e := syscall.Close(file.fd) |
58 file.fd = syscall.InvalidHandle // so it can't be closed again | 58 file.fd = syscall.InvalidHandle // so it can't be closed again |
59 if e != 0 { | 59 if e != 0 { |
60 return os.Errno(e) | 60 return os.Errno(e) |
61 } | 61 } |
62 return nil | 62 return nil |
63 } | 63 } |
64 | 64 |
65 func (file *File) Read(b []byte) (ret int, err os.Error) { | 65 func (file *File) Read(b []byte) (ret int, err error) { |
66 if file == nil { | 66 if file == nil { |
67 return -1, os.EINVAL | 67 return -1, os.EINVAL |
68 } | 68 } |
69 r, e := syscall.Read(file.fd, b) | 69 r, e := syscall.Read(file.fd, b) |
70 if e != 0 { | 70 if e != 0 { |
71 err = os.Errno(e) | 71 err = os.Errno(e) |
72 } | 72 } |
73 return int(r), err | 73 return int(r), err |
74 } | 74 } |
75 | 75 |
76 func (file *File) Write(b []byte) (ret int, err os.Error) { | 76 func (file *File) Write(b []byte) (ret int, err error) { |
77 if file == nil { | 77 if file == nil { |
78 return -1, os.EINVAL | 78 return -1, os.EINVAL |
79 } | 79 } |
80 r, e := syscall.Write(file.fd, b) | 80 r, e := syscall.Write(file.fd, b) |
81 if e != 0 { | 81 if e != 0 { |
82 err = os.Errno(e) | 82 err = os.Errno(e) |
83 } | 83 } |
84 return int(r), err | 84 return int(r), err |
85 } | 85 } |
86 | 86 |
87 func (file *File) String() string { | 87 func (file *File) String() string { |
88 return file.name | 88 return file.name |
89 } | 89 } |
LEFT | RIGHT |