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

Add metadata support #63

Merged
merged 16 commits into from
Jan 9, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,34 @@
"homepage": "https://github.com/ipfs/js-ipfs-mfs#readme",
"devDependencies": {
"aegir": "^20.0.0",
"async-iterator-all": "^1.0.0",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"delay": "^4.3.0",
"detect-node": "^2.0.4",
"detect-webworker": "^1.0.0",
"dirty-chai": "^2.0.1",
"ipfs-block-service": "~0.16.0",
"ipfs-repo": "~0.27.0",
"ipfs-repo": "^0.29.1",
"ipld": "~0.25.0",
"memdown": "^4.0.0",
"it-all": "^1.0.1",
"memdown": "^5.1.0",
"temp-write": "^4.0.0"
},
"dependencies": {
"@hapi/boom": "^7.4.2",
"@hapi/joi": "^15.1.0",
"async-iterator-last": "^1.0.0",
"cids": "~0.7.1",
"debug": "^4.1.0",
"err-code": "^2.0.0",
"hamt-sharding": "~0.0.2",
"interface-datastore": "~0.7.0",
"interface-datastore": "^0.8.0",
"ipfs-multipart": "~0.2.0",
"ipfs-unixfs": "~0.1.16",
"ipfs-unixfs-exporter": "~0.38.0",
"ipfs-unixfs-importer": "~0.40.0",
"ipfs-unixfs": "^0.2.0",
"ipfs-unixfs-exporter": "~0.39.0",
"ipfs-unixfs-importer": "^0.42.0",
"ipfs-utils": "ipfs/js-ipfs-utils#support-unixfs-metadata",
"ipld-dag-pb": "~0.18.0",
"it-last": "^1.0.1",
"joi-browser": "^13.4.0",
"mortice": "^2.0.0",
"multicodec": "~0.5.3",
Expand Down
63 changes: 63 additions & 0 deletions src/cli/chmod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use strict'

const {
asBoolean,
asOctal
} = require('./utils')

module.exports = {
command: 'chmod [mode] [path]',

describe: 'Change file modes',

builder: {
path: {
type: 'string',
describe: 'The MFS path to change the mode of'
},
mode: {
type: 'int',
coerce: asOctal,
describe: 'The mode to use'
},
recursive: {
type: 'boolean',
default: false,
coerce: asBoolean,
describe: 'Whether to change modes recursively'
},
flush: {
alias: 'f',
type: 'boolean',
default: true,
coerce: asBoolean,
describe: 'Flush the changes to disk immediately'
},
'shard-split-threshold': {
type: 'number',
default: 1000,
describe: 'If a directory has more links than this, it will be transformed into a hamt-sharded-directory'
}
},

handler (argv) {
const {
path,
mode,
getIpfs,
recursive,
flush,
shardSplitThreshold
} = argv

argv.resolve((async () => {
const ipfs = await getIpfs()

return ipfs.files.chmod(path, mode, {
recursive,
flush,
shardSplitThreshold
})
})())
}
}
2 changes: 1 addition & 1 deletion src/cli/cp.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = {
describe: 'Create any non-existent intermediate directories'
},
format: {
alias: 'h',
alias: 'f',
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically a breaking change...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure - h was taken by another command so the short version of this option never worked before. More of a fix?

type: 'string',
default: 'dag-pb',
describe: 'If intermediate directories are created, use this format to create them (experimental)'
Expand Down
8 changes: 5 additions & 3 deletions src/cli/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const {
const {
FILE_SEPARATOR
} = require('../core/utils/constants')
const formatMode = require('ipfs-utils/src/files/format-mode')
const formatMtime = require('ipfs-utils/src/files/format-mtime')

module.exports = {
command: 'ls [path]',
Expand Down Expand Up @@ -64,8 +66,8 @@ module.exports = {
}

if (long) {
files.forEach(link => {
print(`${link.name}\t${link.hash}\t${link.size}`)
files.forEach(file => {
print(`${formatMode(file.mode, file.type === 1)}\t${formatMtime(file.mtime)}\t${file.name}\t${file.hash}\t${file.size}`)
})
} else {
files.forEach(link => print(link.name))
Expand All @@ -85,7 +87,7 @@ module.exports = {
}),
through(file => {
if (long) {
print(`${file.name}\t${file.hash}\t${file.size}`)
print(`${formatMode(file.mode, file.type === 1)}\t${formatMtime(file.mtime)}\t${file.name}\t${file.hash}\t${file.size}`)
} else {
print(file.name)
}
Expand Down
25 changes: 22 additions & 3 deletions src/cli/mkdir.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict'

const {
asBoolean
asBoolean,
asOctal
} = require('./utils')

module.exports = {
Expand Down Expand Up @@ -38,6 +39,20 @@ module.exports = {
type: 'number',
default: 1000,
describe: 'If a directory has more links than this, it will be transformed into a hamt-sharded-directory'
},
mode: {
alias: 'm',
type: 'number',
default: true,
coerce: asOctal,
describe: 'Mode to apply to the new directory'
},
mtime: {
alias: 'm',
type: 'number',
default: true,
coerce: asOctal,
describe: 'Mtime to apply to the new directory'
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed, peeps probably want to use strings.

}
},

Expand All @@ -49,7 +64,9 @@ module.exports = {
cidVersion,
hashAlg,
flush,
shardSplitThreshold
shardSplitThreshold,
mode,
mtime
} = argv

argv.resolve((async () => {
Expand All @@ -60,7 +77,9 @@ module.exports = {
cidVersion,
hashAlg,
flush,
shardSplitThreshold
shardSplitThreshold,
mode,
mtime
})
})())
}
Expand Down
8 changes: 6 additions & 2 deletions src/cli/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ module.exports = {
Size: <size>
CumulativeSize: <cumulsize>
ChildBlocks: <childs>
Type: <type>`,
describe: 'Print statistics in given format. Allowed tokens: <hash> <size> <cumulsize> <type> <childs>. Conflicts with other format options.'
Type: <type>
Mode: <mode>
Mtime: <mtime>`,
describe: 'Print statistics in given format. Allowed tokens: <hash> <size> <cumulsize> <type> <childs> <mode> <mtime>. Conflicts with other format options.'
},
hash: {
alias: 'h',
Expand Down Expand Up @@ -79,6 +81,8 @@ Type: <type>`,
.replace('<cumulsize>', stats.cumulativeSize)
.replace('<childs>', stats.blocks)
.replace('<type>', stats.type)
.replace('<mode>', stats.mode)
.replace('<mtime>', stats.mtime)
)
})
})())
Expand Down
59 changes: 59 additions & 0 deletions src/cli/touch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict'

const {
asBoolean
} = require('./utils')

module.exports = {
command: 'touch [path]',

describe: 'change file modification times',

builder: {
flush: {
alias: 'f',
type: 'boolean',
default: true,
coerce: asBoolean,
describe: 'Flush the changes to disk immediately'
},
'shard-split-threshold': {
type: 'number',
default: 1000,
describe: 'If a directory has more links than this, it will be transformed into a hamt-sharded-directory'
},
'cid-version': {
alias: ['cid-ver'],
type: 'number',
default: 0,
describe: 'Cid version to use'
},
mtime: {
alias: 'm',
type: 'number',
default: parseInt(Date.now() / 1000),
describe: 'Time to use as the new modification time'
Copy link

@alanshaw alanshaw Dec 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
describe: 'Time to use as the new modification time'
describe: 'Time in seconds since the Unix epoch to use as the new modification time'

...or is it seconds?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's seconds in the spec.

}
},

handler (argv) {
const {
path,
getIpfs,
flush,
shardSplitThreshold,
cidVersion,
mtime
} = argv

argv.resolve((async () => {
const ipfs = await getIpfs()

return ipfs.files.touch(path, mtime, {
flush,
shardSplitThreshold,
cidVersion
})
})())
}
}
7 changes: 6 additions & 1 deletion src/cli/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@ const asBoolean = (value) => {
return false
}

const asOctal = (value) => {
return parseInt(value, 8)
}

module.exports = {
disablePrinting,
print,
asBoolean
asBoolean,
asOctal
}
74 changes: 74 additions & 0 deletions src/core/chmod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
'use strict'

const applyDefaultOptions = require('./utils/apply-default-options')
const toMfsPath = require('./utils/to-mfs-path')
const log = require('debug')('ipfs:mfs:touch')
const errCode = require('err-code')
const UnixFS = require('ipfs-unixfs')
const toTrail = require('./utils/to-trail')
const addLink = require('./utils/add-link')
const updateTree = require('./utils/update-tree')
const updateMfsRoot = require('./utils/update-mfs-root')
const { DAGNode } = require('ipld-dag-pb')
const mc = require('multicodec')
const mh = require('multihashes')

const defaultOptions = {
flush: true,
shardSplitThreshold: 1000,
cidVersion: 1,
format: 'dag-pb',
hashAlg: 'sha2-256'
}

module.exports = (context) => {
return async function mfsChmod (path, mode, options) {
options = applyDefaultOptions(options, defaultOptions)

log(`Fetching stats for ${path}`)

const {
cid,
mfsDirectory,
name
} = await toMfsPath(context, path)

if (cid.codec !== 'dag-pb') {
throw errCode(new Error(`${path} was not a UnixFS node`), 'ERR_NOT_UNIXFS')
}

let node = await context.ipld.get(cid)
const metadata = UnixFS.unmarshal(node.Data)
metadata.mode = mode
node = new DAGNode(metadata.marshal(), node.Links)

const updatedCid = await context.ipld.put(node, mc.DAG_PB, {
cidVersion: cid.version,
hashAlg: mh.names['sha2-256'],
hashOnly: !options.flush
achingbrain marked this conversation as resolved.
Show resolved Hide resolved
})

const trail = await toTrail(context, mfsDirectory, options)
const parent = trail[trail.length - 1]
const parentNode = await context.ipld.get(parent.cid)

const result = await addLink(context, {
parent: parentNode,
name: name,
cid: updatedCid,
size: node.serialize().length,
flush: options.flush,
format: 'dag-pb',
hashAlg: 'sha2-256',
cidVersion: cid.version
})

parent.cid = result.cid

// update the tree with the new child
const newRootCid = await updateTree(context, trail, options)

// Update the MFS record with the new CID for the root of the tree
await updateMfsRoot(context, newRootCid)
}
}
4 changes: 3 additions & 1 deletion src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ const readOperations = {

// These operations are locked at the function level and will execute in series
const writeOperations = {
chmod: require('./chmod'),
cp: require('./cp'),
flush: require('./flush'),
mkdir: require('./mkdir'),
mv: require('./mv'),
rm: require('./rm')
rm: require('./rm'),
touch: require('./touch')
}

// These operations are asynchronous and manage their own locking
Expand Down
Loading