mirror of
https://github.com/chromium/crashpad.git
synced 2024-12-28 07:48:14 +08:00
Add LoggingOpenFileFor{Read|Write}
I started (https://codereview.chromium.org/812403002/) emulating oflag values on Windows in FileWriter, but it seemed awkward. On the assumption that we're only likely to need "read a file" and "write a file" this seemed simpler, and sufficient (but I don't know if that's necessarily true). Users of open are not yet switched. R=mark@chromium.org BUG=crashpad:1 Review URL: https://codereview.chromium.org/818433002
This commit is contained in:
parent
4a9b858fbd
commit
4034d30023
@ -23,16 +23,34 @@
|
|||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
namespace base {
|
||||||
|
class FilePath;
|
||||||
|
} // namespace base
|
||||||
|
|
||||||
namespace crashpad {
|
namespace crashpad {
|
||||||
|
|
||||||
#if defined(OS_POSIX)
|
#if defined(OS_POSIX) || DOXYGEN
|
||||||
|
//! \brief Platform-specific alias for a low-level file handle.
|
||||||
using FileHandle = int;
|
using FileHandle = int;
|
||||||
#elif defined(OS_WIN)
|
#elif defined(OS_WIN)
|
||||||
using FileHandle = HANDLE;
|
using FileHandle = HANDLE;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
//! \brief Determines the mode that LoggingOpenFileForWrite() uses.
|
||||||
|
enum class FileWriteMode {
|
||||||
|
//! \brief Opens the file if it exists, or creates a new file.
|
||||||
|
kReuseOrCreate,
|
||||||
|
|
||||||
|
//! \brief Creates a new file. If the file already exists, it will be
|
||||||
|
//! overwritten.
|
||||||
|
kTruncateOrCreate,
|
||||||
|
|
||||||
|
//! \brief Creates a new file. If the file already exists, the open will fail.
|
||||||
|
kCreateOrFail,
|
||||||
|
};
|
||||||
|
|
||||||
//! \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.
|
||||||
//!
|
//!
|
||||||
//! This function reads into \a buffer, stopping only when \a size bytes have
|
//! This function reads into \a buffer, stopping only when \a size bytes have
|
||||||
//! been read or when end-of-file has been reached. On Windows, reading from
|
//! been read or when end-of-file has been reached. On Windows, reading from
|
||||||
@ -49,7 +67,7 @@ using FileHandle = HANDLE;
|
|||||||
ssize_t ReadFile(FileHandle file, void* buffer, size_t size);
|
ssize_t ReadFile(FileHandle file, void* buffer, size_t size);
|
||||||
|
|
||||||
//! \brief Writes to a file, retrying when interrupted or following a short
|
//! \brief Writes to a file, retrying when interrupted or following a short
|
||||||
//! write on POSIX.
|
//! write on POSIX.
|
||||||
//!
|
//!
|
||||||
//! This function writes to \a file, stopping only when \a size bytes have been
|
//! This function writes to \a file, stopping only when \a size bytes have been
|
||||||
//! written.
|
//! written.
|
||||||
@ -121,8 +139,35 @@ void CheckedWriteFile(FileHandle file, const void* buffer, size_t size);
|
|||||||
//! \sa ReadFile
|
//! \sa ReadFile
|
||||||
void CheckedReadFileAtEOF(FileHandle file);
|
void CheckedReadFileAtEOF(FileHandle file);
|
||||||
|
|
||||||
|
//! \brief Wraps `open()` or `CreateFile()`, opening an existing file for
|
||||||
|
//! reading. Logs an error if the operation fails.
|
||||||
|
//!
|
||||||
|
//! \return The newly opened FileHandle, or an invalid FileHandle on failure.
|
||||||
|
//!
|
||||||
|
//! \sa ScopedFD
|
||||||
|
//! \sa ScopedFileHANDLE
|
||||||
|
FileHandle LoggingOpenFileForRead(const base::FilePath& path);
|
||||||
|
|
||||||
|
//! \brief Wraps `open()` or `CreateFile()`, creating a file for output. Logs
|
||||||
|
//! 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).
|
||||||
|
//!
|
||||||
|
//! \return The newly opened FileHandle, or an invalid FileHandle on failure.
|
||||||
|
//!
|
||||||
|
//! \sa FileWriteMode
|
||||||
|
//! \sa ScopedFD
|
||||||
|
//! \sa ScopedFileHANDLE
|
||||||
|
FileHandle LoggingOpenFileForWrite(const base::FilePath& path,
|
||||||
|
FileWriteMode write_mode,
|
||||||
|
bool world_readable);
|
||||||
|
|
||||||
//! \brief Wraps `close()` or `CloseHandle()`, logging an error if the operation
|
//! \brief Wraps `close()` or `CloseHandle()`, logging an error if the operation
|
||||||
//! fails.
|
//! fails.
|
||||||
//!
|
//!
|
||||||
//! \return On success, `true` is returned. On failure, an error is logged and
|
//! \return On success, `true` is returned. On failure, an error is logged and
|
||||||
//! `false` is returned.
|
//! `false` is returned.
|
||||||
|
@ -14,8 +14,10 @@
|
|||||||
|
|
||||||
#include "util/file/file_io.h"
|
#include "util/file/file_io.h"
|
||||||
|
|
||||||
|
#include <fcntl.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "base/files/file_path.h"
|
||||||
#include "base/logging.h"
|
#include "base/logging.h"
|
||||||
#include "base/numerics/safe_conversions.h"
|
#include "base/numerics/safe_conversions.h"
|
||||||
#include "base/posix/eintr_wrapper.h"
|
#include "base/posix/eintr_wrapper.h"
|
||||||
@ -74,6 +76,28 @@ ssize_t WriteFile(FileHandle file, const void* buffer, size_t size) {
|
|||||||
return ReadOrWrite<WriteTraits>(file, buffer, size);
|
return ReadOrWrite<WriteTraits>(file, buffer, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FileHandle LoggingOpenFileForRead(const base::FilePath& path) {
|
||||||
|
int fd = HANDLE_EINTR(open(path.value().c_str(), O_RDONLY));
|
||||||
|
PLOG_IF(ERROR, fd < 0) << "open " << path.value();
|
||||||
|
return fd;
|
||||||
|
}
|
||||||
|
|
||||||
|
FileHandle LoggingOpenFileForWrite(const base::FilePath& path,
|
||||||
|
FileWriteMode mode,
|
||||||
|
bool world_readable) {
|
||||||
|
int flags = O_WRONLY | O_CREAT;
|
||||||
|
// kReuseOrCreate does not need any additional flags.
|
||||||
|
if (mode == FileWriteMode::kTruncateOrCreate)
|
||||||
|
flags |= O_TRUNC;
|
||||||
|
else if (mode == FileWriteMode::kCreateOrFail)
|
||||||
|
flags |= O_EXCL;
|
||||||
|
|
||||||
|
int fd = HANDLE_EINTR(
|
||||||
|
open(path.value().c_str(), flags, world_readable ? 0644 : 0600));
|
||||||
|
PLOG_IF(ERROR, fd < 0) << "open " << path.value();
|
||||||
|
return fd;
|
||||||
|
}
|
||||||
|
|
||||||
bool LoggingCloseFile(FileHandle file) {
|
bool LoggingCloseFile(FileHandle file) {
|
||||||
int rv = IGNORE_EINTR(close(file));
|
int rv = IGNORE_EINTR(close(file));
|
||||||
PLOG_IF(ERROR, rv != 0) << "close";
|
PLOG_IF(ERROR, rv != 0) << "close";
|
||||||
|
@ -14,6 +14,7 @@
|
|||||||
|
|
||||||
#include "util/file/file_io.h"
|
#include "util/file/file_io.h"
|
||||||
|
|
||||||
|
#include "base/files/file_path.h"
|
||||||
#include "base/logging.h"
|
#include "base/logging.h"
|
||||||
#include "base/numerics/safe_conversions.h"
|
#include "base/numerics/safe_conversions.h"
|
||||||
|
|
||||||
@ -72,6 +73,36 @@ ssize_t WriteFile(FileHandle file, const void* buffer, size_t size) {
|
|||||||
return bytes_written;
|
return bytes_written;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FileHandle LoggingOpenFileForRead(const base::FilePath& path) {
|
||||||
|
HANDLE file = CreateFile(path.value().c_str(), GENERIC_READ, FILE_SHARE_READ,
|
||||||
|
nullptr, OPEN_EXISTING, 0, nullptr);
|
||||||
|
PLOG_IF(ERROR, file == INVALID_HANDLE_VALUE) << "CreateFile "
|
||||||
|
<< path.value().c_str();
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
|
||||||
|
FileHandle LoggingOpenFileForWrite(const base::FilePath& path,
|
||||||
|
FileWriteMode mode,
|
||||||
|
bool world_readable) {
|
||||||
|
DWORD disposition = 0;
|
||||||
|
switch (mode) {
|
||||||
|
case FileWriteMode::kReuseOrCreate:
|
||||||
|
disposition = OPEN_ALWAYS;
|
||||||
|
break;
|
||||||
|
case FileWriteMode::kTruncateOrCreate:
|
||||||
|
disposition = CREATE_ALWAYS;
|
||||||
|
break;
|
||||||
|
case FileWriteMode::kCreateOrFail:
|
||||||
|
disposition = CREATE_NEW;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
HANDLE file = CreateFile(path.value().c_str(), GENERIC_WRITE, 0, nullptr,
|
||||||
|
disposition, FILE_ATTRIBUTE_NORMAL, nullptr);
|
||||||
|
PLOG_IF(ERROR, file == INVALID_HANDLE_VALUE) << "CreateFile "
|
||||||
|
<< path.value().c_str();
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
|
||||||
bool LoggingCloseFile(FileHandle file) {
|
bool LoggingCloseFile(FileHandle file) {
|
||||||
BOOL rv = CloseHandle(file);
|
BOOL rv = CloseHandle(file);
|
||||||
PLOG_IF(ERROR, !rv) << "CloseHandle";
|
PLOG_IF(ERROR, !rv) << "CloseHandle";
|
||||||
|
Loading…
x
Reference in New Issue
Block a user