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

Commit 8e2b9a72 authored by Tao Bao's avatar Tao Bao Committed by android-build-merger
Browse files

Merge "Support > 2GiB seeks in adb_lseek."

am: 546b539b

Change-Id: Iccff67dd11ca795cec3026e918522a2e4e38725a
parents 5fbbc3c8 546b539b
Loading
Loading
Loading
Loading
+19 −12
Original line number Diff line number Diff line
@@ -841,14 +841,19 @@ static int adb_sideload_host(const char* filename) {
        return -1;
    }

    std::string service = android::base::StringPrintf(
        "sideload-host:%d:%d", static_cast<int>(sb.st_size), SIDELOAD_HOST_BLOCK_SIZE);
    std::string service =
            android::base::StringPrintf("sideload-host:%" PRId64 ":%d",
                                        static_cast<int64_t>(sb.st_size), SIDELOAD_HOST_BLOCK_SIZE);
    std::string error;
    unique_fd device_fd(adb_connect(service, &error));
    if (device_fd < 0) {
        // Try falling back to the older (<= K) sideload method. Maybe this
        // is an older device that doesn't support sideload-host.
        fprintf(stderr, "adb: sideload connection failed: %s\n", error.c_str());

        // If this is a small enough package, maybe this is an older device that doesn't
        // support sideload-host. Try falling back to the older (<= K) sideload method.
        if (sb.st_size > INT_MAX) {
            return -1;
        }
        fprintf(stderr, "adb: trying pre-KitKat sideload method...\n");
        return adb_sideload_legacy(filename, package_fd, static_cast<int>(sb.st_size));
    }
@@ -858,7 +863,7 @@ static int adb_sideload_host(const char* filename) {

    char buf[SIDELOAD_HOST_BLOCK_SIZE];

    size_t xfer = 0;
    int64_t xfer = 0;
    int last_percent = -1;
    while (true) {
        if (!ReadFdExactly(device_fd, buf, 8)) {
@@ -874,20 +879,22 @@ static int adb_sideload_host(const char* filename) {
            return 0;
        }

        int block = strtol(buf, nullptr, 10);

        size_t offset = block * SIDELOAD_HOST_BLOCK_SIZE;
        if (offset >= static_cast<size_t>(sb.st_size)) {
            fprintf(stderr, "adb: failed to read block %d past end\n", block);
        int64_t block = strtoll(buf, nullptr, 10);
        int64_t offset = block * SIDELOAD_HOST_BLOCK_SIZE;
        if (offset >= static_cast<int64_t>(sb.st_size)) {
            fprintf(stderr,
                    "adb: failed to read block %" PRId64 " at offset %" PRId64 ", past end %" PRId64
                    "\n",
                    block, offset, static_cast<int64_t>(sb.st_size));
            return -1;
        }

        size_t to_write = SIDELOAD_HOST_BLOCK_SIZE;
        if ((offset + SIDELOAD_HOST_BLOCK_SIZE) > static_cast<size_t>(sb.st_size)) {
        if ((offset + SIDELOAD_HOST_BLOCK_SIZE) > static_cast<int64_t>(sb.st_size)) {
            to_write = sb.st_size - offset;
        }

        if (adb_lseek(package_fd, offset, SEEK_SET) != static_cast<int>(offset)) {
        if (adb_lseek(package_fd, offset, SEEK_SET) != offset) {
            fprintf(stderr, "adb: failed to seek to package block: %s\n", strerror(errno));
            return -1;
        }
+19 −16
Original line number Diff line number Diff line
@@ -52,10 +52,11 @@
#include <fcntl.h>
#include <io.h>
#include <process.h>
#include <stdint.h>
#include <sys/stat.h>
#include <utime.h>
#include <winsock2.h>
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>

#include <memory>   // unique_ptr
@@ -92,7 +93,7 @@ extern int adb_open(const char* path, int options);
extern int adb_creat(const char* path, int mode);
extern int adb_read(int fd, void* buf, int len);
extern int adb_write(int fd, const void* buf, int len);
extern int adb_lseek(int fd, int pos, int where);
extern int64_t adb_lseek(int fd, int64_t pos, int where);
extern int adb_shutdown(int fd, int direction = SHUT_RDWR);
extern int adb_close(int fd);
extern int adb_register_socket(SOCKET s);
@@ -315,25 +316,24 @@ size_t ParseCompleteUTF8(const char* first, const char* last, std::vector<char>*

#else /* !_WIN32 a.k.a. Unix */

#include <cutils/sockets.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/wait.h>

#include <pthread.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdarg.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <poll.h>
#include <pthread.h>
#include <signal.h>
#include <stdarg.h>
#include <stdint.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>

#include <string>

#include <cutils/sockets.h>

#define OS_PATH_SEPARATORS "/"
#define OS_PATH_SEPARATOR '/'
#define OS_PATH_SEPARATOR_STR "/"
@@ -443,9 +443,12 @@ static __inline__ int adb_write(int fd, const void* buf, size_t len)
#undef   write
#define  write  ___xxx_write

static __inline__ int   adb_lseek(int  fd, int  pos, int  where)
{
static __inline__ int64_t adb_lseek(int fd, int64_t pos, int where) {
#if defined(__APPLE__)
    return lseek(fd, pos, where);
#else
    return lseek64(fd, pos, where);
#endif
}
#undef lseek
#define lseek ___xxx_lseek
+10 −16
Original line number Diff line number Diff line
@@ -52,12 +52,11 @@ extern void fatal(const char *fmt, ...);

typedef const struct FHClassRec_* FHClass;
typedef struct FHRec_* FH;
typedef struct EventHookRec_* EventHook;

typedef struct FHClassRec_ {
    void (*_fh_init)(FH);
    int (*_fh_close)(FH);
    int (*_fh_lseek)(FH, int, int);
    int64_t (*_fh_lseek)(FH, int64_t, int);
    int (*_fh_read)(FH, void*, int);
    int (*_fh_write)(FH, const void*, int);
    int (*_fh_writev)(FH, const adb_iovec*, int);
@@ -65,7 +64,7 @@ typedef struct FHClassRec_ {

static void _fh_file_init(FH);
static int _fh_file_close(FH);
static int _fh_file_lseek(FH, int, int);
static int64_t _fh_file_lseek(FH, int64_t, int);
static int _fh_file_read(FH, void*, int);
static int _fh_file_write(FH, const void*, int);
static int _fh_file_writev(FH, const adb_iovec*, int);
@@ -81,7 +80,7 @@ static const FHClassRec _fh_file_class = {

static void _fh_socket_init(FH);
static int _fh_socket_close(FH);
static int _fh_socket_lseek(FH, int, int);
static int64_t _fh_socket_lseek(FH, int64_t, int);
static int _fh_socket_read(FH, void*, int);
static int _fh_socket_write(FH, const void*, int);
static int _fh_socket_writev(FH, const adb_iovec*, int);
@@ -318,10 +317,8 @@ static int _fh_file_writev(FH f, const adb_iovec* iov, int iovcnt) {
    return wrote_bytes;
}

static int _fh_file_lseek(FH f, int pos, int origin) {
static int64_t _fh_file_lseek(FH f, int64_t pos, int origin) {
    DWORD method;
    DWORD result;

    switch (origin) {
        case SEEK_SET:
            method = FILE_BEGIN;
@@ -337,14 +334,13 @@ static int _fh_file_lseek(FH f, int pos, int origin) {
            return -1;
    }

    result = SetFilePointer(f->fh_handle, pos, nullptr, method);
    if (result == INVALID_SET_FILE_POINTER) {
    LARGE_INTEGER li = {.QuadPart = pos};
    if (!SetFilePointerEx(f->fh_handle, li, &li, method)) {
        errno = EIO;
        return -1;
    } else {
        f->eof = 0;
    }
    return (int)result;
    f->eof = 0;
    return li.QuadPart;
}

/**************************************************************************/
@@ -491,14 +487,12 @@ ssize_t adb_writev(int fd, const adb_iovec* iov, int iovcnt) {
    return f->clazz->_fh_writev(f, iov, iovcnt);
}

int adb_lseek(int fd, int pos, int where) {
int64_t adb_lseek(int fd, int64_t pos, int where) {
    FH f = _fh_from_int(fd, __func__);

    if (!f) {
        errno = EBADF;
        return -1;
    }

    return f->clazz->_fh_lseek(f, pos, where);
}

@@ -644,7 +638,7 @@ static int _fh_socket_close(FH f) {
    return 0;
}

static int _fh_socket_lseek(FH f, int pos, int origin) {
static int64_t _fh_socket_lseek(FH f, int64_t pos, int origin) {
    errno = EPIPE;
    return -1;
}