OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/test/test_file_util.h" | 5 #include "base/test/test_file_util.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <sys/stat.h> | 9 #include <sys/stat.h> |
10 #include <sys/types.h> | 10 #include <sys/types.h> |
11 | 11 |
12 #include <string> | 12 #include <string> |
13 | 13 |
14 #include "base/file_util.h" | 14 #include "base/file_util.h" |
15 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
17 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
18 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
19 | 19 |
20 using base::MakeAbsoluteFilePath; | |
21 | |
22 namespace base { | 20 namespace base { |
23 | 21 |
24 namespace { | 22 namespace { |
25 | 23 |
26 // Deny |permission| on the file |path|. | 24 // Deny |permission| on the file |path|. |
27 bool DenyFilePermission(const FilePath& path, mode_t permission) { | 25 bool DenyFilePermission(const FilePath& path, mode_t permission) { |
28 struct stat stat_buf; | 26 struct stat stat_buf; |
29 if (stat(path.value().c_str(), &stat_buf) != 0) | 27 if (stat(path.value().c_str(), &stat_buf) != 0) |
30 return false; | 28 return false; |
31 stat_buf.st_mode &= ~permission; | 29 stat_buf.st_mode &= ~permission; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 #endif | 86 #endif |
89 | 87 |
90 } // namespace base | 88 } // namespace base |
91 | 89 |
92 namespace file_util { | 90 namespace file_util { |
93 | 91 |
94 using base::DenyFilePermission; | 92 using base::DenyFilePermission; |
95 using base::GetPermissionInfo; | 93 using base::GetPermissionInfo; |
96 using base::RestorePermissionInfo; | 94 using base::RestorePermissionInfo; |
97 | 95 |
98 std::wstring FilePathAsWString(const base::FilePath& path) { | |
99 return base::UTF8ToWide(path.value()); | |
100 } | |
101 base::FilePath WStringAsFilePath(const std::wstring& path) { | |
102 return base::FilePath(base::WideToUTF8(path)); | |
103 } | |
104 | |
105 bool MakeFileUnreadable(const base::FilePath& path) { | 96 bool MakeFileUnreadable(const base::FilePath& path) { |
106 return DenyFilePermission(path, S_IRUSR | S_IRGRP | S_IROTH); | 97 return DenyFilePermission(path, S_IRUSR | S_IRGRP | S_IROTH); |
107 } | 98 } |
108 | 99 |
109 bool MakeFileUnwritable(const base::FilePath& path) { | 100 bool MakeFileUnwritable(const base::FilePath& path) { |
110 return DenyFilePermission(path, S_IWUSR | S_IWGRP | S_IWOTH); | 101 return DenyFilePermission(path, S_IWUSR | S_IWGRP | S_IWOTH); |
111 } | 102 } |
112 | 103 |
113 PermissionRestorer::PermissionRestorer(const base::FilePath& path) | 104 PermissionRestorer::PermissionRestorer(const base::FilePath& path) |
114 : path_(path), info_(NULL), length_(0) { | 105 : path_(path), info_(NULL), length_(0) { |
115 info_ = GetPermissionInfo(path_, &length_); | 106 info_ = GetPermissionInfo(path_, &length_); |
116 DCHECK(info_ != NULL); | 107 DCHECK(info_ != NULL); |
117 DCHECK_NE(0u, length_); | 108 DCHECK_NE(0u, length_); |
118 } | 109 } |
119 | 110 |
120 PermissionRestorer::~PermissionRestorer() { | 111 PermissionRestorer::~PermissionRestorer() { |
121 if (!RestorePermissionInfo(path_, info_, length_)) | 112 if (!RestorePermissionInfo(path_, info_, length_)) |
122 NOTREACHED(); | 113 NOTREACHED(); |
123 } | 114 } |
124 | 115 |
125 } // namespace file_util | 116 } // namespace file_util |
OLD | NEW |