blob: e9a53ffe513a5ca9fa1e52c6ac574e37215c6da5 [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
20#include "Reporter.h"
21
22#include <utils/Errors.h>
23
Joe Onorato1754d742016-11-21 17:51:35 -080024#include <vector>
25
26using namespace android;
27using namespace std;
28
29/**
30 * Reads a file into a buffer, and then writes that data to an FdSet.
31 */
32class FdBuffer
33{
34public:
35 FdBuffer();
36 ~FdBuffer();
37
38 /**
39 * Read the data until the timeout is hit or we hit eof.
40 * Returns NO_ERROR if there were no errors or if we timed out.
41 * Will mark the file O_NONBLOCK.
42 */
43 status_t read(int fd, int64_t timeoutMs);
44
45 /**
Yi Jin0a3406f2017-06-22 19:23:11 -070046 * Read processed results by streaming data to a parsing process, e.g. incident helper.
47 * The parsing process provides IO fds which are 'toFd' and 'fromFd'. The function
48 * reads original data in 'fd' and writes to parsing process through 'toFd', then it reads
49 * and stores the processed data from 'fromFd' in memory for later usage.
50 * This function behaves in a streaming fashion in order to save memory usage.
51 * Returns NO_ERROR if there were no errors or if we timed out.
52 */
53 status_t readProcessedDataInStream(int fd, int toFd, int fromFd, int64_t timeoutMs);
54
55 /**
Joe Onorato1754d742016-11-21 17:51:35 -080056 * Whether we timed out.
57 */
58 bool timedOut() { return mTimedOut; }
59
60 /**
61 * If more than 4 MB is read, we truncate the data and return success.
62 * Downstream tools must handle truncated incident reports as best as possible
63 * anyway because they could be cut off for a lot of reasons and it's best
64 * to get as much useful information out of the system as possible. If this
65 * happens, truncated() will return true so it can be marked. If the data is
66 * exactly 4 MB, truncated is still set. Sorry.
67 */
68 bool truncated() { return mTruncated; }
69
70 /**
71 * How much data was read.
72 */
73 size_t size();
74
75 /**
Yi Jin0ed9b682017-08-18 14:51:20 -070076 * [Deprecated] Write the data that we recorded to the fd given.
77 * TODO: remove it once the iterator api is working
Joe Onorato1754d742016-11-21 17:51:35 -080078 */
79 status_t write(ReportRequestSet* requests);
80
81 /**
82 * How long the read took in milliseconds.
83 */
84 int64_t durationMs() { return mFinishTime - mStartTime; }
85
Yi Jin0ed9b682017-08-18 14:51:20 -070086 /**
87 * Read data stored in FdBuffer
88 */
89 class iterator;
90 friend class iterator;
91 class iterator : public std::iterator<std::random_access_iterator_tag, uint8_t> {
92 private:
93 FdBuffer& mFdBuffer;
94 size_t mIndex;
95 size_t mOffset;
96 public:
97 explicit iterator(FdBuffer& buffer, ssize_t index, ssize_t offset)
98 : mFdBuffer(buffer), mIndex(index), mOffset(offset) {}
99 iterator& operator=(iterator& other) { return other; }
100 iterator& operator+(size_t offset); // this is implemented in .cpp
101 iterator& operator+=(size_t offset) { return *this + offset; }
102 iterator& operator++() { return *this + 1; }
103 iterator operator++(int) { return *this + 1; }
104 bool operator==(iterator other) const {
105 return mIndex == other.mIndex && mOffset == other.mOffset;
106 }
107 bool operator!=(iterator other) const { return !(*this == other); }
108 reference operator*() const { return mFdBuffer.mBuffers[mIndex][mOffset]; }
109
110 // random access could make the iterator out of bound
111 size_t bytesRead();
112 bool outOfBound() { return bytesRead() > mFdBuffer.size(); };
113 };
114 iterator begin() { return iterator(*this, 0, 0); }
115 iterator end();
116
Joe Onorato1754d742016-11-21 17:51:35 -0800117private:
118 vector<uint8_t*> mBuffers;
119 int64_t mStartTime;
120 int64_t mFinishTime;
121 ssize_t mCurrentWritten;
122 bool mTimedOut;
123 bool mTruncated;
124};
125
Yi Jin0a3406f2017-06-22 19:23:11 -0700126class Fpipe {
127public:
128 Fpipe() {}
129 bool close() { return !(::close(mFds[0]) || ::close(mFds[1])); }
130 ~Fpipe() { close(); }
131
Yi Jinb44f7d42017-07-21 12:12:59 -0700132 inline bool init() { return pipe(mFds) != -1; }
Yi Jin0a3406f2017-06-22 19:23:11 -0700133 inline int readFd() const { return mFds[0]; }
134 inline int writeFd() const { return mFds[1]; }
135
136private:
137 int mFds[2];
138};
139
Joe Onorato1754d742016-11-21 17:51:35 -0800140
141#endif // FD_BUFFER_H