mirror of
https://github.com/golang/go
synced 2024-11-14 19:00:27 -07:00
49325dc1d2
CL 102457 added TestDevNullFile. However, this test is failing on Plan 9, because it checks that /dev/null is a character device while there are no special files on Plan 9. We fix this issue by changing Stat to consider all files served by the console device (#c) as character devices. Fixes #24534. Change-Id: I1c60cdf25770358b908790b3fb71910fa914dec0 Reviewed-on: https://go-review.googlesource.com/102424 Run-TryBot: David du Colombier <0intro@gmail.com> Reviewed-by: Alex Brainman <alex.brainman@gmail.com> Reviewed-by: Rob Pike <r@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
111 lines
2.3 KiB
Go
111 lines
2.3 KiB
Go
// Copyright 2011 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"
|
|
"time"
|
|
)
|
|
|
|
const bitSize16 = 2
|
|
|
|
func fileInfoFromStat(d *syscall.Dir) FileInfo {
|
|
fs := &fileStat{
|
|
name: d.Name,
|
|
size: d.Length,
|
|
modTime: time.Unix(int64(d.Mtime), 0),
|
|
sys: d,
|
|
}
|
|
fs.mode = FileMode(d.Mode & 0777)
|
|
if d.Mode&syscall.DMDIR != 0 {
|
|
fs.mode |= ModeDir
|
|
}
|
|
if d.Mode&syscall.DMAPPEND != 0 {
|
|
fs.mode |= ModeAppend
|
|
}
|
|
if d.Mode&syscall.DMEXCL != 0 {
|
|
fs.mode |= ModeExclusive
|
|
}
|
|
if d.Mode&syscall.DMTMP != 0 {
|
|
fs.mode |= ModeTemporary
|
|
}
|
|
// Consider all files not served by #M as device files.
|
|
if d.Type != 'M' {
|
|
fs.mode |= ModeDevice
|
|
}
|
|
// Consider all files served by #c as character device files.
|
|
if d.Type == 'c' {
|
|
fs.mode |= ModeCharDevice
|
|
}
|
|
return fs
|
|
}
|
|
|
|
// arg is an open *File or a path string.
|
|
func dirstat(arg interface{}) (*syscall.Dir, error) {
|
|
var name string
|
|
var err error
|
|
|
|
size := syscall.STATFIXLEN + 16*4
|
|
|
|
for i := 0; i < 2; i++ {
|
|
buf := make([]byte, bitSize16+size)
|
|
|
|
var n int
|
|
switch a := arg.(type) {
|
|
case *File:
|
|
name = a.name
|
|
n, err = syscall.Fstat(a.fd, buf)
|
|
case string:
|
|
name = a
|
|
n, err = syscall.Stat(a, buf)
|
|
default:
|
|
panic("phase error in dirstat")
|
|
}
|
|
|
|
if n < bitSize16 {
|
|
return nil, &PathError{"stat", name, err}
|
|
}
|
|
|
|
// Pull the real size out of the stat message.
|
|
size = int(uint16(buf[0]) | uint16(buf[1])<<8)
|
|
|
|
// If the stat message is larger than our buffer we will
|
|
// go around the loop and allocate one that is big enough.
|
|
if size <= n {
|
|
d, err := syscall.UnmarshalDir(buf[:n])
|
|
if err != nil {
|
|
return nil, &PathError{"stat", name, err}
|
|
}
|
|
return d, nil
|
|
}
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
err = syscall.ErrBadStat
|
|
}
|
|
|
|
return nil, &PathError{"stat", name, err}
|
|
}
|
|
|
|
// statNolog implements Stat for Plan 9.
|
|
func statNolog(name string) (FileInfo, error) {
|
|
d, err := dirstat(name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return fileInfoFromStat(d), nil
|
|
}
|
|
|
|
// lstatNolog implements Lstat for Plan 9.
|
|
func lstatNolog(name string) (FileInfo, error) {
|
|
return statNolog(name)
|
|
}
|
|
|
|
// For testing.
|
|
func atime(fi FileInfo) time.Time {
|
|
return time.Unix(int64(fi.Sys().(*syscall.Dir).Atime), 0)
|
|
}
|