blob: 72a680c88f4b14a7e91fee8d6b6f02b4ff1bb659 [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
11#include "webrtc/base/file.h"
12
13#include <io.h>
14#include "webrtc/base/win32.h"
15
16#include <limits> // NOLINT: win32.h should be considered a system header
17
18#include "webrtc/base/checks.h"
19
20namespace rtc {
21
22File File::Open(const std::string& path) {
23 HANDLE handle =
24 ::CreateFile(ToUtf16(path).c_str(), GENERIC_READ | GENERIC_WRITE, 0,
25 nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
26 return File(handle);
27}
28
29size_t File::Write(const uint8_t* data, size_t length) {
30 RTC_DCHECK_LT(length, std::numeric_limits<DWORD>::max());
31 size_t total_written = 0;
32 do {
33 DWORD written;
34 if (!::WriteFile(file_, data + total_written,
35 static_cast<DWORD>(length - total_written), &written,
36 nullptr)) {
37 break;
38 }
39 total_written += written;
40 } while (total_written < length);
41 return total_written;
42}
43
44size_t File::Read(uint8_t* buffer, size_t length) {
45 RTC_DCHECK_LT(length, std::numeric_limits<DWORD>::max());
46 size_t total_read = 0;
47 do {
48 DWORD read;
49 if (!::ReadFile(file_, buffer + total_read,
50 static_cast<DWORD>(length - total_read), &read, nullptr)) {
51 break;
52 }
53 total_read += read;
54 } while (total_read < length);
55 return total_read;
56}
57
58size_t File::WriteAt(const uint8_t* data, size_t length, size_t offset) {
59 RTC_DCHECK_LT(length, std::numeric_limits<DWORD>::max());
60 size_t total_written = 0;
61 do {
62 DWORD written;
63
64 LARGE_INTEGER offset_li;
65 offset_li.QuadPart = offset + total_written;
66
67 OVERLAPPED overlapped = {0};
68 overlapped.Offset = offset_li.LowPart;
69 overlapped.OffsetHigh = offset_li.HighPart;
70
71 if (!::WriteFile(file_, data + total_written,
72 static_cast<DWORD>(length - total_written), &written,
73 &overlapped)) {
74 break;
75 }
76
77 total_written += written;
78 } while (total_written < length);
79 return total_written;
80}
81
82size_t File::ReadAt(uint8_t* buffer, size_t length, size_t offset) {
83 RTC_DCHECK_LT(length, std::numeric_limits<DWORD>::max());
84 size_t total_read = 0;
85 do {
86 DWORD read;
87
88 LARGE_INTEGER offset_li;
89 offset_li.QuadPart = offset + total_read;
90
91 OVERLAPPED overlapped = {0};
92 overlapped.Offset = offset_li.LowPart;
93 overlapped.OffsetHigh = offset_li.HighPart;
94
95 if (!::ReadFile(file_, buffer + total_read,
96 static_cast<DWORD>(length - total_read), &read,
97 &overlapped)) {
98 break;
99 }
100
101 total_read += read;
102 } while (total_read < length);
103 return total_read;
104}
105
106bool File::Seek(size_t offset) {
107 LARGE_INTEGER distance;
108 distance.QuadPart = offset;
109 return SetFilePointerEx(file_, distance, nullptr, FILE_BEGIN) != 0;
110}
111
112bool File::Close() {
113 if (file_ == kInvalidPlatformFileValue)
114 return false;
115 bool ret = CloseHandle(file_) != 0;
116 file_ = kInvalidPlatformFileValue;
117 return ret;
118}
119
120} // namespace rtc