| 
									
										
										
										
											2014-10-31 11:17:05 -07:00
										 |  |  | // Copyright (c) 2014 GitHub, Inc.
 | 
					
						
							| 
									
										
										
										
											2014-09-24 11:10:07 +08:00
										 |  |  | // Use of this source code is governed by the MIT license that can be
 | 
					
						
							|  |  |  | // found in the LICENSE file.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-04 11:18:25 -08:00
										 |  |  | #include <stddef.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-24 18:44:00 +08:00
										 |  |  | #include <vector>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-04 11:01:01 -08:00
										 |  |  | #include "atom_natives.h"  // NOLINT: This file is generated with coffee2c.
 | 
					
						
							| 
									
										
										
										
											2014-09-24 11:10:07 +08:00
										 |  |  | #include "atom/common/asar/archive.h"
 | 
					
						
							| 
									
										
										
										
											2015-08-07 18:10:19 +08:00
										 |  |  | #include "atom/common/native_mate_converters/callback.h"
 | 
					
						
							| 
									
										
										
										
											2014-09-24 11:10:07 +08:00
										 |  |  | #include "atom/common/native_mate_converters/file_path_converter.h"
 | 
					
						
							| 
									
										
										
										
											2015-09-07 16:29:54 +08:00
										 |  |  | #include "atom/common/node_includes.h"
 | 
					
						
							| 
									
										
										
										
											2014-09-24 11:10:07 +08:00
										 |  |  | #include "native_mate/arguments.h"
 | 
					
						
							|  |  |  | #include "native_mate/dictionary.h"
 | 
					
						
							|  |  |  | #include "native_mate/object_template_builder.h"
 | 
					
						
							|  |  |  | #include "native_mate/wrappable.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-03 16:04:46 +08:00
										 |  |  | v8::Persistent<v8::ObjectTemplate> template_; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-24 11:10:07 +08:00
										 |  |  | class Archive : public mate::Wrappable { | 
					
						
							|  |  |  |  public: | 
					
						
							| 
									
										
										
										
											2015-05-22 19:11:22 +08:00
										 |  |  |   static v8::Local<v8::Value> Create(v8::Isolate* isolate, | 
					
						
							| 
									
										
										
										
											2014-09-24 11:10:07 +08:00
										 |  |  |                                       const base::FilePath& path) { | 
					
						
							| 
									
										
										
										
											2014-09-25 20:48:32 +08:00
										 |  |  |     scoped_ptr<asar::Archive> archive(new asar::Archive(path)); | 
					
						
							|  |  |  |     if (!archive->Init()) | 
					
						
							| 
									
										
										
										
											2014-09-24 18:44:00 +08:00
										 |  |  |       return v8::False(isolate); | 
					
						
							| 
									
										
										
										
											2016-03-08 23:28:53 +09:00
										 |  |  |     return (new Archive(std::move(archive)))->GetWrapper(isolate); | 
					
						
							| 
									
										
										
										
											2014-09-24 11:10:07 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  protected: | 
					
						
							| 
									
										
										
										
											2014-09-25 20:48:32 +08:00
										 |  |  |   explicit Archive(scoped_ptr<asar::Archive> archive) | 
					
						
							| 
									
										
										
										
											2016-03-08 23:28:53 +09:00
										 |  |  |       : archive_(std::move(archive)) {} | 
					
						
							| 
									
										
										
										
											2014-09-24 11:10:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-24 12:02:33 +08:00
										 |  |  |   // Reads the offset and size of file.
 | 
					
						
							| 
									
										
										
										
											2015-05-22 19:11:22 +08:00
										 |  |  |   v8::Local<v8::Value> GetFileInfo(v8::Isolate* isolate, | 
					
						
							| 
									
										
										
										
											2014-09-24 11:10:07 +08:00
										 |  |  |                                     const base::FilePath& path) { | 
					
						
							|  |  |  |     asar::Archive::FileInfo info; | 
					
						
							| 
									
										
										
										
											2014-09-25 20:38:12 +08:00
										 |  |  |     if (!archive_ || !archive_->GetFileInfo(path, &info)) | 
					
						
							| 
									
										
										
										
											2014-09-24 18:44:00 +08:00
										 |  |  |       return v8::False(isolate); | 
					
						
							|  |  |  |     mate::Dictionary dict(isolate, v8::Object::New(isolate)); | 
					
						
							| 
									
										
										
										
											2014-09-24 11:10:07 +08:00
										 |  |  |     dict.Set("size", info.size); | 
					
						
							| 
									
										
										
										
											2015-03-20 20:34:58 +08:00
										 |  |  |     dict.Set("unpacked", info.unpacked); | 
					
						
							| 
									
										
										
										
											2014-09-24 11:10:07 +08:00
										 |  |  |     dict.Set("offset", info.offset); | 
					
						
							|  |  |  |     return dict.GetHandle(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-24 12:02:33 +08:00
										 |  |  |   // Returns a fake result of fs.stat(path).
 | 
					
						
							| 
									
										
										
										
											2015-05-22 19:11:22 +08:00
										 |  |  |   v8::Local<v8::Value> Stat(v8::Isolate* isolate, | 
					
						
							| 
									
										
										
										
											2014-09-24 12:02:33 +08:00
										 |  |  |                              const base::FilePath& path) { | 
					
						
							|  |  |  |     asar::Archive::Stats stats; | 
					
						
							| 
									
										
										
										
											2014-09-25 20:38:12 +08:00
										 |  |  |     if (!archive_ || !archive_->Stat(path, &stats)) | 
					
						
							| 
									
										
										
										
											2014-09-24 18:44:00 +08:00
										 |  |  |       return v8::False(isolate); | 
					
						
							|  |  |  |     mate::Dictionary dict(isolate, v8::Object::New(isolate)); | 
					
						
							| 
									
										
										
										
											2014-09-24 12:02:33 +08:00
										 |  |  |     dict.Set("size", stats.size); | 
					
						
							|  |  |  |     dict.Set("offset", stats.offset); | 
					
						
							|  |  |  |     dict.Set("isFile", stats.is_file); | 
					
						
							|  |  |  |     dict.Set("isDirectory", stats.is_directory); | 
					
						
							|  |  |  |     dict.Set("isLink", stats.is_link); | 
					
						
							|  |  |  |     return dict.GetHandle(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-24 18:44:00 +08:00
										 |  |  |   // Returns all files under a directory.
 | 
					
						
							| 
									
										
										
										
											2015-05-22 19:11:22 +08:00
										 |  |  |   v8::Local<v8::Value> Readdir(v8::Isolate* isolate, | 
					
						
							| 
									
										
										
										
											2014-09-24 18:44:00 +08:00
										 |  |  |                                 const base::FilePath& path) { | 
					
						
							|  |  |  |     std::vector<base::FilePath> files; | 
					
						
							| 
									
										
										
										
											2014-09-25 20:38:12 +08:00
										 |  |  |     if (!archive_ || !archive_->Readdir(path, &files)) | 
					
						
							| 
									
										
										
										
											2014-09-24 18:44:00 +08:00
										 |  |  |       return v8::False(isolate); | 
					
						
							|  |  |  |     return mate::ConvertToV8(isolate, files); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-30 14:53:41 +08:00
										 |  |  |   // Returns the path of file with symbol link resolved.
 | 
					
						
							| 
									
										
										
										
											2015-05-22 19:11:22 +08:00
										 |  |  |   v8::Local<v8::Value> Realpath(v8::Isolate* isolate, | 
					
						
							| 
									
										
										
										
											2014-09-30 14:53:41 +08:00
										 |  |  |                                  const base::FilePath& path) { | 
					
						
							|  |  |  |     base::FilePath realpath; | 
					
						
							|  |  |  |     if (!archive_ || !archive_->Realpath(path, &realpath)) | 
					
						
							|  |  |  |       return v8::False(isolate); | 
					
						
							|  |  |  |     return mate::ConvertToV8(isolate, realpath); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-25 16:56:50 +08:00
										 |  |  |   // Copy the file out into a temporary file and returns the new path.
 | 
					
						
							| 
									
										
										
										
											2015-05-22 19:11:22 +08:00
										 |  |  |   v8::Local<v8::Value> CopyFileOut(v8::Isolate* isolate, | 
					
						
							| 
									
										
										
										
											2014-09-25 16:56:50 +08:00
										 |  |  |                                     const base::FilePath& path) { | 
					
						
							|  |  |  |     base::FilePath new_path; | 
					
						
							| 
									
										
										
										
											2014-09-25 20:38:12 +08:00
										 |  |  |     if (!archive_ || !archive_->CopyFileOut(path, &new_path)) | 
					
						
							| 
									
										
										
										
											2014-09-25 16:56:50 +08:00
										 |  |  |       return v8::False(isolate); | 
					
						
							|  |  |  |     return mate::ConvertToV8(isolate, new_path); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-11 11:02:17 +08:00
										 |  |  |   // Return the file descriptor.
 | 
					
						
							|  |  |  |   int GetFD() const { | 
					
						
							|  |  |  |     if (!archive_) | 
					
						
							|  |  |  |       return -1; | 
					
						
							|  |  |  |     return archive_->GetFD(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-25 21:49:01 +08:00
										 |  |  |   // Free the resources used by archive.
 | 
					
						
							|  |  |  |   void Destroy() { | 
					
						
							|  |  |  |     archive_.reset(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-24 11:10:07 +08:00
										 |  |  |   // mate::Wrappable:
 | 
					
						
							|  |  |  |   mate::ObjectTemplateBuilder GetObjectTemplateBuilder(v8::Isolate* isolate) { | 
					
						
							| 
									
										
										
										
											2015-12-03 16:04:46 +08:00
										 |  |  |     if (template_.IsEmpty()) | 
					
						
							|  |  |  |       template_.Reset(isolate, mate::ObjectTemplateBuilder(isolate) | 
					
						
							|  |  |  |           .SetValue("path", archive_->path()) | 
					
						
							|  |  |  |           .SetMethod("getFileInfo", &Archive::GetFileInfo) | 
					
						
							|  |  |  |           .SetMethod("stat", &Archive::Stat) | 
					
						
							|  |  |  |           .SetMethod("readdir", &Archive::Readdir) | 
					
						
							|  |  |  |           .SetMethod("realpath", &Archive::Realpath) | 
					
						
							|  |  |  |           .SetMethod("copyFileOut", &Archive::CopyFileOut) | 
					
						
							|  |  |  |           .SetMethod("getFd", &Archive::GetFD) | 
					
						
							|  |  |  |           .SetMethod("destroy", &Archive::Destroy) | 
					
						
							|  |  |  |           .Build()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return mate::ObjectTemplateBuilder( | 
					
						
							|  |  |  |         isolate, v8::Local<v8::ObjectTemplate>::New(isolate, template_)); | 
					
						
							| 
									
										
										
										
											2014-09-24 11:10:07 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  private: | 
					
						
							| 
									
										
										
										
											2014-09-25 20:48:32 +08:00
										 |  |  |   scoped_ptr<asar::Archive> archive_; | 
					
						
							| 
									
										
										
										
											2014-09-24 11:10:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   DISALLOW_COPY_AND_ASSIGN(Archive); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-04 10:45:17 -08:00
										 |  |  | void InitAsarSupport(v8::Isolate* isolate, | 
					
						
							| 
									
										
										
										
											2015-05-22 19:11:22 +08:00
										 |  |  |                      v8::Local<v8::Value> process, | 
					
						
							|  |  |  |                      v8::Local<v8::Value> require) { | 
					
						
							| 
									
										
										
										
											2015-02-04 10:45:17 -08:00
										 |  |  |   // Evaluate asar_init.coffee.
 | 
					
						
							| 
									
										
										
										
											2016-03-27 14:54:26 +09:00
										 |  |  |   const char* asar_init_native = reinterpret_cast<const char*>( | 
					
						
							|  |  |  |       static_cast<const unsigned char*>(node::asar_init_native)); | 
					
						
							| 
									
										
										
										
											2015-02-04 10:45:17 -08:00
										 |  |  |   v8::Local<v8::Script> asar_init = v8::Script::Compile(v8::String::NewFromUtf8( | 
					
						
							|  |  |  |       isolate, | 
					
						
							| 
									
										
										
										
											2016-03-27 14:54:26 +09:00
										 |  |  |       asar_init_native, | 
					
						
							| 
									
										
										
										
											2015-02-04 10:45:17 -08:00
										 |  |  |       v8::String::kNormalString, | 
					
						
							|  |  |  |       sizeof(node::asar_init_native) -1)); | 
					
						
							|  |  |  |   v8::Local<v8::Value> result = asar_init->Run(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Initialize asar support.
 | 
					
						
							| 
									
										
										
										
											2015-05-22 19:11:22 +08:00
										 |  |  |   base::Callback<void(v8::Local<v8::Value>, | 
					
						
							|  |  |  |                       v8::Local<v8::Value>, | 
					
						
							| 
									
										
										
										
											2015-02-04 10:45:17 -08:00
										 |  |  |                       std::string)> init; | 
					
						
							|  |  |  |   if (mate::ConvertFromV8(isolate, result, &init)) { | 
					
						
							| 
									
										
										
										
											2016-03-27 14:54:26 +09:00
										 |  |  |     const char* asar_native = reinterpret_cast<const char*>( | 
					
						
							|  |  |  |         static_cast<const unsigned char*>(node::asar_native)); | 
					
						
							| 
									
										
										
										
											2015-02-04 10:45:17 -08:00
										 |  |  |     init.Run(process, | 
					
						
							|  |  |  |              require, | 
					
						
							| 
									
										
										
										
											2016-03-27 14:54:26 +09:00
										 |  |  |              std::string(asar_native, sizeof(node::asar_native) - 1)); | 
					
						
							| 
									
										
										
										
											2015-02-04 10:45:17 -08:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-22 19:11:22 +08:00
										 |  |  | void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused, | 
					
						
							|  |  |  |                 v8::Local<v8::Context> context, void* priv) { | 
					
						
							| 
									
										
										
										
											2014-09-24 11:10:07 +08:00
										 |  |  |   mate::Dictionary dict(context->GetIsolate(), exports); | 
					
						
							|  |  |  |   dict.SetMethod("createArchive", &Archive::Create); | 
					
						
							| 
									
										
										
										
											2015-02-04 10:45:17 -08:00
										 |  |  |   dict.SetMethod("initAsarSupport", &InitAsarSupport); | 
					
						
							| 
									
										
										
										
											2014-09-24 11:10:07 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }  // namespace
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_common_asar, Initialize) |