blob: 63edcb5aed15f36ad1148e835fb2ef087df65f27 [file] [log] [blame]
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +02001/*
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/file.h"
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +020012
13#include <io.h>
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "rtc_base/win32.h"
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +020015
16#include <limits> // NOLINT: win32.h should be considered a system header
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/checks.h"
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +020019
20namespace rtc {
21
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +020022size_t File::Write(const uint8_t* data, size_t length) {
23 RTC_DCHECK_LT(length, std::numeric_limits<DWORD>::max());
24 size_t total_written = 0;
25 do {
26 DWORD written;
27 if (!::WriteFile(file_, data + total_written,
28 static_cast<DWORD>(length - total_written), &written,
29 nullptr)) {
30 break;
31 }
32 total_written += written;
33 } while (total_written < length);
34 return total_written;
35}
36
37size_t File::Read(uint8_t* buffer, size_t length) {
38 RTC_DCHECK_LT(length, std::numeric_limits<DWORD>::max());
39 size_t total_read = 0;
40 do {
41 DWORD read;
42 if (!::ReadFile(file_, buffer + total_read,
43 static_cast<DWORD>(length - total_read), &read, nullptr)) {
44 break;
45 }
46 total_read += read;
47 } while (total_read < length);
48 return total_read;
49}
50
51size_t File::WriteAt(const uint8_t* data, size_t length, size_t offset) {
52 RTC_DCHECK_LT(length, std::numeric_limits<DWORD>::max());
53 size_t total_written = 0;
54 do {
55 DWORD written;
56
57 LARGE_INTEGER offset_li;
58 offset_li.QuadPart = offset + total_written;
59
60 OVERLAPPED overlapped = {0};
61 overlapped.Offset = offset_li.LowPart;
62 overlapped.OffsetHigh = offset_li.HighPart;
63
64 if (!::WriteFile(file_, data + total_written,
65 static_cast<DWORD>(length - total_written), &written,
66 &overlapped)) {
67 break;
68 }
69
70 total_written += written;
71 } while (total_written < length);
72 return total_written;
73}
74
75size_t File::ReadAt(uint8_t* buffer, size_t length, size_t offset) {
76 RTC_DCHECK_LT(length, std::numeric_limits<DWORD>::max());
77 size_t total_read = 0;
78 do {
79 DWORD read;
80
81 LARGE_INTEGER offset_li;
82 offset_li.QuadPart = offset + total_read;
83
84 OVERLAPPED overlapped = {0};
85 overlapped.Offset = offset_li.LowPart;
86 overlapped.OffsetHigh = offset_li.HighPart;
87
88 if (!::ReadFile(file_, buffer + total_read,
89 static_cast<DWORD>(length - total_read), &read,
90 &overlapped)) {
91 break;
92 }
93
94 total_read += read;
95 } while (total_read < length);
96 return total_read;
97}
98
99bool File::Seek(size_t offset) {
100 LARGE_INTEGER distance;
101 distance.QuadPart = offset;
102 return SetFilePointerEx(file_, distance, nullptr, FILE_BEGIN) != 0;
103}
104
105bool File::Close() {
106 if (file_ == kInvalidPlatformFileValue)
107 return false;
108 bool ret = CloseHandle(file_) != 0;
109 file_ = kInvalidPlatformFileValue;
110 return ret;
111}
112
113} // namespace rtc