Index: util/file/file_io_win.cc |
diff --git a/util/numeric/in_range_cast.h b/util/file/file_io_win.cc |
similarity index 39% |
copy from util/numeric/in_range_cast.h |
copy to util/file/file_io_win.cc |
index 3dba10afa16016bb7149cd1fa7b4ae14c72cfca2..118ef7eca4164abcd93f3e2ab9169dd85c3ddcc8 100644 |
--- a/util/numeric/in_range_cast.h |
+++ b/util/file/file_io_win.cc |
@@ -12,33 +12,48 @@ |
// See the License for the specific language governing permissions and |
// limitations under the License. |
-#ifndef CRASHPAD_UTIL_NUMERIC_IN_RANGE_CAST_H_ |
-#define CRASHPAD_UTIL_NUMERIC_IN_RANGE_CAST_H_ |
+#include "util/file/file_io.h" |
#include "base/logging.h" |
#include "base/numerics/safe_conversions.h" |
namespace crashpad { |
-//! \brief Casts to a different type if it can be done without data loss, |
-//! logging a warning message and returing a default value otherwise. |
-//! |
-//! \param[in] source The value to convert and return. |
-//! \param[in] default_value The default value to return, in the event that \a |
-//! source cannot be represented in the destination type. |
-//! |
-//! \return \a source if it can be represented in the destination type, |
-//! otherwise \a default_value. |
-template <typename Destination, typename Source> |
-Destination InRangeCast(Source source, Destination default_value) { |
- if (base::IsValueInRangeForNumericType<Destination>(source)) { |
- return static_cast<Destination>(source); |
+// TODO(scottmg): Handle > DWORD sized writes if necessary. |
+ |
+ssize_t ReadFile(FileHandle file, void* buffer, size_t size) { |
+ DWORD size_dword = base::checked_cast<DWORD>(size); |
+ DWORD total_read = 0; |
+ char* buffer_c = reinterpret_cast<char*>(buffer); |
+ while (size_dword > 0) { |
+ DWORD bytes_read; |
+ BOOL success = ::ReadFile(file, buffer_c, size_dword, &bytes_read, nullptr); |
Mark Mentovai
2014/12/17 05:39:16
If EOF is supposed to be signaled by ReadFile() re
scottmg
2014/12/17 20:18:32
Yes, you're right. (I confirmed this, which I shou
|
+ if (!success && GetLastError() != ERROR_MORE_DATA) |
+ return -1; |
+ |
+ buffer_c += bytes_read; |
+ size_dword -= bytes_read; |
+ total_read += bytes_read; |
} |
+ return total_read; |
+} |
- LOG(WARNING) << "value " << source << " out of range"; |
- return static_cast<Destination>(default_value); |
+ssize_t WriteFile(FileHandle file, const void* buffer, size_t size) { |
+ // TODO(scottmg): This might need to handle the limit for pipes across a |
+ // network in the future. |
+ DWORD size_dword = base::checked_cast<DWORD>(size); |
+ DWORD bytes_written; |
+ BOOL rv = ::WriteFile(file, buffer, size_dword, &bytes_written, nullptr); |
+ if (!rv) |
+ return -1; |
+ CHECK_EQ(bytes_written, size_dword); |
+ return bytes_written; |
} |
-} // namespace crashpad |
+bool LoggingCloseFile(FileHandle file) { |
+ BOOL rv = CloseHandle(file); |
+ PLOG_IF(ERROR, !rv) << "CloseHandle"; |
+ return rv; |
+} |
-#endif // CRASHPAD_UTIL_NUMERIC_IN_RANGE_CAST_H_ |
+} // namespace crashpad |