Logo Search packages:      
Sourcecode: obexd version File versions  Download package

gboolean gw_obex_get ( GwObex ctx,
const gchar *  local,
const gchar *  remote,
const gchar *  type,
const guint8 *  apparam,
gint  apparam_size,
gchar **  buf,
gint *  buf_size,
int  stream_fd,
gboolean  async 
)

Get an object from the server

Parameters:
ctx Pointer returned by gw_obex_setup()
local Local filename which contains the object
remote Remote filename to store the object in
type MIME-type of the object (NULL if not known)
Returns:
TRUE on success, FALSE on failure

Definition at line 839 of file obex-priv.c.

References get_uname(), GW_OBEX_ERROR_BUSY, GW_OBEX_ERROR_INVALID_PARAMS, and GW_OBEX_ERROR_LOCAL_ACCESS.

Referenced by gw_obex_get_async(), gw_obex_get_async_with_apparam(), gw_obex_get_buf(), gw_obex_get_buf_with_apparam(), gw_obex_get_capability(), gw_obex_get_fd(), gw_obex_get_file(), and gw_obex_read_dir().

                                     {
    gboolean ret = FALSE;
    obex_headerdata_t hv;
    obex_object_t *object;

    g_assert(local || buf || stream_fd > 0 || async);
    g_assert(remote || type);

    if (!ctx->done || ctx->xfer) {
        ctx->error = GW_OBEX_ERROR_BUSY;
        return ret;
    }

    ctx->obex_op = OBEX_CMD_GET;

    ctx->xfer = gw_obex_xfer_new(ctx, async, stream_fd);

    object = OBEX_ObjectNew(ctx->handle, OBEX_CMD_GET);

    if (ctx->conid != CONID_INVALID) {
        hv.bq4 = ctx->conid;
        OBEX_ObjectAddHeader(ctx->handle, object, OBEX_HDR_CONNECTION, hv, 4, 0);
    }

    if (apparam && apparam_size > 0) {
        hv.bs = (unsigned char *)apparam;
        OBEX_ObjectAddHeader(ctx->handle, object, OBEX_HDR_APPARAM, hv, apparam_size, 0);
    }

    if (type) {
        hv.bs = (unsigned char *)type;
        OBEX_ObjectAddHeader(ctx->handle, object, OBEX_HDR_TYPE, hv, strlen(type) + 1, 0);
    }

    if (remote) {
        gunichar2 *uname;
        glong uname_len;

        uname_len = get_uname(&uname, remote);
        if (uname_len < 0) {
            OBEX_ObjectDelete(ctx->handle, object);
            ctx->error = GW_OBEX_ERROR_INVALID_PARAMS;
            goto out;
        }

        /* OpenOBEX is buggy and won't append the header unless hv.bs != NULL */
        hv.bs = (unsigned char *) (uname ? (char *)uname : "");

        OBEX_ObjectAddHeader(ctx->handle, object, OBEX_HDR_NAME, hv, uname_len, 0);
        g_free(uname);
    }

    if (local) {
        ctx->xfer->stream_fd = open(local, O_WRONLY | O_CREAT, 0600);
        if (ctx->xfer->stream_fd < 0) {
            if (errno == ENOENT || errno == ENODEV)
                ctx->error = GW_OBEX_ERROR_INVALID_PARAMS;
            else
                ctx->error = GW_OBEX_ERROR_LOCAL_ACCESS;
            debug("open(%s): %s", local, strerror(errno));
            OBEX_ObjectDelete(ctx->handle, object);
            goto out;
        }
    }

    OBEX_ObjectReadStream(ctx->handle, object, NULL);

    if (async) {
        ret = gw_obex_request_async(ctx, object);
        if (ret)
            return ret;
    }
    else {
        ctx->report_progress = TRUE;
        ret = gw_obex_request_sync(ctx, object);
    }

    if (ctx->xfer->stream_fd >= 0 && stream_fd < 0)
        close(ctx->xfer->stream_fd);

    if (ret == FALSE) {
        if (local)
            unlink(local);
    }
    else {
        if (local) {
            debug("%s stored in %s\n", remote ? remote : type, local);
            if (ctx->xfer->modtime != -1) {
                struct utimbuf ubuf;
                ubuf.actime = time(NULL);
                ubuf.modtime = ctx->xfer->modtime;
                if (utime(local, &ubuf) < 0)
                    debug("utime(%s): %s\n", local, g_strerror(errno));
            }
        }
        if (buf) {
            *buf = (gchar *)ctx->xfer->buf;
            *buf_size = ctx->xfer->buf_size;
            /* Make sure gw_obex_xfer_free doesn't free the buffer */
            ctx->xfer->buf = NULL;
        }
    }

out:
    _gw_obex_xfer_free(ctx->xfer);
    ctx->xfer = NULL;

    ctx->report_progress = FALSE;
    ctx->obex_op = OBEX_CMD_NONE;

    return ret;
}


Generated by  Doxygen 1.6.0   Back to index