asar_util.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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& archive_map = *g_archive_map_tls.Pointer()->Get();
  35. if (!base::Contains(archive_map, path)) {
  36. std::shared_ptr<Archive> archive(new Archive(path));
  37. if (!archive->Init())
  38. return nullptr;
  39. archive_map[path] = archive;
  40. }
  41. return archive_map[path];
  42. }
  43. void ClearArchives() {
  44. if (g_archive_map_tls.Pointer()->Get())
  45. delete g_archive_map_tls.Pointer()->Get();
  46. }
  47. bool GetAsarArchivePath(const base::FilePath& full_path,
  48. base::FilePath* asar_path,
  49. base::FilePath* relative_path,
  50. bool allow_root) {
  51. base::FilePath iter = full_path;
  52. while (true) {
  53. base::FilePath dirname = iter.DirName();
  54. if (iter.MatchesExtension(kAsarExtension) && !IsDirectoryCached(iter))
  55. break;
  56. else if (iter == dirname)
  57. return false;
  58. iter = dirname;
  59. }
  60. base::FilePath tail;
  61. if (!((allow_root && iter == full_path) ||
  62. iter.AppendRelativePath(full_path, &tail)))
  63. return false;
  64. *asar_path = iter;
  65. *relative_path = tail;
  66. return true;
  67. }
  68. bool ReadFileToString(const base::FilePath& path, std::string* contents) {
  69. base::FilePath asar_path, relative_path;
  70. if (!GetAsarArchivePath(path, &asar_path, &relative_path))
  71. return base::ReadFileToString(path, contents);
  72. std::shared_ptr<Archive> archive = GetOrCreateAsarArchive(asar_path);
  73. if (!archive)
  74. return false;
  75. Archive::FileInfo info;
  76. if (!archive->GetFileInfo(relative_path, &info))
  77. return false;
  78. if (info.unpacked) {
  79. base::FilePath real_path;
  80. // For unpacked file it will return the real path instead of doing the copy.
  81. archive->CopyFileOut(relative_path, &real_path);
  82. return base::ReadFileToString(real_path, contents);
  83. }
  84. base::File src(asar_path, base::File::FLAG_OPEN | base::File::FLAG_READ);
  85. if (!src.IsValid())
  86. return false;
  87. contents->resize(info.size);
  88. return static_cast<int>(info.size) ==
  89. src.Read(info.offset, const_cast<char*>(contents->data()),
  90. contents->size());
  91. }
  92. } // namespace asar