Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef FD_BUFFER_H |
| 18 | #define FD_BUFFER_H |
| 19 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 20 | #include <utils/Errors.h> |
| 21 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
| 24 | using namespace android; |
| 25 | using namespace std; |
| 26 | |
| 27 | /** |
| 28 | * Reads a file into a buffer, and then writes that data to an FdSet. |
| 29 | */ |
| 30 | class FdBuffer |
| 31 | { |
| 32 | public: |
| 33 | FdBuffer(); |
| 34 | ~FdBuffer(); |
| 35 | |
| 36 | /** |
| 37 | * Read the data until the timeout is hit or we hit eof. |
| 38 | * Returns NO_ERROR if there were no errors or if we timed out. |
| 39 | * Will mark the file O_NONBLOCK. |
| 40 | */ |
| 41 | status_t read(int fd, int64_t timeoutMs); |
| 42 | |
| 43 | /** |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 44 | * Read processed results by streaming data to a parsing process, e.g. incident helper. |
| 45 | * The parsing process provides IO fds which are 'toFd' and 'fromFd'. The function |
| 46 | * reads original data in 'fd' and writes to parsing process through 'toFd', then it reads |
| 47 | * and stores the processed data from 'fromFd' in memory for later usage. |
| 48 | * This function behaves in a streaming fashion in order to save memory usage. |
| 49 | * Returns NO_ERROR if there were no errors or if we timed out. |
| 50 | */ |
| 51 | status_t readProcessedDataInStream(int fd, int toFd, int fromFd, int64_t timeoutMs); |
| 52 | |
| 53 | /** |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 54 | * Whether we timed out. |
| 55 | */ |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 56 | bool timedOut() const { return mTimedOut; } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * If more than 4 MB is read, we truncate the data and return success. |
| 60 | * Downstream tools must handle truncated incident reports as best as possible |
| 61 | * anyway because they could be cut off for a lot of reasons and it's best |
| 62 | * to get as much useful information out of the system as possible. If this |
| 63 | * happens, truncated() will return true so it can be marked. If the data is |
| 64 | * exactly 4 MB, truncated is still set. Sorry. |
| 65 | */ |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 66 | bool truncated() const { return mTruncated; } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * How much data was read. |
| 70 | */ |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 71 | size_t size() const; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 72 | |
| 73 | /** |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 74 | * Flush all the data to given file descriptor; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 75 | */ |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 76 | status_t flush(int fd) const; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 77 | |
| 78 | /** |
| 79 | * How long the read took in milliseconds. |
| 80 | */ |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 81 | int64_t durationMs() const { return mFinishTime - mStartTime; } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 82 | |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 83 | /** |
| 84 | * Read data stored in FdBuffer |
| 85 | */ |
| 86 | class iterator; |
| 87 | friend class iterator; |
| 88 | class iterator : public std::iterator<std::random_access_iterator_tag, uint8_t> { |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 89 | public: |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame^] | 90 | iterator(const FdBuffer& buffer, ssize_t index, ssize_t offset); |
| 91 | iterator& operator=(iterator& other) const; |
| 92 | iterator& operator+(size_t offset); |
| 93 | iterator& operator+=(size_t offset); |
| 94 | iterator& operator++(); |
| 95 | iterator operator++(int); |
| 96 | bool operator==(iterator other) const; |
| 97 | bool operator!=(iterator other) const; |
| 98 | int operator-(iterator other) const; |
| 99 | reference operator*() const; |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 100 | |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 101 | // return the snapshot of the current iterator |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame^] | 102 | iterator snapshot() const; |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 103 | // how many bytes are read |
| 104 | size_t bytesRead() const; |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 105 | // random access could make the iterator out of bound |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame^] | 106 | bool outOfBound() const; |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 107 | private: |
| 108 | const FdBuffer& mFdBuffer; |
| 109 | size_t mIndex; |
| 110 | size_t mOffset; |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 111 | }; |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame^] | 112 | iterator begin() const; |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 113 | iterator end() const; |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 114 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 115 | private: |
| 116 | vector<uint8_t*> mBuffers; |
| 117 | int64_t mStartTime; |
| 118 | int64_t mFinishTime; |
| 119 | ssize_t mCurrentWritten; |
| 120 | bool mTimedOut; |
| 121 | bool mTruncated; |
| 122 | }; |
| 123 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 124 | #endif // FD_BUFFER_H |