| 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 |
|
| 5 |
package os |
| 6 |
|
| 7 |
import ( |
| 8 |
"internal/poll" |
| 9 |
"io" |
| 10 |
"syscall" |
| 11 |
"unsafe" |
| 12 |
) |
| 13 |
|
| 14 |
// FIXME: pathconf returns long, not int. |
| 15 |
//extern pathconf |
| 16 |
func libc_pathconf(*byte, int32) int |
| 17 |
|
| 18 |
func direntType(*syscall.Dirent) byte |
| 19 |
|
| 20 |
func clen(n []byte) int { |
| 21 |
for i := 0; i < len(n); i++ { |
| 22 |
if n[i] == 0 { |
| 23 |
return i |
| 24 |
} |
| 25 |
} |
| 26 |
return len(n) |
| 27 |
} |
| 28 |
|
| 29 |
func (f *File) readdir(n int, mode readdirMode) (names []string, dirents []DirEntry, infos []FileInfo, err error) { |
| 30 |
// If this file has no dirinfo, create one. |
| 31 |
if f.dirinfo == nil { |
| 32 |
fd, call, err := poll.DupCloseOnExec(int(f.pfd.Sysfd)) |
| 33 |
if err != nil { |
| 34 |
return nil, nil, nil, NewSyscallError(call, err) |
| 35 |
} |
| 36 |
|
| 37 |
syscall.Entersyscall() |
| 38 |
r := libc_fdopendir(int32(fd)) |
| 39 |
errno := syscall.GetErrno() |
| 40 |
syscall.Exitsyscall() |
| 41 |
if r == nil { |
| 42 |
return nil, nil, nil, &PathError{"fdopendir", f.name, errno} |
| 43 |
} |
| 44 |
|
| 45 |
f.dirinfo = &dirInfo{r} |
| 46 |
} |
| 47 |
dir := f.dirinfo.dir |
| 48 |
|
| 49 |
// Change the meaning of n for the implementation below. |
| 50 |
// |
| 51 |
// The n above was for the public interface of "if n <= 0, |
| 52 |
// Readdir returns all the FileInfo from the directory in a |
| 53 |
// single slice". |
| 54 |
// |
| 55 |
// But below, we use only negative to mean looping until the |
| 56 |
// end and positive to mean bounded, with positive |
| 57 |
// terminating at 0. |
| 58 |
if n == 0 { |
| 59 |
n = -1 |
| 60 |
} |
| 61 |
|
| 62 |
for n != 0 { |
| 63 |
syscall.Entersyscall() |
| 64 |
syscall.SetErrno(0) |
| 65 |
dirent := libc_readdir(dir) |
| 66 |
errno := syscall.GetErrno() |
| 67 |
syscall.Exitsyscall() |
| 68 |
|
| 69 |
if dirent == nil { |
| 70 |
if errno != 0 { |
| 71 |
return names, dirents, infos, &PathError{Op: "readdir", Path: f.name, Err: errno} |
| 72 |
} |
| 73 |
break // EOF |
| 74 |
} |
| 75 |
|
| 76 |
// In some cases the actual name can be longer than |
| 77 |
// the Name field. |
| 78 |
name := (*[1 << 16]byte)(unsafe.Pointer(&dirent.Name[0]))[:] |
| 79 |
for i, c := range name { |
| 80 |
if c == 0 { |
| 81 |
name = name[:i] |
| 82 |
break |
| 83 |
} |
| 84 |
} |
| 85 |
// Check for useless names before allocating a string. |
| 86 |
if (len(name) == 1 && name[0] == '.') || (len(name) == 2 && name[0] == '.' && name[1] == '.') { |
| 87 |
continue |
| 88 |
} |
| 89 |
if n > 0 { // see 'n == 0' comment above |
| 90 |
n-- |
| 91 |
} |
| 92 |
if mode == readdirName { |
| 93 |
names = append(names, string(name)) |
| 94 |
} else if mode == readdirDirEntry { |
| 95 |
var typ FileMode |
| 96 |
switch direntType(dirent) { |
| 97 |
case 'B': |
| 98 |
typ = ModeDevice |
| 99 |
case 'C': |
| 100 |
typ = ModeDevice | ModeCharDevice |
| 101 |
case 'D': |
| 102 |
typ = ModeDir |
| 103 |
case 'F': |
| 104 |
typ = ModeNamedPipe |
| 105 |
case 'L': |
| 106 |
typ = ModeSymlink |
| 107 |
case 'R': |
| 108 |
typ = 0 |
| 109 |
case 'S': |
| 110 |
typ = ModeSocket |
| 111 |
case 'U': |
| 112 |
typ = ^FileMode(0) |
| 113 |
} |
| 114 |
|
| 115 |
de, err := newUnixDirent(f.name, string(name), typ) |
| 116 |
if IsNotExist(err) { |
| 117 |
// File disappeared between readdir and stat. |
| 118 |
// Treat as if it didn't exist. |
| 119 |
continue |
| 120 |
} |
| 121 |
if err != nil { |
| 122 |
return nil, dirents, nil, err |
| 123 |
} |
| 124 |
dirents = append(dirents, de) |
| 125 |
} else { |
| 126 |
info, err := lstat(f.name + "/" + string(name)) |
| 127 |
if IsNotExist(err) { |
| 128 |
// File disappeared between readdir + stat. |
| 129 |
// Treat as if it didn't exist. |
| 130 |
continue |
| 131 |
} |
| 132 |
if err != nil { |
| 133 |
return nil, nil, infos, err |
| 134 |
} |
| 135 |
infos = append(infos, info) |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
if n > 0 && len(names)+len(dirents)+len(infos) == 0 { |
| 140 |
return nil, nil, nil, io.EOF |
| 141 |
} |
| 142 |
return names, dirents, infos, nil |
| 143 |
} |