Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b592e3bc authored by Yi Jin's avatar Yi Jin
Browse files

This cl formats incidentd and makes it easier for debugging.

Bug: 72755317
Test: clang-format -type=file -i <files>
Change-Id: Ide91227f26c6b1db6d2e5fe8117ca5cc4cf77fd3
parent 5f8b250f
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
BasedOnStyle: Google
AllowShortIfStatementsOnASingleLine: true
AllowShortFunctionsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
BinPackArguments: true
BinPackParameters: true
ColumnLimit: 100
CommentPragmas: NOLINT:.*
ContinuationIndentWidth: 8
DerivePointerAlignment: false
IndentWidth: 4
PointerAlignment: Left
TabWidth: 4
AccessModifierOffset: -4
IncludeCategories:
  - Regex:    '^"Log\.h"'
    Priority:    -1
+2 −2
Original line number Diff line number Diff line
@@ -32,7 +32,7 @@ LOCAL_SRC_FILES := \
        src/Privacy.cpp \
        src/Reporter.cpp \
        src/Section.cpp \
        src/io_util.cpp \
        src/incidentd_util.cpp \
        src/main.cpp \
        src/report_directory.cpp

@@ -116,7 +116,7 @@ LOCAL_SRC_FILES := \
    src/Privacy.cpp \
    src/Reporter.cpp \
    src/Section.cpp \
    src/io_util.cpp \
    src/incidentd_util.cpp \
    src/report_directory.cpp \
    tests/section_list.cpp \
    tests/PrivacyBuffer_test.cpp \
+5 −1
Original line number Diff line number Diff line
@@ -21,3 +21,7 @@ Run the test via AndroidTest.xml
```
root$ atest incidentd_test
```

Use clang-format to style the file

clang-format -style=file -i <file list>
 No newline at end of file
+30 −54
Original line number Diff line number Diff line
@@ -13,8 +13,7 @@
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "incidentd"
#include "Log.h"

#include "FdBuffer.h"

@@ -26,30 +25,16 @@
#include <unistd.h>
#include <wait.h>

const bool DEBUG = false;
const ssize_t BUFFER_SIZE = 16 * 1024;  // 16 KB
const ssize_t MAX_BUFFER_COUNT = 256;   // 4 MB max

FdBuffer::FdBuffer()
    :mBuffer(BUFFER_SIZE),
     mStartTime(-1),
     mFinishTime(-1),
     mTimedOut(false),
     mTruncated(false)
{
}
    : mBuffer(BUFFER_SIZE), mStartTime(-1), mFinishTime(-1), mTimedOut(false), mTruncated(false) {}

FdBuffer::~FdBuffer()
{
}
FdBuffer::~FdBuffer() {}

status_t
FdBuffer::read(int fd, int64_t timeout)
{
    struct pollfd pfds = {
        .fd = fd,
        .events = POLLIN
    };
status_t FdBuffer::read(int fd, int64_t timeout) {
    struct pollfd pfds = {.fd = fd, .events = POLLIN};
    mStartTime = uptimeMillis();

    fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
@@ -63,22 +48,22 @@ FdBuffer::read(int fd, int64_t timeout)

        int64_t remainingTime = (mStartTime + timeout) - uptimeMillis();
        if (remainingTime <= 0) {
            if (DEBUG) ALOGD("timed out due to long read");
            VLOG("timed out due to long read");
            mTimedOut = true;
            break;
        }

        int count = poll(&pfds, 1, remainingTime);
        if (count == 0) {
            if (DEBUG) ALOGD("timed out due to block calling poll");
            VLOG("timed out due to block calling poll");
            mTimedOut = true;
            break;
        } else if (count < 0) {
            if (DEBUG) ALOGD("poll failed: %s", strerror(errno));
            VLOG("poll failed: %s", strerror(errno));
            return -errno;
        } else {
            if ((pfds.revents & POLLERR) != 0) {
                if (DEBUG) ALOGD("return event has error %s", strerror(errno));
                VLOG("return event has error %s", strerror(errno));
                return errno != 0 ? -errno : UNKNOWN_ERROR;
            } else {
                ssize_t amt = ::read(fd, mBuffer.writeBuffer(), mBuffer.currentToWrite());
@@ -86,7 +71,7 @@ FdBuffer::read(int fd, int64_t timeout)
                    if (errno == EAGAIN || errno == EWOULDBLOCK) {
                        continue;
                    } else {
                        if (DEBUG) ALOGD("Fail to read %d: %s", fd, strerror(errno));
                        VLOG("Fail to read %d: %s", fd, strerror(errno));
                        return -errno;
                    }
                } else if (amt == 0) {
@@ -100,9 +85,8 @@ FdBuffer::read(int fd, int64_t timeout)
    return NO_ERROR;
}

status_t
FdBuffer::readProcessedDataInStream(int fd, int toFd, int fromFd, int64_t timeoutMs, const bool isSysfs)
{
status_t FdBuffer::readProcessedDataInStream(int fd, int toFd, int fromFd, int64_t timeoutMs,
                                             const bool isSysfs) {
    struct pollfd pfds[] = {
            {.fd = fd, .events = POLLIN},
            {.fd = toFd, .events = POLLOUT},
@@ -131,7 +115,7 @@ FdBuffer::readProcessedDataInStream(int fd, int toFd, int fromFd, int64_t timeou

        int64_t remainingTime = (mStartTime + timeoutMs) - uptimeMillis();
        if (remainingTime <= 0) {
            if (DEBUG) ALOGD("timed out due to long read");
            VLOG("timed out due to long read");
            mTimedOut = true;
            break;
        }
@@ -139,11 +123,11 @@ FdBuffer::readProcessedDataInStream(int fd, int toFd, int fromFd, int64_t timeou
        // wait for any pfds to be ready to perform IO
        int count = poll(pfds, 3, remainingTime);
        if (count == 0) {
            if (DEBUG) ALOGD("timed out due to block calling poll");
            VLOG("timed out due to block calling poll");
            mTimedOut = true;
            break;
        } else if (count < 0) {
            if (DEBUG) ALOGD("Fail to poll: %s", strerror(errno));
            VLOG("Fail to poll: %s", strerror(errno));
            return -errno;
        }

@@ -151,10 +135,10 @@ FdBuffer::readProcessedDataInStream(int fd, int toFd, int fromFd, int64_t timeou
        for (int i = 0; i < 3; ++i) {
            if ((pfds[i].revents & POLLERR) != 0) {
                if (i == 0 && isSysfs) {
                    if (DEBUG) ALOGD("fd %d is sysfs, ignore its POLLERR return value", fd);
                    VLOG("fd %d is sysfs, ignore its POLLERR return value", fd);
                    continue;
                }
                if (DEBUG) ALOGD("fd[%d]=%d returns error events: %s", i, fd, strerror(errno));
                VLOG("fd[%d]=%d returns error events: %s", i, fd, strerror(errno));
                return errno != 0 ? -errno : UNKNOWN_ERROR;
            }
        }
@@ -169,7 +153,7 @@ FdBuffer::readProcessedDataInStream(int fd, int toFd, int fromFd, int64_t timeou
            }
            if (amt < 0) {
                if (!(errno == EAGAIN || errno == EWOULDBLOCK)) {
                    if (DEBUG) ALOGD("Fail to read fd %d: %s", fd, strerror(errno));
                    VLOG("Fail to read fd %d: %s", fd, strerror(errno));
                    return -errno;
                }                   // otherwise just continue
            } else if (amt == 0) {  // reach EOF so don't have to poll pfds[0].
@@ -191,7 +175,7 @@ FdBuffer::readProcessedDataInStream(int fd, int toFd, int fromFd, int64_t timeou
            }
            if (amt < 0) {
                if (!(errno == EAGAIN || errno == EWOULDBLOCK)) {
                    if (DEBUG) ALOGD("Fail to write toFd %d: %s", toFd, strerror(errno));
                    VLOG("Fail to write toFd %d: %s", toFd, strerror(errno));
                    return -errno;
                }  // otherwise just continue
            } else {
@@ -218,7 +202,7 @@ FdBuffer::readProcessedDataInStream(int fd, int toFd, int fromFd, int64_t timeou
        ssize_t amt = ::read(fromFd, mBuffer.writeBuffer(), mBuffer.currentToWrite());
        if (amt < 0) {
            if (!(errno == EAGAIN || errno == EWOULDBLOCK)) {
                if (DEBUG) ALOGD("Fail to read fromFd %d: %s", fromFd, strerror(errno));
                VLOG("Fail to read fromFd %d: %s", fromFd, strerror(errno));
                return -errno;
            }  // otherwise just continue
        } else if (amt == 0) {
@@ -232,14 +216,6 @@ FdBuffer::readProcessedDataInStream(int fd, int toFd, int fromFd, int64_t timeou
    return NO_ERROR;
}

size_t
FdBuffer::size() const
{
    return mBuffer.size();
}
size_t FdBuffer::size() const { return mBuffer.size(); }

EncodedBuffer::iterator
FdBuffer::data() const
{
    return mBuffer.begin();
}
EncodedBuffer::iterator FdBuffer::data() const { return mBuffer.begin(); }
+5 −4
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#pragma once

#ifndef FD_BUFFER_H
#define FD_BUFFER_H
@@ -27,8 +28,7 @@ using namespace std;
/**
 * Reads a file into a buffer, and then writes that data to an FdSet.
 */
class FdBuffer
{
class FdBuffer {
public:
    FdBuffer();
    ~FdBuffer();
@@ -50,7 +50,8 @@ public:
     *
     * Poll will return POLLERR if fd is from sysfs, handle this edge case.
     */
    status_t readProcessedDataInStream(int fd, int toFd, int fromFd, int64_t timeoutMs, const bool isSysfs=false);
    status_t readProcessedDataInStream(int fd, int toFd, int fromFd, int64_t timeoutMs,
                                       const bool isSysfs = false);

    /**
     * Whether we timed out.
Loading