示例#1
0
func ActionCLI(ctx *cli.Context) {

	var path, outfile string
	path = ctx.String("d")
	outfile = ctx.String("o")

	if false == common.IsDir(path) {
		common.UsageAndExit("Expecting an existing directory: %s", path)
	}

	var cliWriter io.Writer
	cliWriter = os.Stdout
	if ctx.GlobalBool("q") {
		cliWriter = ioutil.Discard
	}

	wc := newWalkCount(path)
	if err := walk.Walk(path, wc.walkFn); err != nil {
		common.UsageAndExit("Walk Counter Error: %s", err)
	}

	if path == "." {
		path = ""
	}

	w := newTraverse(cliWriter, path, outfile, wc.fileCount)
	defer w.close()
	if err := walk.Walk(path, w.walkFn); err != nil {
		common.UsageAndExit("Walk Error: %s", err)
	}
}
示例#2
0
func TestBug3486(t *testing.T) { // http://code.google.com/p/go/issues/detail?id=3486
	root, err := walk.EvalSymlinks(runtime.GOROOT() + "/test")
	if err != nil {
		t.Fatal(err)
	}
	bugs := walk.Join(root, "bugs")
	ken := walk.Join(root, "ken")
	seenBugs := false
	seenKen := false
	walk.Walk(root, func(pth string, info os.FileInfo, err error) error {
		if err != nil {
			t.Fatal(err)
		}

		switch pth {
		case bugs:
			seenBugs = true
			return walk.SkipDir
		case ken:
			if !seenBugs {
				t.Fatal("walk.Walk out of order - ken before bugs")
			}
			seenKen = true
		}
		return nil
	})
	if !seenKen {
		t.Fatalf("%q not seen", ken)
	}
}
示例#3
0
func (c Crawler) Crawl(cb CrawlFunc) error {

	walker := func(path string, info os.FileInfo, err error) error {

		if err != nil {
			return err
		}

		if info.IsDir() {
			return nil
		}

		// ensure dot-geojson here

		_ = cb(path, info)
		return nil
	}

	err := walk.Walk(c.Root, walker)

	if err != nil {
		fmt.Printf("error: %s\n", err)
	}

	return nil
}
示例#4
0
func TestWalkFileError(t *testing.T) {
	td, err := ioutil.TempDir("", "walktest")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(td)

	touch(t, walk.Join(td, "foo"))
	touch(t, walk.Join(td, "bar"))
	dir := walk.Join(td, "dir")
	if err := os.MkdirAll(walk.Join(td, "dir"), 0755); err != nil {
		t.Fatal(err)
	}
	touch(t, walk.Join(dir, "baz"))
	touch(t, walk.Join(dir, "stat-error"))
	defer func() {
		*walk.LstatP = os.Lstat
	}()
	statErr := errors.New("some stat error")
	*walk.LstatP = func(path string) (os.FileInfo, error) {
		if strings.HasSuffix(path, "stat-error") {
			return nil, statErr
		}
		return os.Lstat(path)
	}
	got := map[string]error{}
	err = walk.Walk(td, func(path string, fi os.FileInfo, err error) error {
		rel, _ := walk.Rel(td, path)
		got[walk.ToSlash(rel)] = err
		return nil
	})
	if err != nil {
		t.Errorf("Walk error: %v", err)
	}
	want := map[string]error{
		".":              nil,
		"foo":            nil,
		"bar":            nil,
		"dir":            nil,
		"dir/baz":        nil,
		"dir/stat-error": statErr,
	}
	if !reflect.DeepEqual(got, want) {
		t.Errorf("Walked %#v; want %#v", got, want)
	}
}
示例#5
0
文件: glob.go 项目: THEY/godo
// walkFiles walks a directory starting at root returning all directories and files
// include those found in subdirectories.
func walkFiles(root string) ([]*FileAsset, error) {
	fileAssets := []*FileAsset{}
	var lock sync.Mutex
	visitor := func(path string, info os.FileInfo, err error) error {
		if err == nil {
			lock.Lock()
			fileAssets = append(fileAssets, &FileAsset{FileInfo: info, Path: filepath.ToSlash(path)})
			lock.Unlock()
		}
		return nil
	}
	err := walk.Walk(root, visitor)
	if err != nil {
		return nil, err
	}
	return fileAssets, nil
}
示例#6
0
func TestWalk(t *testing.T) {
	makeTree(t)
	errors := make([]error, 0, 10)
	clear := true
	markFn := func(path string, info os.FileInfo, err error) error {
		return mark(path, info, err, &errors, clear)
	}
	// Expect no errors.
	err := walk.Walk(tree.name, markFn)
	if err != nil {
		t.Fatalf("no error expected, found: %s", err)
	}
	if len(errors) != 0 {
		t.Fatalf("unexpected errors: %s", errors)
	}
	checkMarks(t, true)
	errors = errors[0:0]

	// Test permission errors.  Only possible if we're not root
	// and only on some file systems (AFS, FAT).  To avoid errors during
	// all.bash on those file systems, skip during go test -short.
	if os.Getuid() > 0 && !testing.Short() {
		// introduce 2 errors: chmod top-level directories to 0
		os.Chmod(walk.Join(tree.name, tree.entries[1].name), 0)
		os.Chmod(walk.Join(tree.name, tree.entries[3].name), 0)

		// 3) capture errors, expect two.
		// mark respective subtrees manually
		markTree(tree.entries[1])
		markTree(tree.entries[3])
		// correct double-marking of directory itself
		tree.entries[1].mark--
		tree.entries[3].mark--
		err := walk.Walk(tree.name, markFn)
		if err != nil {
			t.Fatalf("expected no error return from Walk, got %s", err)
		}
		if len(errors) != 2 {
			t.Errorf("expected 2 errors, got %d: %s", len(errors), errors)
		}
		// the inaccessible subtrees were marked manually
		checkMarks(t, true)
		errors = errors[0:0]

		// 4) capture errors, stop after first error.
		// mark respective subtrees manually
		markTree(tree.entries[1])
		markTree(tree.entries[3])
		// correct double-marking of directory itself
		tree.entries[1].mark--
		tree.entries[3].mark--
		clear = false // error will stop processing
		err = walk.Walk(tree.name, markFn)
		if err == nil {
			t.Fatalf("expected error return from Walk")
		}
		if len(errors) != 1 {
			t.Errorf("expected 1 error, got %d: %s", len(errors), errors)
		}
		// the inaccessible subtrees were marked manually
		checkMarks(t, false)
		errors = errors[0:0]

		// restore permissions
		os.Chmod(walk.Join(tree.name, tree.entries[1].name), 0770)
		os.Chmod(walk.Join(tree.name, tree.entries[3].name), 0770)
	}

	// cleanup
	if err := os.RemoveAll(tree.name); err != nil {
		t.Errorf("removeTree: %v", err)
	}
}