Skip to content
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
64 changes: 32 additions & 32 deletions lib/create.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@

var fs = require('fs')
var path = require('path')
var assert = require('assert')
var encode = require('./encode')
const fs = require('fs')
const path = require('path')
const assert = require('assert')
const encode = require('./encode')

var addon = require('../build/Release/volume.node')
const addon = require('../build/Release/volume.node')

var findVolume = function (startPath, startStat) {
var lastDev = startStat.dev
var lastIno = startStat.ino
var lastPath = startPath
const findVolume = function (startPath, startStat) {
let lastDev = startStat.dev
let lastIno = startStat.ino
let lastPath = startPath

while (1) {
var parentPath = path.resolve(lastPath, '..')
var parentStat = fs.statSync(parentPath)
const parentPath = path.resolve(lastPath, '..')
const parentStat = fs.statSync(parentPath)

if (parentStat.dev !== lastDev) {
return lastPath
Expand All @@ -29,10 +29,10 @@ var findVolume = function (startPath, startStat) {
}
}

var utf16be = function (str) {
var b = Buffer.from(str, 'ucs2')
for (var i = 0; i < b.length; i += 2) {
var a = b[i]
const utf16be = function (str) {
const b = Buffer.from(str, 'ucs2')
for (let i = 0; i < b.length; i += 2) {
const a = b[i]
b[i] = b[i + 1]
b[i + 1] = a
}
Expand All @@ -53,17 +53,17 @@ var utf16be = function (str) {
* @returns {Buffer} Alias file in binary format.
*/
module.exports = exports = function (targetPath, options) {
var info = { version: 2, extra: [] }
const info = { version: 2, extra: [] }

var parentPath = path.resolve(targetPath, '..')
var targetStat = fs.statSync(targetPath)
var parentStat = fs.statSync(parentPath)
var volumePath = findVolume(targetPath, targetStat)
var volumeStat = fs.statSync(volumePath)
const parentPath = path.resolve(targetPath, '..')
const targetStat = fs.statSync(targetPath)
const parentStat = fs.statSync(parentPath)
const volumePath = findVolume(targetPath, targetStat)
const volumeStat = fs.statSync(volumePath)

assert(targetStat.isFile() || targetStat.isDirectory(), 'Target is a file or directory')

var volumneName = addon.getVolumeName(volumePath) || options.volumeName
const volumneName = addon.getVolumeName(volumePath) || options.volumeName

info.target = {
id: targetStat.ino,
Expand All @@ -85,7 +85,7 @@ module.exports = exports = function (targetPath, options) {
};

(function addType0 () {
var b = Buffer.from(info.parent.name, 'utf8')
const b = Buffer.from(info.parent.name, 'utf8')

info.extra.push({
type: 0,
Expand All @@ -95,7 +95,7 @@ module.exports = exports = function (targetPath, options) {
}());

(function addType1 () {
var b = Buffer.alloc(4)
const b = Buffer.alloc(4)

b.writeUInt32BE(info.parent.id, 0)

Expand All @@ -107,8 +107,8 @@ module.exports = exports = function (targetPath, options) {
}());

(function addType14 () {
var l = info.target.filename.length
var b = Buffer.alloc(2 + (l * 2))
const l = info.target.filename.length
const b = Buffer.alloc(2 + (l * 2))

b.writeUInt16BE(l, 0)
utf16be(info.target.filename).copy(b, 2)
Expand All @@ -121,8 +121,8 @@ module.exports = exports = function (targetPath, options) {
}());

(function addType15 () {
var l = info.volume.name.length
var b = Buffer.alloc(2 + (l * 2))
const l = info.volume.name.length
const b = Buffer.alloc(2 + (l * 2))

b.writeUInt16BE(l, 0)
utf16be(info.volume.name).copy(b, 2)
Expand All @@ -135,10 +135,10 @@ module.exports = exports = function (targetPath, options) {
}());

(function addType18 () {
var vl = volumePath.length
const vl = volumePath.length
assert.equal(targetPath.slice(0, vl), volumePath)
var lp = targetPath.slice(vl)
var b = Buffer.from(lp, 'utf8')
const lp = targetPath.slice(vl)
const b = Buffer.from(lp, 'utf8')

info.extra.push({
type: 18,
Expand All @@ -148,7 +148,7 @@ module.exports = exports = function (targetPath, options) {
}());

(function addType19 () {
var b = Buffer.from(volumePath, 'utf8')
const b = Buffer.from(volumePath, 'utf8')

info.extra.push({
type: 19,
Expand Down
45 changes: 23 additions & 22 deletions lib/decode.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

var assert = require('assert')
var values = require('./values')
const assert = require('assert')
const values = require('./values')

var appleEpoch = Date.UTC(1904, 0, 1)
var appleDate = function (value) {
const appleEpoch = Date.UTC(1904, 0, 1)
const appleDate = function (value) {
return new Date(appleEpoch + (value * 1000))
}

Expand All @@ -15,43 +15,43 @@ var appleDate = function (value) {
* @returns {Object} Data structure representing the information stored in the alias file.
*/
module.exports = exports = function (buf) {
var info = { volume: {}, parent: {}, target: {}, extra: [] }
const info = { volume: {}, parent: {}, target: {}, extra: [] }

assert.equal(buf.readUInt16BE(4), buf.length)

info.version = buf.readUInt16BE(6)
assert.equal(info.version, 2)

var type = buf.readUInt16BE(8)
const type = buf.readUInt16BE(8)
assert(type === 0 || type === 1, 'Type is valid')
info.target.type = values.type[type]

var volNameLength = buf.readUInt8(10)
const volNameLength = buf.readUInt8(10)
assert(volNameLength <= 27, 'Volume name is not longer than 27 chars')
info.volume.name = buf.toString('utf8', 11, 11 + volNameLength)

var volCreateDate = buf.readUInt32BE(38)
const volCreateDate = buf.readUInt32BE(38)
info.volume.created = appleDate(volCreateDate)

var volSig = buf.toString('ascii', 42, 44)
const volSig = buf.toString('ascii', 42, 44)
assert(volSig === 'BD' || volSig === 'H+' || volSig === 'HX', 'Volume signature is valid')
info.volume.signature = volSig

var volType = buf.readUInt16BE(44)
const volType = buf.readUInt16BE(44)
assert(volType >= 0 && volType <= 5, 'Volume type is valid')
info.volume.type = values.volumeType[volType]

var dirId = buf.readUInt32BE(46)
const dirId = buf.readUInt32BE(46)
info.parent.id = dirId

var fileNameLength = buf.readUInt8(50)
const fileNameLength = buf.readUInt8(50)
assert(fileNameLength <= 63, 'File name is not longer than 63 chars')
info.target.filename = buf.toString('utf8', 51, 51 + fileNameLength)

var fileId = buf.readUInt32BE(114)
const fileId = buf.readUInt32BE(114)
info.target.id = fileId

var fileCreateDate = buf.readUInt32BE(118)
const fileCreateDate = buf.readUInt32BE(118)
info.target.created = appleDate(fileCreateDate)

// var fileTypeName = buf.toString('ascii', 122, 126)
Expand All @@ -68,19 +68,19 @@ module.exports = exports = function (buf) {
// var volFSId = buf.readInt16BE(138)
// I have only encountered 00 00

var reserved = buf.slice(140, 150)
const reserved = buf.slice(140, 150)
assert(reserved[0] === 0 && reserved[1] === 0, 'Reserved is zero-filled')
assert(reserved[2] === 0 && reserved[3] === 0, 'Reserved is zero-filled')
assert(reserved[4] === 0 && reserved[5] === 0, 'Reserved is zero-filled')
assert(reserved[6] === 0 && reserved[7] === 0, 'Reserved is zero-filled')
assert(reserved[8] === 0 && reserved[9] === 0, 'Reserved is zero-filled')

var pos = 150
let pos = 150

while (pos < buf.length) {
var partType = buf.readInt16BE(pos)
var length = buf.readUInt16BE(pos + 2)
var data = buf.slice(pos + 4, pos + 4 + length)
const partType = buf.readInt16BE(pos)
const length = buf.readUInt16BE(pos + 2)
const data = buf.slice(pos + 4, pos + 4 + length)
pos += 4 + length

if (partType === -1) {
Expand All @@ -89,7 +89,7 @@ module.exports = exports = function (buf) {
}

if (length % 2 === 1) {
var padding = buf.readUInt8(pos)
const padding = buf.readUInt8(pos)
assert.equal(padding, 0)
pos += 1
}
Expand All @@ -103,11 +103,12 @@ module.exports = exports = function (buf) {
case 1:
assert.equal(info.parent.id, data.readUInt32BE(0))
break
case 2:
var parts = data.toString('utf8').split('\0')
case 2: {
const parts = data.toString('utf8').split('\0')
info.target.path = parts[0]
assert.equal(info.target.filename, parts[1])
break
}
case 14:
// FIXME
// Target: name as (16-bit length), (length char utf16be)
Expand Down
50 changes: 25 additions & 25 deletions lib/encode.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

var assert = require('assert')
var values = require('./values')
const assert = require('assert')
const values = require('./values')

var appleEpoch = Date.UTC(1904, 0, 1)
var appleDate = function (value) {
const appleEpoch = Date.UTC(1904, 0, 1)
const appleDate = function (value) {
if (!(value instanceof Date)) {
// value = new Date(value);
throw new TypeError('Not a date: ' + value)
Expand All @@ -22,82 +22,82 @@ var appleDate = function (value) {
module.exports = exports = function (info) {
assert.equal(info.version, 2)

var baseLength = 150
var extraLength = (info.extra || []).reduce(function (p, c) {
const baseLength = 150
const extraLength = (info.extra || []).reduce(function (p, c) {
assert.equal(c.data.length, c.length)
var padding = (c.length % 2)
const padding = (c.length % 2)
return p + 4 + c.length + padding
}, 0)
var trailerLength = 4
const trailerLength = 4

var buf = Buffer.alloc(baseLength + extraLength + trailerLength)
const buf = Buffer.alloc(baseLength + extraLength + trailerLength)

buf.writeUInt32BE(0, 0)

buf.writeUInt16BE(buf.length, 4)
buf.writeUInt16BE(info.version, 6)

var type = values.type.indexOf(info.target.type)
const type = values.type.indexOf(info.target.type)
assert(type === 0 || type === 1, 'Type is valid')
buf.writeUInt16BE(type, 8)

var volNameLength = info.volume.name.length
const volNameLength = info.volume.name.length
assert(volNameLength <= 27, 'Volume name is not longer than 27 chars')
buf.writeUInt8(volNameLength, 10)
buf.fill(0, 11, 11 + 27)
buf.write(info.volume.name, 11, 'utf8')

var volCreateDate = appleDate(info.volume.created)
const volCreateDate = appleDate(info.volume.created)
buf.writeUInt32BE(volCreateDate, 38)

var volSig = info.volume.signature
const volSig = info.volume.signature
assert(volSig === 'BD' || volSig === 'H+' || volSig === 'HX', 'Volume signature is valid')
buf.write(volSig, 42, 'ascii')

var volType = values.volumeType.indexOf(info.volume.type)
const volType = values.volumeType.indexOf(info.volume.type)
assert(volType >= 0 && volType <= 5, 'Volume type is valid')
buf.writeUInt16BE(volType, 44)

buf.writeUInt32BE(info.parent.id, 46)

var fileNameLength = info.target.filename.length
const fileNameLength = info.target.filename.length
assert(fileNameLength <= 63, 'File name is not longer than 63 chars')
buf.writeUInt8(fileNameLength, 50)
buf.fill(0, 51, 51 + 63)
buf.write(info.target.filename, 51, 'utf8')

buf.writeUInt32BE(info.target.id, 114)

var fileCreateDate = appleDate(info.target.created)
const fileCreateDate = appleDate(info.target.created)
buf.writeUInt32BE(fileCreateDate, 118)

var fileTypeName = '\0\0\0\0'
var fileCreatorName = '\0\0\0\0'
const fileTypeName = '\0\0\0\0'
const fileCreatorName = '\0\0\0\0'
// I have only encountered 00 00 00 00
buf.write(fileTypeName, 122, 'binary')
buf.write(fileCreatorName, 126, 'binary')

var nlvlFrom = -1
var nlvlTo = -1
const nlvlFrom = -1
const nlvlTo = -1
// I have only encountered -1
buf.writeInt16BE(nlvlFrom, 130)
buf.writeInt16BE(nlvlTo, 132)

var volAttributes = 0x00000D02
const volAttributes = 0x00000D02
// I have only encountered 00 00 0D 02
buf.writeUInt32BE(volAttributes, 134)

var volFSId = 0x0000
const volFSId = 0x0000
// I have only encountered 00 00
buf.writeUInt16BE(volFSId, 138)

// Reserved space
buf.fill(0, 140, 150)

var pos = 150
let pos = 150

for (var i = 0; i < info.extra.length; i++) {
var e = info.extra[i]
for (let i = 0; i < info.extra.length; i++) {
const e = info.extra[i]
assert(e.type >= 0, 'Type is valid')

buf.writeInt16BE(e.type, pos)
Expand Down
10 changes: 5 additions & 5 deletions lib/is-alias.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

var fs = require('fs')
const fs = require('fs')

/**
* Check if a file at path is an OS X alias with a `book\0\0\0\0mark\0\0\0\0` magic header.
Expand All @@ -13,8 +13,8 @@ var fs = require('fs')
* @returns {boolean} Return `true` if the file at path is an alias, otherwise `false`.
*/
module.exports = function isAlias (path) {
var read
var fd = fs.openSync(path, 'r')
let read
const fd = fs.openSync(path, 'r')

try {
read = Buffer.alloc(16)
Expand All @@ -23,8 +23,8 @@ module.exports = function isAlias (path) {
fs.closeSync(fd)
}

var expected = '626f6f6b000000006d61726b00000000'
var actual = read.toString('hex')
const expected = '626f6f6b000000006d61726b00000000'
const actual = read.toString('hex')

return (actual === expected)
}
Loading