static int load_buf_fd(usb_handle *usb, int fd,
        struct fastboot_buffer *buf)
{
    int64_t sz64;
    void *data;
    int64_t limit;


    sz64 = file_size(fd);
    if (sz64 < 0) {
        return -1;
    }

    lseek(fd, 0, SEEK_SET);
    limit = get_sparse_limit(usb, sz64);
    if (limit) {
        struct sparse_file **s = load_sparse_files(fd, limit);
        if (s == NULL) {
            return -1;
        }
        buf->type = FB_BUFFER_SPARSE;
        buf->data = s;
    } else {
        unsigned int sz;
        data = load_fd(fd, &sz);
        if (data == 0) return -1;
        buf->type = FB_BUFFER;
        buf->data = data;
        buf->sz = sz;
    }

    return 0;
}
static int load_buf_fd(usb_handle* usb, int fd, struct fastboot_buffer* buf) {
    int64_t sz = get_file_size(fd);
    if (sz == -1) {
        return -1;
    }

    lseek64(fd, 0, SEEK_SET);
    int64_t limit = get_sparse_limit(usb, sz);
    if (limit) {
        sparse_file** s = load_sparse_files(fd, limit);
        if (s == nullptr) {
            return -1;
        }
        buf->type = FB_BUFFER_SPARSE;
        buf->data = s;
    } else {
        void* data = load_fd(fd, &sz);
        if (data == nullptr) return -1;
        buf->type = FB_BUFFER;
        buf->data = data;
        buf->sz = sz;
    }

    return 0;
}
void do_flash(transport_t *trans, const char *pname, const char *fname)
{
    int64_t sz64;
    void *data;
    int64_t limit;

    sz64 = file_size(fname);
    limit = get_sparse_limit(trans, sz64);
    if (limit) {
        struct sparse_file **s = load_sparse_files(fname, limit);
        if (s == NULL) {
            die("cannot sparse load '%s'\n", fname);
        }
        while (*s) {
            sz64 = sparse_file_len(*s, true, false);
            fb_queue_flash_sparse(pname, *s++, sz64);
        }
    } else {
        unsigned int sz;
        data = load_file(fname, &sz);
        if (data == 0) die("cannot load '%s': %s\n", fname, strerror(errno));
        fb_queue_flash(pname, data, sz);
    }
}