atom_api_asar.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright (c) 2014 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include <stddef.h>
  5. #include <vector>
  6. #include "atom/common/asar/archive.h"
  7. #include "atom/common/asar/asar_util.h"
  8. #include "atom/common/native_mate_converters/callback.h"
  9. #include "atom/common/native_mate_converters/file_path_converter.h"
  10. #include "native_mate/arguments.h"
  11. #include "native_mate/dictionary.h"
  12. #include "native_mate/object_template_builder.h"
  13. #include "native_mate/wrappable.h"
  14. #include "atom/common/node_includes.h"
  15. #include "third_party/electron_node/src/node_native_module.h"
  16. namespace {
  17. class Archive : public mate::Wrappable<Archive> {
  18. public:
  19. static v8::Local<v8::Value> Create(v8::Isolate* isolate,
  20. const base::FilePath& path) {
  21. auto archive = std::make_unique<asar::Archive>(path);
  22. if (!archive->Init())
  23. return v8::False(isolate);
  24. return (new Archive(isolate, std::move(archive)))->GetWrapper();
  25. }
  26. static void BuildPrototype(v8::Isolate* isolate,
  27. v8::Local<v8::FunctionTemplate> prototype) {
  28. prototype->SetClassName(mate::StringToV8(isolate, "Archive"));
  29. mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
  30. .SetProperty("path", &Archive::GetPath)
  31. .SetMethod("getFileInfo", &Archive::GetFileInfo)
  32. .SetMethod("stat", &Archive::Stat)
  33. .SetMethod("readdir", &Archive::Readdir)
  34. .SetMethod("realpath", &Archive::Realpath)
  35. .SetMethod("copyFileOut", &Archive::CopyFileOut)
  36. .SetMethod("getFd", &Archive::GetFD);
  37. }
  38. protected:
  39. Archive(v8::Isolate* isolate, std::unique_ptr<asar::Archive> archive)
  40. : archive_(std::move(archive)) {
  41. Init(isolate);
  42. }
  43. // Returns the path of the file.
  44. base::FilePath GetPath() { return archive_->path(); }
  45. // Reads the offset and size of file.
  46. v8::Local<v8::Value> GetFileInfo(v8::Isolate* isolate,
  47. const base::FilePath& path) {
  48. asar::Archive::FileInfo info;
  49. if (!archive_ || !archive_->GetFileInfo(path, &info))
  50. return v8::False(isolate);
  51. mate::Dictionary dict(isolate, v8::Object::New(isolate));
  52. dict.Set("size", info.size);
  53. dict.Set("unpacked", info.unpacked);
  54. dict.Set("offset", info.offset);
  55. return dict.GetHandle();
  56. }
  57. // Returns a fake result of fs.stat(path).
  58. v8::Local<v8::Value> Stat(v8::Isolate* isolate, const base::FilePath& path) {
  59. asar::Archive::Stats stats;
  60. if (!archive_ || !archive_->Stat(path, &stats))
  61. return v8::False(isolate);
  62. mate::Dictionary dict(isolate, v8::Object::New(isolate));
  63. dict.Set("size", stats.size);
  64. dict.Set("offset", stats.offset);
  65. dict.Set("isFile", stats.is_file);
  66. dict.Set("isDirectory", stats.is_directory);
  67. dict.Set("isLink", stats.is_link);
  68. return dict.GetHandle();
  69. }
  70. // Returns all files under a directory.
  71. v8::Local<v8::Value> Readdir(v8::Isolate* isolate,
  72. const base::FilePath& path) {
  73. std::vector<base::FilePath> files;
  74. if (!archive_ || !archive_->Readdir(path, &files))
  75. return v8::False(isolate);
  76. return mate::ConvertToV8(isolate, files);
  77. }
  78. // Returns the path of file with symbol link resolved.
  79. v8::Local<v8::Value> Realpath(v8::Isolate* isolate,
  80. const base::FilePath& path) {
  81. base::FilePath realpath;
  82. if (!archive_ || !archive_->Realpath(path, &realpath))
  83. return v8::False(isolate);
  84. return mate::ConvertToV8(isolate, realpath);
  85. }
  86. // Copy the file out into a temporary file and returns the new path.
  87. v8::Local<v8::Value> CopyFileOut(v8::Isolate* isolate,
  88. const base::FilePath& path) {
  89. base::FilePath new_path;
  90. if (!archive_ || !archive_->CopyFileOut(path, &new_path))
  91. return v8::False(isolate);
  92. return mate::ConvertToV8(isolate, new_path);
  93. }
  94. // Return the file descriptor.
  95. int GetFD() const {
  96. if (!archive_)
  97. return -1;
  98. return archive_->GetFD();
  99. }
  100. private:
  101. std::unique_ptr<asar::Archive> archive_;
  102. DISALLOW_COPY_AND_ASSIGN(Archive);
  103. };
  104. void InitAsarSupport(v8::Isolate* isolate, v8::Local<v8::Value> require) {
  105. // Evaluate asar_init.js.
  106. std::vector<v8::Local<v8::String>> asar_init_params = {
  107. node::FIXED_ONE_BYTE_STRING(isolate, "require")};
  108. std::vector<v8::Local<v8::Value>> asar_init_args = {require};
  109. node::per_process::native_module_loader.CompileAndCall(
  110. isolate->GetCurrentContext(), "electron/js2c/asar_init",
  111. &asar_init_params, &asar_init_args, nullptr);
  112. }
  113. v8::Local<v8::Value> SplitPath(v8::Isolate* isolate,
  114. const base::FilePath& path) {
  115. mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
  116. base::FilePath asar_path, file_path;
  117. if (asar::GetAsarArchivePath(path, &asar_path, &file_path, true)) {
  118. dict.Set("isAsar", true);
  119. dict.Set("asarPath", asar_path);
  120. dict.Set("filePath", file_path);
  121. } else {
  122. dict.Set("isAsar", false);
  123. }
  124. return dict.GetHandle();
  125. }
  126. void Initialize(v8::Local<v8::Object> exports,
  127. v8::Local<v8::Value> unused,
  128. v8::Local<v8::Context> context,
  129. void* priv) {
  130. mate::Dictionary dict(context->GetIsolate(), exports);
  131. dict.SetMethod("createArchive", &Archive::Create);
  132. dict.SetMethod("splitPath", &SplitPath);
  133. dict.SetMethod("initAsarSupport", &InitAsarSupport);
  134. }
  135. } // namespace
  136. NODE_LINKED_MODULE_CONTEXT_AWARE(atom_common_asar, Initialize)