Alex Brainman | b07e4d9 | 2010-04-13 16:30:11 -0700 | [diff] [blame] | 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. |
| 4 | |
Alex Brainman | b07e4d9 | 2010-04-13 16:30:11 -0700 | [diff] [blame] | 5 | package os |
| 6 | |
| 7 | import ( |
| 8 | "runtime" |
| 9 | "syscall" |
| 10 | ) |
| 11 | |
| 12 | // Auxiliary information if the File describes a directory |
| 13 | type dirInfo struct { |
| 14 | stat syscall.Stat_t |
| 15 | usefirststat bool |
| 16 | } |
| 17 | |
Peter Mundy | 12befd0 | 2010-08-03 13:03:50 -0700 | [diff] [blame] | 18 | const DevNull = "NUL" |
| 19 | |
Alex Brainman | b07e4d9 | 2010-04-13 16:30:11 -0700 | [diff] [blame] | 20 | func (file *File) isdir() bool { return file != nil && file.dirinfo != nil } |
| 21 | |
Rob Pike | e45b58f | 2010-08-04 08:34:52 +1000 | [diff] [blame] | 22 | func openFile(name string, flag int, perm uint32) (file *File, err Error) { |
Alex Brainman | b07e4d9 | 2010-04-13 16:30:11 -0700 | [diff] [blame] | 23 | r, e := syscall.Open(name, flag|syscall.O_CLOEXEC, perm) |
| 24 | if e != 0 { |
| 25 | return nil, &PathError{"open", name, Errno(e)} |
| 26 | } |
| 27 | |
| 28 | // There's a race here with fork/exec, which we are |
| 29 | // content to live with. See ../syscall/exec.go |
| 30 | if syscall.O_CLOEXEC == 0 { // O_CLOEXEC not supported |
| 31 | syscall.CloseOnExec(r) |
| 32 | } |
| 33 | |
| 34 | return NewFile(r, name), nil |
| 35 | } |
| 36 | |
| 37 | func openDir(name string) (file *File, err Error) { |
| 38 | d := new(dirInfo) |
| 39 | r, e := syscall.FindFirstFile(syscall.StringToUTF16Ptr(name+"\\*"), &d.stat.Windata) |
| 40 | if e != 0 { |
| 41 | return nil, &PathError{"open", name, Errno(e)} |
| 42 | } |
| 43 | f := NewFile(int(r), name) |
| 44 | d.usefirststat = true |
| 45 | f.dirinfo = d |
| 46 | return f, nil |
| 47 | } |
| 48 | |
Rob Pike | 8a90fd3 | 2011-04-04 23:42:14 -0700 | [diff] [blame] | 49 | // OpenFile is the generalized open call; most users will use Open |
| 50 | // or Create instead. It opens the named file with specified flag |
| 51 | // (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful, |
| 52 | // methods on the returned File can be used for I/O. |
Alex Brainman | b07e4d9 | 2010-04-13 16:30:11 -0700 | [diff] [blame] | 53 | // It returns the File and an Error, if any. |
Rob Pike | 8a90fd3 | 2011-04-04 23:42:14 -0700 | [diff] [blame] | 54 | func OpenFile(name string, flag int, perm uint32) (file *File, err Error) { |
Alex Brainman | b07e4d9 | 2010-04-13 16:30:11 -0700 | [diff] [blame] | 55 | // TODO(brainman): not sure about my logic of assuming it is dir first, then fall back to file |
| 56 | r, e := openDir(name) |
| 57 | if e == nil { |
Alex Brainman | 17fe247 | 2010-10-04 17:31:49 +1100 | [diff] [blame] | 58 | if flag&O_WRONLY != 0 || flag&O_RDWR != 0 { |
| 59 | r.Close() |
| 60 | return nil, &PathError{"open", name, EISDIR} |
| 61 | } |
Alex Brainman | b07e4d9 | 2010-04-13 16:30:11 -0700 | [diff] [blame] | 62 | return r, nil |
| 63 | } |
Alex Brainman | 0352659 | 2010-04-13 22:30:41 -0700 | [diff] [blame] | 64 | r, e = openFile(name, flag, perm) |
Alex Brainman | b07e4d9 | 2010-04-13 16:30:11 -0700 | [diff] [blame] | 65 | if e == nil { |
| 66 | return r, nil |
| 67 | } |
Alex Brainman | 17fe247 | 2010-10-04 17:31:49 +1100 | [diff] [blame] | 68 | // Imitating Unix behavior by replacing syscall.ERROR_PATH_NOT_FOUND with |
| 69 | // os.ENOTDIR. Not sure if we should go into that. |
| 70 | if e2, ok := e.(*PathError); ok { |
| 71 | if e3, ok := e2.Error.(Errno); ok { |
| 72 | if e3 == Errno(syscall.ERROR_PATH_NOT_FOUND) { |
| 73 | return nil, &PathError{"open", name, ENOTDIR} |
| 74 | } |
| 75 | } |
| 76 | } |
Alex Brainman | b07e4d9 | 2010-04-13 16:30:11 -0700 | [diff] [blame] | 77 | return nil, e |
| 78 | } |
| 79 | |
| 80 | // Close closes the File, rendering it unusable for I/O. |
| 81 | // It returns an Error, if any. |
| 82 | func (file *File) Close() Error { |
| 83 | if file == nil || file.fd < 0 { |
| 84 | return EINVAL |
| 85 | } |
| 86 | var e int |
| 87 | if file.isdir() { |
Alex Brainman | cf75c86 | 2011-02-11 10:15:51 +1100 | [diff] [blame] | 88 | e = syscall.FindClose(int32(file.fd)) |
Alex Brainman | b07e4d9 | 2010-04-13 16:30:11 -0700 | [diff] [blame] | 89 | } else { |
Alex Brainman | cf75c86 | 2011-02-11 10:15:51 +1100 | [diff] [blame] | 90 | e = syscall.CloseHandle(int32(file.fd)) |
Alex Brainman | b07e4d9 | 2010-04-13 16:30:11 -0700 | [diff] [blame] | 91 | } |
| 92 | var err Error |
| 93 | if e != 0 { |
| 94 | err = &PathError{"close", file.name, Errno(e)} |
| 95 | } |
| 96 | file.fd = -1 // so it can't be closed again |
| 97 | |
| 98 | // no need for a finalizer anymore |
| 99 | runtime.SetFinalizer(file, nil) |
| 100 | return err |
| 101 | } |
| 102 | |
Alex Brainman | fb6b391 | 2010-04-26 23:17:14 -0700 | [diff] [blame] | 103 | func (file *File) statFile(name string) (fi *FileInfo, err Error) { |
| 104 | var stat syscall.ByHandleFileInformation |
Alex Brainman | cf75c86 | 2011-02-11 10:15:51 +1100 | [diff] [blame] | 105 | e := syscall.GetFileInformationByHandle(int32(file.fd), &stat) |
| 106 | if e != 0 { |
Alex Brainman | fb6b391 | 2010-04-26 23:17:14 -0700 | [diff] [blame] | 107 | return nil, &PathError{"stat", file.name, Errno(e)} |
| 108 | } |
| 109 | return fileInfoFromByHandleInfo(new(FileInfo), file.name, &stat), nil |
| 110 | } |
| 111 | |
| 112 | // Stat returns the FileInfo structure describing file. |
| 113 | // It returns the FileInfo and an error, if any. |
| 114 | func (file *File) Stat() (fi *FileInfo, err Error) { |
| 115 | if file == nil || file.fd < 0 { |
| 116 | return nil, EINVAL |
| 117 | } |
| 118 | if file.isdir() { |
| 119 | // I don't know any better way to do that for directory |
| 120 | return Stat(file.name) |
| 121 | } |
| 122 | return file.statFile(file.name) |
| 123 | } |
| 124 | |
Alex Brainman | b07e4d9 | 2010-04-13 16:30:11 -0700 | [diff] [blame] | 125 | // Readdir reads the contents of the directory associated with file and |
| 126 | // returns an array of up to count FileInfo structures, as would be returned |
Brad Fitzpatrick | 4da5cd4 | 2011-05-16 09:26:16 -0700 | [diff] [blame] | 127 | // by Lstat, in directory order. Subsequent calls on the same file will yield |
Alex Brainman | b07e4d9 | 2010-04-13 16:30:11 -0700 | [diff] [blame] | 128 | // further FileInfos. |
Brad Fitzpatrick | 4da5cd4 | 2011-05-16 09:26:16 -0700 | [diff] [blame] | 129 | // |
| 130 | // If n > 0, Readdir returns at most n names. In this case, if |
| 131 | // Readdirnames returns an empty slice, it will return a non-nil error |
| 132 | // explaining why. At the end of a directory, the error is os.EOF. |
| 133 | // |
| 134 | // If n <= 0, Readdir returns all the FileInfo from the directory in |
| 135 | // a single slice. In this case, if Readdir succeeds (reads all |
| 136 | // the way to the end of the directory), it returns the slice and a |
| 137 | // nil os.Error. If it encounters an error before the end of the |
| 138 | // directory, Readdir returns the FileInfo read until that point |
| 139 | // and a non-nil error. |
| 140 | func (file *File) Readdir(n int) (fi []FileInfo, err Error) { |
Peter Mundy | bfb1276 | 2010-09-23 22:06:59 -0400 | [diff] [blame] | 141 | if file == nil || file.fd < 0 { |
| 142 | return nil, EINVAL |
| 143 | } |
| 144 | if !file.isdir() { |
| 145 | return nil, &PathError{"Readdir", file.name, ENOTDIR} |
| 146 | } |
Alex Brainman | b07e4d9 | 2010-04-13 16:30:11 -0700 | [diff] [blame] | 147 | di := file.dirinfo |
Alex Brainman | 505f0bb | 2011-05-29 11:59:35 +1000 | [diff] [blame^] | 148 | wantAll := n <= 0 |
Brad Fitzpatrick | 4da5cd4 | 2011-05-16 09:26:16 -0700 | [diff] [blame] | 149 | size := n |
Alex Brainman | 505f0bb | 2011-05-29 11:59:35 +1000 | [diff] [blame^] | 150 | if wantAll { |
| 151 | n = -1 |
Alex Brainman | b07e4d9 | 2010-04-13 16:30:11 -0700 | [diff] [blame] | 152 | size = 100 |
| 153 | } |
| 154 | fi = make([]FileInfo, 0, size) // Empty with room to grow. |
Brad Fitzpatrick | 4da5cd4 | 2011-05-16 09:26:16 -0700 | [diff] [blame] | 155 | for n != 0 { |
Alex Brainman | b07e4d9 | 2010-04-13 16:30:11 -0700 | [diff] [blame] | 156 | if di.usefirststat { |
| 157 | di.usefirststat = false |
| 158 | } else { |
Alex Brainman | cf75c86 | 2011-02-11 10:15:51 +1100 | [diff] [blame] | 159 | e := syscall.FindNextFile(int32(file.fd), &di.stat.Windata) |
Alex Brainman | b07e4d9 | 2010-04-13 16:30:11 -0700 | [diff] [blame] | 160 | if e != 0 { |
| 161 | if e == syscall.ERROR_NO_MORE_FILES { |
| 162 | break |
| 163 | } else { |
Brad Fitzpatrick | 4da5cd4 | 2011-05-16 09:26:16 -0700 | [diff] [blame] | 164 | err = &PathError{"FindNextFile", file.name, Errno(e)} |
| 165 | if !wantAll { |
| 166 | fi = nil |
| 167 | } |
| 168 | return |
Alex Brainman | b07e4d9 | 2010-04-13 16:30:11 -0700 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | } |
| 172 | var f FileInfo |
Alex Brainman | fb6b391 | 2010-04-26 23:17:14 -0700 | [diff] [blame] | 173 | fileInfoFromWin32finddata(&f, &di.stat.Windata) |
Alex Brainman | b07e4d9 | 2010-04-13 16:30:11 -0700 | [diff] [blame] | 174 | if f.Name == "." || f.Name == ".." { // Useless names |
| 175 | continue |
| 176 | } |
Brad Fitzpatrick | 4da5cd4 | 2011-05-16 09:26:16 -0700 | [diff] [blame] | 177 | n-- |
Russ Cox | 69c4e93 | 2010-10-27 19:47:23 -0700 | [diff] [blame] | 178 | fi = append(fi, f) |
Alex Brainman | b07e4d9 | 2010-04-13 16:30:11 -0700 | [diff] [blame] | 179 | } |
Brad Fitzpatrick | 4da5cd4 | 2011-05-16 09:26:16 -0700 | [diff] [blame] | 180 | if !wantAll && len(fi) == 0 { |
| 181 | return fi, EOF |
| 182 | } |
Alex Brainman | b07e4d9 | 2010-04-13 16:30:11 -0700 | [diff] [blame] | 183 | return fi, nil |
| 184 | } |
Alex Brainman | fb6b391 | 2010-04-26 23:17:14 -0700 | [diff] [blame] | 185 | |
Alex Brainman | b1deb3b | 2011-04-26 18:09:46 +1000 | [diff] [blame] | 186 | // read reads up to len(b) bytes from the File. |
| 187 | // It returns the number of bytes read and an error, if any. |
| 188 | func (f *File) read(b []byte) (n int, err int) { |
| 189 | f.l.Lock() |
| 190 | defer f.l.Unlock() |
| 191 | return syscall.Read(f.fd, b) |
| 192 | } |
| 193 | |
| 194 | // pread reads len(b) bytes from the File starting at byte offset off. |
| 195 | // It returns the number of bytes read and the error, if any. |
| 196 | // EOF is signaled by a zero count with err set to 0. |
| 197 | func (f *File) pread(b []byte, off int64) (n int, err int) { |
| 198 | f.l.Lock() |
| 199 | defer f.l.Unlock() |
| 200 | curoffset, e := syscall.Seek(f.fd, 0, 1) |
| 201 | if e != 0 { |
| 202 | return 0, e |
| 203 | } |
| 204 | defer syscall.Seek(f.fd, curoffset, 0) |
| 205 | o := syscall.Overlapped{ |
| 206 | OffsetHigh: uint32(off >> 32), |
| 207 | Offset: uint32(off), |
| 208 | } |
| 209 | var done uint32 |
| 210 | e = syscall.ReadFile(int32(f.fd), b, &done, &o) |
| 211 | if e != 0 { |
| 212 | return 0, e |
| 213 | } |
| 214 | return int(done), 0 |
| 215 | } |
| 216 | |
| 217 | // write writes len(b) bytes to the File. |
| 218 | // It returns the number of bytes written and an error, if any. |
| 219 | func (f *File) write(b []byte) (n int, err int) { |
| 220 | f.l.Lock() |
| 221 | defer f.l.Unlock() |
| 222 | return syscall.Write(f.fd, b) |
| 223 | } |
| 224 | |
| 225 | // pwrite writes len(b) bytes to the File starting at byte offset off. |
| 226 | // It returns the number of bytes written and an error, if any. |
| 227 | func (f *File) pwrite(b []byte, off int64) (n int, err int) { |
| 228 | f.l.Lock() |
| 229 | defer f.l.Unlock() |
| 230 | curoffset, e := syscall.Seek(f.fd, 0, 1) |
| 231 | if e != 0 { |
| 232 | return 0, e |
| 233 | } |
| 234 | defer syscall.Seek(f.fd, curoffset, 0) |
| 235 | o := syscall.Overlapped{ |
| 236 | OffsetHigh: uint32(off >> 32), |
| 237 | Offset: uint32(off), |
| 238 | } |
| 239 | var done uint32 |
| 240 | e = syscall.WriteFile(int32(f.fd), b, &done, &o) |
| 241 | if e != 0 { |
| 242 | return 0, e |
| 243 | } |
| 244 | return int(done), 0 |
| 245 | } |
| 246 | |
| 247 | // seek sets the offset for the next Read or Write on file to offset, interpreted |
| 248 | // according to whence: 0 means relative to the origin of the file, 1 means |
| 249 | // relative to the current offset, and 2 means relative to the end. |
| 250 | // It returns the new offset and an error, if any. |
| 251 | func (f *File) seek(offset int64, whence int) (ret int64, err int) { |
| 252 | f.l.Lock() |
| 253 | defer f.l.Unlock() |
| 254 | return syscall.Seek(f.fd, offset, whence) |
| 255 | } |
| 256 | |
Alex Brainman | fb6b391 | 2010-04-26 23:17:14 -0700 | [diff] [blame] | 257 | // Truncate changes the size of the named file. |
| 258 | // If the file is a symbolic link, it changes the size of the link's target. |
| 259 | func Truncate(name string, size int64) Error { |
Rob Pike | 80c5ef9 | 2011-04-04 23:57:08 -0700 | [diff] [blame] | 260 | f, e := OpenFile(name, O_WRONLY|O_CREATE, 0666) |
Alex Brainman | fb6b391 | 2010-04-26 23:17:14 -0700 | [diff] [blame] | 261 | if e != nil { |
| 262 | return e |
| 263 | } |
| 264 | defer f.Close() |
| 265 | e1 := f.Truncate(size) |
| 266 | if e1 != nil { |
| 267 | return e1 |
| 268 | } |
| 269 | return nil |
| 270 | } |