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

feat: support mtime-nsecs in mfs cli #2958

Merged
merged 1 commit into from
Mar 28, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
30 changes: 7 additions & 23 deletions packages/ipfs/src/cli/commands/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ const getFolderSize = promisify(require('get-folder-size'))
const byteman = require('byteman')
const mh = require('multihashes')
const multibase = require('multibase')
const { createProgressBar } = require('../utils')
const {
createProgressBar,
coerceMtime,
coerceMtimeNsecs
} = require('../utils')
const { cidToString } = require('../../utils/cid')
const globSource = require('ipfs-utils/src/files/glob-source')

Expand Down Expand Up @@ -142,32 +146,12 @@ module.exports = {
},
mtime: {
type: 'number',
coerce: (value) => {
value = parseInt(value)

if (isNaN(value)) {
throw new Error('mtime must be a number')
}

return value
},
coerce: coerceMtime,
describe: 'Modification time in seconds before or since the Unix Epoch to apply to created UnixFS entries'
},
'mtime-nsecs': {
type: 'number',
coerce: (value) => {
value = parseInt(value)

if (isNaN(value)) {
throw new Error('mtime-nsecs must be a number')
}

if (value < 0 || value > 999999999) {
throw new Error('mtime-nsecs must be in the range [0,999999999]')
}

return value
},
coerce: coerceMtimeNsecs,
describe: 'Modification time fraction in nanoseconds'
}
},
Expand Down
20 changes: 14 additions & 6 deletions packages/ipfs/src/cli/commands/files/mkdir.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
const {
asBoolean,
asOctal,
asDateFromSeconds
asMtimeFromSeconds,
coerceMtime,
coerceMtimeNsecs
} = require('../../utils')

module.exports = {
Expand Down Expand Up @@ -49,9 +51,14 @@ module.exports = {
describe: 'Mode to apply to the new directory'
},
mtime: {
type: 'date',
coerce: asDateFromSeconds,
describe: 'Mtime to apply to the new directory in seconds'
type: 'number',
coerce: coerceMtime,
describe: 'Modification time in seconds before or since the Unix Epoch to apply to created UnixFS entries'
},
'mtime-nsecs': {
type: 'number',
coerce: coerceMtimeNsecs,
describe: 'Modification time fraction in nanoseconds'
}
},

Expand All @@ -65,7 +72,8 @@ module.exports = {
flush,
shardSplitThreshold,
mode,
mtime
mtime,
mtimeNsecs
} = argv

return ipfs.files.mkdir(path, {
Expand All @@ -75,7 +83,7 @@ module.exports = {
flush,
shardSplitThreshold,
mode,
mtime
mtime: asMtimeFromSeconds(mtime, mtimeNsecs)
})
}
}
21 changes: 14 additions & 7 deletions packages/ipfs/src/cli/commands/files/touch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

const {
asBoolean,
asDateFromSeconds
asMtimeFromSeconds,
coerceMtime,
coerceMtimeNsecs
} = require('../../utils')

module.exports = {
Expand All @@ -12,11 +14,15 @@ module.exports = {

builder: {
mtime: {
type: 'number',
alias: 'm',
type: 'date',
coerce: asDateFromSeconds,
default: Date.now(),
describe: 'Time to use as the new modification time'
coerce: coerceMtime,
describe: 'Modification time in seconds before or since the Unix Epoch to apply to created UnixFS entries'
},
'mtime-nsecs': {
type: 'number',
coerce: coerceMtimeNsecs,
describe: 'Modification time fraction in nanoseconds'
},
flush: {
alias: 'f',
Expand Down Expand Up @@ -52,11 +58,12 @@ module.exports = {
cidVersion,
hashAlg,
shardSplitThreshold,
mtime
mtime,
mtimeNsecs
} = argv

return ipfs.files.touch(path, {
mtime,
mtime: asMtimeFromSeconds(mtime, mtimeNsecs),
flush,
cidVersion,
hashAlg,
Expand Down
21 changes: 14 additions & 7 deletions packages/ipfs/src/cli/commands/files/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
const {
asBoolean,
asOctal,
asDateFromSeconds
asMtimeFromSeconds,
coerceMtime,
coerceMtimeNsecs
} = require('../../utils')

module.exports = {
Expand Down Expand Up @@ -89,10 +91,14 @@ module.exports = {
describe: 'The mode to use'
},
mtime: {
alias: 'm',
type: 'date',
coerce: asDateFromSeconds,
describe: 'Time to use as the new modification time'
type: 'number',
coerce: coerceMtime,
describe: 'Modification time in seconds before or since the Unix Epoch to apply to created UnixFS entries'
},
'mtime-nsecs': {
type: 'number',
coerce: coerceMtimeNsecs,
describe: 'Modification time fraction in nanoseconds'
}
},

Expand All @@ -114,7 +120,8 @@ module.exports = {
flush,
shardSplitThreshold,
mode,
mtime
mtime,
mtimeNsecs
} = argv

await ipfs.files.write(path, getStdin(), {
Expand All @@ -132,7 +139,7 @@ module.exports = {
flush,
shardSplitThreshold,
mode,
mtime
mtime: asMtimeFromSeconds(mtime, mtimeNsecs)
})
}
}
44 changes: 41 additions & 3 deletions packages/ipfs/src/cli/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,44 @@ const asOctal = (value) => {
return parseInt(value, 8)
}

const asDateFromSeconds = (value) => {
return new Date(parseInt(value, 10) * 1000)
const asMtimeFromSeconds = (secs, nsecs) => {
if (secs === null || secs === undefined) {
return undefined
}

const output = {
secs
}

if (nsecs !== null && nsecs !== undefined) {
output.nsecs = nsecs
}

return output
}

const coerceMtime = (value) => {
value = parseInt(value)

if (isNaN(value)) {
throw new Error('mtime must be a number')
}

return value
}

const coerceMtimeNsecs = (value) => {
value = parseInt(value)

if (isNaN(value)) {
throw new Error('mtime-nsecs must be a number')
}

if (value < 0 || value > 999999999) {
throw new Error('mtime-nsecs must be in the range [0,999999999]')
}

return value
}

module.exports = {
Expand All @@ -148,5 +184,7 @@ module.exports = {
ipfsPathHelp,
asBoolean,
asOctal,
asDateFromSeconds
asMtimeFromSeconds,
coerceMtime,
coerceMtimeNsecs
}
19 changes: 18 additions & 1 deletion packages/ipfs/test/cli/files/mkdir.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,24 @@ describe('mkdir', () => {
expect(ipfs.files.mkdir.getCall(0).args).to.deep.equal([
path,
defaultOptions({
mtime: new Date(5000)
mtime: {
secs: 5
}
})
])
})

it('should make a directory a different mtime and mtime nsecs', async () => {
await cli(`files mkdir --mtime 5 --mtime-nsecs 10 ${path}`, { ipfs })

expect(ipfs.files.mkdir.callCount).to.equal(1)
expect(ipfs.files.mkdir.getCall(0).args).to.deep.equal([
path,
defaultOptions({
mtime: {
secs: 5,
nsecs: 10
}
})
])
})
Expand Down
31 changes: 24 additions & 7 deletions packages/ipfs/test/cli/files/touch.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ describe('touch', () => {
}

const path = '/foo'
const mtime = new Date(100000)
const mtime = {
secs: 1000
}
let ipfs

beforeEach(() => {
Expand All @@ -40,7 +42,7 @@ describe('touch', () => {
})

it('should update the mtime for a file', async () => {
await cli(`files touch -m ${mtime.getTime() / 1000} ${path}`, { ipfs })
await cli(`files touch -m ${mtime.secs} ${path}`, { ipfs })

expect(ipfs.files.touch.callCount).to.equal(1)
expect(ipfs.files.touch.getCall(0).args).to.deep.equal([
Expand All @@ -52,7 +54,7 @@ describe('touch', () => {
})

it('should update the mtime without flushing', async () => {
await cli(`files touch -m ${mtime.getTime() / 1000} --flush false ${path}`, { ipfs })
await cli(`files touch -m ${mtime.secs} --flush false ${path}`, { ipfs })

expect(ipfs.files.touch.callCount).to.equal(1)
expect(ipfs.files.touch.getCall(0).args).to.deep.equal([
Expand All @@ -65,7 +67,7 @@ describe('touch', () => {
})

it('should update the mtime without flushing (short option)', async () => {
await cli(`files touch -m ${mtime.getTime() / 1000} -f false ${path}`, { ipfs })
await cli(`files touch -m ${mtime.secs} -f false ${path}`, { ipfs })

expect(ipfs.files.touch.callCount).to.equal(1)
expect(ipfs.files.touch.getCall(0).args).to.deep.equal([
Expand All @@ -78,7 +80,7 @@ describe('touch', () => {
})

it('should update the mtime with a different hash algorithm', async () => {
await cli(`files touch -m ${mtime.getTime() / 1000} --hash-alg sha3-256 ${path}`, { ipfs })
await cli(`files touch -m ${mtime.secs} --hash-alg sha3-256 ${path}`, { ipfs })

expect(ipfs.files.touch.callCount).to.equal(1)
expect(ipfs.files.touch.getCall(0).args).to.deep.equal([
Expand All @@ -91,7 +93,7 @@ describe('touch', () => {
})

it('should update the mtime with a different hash algorithm (short option)', async () => {
await cli(`files touch -m ${mtime.getTime() / 1000} -h sha3-256 ${path}`, { ipfs })
await cli(`files touch -m ${mtime.secs} -h sha3-256 ${path}`, { ipfs })

expect(ipfs.files.touch.callCount).to.equal(1)
expect(ipfs.files.touch.getCall(0).args).to.deep.equal([
Expand All @@ -104,7 +106,7 @@ describe('touch', () => {
})

it('should update the mtime with a shard split threshold', async () => {
await cli(`files touch -m ${mtime.getTime() / 1000} --shard-split-threshold 10 ${path}`, { ipfs })
await cli(`files touch -m ${mtime.secs} --shard-split-threshold 10 ${path}`, { ipfs })

expect(ipfs.files.touch.callCount).to.equal(1)
expect(ipfs.files.touch.getCall(0).args).to.deep.equal([
Expand All @@ -115,4 +117,19 @@ describe('touch', () => {
})
])
})

it('should update the mtime and nsecs', async () => {
await cli(`files touch -m 5 --mtime-nsecs 10 ${path}`, { ipfs })

expect(ipfs.files.touch.callCount).to.equal(1)
expect(ipfs.files.touch.getCall(0).args).to.deep.equal([
path,
defaultOptions({
mtime: {
secs: 5,
nsecs: 10
}
})
])
})
})
22 changes: 21 additions & 1 deletion packages/ipfs/test/cli/files/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,27 @@ describe('write', () => {
path,
stdin,
defaultOptions({
mtime: new Date(11000)
mtime: {
secs: 11
}
})
])
})

it('should write to a file with a specified mtime and mtime nsecs', async () => {
const path = '/foo'

await cli(`files write --mtime 11 --mtime-nsecs 10 ${path}`, { ipfs, getStdin })

expect(ipfs.files.write.callCount).to.equal(1)
expect(ipfs.files.write.getCall(0).args).to.deep.equal([
path,
stdin,
defaultOptions({
mtime: {
secs: 11,
nsecs: 10
}
})
])
})
Expand Down