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

Side by Side Diff: util/file/fd_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
(Empty)
1 // Copyright 2014 The Crashpad Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "util/file/fd_io.h"
16
17 #include <unistd.h>
18
19 #include "base/logging.h"
20 #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
67 namespace crashpad {
68
69 ssize_t ReadFD(int fd, 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);
79 ssize_t rv = ReadFD(fd, buffer, size);
80 if (rv < 0) {
81 PLOG(ERROR) << "read";
82 return false;
83 }
84 if (rv != expect) {
85 LOG(ERROR) << "read: expected " << expect << ", observed " << rv;
86 return false;
87 }
88
89 return true;
90 }
91
92 bool LoggingWriteFD(int fd, const void* buffer, size_t size) {
93 ssize_t expect = base::checked_cast<ssize_t>(size);
94 ssize_t rv = WriteFD(fd, buffer, size);
95 if (rv < 0) {
96 PLOG(ERROR) << "write";
97 return false;
98 }
99 if (rv != expect) {
100 LOG(ERROR) << "write: expected " << expect << ", observed " << rv;
101 return false;
102 }
103
104 return true;
105 }
106
107 void CheckedReadFD(int fd, void* buffer, size_t size) {
108 CHECK(LoggingReadFD(fd, buffer, size));
109 }
110
111 void CheckedWriteFD(int fd, const void* buffer, size_t size) {
112 CHECK(LoggingWriteFD(fd, buffer, size));
113 }
114
115 void CheckedReadFDAtEOF(int fd) {
116 char c;
117 ssize_t rv = ReadFD(fd, &c, 1);
118 if (rv < 0) {
119 PCHECK(rv == 0) << "read";
120 } else {
121 CHECK_EQ(rv, 0) << "read";
122 }
123 }
124
125 bool LoggingCloseFD(int fd) {
126 int rv = IGNORE_EINTR(close(fd));
127 PLOG_IF(ERROR, rv != 0) << "close";
128 return rv == 0;
129 }
130
131 void CheckedCloseFD(int fd) {
132 CHECK(LoggingCloseFD(fd));
133 }
134
135 } // namespace crashpad
OLDNEW
« no previous file with comments | « util/file/fd_io.h ('k') | util/file/file_io.h » ('j') | util/file/file_io.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698