Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
feat: allow for truncating files
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: achingbrain <alex@achingbrain.net>
  • Loading branch information
achingbrain committed May 8, 2018
1 parent 6cf130f commit c515184
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/core/write/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ module.exports = function mfsWrite (ipfs) {
return callback(new Error('cannot have negative byte count'))
}

if (options.length === 0) {
if (options.length === 0 && !options.truncate) {
return callback()
}

Expand Down
43 changes: 43 additions & 0 deletions src/core/write/truncate-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict'

const exporter = require('ipfs-unixfs-engine').exporter
const importer = require('ipfs-unixfs-engine').importer
const pull = require('pull-stream/pull')
const values = require('pull-stream/sources/values')
const asyncMap = require('pull-stream/throughs/async-map')
const collect = require('pull-stream/sinks/collect')
const log = require('debug')('mfs:write:truncate-node')
const {
loadNode
} = require('../utils')

const truncateNode = (ipfs, dagNode, newLength, options, callback) => {
log(`Truncating ${dagNode.multihash} to ${newLength} bytes`)

pull(
exporter(dagNode.multihash, ipfs._ipld, {
offset: 0,
length: newLength
}),
asyncMap((file, cb) => {
pull(
values([{
content: file.content
}]),
importer(ipfs._ipld, {
progress: options.progress,
hashAlg: options.hash,
cidVersion: options.cidVersion,
strategy: options.strategy
}),
collect(cb)
)
}),
asyncMap((imported, cb) => loadNode(ipfs, imported[0], cb)),
collect((error, results) => {
callback(error, results.pop())
})
)
}

module.exports = truncateNode
8 changes: 8 additions & 0 deletions src/core/write/update-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const {
} = require('../utils')
const importNode = require('./import-node')
const updateNodeBytes = require('./update-tree')
const truncateNode = require('./truncate-node')

const updateNode = (ipfs, cidToUpdate, source, options, callback) => {
let offset = options.offset || 0
Expand Down Expand Up @@ -159,6 +160,13 @@ const updateNode = (ipfs, cidToUpdate, source, options, callback) => {
}

next(null, updatedNode)
},
(updatedNode, cb) => {
if (options.truncate) {
return truncateNode(ipfs, updatedNode, streamEnd, options, cb)
}

cb(null, updatedNode)
}
], done)
}
Expand Down
2 changes: 1 addition & 1 deletion test/write.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ describe('write', function () {
})

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

return mfs.write(path, content, {
Expand Down

0 comments on commit c515184

Please sign in to comment.