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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 total_bytes += bytes; | 62 total_bytes += bytes; |
63 } | 63 } |
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 { |
| 73 |
| 74 FileHandle LoggingOpenFileForOutput(int rdwr_or_wronly, |
| 75 const base::FilePath& path, |
| 76 FileWriteMode mode, |
| 77 FilePermissions permissions) { |
| 78 int flags = rdwr_or_wronly | O_CREAT; |
| 79 // kReuseOrCreate does not need any additional flags. |
| 80 if (mode == FileWriteMode::kTruncateOrCreate) |
| 81 flags |= O_TRUNC; |
| 82 else if (mode == FileWriteMode::kCreateOrFail) |
| 83 flags |= O_EXCL; |
| 84 |
| 85 int fd = HANDLE_EINTR( |
| 86 open(path.value().c_str(), |
| 87 flags, |
| 88 permissions == FilePermissions::kWorldReadable ? 0644 : 0600)); |
| 89 PLOG_IF(ERROR, fd < 0) << "open " << path.value(); |
| 90 return fd; |
| 91 } |
| 92 |
| 93 } // namespace |
| 94 |
72 ssize_t ReadFile(FileHandle file, void* buffer, size_t size) { | 95 ssize_t ReadFile(FileHandle file, void* buffer, size_t size) { |
73 return ReadOrWrite<ReadTraits>(file, buffer, size); | 96 return ReadOrWrite<ReadTraits>(file, buffer, size); |
74 } | 97 } |
75 | 98 |
76 ssize_t WriteFile(FileHandle file, const void* buffer, size_t size) { | 99 ssize_t WriteFile(FileHandle file, const void* buffer, size_t size) { |
77 return ReadOrWrite<WriteTraits>(file, buffer, size); | 100 return ReadOrWrite<WriteTraits>(file, buffer, size); |
78 } | 101 } |
79 | 102 |
80 FileHandle LoggingOpenFileForRead(const base::FilePath& path) { | 103 FileHandle LoggingOpenFileForRead(const base::FilePath& path) { |
81 int fd = HANDLE_EINTR(open(path.value().c_str(), O_RDONLY)); | 104 int fd = HANDLE_EINTR(open(path.value().c_str(), O_RDONLY)); |
82 PLOG_IF(ERROR, fd < 0) << "open " << path.value(); | 105 PLOG_IF(ERROR, fd < 0) << "open " << path.value(); |
83 return fd; | 106 return fd; |
84 } | 107 } |
85 | 108 |
86 FileHandle LoggingOpenFileForWrite(const base::FilePath& path, | 109 FileHandle LoggingOpenFileForWrite(const base::FilePath& path, |
87 FileWriteMode mode, | 110 FileWriteMode mode, |
88 FilePermissions permissions) { | 111 FilePermissions permissions) { |
89 int flags = O_WRONLY | O_CREAT; | 112 return LoggingOpenFileForOutput(O_WRONLY, path, mode, permissions); |
90 // kReuseOrCreate does not need any additional flags. | 113 } |
91 if (mode == FileWriteMode::kTruncateOrCreate) | |
92 flags |= O_TRUNC; | |
93 else if (mode == FileWriteMode::kCreateOrFail) | |
94 flags |= O_EXCL; | |
95 | 114 |
96 int fd = HANDLE_EINTR( | 115 FileHandle LoggingOpenFileForReadAndWrite(const base::FilePath& path, |
97 open(path.value().c_str(), | 116 FileWriteMode mode, |
98 flags, | 117 FilePermissions permissions) { |
99 permissions == FilePermissions::kWorldReadable ? 0644 : 0600)); | 118 return LoggingOpenFileForOutput(O_RDWR, path, mode, permissions); |
100 PLOG_IF(ERROR, fd < 0) << "open " << path.value(); | |
101 return fd; | |
102 } | 119 } |
103 | 120 |
104 bool LoggingLockFile(FileHandle file, FileLocking locking) { | 121 bool LoggingLockFile(FileHandle file, FileLocking locking) { |
105 int operation = (locking == FileLocking::kShared) ? LOCK_SH : LOCK_EX; | 122 int operation = (locking == FileLocking::kShared) ? LOCK_SH : LOCK_EX; |
106 int rv = HANDLE_EINTR(flock(file, operation)); | 123 int rv = HANDLE_EINTR(flock(file, operation)); |
107 PLOG_IF(ERROR, rv != 0) << "flock"; | 124 PLOG_IF(ERROR, rv != 0) << "flock"; |
108 return rv == 0; | 125 return rv == 0; |
109 } | 126 } |
110 | 127 |
111 bool LoggingUnlockFile(FileHandle file) { | 128 bool LoggingUnlockFile(FileHandle file) { |
112 int rv = flock(file, LOCK_UN); | 129 int rv = flock(file, LOCK_UN); |
113 PLOG_IF(ERROR, rv != 0) << "flock"; | 130 PLOG_IF(ERROR, rv != 0) << "flock"; |
114 return rv == 0; | 131 return rv == 0; |
115 } | 132 } |
116 | 133 |
117 FileOffset LoggingSeekFile(FileHandle file, FileOffset offset, int whence) { | 134 FileOffset LoggingSeekFile(FileHandle file, FileOffset offset, int whence) { |
118 off_t rv = lseek(file, offset, whence); | 135 off_t rv = lseek(file, offset, whence); |
119 PLOG_IF(ERROR, rv < 0) << "lseek"; | 136 PLOG_IF(ERROR, rv < 0) << "lseek"; |
120 return rv; | 137 return rv; |
121 } | 138 } |
122 | 139 |
| 140 bool LoggingTruncateFile(FileHandle file) { |
| 141 if (HANDLE_EINTR(ftruncate(file, 0)) != 0) { |
| 142 PLOG(ERROR) << "ftruncate"; |
| 143 return false; |
| 144 } |
| 145 return true; |
| 146 } |
| 147 |
123 bool LoggingCloseFile(FileHandle file) { | 148 bool LoggingCloseFile(FileHandle file) { |
124 int rv = IGNORE_EINTR(close(file)); | 149 int rv = IGNORE_EINTR(close(file)); |
125 PLOG_IF(ERROR, rv != 0) << "close"; | 150 PLOG_IF(ERROR, rv != 0) << "close"; |
126 return rv == 0; | 151 return rv == 0; |
127 } | 152 } |
128 | 153 |
129 } // namespace crashpad | 154 } // namespace crashpad |
OLD | NEW |