Skip to content

Commit

Permalink
Merge pull request #832 from ipfs/fix/793-unpin-disable
Browse files Browse the repository at this point in the history
Fix #793: Allow to fully disable unpinning
  • Loading branch information
hsanjuan committed Jul 29, 2019
2 parents 0a5598a + 256f4be commit 30b9957
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
8 changes: 8 additions & 0 deletions ipfsconn/ipfshttp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
DefaultIPFSRequestTimeout = 5 * time.Minute
DefaultPinTimeout = 24 * time.Hour
DefaultUnpinTimeout = 3 * time.Hour
DefaultUnpinDisable = false
)

// Config is used to initialize a Connector and allows to customize
Expand Down Expand Up @@ -53,6 +54,9 @@ type Config struct {
// Unpin Operation timeout
UnpinTimeout time.Duration

// Disables the unpin operation and returns an error.
UnpinDisable bool

// Tracing flag used to skip tracing specific paths when not enabled.
Tracing bool
}
Expand All @@ -64,6 +68,7 @@ type jsonConfig struct {
IPFSRequestTimeout string `json:"ipfs_request_timeout"`
PinTimeout string `json:"pin_timeout"`
UnpinTimeout string `json:"unpin_timeout"`
UnpinDisable bool `json:"unpin_disable,omitempty"`
}

// ConfigKey provides a human-friendly identifier for this type of Config.
Expand All @@ -80,6 +85,7 @@ func (cfg *Config) Default() error {
cfg.IPFSRequestTimeout = DefaultIPFSRequestTimeout
cfg.PinTimeout = DefaultPinTimeout
cfg.UnpinTimeout = DefaultUnpinTimeout
cfg.UnpinDisable = DefaultUnpinDisable

return nil
}
Expand Down Expand Up @@ -154,6 +160,7 @@ func (cfg *Config) applyJSONConfig(jcfg *jsonConfig) error {
}

cfg.NodeAddr = nodeAddr
cfg.UnpinDisable = jcfg.UnpinDisable

err = config.ParseDurations(
"ipfshttp",
Expand Down Expand Up @@ -199,6 +206,7 @@ func (cfg *Config) toJSONConfig() (jcfg *jsonConfig, err error) {
jcfg.IPFSRequestTimeout = cfg.IPFSRequestTimeout.String()
jcfg.PinTimeout = cfg.PinTimeout.String()
jcfg.UnpinTimeout = cfg.UnpinTimeout.String()
jcfg.UnpinDisable = cfg.UnpinDisable

return
}
4 changes: 4 additions & 0 deletions ipfsconn/ipfshttp/ipfshttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,10 @@ func (ipfs *Connector) Unpin(ctx context.Context, hash cid.Cid) error {
ctx, span := trace.StartSpan(ctx, "ipfsconn/ipfshttp/Unpin")
defer span.End()

if ipfs.config.UnpinDisable {
return errors.New("ipfs unpinning is disallowed by configuration on this peer")
}

pinStatus, err := ipfs.PinLsCid(ctx, hash)
if err != nil {
return err
Expand Down
17 changes: 17 additions & 0 deletions ipfsconn/ipfshttp/ipfshttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,23 @@ func TestIPFSUnpin(t *testing.T) {
}
}

func TestIPFSUnpinDisabled(t *testing.T) {
ctx := context.Background()
ipfs, mock := testIPFSConnector(t)
defer mock.Close()
defer ipfs.Shutdown(ctx)
ipfs.config.UnpinDisable = true
err := ipfs.Pin(ctx, test.Cid1, -1)
if err != nil {
t.Fatal(err)
}

err = ipfs.Unpin(ctx, test.Cid1)
if err == nil {
t.Fatal("pin should be disabled")
}
}

func TestIPFSPinLsCid(t *testing.T) {
ctx := context.Background()
ipfs, mock := testIPFSConnector(t)
Expand Down

0 comments on commit 30b9957

Please sign in to comment.