2010-12-03 05:34:57 +01:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package os_test
|
|
|
|
|
|
|
|
import (
|
2018-01-09 02:23:08 +01:00
|
|
|
"fmt"
|
2017-01-14 01:05:42 +01:00
|
|
|
"internal/testenv"
|
2012-10-23 06:31:11 +02:00
|
|
|
"io/ioutil"
|
2010-12-03 05:34:57 +01:00
|
|
|
. "os"
|
2011-09-16 17:47:21 +02:00
|
|
|
"path/filepath"
|
2011-01-21 19:19:03 +01:00
|
|
|
"runtime"
|
2012-03-02 21:01:37 +01:00
|
|
|
"syscall"
|
2011-12-07 02:11:29 +01:00
|
|
|
"testing"
|
2010-12-03 05:34:57 +01:00
|
|
|
)
|
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
var isReadonlyError = func(error) bool { return false }
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
func TestMkdirAll(t *testing.T) {
|
2012-03-02 21:01:37 +01:00
|
|
|
tmpDir := TempDir()
|
|
|
|
path := tmpDir + "/_TestMkdirAll_/dir/./dir2"
|
2010-12-03 05:34:57 +01:00
|
|
|
err := MkdirAll(path, 0777)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("MkdirAll %q: %s", path, err)
|
|
|
|
}
|
2012-03-02 21:01:37 +01:00
|
|
|
defer RemoveAll(tmpDir + "/_TestMkdirAll_")
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
// Already exists, should succeed.
|
|
|
|
err = MkdirAll(path, 0777)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("MkdirAll %q (second time): %s", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make file.
|
|
|
|
fpath := path + "/file"
|
2011-09-16 17:47:21 +02:00
|
|
|
f, err := Create(fpath)
|
2010-12-03 05:34:57 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("create %q: %s", fpath, err)
|
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
defer f.Close()
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
// Can't make directory named after file.
|
|
|
|
err = MkdirAll(fpath, 0777)
|
|
|
|
if err == nil {
|
2011-01-21 19:19:03 +01:00
|
|
|
t.Fatalf("MkdirAll %q: no error", fpath)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
perr, ok := err.(*PathError)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("MkdirAll %q returned %T, not *PathError", fpath, err)
|
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
if filepath.Clean(perr.Path) != filepath.Clean(fpath) {
|
|
|
|
t.Fatalf("MkdirAll %q returned wrong error path: %q not %q", fpath, filepath.Clean(perr.Path), filepath.Clean(fpath))
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Can't make subdirectory of file.
|
|
|
|
ffpath := fpath + "/subdir"
|
|
|
|
err = MkdirAll(ffpath, 0777)
|
|
|
|
if err == nil {
|
2011-01-21 19:19:03 +01:00
|
|
|
t.Fatalf("MkdirAll %q: no error", ffpath)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
perr, ok = err.(*PathError)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("MkdirAll %q returned %T, not *PathError", ffpath, err)
|
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
if filepath.Clean(perr.Path) != filepath.Clean(fpath) {
|
|
|
|
t.Fatalf("MkdirAll %q returned wrong error path: %q not %q", ffpath, filepath.Clean(perr.Path), filepath.Clean(fpath))
|
|
|
|
}
|
|
|
|
|
2012-01-25 21:56:26 +01:00
|
|
|
if runtime.GOOS == "windows" {
|
2012-03-02 21:01:37 +01:00
|
|
|
path := tmpDir + `\_TestMkdirAll_\dir\.\dir2\`
|
2011-09-16 17:47:21 +02:00
|
|
|
err := MkdirAll(path, 0777)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("MkdirAll %q: %s", path, err)
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoveAll(t *testing.T) {
|
2012-03-02 21:01:37 +01:00
|
|
|
tmpDir := TempDir()
|
2010-12-03 05:34:57 +01:00
|
|
|
// Work directory.
|
2012-03-02 21:01:37 +01:00
|
|
|
path := tmpDir + "/_TestRemoveAll_"
|
2010-12-03 05:34:57 +01:00
|
|
|
fpath := path + "/file"
|
|
|
|
dpath := path + "/dir"
|
|
|
|
|
|
|
|
// Make directory with 1 file and remove.
|
|
|
|
if err := MkdirAll(path, 0777); err != nil {
|
|
|
|
t.Fatalf("MkdirAll %q: %s", path, err)
|
|
|
|
}
|
2011-04-07 19:09:10 +02:00
|
|
|
fd, err := Create(fpath)
|
2010-12-03 05:34:57 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("create %q: %s", fpath, err)
|
|
|
|
}
|
|
|
|
fd.Close()
|
|
|
|
if err = RemoveAll(path); err != nil {
|
|
|
|
t.Fatalf("RemoveAll %q (first): %s", path, err)
|
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
if _, err = Lstat(path); err == nil {
|
2010-12-03 05:34:57 +01:00
|
|
|
t.Fatalf("Lstat %q succeeded after RemoveAll (first)", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make directory with file and subdirectory and remove.
|
|
|
|
if err = MkdirAll(dpath, 0777); err != nil {
|
|
|
|
t.Fatalf("MkdirAll %q: %s", dpath, err)
|
|
|
|
}
|
2011-04-07 19:09:10 +02:00
|
|
|
fd, err = Create(fpath)
|
2010-12-03 05:34:57 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("create %q: %s", fpath, err)
|
|
|
|
}
|
|
|
|
fd.Close()
|
2011-04-07 19:09:10 +02:00
|
|
|
fd, err = Create(dpath + "/file")
|
2010-12-03 05:34:57 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("create %q: %s", fpath, err)
|
|
|
|
}
|
|
|
|
fd.Close()
|
|
|
|
if err = RemoveAll(path); err != nil {
|
|
|
|
t.Fatalf("RemoveAll %q (second): %s", path, err)
|
|
|
|
}
|
|
|
|
if _, err := Lstat(path); err == nil {
|
|
|
|
t.Fatalf("Lstat %q succeeded after RemoveAll (second)", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine if we should run the following test.
|
|
|
|
testit := true
|
2012-01-25 21:56:26 +01:00
|
|
|
if runtime.GOOS == "windows" {
|
2010-12-03 05:34:57 +01:00
|
|
|
// Chmod is not supported under windows.
|
|
|
|
testit = false
|
|
|
|
} else {
|
|
|
|
// Test fails as root.
|
|
|
|
testit = Getuid() != 0
|
|
|
|
}
|
|
|
|
if testit {
|
|
|
|
// Make directory with file and subdirectory and trigger error.
|
|
|
|
if err = MkdirAll(dpath, 0777); err != nil {
|
|
|
|
t.Fatalf("MkdirAll %q: %s", dpath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range []string{fpath, dpath + "/file1", path + "/zzz"} {
|
2011-04-07 19:09:10 +02:00
|
|
|
fd, err = Create(s)
|
2010-12-03 05:34:57 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("create %q: %s", s, err)
|
|
|
|
}
|
|
|
|
fd.Close()
|
|
|
|
}
|
|
|
|
if err = Chmod(dpath, 0); err != nil {
|
|
|
|
t.Fatalf("Chmod %q 0: %s", dpath, err)
|
|
|
|
}
|
2011-01-21 19:19:03 +01:00
|
|
|
|
|
|
|
// No error checking here: either RemoveAll
|
|
|
|
// will or won't be able to remove dpath;
|
|
|
|
// either way we want to see if it removes fpath
|
2016-07-22 20:15:38 +02:00
|
|
|
// and path/zzz. Reasons why RemoveAll might
|
2011-01-21 19:19:03 +01:00
|
|
|
// succeed in removing dpath as well include:
|
|
|
|
// * running as root
|
|
|
|
// * running on a file system without permissions (FAT)
|
|
|
|
RemoveAll(path)
|
|
|
|
Chmod(dpath, 0777)
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
for _, s := range []string{fpath, path + "/zzz"} {
|
2013-11-06 20:49:01 +01:00
|
|
|
if _, err = Lstat(s); err == nil {
|
2010-12-03 05:34:57 +01:00
|
|
|
t.Fatalf("Lstat %q succeeded after partial RemoveAll", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err = RemoveAll(path); err != nil {
|
|
|
|
t.Fatalf("RemoveAll %q after partial RemoveAll: %s", path, err)
|
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
if _, err = Lstat(path); err == nil {
|
2010-12-03 05:34:57 +01:00
|
|
|
t.Fatalf("Lstat %q succeeded after RemoveAll (final)", path)
|
|
|
|
}
|
|
|
|
}
|
2011-01-21 19:19:03 +01:00
|
|
|
|
2018-01-09 02:23:08 +01:00
|
|
|
// Test RemoveAll on a large directory.
|
|
|
|
func TestRemoveAllLarge(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping in short mode")
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpDir := TempDir()
|
|
|
|
// Work directory.
|
|
|
|
path := tmpDir + "/_TestRemoveAllLarge_"
|
|
|
|
|
|
|
|
// Make directory with 1000 files and remove.
|
|
|
|
if err := MkdirAll(path, 0777); err != nil {
|
|
|
|
t.Fatalf("MkdirAll %q: %s", path, err)
|
|
|
|
}
|
|
|
|
for i := 0; i < 1000; i++ {
|
|
|
|
fpath := fmt.Sprintf("%s/file%d", path, i)
|
|
|
|
fd, err := Create(fpath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("create %q: %s", fpath, err)
|
|
|
|
}
|
|
|
|
fd.Close()
|
|
|
|
}
|
|
|
|
if err := RemoveAll(path); err != nil {
|
|
|
|
t.Fatalf("RemoveAll %q: %s", path, err)
|
|
|
|
}
|
|
|
|
if _, err := Lstat(path); err == nil {
|
|
|
|
t.Fatalf("Lstat %q succeeded after RemoveAll", path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-21 19:19:03 +01:00
|
|
|
func TestMkdirAllWithSymlink(t *testing.T) {
|
2017-01-14 01:05:42 +01:00
|
|
|
testenv.MustHaveSymlink(t)
|
2011-01-21 19:19:03 +01:00
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
tmpDir, err := ioutil.TempDir("", "TestMkdirAllWithSymlink-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer RemoveAll(tmpDir)
|
|
|
|
|
2012-03-02 21:01:37 +01:00
|
|
|
dir := tmpDir + "/dir"
|
2012-10-23 06:31:11 +02:00
|
|
|
err = Mkdir(dir, 0755)
|
2011-01-21 19:19:03 +01:00
|
|
|
if err != nil {
|
2012-03-02 21:01:37 +01:00
|
|
|
t.Fatalf("Mkdir %s: %s", dir, err)
|
2011-01-21 19:19:03 +01:00
|
|
|
}
|
|
|
|
|
2012-03-02 21:01:37 +01:00
|
|
|
link := tmpDir + "/link"
|
|
|
|
err = Symlink("dir", link)
|
2011-01-21 19:19:03 +01:00
|
|
|
if err != nil {
|
2012-03-02 21:01:37 +01:00
|
|
|
t.Fatalf("Symlink %s: %s", link, err)
|
2011-01-21 19:19:03 +01:00
|
|
|
}
|
|
|
|
|
2012-03-02 21:01:37 +01:00
|
|
|
path := link + "/foo"
|
2011-01-21 19:19:03 +01:00
|
|
|
err = MkdirAll(path, 0755)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("MkdirAll %q: %s", path, err)
|
|
|
|
}
|
|
|
|
}
|
2011-04-07 19:09:10 +02:00
|
|
|
|
|
|
|
func TestMkdirAllAtSlash(t *testing.T) {
|
2015-01-15 01:27:56 +01:00
|
|
|
switch runtime.GOOS {
|
|
|
|
case "android", "plan9", "windows":
|
|
|
|
t.Skipf("skipping on %s", runtime.GOOS)
|
2015-10-31 01:59:47 +01:00
|
|
|
case "darwin":
|
|
|
|
switch runtime.GOARCH {
|
|
|
|
case "arm", "arm64":
|
|
|
|
t.Skipf("skipping on darwin/%s, mkdir returns EPERM", runtime.GOARCH)
|
|
|
|
}
|
2011-04-07 19:09:10 +02:00
|
|
|
}
|
|
|
|
RemoveAll("/_go_os_test")
|
2015-10-31 01:59:47 +01:00
|
|
|
const dir = "/_go_os_test/dir"
|
|
|
|
err := MkdirAll(dir, 0777)
|
2011-04-07 19:09:10 +02:00
|
|
|
if err != nil {
|
|
|
|
pathErr, ok := err.(*PathError)
|
|
|
|
// common for users not to be able to write to /
|
2015-10-31 01:59:47 +01:00
|
|
|
if ok && (pathErr.Err == syscall.EACCES || isReadonlyError(pathErr.Err)) {
|
|
|
|
t.Skipf("could not create %v: %v", dir, err)
|
2011-04-07 19:09:10 +02:00
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
t.Fatalf(`MkdirAll "/_go_os_test/dir": %v, %s`, err, pathErr.Err)
|
2011-04-07 19:09:10 +02:00
|
|
|
}
|
|
|
|
RemoveAll("/_go_os_test")
|
|
|
|
}
|