blob: 4c4823e5a4d8833a20e29165e2818110bac7a663 [file] [log] [blame]
Joe Onorato1754d742016-11-21 17:51:35 -08001/*
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 Onorato1754d742016-11-21 17:51:35 -080020#include <utils/Errors.h>
21
Joe Onorato1754d742016-11-21 17:51:35 -080022#include <vector>
23
24using namespace android;
25using namespace std;
26
27/**
28 * Reads a file into a buffer, and then writes that data to an FdSet.
29 */
30class FdBuffer
31{
32public:
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 Jin0a3406f2017-06-22 19:23:11 -070044 * 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 Onorato1754d742016-11-21 17:51:35 -080054 * Whether we timed out.
55 */
Yi Jin99c248f2017-08-25 18:11:58 -070056 bool timedOut() const { return mTimedOut; }
Joe Onorato1754d742016-11-21 17:51:35 -080057
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 Jin99c248f2017-08-25 18:11:58 -070066 bool truncated() const { return mTruncated; }
Joe Onorato1754d742016-11-21 17:51:35 -080067
68 /**
69 * How much data was read.
70 */
Yi Jin99c248f2017-08-25 18:11:58 -070071 size_t size() const;
Joe Onorato1754d742016-11-21 17:51:35 -080072
73 /**
Yi Jin99c248f2017-08-25 18:11:58 -070074 * Flush all the data to given file descriptor;
Joe Onorato1754d742016-11-21 17:51:35 -080075 */
Yi Jin99c248f2017-08-25 18:11:58 -070076 status_t flush(int fd) const;
Joe Onorato1754d742016-11-21 17:51:35 -080077
78 /**
79 * How long the read took in milliseconds.
80 */
Yi Jin99c248f2017-08-25 18:11:58 -070081 int64_t durationMs() const { return mFinishTime - mStartTime; }
Joe Onorato1754d742016-11-21 17:51:35 -080082
Yi Jin0ed9b682017-08-18 14:51:20 -070083 /**
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 Jin0ed9b682017-08-18 14:51:20 -070089 public:
Yi Jin99c248f2017-08-25 18:11:58 -070090 iterator(const FdBuffer& buffer, ssize_t index, ssize_t offset)
Yi Jin0ed9b682017-08-18 14:51:20 -070091 : mFdBuffer(buffer), mIndex(index), mOffset(offset) {}
Yi Jin99c248f2017-08-25 18:11:58 -070092 iterator& operator=(iterator& other) const { return other; }
Yi Jin0ed9b682017-08-18 14:51:20 -070093 iterator& operator+(size_t offset); // this is implemented in .cpp
94 iterator& operator+=(size_t offset) { return *this + offset; }
95 iterator& operator++() { return *this + 1; }
96 iterator operator++(int) { return *this + 1; }
97 bool operator==(iterator other) const {
98 return mIndex == other.mIndex && mOffset == other.mOffset;
99 }
100 bool operator!=(iterator other) const { return !(*this == other); }
Yi Jin99c248f2017-08-25 18:11:58 -0700101 int operator-(iterator other) const { return (int)bytesRead() - (int)other.bytesRead(); }
Yi Jin0ed9b682017-08-18 14:51:20 -0700102 reference operator*() const { return mFdBuffer.mBuffers[mIndex][mOffset]; }
103
Yi Jin99c248f2017-08-25 18:11:58 -0700104 // return the snapshot of the current iterator
105 iterator snapshot() const { return iterator(mFdBuffer, mIndex, mOffset); }
106 // how many bytes are read
107 size_t bytesRead() const;
Yi Jin0ed9b682017-08-18 14:51:20 -0700108 // random access could make the iterator out of bound
Yi Jin99c248f2017-08-25 18:11:58 -0700109 bool outOfBound() const { return bytesRead() > mFdBuffer.size(); }
110 private:
111 const FdBuffer& mFdBuffer;
112 size_t mIndex;
113 size_t mOffset;
Yi Jin0ed9b682017-08-18 14:51:20 -0700114 };
Yi Jin99c248f2017-08-25 18:11:58 -0700115 iterator begin() const { return iterator(*this, 0, 0); }
116 iterator end() const;
Yi Jin0ed9b682017-08-18 14:51:20 -0700117
Joe Onorato1754d742016-11-21 17:51:35 -0800118private:
119 vector<uint8_t*> mBuffers;
120 int64_t mStartTime;
121 int64_t mFinishTime;
122 ssize_t mCurrentWritten;
123 bool mTimedOut;
124 bool mTruncated;
125};
126
Joe Onorato1754d742016-11-21 17:51:35 -0800127#endif // FD_BUFFER_H