asar.js 19 KB

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