2 * Copyright (c) 2008, 2009, 2010 Zmanda, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published
6 * by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18 * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
21 /* An S3 device uses Amazon's S3 service (http://www.amazon.com/s3) to store
22 * data. It stores data in keys named with a user-specified prefix, inside a
23 * user-specified bucket. Data is stored in the form of numbered (large)
29 #include <sys/types.h>
39 #include <curl/curl.h>
40 #ifdef HAVE_OPENSSL_HMAC_H
41 # include <openssl/hmac.h>
43 # ifdef HAVE_CRYPTO_HMAC_H
44 # include <crypto/hmac.h>
53 * Type checking and casting macros
55 #define TYPE_S3_DEVICE (s3_device_get_type())
56 #define S3_DEVICE(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), s3_device_get_type(), S3Device)
57 #define S3_DEVICE_CONST(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), s3_device_get_type(), S3Device const)
58 #define S3_DEVICE_CLASS(klass) G_TYPE_CHECK_CLASS_CAST((klass), s3_device_get_type(), S3DeviceClass)
59 #define IS_S3_DEVICE(obj) G_TYPE_CHECK_INSTANCE_TYPE((obj), s3_device_get_type ())
61 #define S3_DEVICE_GET_CLASS(obj) G_TYPE_INSTANCE_GET_CLASS((obj), s3_device_get_type(), S3DeviceClass)
62 static GType s3_device_get_type (void);
65 * Main object structure
67 typedef struct _S3MetadataFile S3MetadataFile;
69 typedef struct _S3Device S3Device;
73 /* The "easy" curl handle we use to access Amazon S3 */
76 /* S3 access information */
80 /* The S3 access information. */
85 char *bucket_location;
89 /* a cache for unsuccessful reads (where we get the file but the caller
90 * doesn't have space for it or doesn't want it), where we expect the
91 * next call will request the same file.
97 /* Produce verbose output? */
104 guint64 max_send_speed;
105 guint64 max_recv_speed;
111 typedef struct _S3DeviceClass S3DeviceClass;
112 struct _S3DeviceClass {
113 DeviceClass __parent__;
118 * Constants and static data
121 #define S3_DEVICE_NAME "s3"
123 /* Maximum key length as specified in the S3 documentation
124 * (*excluding* null terminator) */
125 #define S3_MAX_KEY_LENGTH 1024
127 /* Note: for compatability, min can only be decreased and max increased */
128 #define S3_DEVICE_MIN_BLOCK_SIZE 1024
129 #define S3_DEVICE_MAX_BLOCK_SIZE (100*1024*1024)
130 #define S3_DEVICE_DEFAULT_BLOCK_SIZE (10*1024*1024)
132 /* This goes in lieu of file number for metadata. */
133 #define SPECIAL_INFIX "special-"
135 /* pointer to the class of our parent */
136 static DeviceClass *parent_class = NULL;
139 * device-specific properties
142 /* Authentication information for Amazon S3. Both of these are strings. */
143 static DevicePropertyBase device_property_s3_access_key;
144 static DevicePropertyBase device_property_s3_secret_key;
145 #define PROPERTY_S3_SECRET_KEY (device_property_s3_secret_key.ID)
146 #define PROPERTY_S3_ACCESS_KEY (device_property_s3_access_key.ID)
148 /* Same, but for S3 with DevPay. */
149 static DevicePropertyBase device_property_s3_user_token;
150 #define PROPERTY_S3_USER_TOKEN (device_property_s3_user_token.ID)
152 /* Location constraint for new buckets created on Amazon S3. */
153 static DevicePropertyBase device_property_s3_bucket_location;
154 #define PROPERTY_S3_BUCKET_LOCATION (device_property_s3_bucket_location.ID)
156 /* Path to certificate authority certificate */
157 static DevicePropertyBase device_property_ssl_ca_info;
158 #define PROPERTY_SSL_CA_INFO (device_property_ssl_ca_info.ID)
160 /* Whether to use SSL with Amazon S3. */
161 static DevicePropertyBase device_property_s3_ssl;
162 #define PROPERTY_S3_SSL (device_property_s3_ssl.ID)
164 /* Speed limits for sending and receiving */
165 static DevicePropertyBase device_property_max_send_speed;
166 static DevicePropertyBase device_property_max_recv_speed;
167 #define PROPERTY_MAX_SEND_SPEED (device_property_max_send_speed.ID)
168 #define PROPERTY_MAX_RECV_SPEED (device_property_max_recv_speed.ID)
175 void s3_device_register(void);
178 * utility functions */
180 /* Given file and block numbers, return an S3 key.
182 * @param self: the S3Device object
183 * @param file: the file number
184 * @param block: the block within that file
185 * @returns: a newly allocated string containing an S3 key.
188 file_and_block_to_key(S3Device *self,
192 /* Given the name of a special file (such as 'tapestart'), generate
193 * the S3 key to use for that file.
195 * @param self: the S3Device object
196 * @param special_name: name of the special file
197 * @param file: a file number to include; omitted if -1
198 * @returns: a newly alocated string containing an S3 key.
201 special_file_to_key(S3Device *self,
204 /* Write an amanda header file to S3.
206 * @param self: the S3Device object
207 * @param label: the volume label
208 * @param timestamp: the volume timestamp
211 write_amanda_header(S3Device *self,
215 /* "Fast forward" this device to the end by looking up the largest file number
216 * present and setting the current file number one greater.
218 * @param self: the S3Device object
221 seek_to_end(S3Device *self);
223 /* Find the number of the last file that contains any data (even just a header).
225 * @param self: the S3Device object
226 * @returns: the last file, or -1 in event of an error
229 find_last_file(S3Device *self);
231 /* Delete all blocks in the given file, including the filestart block
233 * @param self: the S3Device object
234 * @param file: the file to delete
237 delete_file(S3Device *self,
241 /* Delete all files in the given device
243 * @param self: the S3Device object
246 delete_all_files(S3Device *self);
248 /* Set up self->s3 as best as possible.
250 * The return value is TRUE iff self->s3 is useable.
252 * @param self: the S3Device object
253 * @returns: TRUE if the handle is set up
256 setup_handle(S3Device * self);
262 s3_device_init(S3Device * o);
265 s3_device_class_init(S3DeviceClass * c);
268 s3_device_finalize(GObject * o);
271 s3_device_factory(char * device_name, char * device_type, char * device_node);
274 * Property{Get,Set}Fns */
276 static gboolean s3_device_set_access_key_fn(Device *self,
277 DevicePropertyBase *base, GValue *val,
278 PropertySurety surety, PropertySource source);
280 static gboolean s3_device_set_secret_key_fn(Device *self,
281 DevicePropertyBase *base, GValue *val,
282 PropertySurety surety, PropertySource source);
284 static gboolean s3_device_set_user_token_fn(Device *self,
285 DevicePropertyBase *base, GValue *val,
286 PropertySurety surety, PropertySource source);
288 static gboolean s3_device_set_bucket_location_fn(Device *self,
289 DevicePropertyBase *base, GValue *val,
290 PropertySurety surety, PropertySource source);
292 static gboolean s3_device_set_ca_info_fn(Device *self,
293 DevicePropertyBase *base, GValue *val,
294 PropertySurety surety, PropertySource source);
296 static gboolean s3_device_set_verbose_fn(Device *self,
297 DevicePropertyBase *base, GValue *val,
298 PropertySurety surety, PropertySource source);
300 static gboolean s3_device_set_ssl_fn(Device *self,
301 DevicePropertyBase *base, GValue *val,
302 PropertySurety surety, PropertySource source);
304 static gboolean s3_device_set_max_send_speed_fn(Device *self,
305 DevicePropertyBase *base, GValue *val,
306 PropertySurety surety, PropertySource source);
308 static gboolean s3_device_set_max_recv_speed_fn(Device *self,
309 DevicePropertyBase *base, GValue *val,
310 PropertySurety surety, PropertySource source);
313 * virtual functions */
316 s3_device_open_device(Device *pself, char *device_name,
317 char * device_type, char * device_node);
319 static DeviceStatusFlags s3_device_read_label(Device * self);
322 s3_device_start(Device * self,
323 DeviceAccessMode mode,
328 s3_device_finish(Device * self);
331 s3_device_start_file(Device * self,
332 dumpfile_t * jobInfo);
335 s3_device_write_block(Device * self,
340 s3_device_finish_file(Device * self);
343 s3_device_seek_file(Device *pself,
347 s3_device_seek_block(Device *pself,
351 s3_device_read_block(Device * pself,
356 s3_device_recycle_file(Device *pself,
360 s3_device_erase(Device *pself);
367 file_and_block_to_key(S3Device *self,
371 char *s3_key = g_strdup_printf("%sf%08x-b%016llx.data",
372 self->prefix, file, (long long unsigned int)block);
373 g_assert(strlen(s3_key) <= S3_MAX_KEY_LENGTH);
378 special_file_to_key(S3Device *self,
383 return g_strdup_printf("%s" SPECIAL_INFIX "%s", self->prefix, special_name);
385 return g_strdup_printf("%sf%08x-%s", self->prefix, file, special_name);
389 write_amanda_header(S3Device *self,
393 CurlBuffer amanda_header = {NULL, 0, 0, 0};
396 dumpfile_t * dumpinfo = NULL;
397 Device *d_self = DEVICE(self);
400 /* build the header */
401 header_size = 0; /* no minimum size */
402 dumpinfo = make_tapestart_header(DEVICE(self), label, timestamp);
403 amanda_header.buffer = device_build_amanda_header(DEVICE(self), dumpinfo,
405 if (amanda_header.buffer == NULL) {
406 device_set_error(d_self,
407 stralloc(_("Amanda tapestart header won't fit in a single block!")),
408 DEVICE_STATUS_DEVICE_ERROR);
409 dumpfile_free(dumpinfo);
410 g_free(amanda_header.buffer);
414 /* write out the header and flush the uploads. */
415 key = special_file_to_key(self, "tapestart", -1);
416 g_assert(header_size < G_MAXUINT); /* for cast to guint */
417 amanda_header.buffer_len = (guint)header_size;
418 result = s3_upload(self->s3, self->bucket, key, S3_BUFFER_READ_FUNCS,
419 &amanda_header, NULL, NULL);
420 g_free(amanda_header.buffer);
424 device_set_error(d_self,
425 vstrallocf(_("While writing amanda header: %s"), s3_strerror(self->s3)),
426 DEVICE_STATUS_DEVICE_ERROR | DEVICE_STATUS_VOLUME_ERROR);
427 dumpfile_free(dumpinfo);
429 dumpfile_free(d_self->volume_header);
430 d_self->volume_header = dumpinfo;
437 seek_to_end(S3Device *self) {
440 Device *pself = DEVICE(self);
442 last_file = find_last_file(self);
446 pself->file = last_file;
451 /* Convert an object name into a file number, assuming the given prefix
452 * length. Returns -1 if the object name is invalid, or 0 if the object name
453 * is a "special" key. */
454 static int key_to_file(guint prefix_len, const char * key) {
458 /* skip the prefix */
459 if (strlen(key) <= prefix_len)
464 if (strncmp(key, SPECIAL_INFIX, strlen(SPECIAL_INFIX)) == 0) {
468 /* check that key starts with 'f' */
473 /* check that key is of the form "%08x-" */
474 for (i = 0; i < 8; i++) {
475 if (!(key[i] >= '0' && key[i] <= '9') &&
476 !(key[i] >= 'a' && key[i] <= 'f') &&
477 !(key[i] >= 'A' && key[i] <= 'F')) break;
479 if (key[i] != '-') return -1;
480 if (i < 8) return -1;
482 /* convert the file number */
484 file = strtoul(key, NULL, 16);
486 g_warning(_("unparseable file number '%s'"), key);
493 /* Find the number of the last file that contains any data (even just a header).
494 * Returns -1 in event of an error
497 find_last_file(S3Device *self) {
500 unsigned int prefix_len = strlen(self->prefix);
502 Device *d_self = DEVICE(self);
504 /* list all keys matching C{PREFIX*-*}, stripping the C{-*} */
505 result = s3_list_keys(self->s3, self->bucket, self->prefix, "-", &keys);
507 device_set_error(d_self,
508 vstrallocf(_("While listing S3 keys: %s"), s3_strerror(self->s3)),
509 DEVICE_STATUS_DEVICE_ERROR | DEVICE_STATUS_VOLUME_ERROR);
513 for (; keys; keys = g_slist_remove(keys, keys->data)) {
514 int file = key_to_file(prefix_len, keys->data);
516 /* and if it's the last, keep it */
517 if (file > last_file)
524 /* Find the number of the file following the requested one, if any.
525 * Returns 0 if there is no such file or -1 in event of an error
528 find_next_file(S3Device *self, int last_file) {
531 unsigned int prefix_len = strlen(self->prefix);
533 Device *d_self = DEVICE(self);
535 /* list all keys matching C{PREFIX*-*}, stripping the C{-*} */
536 result = s3_list_keys(self->s3, self->bucket, self->prefix, "-", &keys);
538 device_set_error(d_self,
539 vstrallocf(_("While listing S3 keys: %s"), s3_strerror(self->s3)),
540 DEVICE_STATUS_DEVICE_ERROR | DEVICE_STATUS_VOLUME_ERROR);
544 for (; keys; keys = g_slist_remove(keys, keys->data)) {
547 file = key_to_file(prefix_len, (char*)keys->data);
550 /* Set this in case we don't find a next file; this is not a
551 * hard error, so if we can find a next file we'll return that
556 if (file < next_file && file > last_file) {
565 delete_file(S3Device *self,
570 char *my_prefix = g_strdup_printf("%sf%08x-", self->prefix, file);
571 Device *d_self = DEVICE(self);
573 result = s3_list_keys(self->s3, self->bucket, my_prefix, NULL, &keys);
575 device_set_error(d_self,
576 vstrallocf(_("While listing S3 keys: %s"), s3_strerror(self->s3)),
577 DEVICE_STATUS_DEVICE_ERROR | DEVICE_STATUS_VOLUME_ERROR);
581 /* this will likely be a *lot* of keys */
582 for (; keys; keys = g_slist_remove(keys, keys->data)) {
583 if (self->verbose) g_debug(_("Deleting %s"), (char*)keys->data);
584 if (!s3_delete(self->s3, self->bucket, keys->data)) {
585 device_set_error(d_self,
586 vstrallocf(_("While deleting key '%s': %s"),
587 (char*)keys->data, s3_strerror(self->s3)),
588 DEVICE_STATUS_DEVICE_ERROR);
598 delete_all_files(S3Device *self)
603 * Note: this has to be allowed to retry for a while because the bucket
604 * may have been created and not yet appeared
606 last_file = find_last_file(self);
609 s3_error_code_t s3_error_code;
610 s3_error(self->s3, NULL, &response_code, &s3_error_code, NULL, NULL, NULL);
613 * if the bucket doesn't exist, it doesn't conatin any files,
614 * so the operation is a success
616 if ((response_code == 404 && s3_error_code == S3_ERROR_NoSuchBucket)) {
617 /* find_last_file set an error; clear it */
618 device_set_error(DEVICE(self), NULL, DEVICE_STATUS_SUCCESS);
621 /* find_last_file already set the error */
626 for (file = 1; file <= last_file; file++) {
627 if (!delete_file(self, file))
628 /* delete_file already set our error message */
640 s3_device_register(void)
642 static const char * device_prefix_list[] = { S3_DEVICE_NAME, NULL };
645 /* set up our properties */
646 device_property_fill_and_register(&device_property_s3_secret_key,
647 G_TYPE_STRING, "s3_secret_key",
648 "Secret access key to authenticate with Amazon S3");
649 device_property_fill_and_register(&device_property_s3_access_key,
650 G_TYPE_STRING, "s3_access_key",
651 "Access key ID to authenticate with Amazon S3");
652 device_property_fill_and_register(&device_property_s3_user_token,
653 G_TYPE_STRING, "s3_user_token",
654 "User token for authentication Amazon devpay requests");
655 device_property_fill_and_register(&device_property_s3_bucket_location,
656 G_TYPE_STRING, "s3_bucket_location",
657 "Location constraint for buckets on Amazon S3");
658 device_property_fill_and_register(&device_property_ssl_ca_info,
659 G_TYPE_STRING, "ssl_ca_info",
660 "Path to certificate authority certificate");
661 device_property_fill_and_register(&device_property_s3_ssl,
662 G_TYPE_BOOLEAN, "s3_ssl",
663 "Whether to use SSL with Amazon S3");
664 device_property_fill_and_register(&device_property_max_send_speed,
665 G_TYPE_UINT64, "max_send_speed",
666 "Maximum average upload speed (bytes/sec)");
667 device_property_fill_and_register(&device_property_max_recv_speed,
668 G_TYPE_UINT64, "max_recv_speed",
669 "Maximum average download speed (bytes/sec)");
671 /* register the device itself */
672 register_device(s3_device_factory, device_prefix_list);
676 s3_device_get_type(void)
678 static GType type = 0;
680 if G_UNLIKELY(type == 0) {
681 static const GTypeInfo info = {
682 sizeof (S3DeviceClass),
683 (GBaseInitFunc) NULL,
684 (GBaseFinalizeFunc) NULL,
685 (GClassInitFunc) s3_device_class_init,
686 (GClassFinalizeFunc) NULL,
687 NULL /* class_data */,
690 (GInstanceInitFunc) s3_device_init,
694 type = g_type_register_static (TYPE_DEVICE, "S3Device", &info,
702 s3_device_init(S3Device * self)
704 Device * dself = DEVICE(self);
707 /* Register property values
708 * Note: Some aren't added until s3_device_open_device()
710 bzero(&response, sizeof(response));
712 g_value_init(&response, CONCURRENCY_PARADIGM_TYPE);
713 g_value_set_enum(&response, CONCURRENCY_PARADIGM_SHARED_READ);
714 device_set_simple_property(dself, PROPERTY_CONCURRENCY,
715 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
716 g_value_unset(&response);
718 g_value_init(&response, STREAMING_REQUIREMENT_TYPE);
719 g_value_set_enum(&response, STREAMING_REQUIREMENT_NONE);
720 device_set_simple_property(dself, PROPERTY_STREAMING,
721 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
722 g_value_unset(&response);
724 g_value_init(&response, G_TYPE_BOOLEAN);
725 g_value_set_boolean(&response, TRUE);
726 device_set_simple_property(dself, PROPERTY_APPENDABLE,
727 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
728 g_value_unset(&response);
730 g_value_init(&response, G_TYPE_BOOLEAN);
731 g_value_set_boolean(&response, TRUE);
732 device_set_simple_property(dself, PROPERTY_PARTIAL_DELETION,
733 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
734 g_value_unset(&response);
736 g_value_init(&response, G_TYPE_BOOLEAN);
737 g_value_set_boolean(&response, TRUE);
738 device_set_simple_property(dself, PROPERTY_FULL_DELETION,
739 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
740 g_value_unset(&response);
742 g_value_init(&response, G_TYPE_BOOLEAN);
743 g_value_set_boolean(&response, FALSE);
744 device_set_simple_property(dself, PROPERTY_COMPRESSION,
745 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
746 g_value_unset(&response);
748 g_value_init(&response, MEDIA_ACCESS_MODE_TYPE);
749 g_value_set_enum(&response, MEDIA_ACCESS_MODE_READ_WRITE);
750 device_set_simple_property(dself, PROPERTY_MEDIUM_ACCESS_TYPE,
751 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
752 g_value_unset(&response);
757 s3_device_class_init(S3DeviceClass * c G_GNUC_UNUSED)
759 GObjectClass *g_object_class = (GObjectClass*) c;
760 DeviceClass *device_class = (DeviceClass *)c;
762 parent_class = g_type_class_ref (TYPE_DEVICE);
764 device_class->open_device = s3_device_open_device;
765 device_class->read_label = s3_device_read_label;
766 device_class->start = s3_device_start;
767 device_class->finish = s3_device_finish;
769 device_class->start_file = s3_device_start_file;
770 device_class->write_block = s3_device_write_block;
771 device_class->finish_file = s3_device_finish_file;
773 device_class->seek_file = s3_device_seek_file;
774 device_class->seek_block = s3_device_seek_block;
775 device_class->read_block = s3_device_read_block;
776 device_class->recycle_file = s3_device_recycle_file;
778 device_class->erase = s3_device_erase;
780 g_object_class->finalize = s3_device_finalize;
782 device_class_register_property(device_class, PROPERTY_S3_ACCESS_KEY,
783 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
784 device_simple_property_get_fn,
785 s3_device_set_access_key_fn);
787 device_class_register_property(device_class, PROPERTY_S3_SECRET_KEY,
788 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
789 device_simple_property_get_fn,
790 s3_device_set_secret_key_fn);
792 device_class_register_property(device_class, PROPERTY_S3_USER_TOKEN,
793 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
794 device_simple_property_get_fn,
795 s3_device_set_user_token_fn);
797 device_class_register_property(device_class, PROPERTY_S3_BUCKET_LOCATION,
798 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
799 device_simple_property_get_fn,
800 s3_device_set_bucket_location_fn);
802 device_class_register_property(device_class, PROPERTY_SSL_CA_INFO,
803 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
804 device_simple_property_get_fn,
805 s3_device_set_ca_info_fn);
807 device_class_register_property(device_class, PROPERTY_VERBOSE,
808 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
809 device_simple_property_get_fn,
810 s3_device_set_verbose_fn);
812 device_class_register_property(device_class, PROPERTY_S3_SSL,
813 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
814 device_simple_property_get_fn,
815 s3_device_set_ssl_fn);
817 device_class_register_property(device_class, PROPERTY_MAX_SEND_SPEED,
818 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
819 device_simple_property_get_fn,
820 s3_device_set_max_send_speed_fn);
822 device_class_register_property(device_class, PROPERTY_MAX_RECV_SPEED,
823 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
824 device_simple_property_get_fn,
825 s3_device_set_max_recv_speed_fn);
827 device_class_register_property(device_class, PROPERTY_COMPRESSION,
828 PROPERTY_ACCESS_GET_MASK,
829 device_simple_property_get_fn,
834 s3_device_set_access_key_fn(Device *p_self, DevicePropertyBase *base,
835 GValue *val, PropertySurety surety, PropertySource source)
837 S3Device *self = S3_DEVICE(p_self);
839 amfree(self->access_key);
840 self->access_key = g_value_dup_string(val);
841 device_clear_volume_details(p_self);
843 return device_simple_property_set_fn(p_self, base, val, surety, source);
847 s3_device_set_secret_key_fn(Device *p_self, DevicePropertyBase *base,
848 GValue *val, PropertySurety surety, PropertySource source)
850 S3Device *self = S3_DEVICE(p_self);
852 amfree(self->secret_key);
853 self->secret_key = g_value_dup_string(val);
854 device_clear_volume_details(p_self);
856 return device_simple_property_set_fn(p_self, base, val, surety, source);
860 s3_device_set_user_token_fn(Device *p_self, DevicePropertyBase *base,
861 GValue *val, PropertySurety surety, PropertySource source)
863 S3Device *self = S3_DEVICE(p_self);
865 amfree(self->user_token);
866 self->user_token = g_value_dup_string(val);
867 device_clear_volume_details(p_self);
869 return device_simple_property_set_fn(p_self, base, val, surety, source);
873 s3_device_set_bucket_location_fn(Device *p_self, DevicePropertyBase *base,
874 GValue *val, PropertySurety surety, PropertySource source)
876 S3Device *self = S3_DEVICE(p_self);
877 char *str_val = g_value_dup_string(val);
879 if (str_val[0] && self->use_ssl && !s3_curl_location_compat()) {
880 device_set_error(p_self, stralloc(_(
881 "Location constraint given for Amazon S3 bucket, "
882 "but libcurl is too old support wildcard certificates.")),
883 DEVICE_STATUS_DEVICE_ERROR);
887 if (str_val[0] && !s3_bucket_location_compat(self->bucket)) {
888 device_set_error(p_self, g_strdup_printf(_(
889 "Location constraint given for Amazon S3 bucket, "
890 "but the bucket name (%s) is not usable as a subdomain."),
892 DEVICE_STATUS_DEVICE_ERROR);
896 amfree(self->bucket_location);
897 self->bucket_location = g_value_dup_string(val);
898 device_clear_volume_details(p_self);
900 return device_simple_property_set_fn(p_self, base, val, surety, source);
907 s3_device_set_ca_info_fn(Device *p_self, DevicePropertyBase *base,
908 GValue *val, PropertySurety surety, PropertySource source)
910 S3Device *self = S3_DEVICE(p_self);
912 if (!self->use_ssl) {
913 device_set_error(p_self, stralloc(_(
914 "Path to certificate authority certificate can not be "
915 "set if SSL/TLS is not being used.")),
916 DEVICE_STATUS_DEVICE_ERROR);
920 amfree(self->ca_info);
921 self->ca_info = g_value_dup_string(val);
922 device_clear_volume_details(p_self);
924 return device_simple_property_set_fn(p_self, base, val, surety, source);
928 s3_device_set_verbose_fn(Device *p_self, DevicePropertyBase *base,
929 GValue *val, PropertySurety surety, PropertySource source)
931 S3Device *self = S3_DEVICE(p_self);
933 self->verbose = g_value_get_boolean(val);
934 /* Our S3 handle may not yet have been instantiated; if so, it will
935 * get the proper verbose setting when it is created */
937 s3_verbose(self->s3, self->verbose);
939 return device_simple_property_set_fn(p_self, base, val, surety, source);
943 s3_device_set_ssl_fn(Device *p_self, DevicePropertyBase *base,
944 GValue *val, PropertySurety surety, PropertySource source)
946 S3Device *self = S3_DEVICE(p_self);
949 new_val = g_value_get_boolean(val);
950 /* Our S3 handle may not yet have been instantiated; if so, it will
951 * get the proper use_ssl setting when it is created */
952 if (self->s3 && !s3_use_ssl(self->s3, new_val)) {
953 device_set_error(p_self, g_strdup_printf(_(
954 "Error setting S3 SSL/TLS use "
955 "(tried to enable SSL/TLS for S3, but curl doesn't support it?)")),
956 DEVICE_STATUS_DEVICE_ERROR);
959 self->use_ssl = new_val;
961 return device_simple_property_set_fn(p_self, base, val, surety, source);
965 s3_device_set_max_send_speed_fn(Device *p_self,
966 DevicePropertyBase *base, GValue *val,
967 PropertySurety surety, PropertySource source)
969 S3Device *self = S3_DEVICE(p_self);
972 new_val = g_value_get_uint64(val);
973 if (self->s3 && !s3_set_max_send_speed(self->s3, new_val)) {
974 device_set_error(p_self,
975 g_strdup("Could not set S3 maximum send speed"),
976 DEVICE_STATUS_DEVICE_ERROR);
979 self->max_send_speed = new_val;
981 return device_simple_property_set_fn(p_self, base, val, surety, source);
985 s3_device_set_max_recv_speed_fn(Device *p_self,
986 DevicePropertyBase *base, GValue *val,
987 PropertySurety surety, PropertySource source)
989 S3Device *self = S3_DEVICE(p_self);
992 new_val = g_value_get_uint64(val);
993 if (self->s3 && !s3_set_max_recv_speed(self->s3, new_val)) {
994 device_set_error(p_self,
995 g_strdup("Could not set S3 maximum recv speed"),
996 DEVICE_STATUS_DEVICE_ERROR);
999 self->max_recv_speed = new_val;
1001 return device_simple_property_set_fn(p_self, base, val, surety, source);
1005 s3_device_factory(char * device_name, char * device_type, char * device_node)
1009 g_assert(0 == strcmp(device_type, S3_DEVICE_NAME));
1010 rval = DEVICE(g_object_new(TYPE_S3_DEVICE, NULL));
1011 s3_rval = (S3Device*)rval;
1013 device_open_device(rval, device_name, device_type, device_node);
1018 * Virtual function overrides
1022 s3_device_open_device(Device *pself, char *device_name,
1023 char * device_type, char * device_node)
1025 S3Device *self = S3_DEVICE(pself);
1029 pself->min_block_size = S3_DEVICE_MIN_BLOCK_SIZE;
1030 pself->max_block_size = S3_DEVICE_MAX_BLOCK_SIZE;
1031 pself->block_size = S3_DEVICE_DEFAULT_BLOCK_SIZE;
1033 /* Device name may be bucket/prefix, to support multiple volumes in a
1035 name_colon = strchr(device_node, '/');
1036 if (name_colon == NULL) {
1037 self->bucket = g_strdup(device_node);
1038 self->prefix = g_strdup("");
1040 self->bucket = g_strndup(device_node, name_colon - device_node);
1041 self->prefix = g_strdup(name_colon + 1);
1044 if (self->bucket == NULL || self->bucket[0] == '\0') {
1045 device_set_error(pself,
1046 vstrallocf(_("Empty bucket name in device %s"), device_name),
1047 DEVICE_STATUS_DEVICE_ERROR);
1048 amfree(self->bucket);
1049 amfree(self->prefix);
1053 g_debug(_("S3 driver using bucket '%s', prefix '%s'"), self->bucket, self->prefix);
1055 /* default values */
1056 self->verbose = FALSE;
1058 /* use SSL if available */
1059 self->use_ssl = s3_curl_supports_ssl();
1060 bzero(&tmp_value, sizeof(GValue));
1061 g_value_init(&tmp_value, G_TYPE_BOOLEAN);
1062 g_value_set_boolean(&tmp_value, self->use_ssl);
1063 device_set_simple_property(pself, device_property_s3_ssl.ID,
1064 &tmp_value, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DEFAULT);
1066 if (parent_class->open_device) {
1067 parent_class->open_device(pself, device_name, device_type, device_node);
1071 static void s3_device_finalize(GObject * obj_self) {
1072 S3Device *self = S3_DEVICE (obj_self);
1074 if(G_OBJECT_CLASS(parent_class)->finalize)
1075 (* G_OBJECT_CLASS(parent_class)->finalize)(obj_self);
1077 if(self->s3) s3_free(self->s3);
1078 if(self->bucket) g_free(self->bucket);
1079 if(self->prefix) g_free(self->prefix);
1080 if(self->access_key) g_free(self->access_key);
1081 if(self->secret_key) g_free(self->secret_key);
1082 if(self->user_token) g_free(self->user_token);
1083 if(self->bucket_location) g_free(self->bucket_location);
1084 if(self->ca_info) g_free(self->ca_info);
1087 static gboolean setup_handle(S3Device * self) {
1088 Device *d_self = DEVICE(self);
1089 if (self->s3 == NULL) {
1091 if (self->access_key == NULL || self->access_key[0] == '\0') {
1092 device_set_error(d_self,
1093 stralloc(_("No Amazon access key specified")),
1094 DEVICE_STATUS_DEVICE_ERROR);
1098 if (self->secret_key == NULL || self->secret_key[0] == '\0') {
1099 device_set_error(d_self,
1100 stralloc(_("No Amazon secret key specified")),
1101 DEVICE_STATUS_DEVICE_ERROR);
1105 self->s3 = s3_open(self->access_key, self->secret_key, self->user_token,
1106 self->bucket_location, self->ca_info);
1107 if (self->s3 == NULL) {
1108 device_set_error(d_self,
1109 stralloc(_("Internal error creating S3 handle")),
1110 DEVICE_STATUS_DEVICE_ERROR);
1115 s3_verbose(self->s3, self->verbose);
1117 if (!s3_use_ssl(self->s3, self->use_ssl)) {
1118 device_set_error(d_self, g_strdup_printf(_(
1119 "Error setting S3 SSL/TLS use "
1120 "(tried to enable SSL/TLS for S3, but curl doesn't support it?)")),
1121 DEVICE_STATUS_DEVICE_ERROR);
1125 if (self->max_send_speed &&
1126 !s3_set_max_send_speed(self->s3, self->max_send_speed)) {
1127 device_set_error(d_self,
1128 g_strdup("Could not set S3 maximum send speed"),
1129 DEVICE_STATUS_DEVICE_ERROR);
1133 if (self->max_recv_speed &&
1134 !s3_set_max_recv_speed(self->s3, self->max_recv_speed)) {
1135 device_set_error(d_self,
1136 g_strdup("Could not set S3 maximum recv speed"),
1137 DEVICE_STATUS_DEVICE_ERROR);
1144 static DeviceStatusFlags
1145 s3_device_read_label(Device *pself) {
1146 S3Device *self = S3_DEVICE(pself);
1148 CurlBuffer buf = {NULL, 0, 0, S3_DEVICE_MAX_BLOCK_SIZE};
1149 dumpfile_t *amanda_header;
1151 /* note that this may be called from s3_device_start, when
1152 * self->access_mode is not ACCESS_NULL */
1154 amfree(pself->volume_label);
1155 amfree(pself->volume_time);
1156 dumpfile_free(pself->volume_header);
1157 pself->volume_header = NULL;
1159 if (device_in_error(self)) return pself->status;
1161 if (!setup_handle(self)) {
1162 /* setup_handle already set our error message */
1163 return pself->status;
1166 key = special_file_to_key(self, "tapestart", -1);
1167 if (!s3_read(self->s3, self->bucket, key, S3_BUFFER_WRITE_FUNCS, &buf, NULL, NULL)) {
1168 guint response_code;
1169 s3_error_code_t s3_error_code;
1170 s3_error(self->s3, NULL, &response_code, &s3_error_code, NULL, NULL, NULL);
1172 /* if it's an expected error (not found), just return FALSE */
1173 if (response_code == 404 &&
1174 (s3_error_code == S3_ERROR_NoSuchKey || s3_error_code == S3_ERROR_NoSuchBucket)) {
1175 g_debug(_("Amanda header not found while reading tapestart header (this is expected for empty tapes)"));
1176 device_set_error(pself,
1177 stralloc(_("Amanda header not found -- unlabeled volume?")),
1178 DEVICE_STATUS_DEVICE_ERROR
1179 | DEVICE_STATUS_VOLUME_ERROR
1180 | DEVICE_STATUS_VOLUME_UNLABELED);
1181 return pself->status;
1184 /* otherwise, log it and return */
1185 device_set_error(pself,
1186 vstrallocf(_("While trying to read tapestart header: %s"), s3_strerror(self->s3)),
1187 DEVICE_STATUS_DEVICE_ERROR | DEVICE_STATUS_VOLUME_ERROR);
1188 return pself->status;
1191 /* handle an empty file gracefully */
1192 if (buf.buffer_len == 0) {
1193 device_set_error(pself, stralloc(_("Empty header file")), DEVICE_STATUS_VOLUME_ERROR);
1194 return pself->status;
1197 g_assert(buf.buffer != NULL);
1198 amanda_header = g_new(dumpfile_t, 1);
1199 parse_file_header(buf.buffer, amanda_header, buf.buffer_pos);
1200 pself->volume_header = amanda_header;
1203 if (amanda_header->type != F_TAPESTART) {
1204 device_set_error(pself, stralloc(_("Invalid amanda header")), DEVICE_STATUS_VOLUME_ERROR);
1205 return pself->status;
1208 pself->volume_label = g_strdup(amanda_header->name);
1209 pself->volume_time = g_strdup(amanda_header->datestamp);
1210 /* pself->volume_header is already set */
1212 device_set_error(pself, NULL, DEVICE_STATUS_SUCCESS);
1214 return pself->status;
1218 s3_device_start (Device * pself, DeviceAccessMode mode,
1219 char * label, char * timestamp) {
1222 self = S3_DEVICE(pself);
1224 if (device_in_error(self)) return FALSE;
1226 if (!setup_handle(self)) {
1227 /* setup_handle already set our error message */
1231 pself->access_mode = mode;
1232 pself->in_file = FALSE;
1234 /* try creating the bucket, in case it doesn't exist */
1235 if (mode != ACCESS_READ && !s3_make_bucket(self->s3, self->bucket)) {
1236 guint response_code;
1237 s3_error_code_t s3_error_code;
1238 s3_error(self->s3, NULL, &response_code, &s3_error_code, NULL, NULL, NULL);
1240 /* if it isn't an expected error (bucket already exists),
1242 if (response_code != 409 ||
1243 s3_error_code != S3_ERROR_BucketAlreadyExists) {
1244 device_set_error(pself,
1245 vstrallocf(_("While creating new S3 bucket: %s"), s3_strerror(self->s3)),
1246 DEVICE_STATUS_DEVICE_ERROR);
1251 /* take care of any dirty work for this mode */
1254 if (pself->volume_label == NULL && s3_device_read_label(pself) != DEVICE_STATUS_SUCCESS) {
1255 /* s3_device_read_label already set our error message */
1261 delete_all_files(self);
1263 /* write a new amanda header */
1264 if (!write_amanda_header(self, label, timestamp)) {
1268 pself->volume_label = newstralloc(pself->volume_label, label);
1269 pself->volume_time = newstralloc(pself->volume_time, timestamp);
1271 /* unset the VOLUME_UNLABELED flag, if it was set */
1272 device_set_error(pself, NULL, DEVICE_STATUS_SUCCESS);
1276 if (pself->volume_label == NULL && s3_device_read_label(pself) != DEVICE_STATUS_SUCCESS) {
1277 /* s3_device_read_label already set our error message */
1280 return seek_to_end(self);
1284 g_assert_not_reached();
1291 s3_device_finish (Device * pself) {
1292 if (device_in_error(pself)) return FALSE;
1294 /* we're not in a file anymore */
1295 pself->access_mode = ACCESS_NULL;
1300 /* functions for writing */
1304 s3_device_start_file (Device *pself, dumpfile_t *jobInfo) {
1305 S3Device *self = S3_DEVICE(pself);
1306 CurlBuffer amanda_header = {NULL, 0, 0, 0};
1311 if (device_in_error(self)) return FALSE;
1313 pself->is_eom = FALSE;
1315 /* Set the blocksize to zero, since there's no header to skip (it's stored
1316 * in a distinct file, rather than block zero) */
1317 jobInfo->blocksize = 0;
1319 /* Build the amanda header. */
1320 header_size = 0; /* no minimum size */
1321 amanda_header.buffer = device_build_amanda_header(pself, jobInfo,
1323 if (amanda_header.buffer == NULL) {
1324 device_set_error(pself,
1325 stralloc(_("Amanda file header won't fit in a single block!")),
1326 DEVICE_STATUS_DEVICE_ERROR);
1329 amanda_header.buffer_len = header_size;
1331 /* set the file and block numbers correctly */
1332 pself->file = (pself->file > 0)? pself->file+1 : 1;
1334 pself->in_file = TRUE;
1336 /* write it out as a special block (not the 0th) */
1337 key = special_file_to_key(self, "filestart", pself->file);
1338 result = s3_upload(self->s3, self->bucket, key, S3_BUFFER_READ_FUNCS,
1339 &amanda_header, NULL, NULL);
1340 g_free(amanda_header.buffer);
1343 device_set_error(pself,
1344 vstrallocf(_("While writing filestart header: %s"), s3_strerror(self->s3)),
1345 DEVICE_STATUS_DEVICE_ERROR | DEVICE_STATUS_VOLUME_ERROR);
1353 s3_device_write_block (Device * pself, guint size, gpointer data) {
1356 S3Device * self = S3_DEVICE(pself);
1357 CurlBuffer to_write = {data, size, 0, 0};
1359 g_assert (self != NULL);
1360 g_assert (data != NULL);
1361 if (device_in_error(self)) return FALSE;
1363 filename = file_and_block_to_key(self, pself->file, pself->block);
1365 result = s3_upload(self->s3, self->bucket, filename, S3_BUFFER_READ_FUNCS,
1366 &to_write, NULL, NULL);
1369 device_set_error(pself,
1370 vstrallocf(_("While writing data block to S3: %s"), s3_strerror(self->s3)),
1371 DEVICE_STATUS_DEVICE_ERROR | DEVICE_STATUS_VOLUME_ERROR);
1381 s3_device_finish_file (Device * pself) {
1382 if (device_in_error(pself)) return FALSE;
1384 /* we're not in a file anymore */
1385 pself->in_file = FALSE;
1391 s3_device_recycle_file(Device *pself, guint file) {
1392 S3Device *self = S3_DEVICE(pself);
1393 if (device_in_error(self)) return FALSE;
1395 return delete_file(self, file);
1396 /* delete_file already set our error message if necessary */
1400 s3_device_erase(Device *pself) {
1401 S3Device *self = S3_DEVICE(pself);
1403 const char *errmsg = NULL;
1404 guint response_code;
1405 s3_error_code_t s3_error_code;
1407 if (!setup_handle(self)) {
1408 /* error set by setup_handle */
1412 key = special_file_to_key(self, "tapestart", -1);
1413 if (!s3_delete(self->s3, self->bucket, key)) {
1414 s3_error(self->s3, &errmsg, NULL, NULL, NULL, NULL, NULL);
1415 device_set_error(pself,
1417 DEVICE_STATUS_DEVICE_ERROR);
1422 if (!delete_all_files(self))
1425 if (!s3_delete_bucket(self->s3, self->bucket)) {
1426 s3_error(self->s3, NULL, &response_code, &s3_error_code, NULL, NULL, NULL);
1429 * ignore the error if the bucket isn't empty (there may be data from elsewhere)
1430 * or the bucket not existing (already deleted perhaps?)
1433 (response_code == 409 && s3_error_code == S3_ERROR_BucketNotEmpty) ||
1434 (response_code == 404 && s3_error_code == S3_ERROR_NoSuchBucket))) {
1436 device_set_error(pself,
1438 DEVICE_STATUS_DEVICE_ERROR);
1445 /* functions for reading */
1448 s3_device_seek_file(Device *pself, guint file) {
1449 S3Device *self = S3_DEVICE(pself);
1452 CurlBuffer buf = {NULL, 0, 0, S3_DEVICE_MAX_BLOCK_SIZE};
1453 dumpfile_t *amanda_header;
1454 const char *errmsg = NULL;
1456 if (device_in_error(self)) return NULL;
1459 pself->is_eof = FALSE;
1460 pself->in_file = FALSE;
1464 key = special_file_to_key(self, "filestart", pself->file);
1465 result = s3_read(self->s3, self->bucket, key, S3_BUFFER_WRITE_FUNCS,
1470 guint response_code;
1471 s3_error_code_t s3_error_code;
1472 s3_error(self->s3, &errmsg, &response_code, &s3_error_code, NULL, NULL, NULL);
1474 /* if it's an expected error (not found), check what to do. */
1475 if (response_code == 404 && s3_error_code == S3_ERROR_NoSuchKey) {
1477 next_file = find_next_file(self, pself->file);
1478 if (next_file > 0) {
1479 /* Note short-circut of dispatcher. */
1480 return s3_device_seek_file(pself, next_file);
1481 } else if (next_file == 0) {
1482 /* No next file. Check if we are one past the end. */
1483 key = special_file_to_key(self, "filestart", pself->file - 1);
1484 result = s3_read(self->s3, self->bucket, key,
1485 S3_BUFFER_WRITE_FUNCS, &buf, NULL, NULL);
1488 /* pself->file, etc. are already correct */
1489 return make_tapeend_header();
1491 device_set_error(pself,
1492 stralloc(_("Attempt to read past tape-end file")),
1493 DEVICE_STATUS_SUCCESS);
1498 /* An unexpected error occured finding out if we are the last file. */
1499 device_set_error(pself,
1501 DEVICE_STATUS_DEVICE_ERROR);
1506 /* and make a dumpfile_t out of it */
1507 g_assert(buf.buffer != NULL);
1508 amanda_header = g_new(dumpfile_t, 1);
1509 fh_init(amanda_header);
1510 parse_file_header(buf.buffer, amanda_header, buf.buffer_pos);
1513 switch (amanda_header->type) {
1515 case F_CONT_DUMPFILE:
1516 case F_SPLIT_DUMPFILE:
1520 device_set_error(pself,
1521 stralloc(_("Invalid amanda header while reading file header")),
1522 DEVICE_STATUS_VOLUME_ERROR);
1523 g_free(amanda_header);
1527 pself->in_file = TRUE;
1528 return amanda_header;
1532 s3_device_seek_block(Device *pself, guint64 block) {
1533 if (device_in_error(pself)) return FALSE;
1535 pself->block = block;
1539 typedef struct s3_read_block_data {
1545 } s3_read_block_data;
1547 /* wrapper around s3_buffer_write_func to write as much data as possible to
1548 * the user's buffer, and switch to a dynamically allocated buffer if that
1549 * isn't large enough */
1551 s3_read_block_write_func(void *ptr, size_t size, size_t nmemb, void *stream)
1553 s3_read_block_data *dat = stream;
1554 guint new_bytes, bytes_needed;
1556 /* if data is NULL, call through to s3_buffer_write_func */
1558 return s3_buffer_write_func(ptr, size, nmemb, (void *)(&dat->curl));
1561 new_bytes = (guint) size * nmemb;
1562 bytes_needed = dat->size_written + new_bytes;
1564 if (bytes_needed > (guint)dat->size_req) {
1565 /* this read will overflow the user's buffer, so malloc ourselves
1566 * a new buffer and keep reading */
1567 dat->curl.buffer = g_malloc(bytes_needed);
1568 dat->curl.buffer_len = bytes_needed;
1569 dat->curl.buffer_pos = dat->size_written;
1570 memcpy(dat->curl.buffer, dat->data, dat->size_written);
1571 dat->data = NULL; /* signal that the user's buffer is too small */
1572 return s3_buffer_write_func(ptr, size, nmemb, (void *)(&dat->curl));
1575 /* copy it into the dat->data buffer, and increment the size */
1576 memcpy(dat->data + dat->size_written, ptr, new_bytes);
1577 dat->size_written += new_bytes;
1583 s3_device_read_block (Device * pself, gpointer data, int *size_req) {
1584 S3Device * self = S3_DEVICE(pself);
1586 s3_read_block_data dat = {NULL, 0, 0, { NULL, 0, 0, S3_DEVICE_MAX_BLOCK_SIZE} };
1589 g_assert (self != NULL);
1590 if (device_in_error(self)) return -1;
1593 key = file_and_block_to_key(self, pself->file, pself->block);
1594 g_assert(key != NULL);
1595 if (self->cached_key && (0 == strcmp(key, self->cached_key))) {
1596 if (*size_req >= self->cached_size) {
1597 /* use the cached copy and clear the cache */
1598 memcpy(data, self->cached_buf, self->cached_size);
1599 *size_req = self->cached_size;
1602 g_free(self->cached_key);
1603 self->cached_key = NULL;
1604 g_free(self->cached_buf);
1605 self->cached_buf = NULL;
1610 *size_req = self->cached_size;
1616 /* clear the cache, as it's useless to us */
1617 if (self->cached_key) {
1618 g_free(self->cached_key);
1619 self->cached_key = NULL;
1621 g_free(self->cached_buf);
1622 self->cached_buf = NULL;
1625 /* set up dat for the write_func callback */
1626 if (!data || *size_req <= 0) {
1631 dat.size_req = *size_req;
1634 result = s3_read(self->s3, self->bucket, key, s3_read_block_write_func,
1635 s3_buffer_reset_func, &dat, NULL, NULL);
1637 guint response_code;
1638 s3_error_code_t s3_error_code;
1639 s3_error(self->s3, NULL, &response_code, &s3_error_code, NULL, NULL, NULL);
1644 /* if it's an expected error (not found), just return -1 */
1645 if (response_code == 404 && s3_error_code == S3_ERROR_NoSuchKey) {
1646 pself->is_eof = TRUE;
1647 pself->in_file = FALSE;
1648 device_set_error(pself,
1650 DEVICE_STATUS_SUCCESS);
1654 /* otherwise, log it and return FALSE */
1655 device_set_error(pself,
1656 vstrallocf(_("While reading data block from S3: %s"), s3_strerror(self->s3)),
1657 DEVICE_STATUS_VOLUME_ERROR);
1661 if (dat.data == NULL) {
1662 /* data was larger than the available space, so cache it and return
1663 * the actual size */
1664 self->cached_buf = dat.curl.buffer;
1665 self->cached_size = dat.curl.buffer_pos;
1666 self->cached_key = key;
1669 *size_req = dat.curl.buffer_pos;
1673 /* ok, the read went directly to the user's buffer, so we need only
1674 * set and return the size */
1677 *size_req = dat.size_written;
1678 return dat.size_written;