atom_api_asar.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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/native_mate_converters/callback.h"
  8. #include "atom/common/native_mate_converters/file_path_converter.h"
  9. #include "native_mate/arguments.h"
  10. #include "native_mate/dictionary.h"
  11. #include "native_mate/object_template_builder.h"
  12. #include "native_mate/wrappable.h"
  13. #include "atom/common/node_includes.h"
  14. #include "atom_natives.h" // NOLINT: This file is generated with js2c.
  15. namespace {
  16. class Archive : public mate::Wrappable<Archive> {
  17. public:
  18. static v8::Local<v8::Value> Create(v8::Isolate* isolate,
  19. const base::FilePath& path) {
  20. auto archive = std::make_unique<asar::Archive>(path);
  21. if (!archive->Init())
  22. return v8::False(isolate);
  23. return (new Archive(isolate, std::move(archive)))->GetWrapper();
  24. }
  25. static void BuildPrototype(v8::Isolate* isolate,
  26. v8::Local<v8::FunctionTemplate> prototype) {
  27. prototype->SetClassName(mate::StringToV8(isolate, "Archive"));
  28. mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
  29. .SetProperty("path", &Archive::GetPath)
  30. .SetMethod("getFileInfo", &Archive::GetFileInfo)
  31. .SetMethod("stat", &Archive::Stat)
  32. .SetMethod("readdir", &Archive::Readdir)
  33. .SetMethod("realpath", &Archive::Realpath)
  34. .SetMethod("copyFileOut", &Archive::CopyFileOut)
  35. .SetMethod("getFd", &Archive::GetFD)
  36. .SetMethod("destroy", &Archive::Destroy);
  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. // Free the resources used by archive.
  101. void Destroy() { archive_.reset(); }
  102. private:
  103. std::unique_ptr<asar::Archive> archive_;
  104. DISALLOW_COPY_AND_ASSIGN(Archive);
  105. };
  106. void InitAsarSupport(v8::Isolate* isolate,
  107. v8::Local<v8::Value> process,
  108. v8::Local<v8::Value> require) {
  109. // Evaluate asar_init.js.
  110. v8::Local<v8::Script> asar_init =
  111. v8::Script::Compile(node::asar_init_value.ToStringChecked(isolate));
  112. v8::Local<v8::Value> result = asar_init->Run();
  113. // Initialize asar support.
  114. if (result->IsFunction()) {
  115. v8::Local<v8::Value> args[] = {
  116. process,
  117. require,
  118. node::asar_value.ToStringChecked(isolate),
  119. };
  120. result.As<v8::Function>()->Call(result, 3, args);
  121. }
  122. }
  123. void Initialize(v8::Local<v8::Object> exports,
  124. v8::Local<v8::Value> unused,
  125. v8::Local<v8::Context> context,
  126. void* priv) {
  127. mate::Dictionary dict(context->GetIsolate(), exports);
  128. dict.SetMethod("createArchive", &Archive::Create);
  129. dict.SetMethod("initAsarSupport", &InitAsarSupport);
  130. }
  131. } // namespace
  132. NODE_BUILTIN_MODULE_CONTEXT_AWARE(atom_common_asar, Initialize)