archive.cc 8.1 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 <utility>
  7. #include <vector>
  8. #include "atom/common/asar/scoped_temporary_file.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. #if defined(OS_WIN)
  19. #include <io.h>
  20. #endif
  21. namespace asar {
  22. namespace {
  23. #if defined(OS_WIN)
  24. const char kSeparators[] = "\\/";
  25. #else
  26. const char kSeparators[] = "/";
  27. #endif
  28. bool GetNodeFromPath(std::string path,
  29. const base::DictionaryValue* root,
  30. const base::DictionaryValue** out);
  31. // Gets the "files" from "dir".
  32. bool GetFilesNode(const base::DictionaryValue* root,
  33. const base::DictionaryValue* dir,
  34. const base::DictionaryValue** out) {
  35. // Test for symbol linked directory.
  36. std::string link;
  37. if (dir->GetStringWithoutPathExpansion("link", &link)) {
  38. const base::DictionaryValue* linked_node = nullptr;
  39. if (!GetNodeFromPath(link, root, &linked_node))
  40. return false;
  41. dir = linked_node;
  42. }
  43. return dir->GetDictionaryWithoutPathExpansion("files", out);
  44. }
  45. // Gets sub-file "name" from "dir".
  46. bool GetChildNode(const base::DictionaryValue* root,
  47. const std::string& name,
  48. const base::DictionaryValue* dir,
  49. const base::DictionaryValue** out) {
  50. if (name == "") {
  51. *out = root;
  52. return true;
  53. }
  54. const base::DictionaryValue* files = nullptr;
  55. return GetFilesNode(root, dir, &files) &&
  56. files->GetDictionaryWithoutPathExpansion(name, out);
  57. }
  58. // Gets the node of "path" from "root".
  59. bool GetNodeFromPath(std::string path,
  60. const base::DictionaryValue* root,
  61. const base::DictionaryValue** out) {
  62. if (path == "") {
  63. *out = root;
  64. return true;
  65. }
  66. const base::DictionaryValue* dir = root;
  67. for (size_t delimiter_position = path.find_first_of(kSeparators);
  68. delimiter_position != std::string::npos;
  69. delimiter_position = path.find_first_of(kSeparators)) {
  70. const base::DictionaryValue* child = nullptr;
  71. if (!GetChildNode(root, path.substr(0, delimiter_position), dir, &child))
  72. return false;
  73. dir = child;
  74. path.erase(0, delimiter_position + 1);
  75. }
  76. return GetChildNode(root, path, dir, out);
  77. }
  78. bool FillFileInfoWithNode(Archive::FileInfo* info,
  79. uint32_t header_size,
  80. const base::DictionaryValue* node) {
  81. int size;
  82. if (!node->GetInteger("size", &size))
  83. return false;
  84. info->size = static_cast<uint32_t>(size);
  85. if (node->GetBoolean("unpacked", &info->unpacked) && info->unpacked)
  86. return true;
  87. std::string offset;
  88. if (!node->GetString("offset", &offset))
  89. return false;
  90. if (!base::StringToUint64(offset, &info->offset))
  91. return false;
  92. info->offset += header_size;
  93. node->GetBoolean("executable", &info->executable);
  94. return true;
  95. }
  96. } // namespace
  97. Archive::Archive(const base::FilePath& path)
  98. : path_(path), file_(base::File::FILE_OK) {
  99. base::ThreadRestrictions::ScopedAllowIO allow_io;
  100. file_.Initialize(path_, base::File::FLAG_OPEN | base::File::FLAG_READ);
  101. #if defined(OS_WIN)
  102. fd_ = _open_osfhandle(reinterpret_cast<intptr_t>(file_.GetPlatformFile()), 0);
  103. #elif defined(OS_POSIX)
  104. fd_ = file_.GetPlatformFile();
  105. #endif
  106. }
  107. Archive::~Archive() {
  108. #if defined(OS_WIN)
  109. if (fd_ != -1) {
  110. _close(fd_);
  111. // Don't close the handle since we already closed the fd.
  112. file_.TakePlatformFile();
  113. }
  114. #endif
  115. base::ThreadRestrictions::ScopedAllowIO allow_io;
  116. file_.Close();
  117. }
  118. bool Archive::Init() {
  119. if (!file_.IsValid()) {
  120. if (file_.error_details() != base::File::FILE_ERROR_NOT_FOUND) {
  121. LOG(WARNING) << "Opening " << path_.value() << ": "
  122. << base::File::ErrorToString(file_.error_details());
  123. }
  124. return false;
  125. }
  126. std::vector<char> buf;
  127. int len;
  128. buf.resize(8);
  129. {
  130. base::ThreadRestrictions::ScopedAllowIO allow_io;
  131. len = file_.ReadAtCurrentPos(buf.data(), buf.size());
  132. }
  133. if (len != static_cast<int>(buf.size())) {
  134. PLOG(ERROR) << "Failed to read header size from " << path_.value();
  135. return false;
  136. }
  137. uint32_t size;
  138. if (!base::PickleIterator(base::Pickle(buf.data(), buf.size()))
  139. .ReadUInt32(&size)) {
  140. LOG(ERROR) << "Failed to parse header size from " << path_.value();
  141. return false;
  142. }
  143. buf.resize(size);
  144. {
  145. base::ThreadRestrictions::ScopedAllowIO allow_io;
  146. len = file_.ReadAtCurrentPos(buf.data(), buf.size());
  147. }
  148. if (len != static_cast<int>(buf.size())) {
  149. PLOG(ERROR) << "Failed to read header from " << path_.value();
  150. return false;
  151. }
  152. std::string header;
  153. if (!base::PickleIterator(base::Pickle(buf.data(), buf.size()))
  154. .ReadString(&header)) {
  155. LOG(ERROR) << "Failed to parse header from " << path_.value();
  156. return false;
  157. }
  158. base::Optional<base::Value> value = base::JSONReader::Read(header);
  159. if (!value || !value->is_dict()) {
  160. LOG(ERROR) << "Failed to parse header";
  161. return false;
  162. }
  163. header_size_ = 8 + size;
  164. header_ = base::DictionaryValue::From(
  165. std::make_unique<base::Value>(value->Clone()));
  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->FindKey("link")) {
  186. stats->is_file = false;
  187. stats->is_link = true;
  188. return true;
  189. }
  190. if (node->FindKey("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