Skip to content

Commit

Permalink
Distinguish between Offline and Local Mode.
Browse files Browse the repository at this point in the history
This fixes filestore operations when the daemon is started in offline
mode.

License: MIT
Signed-off-by: Kevin Atkinson <k@kevina.org>
  • Loading branch information
kevina committed Sep 2, 2016
1 parent f4d6a9f commit b34fd9c
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 78 deletions.
1 change: 1 addition & 0 deletions cmd/ipfs/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
res.SetError(err, cmds.ErrNormal)
return
}
node.SetLocal(false)

printSwarmAddrs(node)

Expand Down
1 change: 1 addition & 0 deletions cmd/ipfs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ func (i *cmdInvocation) constructNodeFunc(ctx context.Context) func() (*core.Ipf
if err != nil {
return nil, err
}
n.SetLocal(true)
i.node = n
return i.node, nil
}
Expand Down
6 changes: 3 additions & 3 deletions core/commands/filestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ same as for 'ipfs add'.
res.SetError(err, cmds.ErrNormal)
return
}
} else if node.OnlineMode() {
} else if !node.LocalMode() {
if !req.Files().IsDirectory() {
res.SetError(errors.New("expected directory object"), cmds.ErrNormal)
return
Expand Down Expand Up @@ -814,7 +814,7 @@ copy is not removed. Use "filestore rm-dups" to remove the old copy.
res.SetError(err, cmds.ErrNormal)
return
}
offline := !node.OnlineMode()
local := node.LocalMode()
args := req.Arguments()
if len(args) < 1 {
res.SetError(errors.New("must specify hash"), cmds.ErrNormal)
Expand All @@ -832,7 +832,7 @@ copy is not removed. Use "filestore rm-dups" to remove the old copy.
} else {
path = mhash
}
if offline {
if local {
path, err = filepath.Abs(path)
if err != nil {
res.SetError(err, cmds.ErrNormal)
Expand Down
22 changes: 22 additions & 0 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ type mode int
const (
// zero value is not a valid mode, must be explicitly set
invalidMode mode = iota
localMode
offlineMode
onlineMode
)
Expand Down Expand Up @@ -117,6 +118,7 @@ type IpfsNode struct {
ctx context.Context

mode mode
localModeSet bool
}

// Mounts defines what the node's mount state is. This should
Expand Down Expand Up @@ -384,6 +386,26 @@ func (n *IpfsNode) OnlineMode() bool {
}
}

func (n *IpfsNode) SetLocal(isLocal bool) {
if isLocal {
n.mode = localMode
}
n.localModeSet = true
}

func (n *IpfsNode) LocalMode() bool {
if !n.localModeSet {
// programmer error should not happen
panic("local mode not set")
}
switch n.mode {
case localMode:
return true
default:
return false
}
}

func (n *IpfsNode) Bootstrap(cfg BootstrapConfig) error {

// TODO what should return value be when in offlineMode?
Expand Down
8 changes: 5 additions & 3 deletions filestore/support/blockstore.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package filestore_support

import (
"errors"
"fmt"
b "github.com/ipfs/go-ipfs/blocks"
bs "github.com/ipfs/go-ipfs/blocks/blockstore"
. "github.com/ipfs/go-ipfs/filestore"
Expand Down Expand Up @@ -80,8 +80,10 @@ func (bs *blockstore) prepareBlock(k ds.Key, block b.Block) (*DataObj, error) {
return nil, nil
} else {
posInfo := block.PosInfo()
if posInfo == nil || posInfo.Stat == nil {
return nil, errors.New("no file information for block")
if posInfo == nil {
return nil, fmt.Errorf("%s: no file information for block", block.Key())
} else if posInfo.Stat == nil {
return nil, fmt.Errorf("%s: %s: no stat information for file", block.Key(), posInfo.FullPath)
}
d := &DataObj{
FilePath: CleanPath(posInfo.FullPath),
Expand Down
4 changes: 2 additions & 2 deletions filestore/util/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ func (m fileNodes) add(key bk.Key) {

func ConvertToFile(node *core.IpfsNode, key bk.Key, path string) error {
config, _ := node.Repo.Config()
if node.OnlineMode() && (config == nil || !config.Filestore.APIServerSidePaths) {
return errs.New("Node is online and server side paths are not enabled.")
if !node.LocalMode() && (config == nil || !config.Filestore.APIServerSidePaths) {
return errs.New("Daemon is running and server side paths are not enabled.")
}
if !filepath.IsAbs(path) {
return errs.New("absolute path required")
Expand Down
76 changes: 76 additions & 0 deletions test/sharness/lib/test-filestore-lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,79 @@ filestore_test_exact_paths() {
rm -rf mydir
'
}

filestore_test_w_daemon() {
opt=$1

test_init_ipfs

test_launch_ipfs_daemon $opt

test_add_cat_file "filestore add " "`pwd`"

test_post_add "filestore add " "`pwd`"

test_add_cat_5MB "filestore add " "`pwd`"

filestore_test_exact_paths

test_expect_success "ipfs add -S fails unless enable" '
echo "Hello Worlds!" >mountdir/hello.txt &&
test_must_fail ipfs filestore add -S "`pwd`"/mountdir/hello.txt >actual
'

test_expect_success "filestore mv should fail" '
HASH=QmQHRQ7EU8mUXLXkvqKWPubZqtxYPbwaqYo6NXSfS9zdCc &&
random 5242880 42 >mountdir/bigfile-42 &&
ipfs add mountdir/bigfile-42 &&
test_must_fail ipfs filestore mv $HASH "`pwd`/mountdir/bigfile-42-also"
'

test_kill_ipfs_daemon

test_expect_success "clean filestore" '
ipfs filestore ls -q | xargs ipfs filestore rm &&
test -z "`ipfs filestore ls -q`"
'

test_expect_success "enable Filestore.APIServerSidePaths" '
ipfs config Filestore.APIServerSidePaths --bool true
'

test_launch_ipfs_daemon $opt

test_add_cat_file "filestore add -S" "`pwd`"

test_post_add "filestore add -S" "`pwd`"

test_add_cat_5MB "filestore add -S" "`pwd`"

cat <<EOF > add_expect
added QmQhAyoEzSg5JeAzGDCx63aPekjSGKeQaYs4iRf4y6Qm6w adir
added QmSr7FqYkxYWGoSfy8ZiaMWQ5vosb18DQGCzjwEQnVHkTb `pwd`/adir/file3
added QmVr26fY1tKyspEJBniVhqxQeEjhF78XerGiqWAwraVLQH `pwd`/adir/file1
added QmZm53sWMaAQ59x56tFox8X9exJFELWC33NLjK6m8H7CpN `pwd`/adir/file2
EOF

test_expect_success "testing filestore add -S -r" '
mkdir adir &&
echo "Hello Worlds!" > adir/file1 &&
echo "HELLO WORLDS!" > adir/file2 &&
random 5242880 41 > adir/file3 &&
ipfs filestore add -S -r "`pwd`/adir" | LC_ALL=C sort > add_actual &&
test_cmp add_expect add_actual &&
ipfs cat QmVr26fY1tKyspEJBniVhqxQeEjhF78XerGiqWAwraVLQH > cat_actual
test_cmp adir/file1 cat_actual
'

test_expect_success "filestore mv" '
HASH=QmQHRQ7EU8mUXLXkvqKWPubZqtxYPbwaqYo6NXSfS9zdCc &&
test_must_fail ipfs filestore mv $HASH "mountdir/bigfile-42-also" &&
ipfs filestore mv $HASH "`pwd`/mountdir/bigfile-42-also"
'

filestore_test_exact_paths '-S'

test_kill_ipfs_daemon

}
71 changes: 1 addition & 70 deletions test/sharness/t0261-filestore-online.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,75 +9,6 @@ test_description="Test filestore"
. lib/test-filestore-lib.sh
. lib/test-lib.sh

test_init_ipfs

test_launch_ipfs_daemon

test_add_cat_file "filestore add " "`pwd`"

test_post_add "filestore add " "`pwd`"

test_add_cat_5MB "filestore add " "`pwd`"

filestore_test_exact_paths

test_expect_success "ipfs add -S fails unless enable" '
echo "Hello Worlds!" >mountdir/hello.txt &&
test_must_fail ipfs filestore add -S "`pwd`"/mountdir/hello.txt >actual
'

test_expect_success "filestore mv should fail" '
HASH=QmQHRQ7EU8mUXLXkvqKWPubZqtxYPbwaqYo6NXSfS9zdCc &&
random 5242880 42 >mountdir/bigfile-42 &&
ipfs add mountdir/bigfile-42 &&
test_must_fail ipfs filestore mv $HASH "`pwd`/mountdir/bigfile-42-also"
'

test_kill_ipfs_daemon

test_expect_success "clean filestore" '
ipfs filestore ls -q | xargs ipfs filestore rm &&
test -z "`ipfs filestore ls -q`"
'

test_expect_success "enable Filestore.APIServerSidePaths" '
ipfs config Filestore.APIServerSidePaths --bool true
'

test_launch_ipfs_daemon

test_add_cat_file "filestore add -S" "`pwd`"

test_post_add "filestore add -S" "`pwd`"

test_add_cat_5MB "filestore add -S" "`pwd`"

cat <<EOF > add_expect
added QmQhAyoEzSg5JeAzGDCx63aPekjSGKeQaYs4iRf4y6Qm6w adir
added QmSr7FqYkxYWGoSfy8ZiaMWQ5vosb18DQGCzjwEQnVHkTb `pwd`/adir/file3
added QmVr26fY1tKyspEJBniVhqxQeEjhF78XerGiqWAwraVLQH `pwd`/adir/file1
added QmZm53sWMaAQ59x56tFox8X9exJFELWC33NLjK6m8H7CpN `pwd`/adir/file2
EOF

test_expect_success "testing filestore add -S -r" '
mkdir adir &&
echo "Hello Worlds!" > adir/file1 &&
echo "HELLO WORLDS!" > adir/file2 &&
random 5242880 41 > adir/file3 &&
ipfs filestore add -S -r "`pwd`/adir" | LC_ALL=C sort > add_actual &&
test_cmp add_expect add_actual &&
ipfs cat QmVr26fY1tKyspEJBniVhqxQeEjhF78XerGiqWAwraVLQH > cat_actual
test_cmp adir/file1 cat_actual
'

test_expect_success "filestore mv" '
HASH=QmQHRQ7EU8mUXLXkvqKWPubZqtxYPbwaqYo6NXSfS9zdCc &&
test_must_fail ipfs filestore mv $HASH "mountdir/bigfile-42-also" &&
ipfs filestore mv $HASH "`pwd`/mountdir/bigfile-42-also"
'

filestore_test_exact_paths '-S'

test_kill_ipfs_daemon
filestore_test_w_daemon

test_done
14 changes: 14 additions & 0 deletions test/sharness/t0264-filestore-offline.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh
#
# Copyright (c) 2014 Christian Couder
# MIT Licensed; see the LICENSE file in this repository.
#

test_description="Test filestore"

. lib/test-filestore-lib.sh
. lib/test-lib.sh

filestore_test_w_daemon --offline

test_done

0 comments on commit b34fd9c

Please sign in to comment.