obfuscated_file_util_memory_delegate.cc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "storage/browser/file_system/obfuscated_file_util_memory_delegate.h"
  5. #include <algorithm>
  6. #include <utility>
  7. #include "base/allocator/partition_allocator/partition_alloc_constants.h"
  8. #include "base/files/file_util.h"
  9. #include "base/numerics/checked_math.h"
  10. #include "base/numerics/safe_conversions.h"
  11. #include "base/system/sys_info.h"
  12. #include "build/build_config.h"
  13. #include "net/base/io_buffer.h"
  14. #include "net/base/net_errors.h"
  15. namespace {
  16. // We are giving a relatively large quota to in-memory filesystem (see
  17. // quota_settings.cc), but we do not allocate this memory beforehand for the
  18. // filesystem. Therefore, specially on low-end devices, a website can get to a
  19. // state where it has not used all its quota, but there is no more memory
  20. // available and memory allocation fails and results in Chrome crash.
  21. // By checking for availability of memory before allocating it, we reduce the
  22. // crash possibility.
  23. // Note that quota assignment is the same for on-disk filesystem and the
  24. // assigned quota is not guaranteed to be allocatable later.
  25. bool IsMemoryAvailable(size_t required_memory) {
  26. #if BUILDFLAG(IS_FUCHSIA)
  27. // This function is not implemented on FUCHSIA, yet. (crbug.com/986608)
  28. return true;
  29. #else
  30. uint64_t max_allocatable =
  31. std::min(base::SysInfo::AmountOfAvailablePhysicalMemory(),
  32. static_cast<uint64_t>(partition_alloc::MaxDirectMapped()));
  33. return max_allocatable >= required_memory;
  34. #endif
  35. }
  36. } // namespace
  37. namespace storage {
  38. // Struct keeping one entry of the directory tree.
  39. struct ObfuscatedFileUtilMemoryDelegate::Entry {
  40. enum Type { kDirectory, kFile };
  41. Entry(Type type) : type(type) {
  42. creation_time = base::Time::Now();
  43. last_modified = creation_time;
  44. last_accessed = last_modified;
  45. }
  46. Entry(const Entry&) = delete;
  47. Entry& operator=(const Entry&) = delete;
  48. Entry(Entry&&) = default;
  49. ~Entry() = default;
  50. Type type;
  51. base::Time creation_time;
  52. base::Time last_modified;
  53. base::Time last_accessed;
  54. std::map<base::FilePath::StringType, Entry> directory_content;
  55. std::vector<uint8_t> file_content;
  56. };
  57. // Keeps a decomposed FilePath.
  58. struct ObfuscatedFileUtilMemoryDelegate::DecomposedPath {
  59. // Entry in the directory structure that the input |path| referes to,
  60. // nullptr if the entry does not exist.
  61. Entry* entry = nullptr;
  62. // Parent of the |path| in the directory structure, nullptr if not exists.
  63. Entry* parent = nullptr;
  64. // Normalized components of the path after the |root_|. E.g., if the root
  65. // is 'foo/' and the path is 'foo/./bar/baz', it will be ['bar', 'baz'].
  66. std::vector<base::FilePath::StringType> components;
  67. };
  68. ObfuscatedFileUtilMemoryDelegate::ObfuscatedFileUtilMemoryDelegate(
  69. const base::FilePath& file_system_directory)
  70. : root_(std::make_unique<Entry>(Entry::kDirectory)),
  71. root_path_components_(file_system_directory.GetComponents()) {
  72. DETACH_FROM_SEQUENCE(sequence_checker_);
  73. }
  74. ObfuscatedFileUtilMemoryDelegate::~ObfuscatedFileUtilMemoryDelegate() {
  75. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  76. }
  77. absl::optional<ObfuscatedFileUtilMemoryDelegate::DecomposedPath>
  78. ObfuscatedFileUtilMemoryDelegate::ParsePath(const base::FilePath& path) {
  79. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  80. DecomposedPath dp;
  81. dp.components = path.GetComponents();
  82. // Ensure |path| is under |root_|.
  83. if (dp.components.size() < root_path_components_.size())
  84. return absl::nullopt;
  85. for (size_t i = 0; i < root_path_components_.size(); i++)
  86. if (dp.components[i] != root_path_components_[i])
  87. return absl::nullopt;
  88. dp.components.erase(dp.components.begin(),
  89. dp.components.begin() + root_path_components_.size());
  90. // Normalize path.
  91. for (size_t i = 0; i < dp.components.size(); i++) {
  92. if (dp.components[i] == base::FilePath::kCurrentDirectory) {
  93. dp.components.erase(dp.components.begin() + i);
  94. i--;
  95. } else if (dp.components[i] == base::FilePath::kParentDirectory) {
  96. // Beyond |root|?
  97. if (!i)
  98. return absl::nullopt;
  99. dp.components.erase(dp.components.begin() + i - 1,
  100. dp.components.begin() + i + 1);
  101. i -= 2;
  102. }
  103. }
  104. // Find entry and parent.
  105. dp.parent = nullptr;
  106. dp.entry = root_.get();
  107. for (size_t i = 0; i < dp.components.size(); i++) {
  108. auto child = dp.entry->directory_content.find(dp.components[i]);
  109. if (child == dp.entry->directory_content.end()) {
  110. // If just the last component is not found and the last found part is a
  111. // directory keep the parent.
  112. if (i == dp.components.size() - 1 && dp.entry->type == Entry::kDirectory)
  113. dp.parent = dp.entry;
  114. else
  115. dp.parent = nullptr;
  116. dp.entry = nullptr;
  117. break;
  118. }
  119. dp.parent = dp.entry;
  120. dp.entry = &child->second;
  121. }
  122. return dp;
  123. }
  124. bool ObfuscatedFileUtilMemoryDelegate::DirectoryExists(
  125. const base::FilePath& path) {
  126. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  127. absl::optional<DecomposedPath> dp = ParsePath(path);
  128. return dp && dp->entry && dp->entry->type == Entry::kDirectory;
  129. }
  130. base::File::Error ObfuscatedFileUtilMemoryDelegate::CreateDirectory(
  131. const base::FilePath& path,
  132. bool exclusive,
  133. bool recursive) {
  134. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  135. absl::optional<DecomposedPath> dp = ParsePath(path);
  136. if (!dp)
  137. return base::File::FILE_ERROR_NOT_FOUND;
  138. // If path already exists, ensure it's not a file and exclusive access is not
  139. // requested.
  140. if (dp->entry) {
  141. if (exclusive || dp->entry->type == Entry::kFile)
  142. return base::File::FILE_ERROR_EXISTS;
  143. return base::File::FILE_OK;
  144. }
  145. // If parent exists, add the new directory.
  146. if (dp->parent) {
  147. dp->parent->directory_content.emplace(dp->components.back(),
  148. Entry::kDirectory);
  149. return base::File::FILE_OK;
  150. }
  151. // Recursively add all required ancesstors if allowed.
  152. if (!recursive)
  153. return base::File::FILE_ERROR_NOT_FOUND;
  154. Entry* entry = root_.get();
  155. bool check_existings = true;
  156. for (const auto& c : dp->components) {
  157. if (check_existings) {
  158. auto child = entry->directory_content.find(c);
  159. if (child != entry->directory_content.end()) {
  160. entry = &child->second;
  161. continue;
  162. }
  163. check_existings = false;
  164. }
  165. entry =
  166. &entry->directory_content.emplace(c, Entry::kDirectory).first->second;
  167. }
  168. return base::File::FILE_OK;
  169. }
  170. bool ObfuscatedFileUtilMemoryDelegate::DeleteFileOrDirectory(
  171. const base::FilePath& path,
  172. bool recursive) {
  173. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  174. absl::optional<DecomposedPath> dp = ParsePath(path);
  175. if (!dp)
  176. return false;
  177. if (!dp->entry)
  178. return true;
  179. if (!recursive && dp->entry->directory_content.size())
  180. return false;
  181. dp->parent->directory_content.erase(dp->components.back());
  182. return true;
  183. }
  184. bool ObfuscatedFileUtilMemoryDelegate::IsLink(const base::FilePath& file_path) {
  185. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  186. // In-memory file system does not support links.
  187. return false;
  188. }
  189. bool ObfuscatedFileUtilMemoryDelegate::PathExists(const base::FilePath& path) {
  190. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  191. absl::optional<DecomposedPath> dp = ParsePath(path);
  192. return dp && dp->entry;
  193. }
  194. base::File ObfuscatedFileUtilMemoryDelegate::CreateOrOpen(
  195. const base::FilePath& path,
  196. uint32_t file_flags) {
  197. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  198. // TODO:(https://crbug.com/936722): Once the output of this function is
  199. // changed to base::File::Error, it can use CreateOrOpenInternal to perform
  200. // the task and return the result.
  201. return base::File(base::File::FILE_ERROR_INVALID_OPERATION);
  202. }
  203. void ObfuscatedFileUtilMemoryDelegate::CreateOrOpenInternal(
  204. const DecomposedPath& dp,
  205. uint32_t file_flags) {
  206. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  207. if (!dp.entry) {
  208. dp.parent->directory_content.emplace(dp.components.back(), Entry::kFile);
  209. return;
  210. }
  211. if (dp.entry->file_content.size()) {
  212. if (file_flags &
  213. (base::File::FLAG_OPEN_TRUNCATED | base::File::FLAG_CREATE_ALWAYS)) {
  214. dp.entry->file_content.clear();
  215. }
  216. }
  217. }
  218. base::File::Error ObfuscatedFileUtilMemoryDelegate::DeleteFile(
  219. const base::FilePath& path) {
  220. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  221. absl::optional<DecomposedPath> dp = ParsePath(path);
  222. if (!dp || !dp->entry)
  223. return base::File::FILE_ERROR_NOT_FOUND;
  224. if (dp->entry->type != Entry::kFile)
  225. return base::File::FILE_ERROR_NOT_A_FILE;
  226. dp->parent->directory_content.erase(dp->components.back());
  227. return base::File::FILE_OK;
  228. }
  229. base::File::Error ObfuscatedFileUtilMemoryDelegate::EnsureFileExists(
  230. const base::FilePath& path,
  231. bool* created) {
  232. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  233. absl::optional<DecomposedPath> dp = ParsePath(path);
  234. *created = false;
  235. if (!dp || !dp->parent)
  236. return base::File::FILE_ERROR_NOT_FOUND;
  237. if (dp->entry) {
  238. return dp->entry->type == Entry::kFile ? base::File::FILE_OK
  239. : base::File::FILE_ERROR_NOT_FOUND;
  240. }
  241. CreateOrOpenInternal(*dp, base::File::FLAG_CREATE);
  242. *created = true;
  243. return base::File::FILE_OK;
  244. }
  245. base::File::Error ObfuscatedFileUtilMemoryDelegate::GetFileInfo(
  246. const base::FilePath& path,
  247. base::File::Info* file_info) {
  248. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  249. absl::optional<DecomposedPath> dp = ParsePath(path);
  250. if (!dp || !dp->entry)
  251. return base::File::FILE_ERROR_NOT_FOUND;
  252. file_info->size = dp->entry->file_content.size();
  253. file_info->is_directory = (dp->entry->type == Entry::kDirectory);
  254. file_info->is_symbolic_link = false;
  255. file_info->creation_time = dp->entry->creation_time;
  256. file_info->last_modified = dp->entry->last_modified;
  257. file_info->last_accessed = dp->entry->last_accessed;
  258. DCHECK(file_info->size == 0 || !file_info->is_directory);
  259. return base::File::FILE_OK;
  260. }
  261. base::File::Error ObfuscatedFileUtilMemoryDelegate::Touch(
  262. const base::FilePath& path,
  263. const base::Time& last_access_time,
  264. const base::Time& last_modified_time) {
  265. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  266. absl::optional<DecomposedPath> dp = ParsePath(path);
  267. if (!dp || !dp->entry)
  268. return base::File::FILE_ERROR_FAILED;
  269. dp->entry->last_accessed = last_access_time;
  270. dp->entry->last_modified = last_modified_time;
  271. return base::File::FILE_OK;
  272. }
  273. base::File::Error ObfuscatedFileUtilMemoryDelegate::Truncate(
  274. const base::FilePath& path,
  275. int64_t length) {
  276. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  277. absl::optional<DecomposedPath> dp = ParsePath(path);
  278. if (!dp || !dp->entry || dp->entry->type != Entry::kFile)
  279. return base::File::FILE_ERROR_NOT_FOUND;
  280. // Fail if enough memory is not available.
  281. if (!base::IsValueInRangeForNumericType<size_t>(length) ||
  282. (static_cast<size_t>(length) > dp->entry->file_content.capacity() &&
  283. !IsMemoryAvailable(static_cast<size_t>(length)))) {
  284. return base::File::FILE_ERROR_NO_SPACE;
  285. }
  286. dp->entry->file_content.resize(static_cast<size_t>(length));
  287. return base::File::FILE_OK;
  288. }
  289. NativeFileUtil::CopyOrMoveMode
  290. ObfuscatedFileUtilMemoryDelegate::CopyOrMoveModeForDestination(
  291. const FileSystemURL& /*dest_url*/,
  292. bool copy) {
  293. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  294. return copy ? NativeFileUtil::CopyOrMoveMode::COPY_SYNC
  295. : NativeFileUtil::CopyOrMoveMode::MOVE;
  296. }
  297. base::File::Error ObfuscatedFileUtilMemoryDelegate::CopyOrMoveFile(
  298. const base::FilePath& src_path,
  299. const base::FilePath& dest_path,
  300. FileSystemOperation::CopyOrMoveOptionSet options,
  301. NativeFileUtil::CopyOrMoveMode mode) {
  302. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  303. absl::optional<DecomposedPath> src_dp = ParsePath(src_path);
  304. absl::optional<DecomposedPath> dest_dp = ParsePath(dest_path);
  305. if (!src_dp || !src_dp->entry || !dest_dp || !dest_dp->parent)
  306. return base::File::FILE_ERROR_NOT_FOUND;
  307. bool src_is_directory = src_dp->entry->type == Entry::kDirectory;
  308. // For directories, only 'move' is supported.
  309. if (src_is_directory && mode != NativeFileUtil::CopyOrMoveMode::MOVE) {
  310. return base::File::FILE_ERROR_NOT_A_FILE;
  311. }
  312. base::Time last_modified = src_dp->entry->last_modified;
  313. if (dest_dp->entry) {
  314. if (dest_dp->entry->type != src_dp->entry->type)
  315. return base::File::FILE_ERROR_INVALID_OPERATION;
  316. #if BUILDFLAG(IS_WIN)
  317. // Overwriting an empty directory with another directory isn't
  318. // supported natively on Windows.
  319. // To keep the behavior indistinguishable from on-disk operation,
  320. // in-memory implementation also fails.
  321. if (src_is_directory)
  322. return base::File::FILE_ERROR_NOT_A_FILE;
  323. #endif
  324. }
  325. switch (mode) {
  326. case NativeFileUtil::CopyOrMoveMode::COPY_NOSYNC:
  327. case NativeFileUtil::CopyOrMoveMode::COPY_SYNC:
  328. DCHECK(!src_is_directory);
  329. // Fail if enough memory is not available.
  330. if (!IsMemoryAvailable(src_dp->entry->file_content.size()))
  331. return base::File::FILE_ERROR_NO_SPACE;
  332. if (!CopyOrMoveFileInternal(*src_dp, *dest_dp, false))
  333. return base::File::FILE_ERROR_FAILED;
  334. break;
  335. case NativeFileUtil::CopyOrMoveMode::MOVE:
  336. if (src_is_directory) {
  337. if (!MoveDirectoryInternal(*src_dp, *dest_dp))
  338. return base::File::FILE_ERROR_FAILED;
  339. } else {
  340. if (!CopyOrMoveFileInternal(*src_dp, *dest_dp, true))
  341. return base::File::FILE_ERROR_FAILED;
  342. }
  343. break;
  344. }
  345. if (options.Has(
  346. FileSystemOperation::CopyOrMoveOption::kPreserveLastModified)) {
  347. Touch(dest_path, last_modified, last_modified);
  348. }
  349. // Don't bother with the kPreserveDestinationPermissions option, since
  350. // this is not relevant to in-memory files.
  351. return base::File::FILE_OK;
  352. }
  353. bool ObfuscatedFileUtilMemoryDelegate::MoveDirectoryInternal(
  354. const DecomposedPath& src_dp,
  355. const DecomposedPath& dest_dp) {
  356. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  357. DCHECK(src_dp.entry->type == Entry::kDirectory);
  358. if (!dest_dp.entry) {
  359. dest_dp.parent->directory_content.insert(
  360. std::make_pair(dest_dp.components.back(), std::move(*src_dp.entry)));
  361. } else {
  362. dest_dp.entry->directory_content.insert(
  363. std::make_move_iterator(src_dp.entry->directory_content.begin()),
  364. std::make_move_iterator(src_dp.entry->directory_content.end()));
  365. }
  366. src_dp.parent->directory_content.erase(src_dp.components.back());
  367. return true;
  368. }
  369. bool ObfuscatedFileUtilMemoryDelegate::CopyOrMoveFileInternal(
  370. const DecomposedPath& src_dp,
  371. const DecomposedPath& dest_dp,
  372. bool move) {
  373. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  374. DCHECK(src_dp.entry->type == Entry::kFile);
  375. if (dest_dp.entry)
  376. dest_dp.parent->directory_content.erase(dest_dp.components.back());
  377. if (move) {
  378. dest_dp.parent->directory_content.insert(
  379. std::make_pair(dest_dp.components.back(), std::move(*src_dp.entry)));
  380. src_dp.parent->directory_content.erase(src_dp.components.back());
  381. return true;
  382. }
  383. // Copy the file.
  384. Entry* entry = &dest_dp.parent->directory_content
  385. .emplace(dest_dp.components.back(), Entry::kFile)
  386. .first->second;
  387. entry->creation_time = src_dp.entry->creation_time;
  388. entry->last_modified = src_dp.entry->last_modified;
  389. entry->last_accessed = src_dp.entry->last_accessed;
  390. entry->file_content = src_dp.entry->file_content;
  391. return true;
  392. }
  393. size_t ObfuscatedFileUtilMemoryDelegate::ComputeDirectorySize(
  394. const base::FilePath& path) {
  395. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  396. absl::optional<DecomposedPath> dp = ParsePath(path);
  397. if (!dp || !dp->entry || dp->entry->type != Entry::kDirectory)
  398. return 0;
  399. base::CheckedNumeric<size_t> running_sum = 0;
  400. std::vector<Entry*> directories;
  401. directories.push_back(dp->entry);
  402. while (!directories.empty()) {
  403. Entry* current = directories.back();
  404. directories.pop_back();
  405. for (auto& child : current->directory_content) {
  406. if (child.second.type == Entry::kDirectory)
  407. directories.push_back(&child.second);
  408. else
  409. running_sum += child.second.file_content.size();
  410. }
  411. }
  412. return running_sum.ValueOrDefault(0);
  413. }
  414. int ObfuscatedFileUtilMemoryDelegate::ReadFile(const base::FilePath& path,
  415. int64_t offset,
  416. scoped_refptr<net::IOBuffer> buf,
  417. int buf_len) {
  418. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  419. absl::optional<DecomposedPath> dp = ParsePath(path);
  420. if (!dp || dp->entry->type != Entry::kFile)
  421. return net::ERR_FILE_NOT_FOUND;
  422. int64_t remaining = dp->entry->file_content.size() - offset;
  423. if (offset < 0)
  424. return net::ERR_INVALID_ARGUMENT;
  425. // Seeking past the end of the file is ok, but returns nothing.
  426. // This matches FileStream::Context behavior.
  427. if (remaining < 0)
  428. return 0;
  429. if (buf_len > remaining)
  430. buf_len = static_cast<int>(remaining);
  431. memcpy(buf->data(), dp->entry->file_content.data() + offset, buf_len);
  432. return buf_len;
  433. }
  434. int ObfuscatedFileUtilMemoryDelegate::WriteFile(
  435. const base::FilePath& path,
  436. int64_t offset,
  437. scoped_refptr<net::IOBuffer> buf,
  438. int buf_len) {
  439. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  440. absl::optional<DecomposedPath> dp = ParsePath(path);
  441. if (!dp || !dp->entry || dp->entry->type != Entry::kFile)
  442. return net::ERR_FILE_NOT_FOUND;
  443. size_t offset_u = static_cast<size_t>(offset);
  444. // Fail if |offset| or |buf_len| not valid.
  445. if (offset < 0 || buf_len < 0)
  446. return net::ERR_REQUEST_RANGE_NOT_SATISFIABLE;
  447. // Fail if result doesn't fit in a std::vector.
  448. if (std::numeric_limits<size_t>::max() - offset_u <
  449. static_cast<size_t>(buf_len))
  450. return net::ERR_REQUEST_RANGE_NOT_SATISFIABLE;
  451. const size_t last_position = offset_u + buf_len;
  452. if (last_position > dp->entry->file_content.capacity()) {
  453. // Fail if enough memory is not available.
  454. if (!IsMemoryAvailable(last_position))
  455. return net::ERR_FILE_NO_SPACE;
  456. // If required memory is bigger than half of the max allocatable memory block,
  457. // reserve first to avoid STL getting more than required memory.
  458. // See crbug.com/1043914 for more context.
  459. // |MaxDirectMapped| function is not implemented on FUCHSIA, yet.
  460. // (crbug.com/986608)
  461. #if !BUILDFLAG(IS_FUCHSIA)
  462. if (last_position >= partition_alloc::MaxDirectMapped() / 2) {
  463. // TODO(https://crbug.com/1043914): Allocated memory is rounded up to
  464. // 100MB blocks to reduce memory allocation delays. Switch to a more
  465. // proper container to remove this dependency.
  466. const size_t round_up_size = 100 * 1024 * 1024;
  467. size_t rounded_up = ((last_position / round_up_size) + 1) * round_up_size;
  468. if (!IsMemoryAvailable(rounded_up))
  469. return net::ERR_FILE_NO_SPACE;
  470. dp->entry->file_content.reserve(rounded_up);
  471. }
  472. #endif
  473. }
  474. if (offset_u == dp->entry->file_content.size()) {
  475. dp->entry->file_content.insert(dp->entry->file_content.end(), buf->data(),
  476. buf->data() + buf_len);
  477. } else {
  478. if (last_position > dp->entry->file_content.size())
  479. dp->entry->file_content.resize(last_position);
  480. // if |offset_u| is larger than the original file size, there will be null
  481. // bytes between the end of the file and |offset_u|.
  482. memcpy(dp->entry->file_content.data() + offset, buf->data(), buf_len);
  483. }
  484. return buf_len;
  485. }
  486. base::File::Error ObfuscatedFileUtilMemoryDelegate::CreateFileForTesting(
  487. const base::FilePath& path,
  488. base::span<const char> content) {
  489. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  490. bool created;
  491. base::File::Error result = EnsureFileExists(path, &created);
  492. if (result != base::File::FILE_OK)
  493. return result;
  494. absl::optional<DecomposedPath> dp = ParsePath(path);
  495. DCHECK(dp && dp->entry->type == Entry::kFile);
  496. dp->entry->file_content =
  497. std::vector<uint8_t>(content.begin(), content.end());
  498. return base::File::FILE_OK;
  499. }
  500. base::File::Error ObfuscatedFileUtilMemoryDelegate::CopyInForeignFile(
  501. const base::FilePath& src_path,
  502. const base::FilePath& dest_path,
  503. FileSystemOperation::CopyOrMoveOptionSet /* options */,
  504. NativeFileUtil::CopyOrMoveMode /* mode */) {
  505. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  506. absl::optional<DecomposedPath> dest_dp = ParsePath(dest_path);
  507. if (!dest_dp || !dest_dp->parent)
  508. return base::File::FILE_ERROR_NOT_FOUND;
  509. base::File::Info source_info;
  510. if (!base::GetFileInfo(src_path, &source_info))
  511. return base::File::FILE_ERROR_NOT_FOUND;
  512. if (source_info.is_directory)
  513. return base::File::FILE_ERROR_NOT_A_FILE;
  514. // |size_t| limits the maximum size that the memory file can keep and |int|
  515. // limits the maximum size that base::ReadFile function reads.
  516. if (source_info.size > std::numeric_limits<size_t>::max() ||
  517. source_info.size > std::numeric_limits<int>::max()) {
  518. return base::File::FILE_ERROR_NO_SPACE;
  519. }
  520. // Fail if enough memory is not available.
  521. if (!IsMemoryAvailable(static_cast<size_t>(source_info.size)))
  522. return base::File::FILE_ERROR_NO_SPACE;
  523. // Create file.
  524. Entry* entry = &dest_dp->parent->directory_content
  525. .emplace(dest_dp->components.back(), Entry::kFile)
  526. .first->second;
  527. entry->creation_time = source_info.creation_time;
  528. entry->last_modified = source_info.last_modified;
  529. entry->last_accessed = source_info.last_accessed;
  530. // Read content.
  531. entry->file_content.resize(source_info.size);
  532. int read_bytes = base::ReadFile(
  533. src_path, reinterpret_cast<char*>(entry->file_content.data()),
  534. source_info.size);
  535. if (read_bytes != source_info.size) {
  536. // Delete file and return error if source could not be fully read or any
  537. // error happens.
  538. dest_dp->parent->directory_content.erase(dest_dp->components.back());
  539. return base::File::FILE_ERROR_FAILED;
  540. }
  541. return base::File::FILE_OK;
  542. }
  543. } // namespace storage