2011-04-07 19:09:10 +02:00
|
|
|
// 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 (
|
2011-12-03 03:17:34 +01:00
|
|
|
"errors"
|
|
|
|
"io"
|
2011-04-07 19:09:10 +02:00
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2012-03-02 21:01:37 +01:00
|
|
|
var errShortStat = errors.New("short stat message")
|
|
|
|
var errBadStat = errors.New("bad stat message format")
|
|
|
|
|
2012-01-25 22:54:22 +01:00
|
|
|
func (file *File) readdir(n int) (fi []FileInfo, err error) {
|
2011-04-07 19:09:10 +02:00
|
|
|
// If this file has no dirinfo, create one.
|
|
|
|
if file.dirinfo == nil {
|
2011-05-20 02:18:15 +02:00
|
|
|
file.dirinfo = new(dirInfo)
|
2011-04-07 19:09:10 +02:00
|
|
|
}
|
2011-05-20 02:18:15 +02:00
|
|
|
d := file.dirinfo
|
2011-09-16 17:47:21 +02:00
|
|
|
size := n
|
|
|
|
if size <= 0 {
|
2011-04-07 19:09:10 +02:00
|
|
|
size = 100
|
2011-09-16 17:47:21 +02:00
|
|
|
n = -1
|
2011-04-07 19:09:10 +02:00
|
|
|
}
|
2011-05-20 02:18:15 +02:00
|
|
|
result := make([]FileInfo, 0, size) // Empty with room to grow.
|
2011-09-16 17:47:21 +02:00
|
|
|
for n != 0 {
|
2011-05-20 02:18:15 +02:00
|
|
|
// Refill the buffer if necessary
|
|
|
|
if d.bufp >= d.nbuf {
|
|
|
|
d.bufp = 0
|
2011-12-03 03:17:34 +01:00
|
|
|
var e error
|
2011-05-20 02:18:15 +02:00
|
|
|
d.nbuf, e = file.Read(d.buf[:])
|
2011-12-03 03:17:34 +01:00
|
|
|
if e != nil && e != io.EOF {
|
2011-09-16 17:47:21 +02:00
|
|
|
return result, &PathError{"readdir", file.name, e}
|
2011-05-20 02:18:15 +02:00
|
|
|
}
|
2011-12-03 03:17:34 +01:00
|
|
|
if e == io.EOF {
|
2011-04-07 19:09:10 +02:00
|
|
|
break
|
|
|
|
}
|
2011-05-20 02:18:15 +02:00
|
|
|
if d.nbuf < syscall.STATFIXLEN {
|
2012-03-02 21:01:37 +01:00
|
|
|
return result, &PathError{"readdir", file.name, errShortStat}
|
2011-05-20 02:18:15 +02:00
|
|
|
}
|
2011-04-07 19:09:10 +02:00
|
|
|
}
|
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
// Get a record from buffer
|
|
|
|
m, _ := gbit16(d.buf[d.bufp:])
|
|
|
|
m += 2
|
|
|
|
if m < syscall.STATFIXLEN {
|
2012-03-02 21:01:37 +01:00
|
|
|
return result, &PathError{"readdir", file.name, errShortStat}
|
2011-04-07 19:09:10 +02:00
|
|
|
}
|
2012-10-23 06:31:11 +02:00
|
|
|
dir, e := unmarshalDir(d.buf[d.bufp : d.bufp+int(m)])
|
2011-05-20 02:18:15 +02:00
|
|
|
if e != nil {
|
2011-09-16 17:47:21 +02:00
|
|
|
return result, &PathError{"readdir", file.name, e}
|
2011-04-07 19:09:10 +02:00
|
|
|
}
|
2012-01-12 02:31:45 +01:00
|
|
|
result = append(result, fileInfoFromStat(dir))
|
2011-04-07 19:09:10 +02:00
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
d.bufp += int(m)
|
2011-09-16 17:47:21 +02:00
|
|
|
n--
|
2011-05-20 02:18:15 +02:00
|
|
|
}
|
2011-04-07 19:09:10 +02:00
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
if n >= 0 && len(result) == 0 {
|
2011-12-03 03:17:34 +01:00
|
|
|
return result, io.EOF
|
2011-04-07 19:09:10 +02:00
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
return result, nil
|
|
|
|
}
|
2011-04-07 19:09:10 +02:00
|
|
|
|
2012-01-25 22:54:22 +01:00
|
|
|
func (file *File) readdirnames(n int) (names []string, err error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
fi, err := file.Readdir(n)
|
2011-04-07 19:09:10 +02:00
|
|
|
names = make([]string, len(fi))
|
2011-05-20 02:18:15 +02:00
|
|
|
for i := range fi {
|
2012-01-12 02:31:45 +01:00
|
|
|
names[i] = fi[i].Name()
|
2011-04-07 19:09:10 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
type dir struct {
|
2011-04-07 19:09:10 +02:00
|
|
|
// system-modified data
|
|
|
|
Type uint16 // server type
|
|
|
|
Dev uint32 // server subtype
|
|
|
|
// file data
|
2012-10-23 06:31:11 +02:00
|
|
|
Qid qid // unique id from server
|
2011-04-07 19:09:10 +02:00
|
|
|
Mode uint32 // permissions
|
|
|
|
Atime uint32 // last read time
|
|
|
|
Mtime uint32 // last write time
|
|
|
|
Length uint64 // file length
|
|
|
|
Name string // last element of path
|
|
|
|
Uid string // owner name
|
|
|
|
Gid string // group name
|
|
|
|
Muid string // last modifier name
|
|
|
|
}
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
type qid struct {
|
2011-04-07 19:09:10 +02:00
|
|
|
Path uint64 // the file server's unique identification for the file
|
|
|
|
Vers uint32 // version number for given Path
|
|
|
|
Type uint8 // the type of the file (syscall.QTDIR for example)
|
|
|
|
}
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
var nullDir = dir{
|
2011-04-07 19:09:10 +02:00
|
|
|
^uint16(0),
|
|
|
|
^uint32(0),
|
2012-10-23 06:31:11 +02:00
|
|
|
qid{^uint64(0), ^uint32(0), ^uint8(0)},
|
2011-04-07 19:09:10 +02:00
|
|
|
^uint32(0),
|
|
|
|
^uint32(0),
|
|
|
|
^uint32(0),
|
|
|
|
^uint64(0),
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Null assigns members of d with special "don't care" values indicating
|
2012-11-21 08:03:38 +01:00
|
|
|
// they should not be written by syscall.Wstat.
|
2012-10-23 06:31:11 +02:00
|
|
|
func (d *dir) Null() {
|
2011-04-07 19:09:10 +02:00
|
|
|
*d = nullDir
|
|
|
|
}
|
|
|
|
|
|
|
|
// pdir appends a 9P Stat message based on the contents of Dir d to a byte slice b.
|
2012-10-23 06:31:11 +02:00
|
|
|
func pdir(b []byte, d *dir) []byte {
|
2011-04-07 19:09:10 +02:00
|
|
|
n := len(b)
|
2012-11-21 08:03:38 +01:00
|
|
|
b = pbit16(b, 0) // length, filled in later
|
2011-04-07 19:09:10 +02:00
|
|
|
b = pbit16(b, d.Type)
|
|
|
|
b = pbit32(b, d.Dev)
|
|
|
|
b = pqid(b, d.Qid)
|
|
|
|
b = pbit32(b, d.Mode)
|
|
|
|
b = pbit32(b, d.Atime)
|
|
|
|
b = pbit32(b, d.Mtime)
|
|
|
|
b = pbit64(b, d.Length)
|
|
|
|
b = pstring(b, d.Name)
|
|
|
|
b = pstring(b, d.Uid)
|
|
|
|
b = pstring(b, d.Gid)
|
|
|
|
b = pstring(b, d.Muid)
|
|
|
|
pbit16(b[0:n], uint16(len(b)-(n+2)))
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
// unmarshalDir reads a 9P Stat message from a 9P protocol message stored in b,
|
|
|
|
// returning the corresponding dir struct.
|
|
|
|
func unmarshalDir(b []byte) (d *dir, err error) {
|
2011-04-07 19:09:10 +02:00
|
|
|
n := uint16(0)
|
|
|
|
n, b = gbit16(b)
|
|
|
|
|
|
|
|
if int(n) != len(b) {
|
2012-03-02 21:01:37 +01:00
|
|
|
return nil, errBadStat
|
2011-04-07 19:09:10 +02:00
|
|
|
}
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
d = new(dir)
|
2011-04-07 19:09:10 +02:00
|
|
|
d.Type, b = gbit16(b)
|
|
|
|
d.Dev, b = gbit32(b)
|
|
|
|
d.Qid, b = gqid(b)
|
|
|
|
d.Mode, b = gbit32(b)
|
|
|
|
d.Atime, b = gbit32(b)
|
|
|
|
d.Mtime, b = gbit32(b)
|
|
|
|
d.Length, b = gbit64(b)
|
|
|
|
d.Name, b = gstring(b)
|
|
|
|
d.Uid, b = gstring(b)
|
|
|
|
d.Gid, b = gstring(b)
|
|
|
|
d.Muid, b = gstring(b)
|
|
|
|
|
|
|
|
if len(b) != 0 {
|
2012-03-02 21:01:37 +01:00
|
|
|
return nil, errBadStat
|
2011-04-07 19:09:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return d, nil
|
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// gqid reads the qid part of a 9P Stat message from a 9P protocol message stored in b,
|
2012-10-23 06:31:11 +02:00
|
|
|
// returning the corresponding qid struct and the remaining slice of b.
|
|
|
|
func gqid(b []byte) (qid, []byte) {
|
|
|
|
var q qid
|
2011-04-07 19:09:10 +02:00
|
|
|
q.Path, b = gbit64(b)
|
|
|
|
q.Vers, b = gbit32(b)
|
|
|
|
q.Type, b = gbit8(b)
|
|
|
|
return q, b
|
|
|
|
}
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
// pqid appends a qid struct q to a 9P message b.
|
|
|
|
func pqid(b []byte, q qid) []byte {
|
2011-04-07 19:09:10 +02:00
|
|
|
b = pbit64(b, q.Path)
|
|
|
|
b = pbit32(b, q.Vers)
|
|
|
|
b = pbit8(b, q.Type)
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// gbit8 reads a byte-sized numeric value from a 9P protocol message stored in b,
|
2011-04-07 19:09:10 +02:00
|
|
|
// returning the value and the remaining slice of b.
|
|
|
|
func gbit8(b []byte) (uint8, []byte) {
|
|
|
|
return uint8(b[0]), b[1:]
|
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// gbit16 reads a 16-bit numeric value from a 9P protocol message stored in b,
|
2011-04-07 19:09:10 +02:00
|
|
|
// returning the value and the remaining slice of b.
|
|
|
|
func gbit16(b []byte) (uint16, []byte) {
|
|
|
|
return uint16(b[0]) | uint16(b[1])<<8, b[2:]
|
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// gbit32 reads a 32-bit numeric value from a 9P protocol message stored in b,
|
2011-04-07 19:09:10 +02:00
|
|
|
// returning the value and the remaining slice of b.
|
|
|
|
func gbit32(b []byte) (uint32, []byte) {
|
|
|
|
return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24, b[4:]
|
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// gbit64 reads a 64-bit numeric value from a 9P protocol message stored in b,
|
2011-04-07 19:09:10 +02:00
|
|
|
// returning the value and the remaining slice of b.
|
|
|
|
func gbit64(b []byte) (uint64, []byte) {
|
|
|
|
lo, b := gbit32(b)
|
|
|
|
hi, b := gbit32(b)
|
|
|
|
return uint64(hi)<<32 | uint64(lo), b
|
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// gstring reads a string from a 9P protocol message stored in b,
|
2011-04-07 19:09:10 +02:00
|
|
|
// returning the value as a Go string and the remaining slice of b.
|
|
|
|
func gstring(b []byte) (string, []byte) {
|
|
|
|
n, b := gbit16(b)
|
|
|
|
return string(b[0:n]), b[n:]
|
|
|
|
}
|
|
|
|
|
|
|
|
// pbit8 appends a byte-sized numeric value x to a 9P message b.
|
|
|
|
func pbit8(b []byte, x uint8) []byte {
|
|
|
|
n := len(b)
|
|
|
|
if n+1 > cap(b) {
|
|
|
|
nb := make([]byte, n, 100+2*cap(b))
|
|
|
|
copy(nb, b)
|
|
|
|
b = nb
|
|
|
|
}
|
|
|
|
b = b[0 : n+1]
|
|
|
|
b[n] = x
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// pbit16 appends a 16-bit numeric value x to a 9P message b.
|
|
|
|
func pbit16(b []byte, x uint16) []byte {
|
|
|
|
n := len(b)
|
|
|
|
if n+2 > cap(b) {
|
|
|
|
nb := make([]byte, n, 100+2*cap(b))
|
|
|
|
copy(nb, b)
|
|
|
|
b = nb
|
|
|
|
}
|
|
|
|
b = b[0 : n+2]
|
|
|
|
b[n] = byte(x)
|
|
|
|
b[n+1] = byte(x >> 8)
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// pbit32 appends a 32-bit numeric value x to a 9P message b.
|
|
|
|
func pbit32(b []byte, x uint32) []byte {
|
|
|
|
n := len(b)
|
|
|
|
if n+4 > cap(b) {
|
|
|
|
nb := make([]byte, n, 100+2*cap(b))
|
|
|
|
copy(nb, b)
|
|
|
|
b = nb
|
|
|
|
}
|
|
|
|
b = b[0 : n+4]
|
|
|
|
b[n] = byte(x)
|
|
|
|
b[n+1] = byte(x >> 8)
|
|
|
|
b[n+2] = byte(x >> 16)
|
|
|
|
b[n+3] = byte(x >> 24)
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// pbit64 appends a 64-bit numeric value x to a 9P message b.
|
|
|
|
func pbit64(b []byte, x uint64) []byte {
|
|
|
|
b = pbit32(b, uint32(x))
|
|
|
|
b = pbit32(b, uint32(x>>32))
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// pstring appends a Go string s to a 9P message b.
|
|
|
|
func pstring(b []byte, s string) []byte {
|
|
|
|
if len(s) >= 1<<16 {
|
2011-12-03 03:17:34 +01:00
|
|
|
panic(errors.New("string too long"))
|
2011-04-07 19:09:10 +02:00
|
|
|
}
|
|
|
|
b = pbit16(b, uint16(len(s)))
|
2011-10-27 01:57:58 +02:00
|
|
|
b = append(b, s...)
|
2011-04-07 19:09:10 +02:00
|
|
|
return b
|
|
|
|
}
|