func DoTestVisitDirsOnly(t *testing.T, repo fs.NodeRepo) { dir, errors := fs.IndexDir("../../testroot/", repo) assert.T(t, dir != nil) assert.Equalf(t, 0, len(errors), "%v", errors) collect := []fs.Dir{} visited := []fs.Node{} fs.Walk(dir, func(node fs.Node) bool { visited = append(visited, node) d, ok := node.(fs.Dir) if ok { collect = append(collect, d) return true } _, ok = node.(fs.File) if ok { return false } t.Errorf("Unexpected type during visit: %v", node) return true }) assert.Equalf(t, 3, len(collect), "Unexpected dirs in testroot/: %v", collect) for _, node := range visited { _, ok := node.(fs.Block) if ok { t.Fatalf("Should not have gotten a block, we told visitor to stop at file level.") } } }
func DoTestVisitBlocks(t *testing.T, repo fs.NodeRepo) { dir, errors := fs.IndexDir("../../testroot/", repo) assert.T(t, dir != nil) assert.Equalf(t, 0, len(errors), "%v", errors) collect := []fs.Block{} fs.Walk(dir, func(node fs.Node) bool { b, ok := node.(fs.Block) if ok { collect = append(collect, b) } return true }) matched := false for _, block := range collect { if block.Info().Strong == "d1f11a93449fa4d3f320234743204ce157bbf1f3" { matched = true } } assert.Tf(t, matched, "Failed to find expected block") }
func main() { if len(os.Args) < 2 { fmt.Fprintf(os.Stderr, "Usage: %s <url>\n", os.Args[0]) os.Exit(1) } store, err := clnt.Connect(os.Args[1]) if err != nil { die(err) } fs.Walk(store.Root(), func(node fs.Node) bool { switch node.(type) { case *fs.Dir: d := node.(*fs.Dir) fmt.Printf("d\t%s\t%s\n", d.Strong(), d.Name()) case *fs.File: f := node.(*fs.File) fmt.Printf("f\t%s\t%s\n", f.Strong(), f.Name()) case *fs.Block: b := node.(*fs.Block) f := b.Parent().(*fs.File) fmt.Printf("b\t%s\t%s\t%d\n", node.Strong(), f.Name(), b.Position()) } return true }) }
func DoTestParentRefs(t *testing.T, repo fs.NodeRepo) { tg := treegen.New() treeSpec := tg.D("foo", tg.D("bar", tg.D("aleph", tg.F("A", tg.B(42, 65537)), tg.F("a", tg.B(42, 65537))), tg.D("beth", tg.F("B", tg.B(43, 65537)), tg.F("b", tg.B(43, 65537))), tg.D("jimmy", tg.F("G", tg.B(44, 65537)), tg.F("g", tg.B(44, 65537)))), tg.D("baz", tg.D("uno", tg.F("1", tg.B(1, 65537)), tg.F("I", tg.B(1, 65537))), tg.D("dos", tg.F("2", tg.B(11, 65537)), tg.F("II", tg.B(11, 65537))), tg.D("tres", tg.F("3", tg.B(111, 65537)), tg.F("III", tg.B(111, 65537))))) path := treegen.TestTree(t, treeSpec) defer os.RemoveAll(path) foo, errors := fs.IndexDir(filepath.Join(path, "foo"), repo) assert.Equalf(t, 0, len(errors), "%v", errors) rootCount := 0 fs.Walk(foo, func(node fs.Node) bool { switch node.(type) { case fs.Dir: dir := node.(fs.Dir) if _, hasParent := dir.Parent(); !hasParent { rootCount++ } break case fs.File: file := node.(fs.File) _, hasParent := file.Parent() assert.Tf(t, hasParent, "%v is root?!", file.Info()) break case fs.Block: block := node.(fs.Block) _, hasParent := block.Parent() assert.Tf(t, hasParent, "%v is root?!", block.Info()) break } return true }) assert.Equal(t, 1, rootCount) }
func main() { store, err := clnt.Connect("127.0.0.1:5640") if err != nil { die("Failed to connect", err) } err = store.Refresh() if err != nil { die("Failed to refresh", err) } fs.Walk(store.Root(), func(node fs.Node) bool { switch node.(type) { case *fs.Dir: fmt.Printf("d\t%s\t%s\n", node.Strong(), fs.RelPath(node.(fs.FsNode))) return true case *fs.File: fmt.Printf("f\t%s\t%s\n", node.Strong(), fs.RelPath(node.(fs.FsNode))) } return false }) }
func (plan *PatchPlan) SetMode(errors chan<- os.Error) { fs.Walk(plan.srcStore.Repo().Root(), func(srcNode fs.Node) bool { var err os.Error srcFsNode, is := srcNode.(fs.FsNode) if !is { return false } srcPath := fs.RelPath(srcFsNode) if absPath := plan.dstStore.Resolve(srcPath); absPath != "" { err = os.Chmod(absPath, srcFsNode.Mode()) } else { err = os.NewError(fmt.Sprintf("Expected %s not found in destination", srcPath)) } if err != nil && errors != nil { errors <- err } _, is = srcNode.(fs.Dir) return is }) }
func NewPatchPlan(srcStore fs.BlockStore, dstStore fs.LocalStore) *PatchPlan { plan := &PatchPlan{srcStore: srcStore, dstStore: dstStore} plan.dstFileUnmatch = make(map[string]fs.File) fs.Walk(dstStore.Repo().Root(), func(dstNode fs.Node) bool { dstFile, isDstFile := dstNode.(fs.File) if isDstFile { plan.dstFileUnmatch[fs.RelPath(dstFile)] = dstFile } return !isDstFile }) relocRefs := make(map[string]int) // Find all the FsNode matches fs.Walk(srcStore.Repo().Root(), func(srcNode fs.Node) bool { // Ignore non-FsNodes srcFsNode, isSrcFsNode := srcNode.(fs.FsNode) if !isSrcFsNode { return false } // log.Printf("In src: %s", fs.RelPath(srcFsNode)) srcFile, isSrcFile := srcNode.(fs.File) srcPath := fs.RelPath(srcFsNode) // Remove this srcPath from dst unmatched, if it was present plan.dstFileUnmatch[srcPath] = nil, false var srcStrong string if isSrcFile { srcStrong = srcFile.Info().Strong } else if srcDir, isSrcDir := srcNode.(fs.Dir); isSrcDir { srcStrong = srcDir.Info().Strong } var dstNode fs.FsNode var hasDstNode bool dstNode, hasDstNode = dstStore.Repo().File(srcStrong) if !hasDstNode { dstNode, hasDstNode = dstStore.Repo().Dir(srcStrong) } isDstFile := false if hasDstNode { _, isDstFile = dstNode.(fs.File) } dstFilePath := dstStore.Resolve(srcPath) dstFileInfo, _ := os.Stat(dstFilePath) // Resolve dst node that matches strong checksum with source if hasDstNode && isSrcFile == isDstFile { dstPath := fs.RelPath(dstNode) relocRefs[dstPath]++ // dstPath will be used in this cmd, inc ref count // log.Printf("srcPath=%s dstPath=%s", srcPath, dstPath) if srcPath != dstPath { // Local dst file needs to be renamed or copied to src path from := &LocalPath{LocalStore: dstStore, RelPath: dstPath} to := &LocalPath{LocalStore: dstStore, RelPath: srcPath} plan.Cmds = append(plan.Cmds, &Transfer{From: from, To: to, relocRefs: relocRefs}) } else { // Same path, keep it where it is plan.Cmds = append(plan.Cmds, &Keep{ Path: &LocalPath{LocalStore: dstStore, RelPath: srcPath}}) } // If its a file, figure out what to do with it } else if isSrcFile { switch { // Destination is not a file, so get rid of whatever is there first case dstFileInfo != nil && !dstFileInfo.IsRegular(): plan.Cmds = append(plan.Cmds, &Conflict{ Path: &LocalPath{LocalStore: dstStore, RelPath: srcPath}, FileInfo: dstFileInfo}) fallthrough // Destination file does not exist, so full source copy needed case dstFileInfo == nil: plan.Cmds = append(plan.Cmds, &SrcFileDownload{ SrcFile: srcFile, Path: &LocalPath{LocalStore: dstStore, RelPath: srcPath}}) break // Destination file exists, add block-level commands default: plan.appendFilePlan(srcFile, srcPath) break } // If its a directory, check for conflicting files of same name } else { if dstFileInfo != nil && !dstFileInfo.IsDirectory() { plan.Cmds = append(plan.Cmds, &Conflict{ Path: &LocalPath{LocalStore: dstStore, RelPath: dstFilePath}, FileInfo: dstFileInfo}) } } return !isSrcFile }) return plan }