Guest User

Download Function

a guest
May 27th, 2014
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.43 KB | None | 0 0
  1. int
  2. deltacloud_download_blob(struct deltacloud_api csp, char *bucket_id, char *blob_id, char *cache_path)
  3. {
  4.     struct deltacloud_bucket bucket;
  5.     if(deltacloud_get_bucket_by_id(&csp, bucket_id, &bucket)< 0)
  6.     {
  7.         fprintf(stderr, "DOWNLOAD: Failed to obtain bucket %s: %s.\n", bucket_id, deltacloud_get_last_error_string());
  8.         return -1;
  9.     }
  10.  
  11.     struct deltacloud_bucket_blob blob;
  12.     if(deltacloud_bucket_get_blob_by_id(&csp, &bucket, blob_id, &blob)< 0)
  13.     {
  14.         fprintf(stderr, "DOWNLOAD: Failed to obtain file %s: %s.\n", blob_id, deltacloud_get_last_error_string());
  15.         return -1;             
  16.     }
  17.  
  18.     FILE *fp;
  19.     if((fp= fopen(cache_path, "w"))== NULL)
  20.     {
  21.         fprintf(stderr, "DOWNLOAD: Failed to create file %s.\n", cache_path);
  22.         return -1; 
  23.     }
  24.  
  25.     u_int64_t blob_size= atoll(blob.content_length);
  26.    
  27.     char *buffer;
  28.     fprintf(stderr, "DOWNLOAD: Getting a %d bytes-large file.\n", blob_size);
  29.     if(deltacloud_bucket_blob_get_content(&csp, &blob, &buffer)< 0)
  30.     {
  31.         fprintf(stderr, "DOWNLOAD: Failed to obtain contents of blob %s: %s.\n", blob_id, deltacloud_get_last_error_string());
  32.         return -1;         
  33.     }
  34.  
  35.     if((fwrite(buffer, sizeof(char), blob_size, fp))< blob_size)
  36.     {
  37.         fprintf(stderr, "DOWNLOAD: Failed to write contents of blob %s into the cache: %s.\n", blob_id, deltacloud_get_last_error_string());
  38.         return -1;
  39.     }
  40.     fflush(fp);
  41.     fclose(fp);
  42.  
  43.     free(buffer);
  44.  
  45.     deltacloud_free_bucket_blob(&blob);
  46.     deltacloud_free_bucket(&bucket);
  47.  
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment