sync

Backup service to store encrypted wallet databases (experimental)
Log | Files | Refs | Submodules | README | LICENSE

sync_api_object_upload.c (3703B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2014-2026 Taler Systems SA
      4 
      5   TALER is free software; you can redistribute it and/or modify
      6   it under the terms of the GNU Lesser General Public License as
      7   published by the Free Software Foundation; either version 2.1,
      8   or (at your option) any later version.
      9 
     10   TALER is distributed in the hope that it will be useful, but
     11   WITHOUT ANY WARRANTY; without even the implied warranty of
     12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13   GNU Lesser General Public License for more details.
     14 
     15   You should have received a copy of the GNU Lesser General Public
     16   License along with TALER; see the file COPYING.LGPL.  If not,
     17   see <http://www.gnu.org/licenses/>
     18 */
     19 /**
     20  * @file lib/sync_api_object_upload.c
     21  * @brief Implementation of the object upload (POST) operation
     22  * @author Iván Ávalos
     23  */
     24 #include "sync_api_block_operation_common.h"
     25 
     26 
     27 struct SYNC_BlockOperation *
     28 SYNC_object_upload (struct GNUNET_CURL_Context *ctx,
     29                     const char *base_url,
     30                     const struct SYNC_AccountPrivateKeyP *priv,
     31                     const struct SYNC_ObjectUID *uid,
     32                     size_t data_size,
     33                     const void *data,
     34                     SYNC_BlockOperationCallback cb,
     35                     void *cb_cls)
     36 {
     37   struct SYNC_BlockOperation *op;
     38   struct SYNC_AccountSignatureP account_sig;
     39   struct GNUNET_HashCode data_hash;
     40   CURL *eh;
     41 
     42   /* Compute hash of data */
     43   GNUNET_CRYPTO_hash (data,
     44                       data_size,
     45                       &data_hash);
     46 
     47   /* Build signature */
     48   SYNC_object_upload_sign (priv,
     49                            uid,
     50                            &data_hash,
     51                            &account_sig);
     52 
     53   /* Allocate operation handle */
     54   op = GNUNET_new (struct SYNC_BlockOperation);
     55   op->ctx = ctx;
     56   op->cb = cb;
     57   op->cb_cls = cb_cls;
     58   op->post_ctx.disable_compression = true;
     59   op->url = SYNC_block_operation_make_url_ (base_url,
     60                                             priv,
     61                                             "objects",
     62                                             sizeof (*uid),
     63                                             uid);
     64 
     65   /* Build JSON body */
     66   {
     67     json_t *req;
     68 
     69     req = GNUNET_JSON_PACK (
     70       GNUNET_JSON_pack_data_auto ("object_sig",
     71                                   &account_sig),
     72       GNUNET_JSON_pack_data_varsize ("data",
     73                                      data,
     74                                      data_size));
     75 
     76     /* Set up CURL easy handle */
     77     eh = SYNC_curl_easy_get_ (op->url);
     78     if (NULL == eh)
     79     {
     80       GNUNET_break (0);
     81       json_decref (req);
     82       goto cleanup;
     83     }
     84 
     85     GNUNET_assert (CURLE_OK ==
     86                    curl_easy_setopt (eh,
     87                                      CURLOPT_HEADERFUNCTION,
     88                                      &SYNC_block_operation_handle_header_));
     89     GNUNET_assert (CURLE_OK ==
     90                    curl_easy_setopt (eh,
     91                                      CURLOPT_HEADERDATA,
     92                                      op));
     93 
     94     if (GNUNET_OK !=
     95         TALER_curl_easy_post (&op->post_ctx,
     96                               eh,
     97                               req))
     98     {
     99       GNUNET_break (0);
    100       json_decref (req);
    101       curl_easy_cleanup (eh);
    102       goto cleanup;
    103     }
    104     json_decref (req);
    105   }
    106 
    107   op->job = GNUNET_CURL_job_add2 (ctx,
    108                                   eh,
    109                                   op->post_ctx.headers,
    110                                   &SYNC_block_operation_handle_finished_,
    111                                   op);
    112   return op;
    113 
    114 cleanup:
    115   GNUNET_free (op->url);
    116   GNUNET_free (op);
    117   return NULL;
    118 }
    119 
    120 
    121 /* end of sync_api_object_upload.c */