@cryptotaxi247 / kubo / commits / 6bb02a59c

ls: option to report real file size

License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>

Łukasz Magiera committed Jan 8, 2019 at 17:39 UTC 6bb02a59c017746adc27a3dca5b49cd196046cf1
2 files changed +83 -9
core/commands/ls.go
+17 -8
@@ -44,6 +44,7 @@ type LsOutput struct {
44 const (
45 lsHeadersOptionNameTime = "headers"
46 lsResolveTypeOptionName = "resolve-type"
47 + lsResolveSizeOptionName = "resolve-size"
48 lsStreamOptionName = "stream"
49 )
50
@@ -66,6 +67,7 @@ The JSON output contains type information.
67 Options: []cmdkit.Option{
68 cmdkit.BoolOption(lsHeadersOptionNameTime, "v", "Print table headers (Hash, Size, Name)."),
69 cmdkit.BoolOption(lsResolveTypeOptionName, "Resolve linked objects to find out their types.").WithDefault(true),
70 + cmdkit.BoolOption(lsResolveSizeOptionName, "Resolve linked objects to find out their file size.").WithDefault(false),
71 cmdkit.BoolOption(lsStreamOptionName, "s", "Enable exprimental streaming of directory entries as they are traversed."),
72 },
73 Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
@@ -79,9 +81,10 @@ The JSON output contains type information.
81 return err
82 }
83
82 - resolve, _ := req.Options[lsResolveTypeOptionName].(bool)
84 + resolveType, _ := req.Options[lsResolveTypeOptionName].(bool)
85 + resolveSize, _ := req.Options[lsResolveSizeOptionName].(bool)
86 dserv := nd.DAG
84 - if !resolve {
87 + if !resolveType && !resolveSize {
88 offlineexch := offline.Exchange(nd.Blockstore)
89 bserv := blockservice.New(nd.Blockstore, offlineexch)
90 dserv = merkledag.NewDAGService(bserv)
@@ -131,7 +134,7 @@ The JSON output contains type information.
134 }
135 outputLinks := make([]LsLink, len(links))
136 for j, link := range links {
134 - lsLink, err := makeLsLink(req, dserv, resolve, link)
137 + lsLink, err := makeLsLink(req, dserv, resolveType, resolveSize, link)
138 if err != nil {
139 return err
140 }
@@ -165,7 +168,7 @@ The JSON output contains type information.
168 return linkResult.Err
169 }
170 link := linkResult.Link
168 - lsLink, err := makeLsLink(req, dserv, resolve, link)
171 + lsLink, err := makeLsLink(req, dserv, resolveType, resolveSize, link)
172 if err != nil {
173 return err
174 }
@@ -224,8 +227,9 @@ func makeDagNodeLinkResults(req *cmds.Request, dagnode ipld.Node) <-chan unixfs.
227 return linkResults
228 }
229
227 -func makeLsLink(req *cmds.Request, dserv ipld.DAGService, resolve bool, link *ipld.Link) (*LsLink, error) {
230 +func makeLsLink(req *cmds.Request, dserv ipld.DAGService, resolveType bool, resolveSize bool, link *ipld.Link) (*LsLink, error) {
231 t := unixfspb.Data_DataType(-1)
232 + size := link.Size
233
234 switch link.Cid.Type() {
235 case cid.Raw:
@@ -233,7 +237,7 @@ func makeLsLink(req *cmds.Request, dserv ipld.DAGService, resolve bool, link *ip
237 t = unixfs.TFile
238 case cid.DagProtobuf:
239 linkNode, err := link.GetNode(req.Context, dserv)
236 - if err == ipld.ErrNotFound && !resolve {
240 + if err == ipld.ErrNotFound && !resolveType && !resolveSize {
241 // not an error
242 linkNode = nil
243 } else if err != nil {
@@ -245,13 +249,18 @@ func makeLsLink(req *cmds.Request, dserv ipld.DAGService, resolve bool, link *ip
249 if err != nil {
250 return nil, err
251 }
248 - t = d.Type()
252 + if resolveType {
253 + t = d.Type()
254 + }
255 + if d.Type() == unixfs.TFile && resolveSize {
256 + size = d.FileSize()
257 + }
258 }
259 }
260 return &LsLink{
261 Name: link.Name,
262 Hash: link.Cid.String(),
254 - Size: link.Size,
263 + Size: size,
264 Type: t,
265 }, nil
266 }
test/sharness/t0045-ls.sh
+66 -1
@@ -61,6 +61,29 @@ EOF
61 test_cmp expected_ls actual_ls
62 '
63
64 + test_expect_success "'ipfs ls --resolve-size <three dir hashes>' succeeds" '
65 + ipfs ls --resolve-size QmfNy183bXiRVyrhyWtq3TwHn79yHEkiAGFr18P7YNzESj QmR3jhV4XpxxPjPT3Y8vNnWvWNvakdcT3H6vqpRBsX1MLy QmSix55yz8CzWXf5ZVM9vgEvijnEeeXiTSarVtsqiiCJss >actual_ls
66 + '
67 +
68 + test_expect_success "'ipfs ls <three dir hashes>' output looks good" '
69 + cat <<-\EOF >expected_ls &&
70 +QmfNy183bXiRVyrhyWtq3TwHn79yHEkiAGFr18P7YNzESj:
71 +QmSix55yz8CzWXf5ZVM9vgEvijnEeeXiTSarVtsqiiCJss 246 d1/
72 +QmR3jhV4XpxxPjPT3Y8vNnWvWNvakdcT3H6vqpRBsX1MLy 1143 d2/
73 +QmeomffUNfmQy76CQGy9NdmqEnnHU9soCexBnGU3ezPHVH 5 f1
74 +QmNtocSs7MoDkJMc1RkyisCSKvLadujPsfJfSdJ3e1eA1M 5 f2
75 +
76 +QmR3jhV4XpxxPjPT3Y8vNnWvWNvakdcT3H6vqpRBsX1MLy:
77 +QmbQBUSRL9raZtNXfpTDeaxQapibJEG6qEY8WqAN22aUzd 1024 1024
78 +QmaRGe7bVmVaLmxbrMiVNXqW4pRNNp3xq7hFtyRKA3mtJL 6 a
79 +
80 +QmSix55yz8CzWXf5ZVM9vgEvijnEeeXiTSarVtsqiiCJss:
81 +QmQNd6ubRXaNG6Prov8o6vk3bn6eWsj9FxLGrAVDUAGkGe 128 128
82 +QmZULkCELmmk5XNfCgTnCyFgAVxBRBXyDHGGMVoLFLiXEN 6 a
83 +EOF
84 + test_cmp expected_ls actual_ls
85 + '
86 +
87 test_expect_success "'ipfs ls --headers <three dir hashes>' succeeds" '
88 ipfs ls --headers QmfNy183bXiRVyrhyWtq3TwHn79yHEkiAGFr18P7YNzESj QmR3jhV4XpxxPjPT3Y8vNnWvWNvakdcT3H6vqpRBsX1MLy QmSix55yz8CzWXf5ZVM9vgEvijnEeeXiTSarVtsqiiCJss >actual_ls_headers
89 '
@@ -140,6 +163,29 @@ EOF
163 test_cmp expected_ls_stream actual_ls_stream
164 '
165
166 + test_expect_success "'ipfs ls --resolve-size --stream <three dir hashes>' succeeds" '
167 + ipfs ls --resolve-size --stream QmfNy183bXiRVyrhyWtq3TwHn79yHEkiAGFr18P7YNzESj QmR3jhV4XpxxPjPT3Y8vNnWvWNvakdcT3H6vqpRBsX1MLy QmSix55yz8CzWXf5ZVM9vgEvijnEeeXiTSarVtsqiiCJss >actual_ls_stream
168 + '
169 +
170 + test_expect_success "'ipfs ls --stream <three dir hashes>' output looks good" '
171 + cat <<-\EOF >expected_ls_stream &&
172 +QmfNy183bXiRVyrhyWtq3TwHn79yHEkiAGFr18P7YNzESj:
173 +QmSix55yz8CzWXf5ZVM9vgEvijnEeeXiTSarVtsqiiCJss 246 d1/
174 +QmR3jhV4XpxxPjPT3Y8vNnWvWNvakdcT3H6vqpRBsX1MLy 1143 d2/
175 +QmeomffUNfmQy76CQGy9NdmqEnnHU9soCexBnGU3ezPHVH 5 f1
176 +QmNtocSs7MoDkJMc1RkyisCSKvLadujPsfJfSdJ3e1eA1M 5 f2
177 +
178 +QmR3jhV4XpxxPjPT3Y8vNnWvWNvakdcT3H6vqpRBsX1MLy:
179 +QmbQBUSRL9raZtNXfpTDeaxQapibJEG6qEY8WqAN22aUzd 1024 1024
180 +QmaRGe7bVmVaLmxbrMiVNXqW4pRNNp3xq7hFtyRKA3mtJL 6 a
181 +
182 +QmSix55yz8CzWXf5ZVM9vgEvijnEeeXiTSarVtsqiiCJss:
183 +QmQNd6ubRXaNG6Prov8o6vk3bn6eWsj9FxLGrAVDUAGkGe 128 128
184 +QmZULkCELmmk5XNfCgTnCyFgAVxBRBXyDHGGMVoLFLiXEN 6 a
185 +EOF
186 + test_cmp expected_ls_stream actual_ls_stream
187 + '
188 +
189 test_expect_success "'ipfs ls --stream --headers <three dir hashes>' succeeds" '
190 ipfs ls --stream --headers QmfNy183bXiRVyrhyWtq3TwHn79yHEkiAGFr18P7YNzESj QmR3jhV4XpxxPjPT3Y8vNnWvWNvakdcT3H6vqpRBsX1MLy QmSix55yz8CzWXf5ZVM9vgEvijnEeeXiTSarVtsqiiCJss >actual_ls_stream_headers
191 '
@@ -172,7 +218,7 @@ test_ls_cmd_raw_leaves() {
218 mkdir -p somedir &&
219 echo bar > somedir/foo &&
220 ipfs add --raw-leaves -r somedir/ > /dev/null &&
175 - ipfs ls QmThNTdtKaVoCVrYmM5EBS6U3S5vfKFue2TxbxxAxRcKKE > ls-actual
221 + ipfs ls '$1' QmThNTdtKaVoCVrYmM5EBS6U3S5vfKFue2TxbxxAxRcKKE > ls-actual
222 echo "zb2rhf6GzX4ckKZtjy8yy8iyq1KttCrRyqDedD6xubhY3sw2F 4 foo" > ls-expect
223 test_cmp ls-actual ls-expect
224 '
@@ -187,12 +233,22 @@ test_ls_object() {
233 ipfs ls $HASH > ls-actual &&
234 test_cmp ls-actual ls-expect
235 '
236 +
237 + test_expect_success "ipfs add medium size file then 'ipfs ls --resolve-size' works as expected" '
238 + random 500000 2 > somefile &&
239 + HASH=$(ipfs add somefile -q) &&
240 + echo "QmPrM8S5T7Q3M8DQvQMS7m41m3Aq4jBjzAzvky5fH3xfr4 262144 " > ls-expect &&
241 + echo "QmdaAntAzQqqVMo4B8V69nkQd5d918YjHXUe2oF6hr72ri 237856 " >> ls-expect &&
242 + ipfs ls --resolve-size $HASH > ls-actual &&
243 + test_cmp ls-actual ls-expect
244 + '
245 }
246
247 # should work offline
248 test_ls_cmd
249 test_ls_cmd_streaming
250 test_ls_cmd_raw_leaves
251 +test_ls_cmd_raw_leaves --resolve-size
252 test_ls_object
253
254 # should work online
@@ -200,6 +256,7 @@ test_launch_ipfs_daemon
256 test_ls_cmd
257 test_ls_cmd_streaming
258 test_ls_cmd_raw_leaves
259 +test_ls_cmd_raw_leaves --resolve-size
260 test_kill_ipfs_daemon
261 test_ls_object
262
@@ -233,6 +290,10 @@ test_expect_success "'ipfs ls' fails" '
290 test_must_fail ipfs ls $DIR
291 '
292
293 +test_expect_success "'ipfs ls --resolve-type=false --resolve-size=true' fails" '
294 + test_must_fail ipfs ls --resolve-type=false --resolve-size=true $DIR
295 +'
296 +
297 test_launch_ipfs_daemon --offline
298
299 test_expect_success "'ipfs ls --resolve-type=false' ok" '
@@ -243,6 +304,10 @@ test_expect_success "'ipfs ls' fails" '
304 test_must_fail ipfs ls $DIR
305 '
306
307 +test_expect_success "'ipfs ls --resolve-type=false --resolve-size=true' fails" '
308 + test_must_fail ipfs ls --resolve-type=false --resolve-size=true $DIR
309 +'
310 +
311 test_kill_ipfs_daemon
312
313 test_launch_ipfs_daemon