Add LoggingSeekFile

As the whence values are "helpfully" available in windows.h as well
( http://msdn.microsoft.com/en-us/library/windows/desktop/dd757336(v=vs.85).aspx )
don't bother inventing a new enum. Add implementations for POSIX and Win32.

R=mark@chromium.org
BUG=crashpad:1

Review URL: https://codereview.chromium.org/812593005
This commit is contained in:
Scott Graham 2014-12-19 10:45:22 -08:00
parent 4034d30023
commit 384497475a
3 changed files with 53 additions and 0 deletions

View File

@ -30,10 +30,18 @@ class FilePath;
namespace crashpad {
#if defined(OS_POSIX) || DOXYGEN
//! \brief Platform-specific alias for a low-level file handle.
using FileHandle = int;
//! \brief Platform-specific alias for a position in an open file.
using FileOffset = off_t;
#elif defined(OS_WIN)
using FileHandle = HANDLE;
using FileOffset = LONGLONG;
#endif
//! \brief Determines the mode that LoggingOpenFileForWrite() uses.
@ -166,6 +174,17 @@ FileHandle LoggingOpenFileForWrite(const base::FilePath& path,
FileWriteMode write_mode,
bool world_readable);
//! \brief Wraps `lseek()` or `SetFilePointerEx()`. Logs an error if the
//! operation fails.
//!
//! Repositions the offset of the open \a file to the specified \a offset,
//! relative to \a whence. \a whence must be one of `SEEK_SET`, `SEEK_CUR`, or
//! `SEEK_END`, and is interpreted in the usual way.
//!
//! \return The resulting offset in bytes from the beginning of the file, or
//! `-1` on failure.
FileOffset LoggingSeekFile(FileHandle file, FileOffset offset, int whence);
//! \brief Wraps `close()` or `CloseHandle()`, logging an error if the operation
//! fails.
//!

View File

@ -98,6 +98,12 @@ FileHandle LoggingOpenFileForWrite(const base::FilePath& path,
return fd;
}
FileOffset LoggingSeekFile(FileHandle file, FileOffset offset, int whence) {
off_t rv = lseek(file, offset, whence);
PLOG_IF(ERROR, rv < 0) << "lseek";
return rv;
}
bool LoggingCloseFile(FileHandle file) {
int rv = IGNORE_EINTR(close(file));
PLOG_IF(ERROR, rv != 0) << "close";

View File

@ -103,6 +103,34 @@ FileHandle LoggingOpenFileForWrite(const base::FilePath& path,
return file;
}
FileOffset LoggingSeekFile(FileHandle file, FileOffset offset, int whence) {
DWORD method = 0;
switch (whence) {
case SEEK_SET:
method = FILE_BEGIN;
break;
case SEEK_CUR:
method = FILE_CURRENT;
break;
case SEEK_END:
method = FILE_END;
break;
default:
NOTREACHED();
break;
}
LARGE_INTEGER distance_to_move;
distance_to_move.QuadPart = offset;
LARGE_INTEGER new_offset;
BOOL result = SetFilePointerEx(file, distance_to_move, &new_offset, method);
if (!result) {
PLOG(ERROR) << "SetFilePointerEx";
return -1;
}
return new_offset.QuadPart;
}
bool LoggingCloseFile(FileHandle file) {
BOOL rv = CloseHandle(file);
PLOG_IF(ERROR, !rv) << "CloseHandle";