asar_util.cc 3.8 KB

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