~john-koepi/ubuntu/trusty/golang/default

« back to all changes in this revision

Viewing changes to src/pkg/os/file_windows.go

  • Committer: Bazaar Package Importer
  • Author(s): Ondřej Surý
  • Date: 2011-08-03 17:04:59 UTC
  • mfrom: (14.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20110803170459-wzd99m3567y80ila
Tags: 1:59-1
* Imported Upstream version 59
* Refresh patches to a new release
* Fix FTBFS on ARM (Closes: #634270)
* Update version.bash to work with Debian packaging and not hg
  repository

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
 
7
7
import (
8
8
        "runtime"
 
9
        "sync"
9
10
        "syscall"
10
11
)
11
12
 
 
13
// File represents an open file descriptor.
 
14
type File struct {
 
15
        fd      syscall.Handle
 
16
        name    string
 
17
        dirinfo *dirInfo   // nil unless directory being read
 
18
        nepipe  int        // number of consecutive EPIPE in Write
 
19
        l       sync.Mutex // used to implement windows pread/pwrite
 
20
}
 
21
 
 
22
// Fd returns the Windows handle referencing the open file.
 
23
func (file *File) Fd() syscall.Handle {
 
24
        if file == nil {
 
25
                return syscall.InvalidHandle
 
26
        }
 
27
        return file.fd
 
28
}
 
29
 
 
30
// NewFile returns a new File with the given file descriptor and name.
 
31
func NewFile(fd syscall.Handle, name string) *File {
 
32
        if fd < 0 {
 
33
                return nil
 
34
        }
 
35
        f := &File{fd: fd, name: name}
 
36
        runtime.SetFinalizer(f, (*File).Close)
 
37
        return f
 
38
}
 
39
 
12
40
// Auxiliary information if the File describes a directory
13
41
type dirInfo struct {
14
42
        stat         syscall.Stat_t
40
68
        if e != 0 {
41
69
                return nil, &PathError{"open", name, Errno(e)}
42
70
        }
43
 
        f := NewFile(int(r), name)
 
71
        f := NewFile(r, name)
44
72
        d.usefirststat = true
45
73
        f.dirinfo = d
46
74
        return f, nil
85
113
        }
86
114
        var e int
87
115
        if file.isdir() {
88
 
                e = syscall.FindClose(int32(file.fd))
 
116
                e = syscall.FindClose(syscall.Handle(file.fd))
89
117
        } else {
90
 
                e = syscall.CloseHandle(int32(file.fd))
 
118
                e = syscall.CloseHandle(syscall.Handle(file.fd))
91
119
        }
92
120
        var err Error
93
121
        if e != 0 {
94
122
                err = &PathError{"close", file.name, Errno(e)}
95
123
        }
96
 
        file.fd = -1 // so it can't be closed again
 
124
        file.fd = syscall.InvalidHandle // so it can't be closed again
97
125
 
98
126
        // no need for a finalizer anymore
99
127
        runtime.SetFinalizer(file, nil)
102
130
 
103
131
func (file *File) statFile(name string) (fi *FileInfo, err Error) {
104
132
        var stat syscall.ByHandleFileInformation
105
 
        e := syscall.GetFileInformationByHandle(int32(file.fd), &stat)
 
133
        e := syscall.GetFileInformationByHandle(syscall.Handle(file.fd), &stat)
106
134
        if e != 0 {
107
135
                return nil, &PathError{"stat", file.name, Errno(e)}
108
136
        }
156
184
                if di.usefirststat {
157
185
                        di.usefirststat = false
158
186
                } else {
159
 
                        e := syscall.FindNextFile(int32(file.fd), &di.stat.Windata)
 
187
                        e := syscall.FindNextFile(syscall.Handle(file.fd), &di.stat.Windata)
160
188
                        if e != 0 {
161
189
                                if e == syscall.ERROR_NO_MORE_FILES {
162
190
                                        break
207
235
                Offset:     uint32(off),
208
236
        }
209
237
        var done uint32
210
 
        e = syscall.ReadFile(int32(f.fd), b, &done, &o)
 
238
        e = syscall.ReadFile(syscall.Handle(f.fd), b, &done, &o)
211
239
        if e != 0 {
212
240
                return 0, e
213
241
        }
237
265
                Offset:     uint32(off),
238
266
        }
239
267
        var done uint32
240
 
        e = syscall.WriteFile(int32(f.fd), b, &done, &o)
 
268
        e = syscall.WriteFile(syscall.Handle(f.fd), b, &done, &o)
241
269
        if e != 0 {
242
270
                return 0, e
243
271
        }
268
296
        }
269
297
        return nil
270
298
}
 
299
 
 
300
// Pipe returns a connected pair of Files; reads from r return bytes written to w.
 
301
// It returns the files and an Error, if any.
 
302
func Pipe() (r *File, w *File, err Error) {
 
303
        var p [2]syscall.Handle
 
304
 
 
305
        // See ../syscall/exec.go for description of lock.
 
306
        syscall.ForkLock.RLock()
 
307
        e := syscall.Pipe(p[0:])
 
308
        if iserror(e) {
 
309
                syscall.ForkLock.RUnlock()
 
310
                return nil, nil, NewSyscallError("pipe", e)
 
311
        }
 
312
        syscall.CloseOnExec(p[0])
 
313
        syscall.CloseOnExec(p[1])
 
314
        syscall.ForkLock.RUnlock()
 
315
 
 
316
        return NewFile(p[0], "|0"), NewFile(p[1], "|1"), nil
 
317
}