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

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

Send an object to the server

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

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

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

Referenced by gw_obex_delete(), gw_obex_put_async(), gw_obex_put_buf(), gw_obex_put_buf_with_apparam(), gw_obex_put_fd(), and gw_obex_put_file().

                                                    {
    gboolean ret = FALSE;
    obex_headerdata_t hv;
    obex_object_t *object;
    gunichar2 *uname = NULL;
    glong uname_len = 0;

    g_assert(remote || type);

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

    if (remote) {
        uname_len = get_uname(&uname, remote);
        if (uname_len < 0) {
            ctx->error = GW_OBEX_ERROR_INVALID_PARAMS;
            return FALSE;
        }
    }

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

    if (local) {
        if (file_is_dir(local)) {
            debug("Trying to PUT a directory\n");
            ctx->error = GW_OBEX_ERROR_INVALID_PARAMS;
            goto out;
        }

        ctx->xfer->stream_fd = open(local, O_RDONLY);
        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));
            goto out;
        }
        ctx->xfer->buf = g_malloc(ctx->tx_max);
        ctx->xfer->buf_size = ctx->tx_max;
        debug("Sending %s to %s\n", local, remote ? remote : type);
    }
    else if (buf) {
        ctx->xfer->buf = (unsigned char *)buf;
        ctx->xfer->buf_size = object_size;
        debug("Sending to %s\n", remote ? remote : type);
    }
    else if (stream_fd < 0 && !async) { /* Delete */
        ctx->report_progress = FALSE;
        debug("Deleting %s\n", remote ? remote : type);
    }

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

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

    if (uname) {
        hv.bs = (unsigned char *)uname;
        OBEX_ObjectAddHeader(ctx->handle, object, OBEX_HDR_NAME, hv, uname_len, 0);
        g_free(uname);
        uname = NULL;
    }

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

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

    /* Try to figure out modification time if none was given */
    if (ctx->xfer->stream_fd >= 0) {
        struct stat stats;
        if (fstat(ctx->xfer->stream_fd, &stats) == 0) {
            object_size = stats.st_size;
            if (object_time < 0)
                object_time = stats.st_mtime;
        }
    }

    /* Add a time header if possible */
    if (object_time >= 0) {
        char tstr[17];
        int len;

        len = make_iso8601(object_time, tstr, sizeof(tstr));

        if (len >= 0) {
            debug("Adding time header: %s\n", tstr);
            hv.bs = (unsigned char *)tstr;
            OBEX_ObjectAddHeader(ctx->handle, object, OBEX_HDR_TIME, hv, len, 0);
        }
    }

    /* Add a length header if possible */
    if (object_size > 0) {
        ctx->xfer->target_size = object_size;
        debug("Adding size header: %d\n", object_size);
        hv.bq4 = (uint32_t)object_size;
        OBEX_ObjectAddHeader(ctx->handle, object, OBEX_HDR_LENGTH, hv, 4, 0);
    }
    else
        ctx->xfer->target_size = GW_OBEX_UNKNOWN_LENGTH;

    if (ctx->xfer->stream_fd >= 0 || buf || async) {
        hv.bs = NULL;
        OBEX_ObjectAddHeader(ctx->handle, object, OBEX_HDR_BODY, hv, 0, OBEX_FL_STREAM_START);
    }

    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);
    }

out:
    g_free(uname);

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

    if (buf)
        ctx->xfer->buf = NULL;

    _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