mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
os, syscall: refactor Unix directory parsing
Moved the details of how to read a directory and how to parse the results behind the new syscall functions ReadDirent and ParseDirent. Now os needs just one copy of Readdirnames for the three Unix variants, and it no longer imports "unsafe". R=r, r2 CC=golang-dev https://golang.org/cl/4368048
This commit is contained in:
parent
1e1e2f0971
commit
cf56f06ab6
7 changed files with 83 additions and 160 deletions
54
src/pkg/os/dir_unix.go
Normal file
54
src/pkg/os/dir_unix.go
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
// Copyright 2009 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package os
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
)
|
||||
|
||||
const (
|
||||
blockSize = 4096
|
||||
)
|
||||
|
||||
// Readdirnames reads the contents of the directory associated with file and
|
||||
// returns an array of up to count names, in directory order. Subsequent
|
||||
// calls on the same file will yield further names.
|
||||
// A negative count means to read until EOF.
|
||||
// Readdirnames returns the array and an Error, if any.
|
||||
func (file *File) Readdirnames(count int) (names []string, err Error) {
|
||||
// If this file has no dirinfo, create one.
|
||||
if file.dirinfo == nil {
|
||||
file.dirinfo = new(dirInfo)
|
||||
// The buffer must be at least a block long.
|
||||
file.dirinfo.buf = make([]byte, blockSize)
|
||||
}
|
||||
d := file.dirinfo
|
||||
size := count
|
||||
if size < 0 {
|
||||
size = 100
|
||||
}
|
||||
names = make([]string, 0, size) // Empty with room to grow.
|
||||
for count != 0 {
|
||||
// Refill the buffer if necessary
|
||||
if d.bufp >= d.nbuf {
|
||||
d.bufp = 0
|
||||
var errno int
|
||||
d.nbuf, errno = syscall.ReadDirent(file.fd, d.buf)
|
||||
if errno != 0 {
|
||||
return names, NewSyscallError("readdirent", errno)
|
||||
}
|
||||
if d.nbuf <= 0 {
|
||||
break // EOF
|
||||
}
|
||||
}
|
||||
|
||||
// Drain the buffer
|
||||
var nb, nc int
|
||||
nb, nc, names = syscall.ParseDirent(d.buf[d.bufp:d.nbuf], count, names)
|
||||
d.bufp += nb
|
||||
count -= nc
|
||||
}
|
||||
return names, nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue