• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
20 #include "Reporter.h"
21 
22 #include <utils/Errors.h>
23 
24 #include <set>
25 #include <vector>
26 
27 using namespace android;
28 using namespace std;
29 
30 /**
31  * Reads a file into a buffer, and then writes that data to an FdSet.
32  */
33 class FdBuffer
34 {
35 public:
36     FdBuffer();
37     ~FdBuffer();
38 
39     /**
40      * Read the data until the timeout is hit or we hit eof.
41      * Returns NO_ERROR if there were no errors or if we timed out.
42      * Will mark the file O_NONBLOCK.
43      */
44     status_t read(int fd, int64_t timeoutMs);
45 
46     /**
47      * Whether we timed out.
48      */
timedOut()49     bool timedOut() { return mTimedOut; }
50 
51     /**
52      * If more than 4 MB is read, we truncate the data and return success.
53      * Downstream tools must handle truncated incident reports as best as possible
54      * anyway because they could be cut off for a lot of reasons and it's best
55      * to get as much useful information out of the system as possible. If this
56      * happens, truncated() will return true so it can be marked. If the data is
57      * exactly 4 MB, truncated is still set. Sorry.
58      */
truncated()59     bool truncated() { return mTruncated; }
60 
61     /**
62      * How much data was read.
63      */
64     size_t size();
65 
66     /**
67      * Write the data that we recorded to the fd given.
68      */
69     status_t write(ReportRequestSet* requests);
70 
71     /**
72      * How long the read took in milliseconds.
73      */
durationMs()74     int64_t durationMs() { return mFinishTime - mStartTime; }
75 
76 private:
77     vector<uint8_t*> mBuffers;
78     int64_t mStartTime;
79     int64_t mFinishTime;
80     ssize_t mCurrentWritten;
81     bool mTimedOut;
82     bool mTruncated;
83 };
84 
85 
86 #endif // FD_BUFFER_H
87