-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support unixfs metadata and formatting it (#14)
Adds mode and mtime properties to normalised .add inputs, also adds functions for turning metadata into strings for CLI use. BREAKING CHANGE: In order to support metadata on intermediate directories, globSource in this module will now emit directories and files where previously it only emitted files.
- Loading branch information
1 parent
251eff0
commit 173e4bf
Showing
11 changed files
with
368 additions
and
52 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
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,66 @@ | ||
'use strict' | ||
|
||
const S_ISUID = parseInt('4000', 8) // set UID bit | ||
const S_ISGID = parseInt('2000', 8) // set-group-ID bit (see below) | ||
const S_ISVTX = parseInt('1000', 8) // sticky bit (see below) | ||
// const S_IRWXU = parseInt('700', 8) // mask for file owner permissions | ||
const S_IRUSR = parseInt('400', 8) // owner has read permission | ||
const S_IWUSR = parseInt('200', 8) // owner has write permission | ||
const S_IXUSR = parseInt('100', 8) // owner has execute permission | ||
// const S_IRWXG = parseInt('70', 8) // mask for group permissions | ||
const S_IRGRP = parseInt('40', 8) // group has read permission | ||
const S_IWGRP = parseInt('20', 8) // group has write permission | ||
const S_IXGRP = parseInt('10', 8) // group has execute permission | ||
// const S_IRWXO = parseInt('7', 8) // mask for permissions for others (not in group) | ||
const S_IROTH = parseInt('4', 8) // others have read permission | ||
const S_IWOTH = parseInt('2', 8) // others have write permission | ||
const S_IXOTH = parseInt('1', 8) // others have execute permission | ||
|
||
function checkPermission (mode, perm, type, output) { | ||
if ((mode & perm) === perm) { | ||
output.push(type) | ||
} else { | ||
output.push('-') | ||
} | ||
} | ||
|
||
function formatMode (mode, isDirectory) { | ||
const output = [] | ||
|
||
if (isDirectory) { | ||
output.push('d') | ||
} else { | ||
output.push('-') | ||
} | ||
|
||
checkPermission(mode, S_IRUSR, 'r', output) | ||
checkPermission(mode, S_IWUSR, 'w', output) | ||
|
||
if ((mode & S_ISUID) === S_ISUID) { | ||
output.push('s') | ||
} else { | ||
checkPermission(mode, S_IXUSR, 'x', output) | ||
} | ||
|
||
checkPermission(mode, S_IRGRP, 'r', output) | ||
checkPermission(mode, S_IWGRP, 'w', output) | ||
|
||
if ((mode & S_ISGID) === S_ISGID) { | ||
output.push('s') | ||
} else { | ||
checkPermission(mode, S_IXGRP, 'x', output) | ||
} | ||
|
||
checkPermission(mode, S_IROTH, 'r', output) | ||
checkPermission(mode, S_IWOTH, 'w', output) | ||
|
||
if ((mode & S_ISVTX) === S_ISVTX) { | ||
output.push('t') | ||
} else { | ||
checkPermission(mode, S_IXOTH, 'x', output) | ||
} | ||
|
||
return output.join('') | ||
} | ||
|
||
module.exports = formatMode |
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,19 @@ | ||
'use strict' | ||
|
||
function formatMtime (mtime) { | ||
if (mtime === undefined) { | ||
return '-' | ||
} | ||
|
||
return new Date(mtime * 1000).toLocaleDateString(Intl.DateTimeFormat().resolvedOptions().locale, { | ||
year: 'numeric', | ||
month: 'short', | ||
day: 'numeric', | ||
hour: '2-digit', | ||
minute: '2-digit', | ||
second: '2-digit', | ||
timeZoneName: 'short' | ||
}) | ||
} | ||
|
||
module.exports = formatMtime |
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
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
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' | ||
|
||
/* eslint-env mocha */ | ||
const chai = require('chai') | ||
const dirtyChai = require('dirty-chai') | ||
const formatMode = require('../../src/files/format-mode') | ||
|
||
chai.use(dirtyChai) | ||
const expect = chai.expect | ||
|
||
describe('format-mode', function () { | ||
it('formats mode for directories', function () { | ||
expect(formatMode(parseInt('0777', 8), true)).to.equal('drwxrwxrwx') | ||
}) | ||
|
||
it('formats mode for files', function () { | ||
expect(formatMode(parseInt('0777', 8), false)).to.equal('-rwxrwxrwx') | ||
}) | ||
|
||
it('setgid, setuid and stick bit', function () { | ||
expect(formatMode(parseInt('1777', 8), false)).to.equal('-rwxrwxrwt') | ||
expect(formatMode(parseInt('2777', 8), false)).to.equal('-rwxrwsrwx') | ||
expect(formatMode(parseInt('4777', 8), false)).to.equal('-rwsrwxrwx') | ||
expect(formatMode(parseInt('5777', 8), false)).to.equal('-rwsrwxrwt') | ||
expect(formatMode(parseInt('6777', 8), false)).to.equal('-rwsrwsrwx') | ||
expect(formatMode(parseInt('7777', 8), false)).to.equal('-rwsrwsrwt') | ||
}) | ||
|
||
it('formats user', function () { | ||
expect(formatMode(parseInt('0100', 8), false)).to.equal('---x------') | ||
expect(formatMode(parseInt('0200', 8), false)).to.equal('--w-------') | ||
expect(formatMode(parseInt('0300', 8), false)).to.equal('--wx------') | ||
expect(formatMode(parseInt('0400', 8), false)).to.equal('-r--------') | ||
expect(formatMode(parseInt('0500', 8), false)).to.equal('-r-x------') | ||
expect(formatMode(parseInt('0600', 8), false)).to.equal('-rw-------') | ||
expect(formatMode(parseInt('0700', 8), false)).to.equal('-rwx------') | ||
}) | ||
|
||
it('formats group', function () { | ||
expect(formatMode(parseInt('0010', 8), false)).to.equal('------x---') | ||
expect(formatMode(parseInt('0020', 8), false)).to.equal('-----w----') | ||
expect(formatMode(parseInt('0030', 8), false)).to.equal('-----wx---') | ||
expect(formatMode(parseInt('0040', 8), false)).to.equal('----r-----') | ||
expect(formatMode(parseInt('0050', 8), false)).to.equal('----r-x---') | ||
expect(formatMode(parseInt('0060', 8), false)).to.equal('----rw----') | ||
expect(formatMode(parseInt('0070', 8), false)).to.equal('----rwx---') | ||
}) | ||
|
||
it('formats other', function () { | ||
expect(formatMode(parseInt('0001', 8), false)).to.equal('---------x') | ||
expect(formatMode(parseInt('0002', 8), false)).to.equal('--------w-') | ||
expect(formatMode(parseInt('0003', 8), false)).to.equal('--------wx') | ||
expect(formatMode(parseInt('0004', 8), false)).to.equal('-------r--') | ||
expect(formatMode(parseInt('0005', 8), false)).to.equal('-------r-x') | ||
expect(formatMode(parseInt('0006', 8), false)).to.equal('-------rw-') | ||
expect(formatMode(parseInt('0007', 8), false)).to.equal('-------rwx') | ||
}) | ||
}) |
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,15 @@ | ||
'use strict' | ||
|
||
/* eslint-env mocha */ | ||
const chai = require('chai') | ||
const dirtyChai = require('dirty-chai') | ||
const formatMtime = require('../../src/files/format-mtime') | ||
|
||
chai.use(dirtyChai) | ||
const expect = chai.expect | ||
|
||
describe('format-mtime', function () { | ||
it('formats mtime', function () { | ||
expect((new Date(formatMtime(0))).getTime()).to.equal(0) | ||
}) | ||
}) |
Oops, something went wrong.