#include "base/test/test_file_util.h"
#include <errno.h>
#include <fcntl.h>
#include <stddef.h>
#include <sys/types.h>
#include <unistd.h>
#include <string>
#include "base/check_op.h"
#include "base/files/file.h"
#include "base/files/file_util.h"
#include "base/notreached.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
namespace base {
namespace {
bool DenyFilePermission(const FilePath& path, mode_t permission) {
stat_wrapper_t stat_buf;
if (File::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;
stat_wrapper_t stat_buf;
if (File::Stat(path.value().c_str(), &stat_buf) != 0)
return nullptr;
*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) {
if (recurse)
return DeletePathRecursively(file);
return DeleteFile(file);
}
void SyncPageCacheToDisk() {
sync();
}
#if !BUILDFLAG(IS_LINUX) && !BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_APPLE) && \
!BUILDFLAG(IS_ANDROID)
bool EvictFileFromSystemCache(const FilePath& file) {
NOTIMPLEMENTED();
return false;
}
#endif
bool MakeFileUnreadable(const FilePath& path) {
return DenyFilePermission(path, S_IRUSR | S_IRGRP | S_IROTH);
}
bool MakeFileUnwritable(const FilePath& path) {
return DenyFilePermission(path, S_IWUSR | S_IWGRP | S_IWOTH);
}
FilePermissionRestorer::FilePermissionRestorer(const FilePath& path)
: path_(path), info_(nullptr), length_(0) {
info_ = GetPermissionInfo(path_, &length_);
DCHECK(info_ != nullptr);
DCHECK_NE(0u, length_);
}
FilePermissionRestorer::~FilePermissionRestorer() {
if (!RestorePermissionInfo(path_, info_, length_))
NOTREACHED();
}
}