Skip to content

Commit

Permalink
feat: Unixfs1.5 mode and modification time support (#150)
Browse files Browse the repository at this point in the history
Apply unix mode and modification time to files.

Uses kubo and boxo with for Unixfs 1.5 to provide support for mode and modification time. If a retrieved file has mode and/or mod time, these values are applied to the saved file.

This demonstrates that kubo/client/rpc is working.

- Additional tests show that kubo/client/rpc works over a unix domain socket.
- Fix path parsing with new `path` package
- Add tests for directory contents with mod time and mode
- Use latest kubo and boxo
- Depend on go1.22
  • Loading branch information
gammazero authored Sep 12, 2024
1 parent 10a70df commit 9996224
Show file tree
Hide file tree
Showing 11 changed files with 1,032 additions and 838 deletions.
204 changes: 114 additions & 90 deletions go.mod

Large diffs are not rendered by default.

627 changes: 270 additions & 357 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package main
import (
"context"

iface "github.com/ipfs/boxo/coreiface"
ipfshttp "github.com/ipfs/kubo/client/rpc"
iface "github.com/ipfs/kubo/core/coreiface"
)

func http(ctx context.Context) (iface.CoreAPI, error) {
Expand Down
47 changes: 32 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import (
"os/signal"
gopath "path"
"path/filepath"
"runtime"
"strings"
"sync"
"syscall"

"github.com/cheggaaa/pb/v3"
iface "github.com/ipfs/boxo/coreiface"
ipath "github.com/ipfs/boxo/coreiface/path"
files "github.com/ipfs/boxo/files"
ipath "github.com/ipfs/boxo/path"
iface "github.com/ipfs/kubo/core/coreiface"
cli "github.com/urfave/cli/v2"
)

Expand Down Expand Up @@ -166,25 +167,28 @@ func movePostfixOptions(args []string) []string {
}

func parsePath(path string) (ipath.Path, error) {
ipfsPath := ipath.New(path)
if ipfsPath.IsValid() == nil {
ipfsPath, err := ipath.NewPath(path)
if err == nil {
return ipfsPath, nil
}
origErr := err

ipfsPath, err = ipath.NewPath("/ipfs/" + path)
if err == nil {
return ipfsPath, nil
}

u, err := url.Parse(path)
if err != nil {
return nil, fmt.Errorf("%q could not be parsed: %s", path, err)
return nil, origErr
}

switch proto := u.Scheme; proto {
switch u.Scheme {
case "ipfs", "ipld", "ipns":
ipfsPath = ipath.New(gopath.Join("/", proto, u.Host, u.Path))
return ipath.NewPath(gopath.Join("/", u.Scheme, u.Host, u.Path))
case "http", "https":
ipfsPath = ipath.New(u.Path)
default:
return nil, fmt.Errorf("%q is not recognized as an IPFS path", path)
return ipath.NewPath(u.Path)
}
return ipfsPath, ipfsPath.IsValid()
return nil, fmt.Errorf("%q is not recognized as an IPFS path", path)
}

// WriteTo writes the given node to the local filesystem at fpath.
Expand All @@ -205,7 +209,16 @@ func WriteTo(nd files.Node, fpath string, progress bool) error {
func writeToRec(nd files.Node, fpath string, bar *pb.ProgressBar) error {
switch nd := nd.(type) {
case *files.Symlink:
return os.Symlink(nd.Target, fpath)
err := os.Symlink(nd.Target, fpath)
if err != nil {
return err
}
switch runtime.GOOS {
case "linux", "freebsd", "netbsd", "openbsd", "dragonfly":
return files.UpdateModTime(fpath, nd.ModTime())
default:
return nil
}
case files.File:
f, err := os.Create(fpath)
defer f.Close()
Expand All @@ -221,20 +234,24 @@ func writeToRec(nd files.Node, fpath string, bar *pb.ProgressBar) error {
if err != nil {
return err
}
return nil
return files.UpdateMeta(fpath, nd.Mode(), nd.ModTime())
case files.Directory:
err := os.Mkdir(fpath, 0777)
if err != nil {
return err
}

entries := nd.Entries()
for entries.Next() {
child := filepath.Join(fpath, entries.Name())
if err := writeToRec(entries.Node(), child, bar); err != nil {
return err
}
}

if err = files.UpdateMeta(fpath, nd.Mode(), nd.ModTime()); err != nil {
return err
}

return entries.Err()
default:
return fmt.Errorf("file type %T at %q is not supported", nd, fpath)
Expand Down
4 changes: 2 additions & 2 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"os"
"path/filepath"

iface "github.com/ipfs/boxo/coreiface"
"github.com/ipfs/boxo/coreiface/options"
config "github.com/ipfs/kubo/config"
"github.com/ipfs/kubo/core"
"github.com/ipfs/kubo/core/coreapi"
iface "github.com/ipfs/kubo/core/coreiface"
"github.com/ipfs/kubo/core/coreiface/options"
"github.com/ipfs/kubo/core/node/libp2p"
"github.com/ipfs/kubo/plugin/loader"
"github.com/ipfs/kubo/repo/fsrepo"
Expand Down
Loading

0 comments on commit 9996224

Please sign in to comment.