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

Side by Side Diff: util/file/file_io.cc

Issue 811823003: Cross platform low level file IO wrappers (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: . 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 unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Crashpad Authors. All rights reserved. 1 // Copyright 2014 The Crashpad Authors. All rights reserved.
2 // 2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License. 4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at 5 // You may obtain a copy of the License at
6 // 6 //
7 // http://www.apache.org/licenses/LICENSE-2.0 7 // http://www.apache.org/licenses/LICENSE-2.0
8 // 8 //
9 // Unless required by applicable law or agreed to in writing, software 9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, 10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and 12 // See the License for the specific language governing permissions and
13 // limitations under the License. 13 // limitations under the License.
14 14
15 #include "util/file/fd_io.h" 15 #include "util/file/file_io.h"
16
17 #include <unistd.h>
18 16
19 #include "base/logging.h" 17 #include "base/logging.h"
20 #include "base/numerics/safe_conversions.h" 18 #include "base/numerics/safe_conversions.h"
21 #include "base/posix/eintr_wrapper.h"
22
23 namespace {
24
25 struct ReadTraits {
26 using VoidBufferType = void*;
27 using CharBufferType = char*;
28 static ssize_t Operate(int fd, CharBufferType buffer, size_t size) {
29 return read(fd, buffer, size);
30 }
31 };
32
33 struct WriteTraits {
34 using VoidBufferType = const void*;
35 using CharBufferType = const char*;
36 static ssize_t Operate(int fd, CharBufferType buffer, size_t size) {
37 return write(fd, buffer, size);
38 }
39 };
40
41 template <typename Traits>
42 ssize_t ReadOrWrite(int fd,
43 typename Traits::VoidBufferType buffer,
44 size_t size) {
45 typename Traits::CharBufferType buffer_c =
46 reinterpret_cast<typename Traits::CharBufferType>(buffer);
47
48 ssize_t total_bytes = 0;
49 while (size > 0) {
50 ssize_t bytes = HANDLE_EINTR(Traits::Operate(fd, buffer_c, size));
51 if (bytes < 0) {
52 return bytes;
53 } else if (bytes == 0) {
54 break;
55 }
56
57 buffer_c += bytes;
58 size -= bytes;
59 total_bytes += bytes;
60 }
61
62 return total_bytes;
63 }
64
65 } // namespace
66 19
67 namespace crashpad { 20 namespace crashpad {
68 21
69 ssize_t ReadFD(int fd, void* buffer, size_t size) { 22 bool LoggingReadFile(FileHandle file, void* buffer, size_t size) {
70 return ReadOrWrite<ReadTraits>(fd, buffer, size);
71 }
72
73 ssize_t WriteFD(int fd, const void* buffer, size_t size) {
74 return ReadOrWrite<WriteTraits>(fd, buffer, size);
75 }
76
77 bool LoggingReadFD(int fd, void* buffer, size_t size) {
78 ssize_t expect = base::checked_cast<ssize_t>(size); 23 ssize_t expect = base::checked_cast<ssize_t>(size);
79 ssize_t rv = ReadFD(fd, buffer, size); 24 ssize_t rv = ReadFile(file, buffer, size);
80 if (rv < 0) { 25 if (rv < 0) {
81 PLOG(ERROR) << "read"; 26 PLOG(ERROR) << "read";
82 return false; 27 return false;
83 } 28 }
84 if (rv != expect) { 29 if (rv != expect) {
85 LOG(ERROR) << "read: expected " << expect << ", observed " << rv; 30 LOG(ERROR) << "read: expected " << expect << ", observed " << rv;
86 return false; 31 return false;
87 } 32 }
88 33
89 return true; 34 return true;
90 } 35 }
91 36
92 bool LoggingWriteFD(int fd, const void* buffer, size_t size) { 37 bool LoggingWriteFile(FileHandle file, const void* buffer, size_t size) {
93 ssize_t expect = base::checked_cast<ssize_t>(size); 38 ssize_t expect = base::checked_cast<ssize_t>(size);
94 ssize_t rv = WriteFD(fd, buffer, size); 39 ssize_t rv = WriteFile(file, buffer, size);
95 if (rv < 0) { 40 if (rv < 0) {
96 PLOG(ERROR) << "write"; 41 PLOG(ERROR) << "write";
97 return false; 42 return false;
98 } 43 }
99 if (rv != expect) { 44 if (rv != expect) {
100 LOG(ERROR) << "write: expected " << expect << ", observed " << rv; 45 LOG(ERROR) << "write: expected " << expect << ", observed " << rv;
101 return false; 46 return false;
102 } 47 }
103 48
104 return true; 49 return true;
105 } 50 }
106 51
107 void CheckedReadFD(int fd, void* buffer, size_t size) { 52 void CheckedReadFile(FileHandle file, void* buffer, size_t size) {
108 CHECK(LoggingReadFD(fd, buffer, size)); 53 CHECK(LoggingReadFile(file, buffer, size));
109 } 54 }
110 55
111 void CheckedWriteFD(int fd, const void* buffer, size_t size) { 56 void CheckedWriteFile(FileHandle file, const void* buffer, size_t size) {
112 CHECK(LoggingWriteFD(fd, buffer, size)); 57 CHECK(LoggingWriteFile(file, buffer, size));
113 } 58 }
114 59
115 void CheckedReadFDAtEOF(int fd) { 60 void CheckedReadFileAtEOF(FileHandle file) {
116 char c; 61 char c;
117 ssize_t rv = ReadFD(fd, &c, 1); 62 ssize_t rv = ReadFile(file, &c, 1);
118 if (rv < 0) { 63 if (rv < 0) {
119 PCHECK(rv == 0) << "read"; 64 PCHECK(rv == 0) << "read";
120 } else { 65 } else {
121 CHECK_EQ(rv, 0) << "read"; 66 CHECK_EQ(rv, 0) << "read";
122 } 67 }
123 } 68 }
124 69
125 bool LoggingCloseFD(int fd) { 70 void CheckedCloseFile(FileHandle file) {
126 int rv = IGNORE_EINTR(close(fd)); 71 CHECK(LoggingCloseFile(file));
127 PLOG_IF(ERROR, rv != 0) << "close";
128 return rv == 0;
129 }
130
131 void CheckedCloseFD(int fd) {
132 CHECK(LoggingCloseFD(fd));
133 } 72 }
134 73
135 } // namespace crashpad 74 } // namespace crashpad
OLDNEW
« util/file/file_io.h ('K') | « util/file/file_io.h ('k') | util/file/file_io_posix.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698