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
9d88d2cd
Commit
9d88d2cd
authored
Jun 14, 2018
by
Hector Sanjuan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
RepoStat: address review comments
License: MIT Signed-off-by:
Hector Sanjuan
<
hector@protocol.ai
>
parent
37d49896
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
49 additions
and
39 deletions
+49
-39
core/commands/repo.go
core/commands/repo.go
+28
-25
core/corerepo/stat.go
core/corerepo/stat.go
+21
-14
No files found.
core/commands/repo.go
View file @
9d88d2cd
...
...
@@ -150,7 +150,7 @@ var repoStatCmd = &cmds.Command{
Helptext
:
cmdkit
.
HelpText
{
Tagline
:
"Get stats for the currently used repo."
,
ShortDescription
:
`
'ipfs repo stat' provides information about the local set of
'ipfs repo stat' provides information about the local set of
stored objects. It outputs:
RepoSize int Size in bytes that the repo is currently taking.
...
...
@@ -171,26 +171,33 @@ Version string The repo version.
return
}
statF
:=
corerepo
.
RepoStat
sizeOnly
,
_
:=
req
.
Options
[
"size-only"
]
.
(
bool
)
if
sizeOnly
{
statF
=
corerepo
.
RepoSize
sizeStat
,
err
:=
corerepo
.
RepoSize
(
req
.
Context
,
n
)
if
err
!=
nil
{
res
.
SetError
(
err
,
cmdkit
.
ErrNormal
)
return
}
cmds
.
EmitOnce
(
res
,
&
corerepo
.
Stat
{
SizeStat
:
sizeStat
,
})
return
}
stat
,
err
:=
s
tat
F
(
req
.
Context
,
n
)
stat
,
err
:=
corerepo
.
RepoS
tat
(
req
.
Context
,
n
)
if
err
!=
nil
{
res
.
SetError
(
err
,
cmdkit
.
ErrNormal
)
return
}
cmds
.
EmitOnce
(
res
,
stat
)
cmds
.
EmitOnce
(
res
,
&
stat
)
},
Type
:
corerepo
.
Stat
{},
Type
:
&
corerepo
.
Stat
{},
Encoders
:
cmds
.
EncoderMap
{
cmds
.
Text
:
cmds
.
MakeEncoder
(
func
(
req
*
cmds
.
Request
,
w
io
.
Writer
,
v
interface
{})
error
{
stat
,
ok
:=
v
.
(
*
corerepo
.
Stat
)
if
!
ok
{
fmt
.
Println
(
"adios"
)
return
e
.
TypeErr
(
stat
,
v
)
}
...
...
@@ -200,32 +207,28 @@ Version string The repo version.
human
,
_
:=
req
.
Options
[
"human"
]
.
(
bool
)
sizeOnly
,
_
:=
req
.
Options
[
"size-only"
]
.
(
bool
)
sizeInMiB
:=
stat
.
RepoSize
/
(
1024
*
1024
)
if
human
&&
sizeInMiB
>
0
{
fmt
.
Fprintf
(
wtr
,
"RepoSize (MiB):
\t
%d
\n
"
,
sizeInMiB
)
}
else
{
fmt
.
Fprintf
(
wtr
,
"RepoSize:
\t
%d
\n
"
,
stat
.
RepoSize
)
}
if
stat
.
StorageMax
!=
corerepo
.
NoLimit
{
maxSizeInMiB
:=
stat
.
StorageMax
/
(
1024
*
1024
)
if
human
&&
maxSizeInMiB
>
0
{
fmt
.
Fprintf
(
wtr
,
"StorageMax (MiB):
\t
%d
\n
"
,
maxSizeInMiB
)
printSize
:=
func
(
name
string
,
size
uint64
)
{
sizeInMiB
:=
size
/
(
1024
*
1024
)
if
human
&&
sizeInMiB
>
0
{
fmt
.
Fprintf
(
wtr
,
"%s (MiB):
\t
%d
\n
"
,
name
,
sizeInMiB
)
}
else
{
fmt
.
Fprintf
(
wtr
,
"
StorageMax:
\t
%d
\n
"
,
stat
.
StorageMax
)
fmt
.
Fprintf
(
wtr
,
"
%s:
\t
%d
\n
"
,
name
,
size
)
}
}
if
sizeOnly
{
return
nil
if
!
sizeOnly
{
fmt
.
Fprintf
(
wtr
,
"NumObjects:
\t
%d
\n
"
,
stat
.
NumObjects
)
}
fmt
.
Fprintf
(
wtr
,
"NumObjects:
\t
%d
\n
"
,
stat
.
NumObjects
)
fmt
.
Fprintf
(
wtr
,
"RepoPath:
\t
%s
\n
"
,
stat
.
RepoPath
)
fmt
.
Fprintf
(
wtr
,
"Version:
\t
%s
\n
"
,
stat
.
Version
)
printSize
(
"RepoSize"
,
stat
.
RepoSize
)
printSize
(
"StorageMax"
,
stat
.
StorageMax
)
return
nil
if
!
sizeOnly
{
fmt
.
Fprintf
(
wtr
,
"RepoPath:
\t
%s
\n
"
,
stat
.
RepoPath
)
fmt
.
Fprintf
(
wtr
,
"Version:
\t
%s
\n
"
,
stat
.
Version
)
}
return
nil
}),
},
}
...
...
core/corerepo/stat.go
View file @
9d88d2cd
...
...
@@ -12,10 +12,15 @@ import (
humanize
"gx/ipfs/QmPSBJL4momYnE7DcUyk2DVhD6rH488ZmHBGLbxNdhU44K/go-humanize"
)
// Stat wraps information about the
objects stored on disk
.
type
Stat
struct
{
//
Size
Stat wraps information about the
repository size and its limit
.
type
Size
Stat
struct
{
RepoSize
uint64
// size in bytes
StorageMax
uint64
// size in bytes
}
// Stat wraps information about the objects stored on disk.
type
Stat
struct
{
SizeStat
NumObjects
uint64
RepoPath
string
Version
string
...
...
@@ -25,15 +30,15 @@ type Stat struct {
const
NoLimit
uint64
=
math
.
MaxUint64
// RepoStat returns a *Stat object with all the fields set.
func
RepoStat
(
ctx
context
.
Context
,
n
*
core
.
IpfsNode
)
(
*
Stat
,
error
)
{
func
RepoStat
(
ctx
context
.
Context
,
n
*
core
.
IpfsNode
)
(
Stat
,
error
)
{
sizeStat
,
err
:=
RepoSize
(
ctx
,
n
)
if
err
!=
nil
{
return
nil
,
err
return
Stat
{}
,
err
}
allKeys
,
err
:=
n
.
Blockstore
.
AllKeysChan
(
ctx
)
if
err
!=
nil
{
return
nil
,
err
return
Stat
{}
,
err
}
count
:=
uint64
(
0
)
...
...
@@ -43,41 +48,43 @@ func RepoStat(ctx context.Context, n *core.IpfsNode) (*Stat, error) {
path
,
err
:=
fsrepo
.
BestKnownPath
()
if
err
!=
nil
{
return
nil
,
err
return
Stat
{}
,
err
}
return
&
Stat
{
return
Stat
{
SizeStat
:
SizeStat
{
RepoSize
:
sizeStat
.
RepoSize
,
StorageMax
:
sizeStat
.
StorageMax
,
},
NumObjects
:
count
,
RepoSize
:
sizeStat
.
RepoSize
,
StorageMax
:
sizeStat
.
StorageMax
,
RepoPath
:
path
,
Version
:
fmt
.
Sprintf
(
"fs-repo@%d"
,
fsrepo
.
RepoVersion
),
},
nil
}
// RepoSize returns a *Stat object with the RepoSize and StorageMax fields set.
func
RepoSize
(
ctx
context
.
Context
,
n
*
core
.
IpfsNode
)
(
*
Stat
,
error
)
{
func
RepoSize
(
ctx
context
.
Context
,
n
*
core
.
IpfsNode
)
(
Size
Stat
,
error
)
{
r
:=
n
.
Repo
cfg
,
err
:=
r
.
Config
()
if
err
!=
nil
{
return
nil
,
err
return
SizeStat
{}
,
err
}
usage
,
err
:=
r
.
GetStorageUsage
()
if
err
!=
nil
{
return
nil
,
err
return
SizeStat
{}
,
err
}
storageMax
:=
NoLimit
if
cfg
.
Datastore
.
StorageMax
!=
""
{
storageMax
,
err
=
humanize
.
ParseBytes
(
cfg
.
Datastore
.
StorageMax
)
if
err
!=
nil
{
return
nil
,
err
return
SizeStat
{}
,
err
}
}
return
&
Stat
{
return
Size
Stat
{
RepoSize
:
usage
,
StorageMax
:
storageMax
,
},
nil
...
...
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