blob: db3a74b7817821fe9be60cf81eb18b426846f4c3 [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 */
Yi Jinb592e3b2018-02-01 15:17:04 -080016#pragma once
Joe Onorato1754d742016-11-21 17:51:35 -080017
18#ifndef FD_BUFFER_H
19#define FD_BUFFER_H
20
Yi Jin6355d2f2018-03-14 15:18:02 -070021#include <android-base/unique_fd.h>
Yi Jinc23fad22017-09-15 17:24:59 -070022#include <android/util/EncodedBuffer.h>
Joe Onorato1754d742016-11-21 17:51:35 -080023#include <utils/Errors.h>
24
Joe Onorato1754d742016-11-21 17:51:35 -080025using namespace android;
Yi Jin6355d2f2018-03-14 15:18:02 -070026using namespace android::base;
Yi Jinc23fad22017-09-15 17:24:59 -070027using namespace android::util;
Joe Onorato1754d742016-11-21 17:51:35 -080028using namespace std;
29
30/**
Yi Jin1a11fa12018-02-22 16:44:10 -080031 * Reads data from fd into a buffer, fd must be closed explicitly.
Joe Onorato1754d742016-11-21 17:51:35 -080032 */
Yi Jinb592e3b2018-02-01 15:17:04 -080033class FdBuffer {
Joe Onorato1754d742016-11-21 17:51:35 -080034public:
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 */
Yi Jin6355d2f2018-03-14 15:18:02 -070043 status_t read(unique_fd* fd, int64_t timeoutMs);
Joe Onorato1754d742016-11-21 17:51:35 -080044
45 /**
Kweku Adamseadd1232018-02-05 16:45:13 -080046 * Read the data until we hit eof.
47 * Returns NO_ERROR if there were no errors.
48 */
Yi Jin6355d2f2018-03-14 15:18:02 -070049 status_t readFully(unique_fd* fd);
Kweku Adamseadd1232018-02-05 16:45:13 -080050
51 /**
Yi Jin0a3406f2017-06-22 19:23:11 -070052 * Read processed results by streaming data to a parsing process, e.g. incident helper.
53 * The parsing process provides IO fds which are 'toFd' and 'fromFd'. The function
54 * reads original data in 'fd' and writes to parsing process through 'toFd', then it reads
55 * and stores the processed data from 'fromFd' in memory for later usage.
56 * This function behaves in a streaming fashion in order to save memory usage.
57 * Returns NO_ERROR if there were no errors or if we timed out.
Yi Jin0eb22342017-11-06 17:17:27 -080058 *
59 * Poll will return POLLERR if fd is from sysfs, handle this edge case.
Yi Jin0a3406f2017-06-22 19:23:11 -070060 */
Yi Jin6355d2f2018-03-14 15:18:02 -070061 status_t readProcessedDataInStream(unique_fd* fd, unique_fd* toFd, unique_fd* fromFd,
62 int64_t timeoutMs, const bool isSysfs = false);
Yi Jin0a3406f2017-06-22 19:23:11 -070063
64 /**
Joe Onorato1754d742016-11-21 17:51:35 -080065 * Whether we timed out.
66 */
Yi Jin99c248f2017-08-25 18:11:58 -070067 bool timedOut() const { return mTimedOut; }
Joe Onorato1754d742016-11-21 17:51:35 -080068
69 /**
70 * If more than 4 MB is read, we truncate the data and return success.
71 * Downstream tools must handle truncated incident reports as best as possible
72 * anyway because they could be cut off for a lot of reasons and it's best
73 * to get as much useful information out of the system as possible. If this
74 * happens, truncated() will return true so it can be marked. If the data is
75 * exactly 4 MB, truncated is still set. Sorry.
76 */
Yi Jin99c248f2017-08-25 18:11:58 -070077 bool truncated() const { return mTruncated; }
Joe Onorato1754d742016-11-21 17:51:35 -080078
79 /**
80 * How much data was read.
81 */
Yi Jin99c248f2017-08-25 18:11:58 -070082 size_t size() const;
Joe Onorato1754d742016-11-21 17:51:35 -080083
84 /**
Joe Onorato1754d742016-11-21 17:51:35 -080085 * How long the read took in milliseconds.
86 */
Yi Jin99c248f2017-08-25 18:11:58 -070087 int64_t durationMs() const { return mFinishTime - mStartTime; }
Joe Onorato1754d742016-11-21 17:51:35 -080088
Yi Jin0ed9b682017-08-18 14:51:20 -070089 /**
Yi Jinc23fad22017-09-15 17:24:59 -070090 * Reader API for data stored in FdBuffer
Yi Jin0ed9b682017-08-18 14:51:20 -070091 */
Yi Jinc23fad22017-09-15 17:24:59 -070092 EncodedBuffer::iterator data() const;
Yi Jin0ed9b682017-08-18 14:51:20 -070093
Yi Jin1a11fa12018-02-22 16:44:10 -080094 /**
95 * Return the internal buffer, don't call unless you are familiar with EncodedBuffer.
96 */
97 EncodedBuffer* getInternalBuffer() { return &mBuffer; }
98
Joe Onorato1754d742016-11-21 17:51:35 -080099private:
Yi Jinc23fad22017-09-15 17:24:59 -0700100 EncodedBuffer mBuffer;
Joe Onorato1754d742016-11-21 17:51:35 -0800101 int64_t mStartTime;
102 int64_t mFinishTime;
Joe Onorato1754d742016-11-21 17:51:35 -0800103 bool mTimedOut;
104 bool mTruncated;
105};
106
Yi Jinb592e3b2018-02-01 15:17:04 -0800107#endif // FD_BUFFER_H