Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
c10e329b
提交
c10e329b
authored
3月 02, 2016
作者:
David Dias
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
repo-stat
License: MIT Signed-off-by:
David Dias
<
daviddias.p@gmail.com
>
上级
288f7bc6
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
99 行增加
和
43 行删除
+99
-43
repo.go
core/commands/repo.go
+31
-43
stat.go
core/corerepo/stat.go
+43
-0
t0080-repo.sh
test/sharness/t0080-repo.sh
+25
-0
没有找到文件。
core/commands/repo.go
浏览文件 @
c10e329b
...
@@ -3,22 +3,12 @@ package commands
...
@@ -3,22 +3,12 @@ package commands
import
(
import
(
"bytes"
"bytes"
"fmt"
"fmt"
"io"
"strings"
humanize
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/dustin/go-humanize"
cmds
"github.com/ipfs/go-ipfs/commands"
cmds
"github.com/ipfs/go-ipfs/commands"
corerepo
"github.com/ipfs/go-ipfs/core/corerepo"
corerepo
"github.com/ipfs/go-ipfs/core/corerepo"
fsrepo
"github.com/ipfs/go-ipfs/repo/fsrepo"
u
"gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
u
"gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
"io"
)
)
type
RepoStat
struct
{
repoPath
string
repoSize
uint64
// size in bytes
numBlocks
uint64
}
var
RepoCmd
=
&
cmds
.
Command
{
var
RepoCmd
=
&
cmds
.
Command
{
Helptext
:
cmds
.
HelpText
{
Helptext
:
cmds
.
HelpText
{
Tagline
:
"Manipulate the IPFS repo."
,
Tagline
:
"Manipulate the IPFS repo."
,
...
@@ -108,59 +98,57 @@ order to reclaim hard disk space.
...
@@ -108,59 +98,57 @@ order to reclaim hard disk space.
var
repoStatCmd
=
&
cmds
.
Command
{
var
repoStatCmd
=
&
cmds
.
Command
{
Helptext
:
cmds
.
HelpText
{
Helptext
:
cmds
.
HelpText
{
Tagline
:
"Print status of the local repo."
,
Tagline
:
"Get stats for the currently used repo."
,
ShortDescription
:
``
,
ShortDescription
:
`
'ipfs repo stat' is a plumbing command that will scan the local
set of stored objects and print repo statistics. It outputs to stdout:
NumObjects int number of objects in the local repo
RepoSize int size in bytes that the repo is currently taking
RepoPath string the path to the repo being currently used
`
,
},
},
Run
:
func
(
req
cmds
.
Request
,
res
cmds
.
Response
)
{
Run
:
func
(
req
cmds
.
Request
,
res
cmds
.
Response
)
{
ctx
:=
req
.
Context
()
n
,
err
:=
req
.
InvocContext
()
.
GetNode
()
n
,
err
:=
req
.
InvocContext
()
.
GetNode
()
if
err
!=
nil
{
if
err
!=
nil
{
res
.
SetError
(
err
,
cmds
.
ErrNormal
)
res
.
SetError
(
err
,
cmds
.
ErrNormal
)
return
return
}
}
usage
,
err
:=
n
.
Repo
.
GetStorageUsage
()
stat
,
err
:=
corerepo
.
RepoStat
(
n
,
req
.
Context
())
if
err
!=
nil
{
res
.
SetError
(
err
,
cmds
.
ErrNormal
)
return
}
allKeys
,
err
:=
n
.
Blockstore
.
AllKeysChan
(
ctx
)
if
err
!=
nil
{
res
.
SetError
(
err
,
cmds
.
ErrNormal
)
return
}
count
:=
uint64
(
0
)
for
range
allKeys
{
count
++
}
path
,
err
:=
fsrepo
.
BestKnownPath
()
if
err
!=
nil
{
if
err
!=
nil
{
res
.
SetError
(
err
,
cmds
.
ErrNormal
)
res
.
SetError
(
err
,
cmds
.
ErrNormal
)
return
return
}
}
res
.
SetOutput
(
&
RepoStat
{
res
.
SetOutput
(
stat
)
repoPath
:
path
,
repoSize
:
usage
,
numBlocks
:
count
,
})
},
},
Type
:
RepoStat
{},
Options
:
[]
cmds
.
Option
{
cmds
.
BoolOption
(
"human"
,
"Output RepoSize in MiB."
),
},
Type
:
corerepo
.
Stat
{},
Marshalers
:
cmds
.
MarshalerMap
{
Marshalers
:
cmds
.
MarshalerMap
{
cmds
.
Text
:
func
(
res
cmds
.
Response
)
(
io
.
Reader
,
error
)
{
cmds
.
Text
:
func
(
res
cmds
.
Response
)
(
io
.
Reader
,
error
)
{
stat
,
ok
:=
res
.
Output
()
.
(
*
Repo
Stat
)
stat
,
ok
:=
res
.
Output
()
.
(
*
corerepo
.
Stat
)
if
!
ok
{
if
!
ok
{
return
nil
,
u
.
ErrCast
()
return
nil
,
u
.
ErrCast
()
}
}
out
:=
fmt
.
Sprintf
(
human
,
_
,
err
:=
res
.
Request
()
.
Option
(
"human"
)
.
Bool
()
"Path: %s
\n
Size: %s
\n
Blocks: %d
\n
"
,
if
err
!=
nil
{
stat
.
repoPath
,
humanize
.
Bytes
(
stat
.
repoSize
),
stat
.
numBlocks
)
return
nil
,
err
}
buf
:=
new
(
bytes
.
Buffer
)
fmt
.
Fprintf
(
buf
,
"NumObjects
\t
%d
\n
"
,
stat
.
NumObjects
)
sizeInMiB
:=
stat
.
RepoSize
/
(
1024
*
1024
)
if
human
&&
sizeInMiB
>
0
{
fmt
.
Fprintf
(
buf
,
"RepoSize (MiB)
\t
%d
\n
"
,
sizeInMiB
)
}
else
{
fmt
.
Fprintf
(
buf
,
"RepoSize
\t
%d
\n
"
,
stat
.
RepoSize
)
}
fmt
.
Fprintf
(
buf
,
"RepoPath
\t
%s
\n
"
,
stat
.
RepoPath
)
return
strings
.
NewReader
(
out
)
,
nil
return
buf
,
nil
},
},
},
},
}
}
core/corerepo/stat.go
0 → 100644
浏览文件 @
c10e329b
package
corerepo
import
(
"github.com/ipfs/go-ipfs/core"
fsrepo
"github.com/ipfs/go-ipfs/repo/fsrepo"
context
"gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
)
type
Stat
struct
{
NumObjects
uint64
RepoSize
uint64
// size in bytes
RepoPath
string
}
func
RepoStat
(
n
*
core
.
IpfsNode
,
ctx
context
.
Context
)
(
*
Stat
,
error
)
{
r
:=
n
.
Repo
usage
,
err
:=
r
.
GetStorageUsage
()
if
err
!=
nil
{
return
nil
,
err
}
allKeys
,
err
:=
n
.
Blockstore
.
AllKeysChan
(
ctx
)
if
err
!=
nil
{
return
nil
,
err
}
count
:=
uint64
(
0
)
for
range
allKeys
{
count
++
}
path
,
err
:=
fsrepo
.
BestKnownPath
()
if
err
!=
nil
{
return
nil
,
err
}
return
&
Stat
{
NumObjects
:
count
,
RepoSize
:
usage
,
RepoPath
:
path
,
},
nil
}
test/sharness/t0080-repo.sh
浏览文件 @
c10e329b
...
@@ -219,6 +219,31 @@ test_expect_success "'ipfs refs --unique --recursive (bigger)'" '
...
@@ -219,6 +219,31 @@ test_expect_success "'ipfs refs --unique --recursive (bigger)'" '
test_sort_cmp expected actual || test_fsh cat refs_output
test_sort_cmp expected actual || test_fsh cat refs_output
'
'
get_field_num
()
{
field
=
$1
file
=
$2
num
=
$(
grep
"
$field
"
"
$file
"
| awk
'{ print $2 }'
)
echo
$num
}
test_expect_success
"'ipfs repo stat' succeeds"
'
ipfs repo stat > repo-stats
'
test_expect_success
"repo stats came out correct"
'
grep "RepoPath" repo-stats &&
grep "RepoSize" repo-stats &&
grep "NumObjects" repo-stats
'
test_expect_success
"'ipfs repo stat' after adding a file"
'
ipfs add repo-stats &&
ipfs repo stat > repo-stats-2
'
test_expect_success
"repo stats are updated correctly"
'
test $(get_field_num "RepoSize" repo-stats-2) -ge $(get_field_num "RepoSize" repo-stats)
'
test_kill_ipfs_daemon
test_kill_ipfs_daemon
test_done
test_done
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论