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_op.h"
16 #include "base/files/file.h"
17 #include "base/files/file_util.h"
18 #include "base/notimplemented.h"
19 #include "base/notreached.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.value().c_str(), &stat_buf) != 0)
32 return false;
33 stat_buf.st_mode &= ~permission;
34
35 int rv = HANDLE_EINTR(chmod(path.value().c_str(), stat_buf.st_mode));
36 return rv == 0;
37 }
38
39 // Gets a blob indicating the permission information for |path|.
40 // |length| is the length of the blob. Zero on failure.
41 // Returns the blob pointer, or NULL on failure.
GetPermissionInfo(const FilePath & path,size_t * length)42 void* GetPermissionInfo(const FilePath& path, size_t* length) {
43 DCHECK(length);
44 *length = 0;
45
46 stat_wrapper_t stat_buf;
47 if (File::Stat(path.value().c_str(), &stat_buf) != 0)
48 return nullptr;
49
50 *length = sizeof(mode_t);
51 mode_t* mode = new mode_t;
52 *mode = stat_buf.st_mode & ~S_IFMT; // Filter out file/path kind.
53
54 return mode;
55 }
56
57 // Restores the permission information for |path|, given the blob retrieved
58 // using |GetPermissionInfo()|.
59 // |info| is the pointer to the blob.
60 // |length| is the length of the blob.
61 // Either |info| or |length| may be NULL/0, in which case nothing happens.
RestorePermissionInfo(const FilePath & path,void * info,size_t length)62 bool RestorePermissionInfo(const FilePath& path, void* info, size_t length) {
63 if (!info || (length == 0))
64 return false;
65
66 DCHECK_EQ(sizeof(mode_t), length);
67 mode_t* mode = reinterpret_cast<mode_t*>(info);
68
69 int rv = HANDLE_EINTR(chmod(path.value().c_str(), *mode));
70
71 delete mode;
72
73 return rv == 0;
74 }
75
76 } // namespace
77
DieFileDie(const FilePath & file,bool recurse)78 bool DieFileDie(const FilePath& file, bool recurse) {
79 // There is no need to workaround Windows problems on POSIX.
80 // Just pass-through.
81 if (recurse)
82 return DeletePathRecursively(file);
83 return DeleteFile(file);
84 }
85
SyncPageCacheToDisk()86 void SyncPageCacheToDisk() {
87 // On Linux (and Android) the sync(2) call waits for I/O completions.
88 sync();
89 }
90
91 #if !BUILDFLAG(IS_LINUX) && !BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_APPLE) && \
92 !BUILDFLAG(IS_ANDROID)
EvictFileFromSystemCache(const FilePath & file)93 bool EvictFileFromSystemCache(const FilePath& file) {
94 // There doesn't seem to be a POSIX way to cool the disk cache.
95 NOTIMPLEMENTED();
96 return false;
97 }
98 #endif
99
MakeFileUnreadable(const FilePath & path)100 bool MakeFileUnreadable(const FilePath& path) {
101 return DenyFilePermission(path, S_IRUSR | S_IRGRP | S_IROTH);
102 }
103
MakeFileUnwritable(const FilePath & path)104 bool MakeFileUnwritable(const FilePath& path) {
105 return DenyFilePermission(path, S_IWUSR | S_IWGRP | S_IWOTH);
106 }
107
FilePermissionRestorer(const FilePath & path)108 FilePermissionRestorer::FilePermissionRestorer(const FilePath& path)
109 : path_(path), info_(nullptr), length_(0) {
110 info_ = GetPermissionInfo(path_, &length_);
111 DCHECK(info_ != nullptr);
112 DCHECK_NE(0u, length_);
113 }
114
~FilePermissionRestorer()115 FilePermissionRestorer::~FilePermissionRestorer() {
116 if (!RestorePermissionInfo(path_, info_, length_))
117 NOTREACHED();
118 }
119
120 } // namespace base
121