Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit c515184

Browse files
committed
feat: allow for truncating files
License: MIT Signed-off-by: achingbrain <[email protected]>
1 parent 6cf130f commit c515184

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

src/core/write/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ module.exports = function mfsWrite (ipfs) {
119119
return callback(new Error('cannot have negative byte count'))
120120
}
121121

122-
if (options.length === 0) {
122+
if (options.length === 0 && !options.truncate) {
123123
return callback()
124124
}
125125

src/core/write/truncate-node.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict'
2+
3+
const exporter = require('ipfs-unixfs-engine').exporter
4+
const importer = require('ipfs-unixfs-engine').importer
5+
const pull = require('pull-stream/pull')
6+
const values = require('pull-stream/sources/values')
7+
const asyncMap = require('pull-stream/throughs/async-map')
8+
const collect = require('pull-stream/sinks/collect')
9+
const log = require('debug')('mfs:write:truncate-node')
10+
const {
11+
loadNode
12+
} = require('../utils')
13+
14+
const truncateNode = (ipfs, dagNode, newLength, options, callback) => {
15+
log(`Truncating ${dagNode.multihash} to ${newLength} bytes`)
16+
17+
pull(
18+
exporter(dagNode.multihash, ipfs._ipld, {
19+
offset: 0,
20+
length: newLength
21+
}),
22+
asyncMap((file, cb) => {
23+
pull(
24+
values([{
25+
content: file.content
26+
}]),
27+
importer(ipfs._ipld, {
28+
progress: options.progress,
29+
hashAlg: options.hash,
30+
cidVersion: options.cidVersion,
31+
strategy: options.strategy
32+
}),
33+
collect(cb)
34+
)
35+
}),
36+
asyncMap((imported, cb) => loadNode(ipfs, imported[0], cb)),
37+
collect((error, results) => {
38+
callback(error, results.pop())
39+
})
40+
)
41+
}
42+
43+
module.exports = truncateNode

src/core/write/update-node.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const {
2525
} = require('../utils')
2626
const importNode = require('./import-node')
2727
const updateNodeBytes = require('./update-tree')
28+
const truncateNode = require('./truncate-node')
2829

2930
const updateNode = (ipfs, cidToUpdate, source, options, callback) => {
3031
let offset = options.offset || 0
@@ -159,6 +160,13 @@ const updateNode = (ipfs, cidToUpdate, source, options, callback) => {
159160
}
160161

161162
next(null, updatedNode)
163+
},
164+
(updatedNode, cb) => {
165+
if (options.truncate) {
166+
return truncateNode(ipfs, updatedNode, streamEnd, options, cb)
167+
}
168+
169+
cb(null, updatedNode)
162170
}
163171
], done)
164172
}

test/write.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ describe('write', function () {
369369
})
370370

371371
runTest(({type, path, content}) => {
372-
it.skip(`truncates a file when requested (${type})`, () => {
372+
it(`truncates a file after writing (${type})`, () => {
373373
const newContent = Buffer.from('Oh hai!')
374374

375375
return mfs.write(path, content, {

0 commit comments

Comments
 (0)