• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/test/test_file_util.h"
6 
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <stddef.h>
10 #include <sys/types.h>
11 #include <unistd.h>
12 
13 #include <string>
14 
15 #include "base/check.h"
16 #include "base/check_op.h"
17 #include "base/files/file.h"
18 #include "base/files/file_util.h"
19 #include "base/notimplemented.h"
20 #include "base/strings/string_util.h"
21 #include "base/strings/utf_string_conversions.h"
22 #include "build/build_config.h"
23 
24 namespace base {
25 
26 namespace {
27 
28 // Deny |permission| on the file |path|.
DenyFilePermission(const FilePath & path,mode_t permission)29 bool DenyFilePermission(const FilePath& path, mode_t permission) {
30   stat_wrapper_t stat_buf;
31   if (File::Stat(path, &stat_buf) != 0) {
32     return false;
33   }
34   stat_buf.st_mode &= ~permission;
35 
36   int rv = HANDLE_EINTR(chmod(path.value().c_str(), stat_buf.st_mode));
37   return rv == 0;
38 }
39 
40 // Gets a blob indicating the permission information for |path|.
41 // |length| is the length of the blob.  Zero on failure.
42 // Returns the blob pointer, or NULL on failure.
GetPermissionInfo(const FilePath & path,size_t * length)43 void* GetPermissionInfo(const FilePath& path, size_t* length) {
44   DCHECK(length);
45   *length = 0;
46 
47   stat_wrapper_t stat_buf;
48   if (File::Stat(path, &stat_buf) != 0) {
49     return nullptr;
50   }
51 
52   *length = sizeof(mode_t);
53   mode_t* mode = new mode_t;
54   *mode = stat_buf.st_mode & ~S_IFMT;  // Filter out file/path kind.
55 
56   return mode;
57 }
58 
59 // Restores the permission information for |path|, given the blob retrieved
60 // using |GetPermissionInfo()|.
61 // |info| is the pointer to the blob.
62 // |length| is the length of the blob.
63 // Either |info| or |length| may be NULL/0, in which case nothing happens.
RestorePermissionInfo(const FilePath & path,void * info,size_t length)64 bool RestorePermissionInfo(const FilePath& path, void* info, size_t length) {
65   if (!info || (length == 0))
66     return false;
67 
68   DCHECK_EQ(sizeof(mode_t), length);
69   mode_t* mode = reinterpret_cast<mode_t*>(info);
70 
71   int rv = HANDLE_EINTR(chmod(path.value().c_str(), *mode));
72 
73   delete mode;
74 
75   return rv == 0;
76 }
77 
78 }  // namespace
79 
DieFileDie(const FilePath & file,bool recurse)80 bool DieFileDie(const FilePath& file, bool recurse) {
81   // There is no need to workaround Windows problems on POSIX.
82   // Just pass-through.
83   if (recurse)
84     return DeletePathRecursively(file);
85   return DeleteFile(file);
86 }
87 
SyncPageCacheToDisk()88 void SyncPageCacheToDisk() {
89   // On Linux (and Android) the sync(2) call waits for I/O completions.
90   sync();
91 }
92 
93 #if !BUILDFLAG(IS_LINUX) && !BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_APPLE) && \
94     !BUILDFLAG(IS_ANDROID)
EvictFileFromSystemCache(const FilePath & file)95 bool EvictFileFromSystemCache(const FilePath& file) {
96   // There doesn't seem to be a POSIX way to cool the disk cache.
97   NOTIMPLEMENTED();
98   return false;
99 }
100 #endif
101 
MakeFileUnreadable(const FilePath & path)102 bool MakeFileUnreadable(const FilePath& path) {
103   return DenyFilePermission(path, S_IRUSR | S_IRGRP | S_IROTH);
104 }
105 
MakeFileUnwritable(const FilePath & path)106 bool MakeFileUnwritable(const FilePath& path) {
107   return DenyFilePermission(path, S_IWUSR | S_IWGRP | S_IWOTH);
108 }
109 
FilePermissionRestorer(const FilePath & path)110 FilePermissionRestorer::FilePermissionRestorer(const FilePath& path)
111     : path_(path), info_(nullptr), length_(0) {
112   info_ = GetPermissionInfo(path_, &length_);
113   DCHECK(info_ != nullptr);
114   DCHECK_NE(0u, length_);
115 }
116 
~FilePermissionRestorer()117 FilePermissionRestorer::~FilePermissionRestorer() {
118   const bool success = RestorePermissionInfo(path_, info_, length_);
119   CHECK(success);
120 }
121 
122 }  // namespace base
123