diff --git a/tools/generate_dump.cc b/tools/generate_dump.cc index d9361497..e1bbbfa7 100644 --- a/tools/generate_dump.cc +++ b/tools/generate_dump.cc @@ -153,7 +153,7 @@ int GenerateDumpMain(int argc, char* argv[]) { FileWriter file_writer; if (!file_writer.Open(base::FilePath(options.dump_path), FileWriteMode::kTruncateOrCreate, - true)) { + FilePermissions::kWorldReadable)) { return EXIT_FAILURE; } diff --git a/util/file/file_io.h b/util/file/file_io.h index 901566c7..ef22e2d7 100644 --- a/util/file/file_io.h +++ b/util/file/file_io.h @@ -64,6 +64,15 @@ enum class FileWriteMode { 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 //! short read. //! @@ -166,18 +175,18 @@ FileHandle LoggingOpenFileForRead(const base::FilePath& path); //! an error if the operation fails. //! //! \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 -//! `mode` permissions bits for `open()`, otherwise `0600` will be used. On -//! Windows, the file is always opened in binary mode (that is, no CRLF -//! translation). +//! open the file. On POSIX, \a permissions determines the value that is passed +//! as `mode` to `open()`. On Windows, the file is always opened in binary mode +//! (that is, no CRLF translation). //! //! \return The newly opened FileHandle, or an invalid FileHandle on failure. //! //! \sa FileWriteMode +//! \sa FilePermissions //! \sa ScopedFileHandle FileHandle LoggingOpenFileForWrite(const base::FilePath& path, FileWriteMode write_mode, - bool world_readable); + FilePermissions permissions); //! \brief Wraps `lseek()` or `SetFilePointerEx()`. Logs an error if the //! operation fails. diff --git a/util/file/file_io_posix.cc b/util/file/file_io_posix.cc index fbfdad08..60bc9a8d 100644 --- a/util/file/file_io_posix.cc +++ b/util/file/file_io_posix.cc @@ -84,7 +84,7 @@ FileHandle LoggingOpenFileForRead(const base::FilePath& path) { FileHandle LoggingOpenFileForWrite(const base::FilePath& path, FileWriteMode mode, - bool world_readable) { + FilePermissions permissions) { int flags = O_WRONLY | O_CREAT; // kReuseOrCreate does not need any additional flags. if (mode == FileWriteMode::kTruncateOrCreate) @@ -93,7 +93,9 @@ FileHandle LoggingOpenFileForWrite(const base::FilePath& path, flags |= O_EXCL; 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(); return fd; } diff --git a/util/file/file_io_win.cc b/util/file/file_io_win.cc index 9106ab0b..861f15ac 100644 --- a/util/file/file_io_win.cc +++ b/util/file/file_io_win.cc @@ -83,7 +83,7 @@ FileHandle LoggingOpenFileForRead(const base::FilePath& path) { FileHandle LoggingOpenFileForWrite(const base::FilePath& path, FileWriteMode mode, - bool world_readable) { + FilePermissions permissions) { DWORD disposition = 0; switch (mode) { case FileWriteMode::kReuseOrCreate: diff --git a/util/file/file_writer.cc b/util/file/file_writer.cc index d6ab9e7c..4794c7f0 100644 --- a/util/file/file_writer.cc +++ b/util/file/file_writer.cc @@ -45,9 +45,9 @@ FileWriter::~FileWriter() { bool FileWriter::Open(const base::FilePath& path, FileWriteMode write_mode, - bool world_readable) { + FilePermissions permissions) { CHECK(!file_.is_valid()); - file_.reset(LoggingOpenFileForWrite(path, write_mode, world_readable)); + file_.reset(LoggingOpenFileForWrite(path, write_mode, permissions)); return file_.is_valid(); } diff --git a/util/file/file_writer.h b/util/file/file_writer.h index d0c4a359..7b9524fc 100644 --- a/util/file/file_writer.h +++ b/util/file/file_writer.h @@ -91,7 +91,7 @@ class FileWriter : public FileWriterInterface { //! after Close(). bool Open(const base::FilePath& path, FileWriteMode write_mode, - bool world_readable); + FilePermissions permissions); //! \brief Wraps CheckedCloseHandle(). //!