asar_util.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright (c) 2015 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/asar_util.h"
  5. #include <map>
  6. #include <string>
  7. #include "base/files/file_path.h"
  8. #include "base/files/file_util.h"
  9. #include "base/lazy_instance.h"
  10. #include "base/stl_util.h"
  11. #include "base/threading/thread_local.h"
  12. #include "base/threading/thread_restrictions.h"
  13. #include "shell/common/asar/archive.h"
  14. namespace asar {
  15. namespace {
  16. // The global instance of ArchiveMap, will be destroyed on exit.
  17. typedef std::map<base::FilePath, std::shared_ptr<Archive>> ArchiveMap;
  18. base::LazyInstance<base::ThreadLocalPointer<ArchiveMap>>::Leaky
  19. g_archive_map_tls = LAZY_INSTANCE_INITIALIZER;
  20. const base::FilePath::CharType kAsarExtension[] = FILE_PATH_LITERAL(".asar");
  21. std::map<base::FilePath, bool> g_is_directory_cache;
  22. bool IsDirectoryCached(const base::FilePath& path) {
  23. auto it = g_is_directory_cache.find(path);
  24. if (it != g_is_directory_cache.end()) {
  25. return it->second;
  26. }
  27. base::ThreadRestrictions::ScopedAllowIO allow_io;
  28. return g_is_directory_cache[path] = base::DirectoryExists(path);
  29. }
  30. } // namespace
  31. std::shared_ptr<Archive> GetOrCreateAsarArchive(const base::FilePath& path) {
  32. if (!g_archive_map_tls.Pointer()->Get())
  33. g_archive_map_tls.Pointer()->Set(new ArchiveMap);
  34. ArchiveMap& map = *g_archive_map_tls.Pointer()->Get();
  35. // if we have it, return it
  36. const auto lower = map.lower_bound(path);
  37. if (lower != std::end(map) && !map.key_comp()(path, lower->first))
  38. return lower->second;
  39. // if we can create it, return it
  40. auto archive = std::make_shared<Archive>(path);
  41. if (archive->Init()) {
  42. base::TryEmplace(map, lower, path, archive);
  43. return archive;
  44. }
  45. // didn't have it, couldn't create it
  46. return nullptr;
  47. }
  48. void ClearArchives() {
  49. if (g_archive_map_tls.Pointer()->Get())
  50. delete g_archive_map_tls.Pointer()->Get();
  51. }
  52. bool GetAsarArchivePath(const base::FilePath& full_path,
  53. base::FilePath* asar_path,
  54. base::FilePath* relative_path,
  55. bool allow_root) {
  56. base::FilePath iter = full_path;
  57. while (true) {
  58. base::FilePath dirname = iter.DirName();
  59. if (iter.MatchesExtension(kAsarExtension) && !IsDirectoryCached(iter))
  60. break;
  61. else if (iter == dirname)
  62. return false;
  63. iter = dirname;
  64. }
  65. base::FilePath tail;
  66. if (!((allow_root && iter == full_path) ||
  67. iter.AppendRelativePath(full_path, &tail)))
  68. return false;
  69. *asar_path = iter;
  70. *relative_path = tail;
  71. return true;
  72. }
  73. bool ReadFileToString(const base::FilePath& path, std::string* contents) {
  74. base::FilePath asar_path, relative_path;
  75. if (!GetAsarArchivePath(path, &asar_path, &relative_path))
  76. return base::ReadFileToString(path, contents);
  77. std::shared_ptr<Archive> archive = GetOrCreateAsarArchive(asar_path);
  78. if (!archive)
  79. return false;
  80. Archive::FileInfo info;
  81. if (!archive->GetFileInfo(relative_path, &info))
  82. return false;
  83. if (info.unpacked) {
  84. base::FilePath real_path;
  85. // For unpacked file it will return the real path instead of doing the copy.
  86. archive->CopyFileOut(relative_path, &real_path);
  87. return base::ReadFileToString(real_path, contents);
  88. }
  89. base::File src(asar_path, base::File::FLAG_OPEN | base::File::FLAG_READ);
  90. if (!src.IsValid())
  91. return false;
  92. contents->resize(info.size);
  93. return static_cast<int>(info.size) ==
  94. src.Read(info.offset, const_cast<char*>(contents->data()),
  95. contents->size());
  96. }
  97. } // namespace asar