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
25eeb1e2
Unverified
Commit
25eeb1e2
authored
Feb 08, 2018
by
Whyrusleeping
Committed by
GitHub
Feb 08, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #4638 from MichaelMure/statlocal
add a --with-local option to ipfs files stat
parents
990e4df3
30d4eaa0
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
165 additions
and
113 deletions
+165
-113
core/commands/files.go
core/commands/files.go
+155
-111
core/commands/root.go
core/commands/root.go
+1
-2
test/sharness/t0250-files-api.sh
test/sharness/t0250-files-api.sh
+9
-0
No files found.
core/commands/files
/files
.go
→
core/commands/files.go
View file @
25eeb1e2
...
...
@@ -10,24 +10,30 @@ import (
gopath
"path"
"strings"
cmds
"github.com/ipfs/go-ipfs/commands"
bservice
"github.com/ipfs/go-ipfs/blockservice"
oldcmds
"github.com/ipfs/go-ipfs/commands"
lgc
"github.com/ipfs/go-ipfs/commands/legacy"
core
"github.com/ipfs/go-ipfs/core"
e
"github.com/ipfs/go-ipfs/core/commands/e"
"github.com/ipfs/go-ipfs/exchange/offline"
dag
"github.com/ipfs/go-ipfs/merkledag"
mfs
"github.com/ipfs/go-ipfs/mfs"
path
"github.com/ipfs/go-ipfs/path"
ft
"github.com/ipfs/go-ipfs/unixfs"
uio
"github.com/ipfs/go-ipfs/unixfs/io"
humanize
"gx/ipfs/QmPSBJL4momYnE7DcUyk2DVhD6rH488ZmHBGLbxNdhU44K/go-humanize"
logging
"gx/ipfs/QmRb5jh8z2E8hMGN2tkvs1yHynUanqnZ3UeKwgN1i9P1F8/go-log"
cmds
"gx/ipfs/QmZ9hww8R3FKrDRCYPxhN13m6XgjPDpaSvdUfisPvERzXz/go-ipfs-cmds"
mh
"gx/ipfs/QmZyZDi491cCNTLfAhwcaDii2Kg4pwKRkhqQzURGDvY6ua/go-multihash"
cid
"gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid"
cmdkit
"gx/ipfs/QmceUdzxkimdYsgtX733uNgzf1DLHyBKN6ehGSp85ayppM/go-ipfs-cmdkit"
ipld
"gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format"
)
var
log
=
logging
.
Logger
(
"cmds/files"
)
var
f
log
=
logging
.
Logger
(
"cmds/files"
)
// FilesCmd is the 'ipfs files' command
var
FilesCmd
=
&
cmds
.
Command
{
Helptext
:
cmdkit
.
HelpText
{
Tagline
:
"Interact with unixfs files."
,
...
...
@@ -49,23 +55,34 @@ operations.
cmdkit
.
BoolOption
(
"f"
,
"flush"
,
"Flush target and ancestors after write."
)
.
WithDefault
(
true
),
},
Subcommands
:
map
[
string
]
*
cmds
.
Command
{
"read"
:
F
ilesReadCmd
,
"write"
:
F
ilesWriteCmd
,
"mv"
:
F
ilesMvCmd
,
"cp"
:
F
ilesCpCmd
,
"ls"
:
F
ilesLsCmd
,
"mkdir"
:
F
ilesMkdirCmd
,
"stat"
:
F
ilesStatCmd
,
"rm"
:
F
ilesRmCmd
,
"flush"
:
F
ilesFlushCmd
,
"chcid"
:
F
ilesChcidCmd
,
"read"
:
lgc
.
NewCommand
(
f
ilesReadCmd
)
,
"write"
:
lgc
.
NewCommand
(
f
ilesWriteCmd
)
,
"mv"
:
lgc
.
NewCommand
(
f
ilesMvCmd
)
,
"cp"
:
lgc
.
NewCommand
(
f
ilesCpCmd
)
,
"ls"
:
lgc
.
NewCommand
(
f
ilesLsCmd
)
,
"mkdir"
:
lgc
.
NewCommand
(
f
ilesMkdirCmd
)
,
"stat"
:
f
ilesStatCmd
,
"rm"
:
lgc
.
NewCommand
(
f
ilesRmCmd
)
,
"flush"
:
lgc
.
NewCommand
(
f
ilesFlushCmd
)
,
"chcid"
:
lgc
.
NewCommand
(
f
ilesChcidCmd
)
,
},
}
var
cidVersionOption
=
cmdkit
.
IntOption
(
"cid-version"
,
"cid-ver"
,
"Cid version to use. (experimental)"
)
var
hashOption
=
cmdkit
.
StringOption
(
"hash"
,
"Hash function to use. Will set Cid version to 1 if used. (experimental)"
)
var
formatError
=
errors
.
New
(
"Format was set by multiple options. Only one format option is allowed"
)
var
errFormat
=
errors
.
New
(
"format was set by multiple options. Only one format option is allowed"
)
type
statOutput
struct
{
Hash
string
Size
uint64
CumulativeSize
uint64
Blocks
int
Type
string
WithLocality
bool
`json:",omitempty"`
Local
bool
`json:",omitempty"`
SizeLocal
uint64
`json:",omitempty"`
}
const
defaultStatFormat
=
`<hash>
Size: <size>
...
...
@@ -73,7 +90,7 @@ CumulativeSize: <cumulsize>
ChildBlocks: <childs>
Type: <type>`
var
F
ilesStatCmd
=
&
cmds
.
Command
{
var
f
ilesStatCmd
=
&
cmds
.
Command
{
Helptext
:
cmdkit
.
HelpText
{
Tagline
:
"Display file status."
,
},
...
...
@@ -86,79 +103,101 @@ var FilesStatCmd = &cmds.Command{
"<hash> <size> <cumulsize> <type> <childs>. Conflicts with other format options."
)
.
WithDefault
(
defaultStatFormat
),
cmdkit
.
BoolOption
(
"hash"
,
"Print only hash. Implies '--format=<hash>'. Conflicts with other format options."
),
cmdkit
.
BoolOption
(
"size"
,
"Print only size. Implies '--format=<cumulsize>'. Conflicts with other format options."
),
cmdkit
.
BoolOption
(
"with-local"
,
"Compute the amount of the dag that is local, and if possible the total size"
),
},
Run
:
func
(
req
cmds
.
Request
,
res
cmds
.
Response
)
{
Run
:
func
(
req
*
cmds
.
Request
,
res
cmds
.
Response
Emitter
,
env
cmds
.
Environment
)
{
_
,
err
:=
statGetFormatOptions
(
req
)
if
err
!=
nil
{
res
.
SetError
(
err
,
cmdkit
.
ErrClient
)
}
node
,
err
:=
req
.
InvocContext
()
.
GetNode
()
node
,
err
:=
GetNode
(
env
)
if
err
!=
nil
{
res
.
SetError
(
err
,
cmdkit
.
ErrNormal
)
return
}
path
,
err
:=
checkPath
(
req
.
Arguments
()
[
0
])
path
,
err
:=
checkPath
(
req
.
Arguments
[
0
])
if
err
!=
nil
{
res
.
SetError
(
err
,
cmdkit
.
ErrNormal
)
return
}
fs
n
,
err
:=
mfs
.
Lookup
(
node
.
FilesRoot
,
path
)
n
d
,
err
:=
getNodeFromPath
(
req
.
Context
,
node
,
path
)
if
err
!=
nil
{
res
.
SetError
(
err
,
cmdkit
.
ErrNormal
)
return
}
o
,
err
:=
statNode
(
n
ode
.
DAG
,
fsn
)
o
,
err
:=
statNode
(
n
d
)
if
err
!=
nil
{
res
.
SetError
(
err
,
cmdkit
.
ErrNormal
)
return
}
res
.
SetOutput
(
o
)
},
Marshalers
:
cmds
.
MarshalerMap
{
cmds
.
Text
:
func
(
res
cmds
.
Response
)
(
io
.
Reader
,
error
)
{
v
,
err
:=
unwrapOutput
(
res
.
Output
())
if
err
!=
nil
{
return
nil
,
err
}
withLocal
,
_
:=
req
.
Options
[
"with-local"
]
.
(
bool
)
if
!
withLocal
{
res
.
Emit
(
o
)
return
}
// an offline DAGService will not fetch from the network
dagserv
:=
dag
.
NewDAGService
(
bservice
.
New
(
node
.
Blockstore
,
offline
.
Exchange
(
node
.
Blockstore
),
))
local
,
sizeLocal
,
err
:=
walkBlock
(
req
.
Context
,
dagserv
,
nd
)
o
.
WithLocality
=
true
o
.
Local
=
local
o
.
SizeLocal
=
sizeLocal
out
,
ok
:=
v
.
(
*
Object
)
res
.
Emit
(
o
)
},
Encoders
:
cmds
.
EncoderMap
{
cmds
.
Text
:
cmds
.
MakeEncoder
(
func
(
req
*
cmds
.
Request
,
w
io
.
Writer
,
v
interface
{})
error
{
out
,
ok
:=
v
.
(
*
statOutput
)
if
!
ok
{
return
nil
,
e
.
TypeErr
(
out
,
v
)
return
e
.
TypeErr
(
out
,
v
)
}
buf
:=
new
(
bytes
.
Buffer
)
s
,
_
:=
statGetFormatOptions
(
re
s
.
Request
()
)
s
,
_
:=
statGetFormatOptions
(
re
q
)
s
=
strings
.
Replace
(
s
,
"<hash>"
,
out
.
Hash
,
-
1
)
s
=
strings
.
Replace
(
s
,
"<size>"
,
fmt
.
Sprintf
(
"%d"
,
out
.
Size
),
-
1
)
s
=
strings
.
Replace
(
s
,
"<cumulsize>"
,
fmt
.
Sprintf
(
"%d"
,
out
.
CumulativeSize
),
-
1
)
s
=
strings
.
Replace
(
s
,
"<childs>"
,
fmt
.
Sprintf
(
"%d"
,
out
.
Blocks
),
-
1
)
s
=
strings
.
Replace
(
s
,
"<type>"
,
out
.
Type
,
-
1
)
fmt
.
Fprintln
(
buf
,
s
)
return
buf
,
nil
},
fmt
.
Fprintln
(
w
,
s
)
if
out
.
WithLocality
{
fmt
.
Fprintf
(
w
,
"Local: %s of %s (%.2f%%)
\n
"
,
humanize
.
Bytes
(
out
.
SizeLocal
),
humanize
.
Bytes
(
out
.
CumulativeSize
),
100.0
*
float64
(
out
.
SizeLocal
)
/
float64
(
out
.
CumulativeSize
),
)
}
return
nil
}),
},
Type
:
Objec
t
{},
Type
:
statOutpu
t
{},
}
func
moreThanOne
(
a
,
b
,
c
bool
)
bool
{
return
a
&&
b
||
b
&&
c
||
a
&&
c
}
func
statGetFormatOptions
(
req
cmds
.
Request
)
(
string
,
error
)
{
func
statGetFormatOptions
(
req
*
cmds
.
Request
)
(
string
,
error
)
{
hash
,
_
,
_
:=
req
.
Option
(
"hash"
)
.
B
ool
(
)
size
,
_
,
_
:=
req
.
Option
(
"size"
)
.
B
ool
(
)
format
,
_
,
_
:=
req
.
Option
(
"format"
)
.
S
tring
(
)
hash
,
_
:=
req
.
Option
s
[
"hash"
]
.
(
b
ool
)
size
,
_
:=
req
.
Option
s
[
"size"
]
.
(
b
ool
)
format
,
_
:=
req
.
Option
s
[
"format"
]
.
(
s
tring
)
if
moreThanOne
(
hash
,
size
,
format
!=
defaultStatFormat
)
{
return
""
,
f
ormat
Error
return
""
,
errF
ormat
}
if
hash
{
...
...
@@ -170,12 +209,7 @@ func statGetFormatOptions(req cmds.Request) (string, error) {
}
}
func
statNode
(
ds
ipld
.
DAGService
,
fsn
mfs
.
FSNode
)
(
*
Object
,
error
)
{
nd
,
err
:=
fsn
.
GetNode
()
if
err
!=
nil
{
return
nil
,
err
}
func
statNode
(
nd
ipld
.
Node
)
(
*
statOutput
,
error
)
{
c
:=
nd
.
Cid
()
cumulsize
,
err
:=
nd
.
Size
()
...
...
@@ -191,16 +225,16 @@ func statNode(ds ipld.DAGService, fsn mfs.FSNode) (*Object, error) {
}
var
ndtype
string
switch
fsn
.
Type
()
{
case
mfs
.
TDir
:
switch
d
.
Get
Type
()
{
case
ft
.
TDir
ectory
,
ft
.
THAMTShard
:
ndtype
=
"directory"
case
mfs
.
TFile
:
case
ft
.
TFile
,
ft
.
TMetadata
,
ft
.
TRaw
:
ndtype
=
"file"
default
:
return
nil
,
fmt
.
Errorf
(
"unrecognized node type: %s"
,
fsn
.
Type
())
return
nil
,
fmt
.
Errorf
(
"unrecognized node type: %s"
,
d
.
Get
Type
())
}
return
&
Objec
t
{
return
&
statOutpu
t
{
Hash
:
c
.
String
(),
Blocks
:
len
(
nd
.
Links
()),
Size
:
d
.
GetFilesize
(),
...
...
@@ -208,7 +242,7 @@ func statNode(ds ipld.DAGService, fsn mfs.FSNode) (*Object, error) {
Type
:
ndtype
,
},
nil
case
*
dag
.
RawNode
:
return
&
Objec
t
{
return
&
statOutpu
t
{
Hash
:
c
.
String
(),
Blocks
:
0
,
Size
:
cumulsize
,
...
...
@@ -220,7 +254,39 @@ func statNode(ds ipld.DAGService, fsn mfs.FSNode) (*Object, error) {
}
}
var
FilesCpCmd
=
&
cmds
.
Command
{
func
walkBlock
(
ctx
context
.
Context
,
dagserv
ipld
.
DAGService
,
nd
ipld
.
Node
)
(
bool
,
uint64
,
error
)
{
// Start with the block data size
sizeLocal
:=
uint64
(
len
(
nd
.
RawData
()))
local
:=
true
for
_
,
link
:=
range
nd
.
Links
()
{
child
,
err
:=
dagserv
.
Get
(
ctx
,
link
.
Cid
)
if
err
==
ipld
.
ErrNotFound
{
local
=
false
continue
}
if
err
!=
nil
{
return
local
,
sizeLocal
,
err
}
childLocal
,
childLocalSize
,
err
:=
walkBlock
(
ctx
,
dagserv
,
child
)
if
err
!=
nil
{
return
local
,
sizeLocal
,
err
}
// Recursively add the child size
local
=
local
&&
childLocal
sizeLocal
+=
childLocalSize
}
return
local
,
sizeLocal
,
nil
}
var
filesCpCmd
=
&
oldcmds
.
Command
{
Helptext
:
cmdkit
.
HelpText
{
Tagline
:
"Copy files into mfs."
,
},
...
...
@@ -228,7 +294,7 @@ var FilesCpCmd = &cmds.Command{
cmdkit
.
StringArg
(
"source"
,
true
,
false
,
"Source object to copy."
),
cmdkit
.
StringArg
(
"dest"
,
true
,
false
,
"Destination to copy object to."
),
},
Run
:
func
(
req
cmds
.
Request
,
res
cmds
.
Response
)
{
Run
:
func
(
req
old
cmds
.
Request
,
res
old
cmds
.
Response
)
{
node
,
err
:=
req
.
InvocContext
()
.
GetNode
()
if
err
!=
nil
{
res
.
SetError
(
err
,
cmdkit
.
ErrNormal
)
...
...
@@ -302,19 +368,11 @@ func getNodeFromPath(ctx context.Context, node *core.IpfsNode, p string) (ipld.N
}
}
type
Object
struct
{
Hash
string
Size
uint64
CumulativeSize
uint64
Blocks
int
Type
string
}
type
FilesLsOutput
struct
{
type
filesLsOutput
struct
{
Entries
[]
mfs
.
NodeListing
}
var
F
ilesLsCmd
=
&
cmds
.
Command
{
var
f
ilesLsCmd
=
&
old
cmds
.
Command
{
Helptext
:
cmdkit
.
HelpText
{
Tagline
:
"List directories in the local mutable namespace."
,
ShortDescription
:
`
...
...
@@ -341,7 +399,7 @@ Examples:
Options
:
[]
cmdkit
.
Option
{
cmdkit
.
BoolOption
(
"l"
,
"Use long listing format."
),
},
Run
:
func
(
req
cmds
.
Request
,
res
cmds
.
Response
)
{
Run
:
func
(
req
old
cmds
.
Request
,
res
old
cmds
.
Response
)
{
var
arg
string
if
len
(
req
.
Arguments
())
==
0
{
...
...
@@ -385,33 +443,33 @@ Examples:
Name
:
name
,
})
}
res
.
SetOutput
(
&
F
ilesLsOutput
{
output
})
res
.
SetOutput
(
&
f
ilesLsOutput
{
output
})
}
else
{
listing
,
err
:=
fsn
.
List
(
req
.
Context
())
if
err
!=
nil
{
res
.
SetError
(
err
,
cmdkit
.
ErrNormal
)
return
}
res
.
SetOutput
(
&
F
ilesLsOutput
{
listing
})
res
.
SetOutput
(
&
f
ilesLsOutput
{
listing
})
}
return
case
*
mfs
.
File
:
_
,
name
:=
gopath
.
Split
(
path
)
out
:=
&
F
ilesLsOutput
{[]
mfs
.
NodeListing
{
mfs
.
NodeListing
{
Name
:
name
,
Type
:
1
}}}
out
:=
&
f
ilesLsOutput
{[]
mfs
.
NodeListing
{
mfs
.
NodeListing
{
Name
:
name
,
Type
:
1
}}}
res
.
SetOutput
(
out
)
return
default
:
res
.
SetError
(
errors
.
New
(
"unrecognized type"
),
cmdkit
.
ErrNormal
)
}
},
Marshalers
:
cmds
.
MarshalerMap
{
cmds
.
Text
:
func
(
res
cmds
.
Response
)
(
io
.
Reader
,
error
)
{
Marshalers
:
old
cmds
.
MarshalerMap
{
old
cmds
.
Text
:
func
(
res
old
cmds
.
Response
)
(
io
.
Reader
,
error
)
{
v
,
err
:=
unwrapOutput
(
res
.
Output
())
if
err
!=
nil
{
return
nil
,
err
}
out
,
ok
:=
v
.
(
*
F
ilesLsOutput
)
out
,
ok
:=
v
.
(
*
f
ilesLsOutput
)
if
!
ok
{
return
nil
,
e
.
TypeErr
(
out
,
v
)
}
...
...
@@ -429,10 +487,10 @@ Examples:
return
buf
,
nil
},
},
Type
:
F
ilesLsOutput
{},
Type
:
f
ilesLsOutput
{},
}
var
F
ilesReadCmd
=
&
cmds
.
Command
{
var
f
ilesReadCmd
=
&
old
cmds
.
Command
{
Helptext
:
cmdkit
.
HelpText
{
Tagline
:
"Read a file in a given mfs."
,
ShortDescription
:
`
...
...
@@ -453,7 +511,7 @@ Examples:
cmdkit
.
IntOption
(
"offset"
,
"o"
,
"Byte offset to begin reading from."
),
cmdkit
.
IntOption
(
"count"
,
"n"
,
"Maximum number of bytes to read."
),
},
Run
:
func
(
req
cmds
.
Request
,
res
cmds
.
Response
)
{
Run
:
func
(
req
old
cmds
.
Request
,
res
old
cmds
.
Response
)
{
n
,
err
:=
req
.
InvocContext
()
.
GetNode
()
if
err
!=
nil
{
res
.
SetError
(
err
,
cmdkit
.
ErrNormal
)
...
...
@@ -474,7 +532,7 @@ Examples:
fi
,
ok
:=
fsn
.
(
*
mfs
.
File
)
if
!
ok
{
res
.
SetError
(
fmt
.
Errorf
(
"%s was not a file
.
"
,
path
),
cmdkit
.
ErrNormal
)
res
.
SetError
(
fmt
.
Errorf
(
"%s was not a file"
,
path
),
cmdkit
.
ErrNormal
)
return
}
...
...
@@ -492,7 +550,7 @@ Examples:
return
}
if
offset
<
0
{
res
.
SetError
(
fmt
.
Errorf
(
"
C
annot specify negative offset
.
"
),
cmdkit
.
ErrNormal
)
res
.
SetError
(
fmt
.
Errorf
(
"
c
annot specify negative offset"
),
cmdkit
.
ErrNormal
)
return
}
...
...
@@ -503,7 +561,7 @@ Examples:
}
if
int64
(
offset
)
>
filen
{
res
.
SetError
(
fmt
.
Errorf
(
"
O
ffset was past end of file (%d > %d)
.
"
,
offset
,
filen
),
cmdkit
.
ErrNormal
)
res
.
SetError
(
fmt
.
Errorf
(
"
o
ffset was past end of file (%d > %d)"
,
offset
,
filen
),
cmdkit
.
ErrNormal
)
return
}
...
...
@@ -521,7 +579,7 @@ Examples:
}
if
found
{
if
count
<
0
{
res
.
SetError
(
fmt
.
Errorf
(
"
C
annot specify negative 'count'
.
"
),
cmdkit
.
ErrNormal
)
res
.
SetError
(
fmt
.
Errorf
(
"
c
annot specify negative 'count'"
),
cmdkit
.
ErrNormal
)
return
}
r
=
io
.
LimitReader
(
r
,
int64
(
count
))
...
...
@@ -544,7 +602,7 @@ func (crw *contextReaderWrapper) Read(b []byte) (int, error) {
return
crw
.
R
.
CtxReadFull
(
crw
.
ctx
,
b
)
}
var
F
ilesMvCmd
=
&
cmds
.
Command
{
var
f
ilesMvCmd
=
&
old
cmds
.
Command
{
Helptext
:
cmdkit
.
HelpText
{
Tagline
:
"Move files."
,
ShortDescription
:
`
...
...
@@ -561,7 +619,7 @@ Example:
cmdkit
.
StringArg
(
"source"
,
true
,
false
,
"Source file to move."
),
cmdkit
.
StringArg
(
"dest"
,
true
,
false
,
"Destination path for file to be moved to."
),
},
Run
:
func
(
req
cmds
.
Request
,
res
cmds
.
Response
)
{
Run
:
func
(
req
old
cmds
.
Request
,
res
old
cmds
.
Response
)
{
n
,
err
:=
req
.
InvocContext
()
.
GetNode
()
if
err
!=
nil
{
res
.
SetError
(
err
,
cmdkit
.
ErrNormal
)
...
...
@@ -589,7 +647,7 @@ Example:
},
}
var
F
ilesWriteCmd
=
&
cmds
.
Command
{
var
f
ilesWriteCmd
=
&
old
cmds
.
Command
{
Helptext
:
cmdkit
.
HelpText
{
Tagline
:
"Write to a mutable file in a given filesystem."
,
ShortDescription
:
`
...
...
@@ -636,7 +694,7 @@ stat' on the file or any of its ancestors.
cidVersionOption
,
hashOption
,
},
Run
:
func
(
req
cmds
.
Request
,
res
cmds
.
Response
)
{
Run
:
func
(
req
old
cmds
.
Request
,
res
old
cmds
.
Response
)
{
path
,
err
:=
checkPath
(
req
.
StringArguments
()[
0
])
if
err
!=
nil
{
res
.
SetError
(
err
,
cmdkit
.
ErrNormal
)
...
...
@@ -711,7 +769,7 @@ stat' on the file or any of its ancestors.
_
,
err
=
wfd
.
Seek
(
int64
(
offset
),
io
.
SeekStart
)
if
err
!=
nil
{
log
.
Error
(
"seekfail: "
,
err
)
f
log
.
Error
(
"seekfail: "
,
err
)
res
.
SetError
(
err
,
cmdkit
.
ErrNormal
)
return
}
...
...
@@ -737,7 +795,7 @@ stat' on the file or any of its ancestors.
},
}
var
F
ilesMkdirCmd
=
&
cmds
.
Command
{
var
f
ilesMkdirCmd
=
&
old
cmds
.
Command
{
Helptext
:
cmdkit
.
HelpText
{
Tagline
:
"Make directories."
,
ShortDescription
:
`
...
...
@@ -763,7 +821,7 @@ Examples:
cidVersionOption
,
hashOption
,
},
Run
:
func
(
req
cmds
.
Request
,
res
cmds
.
Response
)
{
Run
:
func
(
req
old
cmds
.
Request
,
res
old
cmds
.
Response
)
{
n
,
err
:=
req
.
InvocContext
()
.
GetNode
()
if
err
!=
nil
{
res
.
SetError
(
err
,
cmdkit
.
ErrNormal
)
...
...
@@ -800,7 +858,7 @@ Examples:
},
}
var
F
ilesFlushCmd
=
&
cmds
.
Command
{
var
f
ilesFlushCmd
=
&
old
cmds
.
Command
{
Helptext
:
cmdkit
.
HelpText
{
Tagline
:
"Flush a given path's data to disk."
,
ShortDescription
:
`
...
...
@@ -811,7 +869,7 @@ are run with the '--flush=false'.
Arguments
:
[]
cmdkit
.
Argument
{
cmdkit
.
StringArg
(
"path"
,
false
,
false
,
"Path to flush. Default: '/'."
),
},
Run
:
func
(
req
cmds
.
Request
,
res
cmds
.
Response
)
{
Run
:
func
(
req
old
cmds
.
Request
,
res
old
cmds
.
Response
)
{
nd
,
err
:=
req
.
InvocContext
()
.
GetNode
()
if
err
!=
nil
{
res
.
SetError
(
err
,
cmdkit
.
ErrNormal
)
...
...
@@ -833,7 +891,7 @@ are run with the '--flush=false'.
},
}
var
F
ilesChcidCmd
=
&
cmds
.
Command
{
var
f
ilesChcidCmd
=
&
old
cmds
.
Command
{
Helptext
:
cmdkit
.
HelpText
{
Tagline
:
"Change the cid version or hash function of the root node of a given path."
,
ShortDescription
:
`
...
...
@@ -847,7 +905,7 @@ Change the cid version or hash function of the root node of a given path.
cidVersionOption
,
hashOption
,
},
Run
:
func
(
req
cmds
.
Request
,
res
cmds
.
Response
)
{
Run
:
func
(
req
old
cmds
.
Request
,
res
old
cmds
.
Response
)
{
nd
,
err
:=
req
.
InvocContext
()
.
GetNode
()
if
err
!=
nil
{
res
.
SetError
(
err
,
cmdkit
.
ErrNormal
)
...
...
@@ -901,7 +959,7 @@ func updatePath(rt *mfs.Root, pth string, prefix *cid.Prefix, flush bool) error
return
nil
}
var
F
ilesRmCmd
=
&
cmds
.
Command
{
var
f
ilesRmCmd
=
&
old
cmds
.
Command
{
Helptext
:
cmdkit
.
HelpText
{
Tagline
:
"Remove a file."
,
ShortDescription
:
`
...
...
@@ -922,7 +980,7 @@ Remove files or directories.
Options
:
[]
cmdkit
.
Option
{
cmdkit
.
BoolOption
(
"recursive"
,
"r"
,
"Recursively remove directories."
),
},
Run
:
func
(
req
cmds
.
Request
,
res
cmds
.
Response
)
{
Run
:
func
(
req
old
cmds
.
Request
,
res
old
cmds
.
Response
)
{
defer
res
.
SetOutput
(
nil
)
nd
,
err
:=
req
.
InvocContext
()
.
GetNode
()
...
...
@@ -956,7 +1014,7 @@ Remove files or directories.
pdir
,
ok
:=
parent
.
(
*
mfs
.
Directory
)
if
!
ok
{
res
.
SetError
(
fmt
.
Errorf
(
"
N
o such file or directory: %s"
,
path
),
cmdkit
.
ErrNormal
)
res
.
SetError
(
fmt
.
Errorf
(
"
n
o such file or directory: %s"
,
path
),
cmdkit
.
ErrNormal
)
return
}
...
...
@@ -1007,7 +1065,7 @@ Remove files or directories.
},
}
func
getPrefix
(
req
cmds
.
Request
)
(
*
cid
.
Prefix
,
error
)
{
func
getPrefix
(
req
old
cmds
.
Request
)
(
*
cid
.
Prefix
,
error
)
{
cidVer
,
cidVerSet
,
_
:=
req
.
Option
(
"cid-version"
)
.
Int
()
hashFunStr
,
hashFunSet
,
_
:=
req
.
Option
(
"hash"
)
.
String
()
...
...
@@ -1055,7 +1113,7 @@ func getFileHandle(r *mfs.Root, path string, create bool, prefix *cid.Prefix) (*
dirname
,
fname
:=
gopath
.
Split
(
path
)
pdiri
,
err
:=
mfs
.
Lookup
(
r
,
dirname
)
if
err
!=
nil
{
log
.
Error
(
"lookupfail "
,
dirname
)
f
log
.
Error
(
"lookupfail "
,
dirname
)
return
nil
,
err
}
pdir
,
ok
:=
pdiri
.
(
*
mfs
.
Directory
)
...
...
@@ -1080,7 +1138,7 @@ func getFileHandle(r *mfs.Root, path string, create bool, prefix *cid.Prefix) (*
fi
,
ok
:=
fsn
.
(
*
mfs
.
File
)
if
!
ok
{
return
nil
,
errors
.
New
(
"
E
xpected *mfs.File, didnt get it. This is likely a race condition
.
"
)
return
nil
,
errors
.
New
(
"
e
xpected *mfs.File, didnt get it. This is likely a race condition"
)
}
return
fi
,
nil
...
...
@@ -1091,11 +1149,11 @@ func getFileHandle(r *mfs.Root, path string, create bool, prefix *cid.Prefix) (*
func
checkPath
(
p
string
)
(
string
,
error
)
{
if
len
(
p
)
==
0
{
return
""
,
fmt
.
Errorf
(
"
P
aths must not be empty
.
"
)
return
""
,
fmt
.
Errorf
(
"
p
aths must not be empty"
)
}
if
p
[
0
]
!=
'/'
{
return
""
,
fmt
.
Errorf
(
"
P
aths must start with a leading slash
.
"
)
return
""
,
fmt
.
Errorf
(
"
p
aths must start with a leading slash"
)
}
cleaned
:=
gopath
.
Clean
(
p
)
...
...
@@ -1104,17 +1162,3 @@ func checkPath(p string) (string, error) {
}
return
cleaned
,
nil
}
// copy+pasted from ../commands.go
func
unwrapOutput
(
i
interface
{})
(
interface
{},
error
)
{
var
(
ch
<-
chan
interface
{}
ok
bool
)
if
ch
,
ok
=
i
.
(
<-
chan
interface
{});
!
ok
{
return
nil
,
e
.
TypeErr
(
ch
,
i
)
}
return
<-
ch
,
nil
}
core/commands/root.go
View file @
25eeb1e2
...
...
@@ -7,7 +7,6 @@ import (
oldcmds
"github.com/ipfs/go-ipfs/commands"
dag
"github.com/ipfs/go-ipfs/core/commands/dag"
e
"github.com/ipfs/go-ipfs/core/commands/e"
files
"github.com/ipfs/go-ipfs/core/commands/files"
ocmd
"github.com/ipfs/go-ipfs/core/commands/object"
unixfs
"github.com/ipfs/go-ipfs/core/commands/unixfs"
...
...
@@ -109,6 +108,7 @@ var rootSubcommands = map[string]*cmds.Command{
"block"
:
BlockCmd
,
"cat"
:
CatCmd
,
"commands"
:
CommandsDaemonCmd
,
"files"
:
FilesCmd
,
"filestore"
:
FileStoreCmd
,
"get"
:
GetCmd
,
"pubsub"
:
PubsubCmd
,
...
...
@@ -120,7 +120,6 @@ var rootSubcommands = map[string]*cmds.Command{
"dht"
:
lgc
.
NewCommand
(
DhtCmd
),
"diag"
:
lgc
.
NewCommand
(
DiagCmd
),
"dns"
:
lgc
.
NewCommand
(
DNSCmd
),
"files"
:
lgc
.
NewCommand
(
files
.
FilesCmd
),
"id"
:
lgc
.
NewCommand
(
IDCmd
),
"key"
:
lgc
.
NewCommand
(
KeyCmd
),
"log"
:
lgc
.
NewCommand
(
LogCmd
),
...
...
test/sharness/t0250-files-api.sh
View file @
25eeb1e2
...
...
@@ -154,6 +154,15 @@ test_files_api() {
ipfs files stat --hash / > roothash
'
test_expect_success
"stat works outside of MFS"
'
ipfs files stat /ipfs/$DIR1
'
test_expect_success
"stat compute the locality of a dag"
'
ipfs files stat --with-local /ipfs/$DIR1 > output
grep -q "(100.00%)" output
'
test_expect_success
"cannot mkdir /
$EXTRA
"
'
test_expect_code 1 ipfs files mkdir $ARGS /
'
...
...
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