Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1118)

Unified Diff: util/file/file_io_win.cc

Issue 811823003: Cross platform low level file IO wrappers (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: various fixes Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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 42%
copy from util/numeric/in_range_cast.h
copy to util/file/file_io_win.cc
index 3dba10afa16016bb7149cd1fa7b4ae14c72cfca2..de13219c0f287c516abea6efeaba8ffaef178c26 100644
--- a/util/numeric/in_range_cast.h
+++ b/util/file/file_io_win.cc
@@ -12,33 +12,44 @@
// 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);
- }
-
- LOG(WARNING) << "value " << source << " out of range";
- return static_cast<Destination>(default_value);
+// 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;
+ BOOL success;
+ do {
+ DWORD bytes_read;
+ success = ::ReadFile(file, buffer, size_dword, &bytes_read, nullptr);
Mark Mentovai 2014/12/17 01:05:24 On the second pass, buffer needs to point beyond i
scottmg 2014/12/17 01:50:27 Oh boy! Hopefully tests would have saved me eventu
+ if (!success && GetLastError() != ERROR_MORE_DATA)
Mark Mentovai 2014/12/17 01:05:24 ReadFile()’s docs say that you’ll get ERROR_MORE_D
scottmg 2014/12/17 01:50:28 Raymond gets upset when Windows programmers poke a
Mark Mentovai 2014/12/17 05:39:16 scottmg wrote:
+ return -1;
+ total_read += bytes_read;
+ } while (!success);
+ return total_read;
}
-} // namespace crashpad
+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);
Mark Mentovai 2014/12/17 01:05:24 WriteFile()’s docs aren’t 100% convincing about sh
scottmg 2014/12/17 01:50:28 SG. The link above is also clear that it cannot su
+ if (!rv)
+ return -1;
+ return bytes_written;
Mark Mentovai 2014/12/17 01:05:24 …so let’s make it explicit and [D]CHECK_EQ(bytes_w
scottmg 2014/12/17 01:50:28 Done.
+}
-#endif // CRASHPAD_UTIL_NUMERIC_IN_RANGE_CAST_H_
+bool LoggingCloseFile(FileHandle file) {
+ BOOL rv = CloseHandle(file);
+ PLOG_IF(ERROR, !rv) << "CloseHandle";
+ return rv;
+}
+
+} // namespace crashpad
« util/file/file_io.h ('K') | « util/file/file_io_posix.cc ('k') | util/file/file_writer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698