archive.cc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 "atom/common/asar/archive.h"
  5. #include <string>
  6. #include <vector>
  7. #include "atom/common/asar/scoped_temporary_file.h"
  8. #include "base/files/file.h"
  9. #include "base/files/file_util.h"
  10. #include "base/json/json_reader.h"
  11. #include "base/logging.h"
  12. #include "base/pickle.h"
  13. #include "base/strings/string_number_conversions.h"
  14. #include "base/task_scheduler/post_task.h"
  15. #include "base/threading/thread_restrictions.h"
  16. #include "base/values.h"
  17. #if defined(OS_WIN)
  18. #include <io.h>
  19. #endif
  20. namespace asar {
  21. namespace {
  22. #if defined(OS_WIN)
  23. const char kSeparators[] = "\\/";
  24. #else
  25. const char kSeparators[] = "/";
  26. #endif
  27. bool GetNodeFromPath(std::string path,
  28. const base::DictionaryValue* root,
  29. const base::DictionaryValue** out);
  30. // Gets the "files" from "dir".
  31. bool GetFilesNode(const base::DictionaryValue* root,
  32. const base::DictionaryValue* dir,
  33. const base::DictionaryValue** out) {
  34. // Test for symbol linked directory.
  35. std::string link;
  36. if (dir->GetStringWithoutPathExpansion("link", &link)) {
  37. const base::DictionaryValue* linked_node = nullptr;
  38. if (!GetNodeFromPath(link, root, &linked_node))
  39. return false;
  40. dir = linked_node;
  41. }
  42. return dir->GetDictionaryWithoutPathExpansion("files", out);
  43. }
  44. // Gets sub-file "name" from "dir".
  45. bool GetChildNode(const base::DictionaryValue* root,
  46. const std::string& name,
  47. const base::DictionaryValue* dir,
  48. const base::DictionaryValue** out) {
  49. if (name == "") {
  50. *out = root;
  51. return true;
  52. }
  53. const base::DictionaryValue* files = nullptr;
  54. return GetFilesNode(root, dir, &files) &&
  55. files->GetDictionaryWithoutPathExpansion(name, out);
  56. }
  57. // Gets the node of "path" from "root".
  58. bool GetNodeFromPath(std::string path,
  59. const base::DictionaryValue* root,
  60. const base::DictionaryValue** out) {
  61. if (path == "") {
  62. *out = root;
  63. return true;
  64. }
  65. const base::DictionaryValue* dir = root;
  66. for (size_t delimiter_position = path.find_first_of(kSeparators);
  67. delimiter_position != std::string::npos;
  68. delimiter_position = path.find_first_of(kSeparators)) {
  69. const base::DictionaryValue* child = nullptr;
  70. if (!GetChildNode(root, path.substr(0, delimiter_position), dir, &child))
  71. return false;
  72. dir = child;
  73. path.erase(0, delimiter_position + 1);
  74. }
  75. return GetChildNode(root, path, dir, out);
  76. }
  77. bool FillFileInfoWithNode(Archive::FileInfo* info,
  78. uint32_t header_size,
  79. const base::DictionaryValue* node) {
  80. int size;
  81. if (!node->GetInteger("size", &size))
  82. return false;
  83. info->size = static_cast<uint32_t>(size);
  84. if (node->GetBoolean("unpacked", &info->unpacked) && info->unpacked)
  85. return true;
  86. std::string offset;
  87. if (!node->GetString("offset", &offset))
  88. return false;
  89. if (!base::StringToUint64(offset, &info->offset))
  90. return false;
  91. info->offset += header_size;
  92. node->GetBoolean("executable", &info->executable);
  93. return true;
  94. }
  95. } // namespace
  96. Archive::Archive(const base::FilePath& path)
  97. : path_(path), file_(base::File::FILE_OK) {
  98. base::ThreadRestrictions::ScopedAllowIO allow_io;
  99. file_.Initialize(path_, base::File::FLAG_OPEN | base::File::FLAG_READ);
  100. #if defined(OS_WIN)
  101. fd_ = _open_osfhandle(reinterpret_cast<intptr_t>(file_.GetPlatformFile()), 0);
  102. #elif defined(OS_POSIX)
  103. fd_ = file_.GetPlatformFile();
  104. #endif
  105. }
  106. Archive::~Archive() {
  107. #if defined(OS_WIN)
  108. if (fd_ != -1) {
  109. _close(fd_);
  110. // Don't close the handle since we already closed the fd.
  111. file_.TakePlatformFile();
  112. }
  113. #endif
  114. base::ThreadRestrictions::ScopedAllowIO allow_io;
  115. file_.Close();
  116. }
  117. bool Archive::Init() {
  118. if (!file_.IsValid()) {
  119. if (file_.error_details() != base::File::FILE_ERROR_NOT_FOUND) {
  120. LOG(WARNING) << "Opening " << path_.value() << ": "
  121. << base::File::ErrorToString(file_.error_details());
  122. }
  123. return false;
  124. }
  125. std::vector<char> buf;
  126. int len;
  127. buf.resize(8);
  128. {
  129. base::ThreadRestrictions::ScopedAllowIO allow_io;
  130. len = file_.ReadAtCurrentPos(buf.data(), buf.size());
  131. }
  132. if (len != static_cast<int>(buf.size())) {
  133. PLOG(ERROR) << "Failed to read header size from " << path_.value();
  134. return false;
  135. }
  136. uint32_t size;
  137. if (!base::PickleIterator(base::Pickle(buf.data(), buf.size()))
  138. .ReadUInt32(&size)) {
  139. LOG(ERROR) << "Failed to parse header size from " << path_.value();
  140. return false;
  141. }
  142. buf.resize(size);
  143. {
  144. base::ThreadRestrictions::ScopedAllowIO allow_io;
  145. len = file_.ReadAtCurrentPos(buf.data(), buf.size());
  146. }
  147. if (len != static_cast<int>(buf.size())) {
  148. PLOG(ERROR) << "Failed to read header from " << path_.value();
  149. return false;
  150. }
  151. std::string header;
  152. if (!base::PickleIterator(base::Pickle(buf.data(), buf.size()))
  153. .ReadString(&header)) {
  154. LOG(ERROR) << "Failed to parse header from " << path_.value();
  155. return false;
  156. }
  157. std::string error;
  158. base::JSONReader reader;
  159. std::unique_ptr<base::Value> value(reader.ReadToValue(header));
  160. if (!value || !value->is_dict()) {
  161. LOG(ERROR) << "Failed to parse header: " << error;
  162. return false;
  163. }
  164. header_size_ = 8 + size;
  165. header_.reset(static_cast<base::DictionaryValue*>(value.release()));
  166. return true;
  167. }
  168. bool Archive::GetFileInfo(const base::FilePath& path, FileInfo* info) {
  169. if (!header_)
  170. return false;
  171. const base::DictionaryValue* node;
  172. if (!GetNodeFromPath(path.AsUTF8Unsafe(), header_.get(), &node))
  173. return false;
  174. std::string link;
  175. if (node->GetString("link", &link))
  176. return GetFileInfo(base::FilePath::FromUTF8Unsafe(link), info);
  177. return FillFileInfoWithNode(info, header_size_, node);
  178. }
  179. bool Archive::Stat(const base::FilePath& path, Stats* stats) {
  180. if (!header_)
  181. return false;
  182. const base::DictionaryValue* node;
  183. if (!GetNodeFromPath(path.AsUTF8Unsafe(), header_.get(), &node))
  184. return false;
  185. if (node->HasKey("link")) {
  186. stats->is_file = false;
  187. stats->is_link = true;
  188. return true;
  189. }
  190. if (node->HasKey("files")) {
  191. stats->is_file = false;
  192. stats->is_directory = true;
  193. return true;
  194. }
  195. return FillFileInfoWithNode(stats, header_size_, node);
  196. }
  197. bool Archive::Readdir(const base::FilePath& path,
  198. std::vector<base::FilePath>* list) {
  199. if (!header_)
  200. return false;
  201. const base::DictionaryValue* node;
  202. if (!GetNodeFromPath(path.AsUTF8Unsafe(), header_.get(), &node))
  203. return false;
  204. const base::DictionaryValue* files;
  205. if (!GetFilesNode(header_.get(), node, &files))
  206. return false;
  207. base::DictionaryValue::Iterator iter(*files);
  208. while (!iter.IsAtEnd()) {
  209. list->push_back(base::FilePath::FromUTF8Unsafe(iter.key()));
  210. iter.Advance();
  211. }
  212. return true;
  213. }
  214. bool Archive::Realpath(const base::FilePath& path, base::FilePath* realpath) {
  215. if (!header_)
  216. return false;
  217. const base::DictionaryValue* node;
  218. if (!GetNodeFromPath(path.AsUTF8Unsafe(), header_.get(), &node))
  219. return false;
  220. std::string link;
  221. if (node->GetString("link", &link)) {
  222. *realpath = base::FilePath::FromUTF8Unsafe(link);
  223. return true;
  224. }
  225. *realpath = path;
  226. return true;
  227. }
  228. bool Archive::CopyFileOut(const base::FilePath& path, base::FilePath* out) {
  229. auto it = external_files_.find(path.value());
  230. if (it != external_files_.end()) {
  231. *out = it->second->path();
  232. return true;
  233. }
  234. FileInfo info;
  235. if (!GetFileInfo(path, &info))
  236. return false;
  237. if (info.unpacked) {
  238. *out = path_.AddExtension(FILE_PATH_LITERAL("unpacked")).Append(path);
  239. return true;
  240. }
  241. auto temp_file = std::make_unique<ScopedTemporaryFile>();
  242. base::FilePath::StringType ext = path.Extension();
  243. if (!temp_file->InitFromFile(&file_, ext, info.offset, info.size))
  244. return false;
  245. #if defined(OS_POSIX)
  246. if (info.executable) {
  247. // chmod a+x temp_file;
  248. base::SetPosixFilePermissions(temp_file->path(), 0755);
  249. }
  250. #endif
  251. *out = temp_file->path();
  252. external_files_[path.value()] = std::move(temp_file);
  253. return true;
  254. }
  255. int Archive::GetFD() const {
  256. return fd_;
  257. }
  258. } // namespace asar