Implement PII Stripper, part 2
Implement EncodedBuffer that strip pii based on given privacy request.
The reason to implement another buffer is the length-delimited field's
size could change when its submessage gets stripped. It also intends to
keep the orignal data around for other requests to consume it.
In addition, the section implementation has adapted EncodedBuffer so
write out to each request's fd could be request-specific. The next step
is allow requests to set its privacy spec.
Notice the current design set the privacy spec of dropbox to AUTOMATIC,
this behavior might change in the future.
Bug: 64687253
Test: unit tests are writtern, see README.md for how to run unit tests.
Change-Id: I7ac236b8265ba9289dc6e17a8a5bf7f67ffb6bf5
diff --git a/cmds/incidentd/src/FdBuffer.h b/cmds/incidentd/src/FdBuffer.h
index e9a53ff..4c4823e 100644
--- a/cmds/incidentd/src/FdBuffer.h
+++ b/cmds/incidentd/src/FdBuffer.h
@@ -17,8 +17,6 @@
#ifndef FD_BUFFER_H
#define FD_BUFFER_H
-#include "Reporter.h"
-
#include <utils/Errors.h>
#include <vector>
@@ -55,7 +53,7 @@
/**
* Whether we timed out.
*/
- bool timedOut() { return mTimedOut; }
+ bool timedOut() const { return mTimedOut; }
/**
* If more than 4 MB is read, we truncate the data and return success.
@@ -65,23 +63,22 @@
* happens, truncated() will return true so it can be marked. If the data is
* exactly 4 MB, truncated is still set. Sorry.
*/
- bool truncated() { return mTruncated; }
+ bool truncated() const { return mTruncated; }
/**
* How much data was read.
*/
- size_t size();
+ size_t size() const;
/**
- * [Deprecated] Write the data that we recorded to the fd given.
- * TODO: remove it once the iterator api is working
+ * Flush all the data to given file descriptor;
*/
- status_t write(ReportRequestSet* requests);
+ status_t flush(int fd) const;
/**
* How long the read took in milliseconds.
*/
- int64_t durationMs() { return mFinishTime - mStartTime; }
+ int64_t durationMs() const { return mFinishTime - mStartTime; }
/**
* Read data stored in FdBuffer
@@ -89,14 +86,10 @@
class iterator;
friend class iterator;
class iterator : public std::iterator<std::random_access_iterator_tag, uint8_t> {
- private:
- FdBuffer& mFdBuffer;
- size_t mIndex;
- size_t mOffset;
public:
- explicit iterator(FdBuffer& buffer, ssize_t index, ssize_t offset)
+ iterator(const FdBuffer& buffer, ssize_t index, ssize_t offset)
: mFdBuffer(buffer), mIndex(index), mOffset(offset) {}
- iterator& operator=(iterator& other) { return other; }
+ iterator& operator=(iterator& other) const { return other; }
iterator& operator+(size_t offset); // this is implemented in .cpp
iterator& operator+=(size_t offset) { return *this + offset; }
iterator& operator++() { return *this + 1; }
@@ -105,14 +98,22 @@
return mIndex == other.mIndex && mOffset == other.mOffset;
}
bool operator!=(iterator other) const { return !(*this == other); }
+ int operator-(iterator other) const { return (int)bytesRead() - (int)other.bytesRead(); }
reference operator*() const { return mFdBuffer.mBuffers[mIndex][mOffset]; }
+ // return the snapshot of the current iterator
+ iterator snapshot() const { return iterator(mFdBuffer, mIndex, mOffset); }
+ // how many bytes are read
+ size_t bytesRead() const;
// random access could make the iterator out of bound
- size_t bytesRead();
- bool outOfBound() { return bytesRead() > mFdBuffer.size(); };
+ bool outOfBound() const { return bytesRead() > mFdBuffer.size(); }
+ private:
+ const FdBuffer& mFdBuffer;
+ size_t mIndex;
+ size_t mOffset;
};
- iterator begin() { return iterator(*this, 0, 0); }
- iterator end();
+ iterator begin() const { return iterator(*this, 0, 0); }
+ iterator end() const;
private:
vector<uint8_t*> mBuffers;
@@ -123,19 +124,4 @@
bool mTruncated;
};
-class Fpipe {
-public:
- Fpipe() {}
- bool close() { return !(::close(mFds[0]) || ::close(mFds[1])); }
- ~Fpipe() { close(); }
-
- inline bool init() { return pipe(mFds) != -1; }
- inline int readFd() const { return mFds[0]; }
- inline int writeFd() const { return mFds[1]; }
-
-private:
- int mFds[2];
-};
-
-
#endif // FD_BUFFER_H