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

Commit bde9346e authored by Tomasz Wasilczyk's avatar Tomasz Wasilczyk Committed by Gerrit Code Review
Browse files

Merge "Binder file module" into main

parents 70127e54 26db5e4b
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -92,6 +92,7 @@ cc_defaults {
        "TextOutput.cpp",
        "Trace.cpp",
        "Utils.cpp",
        "file.cpp",
    ],

    shared_libs: [
@@ -653,4 +654,7 @@ cc_binary {
        "libutils",
        "android.debug_aidl-cpp",
    ],
    static_libs: [
        "libc++fs",
    ],
}
+1 −2
Original line number Diff line number Diff line
@@ -27,8 +27,7 @@
#include <stdio.h>

#include "BuildFlags.h"

#include <android-base/file.h>
#include "file.h"

//#undef ALOGV
//#define ALOGV(...) fprintf(stderr, __VA_ARGS__)
+3 −1
Original line number Diff line number Diff line
@@ -16,13 +16,15 @@

#include "OS.h"
#include "Utils.h"
#include "file.h"

#include <android-base/file.h>
#include <binder/RpcTransportRaw.h>
#include <log/log.h>
#include <string.h>
#include <sys/socket.h>

using android::binder::ReadFully;

namespace android::binder::os {

// Linux kernel supports up to 253 (from SCM_MAX_FD) for unix sockets.
+7 −3
Original line number Diff line number Diff line
@@ -14,19 +14,23 @@
 * limitations under the License.
 */

#include <android-base/file.h>
#include "file.h"

#include <binder/Functional.h>
#include <binder/RecordedTransaction.h>
#include <binder/unique_fd.h>

#include <inttypes.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <algorithm>

using namespace android::binder::impl;
using android::Parcel;
using android::binder::borrowed_fd;
using android::binder::ReadFully;
using android::binder::unique_fd;
using android::binder::WriteFully;
using android::binder::debug::RecordedTransaction;

#define PADDING8(s) ((8 - (s) % 8) % 8)
@@ -183,7 +187,7 @@ std::optional<RecordedTransaction> RecordedTransaction::fromFile(const unique_fd
            return std::nullopt;
        }

        if (!android::base::ReadFully(fd, &chunk, sizeof(ChunkDescriptor))) {
        if (!ReadFully(fd, &chunk, sizeof(ChunkDescriptor))) {
            ALOGE("Failed to read ChunkDescriptor from fd %d. %s", fd.get(), strerror(errno));
            return std::nullopt;
        }
@@ -318,7 +322,7 @@ android::status_t RecordedTransaction::writeChunk(borrowed_fd fd, uint32_t chunk
    buffer.insert(buffer.end(), checksumBytes, checksumBytes + sizeof(transaction_checksum_t));

    // Write buffer to file
    if (!android::base::WriteFully(fd, buffer.data(), buffer.size())) {
    if (!WriteFully(fd, buffer.data(), buffer.size())) {
        ALOGE("Failed to write chunk fd %d", fd.get());
        return UNKNOWN_ERROR;
    }

libs/binder/file.cpp

0 → 100644
+57 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "file.h"

#ifdef BINDER_NO_LIBBASE

#include <stdint.h>

// clang-format off

namespace android::binder {

bool ReadFully(borrowed_fd fd, void* data, size_t byte_count) {
  uint8_t* p = reinterpret_cast<uint8_t*>(data);
  size_t remaining = byte_count;
  while (remaining > 0) {
    ssize_t n = TEMP_FAILURE_RETRY(read(fd.get(), p, remaining));
    if (n == 0) {  // EOF
      errno = ENODATA;
      return false;
    }
    if (n == -1) return false;
    p += n;
    remaining -= n;
  }
  return true;
}

bool WriteFully(borrowed_fd fd, const void* data, size_t byte_count) {
  const uint8_t* p = reinterpret_cast<const uint8_t*>(data);
  size_t remaining = byte_count;
  while (remaining > 0) {
    ssize_t n = TEMP_FAILURE_RETRY(write(fd.get(), p, remaining));
    if (n == -1) return false;
    p += n;
    remaining -= n;
  }
  return true;
}

}  // namespace android::binder

#endif // BINDER_NO_LIBBASE
Loading