mirror of
https://github.com/ipfs/kubo.git
synced 2025-10-17 19:54:25 +08:00

The progressbar should now correctly calculate the size of a directory (by ignoring the directory sizes). fixes #5288 License: MIT Signed-off-by: Steven Allen <steven@stebalien.com>
93 lines
2.1 KiB
Go
93 lines
2.1 KiB
Go
/*
|
|
Package corerepo provides pinning and garbage collection for local
|
|
IPFS block services.
|
|
|
|
IPFS nodes will keep local copies of any object that have either been
|
|
added or requested locally. Not all of these objects are worth
|
|
preserving forever though, so the node administrator can pin objects
|
|
they want to keep and unpin objects that they don't care about.
|
|
|
|
Garbage collection sweeps iterate through the local block store
|
|
removing objects that aren't pinned, which frees storage space for new
|
|
objects.
|
|
*/
|
|
package corerepo
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/ipfs/go-ipfs/core"
|
|
path "gx/ipfs/QmPqCBrmkm7jNfYi7xFS7mUZsrN6DEumBMrxLnL7axNJx1/go-path"
|
|
resolver "gx/ipfs/QmPqCBrmkm7jNfYi7xFS7mUZsrN6DEumBMrxLnL7axNJx1/go-path/resolver"
|
|
uio "gx/ipfs/Qmdqe1sKBpz6W8xFDptGfmzgCPQ5CXNuQPhZeELqMowgsQ/go-unixfs/io"
|
|
|
|
cid "gx/ipfs/QmYVNvtQkeZ6AKSwDrjQTs432QtL6umrrK41EBq3cu7iSP/go-cid"
|
|
)
|
|
|
|
func Pin(n *core.IpfsNode, ctx context.Context, paths []string, recursive bool) ([]*cid.Cid, error) {
|
|
out := make([]*cid.Cid, len(paths))
|
|
|
|
r := &resolver.Resolver{
|
|
DAG: n.DAG,
|
|
ResolveOnce: uio.ResolveUnixfsOnce,
|
|
}
|
|
|
|
for i, fpath := range paths {
|
|
p, err := path.ParsePath(fpath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
dagnode, err := core.Resolve(ctx, n.Namesys, r, p)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("pin: %s", err)
|
|
}
|
|
err = n.Pinning.Pin(ctx, dagnode, recursive)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("pin: %s", err)
|
|
}
|
|
out[i] = dagnode.Cid()
|
|
}
|
|
|
|
err := n.Pinning.Flush()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return out, nil
|
|
}
|
|
|
|
func Unpin(n *core.IpfsNode, ctx context.Context, paths []string, recursive bool) ([]*cid.Cid, error) {
|
|
unpinned := make([]*cid.Cid, len(paths))
|
|
|
|
r := &resolver.Resolver{
|
|
DAG: n.DAG,
|
|
ResolveOnce: uio.ResolveUnixfsOnce,
|
|
}
|
|
|
|
for i, p := range paths {
|
|
p, err := path.ParsePath(p)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
k, err := core.ResolveToCid(ctx, n.Namesys, r, p)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = n.Pinning.Unpin(ctx, k, recursive)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
unpinned[i] = k
|
|
}
|
|
|
|
err := n.Pinning.Flush()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return unpinned, nil
|
|
}
|