OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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> |
(...skipping 14 matching lines...) Expand all Loading... | |
25 bool DenyFilePermission(const FilePath& path, mode_t permission) { | 25 bool DenyFilePermission(const FilePath& path, mode_t permission) { |
26 struct stat stat_buf; | 26 struct stat stat_buf; |
27 if (stat(path.value().c_str(), &stat_buf) != 0) | 27 if (stat(path.value().c_str(), &stat_buf) != 0) |
28 return false; | 28 return false; |
29 stat_buf.st_mode &= ~permission; | 29 stat_buf.st_mode &= ~permission; |
30 | 30 |
31 int rv = HANDLE_EINTR(chmod(path.value().c_str(), stat_buf.st_mode)); | 31 int rv = HANDLE_EINTR(chmod(path.value().c_str(), stat_buf.st_mode)); |
32 return rv == 0; | 32 return rv == 0; |
33 } | 33 } |
34 | 34 |
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 != NULL); | |
Paweł Hajdan Jr.
2012/02/28 07:55:55
nit: Just DCHECK(length) if it compiles.
ahendrickson
2012/03/01 15:18:19
Done.
| |
40 *length = 0; | |
41 | |
42 struct stat stat_buf; | |
43 if (stat(path.value().c_str(), &stat_buf) != 0) | |
44 return NULL; | |
45 | |
46 *length = sizeof(mode_t); | |
47 mode_t* mode = new mode_t; | |
48 *mode = stat_buf.st_mode; | |
Paweł Hajdan Jr.
2012/02/28 07:55:55
That also drags in the type of the thing, i.e. dir
ahendrickson
2012/03/01 15:18:19
Done.
| |
49 | |
50 return mode; | |
51 } | |
52 | |
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) | |
Paweł Hajdan Jr.
2012/02/28 07:55:55
Don't treat |length| as a boolean. Do length == 0
ahendrickson
2012/03/01 15:18:19
Done.
| |
60 return false; | |
61 | |
62 DCHECK_EQ(sizeof(mode_t), length); | |
63 mode_t* mode = reinterpret_cast<mode_t*>(info); | |
64 | |
65 int rv = HANDLE_EINTR(chmod(path.value().c_str(), *mode)); | |
66 | |
67 delete mode; // AKA info. | |
Paweł Hajdan Jr.
2012/02/28 07:55:55
nit: No need for that comment.
ahendrickson
2012/03/01 15:18:19
Done.
| |
68 | |
69 return rv == 0; | |
70 } | |
71 | |
35 } // namespace | 72 } // namespace |
36 | 73 |
37 bool DieFileDie(const FilePath& file, bool recurse) { | 74 bool DieFileDie(const FilePath& file, bool recurse) { |
38 // There is no need to workaround Windows problems on POSIX. | 75 // There is no need to workaround Windows problems on POSIX. |
39 // Just pass-through. | 76 // Just pass-through. |
40 return file_util::Delete(file, recurse); | 77 return file_util::Delete(file, recurse); |
41 } | 78 } |
42 | 79 |
43 // Mostly a verbatim copy of CopyDirectory | 80 // Mostly a verbatim copy of CopyDirectory |
44 bool CopyRecursiveDirNoCache(const FilePath& source_dir, | 81 bool CopyRecursiveDirNoCache(const FilePath& source_dir, |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
133 } | 170 } |
134 | 171 |
135 bool MakeFileUnreadable(const FilePath& path) { | 172 bool MakeFileUnreadable(const FilePath& path) { |
136 return DenyFilePermission(path, S_IRUSR | S_IRGRP | S_IROTH); | 173 return DenyFilePermission(path, S_IRUSR | S_IRGRP | S_IROTH); |
137 } | 174 } |
138 | 175 |
139 bool MakeFileUnwritable(const FilePath& path) { | 176 bool MakeFileUnwritable(const FilePath& path) { |
140 return DenyFilePermission(path, S_IWUSR | S_IWGRP | S_IWOTH); | 177 return DenyFilePermission(path, S_IWUSR | S_IWGRP | S_IWOTH); |
141 } | 178 } |
142 | 179 |
180 PermissionRestorer::PermissionRestorer(const FilePath& path) | |
181 : path_(path), info_(NULL), length_(0) { | |
182 info_ = GetPermissionInfo(path_, &length_); | |
Paweł Hajdan Jr.
2012/02/28 07:55:55
Why don't you just paste the body of GetPermission
ahendrickson
2012/03/01 15:18:19
This seems pretty straightforward to me.
| |
183 DCHECK(info_ != NULL); | |
184 DCHECK_NE(0u, length_); | |
185 } | |
186 | |
187 PermissionRestorer::~PermissionRestorer() { | |
188 if (!RestorePermissionInfo(path_, info_, length_)) | |
Paweł Hajdan Jr.
2012/02/28 07:55:55
Same here.
ahendrickson
2012/03/01 15:18:19
Ditto.
| |
189 NOTREACHED(); | |
190 } | |
191 | |
143 } // namespace file_util | 192 } // namespace file_util |
OLD | NEW |