Change 'bool world_readable' to an enum

More clear at callsites, and relatively important not to accidentally choose the wrong one.

R=mark@chromium.org

Review URL: https://codereview.chromium.org/821483002
This commit is contained in:
Scott Graham 2014-12-19 15:35:30 -08:00
parent 9cfd2c515e
commit 6865865773
6 changed files with 23 additions and 12 deletions

View File

@ -153,7 +153,7 @@ int GenerateDumpMain(int argc, char* argv[]) {
FileWriter file_writer; FileWriter file_writer;
if (!file_writer.Open(base::FilePath(options.dump_path), if (!file_writer.Open(base::FilePath(options.dump_path),
FileWriteMode::kTruncateOrCreate, FileWriteMode::kTruncateOrCreate,
true)) { FilePermissions::kWorldReadable)) {
return EXIT_FAILURE; return EXIT_FAILURE;
} }

View File

@ -64,6 +64,15 @@ enum class FileWriteMode {
kCreateOrFail, kCreateOrFail,
}; };
//! \brief Determines the permissions bits for files created on POSIX systems.
enum class FilePermissions : bool {
//! \brief Equivalent to `0600`.
kOwnerOnly,
//! \brief Equivalent to `0644`.
kWorldReadable,
};
//! \brief Reads from a file, retrying when interrupted on POSIX or following a //! \brief Reads from a file, retrying when interrupted on POSIX or following a
//! short read. //! short read.
//! //!
@ -166,18 +175,18 @@ FileHandle LoggingOpenFileForRead(const base::FilePath& path);
//! an error if the operation fails. //! an error if the operation fails.
//! //!
//! \a write_mode determines the style (truncate, reuse, etc.) that is used to //! \a write_mode determines the style (truncate, reuse, etc.) that is used to
//! open the file. On POSIX, if \a world_readable, `0644` will be used as //! open the file. On POSIX, \a permissions determines the value that is passed
//! `mode` permissions bits for `open()`, otherwise `0600` will be used. On //! as `mode` to `open()`. On Windows, the file is always opened in binary mode
//! Windows, the file is always opened in binary mode (that is, no CRLF //! (that is, no CRLF translation).
//! translation).
//! //!
//! \return The newly opened FileHandle, or an invalid FileHandle on failure. //! \return The newly opened FileHandle, or an invalid FileHandle on failure.
//! //!
//! \sa FileWriteMode //! \sa FileWriteMode
//! \sa FilePermissions
//! \sa ScopedFileHandle //! \sa ScopedFileHandle
FileHandle LoggingOpenFileForWrite(const base::FilePath& path, FileHandle LoggingOpenFileForWrite(const base::FilePath& path,
FileWriteMode write_mode, FileWriteMode write_mode,
bool world_readable); FilePermissions permissions);
//! \brief Wraps `lseek()` or `SetFilePointerEx()`. Logs an error if the //! \brief Wraps `lseek()` or `SetFilePointerEx()`. Logs an error if the
//! operation fails. //! operation fails.

View File

@ -84,7 +84,7 @@ FileHandle LoggingOpenFileForRead(const base::FilePath& path) {
FileHandle LoggingOpenFileForWrite(const base::FilePath& path, FileHandle LoggingOpenFileForWrite(const base::FilePath& path,
FileWriteMode mode, FileWriteMode mode,
bool world_readable) { FilePermissions permissions) {
int flags = O_WRONLY | O_CREAT; int flags = O_WRONLY | O_CREAT;
// kReuseOrCreate does not need any additional flags. // kReuseOrCreate does not need any additional flags.
if (mode == FileWriteMode::kTruncateOrCreate) if (mode == FileWriteMode::kTruncateOrCreate)
@ -93,7 +93,9 @@ FileHandle LoggingOpenFileForWrite(const base::FilePath& path,
flags |= O_EXCL; flags |= O_EXCL;
int fd = HANDLE_EINTR( int fd = HANDLE_EINTR(
open(path.value().c_str(), flags, world_readable ? 0644 : 0600)); open(path.value().c_str(),
flags,
permissions == FilePermissions::kWorldReadable ? 0644 : 0600));
PLOG_IF(ERROR, fd < 0) << "open " << path.value(); PLOG_IF(ERROR, fd < 0) << "open " << path.value();
return fd; return fd;
} }

View File

@ -83,7 +83,7 @@ FileHandle LoggingOpenFileForRead(const base::FilePath& path) {
FileHandle LoggingOpenFileForWrite(const base::FilePath& path, FileHandle LoggingOpenFileForWrite(const base::FilePath& path,
FileWriteMode mode, FileWriteMode mode,
bool world_readable) { FilePermissions permissions) {
DWORD disposition = 0; DWORD disposition = 0;
switch (mode) { switch (mode) {
case FileWriteMode::kReuseOrCreate: case FileWriteMode::kReuseOrCreate:

View File

@ -45,9 +45,9 @@ FileWriter::~FileWriter() {
bool FileWriter::Open(const base::FilePath& path, bool FileWriter::Open(const base::FilePath& path,
FileWriteMode write_mode, FileWriteMode write_mode,
bool world_readable) { FilePermissions permissions) {
CHECK(!file_.is_valid()); CHECK(!file_.is_valid());
file_.reset(LoggingOpenFileForWrite(path, write_mode, world_readable)); file_.reset(LoggingOpenFileForWrite(path, write_mode, permissions));
return file_.is_valid(); return file_.is_valid();
} }

View File

@ -91,7 +91,7 @@ class FileWriter : public FileWriterInterface {
//! after Close(). //! after Close().
bool Open(const base::FilePath& path, bool Open(const base::FilePath& path,
FileWriteMode write_mode, FileWriteMode write_mode,
bool world_readable); FilePermissions permissions);
//! \brief Wraps CheckedCloseHandle(). //! \brief Wraps CheckedCloseHandle().
//! //!