test_file_util_posix.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright (c) 2012 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 "base/test/test_file_util.h"
  5. #include <errno.h>
  6. #include <fcntl.h>
  7. #include <stddef.h>
  8. #include <sys/types.h>
  9. #include <unistd.h>
  10. #include <string>
  11. #include "base/check_op.h"
  12. #include "base/files/file.h"
  13. #include "base/files/file_util.h"
  14. #include "base/notreached.h"
  15. #include "base/strings/string_util.h"
  16. #include "base/strings/utf_string_conversions.h"
  17. #include "build/build_config.h"
  18. namespace base {
  19. namespace {
  20. // Deny |permission| on the file |path|.
  21. bool DenyFilePermission(const FilePath& path, mode_t permission) {
  22. stat_wrapper_t stat_buf;
  23. if (File::Stat(path.value().c_str(), &stat_buf) != 0)
  24. return false;
  25. stat_buf.st_mode &= ~permission;
  26. int rv = HANDLE_EINTR(chmod(path.value().c_str(), stat_buf.st_mode));
  27. return rv == 0;
  28. }
  29. // Gets a blob indicating the permission information for |path|.
  30. // |length| is the length of the blob. Zero on failure.
  31. // Returns the blob pointer, or NULL on failure.
  32. void* GetPermissionInfo(const FilePath& path, size_t* length) {
  33. DCHECK(length);
  34. *length = 0;
  35. stat_wrapper_t stat_buf;
  36. if (File::Stat(path.value().c_str(), &stat_buf) != 0)
  37. return nullptr;
  38. *length = sizeof(mode_t);
  39. mode_t* mode = new mode_t;
  40. *mode = stat_buf.st_mode & ~S_IFMT; // Filter out file/path kind.
  41. return mode;
  42. }
  43. // Restores the permission information for |path|, given the blob retrieved
  44. // using |GetPermissionInfo()|.
  45. // |info| is the pointer to the blob.
  46. // |length| is the length of the blob.
  47. // Either |info| or |length| may be NULL/0, in which case nothing happens.
  48. bool RestorePermissionInfo(const FilePath& path, void* info, size_t length) {
  49. if (!info || (length == 0))
  50. return false;
  51. DCHECK_EQ(sizeof(mode_t), length);
  52. mode_t* mode = reinterpret_cast<mode_t*>(info);
  53. int rv = HANDLE_EINTR(chmod(path.value().c_str(), *mode));
  54. delete mode;
  55. return rv == 0;
  56. }
  57. } // namespace
  58. bool DieFileDie(const FilePath& file, bool recurse) {
  59. // There is no need to workaround Windows problems on POSIX.
  60. // Just pass-through.
  61. if (recurse)
  62. return DeletePathRecursively(file);
  63. return DeleteFile(file);
  64. }
  65. void SyncPageCacheToDisk() {
  66. // On Linux (and Android) the sync(2) call waits for I/O completions.
  67. sync();
  68. }
  69. #if !BUILDFLAG(IS_LINUX) && !BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_APPLE) && \
  70. !BUILDFLAG(IS_ANDROID)
  71. bool EvictFileFromSystemCache(const FilePath& file) {
  72. // There doesn't seem to be a POSIX way to cool the disk cache.
  73. NOTIMPLEMENTED();
  74. return false;
  75. }
  76. #endif
  77. bool MakeFileUnreadable(const FilePath& path) {
  78. return DenyFilePermission(path, S_IRUSR | S_IRGRP | S_IROTH);
  79. }
  80. bool MakeFileUnwritable(const FilePath& path) {
  81. return DenyFilePermission(path, S_IWUSR | S_IWGRP | S_IWOTH);
  82. }
  83. FilePermissionRestorer::FilePermissionRestorer(const FilePath& path)
  84. : path_(path), info_(nullptr), length_(0) {
  85. info_ = GetPermissionInfo(path_, &length_);
  86. DCHECK(info_ != nullptr);
  87. DCHECK_NE(0u, length_);
  88. }
  89. FilePermissionRestorer::~FilePermissionRestorer() {
  90. if (!RestorePermissionInfo(path_, info_, length_))
  91. NOTREACHED();
  92. }
  93. } // namespace base