-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathcreate.js
146 lines (116 loc) · 2.94 KB
/
create.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
var fs = require('fs')
var path = require('path')
var assert = require('assert')
var encode = require('./encode')
var addon = require('../build/Release/volume.node')
var findVolume = function (startPath, startStat) {
var lastDev = startStat.dev
var lastIno = startStat.ino
var lastPath = startPath
while (1) {
var parentPath = path.resolve(lastPath, '..')
var parentStat = fs.statSync(parentPath)
if (parentStat.dev !== lastDev) {
return lastPath
}
if (parentStat.ino === lastIno) {
return lastPath
}
lastDev = parentStat.dev
lastIno = parentStat.ino
lastPath = parentPath
}
}
var utf16be = function (str) {
var b = new Buffer(str, 'ucs2')
for (var i = 0; i < b.length; i += 2) {
var a = b[i]
b[i] = b[i + 1]
b[i + 1] = a
}
return b
}
module.exports = exports = function (targetPath) {
var 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)
assert(targetStat.isFile() || targetStat.isDirectory(), 'Target is a file or directory')
info.target = {
id: targetStat.ino,
type: (targetStat.isDirectory() ? 'directory' : 'file'),
filename: path.basename(targetPath),
created: targetStat.ctime
}
info.parent = {
id: parentStat.ino,
name: path.basename(parentPath)
}
info.volume = {
name: addon.getVolumeName(volumePath),
created: volumeStat.ctime,
signature: 'H+',
type: (volumePath === '/' ? 'local' : 'other')
};
(function addType0 () {
var b = new Buffer(info.parent.name, 'utf8')
info.extra.push({
type: 0,
length: b.length,
data: b
})
}());
(function addType1 () {
var b = new Buffer(4)
b.writeUInt32BE(info.parent.id, 0)
info.extra.push({
type: 1,
length: b.length,
data: b
})
}());
(function addType14 () {
var l = info.target.filename.length
var b = new Buffer(2 + (l * 2))
b.writeUInt16BE(l, 0)
utf16be(info.target.filename).copy(b, 2)
info.extra.push({
type: 14,
length: b.length,
data: b
})
}());
(function addType15 () {
var l = info.volume.name.length
var b = new Buffer(2 + (l * 2))
b.writeUInt16BE(l, 0)
utf16be(info.volume.name).copy(b, 2)
info.extra.push({
type: 15,
length: b.length,
data: b
})
}());
(function addType18 () {
var vl = volumePath.length
assert.equal(targetPath.slice(0, vl), volumePath)
var lp = targetPath.slice(vl)
var b = new Buffer(lp, 'utf8')
info.extra.push({
type: 18,
length: b.length,
data: b
})
}());
(function addType19 () {
var b = new Buffer(volumePath, 'utf8')
info.extra.push({
type: 19,
length: b.length,
data: b
})
}())
return encode(info)
}