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..4e28a24ac40277793ef89b3b61ce77bc282189fd 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); |
+ *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 & ~S_IFMT; // Filter out file/path kind. |
+ |
+ 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 == 0)) |
+ 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; |
+ |
+ 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_); |
+ DCHECK(info_ != NULL); |
+ DCHECK_NE(0u, length_); |
+} |
+ |
+PermissionRestorer::~PermissionRestorer() { |
+ if (!RestorePermissionInfo(path_, info_, length_)) |
+ NOTREACHED(); |
+} |
+ |
} // namespace file_util |