Skip to content

Commit

Permalink
Extracted the tree walking code to be shared with other tasks such as…
Browse files Browse the repository at this point in the history
… dump
  • Loading branch information
ducalex committed Apr 2, 2023
1 parent 323eb40 commit 73a742a
Showing 1 changed file with 52 additions and 41 deletions.
93 changes: 52 additions & 41 deletions cmd/restic/cmd_serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,37 @@ func init() {
cmdFlags.StringVarP(&serveOptions.Listen, "listen", "l", "localhost:3080", "set the listen host name and `address`")
}

type fileNode struct {
Path string
Node *restic.Node
}

// listNodes returns all the nodes contained in any of the paths, optionally recursively
func listNodes(ctx context.Context, repo restic.Repository, tree restic.ID, recursive bool, paths []string) ([]fileNode, error) {
var files []fileNode
err := walker.Walk(ctx, repo, tree, nil, func(_ restic.ID, nodepath string, node *restic.Node, err error) (bool, error) {
if err != nil || node == nil {
return false, err
}
for _, path := range paths {
if fs.HasPathPrefix(path, nodepath) {
files = append(files, fileNode{nodepath, node})
break
}
}
if node.Type == "dir" && !recursive {
for _, path := range paths {
if fs.HasPathPrefix(nodepath, path) {
return false, nil
}
}
return false, walker.ErrSkipNode
}
return false, nil
})
return files, err
}

func runWebServer(ctx context.Context, opts ServeOptions, gopts GlobalOptions, args []string) error {
if len(args) > 0 {
return errors.Fatal("this command does not accept additional arguments")
Expand Down Expand Up @@ -81,58 +112,38 @@ func runWebServer(ctx context.Context, opts ServeOptions, gopts GlobalOptions, a
return
}

type fileNode struct {
Path string
Node *restic.Node
}

var files []fileNode

err = walker.Walk(ctx, repo, *sn.Tree, nil, func(_ restic.ID, nodepath string, node *restic.Node, err error) (bool, error) {
if err != nil || node == nil {
return false, err
}
if fs.HasPathPrefix(curPath, nodepath) {
files = append(files, fileNode{nodepath, node})
}
if node.Type == "dir" && !fs.HasPathPrefix(nodepath, curPath) {
return false, walker.ErrSkipNode
}
return false, nil
})

files, err := listNodes(ctx, repo, *sn.Tree, false, []string{curPath})
if err != nil || len(files) == 0 {
http.Error(w, "Path not found in snapshot", http.StatusNotFound)
return
}

if len(files) == 1 && files[0].Node.Type == "file" {
// Requested path is a file, dump it
if err := dump.New("zip", repo, w).WriteNode(ctx, files[0].Node); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
} else {
// Requested path is a folder, list it
var rows []treePageRow
for _, item := range files {
if !fs.HasPathPrefix(item.Path, curPath) {
rows = append(rows, treePageRow{"/tree/" + snapshotID + item.Path, item.Node.Name, item.Node.Type, item.Node.Size, item.Node.ModTime})
}
}
sort.SliceStable(rows, func(i, j int) bool {
return strings.ToLower(rows[i].Name) < strings.ToLower(rows[j].Name)
})
sort.SliceStable(rows, func(i, j int) bool {
return rows[i].Type == "dir" && rows[j].Type != "dir"
})
parent := "/tree/" + snapshotID + curPath + "/.."
if curPath == "/" {
parent = "/"
}
if err := treePage.Execute(w, treePageData{snapshotID + ": " + curPath, parent, rows}); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

var rows []treePageRow
for _, item := range files {
if item.Path != curPath {
rows = append(rows, treePageRow{"/tree/" + snapshotID + item.Path, item.Node.Name, item.Node.Type, item.Node.Size, item.Node.ModTime})
}
}
sort.SliceStable(rows, func(i, j int) bool {
return strings.ToLower(rows[i].Name) < strings.ToLower(rows[j].Name)
})
sort.SliceStable(rows, func(i, j int) bool {
return rows[i].Type == "dir" && rows[j].Type != "dir"
})
parent := "/tree/" + snapshotID + curPath + "/.."
if curPath == "/" {
parent = "/"
}
if err := treePage.Execute(w, treePageData{snapshotID + ": " + curPath, parent, rows}); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit 73a742a

Please sign in to comment.