Chromium Code Reviews| Index: base/test/test_file_util_posix.cc |
| diff --git a/base/test/test_file_util_posix.cc b/base/test/test_file_util_posix.cc |
| index 0096c9e815df5e3c723012cd5e73b555e176c063..143b338c1de91a2d04a2c67068e14b8af78c000a 100644 |
| --- a/base/test/test_file_util_posix.cc |
| +++ b/base/test/test_file_util_posix.cc |
| @@ -32,6 +32,43 @@ bool DenyFilePermission(const FilePath& path, mode_t permission) { |
| return rv == 0; |
| } |
| +// Gets a blob indicating the permission information for |path|. |
| +// |length| is the length of the blob. Zero on failure. |
| +// Returns the blob pointer, or NULL on failure. |
| +void* GetPermissionInfo(const FilePath& path, size_t* length) { |
| + 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.
|
| + *length = 0; |
| + |
| + struct stat stat_buf; |
| + if (stat(path.value().c_str(), &stat_buf) != 0) |
| + return NULL; |
| + |
| + *length = sizeof(mode_t); |
| + mode_t* mode = new mode_t; |
| + *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.
|
| + |
| + return mode; |
| +} |
| + |
| +// Restores the permission information for |path|, given the blob retrieved |
| +// using |GetPermissionInfo()|. |
| +// |info| is the pointer to the blob. |
| +// |length| is the length of the blob. |
| +// Either |info| or |length| may be NULL/0, in which case nothing happens. |
| +bool RestorePermissionInfo(const FilePath& path, void* info, size_t length) { |
| + 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.
|
| + return false; |
| + |
| + DCHECK_EQ(sizeof(mode_t), length); |
| + mode_t* mode = reinterpret_cast<mode_t*>(info); |
| + |
| + int rv = HANDLE_EINTR(chmod(path.value().c_str(), *mode)); |
| + |
| + 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.
|
| + |
| + return rv == 0; |
| +} |
| + |
| } // namespace |
| bool DieFileDie(const FilePath& file, bool recurse) { |
| @@ -140,4 +177,16 @@ bool MakeFileUnwritable(const FilePath& path) { |
| return DenyFilePermission(path, S_IWUSR | S_IWGRP | S_IWOTH); |
| } |
| +PermissionRestorer::PermissionRestorer(const FilePath& path) |
| + : path_(path), info_(NULL), length_(0) { |
| + 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.
|
| + DCHECK(info_ != NULL); |
| + DCHECK_NE(0u, length_); |
| +} |
| + |
| +PermissionRestorer::~PermissionRestorer() { |
| + if (!RestorePermissionInfo(path_, info_, length_)) |
|
Paweł Hajdan Jr.
2012/02/28 07:55:55
Same here.
ahendrickson
2012/03/01 15:18:19
Ditto.
|
| + NOTREACHED(); |
| +} |
| + |
| } // namespace file_util |