Update .DEPS.git
[chromium-blink-merge.git] / base / test / test_file_util_posix.cc
blob5ebf335376e7aa40963645b2c414c9375983cc1f
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.
5 #include "base/test/test_file_util.h"
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <sys/stat.h>
10 #include <sys/types.h>
12 #include <string>
14 #include "base/file_path.h"
15 #include "base/file_util.h"
16 #include "base/logging.h"
17 #include "base/string_util.h"
18 #include "base/utf_string_conversions.h"
20 namespace file_util {
22 namespace {
24 // Deny |permission| on the file |path|.
25 bool DenyFilePermission(const FilePath& path, mode_t permission) {
26 struct stat stat_buf;
27 if (stat(path.value().c_str(), &stat_buf) != 0)
28 return false;
29 stat_buf.st_mode &= ~permission;
31 int rv = HANDLE_EINTR(chmod(path.value().c_str(), stat_buf.st_mode));
32 return rv == 0;
35 // Gets a blob indicating the permission information for |path|.
36 // |length| is the length of the blob. Zero on failure.
37 // Returns the blob pointer, or NULL on failure.
38 void* GetPermissionInfo(const FilePath& path, size_t* length) {
39 DCHECK(length);
40 *length = 0;
42 struct stat stat_buf;
43 if (stat(path.value().c_str(), &stat_buf) != 0)
44 return NULL;
46 *length = sizeof(mode_t);
47 mode_t* mode = new mode_t;
48 *mode = stat_buf.st_mode & ~S_IFMT; // Filter out file/path kind.
50 return mode;
53 // Restores the permission information for |path|, given the blob retrieved
54 // using |GetPermissionInfo()|.
55 // |info| is the pointer to the blob.
56 // |length| is the length of the blob.
57 // Either |info| or |length| may be NULL/0, in which case nothing happens.
58 bool RestorePermissionInfo(const FilePath& path, void* info, size_t length) {
59 if (!info || (length == 0))
60 return false;
62 DCHECK_EQ(sizeof(mode_t), length);
63 mode_t* mode = reinterpret_cast<mode_t*>(info);
65 int rv = HANDLE_EINTR(chmod(path.value().c_str(), *mode));
67 delete mode;
69 return rv == 0;
72 } // namespace
74 bool DieFileDie(const FilePath& file, bool recurse) {
75 // There is no need to workaround Windows problems on POSIX.
76 // Just pass-through.
77 return file_util::Delete(file, recurse);
80 // Mostly a verbatim copy of CopyDirectory
81 bool CopyRecursiveDirNoCache(const FilePath& source_dir,
82 const FilePath& dest_dir) {
83 char top_dir[PATH_MAX];
84 if (base::strlcpy(top_dir, source_dir.value().c_str(),
85 arraysize(top_dir)) >= arraysize(top_dir)) {
86 return false;
89 // This function does not properly handle destinations within the source
90 FilePath real_to_path = dest_dir;
91 if (PathExists(real_to_path)) {
92 if (!AbsolutePath(&real_to_path))
93 return false;
94 } else {
95 real_to_path = real_to_path.DirName();
96 if (!AbsolutePath(&real_to_path))
97 return false;
99 if (real_to_path.value().compare(0, source_dir.value().size(),
100 source_dir.value()) == 0)
101 return false;
103 bool success = true;
104 int traverse_type = FileEnumerator::FILES |
105 FileEnumerator::SHOW_SYM_LINKS | FileEnumerator::DIRECTORIES;
106 FileEnumerator traversal(source_dir, true, traverse_type);
108 // dest_dir may not exist yet, start the loop with dest_dir
109 FileEnumerator::FindInfo info;
110 FilePath current = source_dir;
111 if (stat(source_dir.value().c_str(), &info.stat) < 0) {
112 DLOG(ERROR) << "CopyRecursiveDirNoCache() couldn't stat source directory: "
113 << source_dir.value() << " errno = " << errno;
114 success = false;
117 while (success && !current.empty()) {
118 // |current| is the source path, including source_dir, so paste
119 // the suffix after source_dir onto dest_dir to create the target_path.
120 std::string suffix(&current.value().c_str()[source_dir.value().size()]);
121 // Strip the leading '/' (if any).
122 if (!suffix.empty()) {
123 DCHECK_EQ('/', suffix[0]);
124 suffix.erase(0, 1);
126 const FilePath target_path = dest_dir.Append(suffix);
128 if (S_ISDIR(info.stat.st_mode)) {
129 if (mkdir(target_path.value().c_str(), info.stat.st_mode & 01777) != 0 &&
130 errno != EEXIST) {
131 DLOG(ERROR) << "CopyRecursiveDirNoCache() couldn't create directory: "
132 << target_path.value() << " errno = " << errno;
133 success = false;
135 } else if (S_ISREG(info.stat.st_mode)) {
136 if (CopyFile(current, target_path)) {
137 success = EvictFileFromSystemCache(target_path);
138 DCHECK(success);
139 } else {
140 DLOG(ERROR) << "CopyRecursiveDirNoCache() couldn't create file: "
141 << target_path.value();
142 success = false;
144 } else {
145 DLOG(WARNING) << "CopyRecursiveDirNoCache() skipping non-regular file: "
146 << current.value();
149 current = traversal.Next();
150 traversal.GetFindInfo(&info);
153 return success;
156 #if !defined(OS_LINUX) && !defined(OS_MACOSX)
157 bool EvictFileFromSystemCache(const FilePath& file) {
158 // There doesn't seem to be a POSIX way to cool the disk cache.
159 NOTIMPLEMENTED();
160 return false;
162 #endif
164 std::wstring FilePathAsWString(const FilePath& path) {
165 return UTF8ToWide(path.value());
167 FilePath WStringAsFilePath(const std::wstring& path) {
168 return FilePath(WideToUTF8(path));
171 bool MakeFileUnreadable(const FilePath& path) {
172 return DenyFilePermission(path, S_IRUSR | S_IRGRP | S_IROTH);
175 bool MakeFileUnwritable(const FilePath& path) {
176 return DenyFilePermission(path, S_IWUSR | S_IWGRP | S_IWOTH);
179 PermissionRestorer::PermissionRestorer(const FilePath& path)
180 : path_(path), info_(NULL), length_(0) {
181 info_ = GetPermissionInfo(path_, &length_);
182 DCHECK(info_ != NULL);
183 DCHECK_NE(0u, length_);
186 PermissionRestorer::~PermissionRestorer() {
187 if (!RestorePermissionInfo(path_, info_, length_))
188 NOTREACHED();
191 } // namespace file_util