2009-02-06 17:54:26 -08: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.
|
|
|
|
|
2009-08-12 13:18:37 -07:00
|
|
|
package os_test
|
2009-02-06 17:54:26 -08:00
|
|
|
|
|
|
|
import (
|
2024-01-27 12:52:56 +08:00
|
|
|
"bytes"
|
2013-12-17 12:19:01 -08:00
|
|
|
"errors"
|
2012-09-19 16:55:21 +10:00
|
|
|
"flag"
|
2009-12-15 15:40:16 -08:00
|
|
|
"fmt"
|
2015-06-05 11:01:53 -04:00
|
|
|
"internal/testenv"
|
2009-12-15 15:40:16 -08:00
|
|
|
"io"
|
2021-01-18 15:23:16 +01:00
|
|
|
"io/fs"
|
2024-01-08 17:20:14 -05:00
|
|
|
"log"
|
2009-12-15 15:40:16 -08:00
|
|
|
. "os"
|
2022-12-13 16:04:09 -05:00
|
|
|
"os/exec"
|
2011-03-24 11:20:28 +11:00
|
|
|
"path/filepath"
|
2012-01-14 06:40:55 +09:00
|
|
|
"runtime"
|
2017-02-10 15:17:38 -08:00
|
|
|
"runtime/debug"
|
2024-05-22 13:38:40 -07:00
|
|
|
"slices"
|
2009-12-15 15:40:16 -08:00
|
|
|
"strings"
|
2014-09-18 14:48:47 -04:00
|
|
|
"sync"
|
2012-02-17 10:04:29 +11:00
|
|
|
"syscall"
|
2009-12-15 15:40:16 -08:00
|
|
|
"testing"
|
2020-07-06 11:27:38 -04:00
|
|
|
"testing/fstest"
|
2011-11-30 12:01:46 -05:00
|
|
|
"time"
|
2009-02-06 17:54:26 -08:00
|
|
|
)
|
|
|
|
|
2022-02-16 10:24:42 -05:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
if Getenv("GO_OS_TEST_DRAIN_STDIN") == "1" {
|
2023-03-28 14:35:20 -07:00
|
|
|
Stdout.Close()
|
|
|
|
io.Copy(io.Discard, Stdin)
|
2022-02-16 10:24:42 -05:00
|
|
|
Exit(0)
|
|
|
|
}
|
|
|
|
|
2024-01-08 17:20:14 -05:00
|
|
|
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
|
|
|
|
2022-02-16 10:24:42 -05:00
|
|
|
Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
2009-03-03 08:39:12 -08:00
|
|
|
var dot = []string{
|
2011-04-06 15:44:40 -04:00
|
|
|
"dir_unix.go",
|
2011-11-14 14:06:50 -05:00
|
|
|
"env.go",
|
2009-02-15 14:18:39 -08:00
|
|
|
"error.go",
|
|
|
|
"file.go",
|
2009-02-09 11:24:35 -08:00
|
|
|
"os_test.go",
|
2009-02-15 14:18:39 -08:00
|
|
|
"types.go",
|
2009-11-01 09:37:13 -08:00
|
|
|
"stat_darwin.go",
|
|
|
|
"stat_linux.go",
|
2009-03-03 08:39:12 -08:00
|
|
|
}
|
2009-02-09 11:24:35 -08:00
|
|
|
|
2010-10-04 17:31:49 +11:00
|
|
|
type sysDir struct {
|
|
|
|
name string
|
|
|
|
files []string
|
|
|
|
}
|
|
|
|
|
2015-02-27 19:52:11 -05:00
|
|
|
var sysdir = func() *sysDir {
|
2012-01-14 06:40:55 +09:00
|
|
|
switch runtime.GOOS {
|
2014-07-08 13:44:06 -04:00
|
|
|
case "android":
|
2015-02-27 19:52:11 -05:00
|
|
|
return &sysDir{
|
2016-02-24 11:58:57 -08:00
|
|
|
"/system/lib",
|
2014-07-08 13:44:06 -04:00
|
|
|
[]string{
|
2016-02-24 11:58:57 -08:00
|
|
|
"libmedia.so",
|
|
|
|
"libpowermanager.so",
|
2014-07-08 13:44:06 -04:00
|
|
|
},
|
|
|
|
}
|
2021-12-30 14:06:30 +01:00
|
|
|
case "ios":
|
|
|
|
wd, err := syscall.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
wd = err.Error()
|
|
|
|
}
|
|
|
|
sd := &sysDir{
|
|
|
|
filepath.Join(wd, "..", ".."),
|
|
|
|
[]string{
|
|
|
|
"ResourceRules.plist",
|
|
|
|
"Info.plist",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
found := true
|
|
|
|
for _, f := range sd.files {
|
|
|
|
path := filepath.Join(sd.name, f)
|
|
|
|
if _, err := Stat(path); err != nil {
|
|
|
|
found = false
|
|
|
|
break
|
2019-04-30 22:15:46 +02:00
|
|
|
}
|
2015-02-27 19:52:11 -05:00
|
|
|
}
|
2021-12-30 14:06:30 +01:00
|
|
|
if found {
|
|
|
|
return sd
|
|
|
|
}
|
|
|
|
// In a self-hosted iOS build the above files might
|
|
|
|
// not exist. Look for system files instead below.
|
2010-10-04 17:31:49 +11:00
|
|
|
case "windows":
|
2015-02-27 19:52:11 -05:00
|
|
|
return &sysDir{
|
2010-10-04 17:31:49 +11:00
|
|
|
Getenv("SystemRoot") + "\\system32\\drivers\\etc",
|
|
|
|
[]string{
|
|
|
|
"networks",
|
|
|
|
"protocol",
|
|
|
|
"services",
|
|
|
|
},
|
|
|
|
}
|
2011-04-12 16:58:56 -07:00
|
|
|
case "plan9":
|
2015-02-27 19:52:11 -05:00
|
|
|
return &sysDir{
|
2011-04-12 16:58:56 -07:00
|
|
|
"/lib/ndb",
|
|
|
|
[]string{
|
|
|
|
"common",
|
|
|
|
"local",
|
|
|
|
},
|
|
|
|
}
|
2023-03-25 10:18:26 -07:00
|
|
|
case "wasip1":
|
|
|
|
// wasmtime has issues resolving symbolic links that are often present
|
|
|
|
// in directories like /etc/group below (e.g. private/etc/group on OSX).
|
|
|
|
// For this reason we use files in the Go source tree instead.
|
|
|
|
return &sysDir{
|
|
|
|
runtime.GOROOT(),
|
|
|
|
[]string{
|
|
|
|
"go.env",
|
|
|
|
"LICENSE",
|
|
|
|
"CONTRIBUTING.md",
|
|
|
|
},
|
|
|
|
}
|
2010-10-04 17:31:49 +11:00
|
|
|
}
|
2015-02-27 19:52:11 -05:00
|
|
|
return &sysDir{
|
|
|
|
"/etc",
|
|
|
|
[]string{
|
|
|
|
"group",
|
|
|
|
"hosts",
|
|
|
|
"passwd",
|
|
|
|
},
|
|
|
|
}
|
2010-10-04 17:31:49 +11:00
|
|
|
}()
|
2009-02-09 11:24:35 -08:00
|
|
|
|
2010-04-22 14:01:33 -07:00
|
|
|
func size(name string, t *testing.T) int64 {
|
2011-04-04 23:42:14 -07:00
|
|
|
file, err := Open(name)
|
2009-02-06 17:54:26 -08:00
|
|
|
if err != nil {
|
2009-11-09 12:07:39 -08:00
|
|
|
t.Fatal("open failed:", err)
|
2009-02-06 17:54:26 -08:00
|
|
|
}
|
2021-10-12 16:00:23 -04:00
|
|
|
defer func() {
|
|
|
|
if err := file.Close(); err != nil {
|
|
|
|
t.Error(err)
|
2009-06-22 13:26:13 -07:00
|
|
|
}
|
2021-10-12 16:00:23 -04:00
|
|
|
}()
|
|
|
|
n, err := io.Copy(io.Discard, file)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2009-02-06 17:54:26 -08:00
|
|
|
}
|
2021-10-12 16:00:23 -04:00
|
|
|
return n
|
2009-02-06 17:54:26 -08:00
|
|
|
}
|
|
|
|
|
2010-10-04 17:31:49 +11:00
|
|
|
func equal(name1, name2 string) (r bool) {
|
2012-01-14 06:40:55 +09:00
|
|
|
switch runtime.GOOS {
|
2010-10-04 17:31:49 +11:00
|
|
|
case "windows":
|
2023-10-08 10:51:45 +08:00
|
|
|
r = strings.EqualFold(name1, name2)
|
2010-10-04 17:31:49 +11:00
|
|
|
default:
|
|
|
|
r = name1 == name2
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-11 17:25:06 -07:00
|
|
|
func newFile(t *testing.T) (f *File) {
|
|
|
|
t.Helper()
|
|
|
|
f, err := CreateTemp("", "_Go_"+t.Name())
|
2014-10-06 19:22:48 -04:00
|
|
|
if err != nil {
|
2024-06-11 17:25:06 -07:00
|
|
|
t.Fatal(err)
|
2010-07-13 10:31:51 +10:00
|
|
|
}
|
2024-06-11 17:25:06 -07:00
|
|
|
t.Cleanup(func() {
|
|
|
|
if err := f.Close(); err != nil && !errors.Is(err, ErrClosed) {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := Remove(f.Name()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
2010-07-13 10:31:51 +10:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2010-10-04 17:31:49 +11:00
|
|
|
var sfdir = sysdir.name
|
|
|
|
var sfname = sysdir.files[0]
|
|
|
|
|
2009-02-06 17:54:26 -08:00
|
|
|
func TestStat(t *testing.T) {
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
2010-10-04 17:31:49 +11:00
|
|
|
path := sfdir + "/" + sfname
|
|
|
|
dir, err := Stat(path)
|
2009-02-06 17:54:26 -08:00
|
|
|
if err != nil {
|
2009-11-09 12:07:39 -08:00
|
|
|
t.Fatal("stat failed:", err)
|
2009-02-06 17:54:26 -08:00
|
|
|
}
|
2011-11-30 12:04:16 -05:00
|
|
|
if !equal(sfname, dir.Name()) {
|
|
|
|
t.Error("name should be ", sfname, "; is", dir.Name())
|
2009-02-06 17:54:26 -08:00
|
|
|
}
|
2010-10-04 17:31:49 +11:00
|
|
|
filesize := size(path, t)
|
2011-11-30 12:04:16 -05:00
|
|
|
if dir.Size() != filesize {
|
|
|
|
t.Error("size should be", filesize, "; is", dir.Size())
|
2009-02-06 17:54:26 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-11 09:54:08 +09:00
|
|
|
func TestStatError(t *testing.T) {
|
2023-09-07 17:21:16 -07:00
|
|
|
t.Chdir(t.TempDir())
|
2017-04-11 09:54:08 +09:00
|
|
|
|
|
|
|
path := "no-such-file"
|
|
|
|
|
|
|
|
fi, err := Stat(path)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("got nil, want error")
|
|
|
|
}
|
|
|
|
if fi != nil {
|
|
|
|
t.Errorf("got %v, want nil", fi)
|
|
|
|
}
|
|
|
|
if perr, ok := err.(*PathError); !ok {
|
|
|
|
t.Errorf("got %T, want %T", err, perr)
|
|
|
|
}
|
|
|
|
|
|
|
|
testenv.MustHaveSymlink(t)
|
|
|
|
|
|
|
|
link := "symlink"
|
|
|
|
err = Symlink(path, link)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fi, err = Stat(link)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("got nil, want error")
|
|
|
|
}
|
|
|
|
if fi != nil {
|
|
|
|
t.Errorf("got %v, want nil", fi)
|
|
|
|
}
|
|
|
|
if perr, ok := err.(*PathError); !ok {
|
|
|
|
t.Errorf("got %T, want %T", err, perr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-01 12:41:35 +02:00
|
|
|
func TestStatSymlinkLoop(t *testing.T) {
|
|
|
|
testenv.MustHaveSymlink(t)
|
2023-09-07 17:21:16 -07:00
|
|
|
t.Chdir(t.TempDir())
|
2022-04-01 12:41:35 +02:00
|
|
|
|
2023-03-28 14:35:20 -07:00
|
|
|
err := Symlink("x", "y")
|
2022-04-01 12:41:35 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2023-03-28 14:35:20 -07:00
|
|
|
defer Remove("y")
|
2022-04-01 12:41:35 +02:00
|
|
|
|
2023-03-28 14:35:20 -07:00
|
|
|
err = Symlink("y", "x")
|
2022-04-01 12:41:35 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2023-03-28 14:35:20 -07:00
|
|
|
defer Remove("x")
|
2022-04-01 12:41:35 +02:00
|
|
|
|
2023-03-28 14:35:20 -07:00
|
|
|
_, err = Stat("x")
|
2022-04-01 12:41:35 +02:00
|
|
|
if _, ok := err.(*fs.PathError); !ok {
|
|
|
|
t.Errorf("expected *PathError, got %T: %v\n", err, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-06 17:54:26 -08:00
|
|
|
func TestFstat(t *testing.T) {
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
2010-10-04 17:31:49 +11:00
|
|
|
path := sfdir + "/" + sfname
|
2011-04-04 23:42:14 -07:00
|
|
|
file, err1 := Open(path)
|
2009-02-06 17:54:26 -08:00
|
|
|
if err1 != nil {
|
2009-11-09 12:07:39 -08:00
|
|
|
t.Fatal("open failed:", err1)
|
2009-02-06 17:54:26 -08:00
|
|
|
}
|
2012-08-17 11:55:11 -07:00
|
|
|
defer file.Close()
|
2009-12-15 15:40:16 -08:00
|
|
|
dir, err2 := file.Stat()
|
2009-02-06 17:54:26 -08:00
|
|
|
if err2 != nil {
|
2009-11-09 12:07:39 -08:00
|
|
|
t.Fatal("fstat failed:", err2)
|
2009-02-06 17:54:26 -08:00
|
|
|
}
|
2011-11-30 12:04:16 -05:00
|
|
|
if !equal(sfname, dir.Name()) {
|
|
|
|
t.Error("name should be ", sfname, "; is", dir.Name())
|
2009-02-06 17:54:26 -08:00
|
|
|
}
|
2010-10-04 17:31:49 +11:00
|
|
|
filesize := size(path, t)
|
2011-11-30 12:04:16 -05:00
|
|
|
if dir.Size() != filesize {
|
|
|
|
t.Error("size should be", filesize, "; is", dir.Size())
|
2009-02-06 17:54:26 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLstat(t *testing.T) {
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
2010-10-04 17:31:49 +11:00
|
|
|
path := sfdir + "/" + sfname
|
|
|
|
dir, err := Lstat(path)
|
2009-02-06 17:54:26 -08:00
|
|
|
if err != nil {
|
2009-11-09 12:07:39 -08:00
|
|
|
t.Fatal("lstat failed:", err)
|
2009-02-06 17:54:26 -08:00
|
|
|
}
|
2011-11-30 12:04:16 -05:00
|
|
|
if !equal(sfname, dir.Name()) {
|
|
|
|
t.Error("name should be ", sfname, "; is", dir.Name())
|
2009-02-06 17:54:26 -08:00
|
|
|
}
|
2022-12-09 11:22:32 -08:00
|
|
|
if dir.Mode()&ModeSymlink == 0 {
|
|
|
|
filesize := size(path, t)
|
|
|
|
if dir.Size() != filesize {
|
|
|
|
t.Error("size should be", filesize, "; is", dir.Size())
|
|
|
|
}
|
2009-02-06 17:54:26 -08:00
|
|
|
}
|
|
|
|
}
|
2009-02-08 10:18:50 -08:00
|
|
|
|
2011-11-01 00:17:05 -04:00
|
|
|
// Read with length 0 should not return EOF.
|
|
|
|
func TestRead0(t *testing.T) {
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
2011-11-01 00:17:05 -04:00
|
|
|
path := sfdir + "/" + sfname
|
|
|
|
f, err := Open(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("open failed:", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
b := make([]byte, 0)
|
|
|
|
n, err := f.Read(b)
|
|
|
|
if n != 0 || err != nil {
|
|
|
|
t.Errorf("Read(0) = %d, %v, want 0, nil", n, err)
|
|
|
|
}
|
|
|
|
b = make([]byte, 100)
|
|
|
|
n, err = f.Read(b)
|
|
|
|
if n <= 0 || err != nil {
|
|
|
|
t.Errorf("Read(100) = %d, %v, want >0, nil", n, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-06 06:10:25 +00:00
|
|
|
// Reading a closed file should return ErrClosed error
|
2016-10-07 00:46:56 -04:00
|
|
|
func TestReadClosed(t *testing.T) {
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-10-07 00:46:56 -04:00
|
|
|
path := sfdir + "/" + sfname
|
|
|
|
file, err := Open(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("open failed:", err)
|
|
|
|
}
|
|
|
|
file.Close() // close immediately
|
|
|
|
|
|
|
|
b := make([]byte, 100)
|
|
|
|
_, err = file.Read(b)
|
|
|
|
|
|
|
|
e, ok := err.(*PathError)
|
2022-09-06 17:48:26 -07:00
|
|
|
if !ok || e.Err != ErrClosed {
|
|
|
|
t.Fatalf("Read: got %T(%v), want %T(%v)", err, err, e, ErrClosed)
|
2016-10-07 00:46:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-13 16:04:09 -05:00
|
|
|
func testReaddirnames(dir string, contents []string) func(*testing.T) {
|
|
|
|
return func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
file, err := Open(dir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("open %q failed: %v", dir, err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
s, err2 := file.Readdirnames(-1)
|
|
|
|
if err2 != nil {
|
|
|
|
t.Fatalf("Readdirnames %q failed: %v", dir, err2)
|
|
|
|
}
|
|
|
|
for _, m := range contents {
|
|
|
|
found := false
|
|
|
|
for _, n := range s {
|
|
|
|
if n == "." || n == ".." {
|
|
|
|
t.Errorf("got %q in directory", n)
|
|
|
|
}
|
|
|
|
if !equal(m, n) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if found {
|
|
|
|
t.Error("present twice:", m)
|
|
|
|
}
|
|
|
|
found = true
|
2020-10-09 11:49:59 -04:00
|
|
|
}
|
2022-12-13 16:04:09 -05:00
|
|
|
if !found {
|
|
|
|
t.Error("could not find", m)
|
2009-02-08 10:18:50 -08:00
|
|
|
}
|
|
|
|
}
|
2022-12-13 16:04:09 -05:00
|
|
|
if s == nil {
|
|
|
|
t.Error("Readdirnames returned nil instead of empty slice")
|
2009-02-08 10:18:50 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-02-09 11:24:35 -08:00
|
|
|
|
2022-12-13 16:04:09 -05:00
|
|
|
func testReaddir(dir string, contents []string) func(*testing.T) {
|
|
|
|
return func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
file, err := Open(dir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("open %q failed: %v", dir, err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
s, err2 := file.Readdir(-1)
|
|
|
|
if err2 != nil {
|
|
|
|
t.Fatalf("Readdir %q failed: %v", dir, err2)
|
|
|
|
}
|
|
|
|
for _, m := range contents {
|
|
|
|
found := false
|
|
|
|
for _, n := range s {
|
|
|
|
if n.Name() == "." || n.Name() == ".." {
|
|
|
|
t.Errorf("got %q in directory", n.Name())
|
|
|
|
}
|
|
|
|
if !equal(m, n.Name()) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if found {
|
|
|
|
t.Error("present twice:", m)
|
|
|
|
}
|
|
|
|
found = true
|
2020-10-09 11:49:59 -04:00
|
|
|
}
|
2022-12-13 16:04:09 -05:00
|
|
|
if !found {
|
|
|
|
t.Error("could not find", m)
|
2020-10-09 11:49:59 -04:00
|
|
|
}
|
|
|
|
}
|
2022-12-13 16:04:09 -05:00
|
|
|
if s == nil {
|
|
|
|
t.Error("Readdir returned nil instead of empty slice")
|
2020-10-09 11:49:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-13 16:04:09 -05:00
|
|
|
func testReadDir(dir string, contents []string) func(*testing.T) {
|
|
|
|
return func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
file, err := Open(dir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("open %q failed: %v", dir, err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
s, err2 := file.ReadDir(-1)
|
|
|
|
if err2 != nil {
|
|
|
|
t.Fatalf("ReadDir %q failed: %v", dir, err2)
|
|
|
|
}
|
|
|
|
for _, m := range contents {
|
|
|
|
found := false
|
|
|
|
for _, n := range s {
|
|
|
|
if n.Name() == "." || n.Name() == ".." {
|
|
|
|
t.Errorf("got %q in directory", n)
|
|
|
|
}
|
|
|
|
if !equal(m, n.Name()) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if found {
|
|
|
|
t.Error("present twice:", m)
|
|
|
|
}
|
|
|
|
found = true
|
|
|
|
lstat, err := Lstat(dir + "/" + m)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if n.IsDir() != lstat.IsDir() {
|
|
|
|
t.Errorf("%s: IsDir=%v, want %v", m, n.IsDir(), lstat.IsDir())
|
|
|
|
}
|
|
|
|
if n.Type() != lstat.Mode().Type() {
|
|
|
|
t.Errorf("%s: IsDir=%v, want %v", m, n.Type(), lstat.Mode().Type())
|
|
|
|
}
|
|
|
|
info, err := n.Info()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s: Info: %v", m, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !SameFile(info, lstat) {
|
|
|
|
t.Errorf("%s: Info: SameFile(info, lstat) = false", m)
|
|
|
|
}
|
2020-10-09 11:49:59 -04:00
|
|
|
}
|
2022-12-13 16:04:09 -05:00
|
|
|
if !found {
|
|
|
|
t.Error("could not find", m)
|
2009-02-09 11:24:35 -08:00
|
|
|
}
|
|
|
|
}
|
2022-12-13 16:04:09 -05:00
|
|
|
if s == nil {
|
|
|
|
t.Error("ReadDir returned nil instead of empty slice")
|
2009-02-09 11:24:35 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-29 13:51:20 -04:00
|
|
|
func TestFileReaddirnames(t *testing.T) {
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
t.Run(".", testReaddirnames(".", dot))
|
|
|
|
t.Run("sysdir", testReaddirnames(sysdir.name, sysdir.files))
|
|
|
|
t.Run("TempDir", testReaddirnames(t.TempDir(), nil))
|
2009-02-09 11:24:35 -08:00
|
|
|
}
|
|
|
|
|
2020-10-29 13:51:20 -04:00
|
|
|
func TestFileReaddir(t *testing.T) {
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
t.Run(".", testReaddir(".", dot))
|
|
|
|
t.Run("sysdir", testReaddir(sysdir.name, sysdir.files))
|
|
|
|
t.Run("TempDir", testReaddir(t.TempDir(), nil))
|
2009-02-09 11:24:35 -08:00
|
|
|
}
|
2009-02-10 11:27:45 -08:00
|
|
|
|
2020-10-29 13:51:20 -04:00
|
|
|
func TestFileReadDir(t *testing.T) {
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
t.Run(".", testReadDir(".", dot))
|
|
|
|
t.Run("sysdir", testReadDir(sysdir.name, sysdir.files))
|
|
|
|
t.Run("TempDir", testReadDir(t.TempDir(), nil))
|
2020-10-09 11:49:59 -04:00
|
|
|
}
|
|
|
|
|
2015-10-28 12:58:58 +02:00
|
|
|
func benchmarkReaddirname(path string, b *testing.B) {
|
|
|
|
var nentries int
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
f, err := Open(path)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("open %q failed: %v", path, err)
|
|
|
|
}
|
|
|
|
ns, err := f.Readdirnames(-1)
|
|
|
|
f.Close()
|
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("readdirnames %q failed: %v", path, err)
|
|
|
|
}
|
|
|
|
nentries = len(ns)
|
|
|
|
}
|
|
|
|
b.Logf("benchmarkReaddirname %q: %d entries", path, nentries)
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkReaddir(path string, b *testing.B) {
|
|
|
|
var nentries int
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
f, err := Open(path)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("open %q failed: %v", path, err)
|
|
|
|
}
|
|
|
|
fs, err := f.Readdir(-1)
|
|
|
|
f.Close()
|
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("readdir %q failed: %v", path, err)
|
|
|
|
}
|
|
|
|
nentries = len(fs)
|
|
|
|
}
|
|
|
|
b.Logf("benchmarkReaddir %q: %d entries", path, nentries)
|
|
|
|
}
|
|
|
|
|
2020-10-09 11:49:59 -04:00
|
|
|
func benchmarkReadDir(path string, b *testing.B) {
|
|
|
|
var nentries int
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
f, err := Open(path)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("open %q failed: %v", path, err)
|
|
|
|
}
|
|
|
|
fs, err := f.ReadDir(-1)
|
|
|
|
f.Close()
|
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("readdir %q failed: %v", path, err)
|
|
|
|
}
|
|
|
|
nentries = len(fs)
|
|
|
|
}
|
|
|
|
b.Logf("benchmarkReadDir %q: %d entries", path, nentries)
|
|
|
|
}
|
|
|
|
|
2015-10-28 12:58:58 +02:00
|
|
|
func BenchmarkReaddirname(b *testing.B) {
|
|
|
|
benchmarkReaddirname(".", b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkReaddir(b *testing.B) {
|
|
|
|
benchmarkReaddir(".", b)
|
|
|
|
}
|
|
|
|
|
2020-10-09 11:49:59 -04:00
|
|
|
func BenchmarkReadDir(b *testing.B) {
|
|
|
|
benchmarkReadDir(".", b)
|
|
|
|
}
|
|
|
|
|
2017-05-09 16:50:41 +10:00
|
|
|
func benchmarkStat(b *testing.B, path string) {
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
_, err := Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("Stat(%q) failed: %v", path, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkLstat(b *testing.B, path string) {
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
_, err := Lstat(path)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("Lstat(%q) failed: %v", path, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkStatDot(b *testing.B) {
|
|
|
|
benchmarkStat(b, ".")
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkStatFile(b *testing.B) {
|
|
|
|
benchmarkStat(b, filepath.Join(runtime.GOROOT(), "src/os/os_test.go"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkStatDir(b *testing.B) {
|
|
|
|
benchmarkStat(b, filepath.Join(runtime.GOROOT(), "src/os"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkLstatDot(b *testing.B) {
|
|
|
|
benchmarkLstat(b, ".")
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkLstatFile(b *testing.B) {
|
|
|
|
benchmarkLstat(b, filepath.Join(runtime.GOROOT(), "src/os/os_test.go"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkLstatDir(b *testing.B) {
|
|
|
|
benchmarkLstat(b, filepath.Join(runtime.GOROOT(), "src/os"))
|
|
|
|
}
|
|
|
|
|
2009-02-10 11:27:45 -08:00
|
|
|
// Read the directory one entry at a time.
|
2009-03-11 12:51:10 -07:00
|
|
|
func smallReaddirnames(file *File, length int, t *testing.T) []string {
|
2009-12-15 15:40:16 -08:00
|
|
|
names := make([]string, length)
|
|
|
|
count := 0
|
2009-02-10 11:27:45 -08:00
|
|
|
for {
|
2009-12-15 15:40:16 -08:00
|
|
|
d, err := file.Readdirnames(1)
|
2011-11-01 21:49:08 -04:00
|
|
|
if err == io.EOF {
|
2011-05-16 09:26:16 -07:00
|
|
|
break
|
|
|
|
}
|
2009-02-10 11:27:45 -08:00
|
|
|
if err != nil {
|
2011-05-16 09:26:16 -07:00
|
|
|
t.Fatalf("readdirnames %q failed: %v", file.Name(), err)
|
2009-02-10 11:27:45 -08:00
|
|
|
}
|
|
|
|
if len(d) == 0 {
|
2011-05-16 21:15:06 -07:00
|
|
|
t.Fatalf("readdirnames %q returned empty slice and no error", file.Name())
|
2009-02-10 11:27:45 -08:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
names[count] = d[0]
|
|
|
|
count++
|
2009-02-10 11:27:45 -08:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
return names[0:count]
|
2009-02-10 11:27:45 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that reading a directory one entry at a time gives the same result
|
|
|
|
// as reading it all at once.
|
|
|
|
func TestReaddirnamesOneAtATime(t *testing.T) {
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
2010-10-04 17:31:49 +11:00
|
|
|
// big directory that doesn't change often.
|
|
|
|
dir := "/usr/bin"
|
2012-01-14 06:40:55 +09:00
|
|
|
switch runtime.GOOS {
|
2014-07-08 13:44:06 -04:00
|
|
|
case "android":
|
|
|
|
dir = "/system/bin"
|
2023-04-27 13:37:48 +09:00
|
|
|
case "ios", "wasip1":
|
2021-12-30 14:06:30 +01:00
|
|
|
wd, err := Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2015-02-27 19:52:11 -05:00
|
|
|
}
|
2021-12-30 14:06:30 +01:00
|
|
|
dir = wd
|
2011-04-12 16:58:56 -07:00
|
|
|
case "plan9":
|
|
|
|
dir = "/bin"
|
2014-07-08 13:44:06 -04:00
|
|
|
case "windows":
|
|
|
|
dir = Getenv("SystemRoot") + "\\system32"
|
2010-10-04 17:31:49 +11:00
|
|
|
}
|
2011-04-04 23:42:14 -07:00
|
|
|
file, err := Open(dir)
|
2009-02-10 11:27:45 -08:00
|
|
|
if err != nil {
|
2009-11-09 12:07:39 -08:00
|
|
|
t.Fatalf("open %q failed: %v", dir, err)
|
2009-02-10 11:27:45 -08:00
|
|
|
}
|
2012-08-17 11:55:11 -07:00
|
|
|
defer file.Close()
|
2009-12-15 15:40:16 -08:00
|
|
|
all, err1 := file.Readdirnames(-1)
|
2009-02-10 11:27:45 -08:00
|
|
|
if err1 != nil {
|
2009-11-09 12:07:39 -08:00
|
|
|
t.Fatalf("readdirnames %q failed: %v", dir, err1)
|
2009-02-10 11:27:45 -08:00
|
|
|
}
|
2011-04-04 23:42:14 -07:00
|
|
|
file1, err2 := Open(dir)
|
2009-02-10 11:27:45 -08:00
|
|
|
if err2 != nil {
|
2009-11-09 12:07:39 -08:00
|
|
|
t.Fatalf("open %q failed: %v", dir, err2)
|
2009-02-10 11:27:45 -08:00
|
|
|
}
|
2013-06-21 11:13:14 +10:00
|
|
|
defer file1.Close()
|
2009-12-15 15:40:16 -08:00
|
|
|
small := smallReaddirnames(file1, len(all)+100, t) // +100 in case we screw up
|
2011-04-12 16:58:56 -07:00
|
|
|
if len(small) < len(all) {
|
|
|
|
t.Fatalf("len(small) is %d, less than %d", len(small), len(all))
|
|
|
|
}
|
2009-02-10 11:27:45 -08:00
|
|
|
for i, n := range all {
|
|
|
|
if small[i] != n {
|
2010-12-07 16:42:54 -05:00
|
|
|
t.Errorf("small read %q mismatch: %v", small[i], n)
|
2009-02-10 11:27:45 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-02-15 14:18:39 -08:00
|
|
|
|
2011-05-27 12:14:48 -07:00
|
|
|
func TestReaddirNValues(t *testing.T) {
|
|
|
|
if testing.Short() {
|
2013-01-24 17:32:10 +11:00
|
|
|
t.Skip("test.short; skipping")
|
2011-05-27 12:14:48 -07:00
|
|
|
}
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
2021-04-07 21:27:27 +02:00
|
|
|
dir := t.TempDir()
|
2011-05-27 12:58:59 -07:00
|
|
|
for i := 1; i <= 105; i++ {
|
2011-05-27 12:14:48 -07:00
|
|
|
f, err := Create(filepath.Join(dir, fmt.Sprintf("%d", i)))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Create: %v", err)
|
|
|
|
}
|
|
|
|
f.Write([]byte(strings.Repeat("X", i)))
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
var d *File
|
|
|
|
openDir := func() {
|
2011-11-01 21:49:08 -04:00
|
|
|
var err error
|
2011-05-27 12:14:48 -07:00
|
|
|
d, err = Open(dir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Open directory: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-09 11:49:59 -04:00
|
|
|
readdirExpect := func(n, want int, wantErr error) {
|
|
|
|
t.Helper()
|
2011-05-27 12:14:48 -07:00
|
|
|
fi, err := d.Readdir(n)
|
|
|
|
if err != wantErr {
|
|
|
|
t.Fatalf("Readdir of %d got error %v, want %v", n, err, wantErr)
|
|
|
|
}
|
|
|
|
if g, e := len(fi), want; g != e {
|
|
|
|
t.Errorf("Readdir of %d got %d files, want %d", n, g, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-09 11:49:59 -04:00
|
|
|
readDirExpect := func(n, want int, wantErr error) {
|
|
|
|
t.Helper()
|
|
|
|
de, err := d.ReadDir(n)
|
|
|
|
if err != wantErr {
|
|
|
|
t.Fatalf("ReadDir of %d got error %v, want %v", n, err, wantErr)
|
|
|
|
}
|
|
|
|
if g, e := len(de), want; g != e {
|
|
|
|
t.Errorf("ReadDir of %d got %d files, want %d", n, g, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
readdirnamesExpect := func(n, want int, wantErr error) {
|
|
|
|
t.Helper()
|
2011-05-27 12:14:48 -07:00
|
|
|
fi, err := d.Readdirnames(n)
|
|
|
|
if err != wantErr {
|
|
|
|
t.Fatalf("Readdirnames of %d got error %v, want %v", n, err, wantErr)
|
|
|
|
}
|
|
|
|
if g, e := len(fi), want; g != e {
|
|
|
|
t.Errorf("Readdirnames of %d got %d files, want %d", n, g, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-09 11:49:59 -04:00
|
|
|
for _, fn := range []func(int, int, error){readdirExpect, readdirnamesExpect, readDirExpect} {
|
2011-05-27 12:58:59 -07:00
|
|
|
// Test the slurp case
|
2011-05-27 12:14:48 -07:00
|
|
|
openDir()
|
2011-05-27 12:58:59 -07:00
|
|
|
fn(0, 105, nil)
|
|
|
|
fn(0, 0, nil)
|
|
|
|
d.Close()
|
|
|
|
|
|
|
|
// Slurp with -1 instead
|
|
|
|
openDir()
|
|
|
|
fn(-1, 105, nil)
|
2011-05-27 12:14:48 -07:00
|
|
|
fn(-2, 0, nil)
|
|
|
|
fn(0, 0, nil)
|
|
|
|
d.Close()
|
|
|
|
|
|
|
|
// Test the bounded case
|
|
|
|
openDir()
|
2011-05-27 12:58:59 -07:00
|
|
|
fn(1, 1, nil)
|
|
|
|
fn(2, 2, nil)
|
|
|
|
fn(105, 102, nil) // and tests buffer >100 case
|
2011-11-01 21:49:08 -04:00
|
|
|
fn(3, 0, io.EOF)
|
2011-05-27 12:14:48 -07:00
|
|
|
d.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-17 12:19:01 -08:00
|
|
|
func touch(t *testing.T, name string) {
|
|
|
|
f, err := Create(name)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReaddirStatFailures(t *testing.T) {
|
2014-01-13 13:24:59 +01:00
|
|
|
switch runtime.GOOS {
|
|
|
|
case "windows", "plan9":
|
|
|
|
// Windows and Plan 9 already do this correctly,
|
|
|
|
// but are structured with different syscalls such
|
|
|
|
// that they don't use Lstat, so the hook below for
|
|
|
|
// testing it wouldn't work.
|
2013-12-17 12:19:01 -08:00
|
|
|
t.Skipf("skipping test on %v", runtime.GOOS)
|
|
|
|
}
|
2022-12-13 16:04:09 -05:00
|
|
|
|
2013-12-17 12:19:01 -08:00
|
|
|
var xerr error // error to return for x
|
|
|
|
*LstatP = func(path string) (FileInfo, error) {
|
|
|
|
if xerr != nil && strings.HasSuffix(path, "x") {
|
|
|
|
return nil, xerr
|
|
|
|
}
|
|
|
|
return Lstat(path)
|
|
|
|
}
|
2022-12-13 16:04:09 -05:00
|
|
|
defer func() { *LstatP = Lstat }()
|
|
|
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
touch(t, filepath.Join(dir, "good1"))
|
|
|
|
touch(t, filepath.Join(dir, "x")) // will disappear or have an error
|
|
|
|
touch(t, filepath.Join(dir, "good2"))
|
2013-12-17 12:19:01 -08:00
|
|
|
readDir := func() ([]FileInfo, error) {
|
|
|
|
d, err := Open(dir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer d.Close()
|
|
|
|
return d.Readdir(-1)
|
|
|
|
}
|
|
|
|
mustReadDir := func(testName string) []FileInfo {
|
|
|
|
fis, err := readDir()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%s: Readdir: %v", testName, err)
|
|
|
|
}
|
|
|
|
return fis
|
|
|
|
}
|
|
|
|
names := func(fis []FileInfo) []string {
|
|
|
|
s := make([]string, len(fis))
|
|
|
|
for i, fi := range fis {
|
|
|
|
s[i] = fi.Name()
|
|
|
|
}
|
2024-05-22 13:38:40 -07:00
|
|
|
slices.Sort(s)
|
2013-12-17 12:19:01 -08:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2016-02-24 11:55:20 +01:00
|
|
|
if got, want := names(mustReadDir("initial readdir")),
|
2024-07-24 10:47:14 +00:00
|
|
|
[]string{"good1", "good2", "x"}; !slices.Equal(got, want) {
|
2013-12-17 12:19:01 -08:00
|
|
|
t.Errorf("initial readdir got %q; want %q", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
xerr = ErrNotExist
|
|
|
|
if got, want := names(mustReadDir("with x disappearing")),
|
2024-07-24 10:47:14 +00:00
|
|
|
[]string{"good1", "good2"}; !slices.Equal(got, want) {
|
2013-12-17 12:19:01 -08:00
|
|
|
t.Errorf("with x disappearing, got %q; want %q", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
xerr = errors.New("some real error")
|
|
|
|
if _, err := readDir(); err != xerr {
|
|
|
|
t.Errorf("with a non-ErrNotExist error, got error %v; want %v", err, xerr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-08 12:19:20 -08:00
|
|
|
// Readdir on a regular file should fail.
|
|
|
|
func TestReaddirOfFile(t *testing.T) {
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
2023-03-28 14:35:20 -07:00
|
|
|
f, err := CreateTemp(t.TempDir(), "_Go_ReaddirOfFile")
|
2015-02-08 12:19:20 -08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
f.Write([]byte("foo"))
|
|
|
|
f.Close()
|
|
|
|
reg, err := Open(f.Name())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer reg.Close()
|
|
|
|
|
|
|
|
names, err := reg.Readdirnames(-1)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("Readdirnames succeeded; want non-nil error")
|
|
|
|
}
|
2020-05-13 14:59:29 -04:00
|
|
|
var pe *PathError
|
|
|
|
if !errors.As(err, &pe) || pe.Path != f.Name() {
|
|
|
|
t.Errorf("Readdirnames returned %q; want a PathError with path %q", err, f.Name())
|
|
|
|
}
|
2015-02-08 12:19:20 -08:00
|
|
|
if len(names) > 0 {
|
|
|
|
t.Errorf("unexpected dir names in regular file: %q", names)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-13 10:16:46 -07:00
|
|
|
func TestHardLink(t *testing.T) {
|
2016-10-28 13:01:51 -04:00
|
|
|
testenv.MustHaveLink(t)
|
2023-09-07 17:21:16 -07:00
|
|
|
t.Chdir(t.TempDir())
|
2016-10-28 13:01:51 -04:00
|
|
|
|
2009-12-15 15:40:16 -08:00
|
|
|
from, to := "hardlinktestfrom", "hardlinktestto"
|
2011-04-04 23:42:14 -07:00
|
|
|
file, err := Create(to)
|
2009-05-13 10:16:46 -07:00
|
|
|
if err != nil {
|
2009-11-09 12:07:39 -08:00
|
|
|
t.Fatalf("open %q failed: %v", to, err)
|
2009-05-13 10:16:46 -07:00
|
|
|
}
|
|
|
|
if err = file.Close(); err != nil {
|
2009-11-09 12:07:39 -08:00
|
|
|
t.Errorf("close %q failed: %v", to, err)
|
2009-05-13 10:16:46 -07:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
err = Link(to, from)
|
2009-05-13 10:16:46 -07:00
|
|
|
if err != nil {
|
2009-11-09 12:07:39 -08:00
|
|
|
t.Fatalf("link %q, %q failed: %v", to, from, err)
|
2009-05-13 10:16:46 -07:00
|
|
|
}
|
2015-02-27 12:34:25 -05:00
|
|
|
|
|
|
|
none := "hardlinktestnone"
|
|
|
|
err = Link(none, none)
|
|
|
|
// Check the returned error is well-formed.
|
|
|
|
if lerr, ok := err.(*LinkError); !ok || lerr.Error() == "" {
|
|
|
|
t.Errorf("link %q, %q failed to return a valid error", none, none)
|
|
|
|
}
|
|
|
|
|
2009-12-15 15:40:16 -08:00
|
|
|
tostat, err := Stat(to)
|
2009-05-13 10:16:46 -07:00
|
|
|
if err != nil {
|
2009-11-09 12:07:39 -08:00
|
|
|
t.Fatalf("stat %q failed: %v", to, err)
|
2009-05-13 10:16:46 -07:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
fromstat, err := Stat(from)
|
2009-05-13 10:16:46 -07:00
|
|
|
if err != nil {
|
2009-11-09 12:07:39 -08:00
|
|
|
t.Fatalf("stat %q failed: %v", from, err)
|
2009-05-13 10:16:46 -07:00
|
|
|
}
|
2012-02-03 00:16:18 -02:00
|
|
|
if !SameFile(tostat, fromstat) {
|
2009-11-09 12:07:39 -08:00
|
|
|
t.Errorf("link %q, %q did not create hard link", to, from)
|
2009-05-13 10:16:46 -07:00
|
|
|
}
|
2017-11-04 19:35:23 -07:00
|
|
|
// We should not be able to perform the same Link() a second time
|
|
|
|
err = Link(to, from)
|
|
|
|
switch err := err.(type) {
|
|
|
|
case *LinkError:
|
|
|
|
if err.Op != "link" {
|
|
|
|
t.Errorf("Link(%q, %q) err.Op = %q; want %q", to, from, err.Op, "link")
|
|
|
|
}
|
|
|
|
if err.Old != to {
|
|
|
|
t.Errorf("Link(%q, %q) err.Old = %q; want %q", to, from, err.Old, to)
|
|
|
|
}
|
|
|
|
if err.New != from {
|
|
|
|
t.Errorf("Link(%q, %q) err.New = %q; want %q", to, from, err.New, from)
|
|
|
|
}
|
|
|
|
if !IsExist(err.Err) {
|
|
|
|
t.Errorf("Link(%q, %q) err.Err = %q; want %q", to, from, err.Err, "file exists error")
|
|
|
|
}
|
|
|
|
case nil:
|
|
|
|
t.Errorf("link %q, %q: expected error, got nil", from, to)
|
|
|
|
default:
|
|
|
|
t.Errorf("link %q, %q: expected %T, got %T %v", from, to, new(LinkError), err, err)
|
|
|
|
}
|
2009-05-13 10:16:46 -07:00
|
|
|
}
|
|
|
|
|
2014-05-20 12:10:19 -04:00
|
|
|
func TestSymlink(t *testing.T) {
|
2016-08-23 22:21:49 +09:00
|
|
|
testenv.MustHaveSymlink(t)
|
2023-09-07 17:21:16 -07:00
|
|
|
t.Chdir(t.TempDir())
|
2016-08-23 22:21:49 +09:00
|
|
|
|
2009-12-15 15:40:16 -08:00
|
|
|
from, to := "symlinktestfrom", "symlinktestto"
|
2011-04-04 23:42:14 -07:00
|
|
|
file, err := Create(to)
|
2009-05-13 10:16:46 -07:00
|
|
|
if err != nil {
|
2017-04-21 15:42:42 +09:00
|
|
|
t.Fatalf("Create(%q) failed: %v", to, err)
|
2009-05-13 10:16:46 -07:00
|
|
|
}
|
|
|
|
if err = file.Close(); err != nil {
|
2017-04-21 15:42:42 +09:00
|
|
|
t.Errorf("Close(%q) failed: %v", to, err)
|
2009-05-13 10:16:46 -07:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
err = Symlink(to, from)
|
2009-05-13 10:16:46 -07:00
|
|
|
if err != nil {
|
2017-04-21 15:42:42 +09:00
|
|
|
t.Fatalf("Symlink(%q, %q) failed: %v", to, from, err)
|
2009-05-13 10:16:46 -07:00
|
|
|
}
|
2011-11-30 12:04:16 -05:00
|
|
|
tostat, err := Lstat(to)
|
2009-05-13 10:16:46 -07:00
|
|
|
if err != nil {
|
2017-04-21 15:42:42 +09:00
|
|
|
t.Fatalf("Lstat(%q) failed: %v", to, err)
|
2009-05-13 10:16:46 -07:00
|
|
|
}
|
2011-11-30 12:04:16 -05:00
|
|
|
if tostat.Mode()&ModeSymlink != 0 {
|
2017-04-21 15:42:42 +09:00
|
|
|
t.Fatalf("Lstat(%q).Mode()&ModeSymlink = %v, want 0", to, tostat.Mode()&ModeSymlink)
|
2009-05-15 11:04:49 -07:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
fromstat, err := Stat(from)
|
2009-05-13 10:16:46 -07:00
|
|
|
if err != nil {
|
2017-04-21 15:42:42 +09:00
|
|
|
t.Fatalf("Stat(%q) failed: %v", from, err)
|
2009-05-13 10:16:46 -07:00
|
|
|
}
|
2012-02-03 00:16:18 -02:00
|
|
|
if !SameFile(tostat, fromstat) {
|
2017-04-21 15:42:42 +09:00
|
|
|
t.Errorf("Symlink(%q, %q) did not create symlink", to, from)
|
2009-05-13 10:16:46 -07:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
fromstat, err = Lstat(from)
|
2009-05-13 10:16:46 -07:00
|
|
|
if err != nil {
|
2017-04-21 15:42:42 +09:00
|
|
|
t.Fatalf("Lstat(%q) failed: %v", from, err)
|
2009-05-13 10:16:46 -07:00
|
|
|
}
|
2011-11-30 12:04:16 -05:00
|
|
|
if fromstat.Mode()&ModeSymlink == 0 {
|
2017-04-21 15:42:42 +09:00
|
|
|
t.Fatalf("Lstat(%q).Mode()&ModeSymlink = 0, want %v", from, ModeSymlink)
|
2009-05-13 10:16:46 -07:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
fromstat, err = Stat(from)
|
2009-05-15 11:04:49 -07:00
|
|
|
if err != nil {
|
2017-04-21 15:42:42 +09:00
|
|
|
t.Fatalf("Stat(%q) failed: %v", from, err)
|
|
|
|
}
|
|
|
|
if fromstat.Name() != from {
|
|
|
|
t.Errorf("Stat(%q).Name() = %q, want %q", from, fromstat.Name(), from)
|
2009-05-15 11:04:49 -07:00
|
|
|
}
|
2011-11-30 12:04:16 -05:00
|
|
|
if fromstat.Mode()&ModeSymlink != 0 {
|
2017-04-21 15:42:42 +09:00
|
|
|
t.Fatalf("Stat(%q).Mode()&ModeSymlink = %v, want 0", from, fromstat.Mode()&ModeSymlink)
|
2009-05-15 11:04:49 -07:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
s, err := Readlink(from)
|
2009-05-13 10:16:46 -07:00
|
|
|
if err != nil {
|
2017-04-21 15:42:42 +09:00
|
|
|
t.Fatalf("Readlink(%q) failed: %v", from, err)
|
2009-05-13 10:16:46 -07:00
|
|
|
}
|
|
|
|
if s != to {
|
2017-04-21 15:42:42 +09:00
|
|
|
t.Fatalf("Readlink(%q) = %q, want %q", from, s, to)
|
2009-05-13 10:16:46 -07:00
|
|
|
}
|
2011-04-04 23:42:14 -07:00
|
|
|
file, err = Open(from)
|
2009-05-13 10:16:46 -07:00
|
|
|
if err != nil {
|
2017-04-21 15:42:42 +09:00
|
|
|
t.Fatalf("Open(%q) failed: %v", from, err)
|
2009-05-13 10:16:46 -07:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
file.Close()
|
2009-05-13 10:16:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLongSymlink(t *testing.T) {
|
2016-08-23 22:21:49 +09:00
|
|
|
testenv.MustHaveSymlink(t)
|
2023-09-07 17:21:16 -07:00
|
|
|
t.Chdir(t.TempDir())
|
2016-08-23 22:21:49 +09:00
|
|
|
|
2009-12-15 15:40:16 -08:00
|
|
|
s := "0123456789abcdef"
|
2009-11-11 14:52:29 -08:00
|
|
|
// Long, but not too long: a common limit is 255.
|
2009-12-15 15:40:16 -08:00
|
|
|
s = s + s + s + s + s + s + s + s + s + s + s + s + s + s + s
|
|
|
|
from := "longsymlinktestfrom"
|
|
|
|
err := Symlink(s, from)
|
2009-05-13 10:16:46 -07:00
|
|
|
if err != nil {
|
2009-11-09 12:07:39 -08:00
|
|
|
t.Fatalf("symlink %q, %q failed: %v", s, from, err)
|
2009-05-13 10:16:46 -07:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
r, err := Readlink(from)
|
2009-05-13 10:16:46 -07:00
|
|
|
if err != nil {
|
2009-11-09 12:07:39 -08:00
|
|
|
t.Fatalf("readlink %q failed: %v", from, err)
|
2009-05-13 10:16:46 -07:00
|
|
|
}
|
|
|
|
if r != s {
|
2009-11-09 12:07:39 -08:00
|
|
|
t.Fatalf("after symlink %q != %q", r, s)
|
2009-05-13 10:16:46 -07:00
|
|
|
}
|
|
|
|
}
|
2009-05-15 10:32:05 -07:00
|
|
|
|
2009-12-04 11:46:56 -08:00
|
|
|
func TestRename(t *testing.T) {
|
2023-09-07 17:21:16 -07:00
|
|
|
t.Chdir(t.TempDir())
|
2009-12-15 15:40:16 -08:00
|
|
|
from, to := "renamefrom", "renameto"
|
2015-02-26 12:10:11 -08:00
|
|
|
|
2011-04-04 23:42:14 -07:00
|
|
|
file, err := Create(from)
|
2009-12-04 11:46:56 -08:00
|
|
|
if err != nil {
|
2015-02-26 12:10:11 -08:00
|
|
|
t.Fatalf("open %q failed: %v", from, err)
|
2009-12-04 11:46:56 -08:00
|
|
|
}
|
|
|
|
if err = file.Close(); err != nil {
|
2015-02-26 12:10:11 -08:00
|
|
|
t.Errorf("close %q failed: %v", from, err)
|
2009-12-04 11:46:56 -08:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
err = Rename(from, to)
|
2009-12-04 11:46:56 -08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("rename %q, %q failed: %v", to, from, err)
|
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
_, err = Stat(to)
|
2009-12-04 11:46:56 -08:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("stat %q failed: %v", to, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-26 12:10:11 -08:00
|
|
|
func TestRenameOverwriteDest(t *testing.T) {
|
2023-09-07 17:21:16 -07:00
|
|
|
t.Chdir(t.TempDir())
|
2015-02-26 12:10:11 -08:00
|
|
|
from, to := "renamefrom", "renameto"
|
|
|
|
|
|
|
|
toData := []byte("to")
|
|
|
|
fromData := []byte("from")
|
|
|
|
|
2023-03-28 14:35:20 -07:00
|
|
|
err := WriteFile(to, toData, 0777)
|
2015-02-26 12:10:11 -08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("write file %q failed: %v", to, err)
|
|
|
|
}
|
|
|
|
|
2023-03-28 14:35:20 -07:00
|
|
|
err = WriteFile(from, fromData, 0777)
|
2015-02-26 12:10:11 -08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("write file %q failed: %v", from, err)
|
|
|
|
}
|
|
|
|
err = Rename(from, to)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("rename %q, %q failed: %v", to, from, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = Stat(from)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("from file %q still exists", from)
|
|
|
|
}
|
|
|
|
if err != nil && !IsNotExist(err) {
|
|
|
|
t.Fatalf("stat from: %v", err)
|
|
|
|
}
|
|
|
|
toFi, err := Stat(to)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("stat %q failed: %v", to, err)
|
|
|
|
}
|
|
|
|
if toFi.Size() != int64(len(fromData)) {
|
|
|
|
t.Errorf(`"to" size = %d; want %d (old "from" size)`, toFi.Size(), len(fromData))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-17 10:46:38 -07:00
|
|
|
func TestRenameFailed(t *testing.T) {
|
2023-09-07 17:21:16 -07:00
|
|
|
t.Chdir(t.TempDir())
|
2015-07-17 10:46:38 -07:00
|
|
|
from, to := "renamefrom", "renameto"
|
|
|
|
|
|
|
|
err := Rename(from, to)
|
|
|
|
switch err := err.(type) {
|
|
|
|
case *LinkError:
|
|
|
|
if err.Op != "rename" {
|
|
|
|
t.Errorf("rename %q, %q: err.Op: want %q, got %q", from, to, "rename", err.Op)
|
|
|
|
}
|
|
|
|
if err.Old != from {
|
|
|
|
t.Errorf("rename %q, %q: err.Old: want %q, got %q", from, to, from, err.Old)
|
|
|
|
}
|
|
|
|
if err.New != to {
|
|
|
|
t.Errorf("rename %q, %q: err.New: want %q, got %q", from, to, to, err.New)
|
|
|
|
}
|
|
|
|
case nil:
|
|
|
|
t.Errorf("rename %q, %q: expected error, got nil", from, to)
|
2016-10-18 12:34:19 -04:00
|
|
|
default:
|
|
|
|
t.Errorf("rename %q, %q: expected %T, got %T %v", from, to, new(LinkError), err, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-13 11:23:35 -05:00
|
|
|
func TestRenameNotExisting(t *testing.T) {
|
2023-09-07 17:21:16 -07:00
|
|
|
t.Chdir(t.TempDir())
|
2017-04-13 11:23:35 -05:00
|
|
|
from, to := "doesnt-exist", "dest"
|
|
|
|
|
|
|
|
Mkdir(to, 0777)
|
|
|
|
|
|
|
|
if err := Rename(from, to); !IsNotExist(err) {
|
|
|
|
t.Errorf("Rename(%q, %q) = %v; want an IsNotExist error", from, to, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-18 12:34:19 -04:00
|
|
|
func TestRenameToDirFailed(t *testing.T) {
|
2023-09-07 17:21:16 -07:00
|
|
|
t.Chdir(t.TempDir())
|
2016-10-18 12:34:19 -04:00
|
|
|
from, to := "renamefrom", "renameto"
|
|
|
|
|
|
|
|
Mkdir(from, 0777)
|
|
|
|
Mkdir(to, 0777)
|
|
|
|
|
|
|
|
err := Rename(from, to)
|
|
|
|
switch err := err.(type) {
|
|
|
|
case *LinkError:
|
|
|
|
if err.Op != "rename" {
|
|
|
|
t.Errorf("rename %q, %q: err.Op: want %q, got %q", from, to, "rename", err.Op)
|
|
|
|
}
|
|
|
|
if err.Old != from {
|
|
|
|
t.Errorf("rename %q, %q: err.Old: want %q, got %q", from, to, from, err.Old)
|
|
|
|
}
|
|
|
|
if err.New != to {
|
|
|
|
t.Errorf("rename %q, %q: err.New: want %q, got %q", from, to, to, err.New)
|
|
|
|
}
|
|
|
|
case nil:
|
|
|
|
t.Errorf("rename %q, %q: expected error, got nil", from, to)
|
2015-07-17 10:46:38 -07:00
|
|
|
default:
|
|
|
|
t.Errorf("rename %q, %q: expected %T, got %T %v", from, to, new(LinkError), err, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 23:24:26 +00:00
|
|
|
func TestRenameCaseDifference(pt *testing.T) {
|
|
|
|
from, to := "renameFROM", "RENAMEfrom"
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
create func() error
|
|
|
|
}{
|
|
|
|
{"dir", func() error {
|
|
|
|
return Mkdir(from, 0777)
|
|
|
|
}},
|
|
|
|
{"file", func() error {
|
|
|
|
fd, err := Create(from)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fd.Close()
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
pt.Run(test.name, func(t *testing.T) {
|
2023-09-07 17:21:16 -07:00
|
|
|
t.Chdir(t.TempDir())
|
2019-10-31 23:24:26 +00:00
|
|
|
|
|
|
|
if err := test.create(); err != nil {
|
|
|
|
t.Fatalf("failed to create test file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := Stat(to); err != nil {
|
|
|
|
// Sanity check that the underlying filesystem is not case sensitive.
|
|
|
|
if IsNotExist(err) {
|
|
|
|
t.Skipf("case sensitive filesystem")
|
|
|
|
}
|
|
|
|
t.Fatalf("stat %q, got: %q", to, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := Rename(from, to); err != nil {
|
|
|
|
t.Fatalf("unexpected error when renaming from %q to %q: %s", from, to, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fd, err := Open(".")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Open .: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stat does not return the real case of the file (it returns what the called asked for)
|
|
|
|
// So we have to use readdir to get the real name of the file.
|
|
|
|
dirNames, err := fd.Readdirnames(-1)
|
2023-09-09 04:14:42 -07:00
|
|
|
fd.Close()
|
2019-10-31 23:24:26 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("readdirnames: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if dirNamesLen := len(dirNames); dirNamesLen != 1 {
|
|
|
|
t.Fatalf("unexpected dirNames len, got %q, want %q", dirNamesLen, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if dirNames[0] != to {
|
|
|
|
t.Errorf("unexpected name, got %q, want %q", dirNames[0], to)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-13 16:04:09 -05:00
|
|
|
func testStartProcess(dir, cmd string, args []string, expect string) func(t *testing.T) {
|
|
|
|
return func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2009-05-15 10:32:05 -07:00
|
|
|
|
2022-12-13 16:04:09 -05:00
|
|
|
r, w, err := Pipe()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Pipe: %v", err)
|
|
|
|
}
|
|
|
|
defer r.Close()
|
|
|
|
attr := &ProcAttr{Dir: dir, Files: []*File{nil, w, Stderr}}
|
|
|
|
p, err := StartProcess(cmd, args, attr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("StartProcess: %v", err)
|
|
|
|
}
|
|
|
|
w.Close()
|
2013-01-17 18:48:11 +08:00
|
|
|
|
2022-12-13 16:04:09 -05:00
|
|
|
var b strings.Builder
|
|
|
|
io.Copy(&b, r)
|
|
|
|
output := b.String()
|
|
|
|
|
|
|
|
fi1, _ := Stat(strings.TrimSpace(output))
|
|
|
|
fi2, _ := Stat(expect)
|
|
|
|
if !SameFile(fi1, fi2) {
|
|
|
|
t.Errorf("exec %q returned %q wanted %q",
|
|
|
|
strings.Join(append([]string{cmd}, args...), " "), output, expect)
|
|
|
|
}
|
|
|
|
p.Wait()
|
2009-05-15 10:32:05 -07:00
|
|
|
}
|
|
|
|
}
|
2009-05-15 14:11:24 -07:00
|
|
|
|
2011-03-24 11:20:28 +11:00
|
|
|
func TestStartProcess(t *testing.T) {
|
2015-06-05 11:01:53 -04:00
|
|
|
testenv.MustHaveExec(t)
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
2014-05-20 12:10:19 -04:00
|
|
|
|
2013-01-17 18:48:11 +08:00
|
|
|
var dir, cmd string
|
2011-03-24 11:20:28 +11:00
|
|
|
var args []string
|
2015-06-18 10:44:52 +09:00
|
|
|
switch runtime.GOOS {
|
|
|
|
case "android":
|
|
|
|
t.Skip("android doesn't have /bin/pwd")
|
|
|
|
case "windows":
|
2011-03-24 11:20:28 +11:00
|
|
|
cmd = Getenv("COMSPEC")
|
|
|
|
dir = Getenv("SystemRoot")
|
|
|
|
args = []string{"/c", "cd"}
|
2015-06-18 10:44:52 +09:00
|
|
|
default:
|
2017-08-29 09:59:27 -07:00
|
|
|
var err error
|
2022-12-13 16:04:09 -05:00
|
|
|
cmd, err = exec.LookPath("pwd")
|
2017-08-29 09:59:27 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Can't find pwd: %v", err)
|
|
|
|
}
|
2011-03-24 11:20:28 +11:00
|
|
|
dir = "/"
|
|
|
|
args = []string{}
|
2017-08-29 09:59:27 -07:00
|
|
|
t.Logf("Testing with %v", cmd)
|
2011-03-24 11:20:28 +11:00
|
|
|
}
|
|
|
|
cmddir, cmdbase := filepath.Split(cmd)
|
|
|
|
args = append([]string{cmdbase}, args...)
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Run("absolute", testStartProcess(dir, cmd, args, dir))
|
|
|
|
t.Run("relative", testStartProcess(cmddir, cmdbase, args, cmddir))
|
2011-03-24 11:20:28 +11:00
|
|
|
}
|
|
|
|
|
2011-11-30 12:04:16 -05:00
|
|
|
func checkMode(t *testing.T, path string, mode FileMode) {
|
2009-12-15 15:40:16 -08:00
|
|
|
dir, err := Stat(path)
|
2009-05-15 14:11:24 -07:00
|
|
|
if err != nil {
|
2009-11-09 12:07:39 -08:00
|
|
|
t.Fatalf("Stat %q (looking for mode %#o): %s", path, mode, err)
|
2009-05-15 14:11:24 -07:00
|
|
|
}
|
2020-08-16 13:48:09 +02:00
|
|
|
if dir.Mode()&ModePerm != mode {
|
2011-11-30 12:04:16 -05:00
|
|
|
t.Errorf("Stat %q: mode %#o want %#o", path, dir.Mode(), mode)
|
2009-05-15 14:11:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChmod(t *testing.T) {
|
2023-03-25 10:18:26 -07:00
|
|
|
// Chmod is not supported on wasip1.
|
|
|
|
if runtime.GOOS == "wasip1" {
|
|
|
|
t.Skip("Chmod is not supported on " + runtime.GOOS)
|
|
|
|
}
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
2024-06-11 17:25:06 -07:00
|
|
|
f := newFile(t)
|
2020-08-16 13:48:09 +02:00
|
|
|
// Creation mode is read write
|
2009-05-15 14:11:24 -07:00
|
|
|
|
2020-08-16 13:48:09 +02:00
|
|
|
fm := FileMode(0456)
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
fm = FileMode(0444) // read-only file
|
2009-05-15 14:11:24 -07:00
|
|
|
}
|
2020-08-16 13:48:09 +02:00
|
|
|
if err := Chmod(f.Name(), fm); err != nil {
|
|
|
|
t.Fatalf("chmod %s %#o: %s", f.Name(), fm, err)
|
|
|
|
}
|
|
|
|
checkMode(t, f.Name(), fm)
|
2009-05-15 14:11:24 -07:00
|
|
|
|
2020-08-16 13:48:09 +02:00
|
|
|
fm = FileMode(0123)
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
fm = FileMode(0666) // read-write file
|
|
|
|
}
|
|
|
|
if err := f.Chmod(fm); err != nil {
|
|
|
|
t.Fatalf("chmod %s %#o: %s", f.Name(), fm, err)
|
2009-05-15 14:11:24 -07:00
|
|
|
}
|
2020-08-16 13:48:09 +02:00
|
|
|
checkMode(t, f.Name(), fm)
|
2009-05-15 14:11:24 -07:00
|
|
|
}
|
|
|
|
|
2010-10-04 17:31:49 +11:00
|
|
|
func checkSize(t *testing.T, f *File, size int64) {
|
2020-10-29 13:51:20 -04:00
|
|
|
t.Helper()
|
2010-10-04 17:31:49 +11:00
|
|
|
dir, err := f.Stat()
|
2009-05-15 14:11:24 -07:00
|
|
|
if err != nil {
|
2010-10-04 17:31:49 +11:00
|
|
|
t.Fatalf("Stat %q (looking for size %d): %s", f.Name(), size, err)
|
2009-05-15 14:11:24 -07:00
|
|
|
}
|
2011-11-30 12:04:16 -05:00
|
|
|
if dir.Size() != size {
|
|
|
|
t.Errorf("Stat %q: size %d want %d", f.Name(), dir.Size(), size)
|
2009-05-15 14:11:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-22 14:44:18 -04:00
|
|
|
func TestFTruncate(t *testing.T) {
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
2024-06-11 17:25:06 -07:00
|
|
|
f := newFile(t)
|
2010-07-13 10:31:51 +10:00
|
|
|
|
2010-10-04 17:31:49 +11:00
|
|
|
checkSize(t, f, 0)
|
2010-07-13 10:31:51 +10:00
|
|
|
f.Write([]byte("hello, world\n"))
|
2010-10-04 17:31:49 +11:00
|
|
|
checkSize(t, f, 13)
|
2010-07-13 10:31:51 +10:00
|
|
|
f.Truncate(10)
|
2010-10-04 17:31:49 +11:00
|
|
|
checkSize(t, f, 10)
|
2010-07-13 10:31:51 +10:00
|
|
|
f.Truncate(1024)
|
2010-10-04 17:31:49 +11:00
|
|
|
checkSize(t, f, 1024)
|
2010-07-13 10:31:51 +10:00
|
|
|
f.Truncate(0)
|
2010-10-04 17:31:49 +11:00
|
|
|
checkSize(t, f, 0)
|
2014-05-20 12:10:19 -04:00
|
|
|
_, err := f.Write([]byte("surprise!"))
|
|
|
|
if err == nil {
|
|
|
|
checkSize(t, f, 13+9) // wrote at offset past where hello, world was.
|
|
|
|
}
|
2010-07-13 10:31:51 +10:00
|
|
|
}
|
|
|
|
|
2011-04-22 14:44:18 -04:00
|
|
|
func TestTruncate(t *testing.T) {
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
2024-06-11 17:25:06 -07:00
|
|
|
f := newFile(t)
|
2011-04-22 14:44:18 -04:00
|
|
|
|
|
|
|
checkSize(t, f, 0)
|
|
|
|
f.Write([]byte("hello, world\n"))
|
|
|
|
checkSize(t, f, 13)
|
|
|
|
Truncate(f.Name(), 10)
|
|
|
|
checkSize(t, f, 10)
|
|
|
|
Truncate(f.Name(), 1024)
|
|
|
|
checkSize(t, f, 1024)
|
|
|
|
Truncate(f.Name(), 0)
|
|
|
|
checkSize(t, f, 0)
|
2014-05-20 12:10:19 -04:00
|
|
|
_, err := f.Write([]byte("surprise!"))
|
|
|
|
if err == nil {
|
|
|
|
checkSize(t, f, 13+9) // wrote at offset past where hello, world was.
|
|
|
|
}
|
2011-04-22 14:44:18 -04:00
|
|
|
}
|
|
|
|
|
2023-03-22 01:04:45 +00:00
|
|
|
func TestTruncateNonexistentFile(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
assertPathError := func(t testing.TB, path string, err error) {
|
|
|
|
t.Helper()
|
2023-03-28 14:35:20 -07:00
|
|
|
if pe, ok := err.(*PathError); !ok || !IsNotExist(err) || pe.Path != path {
|
2023-03-22 01:04:45 +00:00
|
|
|
t.Errorf("got error: %v\nwant an ErrNotExist PathError with path %q", err, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
path := filepath.Join(t.TempDir(), "nonexistent")
|
|
|
|
|
2023-03-28 14:35:20 -07:00
|
|
|
err := Truncate(path, 1)
|
2023-03-22 01:04:45 +00:00
|
|
|
assertPathError(t, path, err)
|
|
|
|
|
|
|
|
// Truncate shouldn't create any new file.
|
2023-03-28 14:35:20 -07:00
|
|
|
_, err = Stat(path)
|
2023-03-22 01:04:45 +00:00
|
|
|
assertPathError(t, path, err)
|
|
|
|
}
|
|
|
|
|
2024-06-21 16:59:21 -07:00
|
|
|
var hasNoatime = sync.OnceValue(func() bool {
|
|
|
|
// A sloppy way to check if noatime flag is set (as all filesystems are
|
|
|
|
// checked, not just the one we're interested in). A correct way
|
|
|
|
// would be to use statvfs syscall and check if flags has ST_NOATIME,
|
|
|
|
// but the syscall is OS-specific and is not even wired into Go stdlib.
|
|
|
|
//
|
|
|
|
// Only used on NetBSD (which ignores explicit atime updates with noatime).
|
|
|
|
mounts, _ := ReadFile("/proc/mounts")
|
|
|
|
return bytes.Contains(mounts, []byte("noatime"))
|
|
|
|
})
|
|
|
|
|
2010-05-19 17:48:53 -07:00
|
|
|
func TestChtimes(t *testing.T) {
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
2024-06-11 17:25:06 -07:00
|
|
|
f := newFile(t)
|
os: fix Chtimes test flakes
It appears that some builders (notably, linux-arm) have some additional
security software installed, which apparently reads the files created by
tests. As a result, test file atime is changed, making the test fail
like these:
=== RUN TestChtimesOmit
...
os_test.go:1475: atime mismatch, got: "2024-07-30 18:42:03.450932494 +0000 UTC", want: "2024-07-30 18:42:02.450932494 +0000 UTC"
=== RUN TestChtimes
...
os_test.go:1539: AccessTime didn't go backwards; was=2024-07-31 20:45:53.390326147 +0000 UTC, after=2024-07-31 20:45:53.394326118 +0000 UTC
According to inode(7), atime is changed when more than 0 bytes are read
from the file. So, one possible solution to these flakes is to make the
test files empty, so no one can read more than 0 bytes from them.
Fixes #68687
Fixes #68663
Change-Id: Ib9234567883ef7b16ff8811e3360cd26c2d6bdab
Reviewed-on: https://go-review.googlesource.com/c/go/+/604315
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Kirill Kolyshkin <kolyshkin@gmail.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Commit-Queue: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
2024-08-08 13:11:14 -07:00
|
|
|
// This should be an empty file (see #68687, #68663).
|
2010-07-13 10:31:51 +10:00
|
|
|
f.Close()
|
2010-05-19 17:48:53 -07:00
|
|
|
|
2014-10-06 19:22:48 -04:00
|
|
|
testChtimes(t, f.Name())
|
|
|
|
}
|
|
|
|
|
os: rewrite TestChtimesWithZeroTimes
First, this enables checks on DragonFly BSD, which partially works since
CL 589496 (except two things: atime is not supported on hammer2 fs, and
when both times are omitted, it doesn't work due to a kernel bug).
Second, there are a few problems with TestChtimesWithZeroTimes:
- test cases are interdependent (former cases influence the latter ones),
making the test using too many different times and also hard to read;
- time is changed forward not backward which could be racy;
- if the test has failed, it hard to see which exact case is failing.
Plus, there are issues with the error exclusion code in
TestChtimesWithZeroTimes:
- the atime comparison is done twice for the default ("unix") case;
- the atime exclusion caused by noatime mount flag applies to all
unixes rather than netbsd only as it should;
- the atime exclusion tries to read wrong files (/bin/mounts and
/etc/mtab instead of /proc/mounts);
- the exclusion for netbsd is only applied for 64-bit arches, which
seems wrong (and I've reproduced noatime issue on NetBSD 9.4/i386).
Let's rewrite it, fixing all these issues, and rename to
TestChtimesOmit.
NB: TestChtimes can now be removed.
Change-Id: If9020256ca920b4db836a1f0b2e055b5fce4a552
Reviewed-on: https://go-review.googlesource.com/c/go/+/591535
Auto-Submit: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Joedian Reid <joedian@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-06-09 12:47:23 -07:00
|
|
|
func TestChtimesOmit(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
testChtimesOmit(t, true, false)
|
|
|
|
testChtimesOmit(t, false, true)
|
|
|
|
testChtimesOmit(t, true, true)
|
|
|
|
testChtimesOmit(t, false, false) // Same as TestChtimes.
|
|
|
|
}
|
|
|
|
|
|
|
|
func testChtimesOmit(t *testing.T, omitAt, omitMt bool) {
|
|
|
|
t.Logf("omit atime: %v, mtime: %v", omitAt, omitMt)
|
2024-06-11 17:25:06 -07:00
|
|
|
file := newFile(t)
|
os: fix Chtimes test flakes
It appears that some builders (notably, linux-arm) have some additional
security software installed, which apparently reads the files created by
tests. As a result, test file atime is changed, making the test fail
like these:
=== RUN TestChtimesOmit
...
os_test.go:1475: atime mismatch, got: "2024-07-30 18:42:03.450932494 +0000 UTC", want: "2024-07-30 18:42:02.450932494 +0000 UTC"
=== RUN TestChtimes
...
os_test.go:1539: AccessTime didn't go backwards; was=2024-07-31 20:45:53.390326147 +0000 UTC, after=2024-07-31 20:45:53.394326118 +0000 UTC
According to inode(7), atime is changed when more than 0 bytes are read
from the file. So, one possible solution to these flakes is to make the
test files empty, so no one can read more than 0 bytes from them.
Fixes #68687
Fixes #68663
Change-Id: Ib9234567883ef7b16ff8811e3360cd26c2d6bdab
Reviewed-on: https://go-review.googlesource.com/c/go/+/604315
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Kirill Kolyshkin <kolyshkin@gmail.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Commit-Queue: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
2024-08-08 13:11:14 -07:00
|
|
|
// This should be an empty file (see #68687, #68663).
|
os: rewrite TestChtimesWithZeroTimes
First, this enables checks on DragonFly BSD, which partially works since
CL 589496 (except two things: atime is not supported on hammer2 fs, and
when both times are omitted, it doesn't work due to a kernel bug).
Second, there are a few problems with TestChtimesWithZeroTimes:
- test cases are interdependent (former cases influence the latter ones),
making the test using too many different times and also hard to read;
- time is changed forward not backward which could be racy;
- if the test has failed, it hard to see which exact case is failing.
Plus, there are issues with the error exclusion code in
TestChtimesWithZeroTimes:
- the atime comparison is done twice for the default ("unix") case;
- the atime exclusion caused by noatime mount flag applies to all
unixes rather than netbsd only as it should;
- the atime exclusion tries to read wrong files (/bin/mounts and
/etc/mtab instead of /proc/mounts);
- the exclusion for netbsd is only applied for 64-bit arches, which
seems wrong (and I've reproduced noatime issue on NetBSD 9.4/i386).
Let's rewrite it, fixing all these issues, and rename to
TestChtimesOmit.
NB: TestChtimes can now be removed.
Change-Id: If9020256ca920b4db836a1f0b2e055b5fce4a552
Reviewed-on: https://go-review.googlesource.com/c/go/+/591535
Auto-Submit: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Joedian Reid <joedian@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-06-09 12:47:23 -07:00
|
|
|
name := file.Name()
|
os: fix Chtimes test flakes
It appears that some builders (notably, linux-arm) have some additional
security software installed, which apparently reads the files created by
tests. As a result, test file atime is changed, making the test fail
like these:
=== RUN TestChtimesOmit
...
os_test.go:1475: atime mismatch, got: "2024-07-30 18:42:03.450932494 +0000 UTC", want: "2024-07-30 18:42:02.450932494 +0000 UTC"
=== RUN TestChtimes
...
os_test.go:1539: AccessTime didn't go backwards; was=2024-07-31 20:45:53.390326147 +0000 UTC, after=2024-07-31 20:45:53.394326118 +0000 UTC
According to inode(7), atime is changed when more than 0 bytes are read
from the file. So, one possible solution to these flakes is to make the
test files empty, so no one can read more than 0 bytes from them.
Fixes #68687
Fixes #68663
Change-Id: Ib9234567883ef7b16ff8811e3360cd26c2d6bdab
Reviewed-on: https://go-review.googlesource.com/c/go/+/604315
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Kirill Kolyshkin <kolyshkin@gmail.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Commit-Queue: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
2024-08-08 13:11:14 -07:00
|
|
|
err := file.Close()
|
2020-10-19 19:19:17 +02:00
|
|
|
if err != nil {
|
os: rewrite TestChtimesWithZeroTimes
First, this enables checks on DragonFly BSD, which partially works since
CL 589496 (except two things: atime is not supported on hammer2 fs, and
when both times are omitted, it doesn't work due to a kernel bug).
Second, there are a few problems with TestChtimesWithZeroTimes:
- test cases are interdependent (former cases influence the latter ones),
making the test using too many different times and also hard to read;
- time is changed forward not backward which could be racy;
- if the test has failed, it hard to see which exact case is failing.
Plus, there are issues with the error exclusion code in
TestChtimesWithZeroTimes:
- the atime comparison is done twice for the default ("unix") case;
- the atime exclusion caused by noatime mount flag applies to all
unixes rather than netbsd only as it should;
- the atime exclusion tries to read wrong files (/bin/mounts and
/etc/mtab instead of /proc/mounts);
- the exclusion for netbsd is only applied for 64-bit arches, which
seems wrong (and I've reproduced noatime issue on NetBSD 9.4/i386).
Let's rewrite it, fixing all these issues, and rename to
TestChtimesOmit.
NB: TestChtimes can now be removed.
Change-Id: If9020256ca920b4db836a1f0b2e055b5fce4a552
Reviewed-on: https://go-review.googlesource.com/c/go/+/591535
Auto-Submit: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Joedian Reid <joedian@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-06-09 12:47:23 -07:00
|
|
|
t.Error(err)
|
2020-10-19 19:19:17 +02:00
|
|
|
}
|
os: rewrite TestChtimesWithZeroTimes
First, this enables checks on DragonFly BSD, which partially works since
CL 589496 (except two things: atime is not supported on hammer2 fs, and
when both times are omitted, it doesn't work due to a kernel bug).
Second, there are a few problems with TestChtimesWithZeroTimes:
- test cases are interdependent (former cases influence the latter ones),
making the test using too many different times and also hard to read;
- time is changed forward not backward which could be racy;
- if the test has failed, it hard to see which exact case is failing.
Plus, there are issues with the error exclusion code in
TestChtimesWithZeroTimes:
- the atime comparison is done twice for the default ("unix") case;
- the atime exclusion caused by noatime mount flag applies to all
unixes rather than netbsd only as it should;
- the atime exclusion tries to read wrong files (/bin/mounts and
/etc/mtab instead of /proc/mounts);
- the exclusion for netbsd is only applied for 64-bit arches, which
seems wrong (and I've reproduced noatime issue on NetBSD 9.4/i386).
Let's rewrite it, fixing all these issues, and rename to
TestChtimesOmit.
NB: TestChtimes can now be removed.
Change-Id: If9020256ca920b4db836a1f0b2e055b5fce4a552
Reviewed-on: https://go-review.googlesource.com/c/go/+/591535
Auto-Submit: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Joedian Reid <joedian@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-06-09 12:47:23 -07:00
|
|
|
fs, err := Stat(name)
|
2020-10-19 19:19:17 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
os: rewrite TestChtimesWithZeroTimes
First, this enables checks on DragonFly BSD, which partially works since
CL 589496 (except two things: atime is not supported on hammer2 fs, and
when both times are omitted, it doesn't work due to a kernel bug).
Second, there are a few problems with TestChtimesWithZeroTimes:
- test cases are interdependent (former cases influence the latter ones),
making the test using too many different times and also hard to read;
- time is changed forward not backward which could be racy;
- if the test has failed, it hard to see which exact case is failing.
Plus, there are issues with the error exclusion code in
TestChtimesWithZeroTimes:
- the atime comparison is done twice for the default ("unix") case;
- the atime exclusion caused by noatime mount flag applies to all
unixes rather than netbsd only as it should;
- the atime exclusion tries to read wrong files (/bin/mounts and
/etc/mtab instead of /proc/mounts);
- the exclusion for netbsd is only applied for 64-bit arches, which
seems wrong (and I've reproduced noatime issue on NetBSD 9.4/i386).
Let's rewrite it, fixing all these issues, and rename to
TestChtimesOmit.
NB: TestChtimes can now be removed.
Change-Id: If9020256ca920b4db836a1f0b2e055b5fce4a552
Reviewed-on: https://go-review.googlesource.com/c/go/+/591535
Auto-Submit: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Joedian Reid <joedian@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-06-09 12:47:23 -07:00
|
|
|
|
|
|
|
wantAtime := Atime(fs)
|
|
|
|
wantMtime := fs.ModTime()
|
2020-10-19 19:19:17 +02:00
|
|
|
switch runtime.GOOS {
|
|
|
|
case "js":
|
os: rewrite TestChtimesWithZeroTimes
First, this enables checks on DragonFly BSD, which partially works since
CL 589496 (except two things: atime is not supported on hammer2 fs, and
when both times are omitted, it doesn't work due to a kernel bug).
Second, there are a few problems with TestChtimesWithZeroTimes:
- test cases are interdependent (former cases influence the latter ones),
making the test using too many different times and also hard to read;
- time is changed forward not backward which could be racy;
- if the test has failed, it hard to see which exact case is failing.
Plus, there are issues with the error exclusion code in
TestChtimesWithZeroTimes:
- the atime comparison is done twice for the default ("unix") case;
- the atime exclusion caused by noatime mount flag applies to all
unixes rather than netbsd only as it should;
- the atime exclusion tries to read wrong files (/bin/mounts and
/etc/mtab instead of /proc/mounts);
- the exclusion for netbsd is only applied for 64-bit arches, which
seems wrong (and I've reproduced noatime issue on NetBSD 9.4/i386).
Let's rewrite it, fixing all these issues, and rename to
TestChtimesOmit.
NB: TestChtimes can now be removed.
Change-Id: If9020256ca920b4db836a1f0b2e055b5fce4a552
Reviewed-on: https://go-review.googlesource.com/c/go/+/591535
Auto-Submit: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Joedian Reid <joedian@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-06-09 12:47:23 -07:00
|
|
|
wantAtime = wantAtime.Truncate(time.Second)
|
|
|
|
wantMtime = wantMtime.Truncate(time.Second)
|
2020-10-19 19:19:17 +02:00
|
|
|
}
|
|
|
|
|
os: rewrite TestChtimesWithZeroTimes
First, this enables checks on DragonFly BSD, which partially works since
CL 589496 (except two things: atime is not supported on hammer2 fs, and
when both times are omitted, it doesn't work due to a kernel bug).
Second, there are a few problems with TestChtimesWithZeroTimes:
- test cases are interdependent (former cases influence the latter ones),
making the test using too many different times and also hard to read;
- time is changed forward not backward which could be racy;
- if the test has failed, it hard to see which exact case is failing.
Plus, there are issues with the error exclusion code in
TestChtimesWithZeroTimes:
- the atime comparison is done twice for the default ("unix") case;
- the atime exclusion caused by noatime mount flag applies to all
unixes rather than netbsd only as it should;
- the atime exclusion tries to read wrong files (/bin/mounts and
/etc/mtab instead of /proc/mounts);
- the exclusion for netbsd is only applied for 64-bit arches, which
seems wrong (and I've reproduced noatime issue on NetBSD 9.4/i386).
Let's rewrite it, fixing all these issues, and rename to
TestChtimesOmit.
NB: TestChtimes can now be removed.
Change-Id: If9020256ca920b4db836a1f0b2e055b5fce4a552
Reviewed-on: https://go-review.googlesource.com/c/go/+/591535
Auto-Submit: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Joedian Reid <joedian@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-06-09 12:47:23 -07:00
|
|
|
var setAtime, setMtime time.Time // Zero value means omit.
|
|
|
|
if !omitAt {
|
|
|
|
wantAtime = wantAtime.Add(-1 * time.Second)
|
|
|
|
setAtime = wantAtime
|
|
|
|
}
|
|
|
|
if !omitMt {
|
|
|
|
wantMtime = wantMtime.Add(-1 * time.Second)
|
|
|
|
setMtime = wantMtime
|
2020-10-19 19:19:17 +02:00
|
|
|
}
|
|
|
|
|
os: rewrite TestChtimesWithZeroTimes
First, this enables checks on DragonFly BSD, which partially works since
CL 589496 (except two things: atime is not supported on hammer2 fs, and
when both times are omitted, it doesn't work due to a kernel bug).
Second, there are a few problems with TestChtimesWithZeroTimes:
- test cases are interdependent (former cases influence the latter ones),
making the test using too many different times and also hard to read;
- time is changed forward not backward which could be racy;
- if the test has failed, it hard to see which exact case is failing.
Plus, there are issues with the error exclusion code in
TestChtimesWithZeroTimes:
- the atime comparison is done twice for the default ("unix") case;
- the atime exclusion caused by noatime mount flag applies to all
unixes rather than netbsd only as it should;
- the atime exclusion tries to read wrong files (/bin/mounts and
/etc/mtab instead of /proc/mounts);
- the exclusion for netbsd is only applied for 64-bit arches, which
seems wrong (and I've reproduced noatime issue on NetBSD 9.4/i386).
Let's rewrite it, fixing all these issues, and rename to
TestChtimesOmit.
NB: TestChtimes can now be removed.
Change-Id: If9020256ca920b4db836a1f0b2e055b5fce4a552
Reviewed-on: https://go-review.googlesource.com/c/go/+/591535
Auto-Submit: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Joedian Reid <joedian@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-06-09 12:47:23 -07:00
|
|
|
// Change the times accordingly.
|
|
|
|
if err := Chtimes(name, setAtime, setMtime); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2020-10-19 19:19:17 +02:00
|
|
|
|
os: rewrite TestChtimesWithZeroTimes
First, this enables checks on DragonFly BSD, which partially works since
CL 589496 (except two things: atime is not supported on hammer2 fs, and
when both times are omitted, it doesn't work due to a kernel bug).
Second, there are a few problems with TestChtimesWithZeroTimes:
- test cases are interdependent (former cases influence the latter ones),
making the test using too many different times and also hard to read;
- time is changed forward not backward which could be racy;
- if the test has failed, it hard to see which exact case is failing.
Plus, there are issues with the error exclusion code in
TestChtimesWithZeroTimes:
- the atime comparison is done twice for the default ("unix") case;
- the atime exclusion caused by noatime mount flag applies to all
unixes rather than netbsd only as it should;
- the atime exclusion tries to read wrong files (/bin/mounts and
/etc/mtab instead of /proc/mounts);
- the exclusion for netbsd is only applied for 64-bit arches, which
seems wrong (and I've reproduced noatime issue on NetBSD 9.4/i386).
Let's rewrite it, fixing all these issues, and rename to
TestChtimesOmit.
NB: TestChtimes can now be removed.
Change-Id: If9020256ca920b4db836a1f0b2e055b5fce4a552
Reviewed-on: https://go-review.googlesource.com/c/go/+/591535
Auto-Submit: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Joedian Reid <joedian@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-06-09 12:47:23 -07:00
|
|
|
// Verify the expectations.
|
|
|
|
fs, err = Stat(name)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
gotAtime := Atime(fs)
|
|
|
|
gotMtime := fs.ModTime()
|
|
|
|
|
2024-07-01 12:41:56 -07:00
|
|
|
// TODO: remove the dragonfly omitAt && omitMt exceptions below once the
|
|
|
|
// fix (https://github.com/DragonFlyBSD/DragonFlyBSD/commit/c7c71870ed0)
|
|
|
|
// is available generally and on CI runners.
|
os: rewrite TestChtimesWithZeroTimes
First, this enables checks on DragonFly BSD, which partially works since
CL 589496 (except two things: atime is not supported on hammer2 fs, and
when both times are omitted, it doesn't work due to a kernel bug).
Second, there are a few problems with TestChtimesWithZeroTimes:
- test cases are interdependent (former cases influence the latter ones),
making the test using too many different times and also hard to read;
- time is changed forward not backward which could be racy;
- if the test has failed, it hard to see which exact case is failing.
Plus, there are issues with the error exclusion code in
TestChtimesWithZeroTimes:
- the atime comparison is done twice for the default ("unix") case;
- the atime exclusion caused by noatime mount flag applies to all
unixes rather than netbsd only as it should;
- the atime exclusion tries to read wrong files (/bin/mounts and
/etc/mtab instead of /proc/mounts);
- the exclusion for netbsd is only applied for 64-bit arches, which
seems wrong (and I've reproduced noatime issue on NetBSD 9.4/i386).
Let's rewrite it, fixing all these issues, and rename to
TestChtimesOmit.
NB: TestChtimes can now be removed.
Change-Id: If9020256ca920b4db836a1f0b2e055b5fce4a552
Reviewed-on: https://go-review.googlesource.com/c/go/+/591535
Auto-Submit: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Joedian Reid <joedian@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-06-09 12:47:23 -07:00
|
|
|
if !gotAtime.Equal(wantAtime) {
|
|
|
|
errormsg := fmt.Sprintf("atime mismatch, got: %q, want: %q", gotAtime, wantAtime)
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "plan9":
|
|
|
|
// Mtime is the time of the last change of content.
|
|
|
|
// Similarly, atime is set whenever the contents are
|
|
|
|
// accessed; also, it is set whenever mtime is set.
|
|
|
|
case "dragonfly":
|
|
|
|
if omitAt && omitMt {
|
|
|
|
t.Log(errormsg)
|
|
|
|
t.Log("Known DragonFly BSD issue (won't work when both times are omitted); ignoring.")
|
|
|
|
} else {
|
|
|
|
// Assume hammer2 fs; https://www.dragonflybsd.org/hammer/ says:
|
|
|
|
// > Because HAMMER2 is a block copy-on-write filesystem,
|
|
|
|
// > the "atime" field is not supported and will typically
|
|
|
|
// > just reflect local system in-memory caches or mtime.
|
|
|
|
//
|
|
|
|
// TODO: if only can CI define TMPDIR to point to a tmpfs
|
|
|
|
// (e.g. /var/run/shm), this exception can be removed.
|
|
|
|
t.Log(errormsg)
|
|
|
|
t.Log("Known DragonFly BSD issue (atime not supported on hammer2); ignoring.")
|
|
|
|
}
|
|
|
|
case "netbsd":
|
|
|
|
if !omitAt && hasNoatime() {
|
|
|
|
t.Log(errormsg)
|
|
|
|
t.Log("Known NetBSD issue (atime not changed on fs mounted with noatime); ignoring.")
|
|
|
|
} else {
|
2020-10-19 19:19:17 +02:00
|
|
|
t.Error(errormsg)
|
|
|
|
}
|
os: rewrite TestChtimesWithZeroTimes
First, this enables checks on DragonFly BSD, which partially works since
CL 589496 (except two things: atime is not supported on hammer2 fs, and
when both times are omitted, it doesn't work due to a kernel bug).
Second, there are a few problems with TestChtimesWithZeroTimes:
- test cases are interdependent (former cases influence the latter ones),
making the test using too many different times and also hard to read;
- time is changed forward not backward which could be racy;
- if the test has failed, it hard to see which exact case is failing.
Plus, there are issues with the error exclusion code in
TestChtimesWithZeroTimes:
- the atime comparison is done twice for the default ("unix") case;
- the atime exclusion caused by noatime mount flag applies to all
unixes rather than netbsd only as it should;
- the atime exclusion tries to read wrong files (/bin/mounts and
/etc/mtab instead of /proc/mounts);
- the exclusion for netbsd is only applied for 64-bit arches, which
seems wrong (and I've reproduced noatime issue on NetBSD 9.4/i386).
Let's rewrite it, fixing all these issues, and rename to
TestChtimesOmit.
NB: TestChtimes can now be removed.
Change-Id: If9020256ca920b4db836a1f0b2e055b5fce4a552
Reviewed-on: https://go-review.googlesource.com/c/go/+/591535
Auto-Submit: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Joedian Reid <joedian@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-06-09 12:47:23 -07:00
|
|
|
default:
|
|
|
|
t.Error(errormsg)
|
2020-10-19 19:19:17 +02:00
|
|
|
}
|
os: rewrite TestChtimesWithZeroTimes
First, this enables checks on DragonFly BSD, which partially works since
CL 589496 (except two things: atime is not supported on hammer2 fs, and
when both times are omitted, it doesn't work due to a kernel bug).
Second, there are a few problems with TestChtimesWithZeroTimes:
- test cases are interdependent (former cases influence the latter ones),
making the test using too many different times and also hard to read;
- time is changed forward not backward which could be racy;
- if the test has failed, it hard to see which exact case is failing.
Plus, there are issues with the error exclusion code in
TestChtimesWithZeroTimes:
- the atime comparison is done twice for the default ("unix") case;
- the atime exclusion caused by noatime mount flag applies to all
unixes rather than netbsd only as it should;
- the atime exclusion tries to read wrong files (/bin/mounts and
/etc/mtab instead of /proc/mounts);
- the exclusion for netbsd is only applied for 64-bit arches, which
seems wrong (and I've reproduced noatime issue on NetBSD 9.4/i386).
Let's rewrite it, fixing all these issues, and rename to
TestChtimesOmit.
NB: TestChtimes can now be removed.
Change-Id: If9020256ca920b4db836a1f0b2e055b5fce4a552
Reviewed-on: https://go-review.googlesource.com/c/go/+/591535
Auto-Submit: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Joedian Reid <joedian@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-06-09 12:47:23 -07:00
|
|
|
}
|
|
|
|
if !gotMtime.Equal(wantMtime) {
|
|
|
|
errormsg := fmt.Sprintf("mtime mismatch, got: %q, want: %q", gotMtime, wantMtime)
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "dragonfly":
|
|
|
|
if omitAt && omitMt {
|
2020-10-19 19:19:17 +02:00
|
|
|
t.Log(errormsg)
|
os: rewrite TestChtimesWithZeroTimes
First, this enables checks on DragonFly BSD, which partially works since
CL 589496 (except two things: atime is not supported on hammer2 fs, and
when both times are omitted, it doesn't work due to a kernel bug).
Second, there are a few problems with TestChtimesWithZeroTimes:
- test cases are interdependent (former cases influence the latter ones),
making the test using too many different times and also hard to read;
- time is changed forward not backward which could be racy;
- if the test has failed, it hard to see which exact case is failing.
Plus, there are issues with the error exclusion code in
TestChtimesWithZeroTimes:
- the atime comparison is done twice for the default ("unix") case;
- the atime exclusion caused by noatime mount flag applies to all
unixes rather than netbsd only as it should;
- the atime exclusion tries to read wrong files (/bin/mounts and
/etc/mtab instead of /proc/mounts);
- the exclusion for netbsd is only applied for 64-bit arches, which
seems wrong (and I've reproduced noatime issue on NetBSD 9.4/i386).
Let's rewrite it, fixing all these issues, and rename to
TestChtimesOmit.
NB: TestChtimes can now be removed.
Change-Id: If9020256ca920b4db836a1f0b2e055b5fce4a552
Reviewed-on: https://go-review.googlesource.com/c/go/+/591535
Auto-Submit: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Joedian Reid <joedian@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-06-09 12:47:23 -07:00
|
|
|
t.Log("Known DragonFly BSD issue (won't work when both times are omitted); ignoring.")
|
|
|
|
} else {
|
2020-10-19 19:19:17 +02:00
|
|
|
t.Error(errormsg)
|
|
|
|
}
|
os: rewrite TestChtimesWithZeroTimes
First, this enables checks on DragonFly BSD, which partially works since
CL 589496 (except two things: atime is not supported on hammer2 fs, and
when both times are omitted, it doesn't work due to a kernel bug).
Second, there are a few problems with TestChtimesWithZeroTimes:
- test cases are interdependent (former cases influence the latter ones),
making the test using too many different times and also hard to read;
- time is changed forward not backward which could be racy;
- if the test has failed, it hard to see which exact case is failing.
Plus, there are issues with the error exclusion code in
TestChtimesWithZeroTimes:
- the atime comparison is done twice for the default ("unix") case;
- the atime exclusion caused by noatime mount flag applies to all
unixes rather than netbsd only as it should;
- the atime exclusion tries to read wrong files (/bin/mounts and
/etc/mtab instead of /proc/mounts);
- the exclusion for netbsd is only applied for 64-bit arches, which
seems wrong (and I've reproduced noatime issue on NetBSD 9.4/i386).
Let's rewrite it, fixing all these issues, and rename to
TestChtimesOmit.
NB: TestChtimes can now be removed.
Change-Id: If9020256ca920b4db836a1f0b2e055b5fce4a552
Reviewed-on: https://go-review.googlesource.com/c/go/+/591535
Auto-Submit: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Joedian Reid <joedian@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-06-09 12:47:23 -07:00
|
|
|
default:
|
|
|
|
t.Error(errormsg)
|
2020-10-19 19:19:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-06 19:22:48 -04:00
|
|
|
func TestChtimesDir(t *testing.T) {
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
2024-06-11 17:25:06 -07:00
|
|
|
testChtimes(t, t.TempDir())
|
2014-10-06 19:22:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func testChtimes(t *testing.T, name string) {
|
|
|
|
st, err := Stat(name)
|
2010-05-19 17:48:53 -07:00
|
|
|
if err != nil {
|
2014-10-06 19:22:48 -04:00
|
|
|
t.Fatalf("Stat %s: %s", name, err)
|
2010-05-19 17:48:53 -07:00
|
|
|
}
|
2012-02-03 00:16:18 -02:00
|
|
|
preStat := st
|
2010-05-19 17:48:53 -07:00
|
|
|
|
|
|
|
// Move access and modification time back a second
|
2011-11-30 12:04:16 -05:00
|
|
|
at := Atime(preStat)
|
|
|
|
mt := preStat.ModTime()
|
2014-10-06 19:22:48 -04:00
|
|
|
err = Chtimes(name, at.Add(-time.Second), mt.Add(-time.Second))
|
2010-05-19 17:48:53 -07:00
|
|
|
if err != nil {
|
2014-10-06 19:22:48 -04:00
|
|
|
t.Fatalf("Chtimes %s: %s", name, err)
|
2010-05-19 17:48:53 -07:00
|
|
|
}
|
|
|
|
|
2014-10-06 19:22:48 -04:00
|
|
|
st, err = Stat(name)
|
2010-05-19 17:48:53 -07:00
|
|
|
if err != nil {
|
2014-10-06 19:22:48 -04:00
|
|
|
t.Fatalf("second Stat %s: %s", name, err)
|
2010-05-19 17:48:53 -07:00
|
|
|
}
|
2012-02-03 00:16:18 -02:00
|
|
|
postStat := st
|
2010-05-19 17:48:53 -07:00
|
|
|
|
2011-11-30 12:04:16 -05:00
|
|
|
pat := Atime(postStat)
|
|
|
|
pmt := postStat.ModTime()
|
2017-02-26 22:27:13 +00:00
|
|
|
if !pat.Before(at) {
|
2024-06-21 16:59:21 -07:00
|
|
|
errormsg := fmt.Sprintf("AccessTime didn't go backwards; was=%v, after=%v", at, pat)
|
2017-02-26 22:27:13 +00:00
|
|
|
switch runtime.GOOS {
|
2019-10-08 19:19:13 +00:00
|
|
|
case "plan9":
|
2017-02-26 22:27:13 +00:00
|
|
|
// Mtime is the time of the last change of
|
|
|
|
// content. Similarly, atime is set whenever
|
|
|
|
// the contents are accessed; also, it is set
|
|
|
|
// whenever mtime is set.
|
2017-11-30 03:31:18 +00:00
|
|
|
case "netbsd":
|
2024-06-21 16:59:21 -07:00
|
|
|
if hasNoatime() {
|
|
|
|
t.Log(errormsg)
|
|
|
|
t.Log("Known NetBSD issue (atime not changed on fs mounted with noatime); ignoring.")
|
2017-12-01 03:25:02 +00:00
|
|
|
} else {
|
2024-08-06 10:06:39 -07:00
|
|
|
t.Error(errormsg)
|
2017-12-01 03:25:02 +00:00
|
|
|
}
|
2017-02-26 22:27:13 +00:00
|
|
|
default:
|
2024-08-06 10:06:39 -07:00
|
|
|
t.Error(errormsg)
|
2017-02-26 22:27:13 +00:00
|
|
|
}
|
2010-05-19 17:48:53 -07:00
|
|
|
}
|
|
|
|
|
2011-11-30 12:04:16 -05:00
|
|
|
if !pmt.Before(mt) {
|
2016-11-13 13:10:42 -08:00
|
|
|
t.Errorf("ModTime didn't go backwards; was=%v, after=%v", mt, pmt)
|
2010-05-19 17:48:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-15 13:53:21 -07:00
|
|
|
func TestChtimesToUnixZero(t *testing.T) {
|
2024-06-11 17:25:06 -07:00
|
|
|
file := newFile(t)
|
2023-05-15 13:53:21 -07:00
|
|
|
fn := file.Name()
|
|
|
|
if _, err := file.Write([]byte("hi")); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := file.Close(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
unixZero := time.Unix(0, 0)
|
|
|
|
if err := Chtimes(fn, unixZero, unixZero); err != nil {
|
|
|
|
t.Fatalf("Chtimes failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
st, err := Stat(fn)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if mt := st.ModTime(); mt != unixZero {
|
|
|
|
t.Errorf("mtime is %v, want %v", mt, unixZero)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-01 17:01:58 +01:00
|
|
|
func TestFileChdir(t *testing.T) {
|
|
|
|
wd, err := Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Getwd: %s", err)
|
|
|
|
}
|
2023-09-07 17:21:16 -07:00
|
|
|
t.Chdir(".") // Ensure wd is restored after the test.
|
2020-03-01 17:01:58 +01:00
|
|
|
|
|
|
|
fd, err := Open(".")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Open .: %s", err)
|
|
|
|
}
|
|
|
|
defer fd.Close()
|
|
|
|
|
|
|
|
if err := Chdir("/"); err != nil {
|
|
|
|
t.Fatalf("Chdir /: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := fd.Chdir(); err != nil {
|
|
|
|
t.Fatalf("fd.Chdir: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
wdNew, err := Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Getwd: %s", err)
|
|
|
|
}
|
2023-11-20 23:08:53 +00:00
|
|
|
|
|
|
|
wdInfo, err := fd.Stat()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
newInfo, err := Stat(wdNew)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !SameFile(wdInfo, newInfo) {
|
|
|
|
t.Fatalf("fd.Chdir failed: got %s, want %s", wdNew, wd)
|
2020-03-01 17:01:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-18 10:49:34 -07:00
|
|
|
func TestChdirAndGetwd(t *testing.T) {
|
2023-09-07 17:21:16 -07:00
|
|
|
t.Chdir(t.TempDir()) // Ensure wd is restored after the test.
|
|
|
|
|
2009-05-18 10:49:34 -07:00
|
|
|
// These are chosen carefully not to be symlinks on a Mac
|
2014-08-07 08:58:25 -04:00
|
|
|
// (unlike, say, /var, /etc), except /tmp, which we handle below.
|
|
|
|
dirs := []string{"/", "/usr/bin", "/tmp"}
|
2014-07-08 13:44:06 -04:00
|
|
|
// /usr/bin does not usually exist on Plan 9 or Android.
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "android":
|
2019-04-07 20:00:39 +02:00
|
|
|
dirs = []string{"/system/bin"}
|
2014-07-08 13:44:06 -04:00
|
|
|
case "plan9":
|
2011-06-14 11:20:34 -04:00
|
|
|
dirs = []string{"/", "/usr"}
|
2023-04-27 13:37:48 +09:00
|
|
|
case "ios", "windows", "wasip1":
|
2021-12-30 14:06:30 +01:00
|
|
|
dirs = nil
|
2023-03-29 10:28:33 +02:00
|
|
|
for _, dir := range []string{t.TempDir(), t.TempDir()} {
|
2021-12-30 14:06:30 +01:00
|
|
|
// Expand symlinks so path equality tests work.
|
2023-09-07 17:21:16 -07:00
|
|
|
dir, err := filepath.EvalSymlinks(dir)
|
2021-12-30 14:06:30 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("EvalSymlinks: %v", err)
|
2015-02-27 19:52:11 -05:00
|
|
|
}
|
2021-12-30 14:06:30 +01:00
|
|
|
dirs = append(dirs, dir)
|
2015-02-27 19:52:11 -05:00
|
|
|
}
|
2011-06-14 11:20:34 -04:00
|
|
|
}
|
2009-05-18 10:49:34 -07:00
|
|
|
for mode := 0; mode < 2; mode++ {
|
2009-09-15 09:41:59 -07:00
|
|
|
for _, d := range dirs {
|
2023-09-07 17:21:16 -07:00
|
|
|
var err error
|
2009-05-18 10:49:34 -07:00
|
|
|
if mode == 0 {
|
2009-11-09 12:07:39 -08:00
|
|
|
err = Chdir(d)
|
2009-05-18 10:49:34 -07:00
|
|
|
} else {
|
2017-11-29 11:46:48 +01:00
|
|
|
fd1, err1 := Open(d)
|
|
|
|
if err1 != nil {
|
|
|
|
t.Errorf("Open %s: %s", d, err1)
|
2009-12-15 15:40:16 -08:00
|
|
|
continue
|
2009-05-18 10:49:34 -07:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
err = fd1.Chdir()
|
|
|
|
fd1.Close()
|
2009-05-18 10:49:34 -07:00
|
|
|
}
|
2014-08-07 08:58:25 -04:00
|
|
|
if d == "/tmp" {
|
|
|
|
Setenv("PWD", "/tmp")
|
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
pwd, err1 := Getwd()
|
2009-05-18 10:49:34 -07:00
|
|
|
if err != nil {
|
2009-12-15 15:40:16 -08:00
|
|
|
t.Fatalf("Chdir %s: %s", d, err)
|
2009-05-18 10:49:34 -07:00
|
|
|
}
|
|
|
|
if err1 != nil {
|
2009-12-15 15:40:16 -08:00
|
|
|
t.Fatalf("Getwd in %s: %s", d, err1)
|
2009-05-18 10:49:34 -07:00
|
|
|
}
|
2023-03-29 10:28:33 +02:00
|
|
|
if !equal(pwd, d) {
|
2009-12-15 15:40:16 -08:00
|
|
|
t.Fatalf("Getwd returned %q want %q", pwd, d)
|
2009-05-18 10:49:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-06-02 23:02:20 -07:00
|
|
|
|
2015-04-12 19:36:23 +02:00
|
|
|
// Test that Chdir+Getwd is program-wide.
|
|
|
|
func TestProgWideChdir(t *testing.T) {
|
|
|
|
const N = 10
|
2023-03-01 16:55:45 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
hold := make(chan struct{})
|
|
|
|
done := make(chan struct{})
|
|
|
|
|
|
|
|
d := t.TempDir()
|
2023-09-07 17:21:16 -07:00
|
|
|
t.Chdir(d)
|
2023-03-01 16:55:45 +00:00
|
|
|
|
|
|
|
// Note the deferred Wait must be called after the deferred close(done),
|
|
|
|
// to ensure the N goroutines have been released even if the main goroutine
|
|
|
|
// calls Fatalf. It must be called before the Chdir back to the original
|
|
|
|
// directory, and before the deferred deletion implied by TempDir,
|
|
|
|
// so as not to interfere while the N goroutines are still running.
|
|
|
|
defer wg.Wait()
|
|
|
|
defer close(done)
|
|
|
|
|
2015-04-12 19:36:23 +02:00
|
|
|
for i := 0; i < N; i++ {
|
2023-03-01 16:55:45 +00:00
|
|
|
wg.Add(1)
|
2015-04-12 19:36:23 +02:00
|
|
|
go func(i int) {
|
2023-03-01 16:55:45 +00:00
|
|
|
defer wg.Done()
|
2015-04-12 19:36:23 +02:00
|
|
|
// Lock half the goroutines in their own operating system
|
|
|
|
// thread to exercise more scheduler possibilities.
|
|
|
|
if i%2 == 1 {
|
|
|
|
// On Plan 9, after calling LockOSThread, the goroutines
|
|
|
|
// run on different processes which don't share the working
|
|
|
|
// directory. This used to be an issue because Go expects
|
|
|
|
// the working directory to be program-wide.
|
|
|
|
// See issue 9428.
|
|
|
|
runtime.LockOSThread()
|
|
|
|
}
|
2023-03-01 16:55:45 +00:00
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
case <-hold:
|
|
|
|
}
|
|
|
|
// Getwd might be wrong
|
|
|
|
f0, err := Stat(".")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
2020-02-13 16:20:30 -05:00
|
|
|
return
|
|
|
|
}
|
2015-04-12 19:36:23 +02:00
|
|
|
pwd, err := Getwd()
|
|
|
|
if err != nil {
|
2023-03-01 16:55:45 +00:00
|
|
|
t.Errorf("Getwd: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if pwd != d {
|
|
|
|
t.Errorf("Getwd() = %q, want %q", pwd, d)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
f1, err := Stat(pwd)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !SameFile(f0, f1) {
|
|
|
|
t.Errorf(`Samefile(Stat("."), Getwd()) reports false (%s != %s)`, f0.Name(), f1.Name())
|
2015-04-13 12:11:00 +09:00
|
|
|
return
|
2015-04-12 19:36:23 +02:00
|
|
|
}
|
|
|
|
}(i)
|
|
|
|
}
|
2023-09-07 17:21:16 -07:00
|
|
|
var err error
|
2023-03-01 16:55:45 +00:00
|
|
|
if err = Chdir(d); err != nil {
|
2015-06-19 09:36:25 +09:00
|
|
|
t.Fatalf("Chdir: %v", err)
|
2015-04-12 19:36:23 +02:00
|
|
|
}
|
2015-04-13 12:11:00 +09:00
|
|
|
// OS X sets TMPDIR to a symbolic link.
|
|
|
|
// So we resolve our working directory again before the test.
|
|
|
|
d, err = Getwd()
|
|
|
|
if err != nil {
|
2015-06-19 09:36:25 +09:00
|
|
|
t.Fatalf("Getwd: %v", err)
|
2015-04-13 12:11:00 +09:00
|
|
|
}
|
2023-03-01 16:55:45 +00:00
|
|
|
close(hold)
|
|
|
|
wg.Wait()
|
2015-04-12 19:36:23 +02:00
|
|
|
}
|
|
|
|
|
2009-06-08 22:10:48 -07:00
|
|
|
func TestSeek(t *testing.T) {
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
2024-06-11 17:25:06 -07:00
|
|
|
f := newFile(t)
|
2009-06-08 22:10:48 -07:00
|
|
|
|
2009-12-15 15:40:16 -08:00
|
|
|
const data = "hello, world\n"
|
|
|
|
io.WriteString(f, data)
|
2009-06-08 22:10:48 -07:00
|
|
|
|
|
|
|
type test struct {
|
2009-12-15 15:40:16 -08:00
|
|
|
in int64
|
|
|
|
whence int
|
|
|
|
out int64
|
2009-10-06 19:40:35 -07:00
|
|
|
}
|
|
|
|
var tests = []test{
|
2016-04-05 11:22:53 -07:00
|
|
|
{0, io.SeekCurrent, int64(len(data))},
|
|
|
|
{0, io.SeekStart, 0},
|
|
|
|
{5, io.SeekStart, 5},
|
|
|
|
{0, io.SeekEnd, int64(len(data))},
|
|
|
|
{0, io.SeekStart, 0},
|
|
|
|
{-1, io.SeekEnd, int64(len(data)) - 1},
|
|
|
|
{1 << 33, io.SeekStart, 1 << 33},
|
|
|
|
{1 << 33, io.SeekEnd, 1<<33 + int64(len(data))},
|
2017-12-06 18:20:26 +00:00
|
|
|
|
|
|
|
// Issue 21681, Windows 4G-1, etc:
|
|
|
|
{1<<32 - 1, io.SeekStart, 1<<32 - 1},
|
|
|
|
{0, io.SeekCurrent, 1<<32 - 1},
|
|
|
|
{2<<32 - 1, io.SeekStart, 2<<32 - 1},
|
|
|
|
{0, io.SeekCurrent, 2<<32 - 1},
|
2009-12-15 15:40:16 -08:00
|
|
|
}
|
2009-06-08 22:10:48 -07:00
|
|
|
for i, tt := range tests {
|
2009-12-15 15:40:16 -08:00
|
|
|
off, err := f.Seek(tt.in, tt.whence)
|
2009-06-08 22:10:48 -07:00
|
|
|
if off != tt.out || err != nil {
|
2009-12-15 15:40:16 -08:00
|
|
|
t.Errorf("#%d: Seek(%v, %v) = %v, %v want %v, nil", i, tt.in, tt.whence, off, err, tt.out)
|
2009-06-08 22:10:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-06-25 20:24:55 -07:00
|
|
|
|
2017-04-21 17:10:58 +09:00
|
|
|
func TestSeekError(t *testing.T) {
|
|
|
|
switch runtime.GOOS {
|
2023-03-25 10:18:26 -07:00
|
|
|
case "js", "plan9", "wasip1":
|
2017-04-21 17:10:58 +09:00
|
|
|
t.Skipf("skipping test on %v", runtime.GOOS)
|
|
|
|
}
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
2017-04-21 17:10:58 +09:00
|
|
|
|
|
|
|
r, w, err := Pipe()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = r.Seek(0, 0)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Seek on pipe should fail")
|
|
|
|
}
|
|
|
|
if perr, ok := err.(*PathError); !ok || perr.Err != syscall.ESPIPE {
|
|
|
|
t.Errorf("Seek returned error %v, want &PathError{Err: syscall.ESPIPE}", err)
|
|
|
|
}
|
|
|
|
_, err = w.Seek(0, 0)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Seek on pipe should fail")
|
|
|
|
}
|
|
|
|
if perr, ok := err.(*PathError); !ok || perr.Err != syscall.ESPIPE {
|
|
|
|
t.Errorf("Seek returned error %v, want &PathError{Err: syscall.ESPIPE}", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-14 15:30:13 -07:00
|
|
|
func TestOpenError(t *testing.T) {
|
|
|
|
t.Parallel()
|
2024-04-23 11:14:19 -07:00
|
|
|
dir := makefs(t, []string{
|
|
|
|
"is-a-file",
|
|
|
|
"is-a-dir/",
|
|
|
|
})
|
|
|
|
t.Run("NoRoot", func(t *testing.T) { testOpenError(t, dir, false) })
|
|
|
|
t.Run("InRoot", func(t *testing.T) { testOpenError(t, dir, true) })
|
|
|
|
}
|
|
|
|
func testOpenError(t *testing.T, dir string, rooted bool) {
|
|
|
|
t.Parallel()
|
|
|
|
var r *Root
|
|
|
|
if rooted {
|
|
|
|
var err error
|
|
|
|
r, err = OpenRoot(dir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer r.Close()
|
2024-10-14 15:30:13 -07:00
|
|
|
}
|
|
|
|
for _, tt := range []struct {
|
|
|
|
path string
|
|
|
|
mode int
|
|
|
|
error error
|
|
|
|
}{{
|
|
|
|
"no-such-file",
|
2009-06-25 20:24:55 -07:00
|
|
|
O_RDONLY,
|
2012-02-17 10:04:29 +11:00
|
|
|
syscall.ENOENT,
|
2024-10-14 15:30:13 -07:00
|
|
|
}, {
|
|
|
|
"is-a-dir",
|
2009-06-25 20:24:55 -07:00
|
|
|
O_WRONLY,
|
2012-02-17 10:04:29 +11:00
|
|
|
syscall.EISDIR,
|
2024-10-14 15:30:13 -07:00
|
|
|
}, {
|
|
|
|
"is-a-file/no-such-file",
|
2009-06-25 20:24:55 -07:00
|
|
|
O_WRONLY,
|
2012-02-17 10:04:29 +11:00
|
|
|
syscall.ENOTDIR,
|
2024-10-14 15:30:13 -07:00
|
|
|
}} {
|
2024-04-23 11:14:19 -07:00
|
|
|
var f *File
|
|
|
|
var err error
|
|
|
|
var name string
|
|
|
|
if rooted {
|
|
|
|
name = fmt.Sprintf("Root(%q).OpenFile(%q, %d)", dir, tt.path, tt.mode)
|
|
|
|
f, err = r.OpenFile(tt.path, tt.mode, 0)
|
|
|
|
} else {
|
|
|
|
path := filepath.Join(dir, tt.path)
|
|
|
|
name = fmt.Sprintf("OpenFile(%q, %d)", path, tt.mode)
|
|
|
|
f, err = OpenFile(path, tt.mode, 0)
|
|
|
|
}
|
2009-06-25 20:24:55 -07:00
|
|
|
if err == nil {
|
2024-04-23 11:14:19 -07:00
|
|
|
t.Errorf("%v succeeded", name)
|
2009-12-15 15:40:16 -08:00
|
|
|
f.Close()
|
|
|
|
continue
|
2009-06-25 20:24:55 -07:00
|
|
|
}
|
2010-10-04 17:31:49 +11:00
|
|
|
perr, ok := err.(*PathError)
|
|
|
|
if !ok {
|
2024-04-23 11:14:19 -07:00
|
|
|
t.Errorf("%v returns error of %T type; want *PathError", name, err)
|
2010-10-04 17:31:49 +11:00
|
|
|
}
|
2011-11-01 21:49:08 -04:00
|
|
|
if perr.Err != tt.error {
|
2012-01-14 06:40:55 +09:00
|
|
|
if runtime.GOOS == "plan9" {
|
2011-11-01 21:49:08 -04:00
|
|
|
syscallErrStr := perr.Err.Error()
|
|
|
|
expectedErrStr := strings.Replace(tt.error.Error(), "file ", "", 1)
|
2011-06-14 11:20:34 -04:00
|
|
|
if !strings.HasSuffix(syscallErrStr, expectedErrStr) {
|
2014-07-09 12:31:20 +02:00
|
|
|
// Some Plan 9 file servers incorrectly return
|
2024-11-19 12:15:26 +00:00
|
|
|
// EPERM or EACCES rather than EISDIR when a directory is
|
2014-07-09 12:31:20 +02:00
|
|
|
// opened for write.
|
2024-11-19 12:15:26 +00:00
|
|
|
if tt.error == syscall.EISDIR &&
|
|
|
|
(strings.HasSuffix(syscallErrStr, syscall.EPERM.Error()) ||
|
|
|
|
strings.HasSuffix(syscallErrStr, syscall.EACCES.Error())) {
|
2014-07-09 12:31:20 +02:00
|
|
|
continue
|
|
|
|
}
|
2024-04-23 11:14:19 -07:00
|
|
|
t.Errorf("%v = _, %q; want suffix %q", name, syscallErrStr, expectedErrStr)
|
2011-06-14 11:20:34 -04:00
|
|
|
}
|
2013-08-24 02:15:50 +10:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if runtime.GOOS == "dragonfly" {
|
|
|
|
// DragonFly incorrectly returns EACCES rather
|
|
|
|
// EISDIR when a directory is opened for write.
|
|
|
|
if tt.error == syscall.EISDIR && perr.Err == syscall.EACCES {
|
|
|
|
continue
|
|
|
|
}
|
2011-06-14 11:20:34 -04:00
|
|
|
}
|
2024-04-23 11:14:19 -07:00
|
|
|
t.Errorf("%v = _, %q; want %q", name, perr.Err.Error(), tt.error.Error())
|
2009-06-25 20:24:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-06-29 13:44:46 -07:00
|
|
|
|
2011-11-26 11:01:49 +11:00
|
|
|
func TestOpenNoName(t *testing.T) {
|
|
|
|
f, err := Open("")
|
|
|
|
if err == nil {
|
|
|
|
f.Close()
|
2020-10-12 05:39:16 +00:00
|
|
|
t.Fatal(`Open("") succeeded`)
|
2011-11-26 11:01:49 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-07 22:11:48 +00:00
|
|
|
func runBinHostname(t *testing.T) string {
|
2009-06-29 13:44:46 -07:00
|
|
|
// Run /bin/hostname and collect output.
|
2009-12-15 15:40:16 -08:00
|
|
|
r, w, err := Pipe()
|
2009-06-29 13:44:46 -07:00
|
|
|
if err != nil {
|
2009-11-09 12:07:39 -08:00
|
|
|
t.Fatal(err)
|
2009-06-29 13:44:46 -07:00
|
|
|
}
|
2013-06-21 11:13:14 +10:00
|
|
|
defer r.Close()
|
2022-04-20 15:02:35 +00:00
|
|
|
|
2022-12-13 16:04:09 -05:00
|
|
|
path, err := exec.LookPath("hostname")
|
2022-04-20 15:02:35 +00:00
|
|
|
if err != nil {
|
2022-12-13 16:04:09 -05:00
|
|
|
if errors.Is(err, exec.ErrNotFound) {
|
2022-04-20 15:02:35 +00:00
|
|
|
t.Skip("skipping test; test requires hostname but it does not exist")
|
|
|
|
}
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-10-25 10:02:37 +02:00
|
|
|
argv := []string{"hostname"}
|
|
|
|
if runtime.GOOS == "aix" {
|
|
|
|
argv = []string{"hostname", "-s"}
|
|
|
|
}
|
|
|
|
p, err := StartProcess(path, argv, &ProcAttr{Files: []*File{nil, w, Stderr}})
|
2009-06-29 13:44:46 -07:00
|
|
|
if err != nil {
|
2009-11-09 12:07:39 -08:00
|
|
|
t.Fatal(err)
|
2009-06-29 13:44:46 -07:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
w.Close()
|
2009-06-29 13:44:46 -07:00
|
|
|
|
2022-09-04 18:45:00 +08:00
|
|
|
var b strings.Builder
|
2009-12-15 15:40:16 -08:00
|
|
|
io.Copy(&b, r)
|
2012-02-20 15:36:08 +11:00
|
|
|
_, err = p.Wait()
|
2011-07-11 15:47:42 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("run hostname Wait: %v", err)
|
|
|
|
}
|
|
|
|
err = p.Kill()
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("expected an error from Kill running 'hostname'")
|
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
output := b.String()
|
2009-06-29 13:44:46 -07:00
|
|
|
if n := len(output); n > 0 && output[n-1] == '\n' {
|
2009-11-09 12:07:39 -08:00
|
|
|
output = output[0 : n-1]
|
2009-06-29 13:44:46 -07:00
|
|
|
}
|
|
|
|
if output == "" {
|
2016-03-07 22:11:48 +00:00
|
|
|
t.Fatalf("/bin/hostname produced no output")
|
2009-06-29 13:44:46 -07:00
|
|
|
}
|
|
|
|
|
2009-12-15 15:40:16 -08:00
|
|
|
return output
|
2009-06-29 13:44:46 -07:00
|
|
|
}
|
|
|
|
|
2018-04-30 22:55:05 +00:00
|
|
|
func testWindowsHostname(t *testing.T, hostname string) {
|
2022-11-16 22:45:54 +08:00
|
|
|
cmd := testenv.Command(t, "hostname")
|
2022-05-08 23:04:28 -04:00
|
|
|
out, err := cmd.Output()
|
2015-02-26 14:19:19 +11:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to execute hostname command: %v %s", err, out)
|
|
|
|
}
|
|
|
|
want := strings.Trim(string(out), "\r\n")
|
|
|
|
if hostname != want {
|
2018-04-30 22:55:05 +00:00
|
|
|
t.Fatalf("Hostname() = %q != system hostname of %q", hostname, want)
|
2015-02-26 14:19:19 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-29 13:44:46 -07:00
|
|
|
func TestHostname(t *testing.T) {
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
2018-04-30 22:55:05 +00:00
|
|
|
hostname, err := Hostname()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if hostname == "" {
|
|
|
|
t.Fatal("Hostname returned empty string and no error")
|
|
|
|
}
|
|
|
|
if strings.Contains(hostname, "\x00") {
|
|
|
|
t.Fatalf("unexpected zero byte in hostname: %q", hostname)
|
|
|
|
}
|
|
|
|
|
2010-10-04 17:31:49 +11:00
|
|
|
// There is no other way to fetch hostname on windows, but via winapi.
|
2014-07-08 13:44:06 -04:00
|
|
|
// On Plan 9 it can be taken from #c/sysname as Hostname() does.
|
2015-06-16 23:42:23 +02:00
|
|
|
switch runtime.GOOS {
|
2015-06-18 10:44:52 +09:00
|
|
|
case "android", "plan9":
|
2018-04-30 22:55:05 +00:00
|
|
|
// No /bin/hostname to verify against.
|
2018-04-30 16:24:07 +00:00
|
|
|
return
|
2015-06-16 23:42:23 +02:00
|
|
|
case "windows":
|
2018-04-30 22:55:05 +00:00
|
|
|
testWindowsHostname(t, hostname)
|
2015-02-26 14:19:19 +11:00
|
|
|
return
|
2010-10-04 17:31:49 +11:00
|
|
|
}
|
2011-08-22 23:24:16 -04:00
|
|
|
|
2015-06-05 11:01:53 -04:00
|
|
|
testenv.MustHaveExec(t)
|
|
|
|
|
2009-06-29 13:44:46 -07:00
|
|
|
// Check internal Hostname() against the output of /bin/hostname.
|
2010-01-26 13:16:03 -08:00
|
|
|
// Allow that the internal Hostname returns a Fully Qualified Domain Name
|
|
|
|
// and the /bin/hostname only returns the first component
|
2016-03-07 22:11:48 +00:00
|
|
|
want := runBinHostname(t)
|
2009-06-29 13:44:46 -07:00
|
|
|
if hostname != want {
|
2021-09-22 10:46:32 -04:00
|
|
|
host, _, ok := strings.Cut(hostname, ".")
|
|
|
|
if !ok || host != want {
|
2010-01-26 13:16:03 -08:00
|
|
|
t.Errorf("Hostname() = %q, want %q", hostname, want)
|
|
|
|
}
|
2009-06-29 13:44:46 -07:00
|
|
|
}
|
|
|
|
}
|
2009-08-27 18:36:45 -07:00
|
|
|
|
|
|
|
func TestReadAt(t *testing.T) {
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
2024-06-11 17:25:06 -07:00
|
|
|
f := newFile(t)
|
2010-07-13 10:31:51 +10:00
|
|
|
|
2009-12-15 15:40:16 -08:00
|
|
|
const data = "hello, world\n"
|
|
|
|
io.WriteString(f, data)
|
2009-08-27 18:36:45 -07:00
|
|
|
|
2009-12-15 15:40:16 -08:00
|
|
|
b := make([]byte, 5)
|
|
|
|
n, err := f.ReadAt(b, 7)
|
2009-08-27 18:36:45 -07:00
|
|
|
if err != nil || n != len(b) {
|
2011-12-15 15:17:52 -08:00
|
|
|
t.Fatalf("ReadAt 7: %d, %v", n, err)
|
2009-08-27 18:36:45 -07:00
|
|
|
}
|
|
|
|
if string(b) != "world" {
|
2009-11-09 12:07:39 -08:00
|
|
|
t.Fatalf("ReadAt 7: have %q want %q", string(b), "world")
|
2009-08-27 18:36:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-19 20:59:03 +02:00
|
|
|
// Verify that ReadAt doesn't affect seek offset.
|
|
|
|
// In the Plan 9 kernel, there used to be a bug in the implementation of
|
|
|
|
// the pread syscall, where the channel offset was erroneously updated after
|
|
|
|
// calling pread on a file.
|
|
|
|
func TestReadAtOffset(t *testing.T) {
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
2024-06-11 17:25:06 -07:00
|
|
|
f := newFile(t)
|
2016-04-19 20:59:03 +02:00
|
|
|
|
|
|
|
const data = "hello, world\n"
|
|
|
|
io.WriteString(f, data)
|
|
|
|
|
|
|
|
f.Seek(0, 0)
|
|
|
|
b := make([]byte, 5)
|
|
|
|
|
|
|
|
n, err := f.ReadAt(b, 7)
|
|
|
|
if err != nil || n != len(b) {
|
|
|
|
t.Fatalf("ReadAt 7: %d, %v", n, err)
|
|
|
|
}
|
|
|
|
if string(b) != "world" {
|
|
|
|
t.Fatalf("ReadAt 7: have %q want %q", string(b), "world")
|
|
|
|
}
|
|
|
|
|
|
|
|
n, err = f.Read(b)
|
|
|
|
if err != nil || n != len(b) {
|
|
|
|
t.Fatalf("Read: %d, %v", n, err)
|
|
|
|
}
|
|
|
|
if string(b) != "hello" {
|
|
|
|
t.Fatalf("Read: have %q want %q", string(b), "hello")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-09 21:12:39 +01:00
|
|
|
// Verify that ReadAt doesn't allow negative offset.
|
|
|
|
func TestReadAtNegativeOffset(t *testing.T) {
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
2024-06-11 17:25:06 -07:00
|
|
|
f := newFile(t)
|
2017-04-09 21:12:39 +01:00
|
|
|
|
|
|
|
const data = "hello, world\n"
|
|
|
|
io.WriteString(f, data)
|
|
|
|
|
|
|
|
f.Seek(0, 0)
|
|
|
|
b := make([]byte, 5)
|
|
|
|
|
|
|
|
n, err := f.ReadAt(b, -10)
|
|
|
|
|
|
|
|
const wantsub = "negative offset"
|
|
|
|
if !strings.Contains(fmt.Sprint(err), wantsub) || n != 0 {
|
|
|
|
t.Errorf("ReadAt(-10) = %v, %v; want 0, ...%q...", n, err, wantsub)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-27 18:36:45 -07:00
|
|
|
func TestWriteAt(t *testing.T) {
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
2024-06-11 17:25:06 -07:00
|
|
|
f := newFile(t)
|
2010-07-13 10:31:51 +10:00
|
|
|
|
2009-12-15 15:40:16 -08:00
|
|
|
const data = "hello, world\n"
|
|
|
|
io.WriteString(f, data)
|
2009-08-27 18:36:45 -07:00
|
|
|
|
2010-02-25 16:01:29 -08:00
|
|
|
n, err := f.WriteAt([]byte("WORLD"), 7)
|
2009-08-27 18:36:45 -07:00
|
|
|
if err != nil || n != 5 {
|
2009-11-09 12:07:39 -08:00
|
|
|
t.Fatalf("WriteAt 7: %d, %v", n, err)
|
2009-08-27 18:36:45 -07:00
|
|
|
}
|
|
|
|
|
2023-03-28 14:35:20 -07:00
|
|
|
b, err := ReadFile(f.Name())
|
2009-08-27 18:36:45 -07:00
|
|
|
if err != nil {
|
2010-07-13 10:31:51 +10:00
|
|
|
t.Fatalf("ReadFile %s: %v", f.Name(), err)
|
2009-08-27 18:36:45 -07:00
|
|
|
}
|
|
|
|
if string(b) != "hello, WORLD\n" {
|
2009-11-09 12:07:39 -08:00
|
|
|
t.Fatalf("after write: have %q want %q", string(b), "hello, WORLD\n")
|
2009-08-27 18:36:45 -07:00
|
|
|
}
|
|
|
|
}
|
2010-09-22 13:12:25 +10:00
|
|
|
|
2017-04-09 21:12:39 +01:00
|
|
|
// Verify that WriteAt doesn't allow negative offset.
|
|
|
|
func TestWriteAtNegativeOffset(t *testing.T) {
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
2024-06-11 17:25:06 -07:00
|
|
|
f := newFile(t)
|
2017-04-09 21:12:39 +01:00
|
|
|
|
|
|
|
n, err := f.WriteAt([]byte("WORLD"), -10)
|
|
|
|
|
|
|
|
const wantsub = "negative offset"
|
|
|
|
if !strings.Contains(fmt.Sprint(err), wantsub) || n != 0 {
|
|
|
|
t.Errorf("WriteAt(-10) = %v, %v; want 0, ...%q...", n, err, wantsub)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-11 13:58:20 +07:00
|
|
|
// Verify that WriteAt doesn't work in append mode.
|
|
|
|
func TestWriteAtInAppendMode(t *testing.T) {
|
2023-09-07 17:21:16 -07:00
|
|
|
t.Chdir(t.TempDir())
|
2019-03-11 13:58:20 +07:00
|
|
|
f, err := OpenFile("write_at_in_append_mode.txt", O_APPEND|O_CREATE, 0666)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("OpenFile: %v", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
_, err = f.WriteAt([]byte(""), 1)
|
|
|
|
if err != ErrWriteAtInAppendMode {
|
|
|
|
t.Fatalf("f.WriteAt returned %v, expected %v", err, ErrWriteAtInAppendMode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-23 11:14:19 -07:00
|
|
|
func writeFile(t *testing.T, r *Root, fname string, flag int, text string) string {
|
|
|
|
t.Helper()
|
|
|
|
var f *File
|
|
|
|
var err error
|
|
|
|
if r == nil {
|
|
|
|
f, err = OpenFile(fname, flag, 0666)
|
|
|
|
} else {
|
|
|
|
f, err = r.OpenFile(fname, flag, 0666)
|
|
|
|
}
|
2010-09-22 13:12:25 +10:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Open: %v", err)
|
|
|
|
}
|
|
|
|
n, err := io.WriteString(f, text)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("WriteString: %d, %v", n, err)
|
|
|
|
}
|
|
|
|
f.Close()
|
2023-03-28 14:35:20 -07:00
|
|
|
data, err := ReadFile(fname)
|
2010-09-22 13:12:25 +10:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("ReadFile: %v", err)
|
|
|
|
}
|
|
|
|
return string(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAppend(t *testing.T) {
|
2024-04-23 11:14:19 -07:00
|
|
|
testMaybeRooted(t, func(t *testing.T, r *Root) {
|
|
|
|
const f = "append.txt"
|
|
|
|
s := writeFile(t, r, f, O_CREATE|O_TRUNC|O_RDWR, "new")
|
|
|
|
if s != "new" {
|
|
|
|
t.Fatalf("writeFile: have %q want %q", s, "new")
|
|
|
|
}
|
|
|
|
s = writeFile(t, r, f, O_APPEND|O_RDWR, "|append")
|
|
|
|
if s != "new|append" {
|
|
|
|
t.Fatalf("writeFile: have %q want %q", s, "new|append")
|
|
|
|
}
|
|
|
|
s = writeFile(t, r, f, O_CREATE|O_APPEND|O_RDWR, "|append")
|
|
|
|
if s != "new|append|append" {
|
|
|
|
t.Fatalf("writeFile: have %q want %q", s, "new|append|append")
|
|
|
|
}
|
|
|
|
err := Remove(f)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Remove: %v", err)
|
|
|
|
}
|
|
|
|
s = writeFile(t, r, f, O_CREATE|O_APPEND|O_RDWR, "new&append")
|
|
|
|
if s != "new&append" {
|
|
|
|
t.Fatalf("writeFile: after append have %q want %q", s, "new&append")
|
|
|
|
}
|
|
|
|
s = writeFile(t, r, f, O_CREATE|O_RDWR, "old")
|
|
|
|
if s != "old&append" {
|
|
|
|
t.Fatalf("writeFile: after create have %q want %q", s, "old&append")
|
|
|
|
}
|
|
|
|
s = writeFile(t, r, f, O_CREATE|O_TRUNC|O_RDWR, "new")
|
|
|
|
if s != "new" {
|
|
|
|
t.Fatalf("writeFile: after truncate have %q want %q", s, "new")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestFilePermissions tests setting Unix permission bits on file creation.
|
|
|
|
func TestFilePermissions(t *testing.T) {
|
|
|
|
if Getuid() == 0 {
|
|
|
|
t.Skip("skipping test when running as root")
|
2011-05-27 17:02:24 +10:00
|
|
|
}
|
2024-04-23 11:14:19 -07:00
|
|
|
for _, test := range []struct {
|
|
|
|
name string
|
|
|
|
mode FileMode
|
|
|
|
}{
|
|
|
|
{"r", 0o444},
|
|
|
|
{"w", 0o222},
|
|
|
|
{"rw", 0o666},
|
|
|
|
} {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "windows":
|
|
|
|
if test.mode&0444 == 0 {
|
|
|
|
t.Skip("write-only files not supported on " + runtime.GOOS)
|
|
|
|
}
|
|
|
|
case "wasip1":
|
|
|
|
t.Skip("file permissions not supported on " + runtime.GOOS)
|
|
|
|
}
|
|
|
|
testMaybeRooted(t, func(t *testing.T, r *Root) {
|
|
|
|
const filename = "f"
|
|
|
|
var f *File
|
|
|
|
var err error
|
|
|
|
if r == nil {
|
|
|
|
f, err = OpenFile(filename, O_RDWR|O_CREATE|O_EXCL, test.mode)
|
|
|
|
} else {
|
|
|
|
f, err = r.OpenFile(filename, O_RDWR|O_CREATE|O_EXCL, test.mode)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
b, err := ReadFile(filename)
|
|
|
|
if test.mode&0o444 != 0 {
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("ReadFile = %v; want success", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("ReadFile = %q, <nil>; want failure", string(b))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_, err = Stat(filename)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Stat = %v; want success", err)
|
|
|
|
}
|
|
|
|
err = WriteFile(filename, nil, 0666)
|
|
|
|
if test.mode&0o222 != 0 {
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("WriteFile = %v; want success", err)
|
|
|
|
b, err := ReadFile(filename)
|
|
|
|
t.Errorf("ReadFile: %v", err)
|
|
|
|
t.Errorf("file contents: %q", b)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("WriteFile(%q) = <nil>; want failure", filename)
|
|
|
|
st, err := Stat(filename)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("mode: %s", st.Mode())
|
|
|
|
}
|
|
|
|
b, err := ReadFile(filename)
|
|
|
|
t.Errorf("ReadFile: %v", err)
|
|
|
|
t.Errorf("file contents: %q", b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2011-05-27 17:02:24 +10:00
|
|
|
}
|
2024-04-23 11:14:19 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestFileRDWRFlags tests the O_RDONLY, O_WRONLY, and O_RDWR flags.
|
|
|
|
func TestFileRDWRFlags(t *testing.T) {
|
|
|
|
for _, test := range []struct {
|
|
|
|
name string
|
|
|
|
flag int
|
|
|
|
}{
|
|
|
|
{"O_RDONLY", O_RDONLY},
|
|
|
|
{"O_WRONLY", O_WRONLY},
|
|
|
|
{"O_RDWR", O_RDWR},
|
|
|
|
} {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
testMaybeRooted(t, func(t *testing.T, r *Root) {
|
|
|
|
const filename = "f"
|
|
|
|
content := []byte("content")
|
|
|
|
if err := WriteFile(filename, content, 0666); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
var f *File
|
|
|
|
var err error
|
|
|
|
if r == nil {
|
|
|
|
f, err = OpenFile(filename, test.flag, 0)
|
|
|
|
} else {
|
|
|
|
f, err = r.OpenFile(filename, test.flag, 0)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
got, err := io.ReadAll(f)
|
|
|
|
if test.flag == O_WRONLY {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("read file: %q, %v; want error", got, err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err != nil || !bytes.Equal(got, content) {
|
|
|
|
t.Errorf("read file: %q, %v; want %q, <nil>", got, err, content)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, err := f.Seek(0, 0); err != nil {
|
|
|
|
t.Fatalf("f.Seek: %v", err)
|
|
|
|
}
|
|
|
|
newcontent := []byte("CONTENT")
|
|
|
|
_, err = f.Write(newcontent)
|
|
|
|
if test.flag == O_RDONLY {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("write file: succeeded, want error")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("write file: %v, want success", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
got, err = ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
want := content
|
|
|
|
if test.flag != O_RDONLY {
|
|
|
|
want = newcontent
|
|
|
|
}
|
|
|
|
if !bytes.Equal(got, want) {
|
|
|
|
t.Fatalf("after write, file contains %q, want %q", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2011-04-22 15:31:25 +10:00
|
|
|
}
|
2010-09-22 13:12:25 +10:00
|
|
|
}
|
2010-10-30 23:06:49 +11:00
|
|
|
|
|
|
|
func TestStatDirWithTrailingSlash(t *testing.T) {
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
2012-02-16 17:05:43 +11:00
|
|
|
// Create new temporary directory and arrange to clean it up.
|
2021-04-07 21:27:27 +02:00
|
|
|
path := t.TempDir()
|
2010-10-30 23:06:49 +11:00
|
|
|
|
|
|
|
// Stat of path should succeed.
|
2021-04-07 21:27:27 +02:00
|
|
|
if _, err := Stat(path); err != nil {
|
2012-02-16 17:05:43 +11:00
|
|
|
t.Fatalf("stat %s failed: %s", path, err)
|
2010-10-30 23:06:49 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stat of path+"/" should succeed too.
|
2012-02-16 17:05:43 +11:00
|
|
|
path += "/"
|
2021-04-07 21:27:27 +02:00
|
|
|
if _, err := Stat(path); err != nil {
|
2012-02-16 17:05:43 +11:00
|
|
|
t.Fatalf("stat %s failed: %s", path, err)
|
2010-10-30 23:06:49 +11:00
|
|
|
}
|
|
|
|
}
|
2011-06-20 15:42:17 -04:00
|
|
|
|
2012-02-21 14:10:34 +11:00
|
|
|
func TestNilProcessStateString(t *testing.T) {
|
|
|
|
var ps *ProcessState
|
|
|
|
s := ps.String()
|
2011-06-20 15:42:17 -04:00
|
|
|
if s != "<nil>" {
|
2012-02-21 14:10:34 +11:00
|
|
|
t.Errorf("(*ProcessState)(nil).String() = %q, want %q", s, "<nil>")
|
2011-06-20 15:42:17 -04:00
|
|
|
}
|
|
|
|
}
|
2012-02-27 12:29:33 +11:00
|
|
|
|
|
|
|
func TestSameFile(t *testing.T) {
|
2023-09-07 17:21:16 -07:00
|
|
|
t.Chdir(t.TempDir())
|
2012-02-27 12:29:33 +11:00
|
|
|
fa, err := Create("a")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Create(a): %v", err)
|
|
|
|
}
|
|
|
|
fa.Close()
|
|
|
|
fb, err := Create("b")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Create(b): %v", err)
|
|
|
|
}
|
|
|
|
fb.Close()
|
|
|
|
|
|
|
|
ia1, err := Stat("a")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Stat(a): %v", err)
|
|
|
|
}
|
|
|
|
ia2, err := Stat("a")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Stat(a): %v", err)
|
|
|
|
}
|
|
|
|
if !SameFile(ia1, ia2) {
|
|
|
|
t.Errorf("files should be same")
|
|
|
|
}
|
|
|
|
|
|
|
|
ib, err := Stat("b")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Stat(b): %v", err)
|
|
|
|
}
|
|
|
|
if SameFile(ia1, ib) {
|
|
|
|
t.Errorf("files should be different")
|
|
|
|
}
|
|
|
|
}
|
2012-03-15 16:33:45 +11:00
|
|
|
|
2022-11-08 16:10:47 -08:00
|
|
|
func testDevNullFileInfo(t *testing.T, statname, devNullName string, fi FileInfo) {
|
2018-03-25 11:08:12 +11:00
|
|
|
pre := fmt.Sprintf("%s(%q): ", statname, devNullName)
|
|
|
|
if fi.Size() != 0 {
|
|
|
|
t.Errorf(pre+"wrong file size have %d want 0", fi.Size())
|
|
|
|
}
|
|
|
|
if fi.Mode()&ModeDevice == 0 {
|
|
|
|
t.Errorf(pre+"wrong file mode %q: ModeDevice is not set", fi.Mode())
|
|
|
|
}
|
|
|
|
if fi.Mode()&ModeCharDevice == 0 {
|
|
|
|
t.Errorf(pre+"wrong file mode %q: ModeCharDevice is not set", fi.Mode())
|
|
|
|
}
|
|
|
|
if fi.Mode().IsRegular() {
|
|
|
|
t.Errorf(pre+"wrong file mode %q: IsRegular returns true", fi.Mode())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-08 16:10:47 -08:00
|
|
|
func testDevNullFile(t *testing.T, devNullName string) {
|
2018-03-25 11:08:12 +11:00
|
|
|
f, err := Open(devNullName)
|
2012-03-15 16:33:45 +11:00
|
|
|
if err != nil {
|
2018-03-25 11:08:12 +11:00
|
|
|
t.Fatalf("Open(%s): %v", devNullName, err)
|
2012-03-15 16:33:45 +11:00
|
|
|
}
|
|
|
|
defer f.Close()
|
2018-03-25 11:08:12 +11:00
|
|
|
|
2012-03-15 16:33:45 +11:00
|
|
|
fi, err := f.Stat()
|
|
|
|
if err != nil {
|
2018-03-25 11:08:12 +11:00
|
|
|
t.Fatalf("Stat(%s): %v", devNullName, err)
|
2012-03-15 16:33:45 +11:00
|
|
|
}
|
2022-11-08 16:10:47 -08:00
|
|
|
testDevNullFileInfo(t, "f.Stat", devNullName, fi)
|
2018-03-25 11:08:12 +11:00
|
|
|
|
|
|
|
fi, err = Stat(devNullName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Stat(%s): %v", devNullName, err)
|
2012-03-15 16:33:45 +11:00
|
|
|
}
|
2022-11-08 16:10:47 -08:00
|
|
|
testDevNullFileInfo(t, "Stat", devNullName, fi)
|
2018-03-25 11:08:12 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDevNullFile(t *testing.T) {
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-11-08 16:10:47 -08:00
|
|
|
testDevNullFile(t, DevNull)
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
testDevNullFile(t, "./nul")
|
|
|
|
testDevNullFile(t, "//./nul")
|
|
|
|
}
|
2012-03-15 16:33:45 +11:00
|
|
|
}
|
2012-09-19 16:55:21 +10:00
|
|
|
|
|
|
|
var testLargeWrite = flag.Bool("large_write", false, "run TestLargeWriteToConsole test that floods console with output")
|
|
|
|
|
|
|
|
func TestLargeWriteToConsole(t *testing.T) {
|
|
|
|
if !*testLargeWrite {
|
2013-01-24 17:32:10 +11:00
|
|
|
t.Skip("skipping console-flooding test; enable with -large_write")
|
2012-09-19 16:55:21 +10:00
|
|
|
}
|
|
|
|
b := make([]byte, 32000)
|
|
|
|
for i := range b {
|
|
|
|
b[i] = '.'
|
|
|
|
}
|
|
|
|
b[len(b)-1] = '\n'
|
|
|
|
n, err := Stdout.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Write to os.Stdout failed: %v", err)
|
|
|
|
}
|
|
|
|
if n != len(b) {
|
|
|
|
t.Errorf("Write to os.Stdout should return %d; got %d", len(b), n)
|
|
|
|
}
|
|
|
|
n, err = Stderr.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Write to os.Stderr failed: %v", err)
|
|
|
|
}
|
|
|
|
if n != len(b) {
|
|
|
|
t.Errorf("Write to os.Stderr should return %d; got %d", len(b), n)
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 17:01:59 +11:00
|
|
|
|
|
|
|
func TestStatDirModeExec(t *testing.T) {
|
2023-03-25 10:18:26 -07:00
|
|
|
if runtime.GOOS == "wasip1" {
|
|
|
|
t.Skip("Chmod is not supported on " + runtime.GOOS)
|
|
|
|
}
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
2012-11-28 17:01:59 +11:00
|
|
|
const mode = 0111
|
2012-11-30 16:10:45 +11:00
|
|
|
|
2021-04-07 21:27:27 +02:00
|
|
|
path := t.TempDir()
|
2012-11-30 16:10:45 +11:00
|
|
|
if err := Chmod(path, 0777); err != nil {
|
|
|
|
t.Fatalf("Chmod %q 0777: %v", path, err)
|
|
|
|
}
|
|
|
|
|
2012-11-28 17:01:59 +11:00
|
|
|
dir, err := Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Stat %q (looking for mode %#o): %s", path, mode, err)
|
|
|
|
}
|
|
|
|
if dir.Mode()&mode != mode {
|
2012-11-30 16:10:45 +11:00
|
|
|
t.Errorf("Stat %q: mode %#o want %#o", path, dir.Mode()&mode, mode)
|
2012-11-28 17:01:59 +11:00
|
|
|
}
|
|
|
|
}
|
2013-06-10 19:14:41 +10:00
|
|
|
|
2016-03-18 13:00:26 +09:00
|
|
|
func TestStatStdin(t *testing.T) {
|
2016-03-24 19:55:40 +01:00
|
|
|
switch runtime.GOOS {
|
|
|
|
case "android", "plan9":
|
|
|
|
t.Skipf("%s doesn't have /bin/sh", runtime.GOOS)
|
2016-03-18 13:00:26 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
if Getenv("GO_WANT_HELPER_PROCESS") == "1" {
|
|
|
|
st, err := Stdin.Stat()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Stat failed: %v", err)
|
|
|
|
}
|
|
|
|
fmt.Println(st.Mode() & ModeNamedPipe)
|
|
|
|
Exit(0)
|
|
|
|
}
|
|
|
|
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
2024-08-29 19:51:22 -07:00
|
|
|
exe := testenv.Executable(t)
|
2022-12-13 16:04:09 -05:00
|
|
|
|
2016-12-07 10:49:45 +11:00
|
|
|
fi, err := Stdin.Stat()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
switch mode := fi.Mode(); {
|
2017-12-16 10:04:03 +11:00
|
|
|
case mode&ModeCharDevice != 0 && mode&ModeDevice != 0:
|
2016-12-07 10:49:45 +11:00
|
|
|
case mode&ModeNamedPipe != 0:
|
|
|
|
default:
|
|
|
|
t.Fatalf("unexpected Stdin mode (%v), want ModeCharDevice or ModeNamedPipe", mode)
|
|
|
|
}
|
|
|
|
|
2023-09-04 10:35:13 -07:00
|
|
|
cmd := testenv.Command(t, exe, "-test.run=^TestStatStdin$")
|
|
|
|
cmd = testenv.CleanCmdEnv(cmd)
|
|
|
|
cmd.Env = append(cmd.Env, "GO_WANT_HELPER_PROCESS=1")
|
|
|
|
// This will make standard input a pipe.
|
|
|
|
cmd.Stdin = strings.NewReader("output")
|
2016-03-18 13:00:26 +09:00
|
|
|
|
|
|
|
output, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to spawn child process: %v %q", err, string(output))
|
|
|
|
}
|
|
|
|
|
|
|
|
// result will be like "prw-rw-rw"
|
|
|
|
if len(output) < 1 || output[0] != 'p' {
|
|
|
|
t.Fatalf("Child process reports stdin is not pipe '%v'", string(output))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-07 20:32:46 +09:00
|
|
|
func TestStatRelativeSymlink(t *testing.T) {
|
|
|
|
testenv.MustHaveSymlink(t)
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
2017-04-07 20:32:46 +09:00
|
|
|
|
2021-04-07 21:27:27 +02:00
|
|
|
tmpdir := t.TempDir()
|
2017-04-07 20:32:46 +09:00
|
|
|
target := filepath.Join(tmpdir, "target")
|
|
|
|
f, err := Create(target)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
st, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
link := filepath.Join(tmpdir, "link")
|
|
|
|
err = Symlink(filepath.Base(target), link)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
st1, err := Stat(link)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !SameFile(st, st1) {
|
|
|
|
t.Error("Stat doesn't follow relative symlink")
|
|
|
|
}
|
2017-04-12 05:34:36 +09:00
|
|
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
Remove(link)
|
|
|
|
err = Symlink(target[len(filepath.VolumeName(target)):], link)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
st1, err := Stat(link)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !SameFile(st, st1) {
|
|
|
|
t.Error("Stat doesn't follow relative symlink")
|
|
|
|
}
|
|
|
|
}
|
2017-04-07 20:32:46 +09:00
|
|
|
}
|
|
|
|
|
2013-06-10 19:14:41 +10:00
|
|
|
func TestReadAtEOF(t *testing.T) {
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
2024-06-11 17:25:06 -07:00
|
|
|
f := newFile(t)
|
2013-06-10 19:14:41 +10:00
|
|
|
|
|
|
|
_, err := f.ReadAt(make([]byte, 10), 0)
|
|
|
|
switch err {
|
|
|
|
case io.EOF:
|
|
|
|
// all good
|
|
|
|
case nil:
|
|
|
|
t.Fatalf("ReadAt succeeded")
|
|
|
|
default:
|
|
|
|
t.Fatalf("ReadAt failed: %s", err)
|
|
|
|
}
|
|
|
|
}
|
2013-06-11 13:06:38 +10:00
|
|
|
|
2016-10-28 13:01:51 -04:00
|
|
|
func TestLongPath(t *testing.T) {
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
2024-06-11 17:25:06 -07:00
|
|
|
tmpdir := t.TempDir()
|
2016-12-11 08:14:38 -08:00
|
|
|
|
|
|
|
// Test the boundary of 247 and fewer bytes (normal) and 248 and more bytes (adjusted).
|
|
|
|
sizes := []int{247, 248, 249, 400}
|
2016-10-28 13:01:51 -04:00
|
|
|
for len(tmpdir) < 400 {
|
|
|
|
tmpdir += "/dir3456789"
|
|
|
|
}
|
2016-12-11 08:14:38 -08:00
|
|
|
for _, sz := range sizes {
|
|
|
|
t.Run(fmt.Sprintf("length=%d", sz), func(t *testing.T) {
|
|
|
|
sizedTempDir := tmpdir[:sz-1] + "x" // Ensure it does not end with a slash.
|
|
|
|
|
|
|
|
// The various sized runs are for this call to trigger the boundary
|
|
|
|
// condition.
|
|
|
|
if err := MkdirAll(sizedTempDir, 0755); err != nil {
|
|
|
|
t.Fatalf("MkdirAll failed: %v", err)
|
2016-10-28 13:01:51 -04:00
|
|
|
}
|
2016-12-11 08:14:38 -08:00
|
|
|
data := []byte("hello world\n")
|
2023-03-28 14:35:20 -07:00
|
|
|
if err := WriteFile(sizedTempDir+"/foo.txt", data, 0644); err != nil {
|
2020-10-29 14:17:47 -04:00
|
|
|
t.Fatalf("os.WriteFile() failed: %v", err)
|
2016-10-28 13:01:51 -04:00
|
|
|
}
|
2016-12-11 08:14:38 -08:00
|
|
|
if err := Rename(sizedTempDir+"/foo.txt", sizedTempDir+"/bar.txt"); err != nil {
|
|
|
|
t.Fatalf("Rename failed: %v", err)
|
|
|
|
}
|
|
|
|
mtime := time.Now().Truncate(time.Minute)
|
|
|
|
if err := Chtimes(sizedTempDir+"/bar.txt", mtime, mtime); err != nil {
|
|
|
|
t.Fatalf("Chtimes failed: %v", err)
|
|
|
|
}
|
|
|
|
names := []string{"bar.txt"}
|
|
|
|
if testenv.HasSymlink() {
|
|
|
|
if err := Symlink(sizedTempDir+"/bar.txt", sizedTempDir+"/symlink.txt"); err != nil {
|
|
|
|
t.Fatalf("Symlink failed: %v", err)
|
|
|
|
}
|
|
|
|
names = append(names, "symlink.txt")
|
|
|
|
}
|
|
|
|
if testenv.HasLink() {
|
|
|
|
if err := Link(sizedTempDir+"/bar.txt", sizedTempDir+"/link.txt"); err != nil {
|
|
|
|
t.Fatalf("Link failed: %v", err)
|
|
|
|
}
|
|
|
|
names = append(names, "link.txt")
|
|
|
|
}
|
|
|
|
for _, wantSize := range []int64{int64(len(data)), 0} {
|
|
|
|
for _, name := range names {
|
|
|
|
path := sizedTempDir + "/" + name
|
|
|
|
dir, err := Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Stat(%q) failed: %v", path, err)
|
|
|
|
}
|
|
|
|
filesize := size(path, t)
|
|
|
|
if dir.Size() != filesize || filesize != wantSize {
|
|
|
|
t.Errorf("Size(%q) is %d, len(ReadFile()) is %d, want %d", path, dir.Size(), filesize, wantSize)
|
|
|
|
}
|
2023-03-25 10:18:26 -07:00
|
|
|
if runtime.GOOS != "wasip1" { // Chmod is not supported on wasip1
|
|
|
|
err = Chmod(path, dir.Mode())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Chmod(%q) failed: %v", path, err)
|
|
|
|
}
|
2017-06-28 20:09:15 +03:00
|
|
|
}
|
2016-12-11 08:14:38 -08:00
|
|
|
}
|
|
|
|
if err := Truncate(sizedTempDir+"/bar.txt", 0); err != nil {
|
|
|
|
t.Fatalf("Truncate failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2016-10-28 13:01:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-11 13:06:38 +10:00
|
|
|
func testKillProcess(t *testing.T, processKiller func(p *Process)) {
|
2021-01-15 10:16:25 -05:00
|
|
|
t.Parallel()
|
2014-05-20 12:10:19 -04:00
|
|
|
|
2022-02-16 10:24:42 -05:00
|
|
|
// Re-exec the test binary to start a process that hangs until stdin is closed.
|
2024-08-29 19:51:22 -07:00
|
|
|
cmd := testenv.Command(t, testenv.Executable(t))
|
2022-12-13 16:04:09 -05:00
|
|
|
cmd.Env = append(cmd.Environ(), "GO_OS_TEST_DRAIN_STDIN=1")
|
2022-02-16 10:24:42 -05:00
|
|
|
stdout, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
stdin, err := cmd.StdinPipe()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = cmd.Start()
|
2013-06-11 13:06:38 +10:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to start test process: %v", err)
|
|
|
|
}
|
2021-01-15 10:16:25 -05:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := cmd.Wait(); err == nil {
|
|
|
|
t.Errorf("Test process succeeded, but expected to fail")
|
|
|
|
}
|
2022-02-16 10:24:42 -05:00
|
|
|
stdin.Close() // Keep stdin alive until the process has finished dying.
|
2013-06-11 13:06:38 +10:00
|
|
|
}()
|
2021-01-15 10:16:25 -05:00
|
|
|
|
2022-02-16 10:24:42 -05:00
|
|
|
// Wait for the process to be started.
|
|
|
|
// (It will close its stdout when it reaches TestMain.)
|
|
|
|
io.Copy(io.Discard, stdout)
|
2013-06-11 13:06:38 +10:00
|
|
|
|
2022-02-16 10:24:42 -05:00
|
|
|
processKiller(cmd.Process)
|
2015-02-08 07:42:10 -08:00
|
|
|
}
|
|
|
|
|
2013-06-11 13:06:38 +10:00
|
|
|
func TestKillStartProcess(t *testing.T) {
|
|
|
|
testKillProcess(t, func(p *Process) {
|
|
|
|
err := p.Kill()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to kill test process: %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-06-14 15:51:00 +10:00
|
|
|
func TestGetppid(t *testing.T) {
|
2016-05-10 07:43:17 +00:00
|
|
|
if runtime.GOOS == "plan9" {
|
|
|
|
// TODO: golang.org/issue/8206
|
|
|
|
t.Skipf("skipping test on plan9; see issue 8206")
|
|
|
|
}
|
|
|
|
|
2014-06-14 15:51:00 +10:00
|
|
|
if Getenv("GO_WANT_HELPER_PROCESS") == "1" {
|
|
|
|
fmt.Print(Getppid())
|
|
|
|
Exit(0)
|
|
|
|
}
|
|
|
|
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
2024-08-29 19:51:22 -07:00
|
|
|
cmd := testenv.Command(t, testenv.Executable(t), "-test.run=^TestGetppid$")
|
2014-06-14 15:51:00 +10:00
|
|
|
cmd.Env = append(Environ(), "GO_WANT_HELPER_PROCESS=1")
|
|
|
|
|
|
|
|
// verify that Getppid() from the forked process reports our process id
|
|
|
|
output, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to spawn child process: %v %q", err, string(output))
|
|
|
|
}
|
|
|
|
|
|
|
|
childPpid := string(output)
|
|
|
|
ourPid := fmt.Sprintf("%d", Getpid())
|
|
|
|
if childPpid != ourPid {
|
|
|
|
t.Fatalf("Child process reports parent process id '%v', expected '%v'", childPpid, ourPid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-11 13:06:38 +10:00
|
|
|
func TestKillFindProcess(t *testing.T) {
|
|
|
|
testKillProcess(t, func(p *Process) {
|
|
|
|
p2, err := FindProcess(p.Pid)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to find test process: %v", err)
|
|
|
|
}
|
|
|
|
err = p2.Kill()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to kill test process: %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2014-01-04 09:58:04 +11:00
|
|
|
|
|
|
|
var nilFileMethodTests = []struct {
|
|
|
|
name string
|
|
|
|
f func(*File) error
|
|
|
|
}{
|
|
|
|
{"Chdir", func(f *File) error { return f.Chdir() }},
|
|
|
|
{"Close", func(f *File) error { return f.Close() }},
|
|
|
|
{"Chmod", func(f *File) error { return f.Chmod(0) }},
|
|
|
|
{"Chown", func(f *File) error { return f.Chown(0, 0) }},
|
|
|
|
{"Read", func(f *File) error { _, err := f.Read(make([]byte, 0)); return err }},
|
|
|
|
{"ReadAt", func(f *File) error { _, err := f.ReadAt(make([]byte, 0), 0); return err }},
|
|
|
|
{"Readdir", func(f *File) error { _, err := f.Readdir(1); return err }},
|
|
|
|
{"Readdirnames", func(f *File) error { _, err := f.Readdirnames(1); return err }},
|
2016-04-05 11:22:53 -07:00
|
|
|
{"Seek", func(f *File) error { _, err := f.Seek(0, io.SeekStart); return err }},
|
2014-01-04 09:58:04 +11:00
|
|
|
{"Stat", func(f *File) error { _, err := f.Stat(); return err }},
|
|
|
|
{"Sync", func(f *File) error { return f.Sync() }},
|
|
|
|
{"Truncate", func(f *File) error { return f.Truncate(0) }},
|
|
|
|
{"Write", func(f *File) error { _, err := f.Write(make([]byte, 0)); return err }},
|
|
|
|
{"WriteAt", func(f *File) error { _, err := f.WriteAt(make([]byte, 0), 0); return err }},
|
|
|
|
{"WriteString", func(f *File) error { _, err := f.WriteString(""); return err }},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that all File methods give ErrInvalid if the receiver is nil.
|
|
|
|
func TestNilFileMethods(t *testing.T) {
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
2014-01-04 09:58:04 +11:00
|
|
|
for _, tt := range nilFileMethodTests {
|
|
|
|
var file *File
|
|
|
|
got := tt.f(file)
|
|
|
|
if got != ErrInvalid {
|
|
|
|
t.Errorf("%v should fail when f is nil; got %v", tt.name, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-18 14:48:47 -04:00
|
|
|
|
|
|
|
func mkdirTree(t *testing.T, root string, level, max int) {
|
|
|
|
if level >= max {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
level++
|
|
|
|
for i := 'a'; i < 'c'; i++ {
|
|
|
|
dir := filepath.Join(root, string(i))
|
|
|
|
if err := Mkdir(dir, 0700); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
mkdirTree(t, dir, level, max)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that simultaneous RemoveAll do not report an error.
|
|
|
|
// As long as it gets removed, we should be happy.
|
|
|
|
func TestRemoveAllRace(t *testing.T) {
|
2014-09-18 20:13:07 -04:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
// Windows has very strict rules about things like
|
|
|
|
// removing directories while someone else has
|
|
|
|
// them open. The racing doesn't work out nicely
|
|
|
|
// like it does on Unix.
|
|
|
|
t.Skip("skipping on windows")
|
|
|
|
}
|
2022-04-25 11:14:48 -04:00
|
|
|
if runtime.GOOS == "dragonfly" {
|
|
|
|
testenv.SkipFlaky(t, 52301)
|
|
|
|
}
|
2014-09-18 20:13:07 -04:00
|
|
|
|
2014-09-18 14:48:47 -04:00
|
|
|
n := runtime.GOMAXPROCS(16)
|
|
|
|
defer runtime.GOMAXPROCS(n)
|
2024-09-04 17:31:31 -07:00
|
|
|
root := t.TempDir()
|
2014-09-18 14:48:47 -04:00
|
|
|
mkdirTree(t, root, 1, 6)
|
|
|
|
hold := make(chan struct{})
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for i := 0; i < 4; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
<-hold
|
|
|
|
err := RemoveAll(root)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %T, %q", err, err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
close(hold) // let workers race to remove root
|
|
|
|
wg.Wait()
|
|
|
|
}
|
2017-02-10 15:17:38 -08:00
|
|
|
|
|
|
|
// Test that reading from a pipe doesn't use up a thread.
|
|
|
|
func TestPipeThreads(t *testing.T) {
|
|
|
|
switch runtime.GOOS {
|
2024-10-31 11:49:44 -05:00
|
|
|
case "aix":
|
|
|
|
t.Skip("skipping on aix; issue 70131")
|
2019-04-29 13:50:49 +00:00
|
|
|
case "illumos", "solaris":
|
|
|
|
t.Skip("skipping on Solaris and illumos; issue 19111")
|
2017-02-10 15:17:38 -08:00
|
|
|
case "windows":
|
|
|
|
t.Skip("skipping on Windows; issue 19098")
|
2017-02-15 12:32:14 -08:00
|
|
|
case "plan9":
|
|
|
|
t.Skip("skipping on Plan 9; does not support runtime poller")
|
2018-03-04 12:16:18 +01:00
|
|
|
case "js":
|
|
|
|
t.Skip("skipping on js; no support for os.Pipe")
|
2023-03-25 10:18:26 -07:00
|
|
|
case "wasip1":
|
|
|
|
t.Skip("skipping on wasip1; no support for os.Pipe")
|
2017-02-10 15:17:38 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
threads := 100
|
|
|
|
|
|
|
|
r := make([]*File, threads)
|
|
|
|
w := make([]*File, threads)
|
|
|
|
for i := 0; i < threads; i++ {
|
|
|
|
rp, wp, err := Pipe()
|
|
|
|
if err != nil {
|
|
|
|
for j := 0; j < i; j++ {
|
|
|
|
r[j].Close()
|
|
|
|
w[j].Close()
|
|
|
|
}
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
r[i] = rp
|
|
|
|
w[i] = wp
|
|
|
|
}
|
|
|
|
|
|
|
|
defer debug.SetMaxThreads(debug.SetMaxThreads(threads / 2))
|
|
|
|
|
2017-09-13 11:16:40 -07:00
|
|
|
creading := make(chan bool, threads)
|
|
|
|
cdone := make(chan bool, threads)
|
2017-02-10 15:17:38 -08:00
|
|
|
for i := 0; i < threads; i++ {
|
|
|
|
go func(i int) {
|
|
|
|
var b [1]byte
|
2017-09-13 11:16:40 -07:00
|
|
|
creading <- true
|
2017-02-10 15:17:38 -08:00
|
|
|
if _, err := r[i].Read(b[:]); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2017-09-13 11:16:40 -07:00
|
|
|
if err := r[i].Close(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
cdone <- true
|
2017-02-10 15:17:38 -08:00
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < threads; i++ {
|
2017-09-13 11:16:40 -07:00
|
|
|
<-creading
|
2017-02-10 15:17:38 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we are still alive, it means that the 100 goroutines did
|
|
|
|
// not require 100 threads.
|
|
|
|
|
|
|
|
for i := 0; i < threads; i++ {
|
|
|
|
if _, err := w[i].Write([]byte{0}); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if err := w[i].Close(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2017-09-13 11:16:40 -07:00
|
|
|
<-cdone
|
2017-02-10 15:17:38 -08:00
|
|
|
}
|
|
|
|
}
|
2017-04-25 17:47:34 -07:00
|
|
|
|
2022-12-13 16:04:09 -05:00
|
|
|
func testDoubleCloseError(path string) func(*testing.T) {
|
|
|
|
return func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
file, err := Open(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := file.Close(); err != nil {
|
|
|
|
t.Fatalf("unexpected error from Close: %v", err)
|
|
|
|
}
|
|
|
|
if err := file.Close(); err == nil {
|
|
|
|
t.Error("second Close did not fail")
|
|
|
|
} else if pe, ok := err.(*PathError); !ok {
|
|
|
|
t.Errorf("second Close: got %T, want %T", err, pe)
|
|
|
|
} else if pe.Err != ErrClosed {
|
|
|
|
t.Errorf("second Close: got %q, want %q", pe.Err, ErrClosed)
|
|
|
|
} else {
|
|
|
|
t.Logf("second close returned expected error %q", err)
|
|
|
|
}
|
2017-04-25 17:47:34 -07:00
|
|
|
}
|
|
|
|
}
|
2018-10-03 21:21:55 +00:00
|
|
|
|
2019-03-06 08:43:27 +00:00
|
|
|
func TestDoubleCloseError(t *testing.T) {
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
|
|
|
t.Run("file", testDoubleCloseError(filepath.Join(sfdir, sfname)))
|
|
|
|
t.Run("dir", testDoubleCloseError(sfdir))
|
2019-03-06 08:43:27 +00:00
|
|
|
}
|
|
|
|
|
2023-08-16 18:36:46 -06:00
|
|
|
func TestUserCacheDir(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
dir, err := UserCacheDir()
|
|
|
|
if err != nil {
|
|
|
|
t.Skipf("skipping: %v", err)
|
|
|
|
}
|
|
|
|
if dir == "" {
|
|
|
|
t.Fatalf("UserCacheDir returned %q; want non-empty path or error", dir)
|
|
|
|
}
|
|
|
|
|
2024-01-08 17:20:14 -05:00
|
|
|
fi, err := Stat(dir)
|
2023-08-16 18:36:46 -06:00
|
|
|
if err != nil {
|
2024-01-08 17:20:14 -05:00
|
|
|
if IsNotExist(err) {
|
|
|
|
t.Log(err)
|
|
|
|
return
|
|
|
|
}
|
2023-08-16 18:36:46 -06:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-01-08 17:20:14 -05:00
|
|
|
if !fi.IsDir() {
|
|
|
|
t.Fatalf("dir %s is not directory; type = %v", dir, fi.Mode())
|
|
|
|
}
|
2023-08-16 18:36:46 -06:00
|
|
|
}
|
|
|
|
|
2024-07-17 11:44:35 +00:00
|
|
|
func TestUserCacheDirXDGConfigDirEnvVar(t *testing.T) {
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "windows", "darwin", "plan9":
|
|
|
|
t.Skip("$XDG_CACHE_HOME is effective only on Unix systems")
|
|
|
|
}
|
|
|
|
|
|
|
|
wd, err := Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
t.Setenv("XDG_CACHE_HOME", wd)
|
|
|
|
|
|
|
|
dir, err := UserCacheDir()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if dir != wd {
|
|
|
|
t.Fatalf("UserCacheDir returned %q; want the value of $XDG_CACHE_HOME %q", dir, wd)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Setenv("XDG_CACHE_HOME", "some-dir")
|
|
|
|
_, err = UserCacheDir()
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("UserCacheDir succeeded though $XDG_CACHE_HOME contains a relative path")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-16 18:36:46 -06:00
|
|
|
func TestUserConfigDir(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
dir, err := UserConfigDir()
|
|
|
|
if err != nil {
|
|
|
|
t.Skipf("skipping: %v", err)
|
|
|
|
}
|
|
|
|
if dir == "" {
|
|
|
|
t.Fatalf("UserConfigDir returned %q; want non-empty path or error", dir)
|
|
|
|
}
|
|
|
|
|
2024-01-08 17:20:14 -05:00
|
|
|
fi, err := Stat(dir)
|
2023-08-16 18:36:46 -06:00
|
|
|
if err != nil {
|
2024-01-08 17:20:14 -05:00
|
|
|
if IsNotExist(err) {
|
|
|
|
t.Log(err)
|
|
|
|
return
|
|
|
|
}
|
2023-08-16 18:36:46 -06:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-01-08 17:20:14 -05:00
|
|
|
if !fi.IsDir() {
|
|
|
|
t.Fatalf("dir %s is not directory; type = %v", dir, fi.Mode())
|
|
|
|
}
|
2023-08-16 18:36:46 -06:00
|
|
|
}
|
|
|
|
|
2024-07-17 11:44:35 +00:00
|
|
|
func TestUserConfigDirXDGConfigDirEnvVar(t *testing.T) {
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "windows", "darwin", "plan9":
|
|
|
|
t.Skip("$XDG_CONFIG_HOME is effective only on Unix systems")
|
|
|
|
}
|
|
|
|
|
|
|
|
wd, err := Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
t.Setenv("XDG_CONFIG_HOME", wd)
|
|
|
|
|
|
|
|
dir, err := UserConfigDir()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if dir != wd {
|
|
|
|
t.Fatalf("UserConfigDir returned %q; want the value of $XDG_CONFIG_HOME %q", dir, wd)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Setenv("XDG_CONFIG_HOME", "some-dir")
|
|
|
|
_, err = UserConfigDir()
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("UserConfigDir succeeded though $XDG_CONFIG_HOME contains a relative path")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-03 21:21:55 +00:00
|
|
|
func TestUserHomeDir(t *testing.T) {
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
2018-11-20 12:30:14 +01:00
|
|
|
dir, err := UserHomeDir()
|
|
|
|
if dir == "" && err == nil {
|
|
|
|
t.Fatal("UserHomeDir returned an empty string but no error")
|
|
|
|
}
|
|
|
|
if err != nil {
|
2022-12-08 14:10:59 -05:00
|
|
|
// UserHomeDir may return a non-nil error if the environment variable
|
|
|
|
// for the home directory is empty or unset in the environment.
|
|
|
|
t.Skipf("skipping: %v", err)
|
2018-10-03 21:21:55 +00:00
|
|
|
}
|
2022-12-08 14:10:59 -05:00
|
|
|
|
2018-10-03 21:21:55 +00:00
|
|
|
fi, err := Stat(dir)
|
|
|
|
if err != nil {
|
2023-03-28 14:35:20 -07:00
|
|
|
if IsNotExist(err) {
|
2022-12-08 14:10:59 -05:00
|
|
|
// The user's home directory has a well-defined location, but does not
|
|
|
|
// exist. (Maybe nothing has written to it yet? That could happen, for
|
|
|
|
// example, on minimal VM images used for CI testing.)
|
|
|
|
t.Log(err)
|
|
|
|
return
|
|
|
|
}
|
2018-10-03 21:21:55 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !fi.IsDir() {
|
|
|
|
t.Fatalf("dir %s is not directory; type = %v", dir, fi.Mode())
|
|
|
|
}
|
|
|
|
}
|
2019-12-06 16:20:50 +00:00
|
|
|
|
|
|
|
func TestDirSeek(t *testing.T) {
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
2019-12-06 16:20:50 +00:00
|
|
|
wd, err := Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
f, err := Open(wd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
dirnames1, err := f.Readdirnames(0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ret, err := f.Seek(0, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if ret != 0 {
|
|
|
|
t.Fatalf("seek result not zero: %d", ret)
|
|
|
|
}
|
|
|
|
|
|
|
|
dirnames2, err := f.Readdirnames(0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(dirnames1) != len(dirnames2) {
|
|
|
|
t.Fatalf("listings have different lengths: %d and %d\n", len(dirnames1), len(dirnames2))
|
|
|
|
}
|
|
|
|
for i, n1 := range dirnames1 {
|
|
|
|
n2 := dirnames2[i]
|
|
|
|
if n1 != n2 {
|
|
|
|
t.Fatalf("different name i=%d n1=%s n2=%s\n", i, n1, n2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-11 17:49:52 -08:00
|
|
|
|
|
|
|
func TestReaddirSmallSeek(t *testing.T) {
|
|
|
|
// See issue 37161. Read only one entry from a directory,
|
|
|
|
// seek to the beginning, and read again. We should not see
|
|
|
|
// duplicate entries.
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-02-11 17:49:52 -08:00
|
|
|
wd, err := Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
df, err := Open(filepath.Join(wd, "testdata", "issue37161"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
names1, err := df.Readdirnames(1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err = df.Seek(0, 0); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
names2, err := df.Readdirnames(0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(names2) != 3 {
|
|
|
|
t.Fatalf("first names: %v, second names: %v", names1, names2)
|
|
|
|
}
|
|
|
|
}
|
2020-04-17 15:42:12 -07:00
|
|
|
|
2023-03-28 14:35:20 -07:00
|
|
|
// isDeadlineExceeded reports whether err is or wraps ErrDeadlineExceeded.
|
2020-04-17 15:42:12 -07:00
|
|
|
// We also check that the error has a Timeout method that returns true.
|
|
|
|
func isDeadlineExceeded(err error) bool {
|
|
|
|
if !IsTimeout(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !errors.Is(err, ErrDeadlineExceeded) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2020-05-19 20:23:58 -07:00
|
|
|
|
|
|
|
// Test that opening a file does not change its permissions. Issue 38225.
|
|
|
|
func TestOpenFileKeepsPermissions(t *testing.T) {
|
2024-04-23 11:14:19 -07:00
|
|
|
t.Run("OpenFile", func(t *testing.T) {
|
|
|
|
testOpenFileKeepsPermissions(t, OpenFile)
|
|
|
|
})
|
|
|
|
t.Run("RootOpenFile", func(t *testing.T) {
|
|
|
|
testOpenFileKeepsPermissions(t, func(name string, flag int, perm FileMode) (*File, error) {
|
|
|
|
dir, file := filepath.Split(name)
|
|
|
|
r, err := OpenRoot(dir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer r.Close()
|
|
|
|
return r.OpenFile(file, flag, perm)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
func testOpenFileKeepsPermissions(t *testing.T, openf func(name string, flag int, perm FileMode) (*File, error)) {
|
2020-05-19 20:23:58 -07:00
|
|
|
t.Parallel()
|
2022-12-13 16:04:09 -05:00
|
|
|
|
2020-05-19 20:23:58 -07:00
|
|
|
dir := t.TempDir()
|
|
|
|
name := filepath.Join(dir, "x")
|
|
|
|
f, err := Create(name)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2024-04-23 11:14:19 -07:00
|
|
|
f, err = openf(name, O_WRONLY|O_CREATE|O_TRUNC, 0)
|
2020-05-19 20:23:58 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if fi, err := f.Stat(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else if fi.Mode()&0222 == 0 {
|
|
|
|
t.Errorf("f.Stat.Mode after OpenFile is %v, should be writable", fi.Mode())
|
|
|
|
}
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if fi, err := Stat(name); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else if fi.Mode()&0222 == 0 {
|
|
|
|
t.Errorf("Stat after OpenFile is %v, should be writable", fi.Mode())
|
|
|
|
}
|
|
|
|
}
|
2020-07-06 11:27:38 -04:00
|
|
|
|
2024-01-27 12:52:56 +08:00
|
|
|
func forceMFTUpdateOnWindows(t *testing.T, path string) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
return
|
|
|
|
}
|
2022-12-13 16:04:09 -05:00
|
|
|
|
2021-01-18 15:23:16 +01:00
|
|
|
// On Windows, we force the MFT to update by reading the actual metadata from GetFileInformationByHandle and then
|
|
|
|
// explicitly setting that. Otherwise it might get out of sync with FindFirstFile. See golang.org/issues/42637.
|
2024-01-27 12:52:56 +08:00
|
|
|
if err := filepath.WalkDir(path, func(path string, d fs.DirEntry, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
info, err := d.Info()
|
|
|
|
if err != nil {
|
2021-01-18 15:23:16 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-01-27 12:52:56 +08:00
|
|
|
stat, err := Stat(path) // This uses GetFileInformationByHandle internally.
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if stat.ModTime() == info.ModTime() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err := Chtimes(path, stat.ModTime(), stat.ModTime()); err != nil {
|
|
|
|
t.Log(err) // We only log, not die, in case the test directory is not writable.
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
2021-01-18 15:23:16 +01:00
|
|
|
}
|
2024-01-27 12:52:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDirFS(t *testing.T) {
|
|
|
|
t.Parallel()
|
2024-11-18 17:49:14 -08:00
|
|
|
testDirFS(t, DirFS("./testdata/dirfs"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRootDirFS(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
r, err := OpenRoot("./testdata/dirfs")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
testDirFS(t, r.FS())
|
|
|
|
}
|
2024-01-27 12:52:56 +08:00
|
|
|
|
2024-11-18 17:49:14 -08:00
|
|
|
func testDirFS(t *testing.T, fsys fs.FS) {
|
2024-01-27 12:52:56 +08:00
|
|
|
forceMFTUpdateOnWindows(t, "./testdata/dirfs")
|
|
|
|
|
2023-05-24 19:49:24 +02:00
|
|
|
if err := fstest.TestFS(fsys, "a", "b", "dir/x"); err != nil {
|
2020-07-06 11:27:38 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-02-09 13:46:53 -05:00
|
|
|
|
2023-05-24 19:49:24 +02:00
|
|
|
rdfs, ok := fsys.(fs.ReadDirFS)
|
|
|
|
if !ok {
|
|
|
|
t.Error("expected DirFS result to implement fs.ReadDirFS")
|
|
|
|
}
|
|
|
|
if _, err := rdfs.ReadDir("nonexistent"); err == nil {
|
2023-06-13 23:01:11 +00:00
|
|
|
t.Error("fs.ReadDir of nonexistent directory succeeded")
|
2023-05-24 19:49:24 +02:00
|
|
|
}
|
|
|
|
|
2022-10-27 17:29:51 -07:00
|
|
|
// Test that the error message does not contain a backslash,
|
|
|
|
// and does not contain the DirFS argument.
|
2022-10-03 20:08:11 -07:00
|
|
|
const nonesuch = "dir/nonesuch"
|
2023-05-24 19:49:24 +02:00
|
|
|
_, err := fsys.Open(nonesuch)
|
2022-10-03 20:08:11 -07:00
|
|
|
if err == nil {
|
|
|
|
t.Error("fs.Open of nonexistent file succeeded")
|
|
|
|
} else {
|
|
|
|
if !strings.Contains(err.Error(), nonesuch) {
|
|
|
|
t.Errorf("error %q does not contain %q", err, nonesuch)
|
|
|
|
}
|
2022-10-31 13:41:59 -07:00
|
|
|
if strings.Contains(err.(*PathError).Path, "testdata") {
|
2022-10-27 17:29:51 -07:00
|
|
|
t.Errorf("error %q contains %q", err, "testdata")
|
|
|
|
}
|
2022-10-03 20:08:11 -07:00
|
|
|
}
|
|
|
|
|
2021-02-09 13:46:53 -05:00
|
|
|
// Test that Open does not accept backslash as separator.
|
|
|
|
d := DirFS(".")
|
2022-10-03 20:08:11 -07:00
|
|
|
_, err = d.Open(`testdata\dirfs`)
|
2021-02-09 13:46:53 -05:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf(`Open testdata\dirfs succeeded`)
|
|
|
|
}
|
2022-11-10 12:16:27 -08:00
|
|
|
|
|
|
|
// Test that Open does not open Windows device files.
|
|
|
|
_, err = d.Open(`NUL`)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf(`Open NUL succeeded`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDirFSRootDir(t *testing.T) {
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
2023-03-28 14:35:20 -07:00
|
|
|
cwd, err := Getwd()
|
2022-11-10 12:16:27 -08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
cwd = cwd[len(filepath.VolumeName(cwd)):] // trim volume prefix (C:) on Windows
|
|
|
|
cwd = filepath.ToSlash(cwd) // convert \ to /
|
|
|
|
cwd = strings.TrimPrefix(cwd, "/") // trim leading /
|
|
|
|
|
|
|
|
// Test that Open can open a path starting at /.
|
|
|
|
d := DirFS("/")
|
|
|
|
f, err := d.Open(cwd + "/testdata/dirfs/a")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDirFSEmptyDir(t *testing.T) {
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-11-10 12:16:27 -08:00
|
|
|
d := DirFS("")
|
2023-03-28 14:35:20 -07:00
|
|
|
cwd, _ := Getwd()
|
2022-11-10 12:16:27 -08:00
|
|
|
for _, path := range []string{
|
|
|
|
"testdata/dirfs/a", // not DirFS(".")
|
|
|
|
filepath.ToSlash(cwd) + "/testdata/dirfs/a", // not DirFS("/")
|
|
|
|
} {
|
|
|
|
_, err := d.Open(path)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf(`DirFS("").Open(%q) succeeded`, path)
|
|
|
|
}
|
|
|
|
}
|
2021-02-09 13:46:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDirFSPathsValid(t *testing.T) {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skipf("skipping on Windows")
|
|
|
|
}
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
2021-02-09 13:46:53 -05:00
|
|
|
|
|
|
|
d := t.TempDir()
|
2023-03-28 14:35:20 -07:00
|
|
|
if err := WriteFile(filepath.Join(d, "control.txt"), []byte(string("Hello, world!")), 0644); err != nil {
|
2021-02-09 13:46:53 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2023-03-28 14:35:20 -07:00
|
|
|
if err := WriteFile(filepath.Join(d, `e:xperi\ment.txt`), []byte(string("Hello, colon and backslash!")), 0644); err != nil {
|
2021-02-09 13:46:53 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-03-28 14:35:20 -07:00
|
|
|
fsys := DirFS(d)
|
2021-02-09 13:46:53 -05:00
|
|
|
err := fs.WalkDir(fsys, ".", func(path string, e fs.DirEntry, err error) error {
|
|
|
|
if fs.ValidPath(e.Name()) {
|
|
|
|
t.Logf("%q ok", e.Name())
|
|
|
|
} else {
|
|
|
|
t.Errorf("%q INVALID", e.Name())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-07-06 11:27:38 -04:00
|
|
|
}
|
2020-10-29 13:51:20 -04:00
|
|
|
|
|
|
|
func TestReadFileProc(t *testing.T) {
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-10-29 13:51:20 -04:00
|
|
|
// Linux files in /proc report 0 size,
|
|
|
|
// but then if ReadFile reads just a single byte at offset 0,
|
|
|
|
// the read at offset 1 returns EOF instead of more data.
|
|
|
|
// ReadFile has a minimum read size of 512 to work around this,
|
|
|
|
// but test explicitly that it's working.
|
|
|
|
name := "/proc/sys/fs/pipe-max-size"
|
|
|
|
if _, err := Stat(name); err != nil {
|
|
|
|
t.Skip(err)
|
|
|
|
}
|
|
|
|
data, err := ReadFile(name)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(data) == 0 || data[len(data)-1] != '\n' {
|
|
|
|
t.Fatalf("read %s: not newline-terminated: %q", name, data)
|
|
|
|
}
|
|
|
|
}
|
2020-11-06 17:37:03 -08:00
|
|
|
|
2022-07-09 19:08:59 +01:00
|
|
|
func TestDirFSReadFileProc(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
fsys := DirFS("/")
|
|
|
|
name := "proc/sys/fs/pipe-max-size"
|
|
|
|
if _, err := fs.Stat(fsys, name); err != nil {
|
|
|
|
t.Skip()
|
|
|
|
}
|
|
|
|
data, err := fs.ReadFile(fsys, name)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(data) == 0 || data[len(data)-1] != '\n' {
|
|
|
|
t.Fatalf("read %s: not newline-terminated: %q", name, data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-06 17:37:03 -08:00
|
|
|
func TestWriteStringAlloc(t *testing.T) {
|
|
|
|
if runtime.GOOS == "js" {
|
|
|
|
t.Skip("js allocates a lot during File.WriteString")
|
|
|
|
}
|
|
|
|
d := t.TempDir()
|
|
|
|
f, err := Create(filepath.Join(d, "whiteboard.txt"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
allocs := testing.AllocsPerRun(100, func() {
|
|
|
|
f.WriteString("I will not allocate when passed a string longer than 32 bytes.\n")
|
|
|
|
})
|
|
|
|
if allocs != 0 {
|
|
|
|
t.Errorf("expected 0 allocs for File.WriteString, got %v", allocs)
|
|
|
|
}
|
|
|
|
}
|
2022-10-04 14:35:39 -07:00
|
|
|
|
|
|
|
// Test that it's OK to have parallel I/O and Close on a pipe.
|
|
|
|
func TestPipeIOCloseRace(t *testing.T) {
|
|
|
|
// Skip on wasm, which doesn't have pipes.
|
2023-03-25 10:18:26 -07:00
|
|
|
if runtime.GOOS == "js" || runtime.GOOS == "wasip1" {
|
|
|
|
t.Skipf("skipping on %s: no pipes", runtime.GOOS)
|
2022-10-04 14:35:39 -07:00
|
|
|
}
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
2022-10-04 14:35:39 -07:00
|
|
|
|
|
|
|
r, w, err := Pipe()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(3)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
for {
|
|
|
|
n, err := w.Write([]byte("hi"))
|
|
|
|
if err != nil {
|
|
|
|
// We look at error strings as the
|
|
|
|
// expected errors are OS-specific.
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, ErrClosed),
|
|
|
|
strings.Contains(err.Error(), "broken pipe"),
|
|
|
|
strings.Contains(err.Error(), "pipe is being closed"),
|
|
|
|
strings.Contains(err.Error(), "hungup channel"):
|
|
|
|
// Ignore an expected error.
|
|
|
|
default:
|
|
|
|
// Unexpected error.
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if n != 2 {
|
|
|
|
t.Errorf("wrote %d bytes, expected 2", n)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
for {
|
|
|
|
var buf [2]byte
|
|
|
|
n, err := r.Read(buf[:])
|
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF && !errors.Is(err, ErrClosed) {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if n != 2 {
|
|
|
|
t.Errorf("read %d bytes, want 2", n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
// Let the other goroutines start. This is just to get
|
|
|
|
// a better test, the test will still pass if they
|
|
|
|
// don't start.
|
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
|
|
|
|
if err := r.Close(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if err := w.Close(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that it's OK to call Close concurrently on a pipe.
|
|
|
|
func TestPipeCloseRace(t *testing.T) {
|
|
|
|
// Skip on wasm, which doesn't have pipes.
|
2023-03-25 10:18:26 -07:00
|
|
|
if runtime.GOOS == "js" || runtime.GOOS == "wasip1" {
|
|
|
|
t.Skipf("skipping on %s: no pipes", runtime.GOOS)
|
2022-10-04 14:35:39 -07:00
|
|
|
}
|
2022-12-13 16:04:09 -05:00
|
|
|
t.Parallel()
|
2022-10-04 14:35:39 -07:00
|
|
|
|
|
|
|
r, w, err := Pipe()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
c := make(chan error, 4)
|
|
|
|
f := func() {
|
|
|
|
defer wg.Done()
|
|
|
|
c <- r.Close()
|
|
|
|
c <- w.Close()
|
|
|
|
}
|
|
|
|
wg.Add(2)
|
|
|
|
go f()
|
|
|
|
go f()
|
|
|
|
nils, errs := 0, 0
|
|
|
|
for i := 0; i < 4; i++ {
|
|
|
|
err := <-c
|
|
|
|
if err == nil {
|
|
|
|
nils++
|
|
|
|
} else {
|
|
|
|
errs++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if nils != 2 || errs != 2 {
|
|
|
|
t.Errorf("got nils %d errs %d, want 2 2", nils, errs)
|
|
|
|
}
|
|
|
|
}
|
2023-12-09 14:32:10 -05:00
|
|
|
|
|
|
|
func TestRandomLen(t *testing.T) {
|
|
|
|
for range 5 {
|
|
|
|
dir, err := MkdirTemp(t.TempDir(), "*")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
base := filepath.Base(dir)
|
|
|
|
if len(base) > 10 {
|
|
|
|
t.Errorf("MkdirTemp returned len %d: %s", len(base), base)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for range 5 {
|
|
|
|
f, err := CreateTemp(t.TempDir(), "*")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
base := filepath.Base(f.Name())
|
|
|
|
f.Close()
|
|
|
|
if len(base) > 10 {
|
|
|
|
t.Errorf("CreateTemp returned len %d: %s", len(base), base)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-27 12:52:56 +08:00
|
|
|
|
|
|
|
func TestCopyFS(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// Test with disk filesystem.
|
|
|
|
forceMFTUpdateOnWindows(t, "./testdata/dirfs")
|
|
|
|
fsys := DirFS("./testdata/dirfs")
|
|
|
|
tmpDir := t.TempDir()
|
|
|
|
if err := CopyFS(tmpDir, fsys); err != nil {
|
|
|
|
t.Fatal("CopyFS:", err)
|
|
|
|
}
|
|
|
|
forceMFTUpdateOnWindows(t, tmpDir)
|
|
|
|
tmpFsys := DirFS(tmpDir)
|
|
|
|
if err := fstest.TestFS(tmpFsys, "a", "b", "dir/x"); err != nil {
|
|
|
|
t.Fatal("TestFS:", err)
|
|
|
|
}
|
2024-09-30 13:53:09 +08:00
|
|
|
if err := verifyCopyFS(t, fsys, tmpFsys); err != nil {
|
2024-01-27 12:52:56 +08:00
|
|
|
t.Fatal("comparing two directories:", err)
|
|
|
|
}
|
|
|
|
|
2024-08-16 08:04:57 +08:00
|
|
|
// Test whether CopyFS disallows copying for disk filesystem when there is any
|
|
|
|
// existing file in the destination directory.
|
|
|
|
if err := CopyFS(tmpDir, fsys); !errors.Is(err, fs.ErrExist) {
|
|
|
|
t.Errorf("CopyFS should have failed and returned error when there is"+
|
|
|
|
"any existing file in the destination directory (in disk filesystem), "+
|
|
|
|
"got: %v, expected any error that indicates <file exists>", err)
|
|
|
|
}
|
|
|
|
|
2024-01-27 12:52:56 +08:00
|
|
|
// Test with memory filesystem.
|
|
|
|
fsys = fstest.MapFS{
|
|
|
|
"william": {Data: []byte("Shakespeare\n")},
|
|
|
|
"carl": {Data: []byte("Gauss\n")},
|
|
|
|
"daVinci": {Data: []byte("Leonardo\n")},
|
|
|
|
"einstein": {Data: []byte("Albert\n")},
|
|
|
|
"dir/newton": {Data: []byte("Sir Isaac\n")},
|
|
|
|
}
|
|
|
|
tmpDir = t.TempDir()
|
|
|
|
if err := CopyFS(tmpDir, fsys); err != nil {
|
|
|
|
t.Fatal("CopyFS:", err)
|
|
|
|
}
|
|
|
|
forceMFTUpdateOnWindows(t, tmpDir)
|
|
|
|
tmpFsys = DirFS(tmpDir)
|
|
|
|
if err := fstest.TestFS(tmpFsys, "william", "carl", "daVinci", "einstein", "dir/newton"); err != nil {
|
|
|
|
t.Fatal("TestFS:", err)
|
|
|
|
}
|
2024-09-30 13:53:09 +08:00
|
|
|
if err := verifyCopyFS(t, fsys, tmpFsys); err != nil {
|
|
|
|
t.Fatal("comparing two directories:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test whether CopyFS disallows copying for memory filesystem when there is any
|
|
|
|
// existing file in the destination directory.
|
|
|
|
if err := CopyFS(tmpDir, fsys); !errors.Is(err, fs.ErrExist) {
|
|
|
|
t.Errorf("CopyFS should have failed and returned error when there is"+
|
|
|
|
"any existing file in the destination directory (in memory filesystem), "+
|
|
|
|
"got: %v, expected any error that indicates <file exists>", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// verifyCopyFS checks the content and permission of each file inside copied FS to ensure
|
|
|
|
// the copied files satisfy the convention stipulated in CopyFS.
|
|
|
|
func verifyCopyFS(t *testing.T, originFS, copiedFS fs.FS) error {
|
|
|
|
testDir := filepath.Join(t.TempDir(), "test")
|
|
|
|
// umask doesn't apply to the wasip and windows and there is no general way to get masked perm,
|
|
|
|
// so create a dir and a file to compare the permission after umask if any
|
|
|
|
if err := Mkdir(testDir, ModePerm); err != nil {
|
|
|
|
return fmt.Errorf("mkdir %q failed: %v", testDir, err)
|
|
|
|
}
|
|
|
|
dirStat, err := Stat(testDir)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("stat dir %q failed: %v", testDir, err)
|
|
|
|
}
|
|
|
|
wantDirMode := dirStat.Mode()
|
|
|
|
|
|
|
|
f, err := Create(filepath.Join(testDir, "tmp"))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("open %q failed: %v", filepath.Join(testDir, "tmp"), err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
wantFileRWStat, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("stat file %q failed: %v", f.Name(), err)
|
|
|
|
}
|
2024-10-09 14:32:21 -07:00
|
|
|
wantFileRWMode := wantFileRWStat.Mode()
|
2024-09-30 13:53:09 +08:00
|
|
|
|
|
|
|
return fs.WalkDir(originFS, ".", func(path string, d fs.DirEntry, err error) error {
|
2024-01-27 12:52:56 +08:00
|
|
|
if d.IsDir() {
|
2024-09-30 13:53:09 +08:00
|
|
|
// the dir . is not the dir created by CopyFS so skip checking its permission
|
|
|
|
if d.Name() == "." {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
dinfo, err := fs.Stat(copiedFS, path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if dinfo.Mode() != wantDirMode {
|
|
|
|
return fmt.Errorf("dir %q mode is %v, want %v",
|
|
|
|
d.Name(), dinfo.Mode(), wantDirMode)
|
|
|
|
}
|
2024-01-27 12:52:56 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-30 13:53:09 +08:00
|
|
|
fInfo, err := originFS.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fInfo.Close()
|
|
|
|
copiedInfo, err := copiedFS.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer copiedInfo.Close()
|
|
|
|
|
|
|
|
// verify the file contents are the same
|
|
|
|
data, err := io.ReadAll(fInfo)
|
2024-01-27 12:52:56 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-09-30 13:53:09 +08:00
|
|
|
newData, err := io.ReadAll(copiedInfo)
|
2024-01-27 12:52:56 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !bytes.Equal(data, newData) {
|
2024-09-30 13:53:09 +08:00
|
|
|
return fmt.Errorf("file %q content is %s, want %s", path, newData, data)
|
2024-01-27 12:52:56 +08:00
|
|
|
}
|
2024-08-16 08:04:57 +08:00
|
|
|
|
2024-09-30 13:53:09 +08:00
|
|
|
fStat, err := fInfo.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
copiedStat, err := copiedInfo.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// check whether the execute permission is inherited from original FS
|
2024-10-09 14:32:21 -07:00
|
|
|
|
|
|
|
if copiedStat.Mode()&0111&wantFileRWMode != fStat.Mode()&0111&wantFileRWMode {
|
2024-09-30 13:53:09 +08:00
|
|
|
return fmt.Errorf("file %q execute mode is %v, want %v",
|
|
|
|
path, copiedStat.Mode()&0111, fStat.Mode()&0111)
|
|
|
|
}
|
|
|
|
|
|
|
|
rwMode := copiedStat.Mode() &^ 0111 // unset the executable permission from file mode
|
2024-10-09 14:32:21 -07:00
|
|
|
if rwMode != wantFileRWMode {
|
2024-09-30 13:53:09 +08:00
|
|
|
return fmt.Errorf("file %q rw mode is %v, want %v",
|
|
|
|
path, rwMode, wantFileRWStat.Mode())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2024-01-27 12:52:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCopyFSWithSymlinks(t *testing.T) {
|
|
|
|
// Test it with absolute and relative symlinks that point inside and outside the tree.
|
|
|
|
testenv.MustHaveSymlink(t)
|
|
|
|
|
|
|
|
// Create a directory and file outside.
|
|
|
|
tmpDir := t.TempDir()
|
2024-09-04 17:31:56 -07:00
|
|
|
outsideDir := filepath.Join(tmpDir, "copyfs_out")
|
|
|
|
if err := Mkdir(outsideDir, 0755); err != nil {
|
|
|
|
t.Fatalf("Mkdir: %v", err)
|
2024-01-27 12:52:56 +08:00
|
|
|
}
|
|
|
|
outsideFile := filepath.Join(outsideDir, "file.out.txt")
|
|
|
|
|
|
|
|
if err := WriteFile(outsideFile, []byte("Testing CopyFS outside"), 0644); err != nil {
|
|
|
|
t.Fatalf("WriteFile: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a directory and file inside.
|
2024-09-04 17:31:56 -07:00
|
|
|
insideDir := filepath.Join(tmpDir, "copyfs_in")
|
|
|
|
if err := Mkdir(insideDir, 0755); err != nil {
|
|
|
|
t.Fatalf("Mkdir: %v", err)
|
2024-01-27 12:52:56 +08:00
|
|
|
}
|
|
|
|
insideFile := filepath.Join(insideDir, "file.in.txt")
|
|
|
|
if err := WriteFile(insideFile, []byte("Testing CopyFS inside"), 0644); err != nil {
|
|
|
|
t.Fatalf("WriteFile: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create directories for symlinks.
|
|
|
|
linkInDir := filepath.Join(insideDir, "in_symlinks")
|
|
|
|
if err := Mkdir(linkInDir, 0755); err != nil {
|
|
|
|
t.Fatalf("Mkdir: %v", err)
|
|
|
|
}
|
|
|
|
linkOutDir := filepath.Join(insideDir, "out_symlinks")
|
|
|
|
if err := Mkdir(linkOutDir, 0755); err != nil {
|
|
|
|
t.Fatalf("Mkdir: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// First, we create the absolute symlink pointing outside.
|
|
|
|
outLinkFile := filepath.Join(linkOutDir, "file.abs.out.link")
|
|
|
|
if err := Symlink(outsideFile, outLinkFile); err != nil {
|
|
|
|
t.Fatalf("Symlink: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then, we create the relative symlink pointing outside.
|
|
|
|
relOutsideFile, err := filepath.Rel(filepath.Join(linkOutDir, "."), outsideFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("filepath.Rel: %v", err)
|
|
|
|
}
|
|
|
|
relOutLinkFile := filepath.Join(linkOutDir, "file.rel.out.link")
|
|
|
|
if err := Symlink(relOutsideFile, relOutLinkFile); err != nil {
|
|
|
|
t.Fatalf("Symlink: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Last, we create the relative symlink pointing inside.
|
|
|
|
relInsideFile, err := filepath.Rel(filepath.Join(linkInDir, "."), insideFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("filepath.Rel: %v", err)
|
|
|
|
}
|
|
|
|
relInLinkFile := filepath.Join(linkInDir, "file.rel.in.link")
|
|
|
|
if err := Symlink(relInsideFile, relInLinkFile); err != nil {
|
|
|
|
t.Fatalf("Symlink: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the directory tree and verify.
|
|
|
|
forceMFTUpdateOnWindows(t, insideDir)
|
|
|
|
fsys := DirFS(insideDir)
|
2024-09-04 17:31:56 -07:00
|
|
|
tmpDupDir := filepath.Join(tmpDir, "copyfs_dup")
|
|
|
|
if err := Mkdir(tmpDupDir, 0755); err != nil {
|
|
|
|
t.Fatalf("Mkdir: %v", err)
|
2024-01-27 12:52:56 +08:00
|
|
|
}
|
|
|
|
|
2024-07-16 10:21:30 -07:00
|
|
|
if err := CopyFS(tmpDupDir, fsys); err != nil {
|
|
|
|
t.Fatalf("CopyFS: %v", err)
|
2024-01-27 12:52:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
forceMFTUpdateOnWindows(t, tmpDupDir)
|
|
|
|
tmpFsys := DirFS(tmpDupDir)
|
|
|
|
if err := fstest.TestFS(tmpFsys, "file.in.txt", "out_symlinks/file.abs.out.link", "out_symlinks/file.rel.out.link", "in_symlinks/file.rel.in.link"); err != nil {
|
|
|
|
t.Fatal("TestFS:", err)
|
|
|
|
}
|
|
|
|
if err := fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error {
|
|
|
|
if d.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
fi, err := d.Info()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if filepath.Ext(path) == ".link" {
|
|
|
|
if fi.Mode()&ModeSymlink == 0 {
|
|
|
|
return errors.New("original file " + path + " should be a symlink")
|
|
|
|
}
|
|
|
|
tmpfi, err := fs.Stat(tmpFsys, path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if tmpfi.Mode()&ModeSymlink != 0 {
|
|
|
|
return errors.New("copied file " + path + " should not be a symlink")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := fs.ReadFile(fsys, path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
newData, err := fs.ReadFile(tmpFsys, path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !bytes.Equal(data, newData) {
|
|
|
|
return errors.New("file " + path + " contents differ")
|
|
|
|
}
|
|
|
|
|
|
|
|
var target string
|
|
|
|
switch fileName := filepath.Base(path); fileName {
|
|
|
|
case "file.abs.out.link", "file.rel.out.link":
|
|
|
|
target = outsideFile
|
|
|
|
case "file.rel.in.link":
|
|
|
|
target = insideFile
|
|
|
|
}
|
|
|
|
if len(target) > 0 {
|
|
|
|
targetData, err := ReadFile(target)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !bytes.Equal(targetData, newData) {
|
|
|
|
return errors.New("file " + path + " contents differ from target")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal("comparing two directories:", err)
|
|
|
|
}
|
|
|
|
}
|
2024-10-16 16:38:37 +02:00
|
|
|
|
|
|
|
func TestAppendDoesntOverwrite(t *testing.T) {
|
2024-04-23 11:14:19 -07:00
|
|
|
testMaybeRooted(t, func(t *testing.T, r *Root) {
|
|
|
|
name := "file"
|
|
|
|
if err := WriteFile(name, []byte("hello"), 0666); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
var f *File
|
|
|
|
var err error
|
|
|
|
if r == nil {
|
|
|
|
f, err = OpenFile(name, O_APPEND|O_WRONLY, 0)
|
|
|
|
} else {
|
|
|
|
f, err = r.OpenFile(name, O_APPEND|O_WRONLY, 0)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := f.Write([]byte(" world")); err != nil {
|
|
|
|
f.Close()
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
got, err := ReadFile(name)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
want := "hello world"
|
|
|
|
if string(got) != want {
|
|
|
|
t.Fatalf("got %q, want %q", got, want)
|
|
|
|
}
|
|
|
|
})
|
2024-10-16 16:38:37 +02:00
|
|
|
}
|
2024-11-12 17:16:10 +01:00
|
|
|
|
|
|
|
func TestRemoveReadOnlyFile(t *testing.T) {
|
|
|
|
testMaybeRooted(t, func(t *testing.T, r *Root) {
|
|
|
|
if err := WriteFile("file", []byte("1"), 0); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
if r == nil {
|
|
|
|
err = Remove("file")
|
|
|
|
} else {
|
|
|
|
err = r.Remove("file")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Remove read-only file: %v", err)
|
|
|
|
}
|
|
|
|
if _, err := Stat("file"); !IsNotExist(err) {
|
|
|
|
t.Fatalf("Stat read-only file after removal: %v (want IsNotExist)", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|