asar.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. (function () {
  2. const asar = process.binding('atom_common_asar')
  3. const {Buffer} = require('buffer')
  4. const childProcess = require('child_process')
  5. const path = require('path')
  6. const util = require('util')
  7. const hasProp = {}.hasOwnProperty
  8. // Cache asar archive objects.
  9. const cachedArchives = {}
  10. const getOrCreateArchive = function (p) {
  11. let archive = cachedArchives[p]
  12. if (archive != null) {
  13. return archive
  14. }
  15. archive = asar.createArchive(p)
  16. if (!archive) {
  17. return false
  18. }
  19. cachedArchives[p] = archive
  20. return archive
  21. }
  22. // Clean cache on quit.
  23. process.on('exit', function () {
  24. for (let p in cachedArchives) {
  25. if (!hasProp.call(cachedArchives, p)) continue
  26. cachedArchives[p].destroy()
  27. }
  28. })
  29. // Separate asar package's path from full path.
  30. const splitPath = function (p) {
  31. // shortcut to disable asar.
  32. if (process.noAsar) {
  33. return [false]
  34. }
  35. if (Buffer.isBuffer(p)) {
  36. p = p.toString()
  37. }
  38. if (typeof p !== 'string') {
  39. return [false]
  40. }
  41. if (p.substr(-5) === '.asar') {
  42. return [true, p, '']
  43. }
  44. p = path.normalize(p)
  45. const index = p.lastIndexOf('.asar' + path.sep)
  46. if (index === -1) {
  47. return [false]
  48. }
  49. return [true, p.substr(0, index + 5), p.substr(index + 6)]
  50. }
  51. // Convert asar archive's Stats object to fs's Stats object.
  52. let nextInode = 0
  53. const uid = process.getuid != null ? process.getuid() : 0
  54. const gid = process.getgid != null ? process.getgid() : 0
  55. const fakeTime = new Date()
  56. const asarStatsToFsStats = function (stats) {
  57. return {
  58. dev: 1,
  59. ino: ++nextInode,
  60. mode: 33188,
  61. nlink: 1,
  62. uid: uid,
  63. gid: gid,
  64. rdev: 0,
  65. atime: stats.atime || fakeTime,
  66. birthtime: stats.birthtime || fakeTime,
  67. mtime: stats.mtime || fakeTime,
  68. ctime: stats.ctime || fakeTime,
  69. size: stats.size,
  70. isFile: function () {
  71. return stats.isFile
  72. },
  73. isDirectory: function () {
  74. return stats.isDirectory
  75. },
  76. isSymbolicLink: function () {
  77. return stats.isLink
  78. },
  79. isBlockDevice: function () {
  80. return false
  81. },
  82. isCharacterDevice: function () {
  83. return false
  84. },
  85. isFIFO: function () {
  86. return false
  87. },
  88. isSocket: function () {
  89. return false
  90. }
  91. }
  92. }
  93. // Create a ENOENT error.
  94. const notFoundError = function (asarPath, filePath, callback) {
  95. const error = new Error(`ENOENT, ${filePath} not found in ${asarPath}`)
  96. error.code = 'ENOENT'
  97. error.errno = -2
  98. if (typeof callback !== 'function') {
  99. throw error
  100. }
  101. process.nextTick(function () {
  102. callback(error)
  103. })
  104. }
  105. // Create a ENOTDIR error.
  106. const notDirError = function (callback) {
  107. const error = new Error('ENOTDIR, not a directory')
  108. error.code = 'ENOTDIR'
  109. error.errno = -20
  110. if (typeof callback !== 'function') {
  111. throw error
  112. }
  113. process.nextTick(function () {
  114. callback(error)
  115. })
  116. }
  117. // Create a EACCES error.
  118. const accessError = function (asarPath, filePath, callback) {
  119. const error = new Error(`EACCES: permission denied, access '${filePath}'`)
  120. error.code = 'EACCES'
  121. error.errno = -13
  122. if (typeof callback !== 'function') {
  123. throw error
  124. }
  125. process.nextTick(function () {
  126. callback(error)
  127. })
  128. }
  129. // Create invalid archive error.
  130. const invalidArchiveError = function (asarPath, callback) {
  131. const error = new Error(`Invalid package ${asarPath}`)
  132. if (typeof callback !== 'function') {
  133. throw error
  134. }
  135. process.nextTick(function () {
  136. callback(error)
  137. })
  138. }
  139. // Override APIs that rely on passing file path instead of content to C++.
  140. const overrideAPISync = function (module, name, arg) {
  141. if (arg == null) {
  142. arg = 0
  143. }
  144. const old = module[name]
  145. module[name] = function () {
  146. const p = arguments[arg]
  147. const [isAsar, asarPath, filePath] = splitPath(p)
  148. if (!isAsar) {
  149. return old.apply(this, arguments)
  150. }
  151. const archive = getOrCreateArchive(asarPath)
  152. if (!archive) {
  153. invalidArchiveError(asarPath)
  154. }
  155. const newPath = archive.copyFileOut(filePath)
  156. if (!newPath) {
  157. notFoundError(asarPath, filePath)
  158. }
  159. arguments[arg] = newPath
  160. return old.apply(this, arguments)
  161. }
  162. }
  163. const overrideAPI = function (module, name, arg) {
  164. if (arg == null) {
  165. arg = 0
  166. }
  167. const old = module[name]
  168. module[name] = function () {
  169. const p = arguments[arg]
  170. const [isAsar, asarPath, filePath] = splitPath(p)
  171. if (!isAsar) {
  172. return old.apply(this, arguments)
  173. }
  174. const callback = arguments[arguments.length - 1]
  175. if (typeof callback !== 'function') {
  176. return overrideAPISync(module, name, arg)
  177. }
  178. const archive = getOrCreateArchive(asarPath)
  179. if (!archive) {
  180. return invalidArchiveError(asarPath, callback)
  181. }
  182. const newPath = archive.copyFileOut(filePath)
  183. if (!newPath) {
  184. return notFoundError(asarPath, filePath, callback)
  185. }
  186. arguments[arg] = newPath
  187. return old.apply(this, arguments)
  188. }
  189. }
  190. // Override fs APIs.
  191. exports.wrapFsWithAsar = function (fs) {
  192. const logFDs = {}
  193. const logASARAccess = function (asarPath, filePath, offset) {
  194. if (!process.env.ELECTRON_LOG_ASAR_READS) {
  195. return
  196. }
  197. if (!logFDs[asarPath]) {
  198. const path = require('path')
  199. const logFilename = path.basename(asarPath, '.asar') + '-access-log.txt'
  200. const logPath = path.join(require('os').tmpdir(), logFilename)
  201. logFDs[asarPath] = fs.openSync(logPath, 'a')
  202. console.log('Logging ' + asarPath + ' access to ' + logPath)
  203. }
  204. fs.writeSync(logFDs[asarPath], offset + ': ' + filePath + '\n')
  205. }
  206. const {lstatSync} = fs
  207. fs.lstatSync = function (p) {
  208. const [isAsar, asarPath, filePath] = splitPath(p)
  209. if (!isAsar) {
  210. return lstatSync(p)
  211. }
  212. const archive = getOrCreateArchive(asarPath)
  213. if (!archive) {
  214. invalidArchiveError(asarPath)
  215. }
  216. const stats = archive.stat(filePath)
  217. if (!stats) {
  218. notFoundError(asarPath, filePath)
  219. }
  220. return asarStatsToFsStats(stats)
  221. }
  222. const {lstat} = fs
  223. fs.lstat = function (p, callback) {
  224. const [isAsar, asarPath, filePath] = splitPath(p)
  225. if (!isAsar) {
  226. return lstat(p, callback)
  227. }
  228. const archive = getOrCreateArchive(asarPath)
  229. if (!archive) {
  230. return invalidArchiveError(asarPath, callback)
  231. }
  232. const stats = getOrCreateArchive(asarPath).stat(filePath)
  233. if (!stats) {
  234. return notFoundError(asarPath, filePath, callback)
  235. }
  236. process.nextTick(function () {
  237. callback(null, asarStatsToFsStats(stats))
  238. })
  239. }
  240. const {statSync} = fs
  241. fs.statSync = function (p) {
  242. const [isAsar] = splitPath(p)
  243. if (!isAsar) {
  244. return statSync(p)
  245. }
  246. // Do not distinguish links for now.
  247. return fs.lstatSync(p)
  248. }
  249. const {stat} = fs
  250. fs.stat = function (p, callback) {
  251. const [isAsar] = splitPath(p)
  252. if (!isAsar) {
  253. return stat(p, callback)
  254. }
  255. // Do not distinguish links for now.
  256. process.nextTick(function () {
  257. fs.lstat(p, callback)
  258. })
  259. }
  260. const {statSyncNoException} = fs
  261. fs.statSyncNoException = function (p) {
  262. const [isAsar, asarPath, filePath] = splitPath(p)
  263. if (!isAsar) {
  264. return statSyncNoException(p)
  265. }
  266. const archive = getOrCreateArchive(asarPath)
  267. if (!archive) {
  268. return false
  269. }
  270. const stats = archive.stat(filePath)
  271. if (!stats) {
  272. return false
  273. }
  274. return asarStatsToFsStats(stats)
  275. }
  276. const {realpathSync} = fs
  277. fs.realpathSync = function (p) {
  278. const [isAsar, asarPath, filePath] = splitPath(p)
  279. if (!isAsar) {
  280. return realpathSync.apply(this, arguments)
  281. }
  282. const archive = getOrCreateArchive(asarPath)
  283. if (!archive) {
  284. invalidArchiveError(asarPath)
  285. }
  286. const real = archive.realpath(filePath)
  287. if (real === false) {
  288. notFoundError(asarPath, filePath)
  289. }
  290. return path.join(realpathSync(asarPath), real)
  291. }
  292. const {realpath} = fs
  293. fs.realpath = function (p, cache, callback) {
  294. const [isAsar, asarPath, filePath] = splitPath(p)
  295. if (!isAsar) {
  296. return realpath.apply(this, arguments)
  297. }
  298. if (typeof cache === 'function') {
  299. callback = cache
  300. cache = void 0
  301. }
  302. const archive = getOrCreateArchive(asarPath)
  303. if (!archive) {
  304. return invalidArchiveError(asarPath, callback)
  305. }
  306. const real = archive.realpath(filePath)
  307. if (real === false) {
  308. return notFoundError(asarPath, filePath, callback)
  309. }
  310. return realpath(asarPath, function (err, p) {
  311. if (err) {
  312. return callback(err)
  313. }
  314. return callback(null, path.join(p, real))
  315. })
  316. }
  317. const {exists} = fs
  318. fs.exists = function (p, callback) {
  319. const [isAsar, asarPath, filePath] = splitPath(p)
  320. if (!isAsar) {
  321. return exists(p, callback)
  322. }
  323. const archive = getOrCreateArchive(asarPath)
  324. if (!archive) {
  325. return invalidArchiveError(asarPath, callback)
  326. }
  327. process.nextTick(function () {
  328. callback(archive.stat(filePath) !== false)
  329. })
  330. }
  331. const {existsSync} = fs
  332. fs.existsSync = function (p) {
  333. const [isAsar, asarPath, filePath] = splitPath(p)
  334. if (!isAsar) {
  335. return existsSync(p)
  336. }
  337. const archive = getOrCreateArchive(asarPath)
  338. if (!archive) {
  339. return false
  340. }
  341. return archive.stat(filePath) !== false
  342. }
  343. const {access} = fs
  344. fs.access = function (p, mode, callback) {
  345. const [isAsar, asarPath, filePath] = splitPath(p)
  346. if (!isAsar) {
  347. return access.apply(this, arguments)
  348. }
  349. if (typeof mode === 'function') {
  350. callback = mode
  351. mode = fs.constants.F_OK
  352. }
  353. const archive = getOrCreateArchive(asarPath)
  354. if (!archive) {
  355. return invalidArchiveError(asarPath, callback)
  356. }
  357. const info = archive.getFileInfo(filePath)
  358. if (!info) {
  359. return notFoundError(asarPath, filePath, callback)
  360. }
  361. if (info.unpacked) {
  362. const realPath = archive.copyFileOut(filePath)
  363. return fs.access(realPath, mode, callback)
  364. }
  365. const stats = getOrCreateArchive(asarPath).stat(filePath)
  366. if (!stats) {
  367. return notFoundError(asarPath, filePath, callback)
  368. }
  369. if (mode & fs.constants.W_OK) {
  370. return accessError(asarPath, filePath, callback)
  371. }
  372. process.nextTick(function () {
  373. callback()
  374. })
  375. }
  376. const {accessSync} = fs
  377. fs.accessSync = function (p, mode) {
  378. const [isAsar, asarPath, filePath] = splitPath(p)
  379. if (!isAsar) {
  380. return accessSync.apply(this, arguments)
  381. }
  382. if (mode == null) {
  383. mode = fs.constants.F_OK
  384. }
  385. const archive = getOrCreateArchive(asarPath)
  386. if (!archive) {
  387. invalidArchiveError(asarPath)
  388. }
  389. const info = archive.getFileInfo(filePath)
  390. if (!info) {
  391. notFoundError(asarPath, filePath)
  392. }
  393. if (info.unpacked) {
  394. const realPath = archive.copyFileOut(filePath)
  395. return fs.accessSync(realPath, mode)
  396. }
  397. const stats = getOrCreateArchive(asarPath).stat(filePath)
  398. if (!stats) {
  399. notFoundError(asarPath, filePath)
  400. }
  401. if (mode & fs.constants.W_OK) {
  402. accessError(asarPath, filePath)
  403. }
  404. }
  405. const {readFile} = fs
  406. fs.readFile = function (p, options, callback) {
  407. const [isAsar, asarPath, filePath] = splitPath(p)
  408. if (!isAsar) {
  409. return readFile.apply(this, arguments)
  410. }
  411. if (typeof options === 'function') {
  412. callback = options
  413. options = void 0
  414. }
  415. const archive = getOrCreateArchive(asarPath)
  416. if (!archive) {
  417. return invalidArchiveError(asarPath, callback)
  418. }
  419. const info = archive.getFileInfo(filePath)
  420. if (!info) {
  421. return notFoundError(asarPath, filePath, callback)
  422. }
  423. if (info.size === 0) {
  424. return process.nextTick(function () {
  425. callback(null, new Buffer(0))
  426. })
  427. }
  428. if (info.unpacked) {
  429. const realPath = archive.copyFileOut(filePath)
  430. return fs.readFile(realPath, options, callback)
  431. }
  432. if (!options) {
  433. options = {
  434. encoding: null
  435. }
  436. } else if (util.isString(options)) {
  437. options = {
  438. encoding: options
  439. }
  440. } else if (!util.isObject(options)) {
  441. throw new TypeError('Bad arguments')
  442. }
  443. const {encoding} = options
  444. const buffer = new Buffer(info.size)
  445. const fd = archive.getFd()
  446. if (!(fd >= 0)) {
  447. return notFoundError(asarPath, filePath, callback)
  448. }
  449. logASARAccess(asarPath, filePath, info.offset)
  450. fs.read(fd, buffer, 0, info.size, info.offset, function (error) {
  451. callback(error, encoding ? buffer.toString(encoding) : buffer)
  452. })
  453. }
  454. const {readFileSync} = fs
  455. fs.readFileSync = function (p, options) {
  456. const [isAsar, asarPath, filePath] = splitPath(p)
  457. if (!isAsar) {
  458. return readFileSync.apply(this, arguments)
  459. }
  460. const archive = getOrCreateArchive(asarPath)
  461. if (!archive) {
  462. invalidArchiveError(asarPath)
  463. }
  464. const info = archive.getFileInfo(filePath)
  465. if (!info) {
  466. notFoundError(asarPath, filePath)
  467. }
  468. if (info.size === 0) {
  469. if (options) {
  470. return ''
  471. } else {
  472. return new Buffer(0)
  473. }
  474. }
  475. if (info.unpacked) {
  476. const realPath = archive.copyFileOut(filePath)
  477. return fs.readFileSync(realPath, options)
  478. }
  479. if (!options) {
  480. options = {
  481. encoding: null
  482. }
  483. } else if (util.isString(options)) {
  484. options = {
  485. encoding: options
  486. }
  487. } else if (!util.isObject(options)) {
  488. throw new TypeError('Bad arguments')
  489. }
  490. const {encoding} = options
  491. const buffer = new Buffer(info.size)
  492. const fd = archive.getFd()
  493. if (!(fd >= 0)) {
  494. notFoundError(asarPath, filePath)
  495. }
  496. logASARAccess(asarPath, filePath, info.offset)
  497. fs.readSync(fd, buffer, 0, info.size, info.offset)
  498. if (encoding) {
  499. return buffer.toString(encoding)
  500. } else {
  501. return buffer
  502. }
  503. }
  504. const {readdir} = fs
  505. fs.readdir = function (p, callback) {
  506. const [isAsar, asarPath, filePath] = splitPath(p)
  507. if (!isAsar) {
  508. return readdir.apply(this, arguments)
  509. }
  510. const archive = getOrCreateArchive(asarPath)
  511. if (!archive) {
  512. return invalidArchiveError(asarPath, callback)
  513. }
  514. const files = archive.readdir(filePath)
  515. if (!files) {
  516. return notFoundError(asarPath, filePath, callback)
  517. }
  518. process.nextTick(function () {
  519. callback(null, files)
  520. })
  521. }
  522. const {readdirSync} = fs
  523. fs.readdirSync = function (p) {
  524. const [isAsar, asarPath, filePath] = splitPath(p)
  525. if (!isAsar) {
  526. return readdirSync.apply(this, arguments)
  527. }
  528. const archive = getOrCreateArchive(asarPath)
  529. if (!archive) {
  530. invalidArchiveError(asarPath)
  531. }
  532. const files = archive.readdir(filePath)
  533. if (!files) {
  534. notFoundError(asarPath, filePath)
  535. }
  536. return files
  537. }
  538. const {internalModuleReadFile} = process.binding('fs')
  539. process.binding('fs').internalModuleReadFile = function (p) {
  540. const [isAsar, asarPath, filePath] = splitPath(p)
  541. if (!isAsar) {
  542. return internalModuleReadFile(p)
  543. }
  544. const archive = getOrCreateArchive(asarPath)
  545. if (!archive) {
  546. return
  547. }
  548. const info = archive.getFileInfo(filePath)
  549. if (!info) {
  550. return
  551. }
  552. if (info.size === 0) {
  553. return ''
  554. }
  555. if (info.unpacked) {
  556. const realPath = archive.copyFileOut(filePath)
  557. return fs.readFileSync(realPath, {
  558. encoding: 'utf8'
  559. })
  560. }
  561. const buffer = new Buffer(info.size)
  562. const fd = archive.getFd()
  563. if (!(fd >= 0)) {
  564. return
  565. }
  566. logASARAccess(asarPath, filePath, info.offset)
  567. fs.readSync(fd, buffer, 0, info.size, info.offset)
  568. return buffer.toString('utf8')
  569. }
  570. const {internalModuleStat} = process.binding('fs')
  571. process.binding('fs').internalModuleStat = function (p) {
  572. const [isAsar, asarPath, filePath] = splitPath(p)
  573. if (!isAsar) {
  574. return internalModuleStat(p)
  575. }
  576. const archive = getOrCreateArchive(asarPath)
  577. // -ENOENT
  578. if (!archive) {
  579. return -34
  580. }
  581. const stats = archive.stat(filePath)
  582. // -ENOENT
  583. if (!stats) {
  584. return -34
  585. }
  586. if (stats.isDirectory) {
  587. return 1
  588. } else {
  589. return 0
  590. }
  591. }
  592. // Calling mkdir for directory inside asar archive should throw ENOTDIR
  593. // error, but on Windows it throws ENOENT.
  594. // This is to work around the recursive looping bug of mkdirp since it is
  595. // widely used.
  596. if (process.platform === 'win32') {
  597. const {mkdir} = fs
  598. fs.mkdir = function (p, mode, callback) {
  599. if (typeof mode === 'function') {
  600. callback = mode
  601. }
  602. const [isAsar, , filePath] = splitPath(p)
  603. if (isAsar && filePath.length) {
  604. return notDirError(callback)
  605. }
  606. mkdir(p, mode, callback)
  607. }
  608. const {mkdirSync} = fs
  609. fs.mkdirSync = function (p, mode) {
  610. const [isAsar, , filePath] = splitPath(p)
  611. if (isAsar && filePath.length) {
  612. notDirError()
  613. }
  614. return mkdirSync(p, mode)
  615. }
  616. }
  617. // Executing a command string containing a path to an asar
  618. // archive confuses `childProcess.execFile`, which is internally
  619. // called by `childProcess.{exec,execSync}`, causing
  620. // Electron to consider the full command as a single path
  621. // to an archive.
  622. ['exec', 'execSync'].forEach(function (functionName) {
  623. const old = childProcess[functionName]
  624. childProcess[functionName] = function () {
  625. const processNoAsarOriginalValue = process.noAsar
  626. process.noAsar = true
  627. try {
  628. return old.apply(this, arguments)
  629. } finally {
  630. process.noAsar = processNoAsarOriginalValue
  631. }
  632. }
  633. })
  634. overrideAPI(fs, 'open')
  635. overrideAPI(childProcess, 'execFile')
  636. overrideAPISync(process, 'dlopen', 1)
  637. overrideAPISync(require('module')._extensions, '.node', 1)
  638. overrideAPISync(fs, 'openSync')
  639. overrideAPISync(childProcess, 'execFileSync')
  640. }
  641. })()