archive.cc 8.4 KB

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