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

Commit 79202800 authored by Songchun Fan's avatar Songchun Fan Committed by android-build-merger
Browse files

Merge "[adb] Add a getter for native Win32 file handle" am: 93619e37

am: a791d595

Change-Id: Ia253e457dd80096b4cfacc107561e40f1f0c4837
parents 0dd7bc7a a791d595
Loading
Loading
Loading
Loading
+30 −0
Original line number Diff line number Diff line
@@ -91,11 +91,14 @@ extern int adb_mkdir(const std::string& path, int mode);
extern int adb_open(const char* path, int options);
extern int adb_creat(const char* path, int mode);
extern int adb_read(borrowed_fd fd, void* buf, int len);
extern int adb_pread(borrowed_fd fd, void* buf, int len, off64_t offset);
extern int adb_write(borrowed_fd fd, const void* buf, int len);
extern int adb_pwrite(borrowed_fd fd, const void* buf, int len, off64_t offset);
extern int64_t adb_lseek(borrowed_fd fd, int64_t pos, int where);
extern int adb_shutdown(borrowed_fd fd, int direction = SHUT_RDWR);
extern int adb_close(int fd);
extern int adb_register_socket(SOCKET s);
extern HANDLE adb_get_os_handle(borrowed_fd fd);

// See the comments for the !defined(_WIN32) version of unix_close().
static __inline__ int unix_close(int fd) {
@@ -115,6 +118,9 @@ static __inline__ int unix_read(borrowed_fd fd, void* buf, size_t len) {
#undef   read
#define  read  ___xxx_read

#undef pread
#define pread ___xxx_pread

// See the comments for the !defined(_WIN32) version of unix_write().
static __inline__ int unix_write(borrowed_fd fd, const void* buf, size_t len) {
    return write(fd.get(), buf, len);
@@ -122,6 +128,9 @@ static __inline__ int unix_write(borrowed_fd fd, const void* buf, size_t len) {
#undef   write
#define  write  ___xxx_write

#undef pwrite
#define pwrite ___xxx_pwrite

// See the comments for the !defined(_WIN32) version of unix_lseek().
static __inline__ int unix_lseek(borrowed_fd fd, int pos, int where) {
    return lseek(fd.get(), pos, where);
@@ -415,6 +424,14 @@ static __inline__ int adb_read(borrowed_fd fd, void* buf, size_t len) {
    return TEMP_FAILURE_RETRY(read(fd.get(), buf, len));
}

static __inline__ int adb_pread(int fd, void* buf, size_t len, off64_t offset) {
#if defined(__APPLE__)
    return TEMP_FAILURE_RETRY(pread(fd, buf, len, offset));
#else
    return TEMP_FAILURE_RETRY(pread64(fd, buf, len, offset));
#endif
}

// Like unix_read(), but does not handle EINTR.
static __inline__ int unix_read_interruptible(borrowed_fd fd, void* buf, size_t len) {
    return read(fd.get(), buf, len);
@@ -422,12 +439,25 @@ static __inline__ int unix_read_interruptible(borrowed_fd fd, void* buf, size_t

#undef read
#define read ___xxx_read
#undef pread
#define pread ___xxx_pread

static __inline__ int adb_write(borrowed_fd fd, const void* buf, size_t len) {
    return TEMP_FAILURE_RETRY(write(fd.get(), buf, len));
}

static __inline__ int adb_pwrite(int fd, const void* buf, size_t len, off64_t offset) {
#if defined(__APPLE__)
    return TEMP_FAILURE_RETRY(pwrite(fd, buf, len, offset));
#else
    return TEMP_FAILURE_RETRY(pwrite64(fd, buf, len, offset));
#endif
}

#undef   write
#define  write  ___xxx_write
#undef pwrite
#define pwrite ___xxx_pwrite

static __inline__ int64_t adb_lseek(borrowed_fd fd, int64_t pos, int where) {
#if defined(__APPLE__)
+68 −12
Original line number Diff line number Diff line
@@ -60,6 +60,7 @@ typedef struct FHClassRec_ {
    int (*_fh_read)(FH, void*, int);
    int (*_fh_write)(FH, const void*, int);
    int (*_fh_writev)(FH, const adb_iovec*, int);
    intptr_t (*_fh_get_os_handle)(FH);
} FHClassRec;

static void _fh_file_init(FH);
@@ -68,14 +69,11 @@ 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);
static intptr_t _fh_file_get_os_handle(FH f);

static const FHClassRec _fh_file_class = {
    _fh_file_init,
    _fh_file_close,
    _fh_file_lseek,
    _fh_file_read,
    _fh_file_write,
    _fh_file_writev,
        _fh_file_init,  _fh_file_close,  _fh_file_lseek,         _fh_file_read,
        _fh_file_write, _fh_file_writev, _fh_file_get_os_handle,
};

static void _fh_socket_init(FH);
@@ -84,14 +82,11 @@ 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);
static intptr_t _fh_socket_get_os_handle(FH f);

static const FHClassRec _fh_socket_class = {
    _fh_socket_init,
    _fh_socket_close,
    _fh_socket_lseek,
    _fh_socket_read,
    _fh_socket_write,
    _fh_socket_writev,
        _fh_socket_init,  _fh_socket_close,  _fh_socket_lseek,         _fh_socket_read,
        _fh_socket_write, _fh_socket_writev, _fh_socket_get_os_handle,
};

#if defined(assert)
@@ -331,6 +326,10 @@ static int64_t _fh_file_lseek(FH f, int64_t pos, int origin) {
    return li.QuadPart;
}

static intptr_t _fh_file_get_os_handle(FH f) {
    return reinterpret_cast<intptr_t>(f->u.handle);
}

/**************************************************************************/
/**************************************************************************/
/*****                                                                *****/
@@ -456,6 +455,26 @@ int adb_read(borrowed_fd fd, void* buf, int len) {
    return f->clazz->_fh_read(f, buf, len);
}

int adb_pread(borrowed_fd fd, void* buf, int len, off64_t offset) {
    OVERLAPPED overlapped = {};
    overlapped.Offset = static_cast<DWORD>(offset);
    overlapped.OffsetHigh = static_cast<DWORD>(offset >> 32);
    DWORD bytes_read;
    if (!::ReadFile(adb_get_os_handle(fd), buf, static_cast<DWORD>(len), &bytes_read,
                    &overlapped)) {
        D("adb_pread: could not read %d bytes from FD %d", len, fd.get());
        switch (::GetLastError()) {
            case ERROR_IO_PENDING:
                errno = EAGAIN;
                return -1;
            default:
                errno = EINVAL;
                return -1;
        }
    }
    return static_cast<int>(bytes_read);
}

int adb_write(borrowed_fd fd, const void* buf, int len) {
    FH f = _fh_from_int(fd, __func__);

@@ -478,6 +497,25 @@ ssize_t adb_writev(borrowed_fd fd, const adb_iovec* iov, int iovcnt) {
    return f->clazz->_fh_writev(f, iov, iovcnt);
}

int adb_pwrite(borrowed_fd fd, const void* buf, int len, off64_t offset) {
    OVERLAPPED params = {};
    params.Offset = static_cast<DWORD>(offset);
    params.OffsetHigh = static_cast<DWORD>(offset >> 32);
    DWORD bytes_written = 0;
    if (!::WriteFile(adb_get_os_handle(fd), buf, len, &bytes_written, &params)) {
        D("adb_pwrite: could not write %d bytes to FD %d", len, fd.get());
        switch (::GetLastError()) {
            case ERROR_IO_PENDING:
                errno = EAGAIN;
                return -1;
            default:
                errno = EINVAL;
                return -1;
        }
    }
    return static_cast<int>(bytes_written);
}

int64_t adb_lseek(borrowed_fd fd, int64_t pos, int where) {
    FH f = _fh_from_int(fd, __func__);
    if (!f) {
@@ -500,6 +538,20 @@ int adb_close(int fd) {
    return 0;
}

HANDLE adb_get_os_handle(borrowed_fd fd) {
    FH f = _fh_from_int(fd, __func__);

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

    D("adb_get_os_handle: %s", f->name);
    const intptr_t intptr_handle = f->clazz->_fh_get_os_handle(f);
    const HANDLE handle = reinterpret_cast<const HANDLE>(intptr_handle);
    return handle;
}

/**************************************************************************/
/**************************************************************************/
/*****                                                                *****/
@@ -694,6 +746,10 @@ static int _fh_socket_writev(FH f, const adb_iovec* iov, int iovcnt) {
    return static_cast<int>(bytes_written);
}

static intptr_t _fh_socket_get_os_handle(FH f) {
    return f->u.socket;
}

/**************************************************************************/
/**************************************************************************/
/*****                                                                *****/