This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b232a09
commit bad24b3
Showing
13 changed files
with
656 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
'use strict' | ||
|
||
const { | ||
} = require('./utils') | ||
|
||
module.exports = { | ||
command: 'ls <path>', | ||
|
||
describe: 'List directories in the local mutable namespace.', | ||
|
||
builder: { | ||
long: { | ||
alias: 'l', | ||
type: 'boolean', | ||
default: false, | ||
describe: 'Use long listing format.' | ||
} | ||
}, | ||
|
||
handler (argv) { | ||
let { | ||
path, | ||
ipfs, | ||
long | ||
} = argv | ||
|
||
ipfs.mfs.ls(path, { | ||
long | ||
}, (error, files) => { | ||
if (error) { | ||
throw error | ||
} | ||
|
||
files.forEach(link => { | ||
if (long) { | ||
return print(`${link.name} ${link.hash} ${link.size}`) | ||
} | ||
|
||
print(link.name) | ||
}) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
'use strict' | ||
|
||
const { | ||
} = require('./utils') | ||
|
||
module.exports = { | ||
command: 'mkdir <path>', | ||
|
||
describe: 'Make directories.', | ||
|
||
builder: { | ||
parents: { | ||
alias: 'p', | ||
type: 'boolean', | ||
default: false, | ||
describe: 'No error if existing, make parent directories as needed.' | ||
}, | ||
cidVersion: { | ||
alias: ['cid-ver', 'cid-version'], | ||
type: 'integer', | ||
describe: 'Cid version to use. (experimental).' | ||
}, | ||
hash: { | ||
type: 'string', | ||
describe: 'Hash function to use. Will set Cid version to 1 if used. (experimental).' | ||
}, | ||
flush: { | ||
alias: 'f', | ||
type: 'boolean', | ||
describe: 'Weird undocumented option' | ||
} | ||
}, | ||
|
||
handler (argv) { | ||
let { | ||
path, | ||
ipfs, | ||
parents, | ||
cidVersion, | ||
hash, | ||
flush | ||
} = argv | ||
|
||
ipfs.mfs.mkdir(path, { | ||
parents, | ||
cidVersion, | ||
hash, | ||
flush | ||
}, (error, result) => { | ||
if (error) { | ||
throw error | ||
} | ||
|
||
print(result) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict' | ||
|
||
let visible = true | ||
|
||
const disablePrinting = () => { | ||
visible = false | ||
} | ||
|
||
const print = (msg = '', newline = true) => { | ||
if (!visible) { | ||
return | ||
} | ||
|
||
msg = newline ? msg + '\n' : msg | ||
process.stdout.write(msg) | ||
} | ||
|
||
module.exports = { | ||
disablePrinting, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
'use strict' | ||
|
||
module.exports = { | ||
ls: require('./ls'), | ||
mkdir: require('./mkdir') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
'use strict' | ||
|
||
const exporter = require('ipfs-unixfs-engine').exporter | ||
const promisify = require('promisify-es6') | ||
const pull = require('pull-stream') | ||
const { | ||
collect | ||
} = pull | ||
const { | ||
withMfsRoot, | ||
validatePath | ||
} = require('./utils') | ||
|
||
const defaultOptions = { | ||
long: false | ||
} | ||
|
||
module.exports = function mfsLs (ipfs) { | ||
return promisify((path, options, callback) => { | ||
withMfsRoot(ipfs, (error, root) => { | ||
if (error) { | ||
return callback(error) | ||
} | ||
|
||
if (typeof options === 'function') { | ||
callback = options | ||
options = {} | ||
} | ||
|
||
options = Object.assign({}, defaultOptions, options) | ||
|
||
try { | ||
path = validatePath(path) | ||
root = root.toBaseEncodedString() | ||
} catch (error) { | ||
return callback(error) | ||
} | ||
|
||
pull( | ||
exporter(`/ipfs/${root}${path}`, ipfs._ipld), | ||
collect((error, results) => { | ||
if (error) { | ||
return callback(error) | ||
} | ||
|
||
if (!results || !results.length) { | ||
return callback(new Error('file does not exist')) | ||
} | ||
|
||
const files = (results[0].links || []).map(link => ({ | ||
name: link.name, | ||
type: link.type, | ||
size: link.size, | ||
hash: link.multihash | ||
})) | ||
|
||
callback(null, files) | ||
}) | ||
) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
'use strict' | ||
|
||
const UnixFS = require('ipfs-unixfs') | ||
const promisify = require('promisify-es6') | ||
const CID = require('cids') | ||
const bs58 = require('bs58') | ||
const log = require('debug')('mfs:mkdir') | ||
const dagPb = require('ipld-dag-pb') | ||
const { | ||
DAGNode, | ||
DAGLink | ||
} = dagPb | ||
const { | ||
waterfall, | ||
reduce | ||
} = require('async') | ||
const { | ||
withMfsRoot, | ||
updateMfsRoot, | ||
validatePath, | ||
FILE_SEPARATOR | ||
} = require('./utils') | ||
|
||
const defaultOptions = { | ||
parents: true, | ||
hash: undefined, | ||
cidVersion: undefined | ||
} | ||
|
||
const addLink = (ipfs, parent, child, name, callback) => { | ||
waterfall([ | ||
(done) => { | ||
DAGNode.rmLink(parent, name, done) | ||
}, | ||
(parent, done) => { | ||
DAGNode.addLink(parent, new DAGLink(name, child.size, child.hash || child.multihash), done) | ||
}, | ||
(parent, done) => { | ||
ipfs.dag.put(parent, { | ||
cid: new CID(parent.hash || parent.multihash) | ||
}, (error) => done(error, parent)) | ||
} | ||
], callback) | ||
} | ||
|
||
module.exports = function mfsMkdir (ipfs) { | ||
return promisify((path, options, callback) => { | ||
withMfsRoot(ipfs, (error, root) => { | ||
if (error) { | ||
return callback(error) | ||
} | ||
|
||
if (typeof options === 'function') { | ||
callback = options | ||
options = {} | ||
} | ||
|
||
options = Object.assign({}, defaultOptions, options) | ||
|
||
if (!path) { | ||
return callback(new Error('no path given to Mkdir')) | ||
} | ||
|
||
const pathSegments = validatePath(path) | ||
.split(FILE_SEPARATOR) | ||
.filter(Boolean) | ||
|
||
if (pathSegments.length === 0) { | ||
return callback(options.parents ? null : new Error(`cannot create directory '${FILE_SEPARATOR}': Already exists`)) | ||
} | ||
|
||
const trail = [] | ||
|
||
waterfall([ | ||
(cb) => ipfs.dag.get(root, cb), | ||
(result, cb) => { | ||
const rootNode = result.value | ||
|
||
trail.push({ | ||
name: FILE_SEPARATOR, | ||
node: rootNode, | ||
parent: null | ||
}) | ||
|
||
reduce(pathSegments.map((pathSegment, index) => ({pathSegment, index})), { | ||
name: FILE_SEPARATOR, | ||
node: rootNode, | ||
parent: null | ||
}, (parent, {pathSegment, index}, done) => { | ||
const lastPathSegment = index === pathSegments.length - 1 | ||
const existingLink = parent.node.links.find(link => link.name === pathSegment) | ||
|
||
log(`Looking for ${pathSegment} in ${parent.name}`) | ||
|
||
if (!existingLink) { | ||
if (!lastPathSegment && !options.parents) { | ||
return done(new Error(`Cannot create ${path} - intermediate directory '${pathSegment}' did not exist: Try again with the --parents flag`)) | ||
} | ||
|
||
log(`Adding empty directory '${pathSegment}' to parent ${parent.name}`) | ||
|
||
return waterfall([ | ||
(next) => DAGNode.create(new UnixFS('directory').marshal(), [], next), | ||
(emptyDirectory, next) => { | ||
addLink(ipfs, parent.node, emptyDirectory, pathSegment, (error, updatedParent) => { | ||
parent.node = updatedParent | ||
|
||
next(error, { | ||
name: pathSegment, | ||
node: emptyDirectory, | ||
parent: parent | ||
}) | ||
}) | ||
} | ||
], done) | ||
} | ||
|
||
if (lastPathSegment && existingLink && !options.parents) { | ||
return done(new Error(`Cannot create directory '${path}': Already exists`)) | ||
} | ||
|
||
let hash = existingLink.hash || existingLink.multihash | ||
|
||
if (Buffer.isBuffer(hash)) { | ||
hash = bs58.encode(hash) | ||
} | ||
|
||
// child existed, fetch it | ||
ipfs.dag.get(hash, (error, result) => { | ||
const child = { | ||
name: pathSegment, | ||
node: result && result.value, | ||
parent: parent | ||
} | ||
|
||
trail.push(child) | ||
|
||
done(error, child) | ||
}) | ||
}, cb) | ||
}, | ||
(result, cb) => { | ||
// replace all links and store in the repo | ||
reduce(pathSegments, result, (child, pathSegment, next) => { | ||
addLink(ipfs, child.parent.node, child.node, child.name, (error, updatedParent) => { | ||
child.parent.node = updatedParent | ||
|
||
next(error, child.parent) | ||
}) | ||
}, cb) | ||
}, | ||
(result, cb) => { | ||
// update new MFS root CID | ||
updateMfsRoot(ipfs, result.node.multihash, cb) | ||
} | ||
], callback) | ||
}) | ||
}) | ||
} |
Oops, something went wrong.