OLD | NEW |
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, |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 | 64 |
65 return total_bytes; | 65 return total_bytes; |
66 } | 66 } |
67 | 67 |
68 } // namespace | 68 } // namespace |
69 | 69 |
70 namespace crashpad { | 70 namespace crashpad { |
71 | 71 |
72 namespace { | 72 namespace { |
73 | 73 |
74 FileHandle LoggingOpenFileForOutput(int rdwr_or_wronly, | 74 FileHandle OpenFileForOutput(int rdwr_or_wronly, |
75 const base::FilePath& path, | 75 const base::FilePath& path, |
76 FileWriteMode mode, | 76 FileWriteMode mode, |
77 FilePermissions permissions) { | 77 FilePermissions permissions) { |
78 int flags = rdwr_or_wronly & (O_RDWR | O_WRONLY); | 78 DCHECK(rdwr_or_wronly & (O_RDWR | O_WRONLY)); |
79 DCHECK_NE(flags, 0); | 79 DCHECK_EQ(rdwr_or_wronly & ~(O_RDWR | O_WRONLY), 0); |
| 80 |
| 81 int flags = rdwr_or_wronly; |
80 | 82 |
81 switch (mode) { | 83 switch (mode) { |
82 case FileWriteMode::kReuseOrFail: | 84 case FileWriteMode::kReuseOrFail: |
83 break; | 85 break; |
84 case FileWriteMode::kReuseOrCreate: | 86 case FileWriteMode::kReuseOrCreate: |
85 flags |= O_CREAT; | 87 flags |= O_CREAT; |
86 break; | 88 break; |
87 case FileWriteMode::kTruncateOrCreate: | 89 case FileWriteMode::kTruncateOrCreate: |
88 flags |= O_CREAT | O_TRUNC; | 90 flags |= O_CREAT | O_TRUNC; |
89 break; | 91 break; |
90 case FileWriteMode::kCreateOrFail: | 92 case FileWriteMode::kCreateOrFail: |
91 flags |= O_CREAT | O_EXCL; | 93 flags |= O_CREAT | O_EXCL; |
92 break; | 94 break; |
93 } | 95 } |
94 | 96 |
95 int fd = HANDLE_EINTR( | 97 return HANDLE_EINTR( |
96 open(path.value().c_str(), | 98 open(path.value().c_str(), |
97 flags, | 99 flags, |
98 permissions == FilePermissions::kWorldReadable ? 0644 : 0600)); | 100 permissions == FilePermissions::kWorldReadable ? 0644 : 0600)); |
99 PLOG_IF(ERROR, fd < 0) << "open " << path.value(); | |
100 return fd; | |
101 } | 101 } |
102 | 102 |
103 } // namespace | 103 } // namespace |
104 | 104 |
105 ssize_t ReadFile(FileHandle file, void* buffer, size_t size) { | 105 ssize_t ReadFile(FileHandle file, void* buffer, size_t size) { |
106 return ReadOrWrite<ReadTraits>(file, buffer, size); | 106 return ReadOrWrite<ReadTraits>(file, buffer, size); |
107 } | 107 } |
108 | 108 |
109 ssize_t WriteFile(FileHandle file, const void* buffer, size_t size) { | 109 ssize_t WriteFile(FileHandle file, const void* buffer, size_t size) { |
110 return ReadOrWrite<WriteTraits>(file, buffer, size); | 110 return ReadOrWrite<WriteTraits>(file, buffer, size); |
111 } | 111 } |
112 | 112 |
| 113 FileHandle OpenFileForRead(const base::FilePath& path) { |
| 114 return HANDLE_EINTR(open(path.value().c_str(), O_RDONLY)); |
| 115 } |
| 116 |
| 117 FileHandle OpenFileForWrite(const base::FilePath& path, |
| 118 FileWriteMode mode, |
| 119 FilePermissions permissions) { |
| 120 return OpenFileForOutput(O_WRONLY, path, mode, permissions); |
| 121 } |
| 122 |
| 123 FileHandle OpenFileForReadAndWrite(const base::FilePath& path, |
| 124 FileWriteMode mode, |
| 125 FilePermissions permissions) { |
| 126 return OpenFileForOutput(O_RDWR, path, mode, permissions); |
| 127 } |
| 128 |
113 FileHandle LoggingOpenFileForRead(const base::FilePath& path) { | 129 FileHandle LoggingOpenFileForRead(const base::FilePath& path) { |
114 int fd = HANDLE_EINTR(open(path.value().c_str(), O_RDONLY)); | 130 FileHandle fd = OpenFileForRead(path); |
115 PLOG_IF(ERROR, fd < 0) << "open " << path.value(); | 131 PLOG_IF(ERROR, fd < 0) << "open " << path.value(); |
116 return fd; | 132 return fd; |
117 } | 133 } |
118 | 134 |
119 FileHandle LoggingOpenFileForWrite(const base::FilePath& path, | 135 FileHandle LoggingOpenFileForWrite(const base::FilePath& path, |
120 FileWriteMode mode, | 136 FileWriteMode mode, |
121 FilePermissions permissions) { | 137 FilePermissions permissions) { |
122 return LoggingOpenFileForOutput(O_WRONLY, path, mode, permissions); | 138 FileHandle fd = OpenFileForWrite(path, mode, permissions); |
| 139 PLOG_IF(ERROR, fd < 0) << "open " << path.value(); |
| 140 return fd; |
123 } | 141 } |
124 | 142 |
125 FileHandle LoggingOpenFileForReadAndWrite(const base::FilePath& path, | 143 FileHandle LoggingOpenFileForReadAndWrite(const base::FilePath& path, |
126 FileWriteMode mode, | 144 FileWriteMode mode, |
127 FilePermissions permissions) { | 145 FilePermissions permissions) { |
128 return LoggingOpenFileForOutput(O_RDWR, path, mode, permissions); | 146 FileHandle fd = OpenFileForReadAndWrite(path, mode, permissions); |
| 147 PLOG_IF(ERROR, fd < 0) << "open " << path.value(); |
| 148 return fd; |
129 } | 149 } |
130 | 150 |
131 bool LoggingLockFile(FileHandle file, FileLocking locking) { | 151 bool LoggingLockFile(FileHandle file, FileLocking locking) { |
132 int operation = (locking == FileLocking::kShared) ? LOCK_SH : LOCK_EX; | 152 int operation = (locking == FileLocking::kShared) ? LOCK_SH : LOCK_EX; |
133 int rv = HANDLE_EINTR(flock(file, operation)); | 153 int rv = HANDLE_EINTR(flock(file, operation)); |
134 PLOG_IF(ERROR, rv != 0) << "flock"; | 154 PLOG_IF(ERROR, rv != 0) << "flock"; |
135 return rv == 0; | 155 return rv == 0; |
136 } | 156 } |
137 | 157 |
138 bool LoggingUnlockFile(FileHandle file) { | 158 bool LoggingUnlockFile(FileHandle file) { |
(...skipping 16 matching lines...) Expand all Loading... |
155 return true; | 175 return true; |
156 } | 176 } |
157 | 177 |
158 bool LoggingCloseFile(FileHandle file) { | 178 bool LoggingCloseFile(FileHandle file) { |
159 int rv = IGNORE_EINTR(close(file)); | 179 int rv = IGNORE_EINTR(close(file)); |
160 PLOG_IF(ERROR, rv != 0) << "close"; | 180 PLOG_IF(ERROR, rv != 0) << "close"; |
161 return rv == 0; | 181 return rv == 0; |
162 } | 182 } |
163 | 183 |
164 } // namespace crashpad | 184 } // namespace crashpad |
OLD | NEW |