Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3
Commits
5abf3c2a
Commit
5abf3c2a
authored
Jul 05, 2014
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added ls + recusrive directory adding
parent
56b9b57b
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
115 additions
and
21 deletions
+115
-21
cli/add.go
cli/add.go
+43
-20
cli/ipfs.go
cli/ipfs.go
+1
-0
cli/ls.go
cli/ls.go
+51
-0
core/core.go
core/core.go
+0
-1
importer/importer.go
importer/importer.go
+20
-0
No files found.
cli/add.go
View file @
5abf3c2a
...
...
@@ -6,11 +6,16 @@ import (
"github.com/jbenet/commander"
core
"github.com/jbenet/go-ipfs/core"
importer
"github.com/jbenet/go-ipfs/importer"
dag
"github.com/jbenet/go-ipfs/merkledag"
u
"github.com/jbenet/go-ipfs/util"
mh
"github.com/jbenet/go-multihash"
"io/ioutil"
"os"
"path/filepath"
)
var
DepthLimitExceeded
=
fmt
.
Errorf
(
"depth limit exceeded"
)
var
cmdIpfsAdd
=
&
commander
.
Command
{
UsageLine
:
"add"
,
Short
:
"Add an object to ipfs."
,
...
...
@@ -49,18 +54,26 @@ func addCmd(c *commander.Command, inp []string) error {
}
for
_
,
fpath
:=
range
inp
{
err
=
addPath
(
n
,
fpath
,
depth
)
_
,
err
:
=
addPath
(
n
,
fpath
,
depth
)
if
err
!=
nil
{
u
.
PErr
(
"error adding %s: %v
\n
"
,
fpath
,
err
)
if
!
recursive
{
return
fmt
.
Errorf
(
"%s is a directory. Use -r to add recursively."
,
fpath
)
}
else
{
u
.
PErr
(
"error adding %s: %v
\n
"
,
fpath
,
err
)
}
}
}
return
err
}
func
addPath
(
n
*
core
.
IpfsNode
,
fpath
string
,
depth
int
)
error
{
func
addPath
(
n
*
core
.
IpfsNode
,
fpath
string
,
depth
int
)
(
*
dag
.
Node
,
error
)
{
if
depth
==
0
{
return
nil
,
DepthLimitExceeded
}
fi
,
err
:=
os
.
Stat
(
fpath
)
if
err
!=
nil
{
return
err
return
nil
,
err
}
if
fi
.
IsDir
()
{
...
...
@@ -70,33 +83,43 @@ func addPath(n *core.IpfsNode, fpath string, depth int) error {
}
}
func
addDir
(
n
*
core
.
IpfsNode
,
fpath
string
,
depth
int
)
error
{
return
u
.
NotImplementedError
}
func
addDir
(
n
*
core
.
IpfsNode
,
fpath
string
,
depth
int
)
(
*
dag
.
Node
,
error
)
{
tree
:=
&
dag
.
Node
{}
func
addFile
(
n
*
core
.
IpfsNode
,
fpath
string
,
depth
int
)
error
{
stat
,
err
:=
os
.
Stat
(
fpath
)
files
,
err
:=
ioutil
.
ReadDir
(
fpath
)
if
err
!=
nil
{
return
err
return
nil
,
err
}
if
stat
.
IsDir
()
{
return
fmt
.
Errorf
(
"addFile: `fpath` is a directory"
)
}
// construct nodes for containing files.
for
_
,
f
:=
range
files
{
fp
:=
filepath
.
Join
(
fpath
,
f
.
Name
())
nd
,
err
:=
addPath
(
n
,
fp
,
depth
-
1
)
if
err
!=
nil
{
return
nil
,
err
}
f
,
err
:
=
os
.
Open
(
fpath
)
if
err
!=
nil
{
return
err
i
f
err
=
tree
.
AddNodeLink
(
f
.
Name
(),
nd
);
err
!=
nil
{
return
nil
,
err
}
}
defer
f
.
Close
()
root
,
err
:=
importer
.
NewDagFromReader
(
f
,
stat
.
Size
())
return
tree
,
addNode
(
n
,
tree
,
fpath
)
}
func
addFile
(
n
*
core
.
IpfsNode
,
fpath
string
,
depth
int
)
(
*
dag
.
Node
,
error
)
{
root
,
err
:=
importer
.
NewDagFromFile
(
fpath
)
if
err
!=
nil
{
return
err
return
nil
,
err
}
return
root
,
addNode
(
n
,
root
,
fpath
)
}
// addNode adds the node to the graph + local storage
func
addNode
(
n
*
core
.
IpfsNode
,
nd
*
dag
.
Node
,
fpath
string
)
error
{
// add the file to the graph + local storage
k
,
err
:=
n
.
AddDagNode
(
root
)
k
,
err
:=
n
.
AddDagNode
(
nd
)
if
err
!=
nil
{
return
err
}
...
...
cli/ipfs.go
View file @
5abf3c2a
...
...
@@ -38,6 +38,7 @@ Use "ipfs help <command>" for more information about a command.
Subcommands
:
[]
*
commander
.
Command
{
cmdIpfsAdd
,
cmdIpfsCat
,
cmdIpfsLs
,
cmdIpfsVersion
,
cmdIpfsCommands
,
},
...
...
cli/ls.go
0 → 100644
View file @
5abf3c2a
package
main
import
(
"github.com/gonuts/flag"
"github.com/jbenet/commander"
u
"github.com/jbenet/go-ipfs/util"
mh
"github.com/jbenet/go-multihash"
)
var
cmdIpfsLs
=
&
commander
.
Command
{
UsageLine
:
"ls"
,
Short
:
"List links from an object."
,
Long
:
`ipfs ls <ipfs-path> - List links from an object.
Retrieves the object named by <ipfs-path> and displays the links
it contains, with the following format:
<link base58 hash> <link size in bytes> <link name>
`
,
Run
:
lsCmd
,
Flag
:
*
flag
.
NewFlagSet
(
"ipfs-ls"
,
flag
.
ExitOnError
),
}
func
lsCmd
(
c
*
commander
.
Command
,
inp
[]
string
)
error
{
if
len
(
inp
)
<
1
{
u
.
POut
(
c
.
Long
)
return
nil
}
// for now only hashes, no path resolution
h
,
err
:=
mh
.
FromB58String
(
inp
[
0
])
if
err
!=
nil
{
return
err
}
n
,
err
:=
localNode
()
if
err
!=
nil
{
return
err
}
nd
,
err
:=
n
.
GetDagNode
(
u
.
Key
(
h
))
if
err
!=
nil
{
return
err
}
for
_
,
link
:=
range
nd
.
Links
{
u
.
POut
(
"%s %d %s
\n
"
,
link
.
Hash
.
B58String
(),
link
.
Size
,
link
.
Name
)
}
return
nil
}
core/core.go
View file @
5abf3c2a
...
...
@@ -11,7 +11,6 @@ import (
)
// IPFS Core module. It represents an IPFS instance.
type
IpfsNode
struct
{
// the node's configuration
...
...
importer/importer.go
View file @
5abf3c2a
...
...
@@ -5,6 +5,7 @@ import (
dag
"github.com/jbenet/go-ipfs/merkledag"
"io"
"io/ioutil"
"os"
)
var
BlockSizeLimit
=
int64
(
1048576
)
// 1 MB
...
...
@@ -38,3 +39,22 @@ func NewDagFromReader(r io.Reader, size int64) (*dag.Node, error) {
// no children for now because not block splitting yet
return
root
,
nil
}
func
NewDagFromFile
(
fpath
string
)
(
*
dag
.
Node
,
error
)
{
stat
,
err
:=
os
.
Stat
(
fpath
)
if
err
!=
nil
{
return
nil
,
err
}
if
stat
.
IsDir
()
{
return
nil
,
fmt
.
Errorf
(
"`fpath` is a directory"
)
}
f
,
err
:=
os
.
Open
(
fpath
)
if
err
!=
nil
{
return
nil
,
err
}
defer
f
.
Close
()
return
NewDagFromReader
(
f
,
stat
.
Size
())
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment