Source file
src/os/file_unix.go
Documentation: os
1
2
3
4
5
6
7
8 package os
9
10 import (
11 "internal/poll"
12 "internal/syscall/unix"
13 "runtime"
14 "syscall"
15 )
16
17
18 func fixLongPath(path string) string {
19 return path
20 }
21
22 func rename(oldname, newname string) error {
23 fi, err := Lstat(newname)
24 if err == nil && fi.IsDir() {
25
26
27
28
29
30
31
32
33 if ofi, err := Lstat(oldname); err != nil {
34 if pe, ok := err.(*PathError); ok {
35 err = pe.Err
36 }
37 return &LinkError{"rename", oldname, newname, err}
38 } else if newname == oldname || !SameFile(fi, ofi) {
39 return &LinkError{"rename", oldname, newname, syscall.EEXIST}
40 }
41 }
42 err = ignoringEINTR(func() error {
43 return syscall.Rename(oldname, newname)
44 })
45 if err != nil {
46 return &LinkError{"rename", oldname, newname, err}
47 }
48 return nil
49 }
50
51
52
53
54
55 type file struct {
56 pfd poll.FD
57 name string
58 dirinfo *dirInfo
59 nonblock bool
60 stdoutOrErr bool
61 appendMode bool
62 }
63
64
65
66
67
68
69
70
71
72
73
74
75
76 func (f *File) Fd() uintptr {
77 if f == nil {
78 return ^(uintptr(0))
79 }
80
81
82
83
84
85
86 if f.nonblock {
87 f.pfd.SetBlocking()
88 }
89
90 return uintptr(f.pfd.Sysfd)
91 }
92
93
94
95
96
97
98
99
100
101
102 func NewFile(fd uintptr, name string) *File {
103 kind := kindNewFile
104 if nb, err := unix.IsNonblock(int(fd)); err == nil && nb {
105 kind = kindNonBlock
106 }
107 return newFile(fd, name, kind)
108 }
109
110
111 type newFileKind int
112
113 const (
114 kindNewFile newFileKind = iota
115 kindOpenFile
116 kindPipe
117 kindNonBlock
118 )
119
120
121
122
123 func newFile(fd uintptr, name string, kind newFileKind) *File {
124 fdi := int(fd)
125 if fdi < 0 {
126 return nil
127 }
128 f := &File{&file{
129 pfd: poll.FD{
130 Sysfd: fdi,
131 IsStream: true,
132 ZeroReadIsEOF: true,
133 },
134 name: name,
135 stdoutOrErr: fdi == 1 || fdi == 2,
136 }}
137
138 pollable := kind == kindOpenFile || kind == kindPipe || kind == kindNonBlock
139
140
141
142
143 if kind == kindOpenFile {
144 switch runtime.GOOS {
145 case "darwin", "ios", "dragonfly", "freebsd", "netbsd", "openbsd":
146 var st syscall.Stat_t
147 err := ignoringEINTR(func() error {
148 return syscall.Fstat(fdi, &st)
149 })
150 typ := st.Mode & syscall.S_IFMT
151
152
153
154
155
156
157
158 if err == nil && (typ == syscall.S_IFREG || typ == syscall.S_IFDIR) {
159 pollable = false
160 }
161
162
163
164
165
166 if (runtime.GOOS == "darwin" || runtime.GOOS == "ios") && typ == syscall.S_IFIFO {
167 pollable = false
168 }
169 }
170 }
171
172 if err := f.pfd.Init("file", pollable); err != nil {
173
174
175
176
177
178
179 } else if pollable {
180
181
182 if err := syscall.SetNonblock(fdi, true); err == nil {
183 f.nonblock = true
184 }
185 }
186
187 runtime.SetFinalizer(f.file, (*file).close)
188 return f
189 }
190
191
192
193
194 func epipecheck(file *File, e error) {
195 if e == syscall.EPIPE && file.stdoutOrErr {
196 sigpipe()
197 }
198 }
199
200
201
202 const DevNull = "/dev/null"
203
204
205
206 func openFileNolog(name string, flag int, perm FileMode) (*File, error) {
207 setSticky := false
208 if !supportsCreateWithStickyBit && flag&O_CREATE != 0 && perm&ModeSticky != 0 {
209 if _, err := Stat(name); IsNotExist(err) {
210 setSticky = true
211 }
212 }
213
214 var r int
215 for {
216 var e error
217 r, e = syscall.Open(name, flag|syscall.O_CLOEXEC, syscallMode(perm))
218 if e == nil {
219 break
220 }
221
222
223 if e == syscall.EINTR {
224 continue
225 }
226
227 return nil, &PathError{Op: "open", Path: name, Err: e}
228 }
229
230
231 if setSticky {
232 setStickyBit(name)
233 }
234
235
236
237 if !supportsCloseOnExec {
238 syscall.CloseOnExec(r)
239 }
240
241 return newFile(uintptr(r), name, kindOpenFile), nil
242 }
243
244 func (file *file) close() error {
245 if file == nil {
246 return syscall.EINVAL
247 }
248 if file.dirinfo != nil {
249 file.dirinfo.close()
250 file.dirinfo = nil
251 }
252 var err error
253 if e := file.pfd.Close(); e != nil {
254 if e == poll.ErrFileClosing {
255 e = ErrClosed
256 }
257 err = &PathError{Op: "close", Path: file.name, Err: e}
258 }
259
260
261 runtime.SetFinalizer(file, nil)
262 return err
263 }
264
265
266
267
268
269 func (f *File) seek(offset int64, whence int) (ret int64, err error) {
270 if f.dirinfo != nil {
271
272
273 f.dirinfo.close()
274 f.dirinfo = nil
275 }
276 ret, err = f.pfd.Seek(offset, whence)
277 runtime.KeepAlive(f)
278 return ret, err
279 }
280
281
282
283
284 func Truncate(name string, size int64) error {
285 e := ignoringEINTR(func() error {
286 return syscall.Truncate(name, size)
287 })
288 if e != nil {
289 return &PathError{Op: "truncate", Path: name, Err: e}
290 }
291 return nil
292 }
293
294
295
296 func Remove(name string) error {
297
298
299
300
301 e := ignoringEINTR(func() error {
302 return syscall.Unlink(name)
303 })
304 if e == nil {
305 return nil
306 }
307 e1 := ignoringEINTR(func() error {
308 return syscall.Rmdir(name)
309 })
310 if e1 == nil {
311 return nil
312 }
313
314
315
316
317
318
319
320
321
322
323 if e1 != syscall.ENOTDIR {
324 e = e1
325 }
326 return &PathError{Op: "remove", Path: name, Err: e}
327 }
328
329 func tempDir() string {
330 dir := Getenv("TMPDIR")
331 if dir == "" {
332 if runtime.GOOS == "android" {
333 dir = "/data/local/tmp"
334 } else {
335 dir = "/tmp"
336 }
337 }
338 return dir
339 }
340
341
342
343 func Link(oldname, newname string) error {
344 e := ignoringEINTR(func() error {
345 return syscall.Link(oldname, newname)
346 })
347 if e != nil {
348 return &LinkError{"link", oldname, newname, e}
349 }
350 return nil
351 }
352
353
354
355
356
357 func Symlink(oldname, newname string) error {
358 e := ignoringEINTR(func() error {
359 return syscall.Symlink(oldname, newname)
360 })
361 if e != nil {
362 return &LinkError{"symlink", oldname, newname, e}
363 }
364 return nil
365 }
366
367
368
369 func Readlink(name string) (string, error) {
370 for len := 128; ; len *= 2 {
371 b := make([]byte, len)
372 var (
373 n int
374 e error
375 )
376 for {
377 n, e = fixCount(syscall.Readlink(name, b))
378 if e != syscall.EINTR {
379 break
380 }
381 }
382
383 if runtime.GOOS == "aix" && e == syscall.ERANGE {
384 continue
385 }
386 if e != nil {
387 return "", &PathError{Op: "readlink", Path: name, Err: e}
388 }
389 if n < len {
390 return string(b[0:n]), nil
391 }
392 }
393 }
394
395 type unixDirent struct {
396 parent string
397 name string
398 typ FileMode
399 info FileInfo
400 }
401
402 func (d *unixDirent) Name() string { return d.name }
403 func (d *unixDirent) IsDir() bool { return d.typ.IsDir() }
404 func (d *unixDirent) Type() FileMode { return d.typ }
405
406 func (d *unixDirent) Info() (FileInfo, error) {
407 if d.info != nil {
408 return d.info, nil
409 }
410 return lstat(d.parent + "/" + d.name)
411 }
412
413 func newUnixDirent(parent, name string, typ FileMode) (DirEntry, error) {
414 ude := &unixDirent{
415 parent: parent,
416 name: name,
417 typ: typ,
418 }
419 if typ != ^FileMode(0) && !testingForceReadDirLstat {
420 return ude, nil
421 }
422
423 info, err := lstat(parent + "/" + name)
424 if err != nil {
425 return nil, err
426 }
427
428 ude.typ = info.Mode().Type()
429 ude.info = info
430 return ude, nil
431 }
432
View as plain text