This source file includes following definitions.
- DenyFilePermission
- GetPermissionInfo
- RestorePermissionInfo
- DieFileDie
- EvictFileFromSystemCache
- FilePathAsWString
- WStringAsFilePath
- MakeFileUnreadable
- MakeFileUnwritable
- length_
#include "base/test/test_file_util.h"
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string>
#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
using base::MakeAbsoluteFilePath;
namespace base {
namespace {
bool DenyFilePermission(const FilePath& path, mode_t permission) {
struct stat stat_buf;
if (stat(path.value().c_str(), &stat_buf) != 0)
return false;
stat_buf.st_mode &= ~permission;
int rv = HANDLE_EINTR(chmod(path.value().c_str(), stat_buf.st_mode));
return rv == 0;
}
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;
return mode;
}
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;
}
}
bool DieFileDie(const FilePath& file, bool recurse) {
return DeleteFile(file, recurse);
}
#if !defined(OS_LINUX) && !defined(OS_MACOSX)
bool EvictFileFromSystemCache(const FilePath& file) {
NOTIMPLEMENTED();
return false;
}
#endif
}
namespace file_util {
using base::DenyFilePermission;
using base::GetPermissionInfo;
using base::RestorePermissionInfo;
std::wstring FilePathAsWString(const base::FilePath& path) {
return base::UTF8ToWide(path.value());
}
base::FilePath WStringAsFilePath(const std::wstring& path) {
return base::FilePath(base::WideToUTF8(path));
}
bool MakeFileUnreadable(const base::FilePath& path) {
return DenyFilePermission(path, S_IRUSR | S_IRGRP | S_IROTH);
}
bool MakeFileUnwritable(const base::FilePath& path) {
return DenyFilePermission(path, S_IWUSR | S_IWGRP | S_IWOTH);
}
PermissionRestorer::PermissionRestorer(const base::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();
}
}