asar.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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 = {
  418. encoding: null
  419. }
  420. } else if (util.isString(options)) {
  421. options = {
  422. encoding: options
  423. }
  424. } else if (!util.isObject(options)) {
  425. throw new TypeError('Bad arguments')
  426. }
  427. const {encoding} = options
  428. const archive = getOrCreateArchive(asarPath)
  429. if (!archive) {
  430. return invalidArchiveError(asarPath, callback)
  431. }
  432. const info = archive.getFileInfo(filePath)
  433. if (!info) {
  434. return notFoundError(asarPath, filePath, callback)
  435. }
  436. if (info.size === 0) {
  437. return process.nextTick(function () {
  438. callback(null, encoding ? '' : new Buffer(0))
  439. })
  440. }
  441. if (info.unpacked) {
  442. const realPath = archive.copyFileOut(filePath)
  443. return fs.readFile(realPath, options, callback)
  444. }
  445. const buffer = new Buffer(info.size)
  446. const fd = archive.getFd()
  447. if (!(fd >= 0)) {
  448. return notFoundError(asarPath, filePath, callback)
  449. }
  450. logASARAccess(asarPath, filePath, info.offset)
  451. fs.read(fd, buffer, 0, info.size, info.offset, function (error) {
  452. callback(error, encoding ? buffer.toString(encoding) : buffer)
  453. })
  454. }
  455. const {readFileSync} = fs
  456. fs.readFileSync = function (p, options) {
  457. const [isAsar, asarPath, filePath] = splitPath(p)
  458. if (!isAsar) {
  459. return readFileSync.apply(this, arguments)
  460. }
  461. const archive = getOrCreateArchive(asarPath)
  462. if (!archive) {
  463. invalidArchiveError(asarPath)
  464. }
  465. const info = archive.getFileInfo(filePath)
  466. if (!info) {
  467. notFoundError(asarPath, filePath)
  468. }
  469. if (info.size === 0) {
  470. if (options) {
  471. return ''
  472. } else {
  473. return new Buffer(0)
  474. }
  475. }
  476. if (info.unpacked) {
  477. const realPath = archive.copyFileOut(filePath)
  478. return fs.readFileSync(realPath, options)
  479. }
  480. if (!options) {
  481. options = {
  482. encoding: null
  483. }
  484. } else if (util.isString(options)) {
  485. options = {
  486. encoding: options
  487. }
  488. } else if (!util.isObject(options)) {
  489. throw new TypeError('Bad arguments')
  490. }
  491. const {encoding} = options
  492. const buffer = new Buffer(info.size)
  493. const fd = archive.getFd()
  494. if (!(fd >= 0)) {
  495. notFoundError(asarPath, filePath)
  496. }
  497. logASARAccess(asarPath, filePath, info.offset)
  498. fs.readSync(fd, buffer, 0, info.size, info.offset)
  499. if (encoding) {
  500. return buffer.toString(encoding)
  501. } else {
  502. return buffer
  503. }
  504. }
  505. const {readdir} = fs
  506. fs.readdir = function (p, callback) {
  507. const [isAsar, asarPath, filePath] = splitPath(p)
  508. if (!isAsar) {
  509. return readdir.apply(this, arguments)
  510. }
  511. const archive = getOrCreateArchive(asarPath)
  512. if (!archive) {
  513. return invalidArchiveError(asarPath, callback)
  514. }
  515. const files = archive.readdir(filePath)
  516. if (!files) {
  517. return notFoundError(asarPath, filePath, callback)
  518. }
  519. process.nextTick(function () {
  520. callback(null, files)
  521. })
  522. }
  523. const {readdirSync} = fs
  524. fs.readdirSync = function (p) {
  525. const [isAsar, asarPath, filePath] = splitPath(p)
  526. if (!isAsar) {
  527. return readdirSync.apply(this, arguments)
  528. }
  529. const archive = getOrCreateArchive(asarPath)
  530. if (!archive) {
  531. invalidArchiveError(asarPath)
  532. }
  533. const files = archive.readdir(filePath)
  534. if (!files) {
  535. notFoundError(asarPath, filePath)
  536. }
  537. return files
  538. }
  539. const {internalModuleReadFile} = process.binding('fs')
  540. process.binding('fs').internalModuleReadFile = function (p) {
  541. const [isAsar, asarPath, filePath] = splitPath(p)
  542. if (!isAsar) {
  543. return internalModuleReadFile(p)
  544. }
  545. const archive = getOrCreateArchive(asarPath)
  546. if (!archive) {
  547. return
  548. }
  549. const info = archive.getFileInfo(filePath)
  550. if (!info) {
  551. return
  552. }
  553. if (info.size === 0) {
  554. return ''
  555. }
  556. if (info.unpacked) {
  557. const realPath = archive.copyFileOut(filePath)
  558. return fs.readFileSync(realPath, {
  559. encoding: 'utf8'
  560. })
  561. }
  562. const buffer = new Buffer(info.size)
  563. const fd = archive.getFd()
  564. if (!(fd >= 0)) {
  565. return
  566. }
  567. logASARAccess(asarPath, filePath, info.offset)
  568. fs.readSync(fd, buffer, 0, info.size, info.offset)
  569. return buffer.toString('utf8')
  570. }
  571. const {internalModuleStat} = process.binding('fs')
  572. process.binding('fs').internalModuleStat = function (p) {
  573. const [isAsar, asarPath, filePath] = splitPath(p)
  574. if (!isAsar) {
  575. return internalModuleStat(p)
  576. }
  577. const archive = getOrCreateArchive(asarPath)
  578. // -ENOENT
  579. if (!archive) {
  580. return -34
  581. }
  582. const stats = archive.stat(filePath)
  583. // -ENOENT
  584. if (!stats) {
  585. return -34
  586. }
  587. if (stats.isDirectory) {
  588. return 1
  589. } else {
  590. return 0
  591. }
  592. }
  593. // Calling mkdir for directory inside asar archive should throw ENOTDIR
  594. // error, but on Windows it throws ENOENT.
  595. // This is to work around the recursive looping bug of mkdirp since it is
  596. // widely used.
  597. if (process.platform === 'win32') {
  598. const {mkdir} = fs
  599. fs.mkdir = function (p, mode, callback) {
  600. if (typeof mode === 'function') {
  601. callback = mode
  602. }
  603. const [isAsar, , filePath] = splitPath(p)
  604. if (isAsar && filePath.length) {
  605. return notDirError(callback)
  606. }
  607. mkdir(p, mode, callback)
  608. }
  609. const {mkdirSync} = fs
  610. fs.mkdirSync = function (p, mode) {
  611. const [isAsar, , filePath] = splitPath(p)
  612. if (isAsar && filePath.length) {
  613. notDirError()
  614. }
  615. return mkdirSync(p, mode)
  616. }
  617. }
  618. // Executing a command string containing a path to an asar
  619. // archive confuses `childProcess.execFile`, which is internally
  620. // called by `childProcess.{exec,execSync}`, causing
  621. // Electron to consider the full command as a single path
  622. // to an archive.
  623. ['exec', 'execSync'].forEach(function (functionName) {
  624. const old = childProcess[functionName]
  625. childProcess[functionName] = function () {
  626. const processNoAsarOriginalValue = process.noAsar
  627. process.noAsar = true
  628. try {
  629. return old.apply(this, arguments)
  630. } finally {
  631. process.noAsar = processNoAsarOriginalValue
  632. }
  633. }
  634. })
  635. overrideAPI(fs, 'open')
  636. overrideAPI(childProcess, 'execFile')
  637. overrideAPISync(process, 'dlopen', 1)
  638. overrideAPISync(require('module')._extensions, '.node', 1)
  639. overrideAPISync(fs, 'openSync')
  640. overrideAPISync(childProcess, 'execFileSync')
  641. }
  642. })()