Debian Codegraph @

device-src/s3-device.c

raw
1 /*
2 * Copyright (c) 2008-2012 Zmanda, Inc. All Rights Reserved.
3 * Copyright (c) 2013-2016 Carbonite, Inc. All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * Contact information: Carbonite Inc., 756 N Pastoria Ave
20 * Sunnyvale, CA 94085, or: http://www.zmanda.com
21 */
22
23 /* An S3 device uses Amazon's S3 service (http://www.amazon.com/s3) to store
24 * data. It stores data in keys named with a user-specified prefix, inside a
25 * user-specified bucket. Data is stored in the form of numbered (large)
26 * blocks.
27 */
28
29 #include "amanda.h"
30 #include <string.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <unistd.h>
34 #include <dirent.h>
35 #include <regex.h>
36 #include <time.h>
37 #include "amutil.h"
38 #include "conffile.h"
39 #include "device.h"
40 #include "s3-device.h"
41 #include "s3.h"
42 #include <curl/curl.h>
43 #ifdef HAVE_OPENSSL_HMAC_H
44 # include <openssl/hmac.h>
45 #else
46 # ifdef HAVE_CRYPTO_HMAC_H
47 # include <crypto/hmac.h>
48 # else
49 # ifdef HAVE_HMAC_H
50 # include <hmac.h>
51 # endif
52 # endif
53 #endif
54
55
56 /*
57 * Constants and static data
58 */
59
60 #define S3_DEVICE_NAME "s3"
61
62 /* Maximum key length as specified in the S3 documentation
63 * (*excluding* null terminator) */
64 #define S3_MAX_KEY_LENGTH 1024
65
66 /* Note: for compatability, min can only be decreased and max increased */
67 #define S3_DEVICE_MIN_BLOCK_SIZE 1024
68 #define S3_DEVICE_MAX_BLOCK_SIZE (3*1024*1024*1024ULL)
69 #define S3_DEVICE_DEFAULT_BLOCK_SIZE (10*1024*1024)
70 #define EOM_EARLY_WARNING_ZONE_BLOCKS 4
71
72 /* This goes in lieu of file number for metadata. */
73 #define SPECIAL_INFIX "special-"
74
75 /* pointer to the class of our parent */
76 static DeviceClass *parent_class = NULL;
77
78 /*
79 * device-specific properties
80 */
81
82 /* Authentication information for Amazon S3. Both of these are strings. */
83 DevicePropertyBase device_property_s3_access_key;
84 DevicePropertyBase device_property_s3_secret_key;
85 static DevicePropertyBase device_property_s3_session_token;
86 //#define PROPERTY_S3_SECRET_KEY (device_property_s3_secret_key.ID)
87 //#define PROPERTY_S3_ACCESS_KEY (device_property_s3_access_key.ID)
88 #define PROPERTY_S3_SESSION_TOKEN (device_property_s3_session_token.ID)
89
90 /* Authentication information for Openstack Swift. Both of these are strings. */
91 static DevicePropertyBase device_property_swift_account_id;
92 static DevicePropertyBase device_property_swift_access_key;
93 #define PROPERTY_SWIFT_ACCOUNT_ID (device_property_swift_account_id.ID)
94 #define PROPERTY_SWIFT_ACCESS_KEY (device_property_swift_access_key.ID)
95
96 /* Authentication information for Openstack Swift. Both of these are strings. */
97 static DevicePropertyBase device_property_username;
98 static DevicePropertyBase device_property_password;
99 static DevicePropertyBase device_property_tenant_id;
100 static DevicePropertyBase device_property_tenant_name;
101 static DevicePropertyBase device_property_project_name;
102 static DevicePropertyBase device_property_domain_name;
103 #define PROPERTY_USERNAME (device_property_username.ID)
104 #define PROPERTY_PASSWORD (device_property_password.ID)
105 #define PROPERTY_TENANT_ID (device_property_tenant_id.ID)
106 #define PROPERTY_TENANT_NAME (device_property_tenant_name.ID)
107 #define PROPERTY_PROJECT_NAME (device_property_project_name.ID)
108 #define PROPERTY_DOMAIN_NAME (device_property_domain_name.ID)
109
110 /* Host and path */
111 static DevicePropertyBase device_property_s3_host;
112 static DevicePropertyBase device_property_s3_service_path;
113 #define PROPERTY_S3_HOST (device_property_s3_host.ID)
114 #define PROPERTY_S3_SERVICE_PATH (device_property_s3_service_path.ID)
115
116 /* Same, but for S3 with DevPay. */
117 static DevicePropertyBase device_property_s3_user_token;
118 #define PROPERTY_S3_USER_TOKEN (device_property_s3_user_token.ID)
119
120 /* Location constraint for new buckets created on Amazon S3. */
121 static DevicePropertyBase device_property_s3_bucket_location;
122 #define PROPERTY_S3_BUCKET_LOCATION (device_property_s3_bucket_location.ID)
123
124 /* Storage class */
125 static DevicePropertyBase device_property_s3_storage_class;
126 #define PROPERTY_S3_STORAGE_CLASS (device_property_s3_storage_class.ID)
127
128 /* Server side encryption */
129 static DevicePropertyBase device_property_s3_server_side_encryption;
130 #define PROPERTY_S3_SERVER_SIDE_ENCRYPTION (device_property_s3_server_side_encryption.ID)
131
132 /* Which strotage api to use. */
133 static DevicePropertyBase device_property_storage_api;
134 #define PROPERTY_STORAGE_API (device_property_storage_api.ID)
135
136 /* Whether to use openstack protocol. */
137 /* DEPRECATED */
138 static DevicePropertyBase device_property_openstack_swift_api;
139 #define PROPERTY_OPENSTACK_SWIFT_API (device_property_openstack_swift_api.ID)
140
141 /* Whether to use chunked transfer-encoding */
142 static DevicePropertyBase device_property_chunked;
143 #define PROPERTY_CHUNKED (device_property_chunked.ID)
144
145 /* Whether to use SSL with Amazon S3. */
146 static DevicePropertyBase device_property_s3_ssl;
147 #define PROPERTY_S3_SSL (device_property_s3_ssl.ID)
148
149 /* Whether to use subdomain */
150 static DevicePropertyBase device_property_s3_subdomain;
151 #define PROPERTY_S3_SUBDOMAIN (device_property_s3_subdomain.ID)
152
153 /* Whether to use s3 multi-part upload */
154 static DevicePropertyBase device_property_s3_multi_part_upload;
155 #define PROPERTY_S3_MULTI_PART_UPLOAD (device_property_s3_multi_part_upload.ID)
156
157 /* If the s3 server have the multi-delete functionality */
158 static DevicePropertyBase device_property_s3_multi_delete;
159 #define PROPERTY_S3_MULTI_DELETE (device_property_s3_multi_delete.ID)
160
161 /* The client_id for OAUTH2 */
162 static DevicePropertyBase device_property_client_id;
163 #define PROPERTY_CLIENT_ID (device_property_client_id.ID)
164
165 /* The client_secret for OAUTH2 */
166 static DevicePropertyBase device_property_client_secret;
167 #define PROPERTY_CLIENT_SECRET (device_property_client_secret.ID)
168
169 /* The refresh token for OAUTH2 */
170 static DevicePropertyBase device_property_refresh_token;
171 #define PROPERTY_REFRESH_TOKEN (device_property_refresh_token.ID)
172
173 /* The PROJECT ID */
174 static DevicePropertyBase device_property_project_id;
175 #define PROPERTY_PROJECT_ID (device_property_project_id.ID)
176
177 /* The PROJECT ID */
178 static DevicePropertyBase device_property_create_bucket;
179 #define PROPERTY_CREATE_BUCKET (device_property_create_bucket.ID)
180
181 /* glacier */
182 static DevicePropertyBase device_property_read_from_glacier;
183 #define PROPERTY_READ_FROM_GLACIER (device_property_read_from_glacier.ID)
184 static DevicePropertyBase device_property_transition_to_glacier;
185 #define PROPERTY_TRANSITION_TO_GLACIER (device_property_transition_to_glacier.ID)
186
187 static DevicePropertyBase device_property_timeout;
188 #define PROPERTY_TIMEOUT (device_property_timeout.ID)
189
190 /* CAStor replication values for objects and buckets */
191 static DevicePropertyBase device_property_s3_reps;
192 #define PROPERTY_S3_REPS (device_property_s3_reps.ID)
193 #define S3_DEVICE_REPS_DEFAULT "2"
194 static DevicePropertyBase device_property_s3_reps_bucket;
195 #define PROPERTY_S3_REPS_BUCKET (device_property_s3_reps_bucket.ID)
196 #define S3_DEVICE_REPS_BUCKET_DEFAULT "4"
197
198 /*
199 * prototypes
200 */
201
202 void s3_device_register(void);
203
204 /*
205 * utility functions */
206
207 /* Given file and block numbers, return an S3 key.
208 *
209 * @param self: the S3Device object
210 * @param file: the file number
211 * @returns: a newly allocated string containing the prefix for the file.
212 */
213 static char *
214 file_to_prefix(S3Device *self,
215 int file);
216
217 /* Given file and block numbers, return an S3 key.
218 *
219 * @param self: the S3Device object
220 * @param file: the file number
221 * @param block: the block within that file
222 * @returns: a newly allocated string containing an S3 key.
223 */
224 static char *
225 file_and_block_to_key(S3Device *self,
226 int file,
227 guint64 block);
228
229 /* Given file, return an S3 key.
230 *
231 * @param self: the S3Device object
232 * @param file: the file number
233 * @returns: a newly allocated string containing an S3 key.
234 */
235 static char *
236 file_to_multi_part_key(S3Device *self,
237 int file);
238
239 /* Given the name of a special file (such as 'tapestart'), generate
240 * the S3 key to use for that file.
241 *
242 * @param self: the S3Device object
243 * @param special_name: name of the special file
244 * @param file: a file number to include; omitted if -1
245 * @returns: a newly alocated string containing an S3 key.
246 */
247 static char *
248 special_file_to_key(S3Device *self,
249 char *special_name,
250 int file);
251 /* Write an amanda header file to S3.
252 *
253 * @param self: the S3Device object
254 * @param label: the volume label
255 * @param timestamp: the volume timestamp
256 */
257 static gboolean
258 write_amanda_header(S3Device *self,
259 char *label,
260 char * timestamp);
261
262 /* "Fast forward" this device to the end by looking up the largest file number
263 * present and setting the current file number one greater.
264 *
265 * @param self: the S3Device object
266 */
267 static gboolean
268 seek_to_end(S3Device *self);
269
270 /* Find the number of the last file that contains any data (even just a header).
271 *
272 * @param self: the S3Device object
273 * @returns: the last file, or -1 in event of an error
274 */
275 static int
276 find_last_file(S3Device *self);
277
278 /* Delete all blocks in the given file, including the filestart block
279 *
280 * @param self: the S3Device object
281 * @param file: the file to delete
282 */
283 static gboolean
284 delete_file(S3Device *self,
285 int file);
286
287
288 /* Delete all files in the given device
289 *
290 * @param self: the S3Device object
291 */
292 static gboolean
293 delete_all_files(S3Device *self);
294
295 /* Set up self->s3t as best as possible.
296 *
297 * The return value is TRUE iff self->s3t is useable.
298 *
299 * @param self: the S3Device object
300 * @returns: TRUE if the handle is set up
301 */
302 static gboolean
303 setup_handle(S3Device * self);
304
305 static void
306 s3_wait_thread_delete(S3Device *self);
307
308 static gboolean
309 catalog_open(S3Device *self);
310
311 static gboolean
312 catalog_reset(S3Device *self,
313 char *header,
314 char *label);
315
316 static gboolean
317 catalog_remove(S3Device *self);
318
319 static gboolean
320 catalog_close(S3Device *self);
321
322 /*
323 * class mechanics */
324
325 static void
326 s3_device_init(S3Device * o);
327
328 static void
329 s3_device_class_init(S3DeviceClass * c);
330
331 static void
332 s3_device_base_init( S3DeviceClass *c);
333
334 static void
335 s3_device_finalize(GObject * o);
336
337 static Device*
338 s3_device_factory(char * device_name, char * device_type, char * device_node);
339
340 /*
341 * Property{Get,Set}Fns */
342
343 static gboolean s3_device_set_access_key_fn(Device *self,
344 DevicePropertyBase *base, GValue *val,
345 PropertySurety surety, PropertySource source);
346
347 static gboolean s3_device_set_secret_key_fn(Device *self,
348 DevicePropertyBase *base, GValue *val,
349 PropertySurety surety, PropertySource source);
350
351 static gboolean s3_device_set_session_token_fn(Device *self,
352 DevicePropertyBase *base, GValue *val,
353 PropertySurety surety, PropertySource source);
354
355 static gboolean s3_device_set_swift_account_id_fn(Device *self,
356 DevicePropertyBase *base, GValue *val,
357 PropertySurety surety, PropertySource source);
358
359 static gboolean s3_device_set_swift_access_key_fn(Device *self,
360 DevicePropertyBase *base, GValue *val,
361 PropertySurety surety, PropertySource source);
362
363 static gboolean s3_device_set_username(Device *self,
364 DevicePropertyBase *base, GValue *val,
365 PropertySurety surety, PropertySource source);
366
367 static gboolean s3_device_set_password(Device *self,
368 DevicePropertyBase *base, GValue *val,
369 PropertySurety surety, PropertySource source);
370
371 static gboolean s3_device_set_tenant_id(Device *self,
372 DevicePropertyBase *base, GValue *val,
373 PropertySurety surety, PropertySource source);
374
375 static gboolean s3_device_set_tenant_name(Device *self,
376 DevicePropertyBase *base, GValue *val,
377 PropertySurety surety, PropertySource source);
378
379 static gboolean s3_device_set_project_name(Device *self,
380 DevicePropertyBase *base, GValue *val,
381 PropertySurety surety, PropertySource source);
382
383 static gboolean s3_device_set_domain_name(Device *self,
384 DevicePropertyBase *base, GValue *val,
385 PropertySurety surety, PropertySource source);
386
387 static gboolean s3_device_set_user_token_fn(Device *self,
388 DevicePropertyBase *base, GValue *val,
389 PropertySurety surety, PropertySource source);
390
391 static gboolean s3_device_set_bucket_location_fn(Device *self,
392 DevicePropertyBase *base, GValue *val,
393 PropertySurety surety, PropertySource source);
394
395 static gboolean s3_device_set_storage_class_fn(Device *self,
396 DevicePropertyBase *base, GValue *val,
397 PropertySurety surety, PropertySource source);
398
399 static gboolean s3_device_set_server_side_encryption_fn(Device *self,
400 DevicePropertyBase *base, GValue *val,
401 PropertySurety surety, PropertySource source);
402
403 static gboolean s3_device_set_proxy_fn(Device *self,
404 DevicePropertyBase *base, GValue *val,
405 PropertySurety surety, PropertySource source);
406
407 static gboolean s3_device_set_ca_info_fn(Device *self,
408 DevicePropertyBase *base, GValue *val,
409 PropertySurety surety, PropertySource source);
410
411 static gboolean s3_device_set_verbose_fn(Device *self,
412 DevicePropertyBase *base, GValue *val,
413 PropertySurety surety, PropertySource source);
414
415 static gboolean s3_device_set_create_bucket_fn(Device *self,
416 DevicePropertyBase *base, GValue *val,
417 PropertySurety surety, PropertySource source);
418
419 static gboolean s3_device_set_read_from_glacier_fn(Device *self,
420 DevicePropertyBase *base, GValue *val,
421 PropertySurety surety, PropertySource source);
422
423 static gboolean s3_device_set_transition_to_glacier_fn(Device *self,
424 DevicePropertyBase *base, GValue *val,
425 PropertySurety surety, PropertySource source);
426
427 static gboolean s3_device_set_storage_api(Device *self,
428 DevicePropertyBase *base, GValue *val,
429 PropertySurety surety, PropertySource source);
430
431 static gboolean s3_device_set_openstack_swift_api_fn(Device *self,
432 DevicePropertyBase *base, GValue *val,
433 PropertySurety surety, PropertySource source);
434
435 static gboolean s3_device_set_s3_multi_delete_fn(Device *self,
436 DevicePropertyBase *base, GValue *val,
437 PropertySurety surety, PropertySource source);
438
439 static gboolean s3_device_set_chunked_fn(Device *self,
440 DevicePropertyBase *base, GValue *val,
441 PropertySurety surety, PropertySource source);
442
443 static gboolean s3_device_set_ssl_fn(Device *self,
444 DevicePropertyBase *base, GValue *val,
445 PropertySurety surety, PropertySource source);
446
447 static gboolean s3_device_set_reuse_connection_fn(Device *self,
448 DevicePropertyBase *base, GValue *val,
449 PropertySurety surety, PropertySource source);
450
451 static gboolean s3_device_set_timeout_fn(Device *self,
452 DevicePropertyBase *base, GValue *val,
453 PropertySurety surety, PropertySource source);
454
455 static gboolean s3_device_set_max_send_speed_fn(Device *self,
456 DevicePropertyBase *base, GValue *val,
457 PropertySurety surety, PropertySource source);
458
459 static gboolean s3_device_set_max_recv_speed_fn(Device *self,
460 DevicePropertyBase *base, GValue *val,
461 PropertySurety surety, PropertySource source);
462
463 static gboolean s3_device_set_nb_threads_backup(Device *self,
464 DevicePropertyBase *base, GValue *val,
465 PropertySurety surety, PropertySource source);
466
467 static gboolean s3_device_set_nb_threads_recovery(Device *self,
468 DevicePropertyBase *base, GValue *val,
469 PropertySurety surety, PropertySource source);
470
471 static gboolean s3_device_set_s3_multi_part_upload(Device *self,
472 DevicePropertyBase *base, GValue *val,
473 PropertySurety surety, PropertySource source);
474
475 static gboolean s3_device_set_max_volume_usage_fn(Device *p_self,
476 DevicePropertyBase *base, GValue *val,
477 PropertySurety surety, PropertySource source);
478
479 static gboolean property_set_leom_fn(Device *p_self,
480 DevicePropertyBase *base, GValue *val,
481 PropertySurety surety, PropertySource source);
482
483 static gboolean s3_device_set_enforce_max_volume_usage_fn(Device *p_self,
484 DevicePropertyBase *base, GValue *val,
485 PropertySurety surety, PropertySource source);
486
487 static gboolean s3_device_set_use_subdomain_fn(Device *p_self,
488 DevicePropertyBase *base, GValue *val,
489 PropertySurety surety, PropertySource source);
490
491 static gboolean s3_device_set_host_fn(Device *p_self,
492 DevicePropertyBase *base, GValue *val,
493 PropertySurety surety, PropertySource source);
494
495 static gboolean s3_device_set_service_path_fn(Device *p_self,
496 DevicePropertyBase *base, GValue *val,
497 PropertySurety surety, PropertySource source);
498
499 static gboolean s3_device_set_client_id_fn(Device *p_self,
500 DevicePropertyBase *base, GValue *val,
501 PropertySurety surety, PropertySource source);
502
503 static gboolean s3_device_set_client_secret_fn(Device *p_self,
504 DevicePropertyBase *base, GValue *val,
505 PropertySurety surety, PropertySource source);
506
507 static gboolean s3_device_set_refresh_token_fn(Device *p_self,
508 DevicePropertyBase *base, GValue *val,
509 PropertySurety surety, PropertySource source);
510
511 static gboolean s3_device_set_project_id_fn(Device *p_self,
512 DevicePropertyBase *base, GValue *val,
513 PropertySurety surety, PropertySource source);
514
515 static gboolean s3_device_set_reps_fn(Device *self,
516 DevicePropertyBase *base, GValue *val,
517 PropertySurety surety, PropertySource source);
518
519 static gboolean s3_device_set_reps_bucket_fn(Device *self,
520 DevicePropertyBase *base, GValue *val,
521 PropertySurety surety, PropertySource source);
522
523 static void s3_thread_read_block(gpointer thread_data,
524 gpointer data);
525 static void s3_thread_write_block(gpointer thread_data,
526 gpointer data);
527 static gboolean make_bucket(Device * pself);
528
529
530 /* Wait that all threads are done */
531 static void reset_thread(S3Device *self);
532
533 /*
534 * virtual functions */
535
536 static void
537 s3_device_open_device(Device *pself, char *device_name,
538 char * device_type, char * device_node);
539
540 static gboolean
541 s3_device_create(Device *pself);
542
543 static DeviceStatusFlags s3_device_read_label(Device * self);
544
545 static gboolean
546 s3_device_start(Device * self,
547 DeviceAccessMode mode,
548 char * label,
549 char * timestamp);
550
551 static gboolean
552 s3_device_finish(Device * self);
553
554 static guint64
555 s3_device_get_bytes_read(Device * self);
556
557 static guint64
558 s3_device_get_bytes_written(Device * self);
559
560 static gboolean
561 s3_device_start_file(Device * self,
562 dumpfile_t * jobInfo);
563
564 static DeviceWriteResult
565 s3_device_write_block(Device * self,
566 guint size,
567 gpointer data);
568
569 static gboolean
570 s3_device_finish_file(Device * self);
571
572 static gboolean
573 s3_device_init_seek_file(Device *pself,
574 guint file);
575
576 static dumpfile_t*
577 s3_device_seek_file(Device *pself,
578 guint file);
579
580 static gboolean
581 s3_device_seek_block(Device *pself,
582 guint64 block);
583
584 static int
585 s3_device_read_block(Device *pself,
586 gpointer data,
587 int *size_req,
588 int max_block);
589
590 static gboolean
591 s3_device_recycle_file(Device *pself,
592 guint file);
593
594 static gboolean
595 s3_device_erase(Device *pself);
596
597 static gboolean
598 s3_device_set_reuse(Device *pself);
599
600 static gboolean
601 s3_device_set_no_reuse(Device *pself,
602 char *label,
603 char *datestamp);
604
605 static gboolean
606 check_at_leom(S3Device *self,
607 guint64 size);
608
609 static gboolean
610 check_at_peom(S3Device *self,
611 guint64 size);
612
613 /*
614 * Private functions
615 */
616
617 static char *
618 file_to_prefix(S3Device *self,
619 int file)
620 {
621 char *prefix = g_strdup_printf("%sf%08x", self->prefix, file);
622 g_assert(strlen(prefix) <= S3_MAX_KEY_LENGTH);
623 return prefix;
624 }
625
626 static char *
627 file_and_block_to_key(S3Device *self,
628 int file,
629 guint64 block)
630 {
631 char *s3_key = g_strdup_printf("%sf%08x-b%016llx.data",
632 self->prefix, file, (long long unsigned int)block);
633 g_assert(strlen(s3_key) <= S3_MAX_KEY_LENGTH);
634 return s3_key;
635 }
636
637 static char *
638 file_to_multi_part_key(S3Device *self,
639 int file)
640 {
641 char *s3_key = g_strdup_printf("%sf%08x-mp.data", self->prefix, file);
642 g_assert(strlen(s3_key) <= S3_MAX_KEY_LENGTH);
643 return s3_key;
644 }
645
646 static char *
647 special_file_to_key(S3Device *self,
648 char *special_name,
649 int file)
650 {
651 if (file == -1)
652 return g_strdup_printf("%s" SPECIAL_INFIX "%s", self->prefix, special_name);
653 else
654 return g_strdup_printf("%sf%08x-%s", self->prefix, file, special_name);
655 }
656
657 static gboolean
658 write_amanda_header(S3Device *self,
659 char *label,
660 char * timestamp)
661 {
662 CurlBuffer amanda_header = {NULL, 0, 0, 0, TRUE, NULL, NULL};
663 char * key = NULL;
664 gboolean result;
665 dumpfile_t * dumpinfo = NULL;
666 Device *d_self = DEVICE(self);
667 size_t header_size;
668
669 /* build the header */
670 header_size = 0; /* no minimum size */
671 dumpinfo = make_tapestart_header(DEVICE(self), label, timestamp);
672 amanda_header.buffer = device_build_amanda_header(DEVICE(self), dumpinfo,
673 &header_size);
674 if (amanda_header.buffer == NULL) {
675 device_set_error(d_self,
676 g_strdup(_("Amanda tapestart header won't fit in a single block!")),
677 DEVICE_STATUS_DEVICE_ERROR);
678 dumpfile_free(dumpinfo);
679 g_free(amanda_header.buffer);
680 return FALSE;
681 }
682
683 if(check_at_leom(self, header_size))
684 d_self->is_eom = TRUE;
685
686 if(check_at_peom(self, header_size)) {
687 d_self->is_eom = TRUE;
688 device_set_error(d_self,
689 g_strdup(_("No space left on device")),
690 DEVICE_STATUS_DEVICE_ERROR);
691 dumpfile_free(dumpinfo);
692 g_free(amanda_header.buffer);
693 return FALSE;
694 }
695
696 /* write out the header and flush the uploads. */
697 catalog_reset(self, amanda_header.buffer, label);
698 key = special_file_to_key(self, "tapestart", -1);
699 g_assert(header_size < G_MAXUINT); /* for cast to guint */
700 amanda_header.buffer_len = (guint)header_size;
701 result = s3_upload(self->s3t[0].s3, self->bucket, key, FALSE,
702 S3_BUFFER_READ_FUNCS,
703 &amanda_header, NULL, NULL);
704 g_free(amanda_header.buffer);
705 g_free(key);
706
707 if (!result) {
708 device_set_error(d_self,
709 g_strdup_printf(_("While writing amanda header: %s"), s3_strerror(self->s3t[0].s3)),
710 DEVICE_STATUS_DEVICE_ERROR | DEVICE_STATUS_VOLUME_ERROR);
711 dumpfile_free(dumpinfo);
712 } else {
713 dumpfile_free(d_self->volume_header);
714 d_self->volume_header = dumpinfo;
715 self->volume_bytes += header_size;
716 }
717 d_self->header_block_size = header_size;
718 return result;
719 }
720
721 static gboolean
722 seek_to_end(S3Device *self) {
723 int last_file;
724
725 Device *pself = DEVICE(self);
726
727 last_file = find_last_file(self);
728 if (last_file < 0)
729 return FALSE;
730
731 pself->file = last_file;
732
733 return TRUE;
734 }
735
736 /* Convert an object name into a file number, assuming the given prefix
737 * length. Returns -1 if the object name is invalid, or 0 if the object name
738 * is a "special" key. */
739 static int key_to_file(guint prefix_len, const char * key) {
740 int file;
741 int i;
742
743 /* skip the prefix */
744 if (strlen(key) <= prefix_len)
745 return -1;
746
747 key += prefix_len;
748
749 if (g_str_has_prefix(key, SPECIAL_INFIX)) {
750 return 0;
751 }
752
753 /* check that key starts with 'f' */
754 if (key[0] != 'f')
755 return -1;
756 key++;
757
758 /* check that key is of the form "%08x-" */
759 for (i = 0; i < 8; i++) {
760 if (!(key[i] >= '0' && key[i] <= '9') &&
761 !(key[i] >= 'a' && key[i] <= 'f') &&
762 !(key[i] >= 'A' && key[i] <= 'F')) break;
763 }
764 if (key[i] != '-') return -1;
765 if (i < 8) return -1;
766
767 /* convert the file number */
768 errno = 0;
769 file = strtoul(key, NULL, 16);
770 if (errno != 0) {
771 g_warning(_("unparseable file number '%s'"), key);
772 return -1;
773 }
774
775 return file;
776 }
777
778 static int
779 abort_partial_upload(S3Device *self)
780 {
781 Device *d_self = DEVICE(self);
782 gboolean result;
783 GSList *objects;
784 s3_object *part = NULL;
785
786 if (!self->use_s3_multi_part_upload)
787 return TRUE;
788
789 result = s3_list_keys(self->s3t[0].s3, self->bucket, "uploads", self->prefix, NULL, &objects, NULL);
790
791 if (!result) {
792 device_set_error(d_self,
793 g_strdup_printf(_("While listing partial upload: %s"), s3_strerror(self->s3t[0].s3)),
794 DEVICE_STATUS_DEVICE_ERROR | DEVICE_STATUS_VOLUME_ERROR);
795 return FALSE;
796 }
797
798 for (; objects; ) {
799 part = (s3_object *)objects->data;
800 objects = g_slist_remove(objects, objects->data);
801 g_debug("partial upload: %s : %s", part->key, part->uploadId);
802 s3_abort_multi_part_upload(self->s3t[0].s3, self->bucket, part->key, part->uploadId);
803 free_s3_object(part);
804 }
805 return TRUE;
806 }
807
808 /* Find the number of the last file that contains any data (even just a header).
809 * Returns -1 in event of an error
810 */
811 static int
812 find_last_file(S3Device *self) {
813 gboolean result;
814 GSList *keys;
815 unsigned int prefix_len = strlen(self->prefix);
816 int last_file = 0;
817 Device *d_self = DEVICE(self);
818
819 /* list all keys matching C{PREFIX*-*}, stripping the C{-*} */
820 result = s3_list_keys(self->s3t[0].s3, self->bucket, NULL, self->prefix, "-", &keys, NULL);
821 if (!result) {
822 device_set_error(d_self,
823 g_strdup_printf(_("While listing S3 keys: %s"), s3_strerror(self->s3t[0].s3)),
824 DEVICE_STATUS_DEVICE_ERROR | DEVICE_STATUS_VOLUME_ERROR);
825 return -1;
826 }
827
828 for (; keys; keys = g_slist_remove(keys, keys->data)) {
829 s3_object *object = keys->data;
830 int file = key_to_file(prefix_len, object->prefix);
831
832 /* and if it's the last, keep it */
833 if (file > last_file)
834 last_file = file;
835 }
836
837 return last_file;
838 }
839
840 /* Find the number of the file following the requested one, if any.
841 * Returns 0 if there is no such file or -1 in event of an error
842 */
843 static int
844 find_next_file(S3Device *self, int last_file) {
845 gboolean result;
846 GSList *objects;
847 unsigned int prefix_len = strlen(self->prefix);
848 int next_file = 0;
849 Device *d_self = DEVICE(self);
850
851 /* list all keys matching C{PREFIX*-*}, stripping the C{-*} */
852 result = s3_list_keys(self->s3t[0].s3, self->bucket, NULL, self->prefix, "-",
853 &objects, NULL);
854 if (!result) {
855 device_set_error(d_self,
856 g_strdup_printf(_("While listing S3 keys: %s"), s3_strerror(self->s3t[0].s3)),
857 DEVICE_STATUS_DEVICE_ERROR | DEVICE_STATUS_VOLUME_ERROR);
858 return -1;
859 }
860
861 for (; objects; objects = g_slist_remove(objects, objects->data)) {
862 int file;
863 s3_object *object = objects->data;
864
865 file = key_to_file(prefix_len, object->prefix);
866
867 if (file < 0) {
868 /* Set this in case we don't find a next file; this is not a
869 * hard error, so if we can find a next file we'll return that
870 * instead. */
871 next_file = -1;
872 }
873
874 if (file < next_file && file > last_file) {
875 next_file = file;
876 }
877 }
878
879 return next_file;
880 }
881
882 static gboolean
883 delete_file(S3Device *self,
884 int file)
885 {
886 int thread = -1;
887
888 gboolean result;
889 GSList *objects;
890 guint64 total_size = 0;
891 Device *d_self = DEVICE(self);
892 char *my_prefix;
893
894 if (file == -1) {
895 my_prefix = g_strdup_printf("%sf", self->prefix);
896 } else {
897 my_prefix = g_strdup_printf("%sf%08x-", self->prefix, file);
898 }
899
900 result = s3_list_keys(self->s3t[0].s3, self->bucket, NULL, my_prefix, NULL,
901 &objects, &total_size);
902 g_free(my_prefix);
903 if (!result) {
904 guint response_code;
905 s3_error_code_t s3_error_code;
906 CURLcode curl_code;
907
908 s3_error(self->s3t[0].s3, NULL, &response_code,
909 &s3_error_code, NULL, &curl_code, NULL);
910 if (response_code == 404 && s3_error_code == S3_ERROR_NoSuchBucket) {
911 return TRUE;
912 }
913 device_set_error(d_self,
914 g_strdup_printf(_("While listing S3 keys: %s"),
915 s3_strerror(self->s3t[0].s3)),
916 DEVICE_STATUS_DEVICE_ERROR | DEVICE_STATUS_VOLUME_ERROR);
917 return FALSE;
918 }
919
920 g_mutex_lock(self->thread_idle_mutex);
921 if (!self->objects) {
922 self->objects = objects;
923 } else {
924 self->objects = g_slist_concat(self->objects, objects);
925 }
926
927 if (!self->objects) {
928 g_mutex_unlock(self->thread_idle_mutex);
929 return TRUE;
930 }
931
932 // start the threads
933 for (thread = 0; thread < self->nb_threads; thread++) {
934 if (self->s3t[thread].idle == 1) {
935 /* Check if the thread is in error */
936 if (self->s3t[thread].errflags != DEVICE_STATUS_SUCCESS) {
937 device_set_error(d_self,
938 (char *)self->s3t[thread].errmsg,
939 self->s3t[thread].errflags);
940 self->s3t[thread].errflags = DEVICE_STATUS_SUCCESS;
941 self->s3t[thread].errmsg = NULL;
942 g_mutex_unlock(self->thread_idle_mutex);
943 s3_wait_thread_delete(self);
944 return FALSE;
945 }
946 self->s3t[thread].idle = 0;
947 self->s3t[thread].done = 0;
948 g_thread_pool_push(self->thread_pool_delete, &self->s3t[thread],
949 NULL);
950 }
951 }
952 g_cond_wait(self->thread_idle_cond, self->thread_idle_mutex);
953 g_mutex_unlock(self->thread_idle_mutex);
954
955 self->volume_bytes = total_size;
956
957 s3_wait_thread_delete(self);
958
959 return TRUE;
960 }
961
962 static void
963 s3_thread_delete_block(
964 gpointer thread_data,
965 gpointer data)
966 {
967 static int count = 0;
968 S3_by_thread *s3t = (S3_by_thread *)thread_data;
969 Device *pself = (Device *)data;
970 S3Device *self = S3_DEVICE(pself);
971 int result = 1;
972 char *filename;
973 s3_object *object;
974
975 g_mutex_lock(self->thread_idle_mutex);
976 while (result && self->objects) {
977 if (self->use_s3_multi_delete) {
978 GSList *lobjects = NULL;
979 int n = 0;
980 while (self->objects && n<1000) {
981 object = self->objects->data;
982 self->objects = g_slist_remove(self->objects, object);
983 lobjects = g_slist_prepend(lobjects, object);
984 n++;
985 }
986 g_mutex_unlock(self->thread_idle_mutex);
987 result = s3_multi_delete(s3t->s3, (const char *)self->bucket,
988 lobjects);
989 if (result != 1) {
990 if (result == 2) {
991 g_debug("Deleting multiple keys not implemented");
992 } else { /* result == 0 */
993 g_debug("Deleteing multiple keys failed: %s",
994 s3_strerror(s3t->s3));
995 }
996
997 g_mutex_lock(self->thread_idle_mutex);
998 self->use_s3_multi_delete = 0;
999 /* re-add all filenames */
1000 while (lobjects) {
1001 object = lobjects->data;
1002 lobjects = g_slist_remove(lobjects, object);
1003 self->objects = g_slist_prepend(self->objects, object);
1004 }
1005 g_mutex_unlock(self->thread_idle_mutex);
1006 } else {
1007 slist_free_full(lobjects, free_s3_object);
1008 }
1009 } else {
1010 object = self->objects->data;
1011 self->objects = g_slist_remove(self->objects, object);
1012 filename = object->key;
1013 count++;
1014 if (count >= 1000) {
1015 g_debug("Deleting %s ...", filename);
1016 count = 0;
1017 }
1018 g_mutex_unlock(self->thread_idle_mutex);
1019 result = s3_delete(s3t->s3, (const char *)self->bucket,
1020 (const char *)filename);
1021 if (!result) {
1022 s3t->errflags = DEVICE_STATUS_DEVICE_ERROR | DEVICE_STATUS_VOLUME_ERROR;
1023 s3t->errmsg = g_strdup_printf(_("While deleting key '%s': %s"),
1024
1025 filename, s3_strerror(s3t->s3));
1026 }
1027 g_free(filename);
1028 }
1029 g_mutex_lock(self->thread_idle_mutex);
1030 }
1031 s3t->idle = 1;
1032 s3t->done = 1;
1033 g_cond_broadcast(self->thread_idle_cond);
1034 g_mutex_unlock(self->thread_idle_mutex);
1035 }
1036
1037 static void
1038 s3_wait_thread_delete(S3Device *self)
1039 {
1040 Device *d_self = (Device *)self;
1041 int idle_thread = 0;
1042 int thread;
1043
1044 g_mutex_lock(self->thread_idle_mutex);
1045 while (idle_thread != self->nb_threads) {
1046 idle_thread = 0;
1047 for (thread = 0; thread < self->nb_threads; thread++) {
1048 if (self->s3t[thread].idle == 1) {
1049 idle_thread++;
1050 }
1051 /* Check if the thread is in error */
1052 if (self->s3t[thread].errflags != DEVICE_STATUS_SUCCESS) {
1053 device_set_error(d_self, (char *)self->s3t[thread].errmsg,
1054 self->s3t[thread].errflags);
1055 self->s3t[thread].errflags = DEVICE_STATUS_SUCCESS;
1056 self->s3t[thread].errmsg = NULL;
1057 }
1058 }
1059 if (idle_thread != self->nb_threads) {
1060 g_cond_wait(self->thread_idle_cond, self->thread_idle_mutex);
1061 }
1062 }
1063 g_mutex_unlock(self->thread_idle_mutex);
1064 }
1065
1066
1067 static gboolean
1068 delete_all_files(S3Device *self)
1069 {
1070 return delete_file(self, -1);
1071 }
1072
1073 /*
1074 * Class mechanics
1075 */
1076
1077 void
1078 s3_device_register(void)
1079 {
1080 static const char * device_prefix_list[] = { S3_DEVICE_NAME, NULL };
1081 g_assert(s3_init());
1082
1083 /* set up our properties */
1084 device_property_fill_and_register(&device_property_s3_secret_key,
1085 G_TYPE_STRING, "s3_secret_key",
1086 "Secret access key to authenticate with Amazon S3");
1087 device_property_fill_and_register(&device_property_s3_access_key,
1088 G_TYPE_STRING, "s3_access_key",
1089 "Access key ID to authenticate with Amazon S3");
1090 device_property_fill_and_register(&device_property_s3_session_token,
1091 G_TYPE_STRING, "s3_session_token",
1092 "Session token to authenticate with Amazon S3");
1093 device_property_fill_and_register(&device_property_swift_account_id,
1094 G_TYPE_STRING, "swift_account_id",
1095 "Account ID to authenticate with openstack swift");
1096 device_property_fill_and_register(&device_property_swift_access_key,
1097 G_TYPE_STRING, "swift_access_key",
1098 "Access key to authenticate with openstack swift");
1099 device_property_fill_and_register(&device_property_username,
1100 G_TYPE_STRING, "username",
1101 "Username to authenticate with");
1102 device_property_fill_and_register(&device_property_password,
1103 G_TYPE_STRING, "password",
1104 "password to authenticate with");
1105 device_property_fill_and_register(&device_property_tenant_id,
1106 G_TYPE_STRING, "tenant_id",
1107 "tenant_id to authenticate with");
1108 device_property_fill_and_register(&device_property_tenant_name,
1109 G_TYPE_STRING, "tenant_name",
1110 "tenant_name to authenticate with");
1111 device_property_fill_and_register(&device_property_project_name,
1112 G_TYPE_STRING, "project_name",
1113 "project_name to authenticate with");
1114 device_property_fill_and_register(&device_property_domain_name,
1115 G_TYPE_STRING, "domain_name",
1116 "domain_name to authenticate with");
1117 device_property_fill_and_register(&device_property_s3_host,
1118 G_TYPE_STRING, "s3_host",
1119 "hostname:port of the server");
1120 device_property_fill_and_register(&device_property_s3_service_path,
1121 G_TYPE_STRING, "s3_service_path",
1122 "path to add in the url");
1123 device_property_fill_and_register(&device_property_s3_user_token,
1124 G_TYPE_STRING, "s3_user_token",
1125 "User token for authentication Amazon devpay requests");
1126 device_property_fill_and_register(&device_property_s3_bucket_location,
1127 G_TYPE_STRING, "s3_bucket_location",
1128 "Location constraint for buckets on Amazon S3");
1129 device_property_fill_and_register(&device_property_s3_storage_class,
1130 G_TYPE_STRING, "s3_storage_class",
1131 "Storage class as specified by Amazon (STANDARD or REDUCED_REDUNDANCY)");
1132 device_property_fill_and_register(&device_property_s3_server_side_encryption,
1133 G_TYPE_STRING, "s3_server_side_encryption",
1134 "Serve side encryption as specified by Amazon (AES256)");
1135 device_property_fill_and_register(&device_property_storage_api,
1136 G_TYPE_STRING, "storage_api",
1137 "Which cloud API to use.");
1138 device_property_fill_and_register(&device_property_openstack_swift_api,
1139 G_TYPE_STRING, "openstack_swift_api",
1140 "Whether to use openstack protocol");
1141 device_property_fill_and_register(&device_property_client_id,
1142 G_TYPE_STRING, "client_id",
1143 "client_id for use with oauth2");
1144 device_property_fill_and_register(&device_property_client_secret,
1145 G_TYPE_STRING, "client_secret",
1146 "client_secret for use with oauth2");
1147 device_property_fill_and_register(&device_property_refresh_token,
1148 G_TYPE_STRING, "refresh_token",
1149 "refresh_token for use with oauth2");
1150 device_property_fill_and_register(&device_property_project_id,
1151 G_TYPE_STRING, "project_id",
1152 "project id for use with google");
1153 device_property_fill_and_register(&device_property_chunked,
1154 G_TYPE_BOOLEAN, "chunked",
1155 "Whether to use chunked transfer-encoding");
1156 device_property_fill_and_register(&device_property_s3_ssl,
1157 G_TYPE_BOOLEAN, "s3_ssl",
1158 "Whether to use SSL with Amazon S3");
1159
1160 device_property_fill_and_register(&device_property_create_bucket,
1161 G_TYPE_BOOLEAN, "create_bucket",
1162 "Whether to create/delete bucket");
1163 device_property_fill_and_register(&device_property_read_from_glacier,
1164 G_TYPE_BOOLEAN, "read_from_glacier",
1165 "Whether to add code to read from glacier storage class");
1166 device_property_fill_and_register(&device_property_transition_to_glacier,
1167 G_TYPE_UINT64, "transition_to_glacier",
1168 "The number of days to wait before migrating to glacier after set to no-reuse");
1169 device_property_fill_and_register(&device_property_s3_subdomain,
1170 G_TYPE_BOOLEAN, "s3_subdomain",
1171 "Whether to use subdomain");
1172 device_property_fill_and_register(&device_property_s3_multi_delete,
1173 G_TYPE_BOOLEAN, "s3_multi_delete",
1174 "Whether to use multi-delete");
1175 device_property_fill_and_register(&device_property_s3_reps,
1176 G_TYPE_STRING, "reps",
1177 "Number of replicas for data objects in CAStor");
1178 device_property_fill_and_register(&device_property_s3_reps_bucket,
1179 G_TYPE_STRING, "reps_bucket",
1180 "Number of replicas for automatically created buckets in CAStor");
1181 device_property_fill_and_register(&device_property_s3_multi_part_upload,
1182 G_TYPE_BOOLEAN, "s3_multi_part_upload",
1183 "If multi part upload must be used");
1184
1185 device_property_fill_and_register(&device_property_timeout,
1186 G_TYPE_UINT64, "timeout",
1187 "The timeout for one tranfer");
1188
1189 /* register the device itself */
1190 register_device(s3_device_factory, device_prefix_list);
1191 }
1192
1193 GType
1194 s3_device_get_type(void)
1195 {
1196 static GType type = 0;
1197
1198 if (G_UNLIKELY(type == 0)) {
1199 static const GTypeInfo info = {
1200 sizeof (S3DeviceClass),
1201 (GBaseInitFunc) s3_device_base_init,
1202 (GBaseFinalizeFunc) NULL,
1203 (GClassInitFunc) s3_device_class_init,
1204 (GClassFinalizeFunc) NULL,
1205 NULL /* class_data */,
1206 sizeof (S3Device),
1207 0 /* n_preallocs */,
1208 (GInstanceInitFunc) s3_device_init,
1209 NULL
1210 };
1211
1212 type = g_type_register_static (TYPE_DEVICE, "S3Device", &info,
1213 (GTypeFlags)0);
1214 }
1215
1216 return type;
1217 }
1218
1219 static void
1220 s3_device_init(S3Device * self)
1221 {
1222 Device * dself = DEVICE(self);
1223 GValue response;
1224
1225 self->s3_api = S3_API_UNKNOWN;
1226 self->volume_bytes = 0;
1227 self->volume_limit = 0;
1228 self->leom = TRUE;
1229 self->enforce_volume_limit = FALSE;
1230 self->use_subdomain = FALSE;
1231 self->nb_threads = 1;
1232 self->nb_threads_backup = 1;
1233 self->nb_threads_recovery = 1;
1234 self->use_s3_multi_part_upload = FALSE;
1235 self->thread_pool_delete = NULL;
1236 self->thread_pool_write = NULL;
1237 self->thread_pool_read = NULL;
1238 self->thread_idle_cond = NULL;
1239 self->thread_idle_mutex = NULL;
1240 self->use_s3_multi_delete = 1;
1241 self->set_s3_multi_delete = 0;
1242 self->reps = NULL;
1243 self->reps_bucket = NULL;
1244 self->transition_to_glacier = -1;
1245
1246 /* Register property values
1247 * Note: Some aren't added until s3_device_open_device()
1248 */
1249 bzero(&response, sizeof(response));
1250
1251 g_value_init(&response, CONCURRENCY_PARADIGM_TYPE);
1252 g_value_set_enum(&response, CONCURRENCY_PARADIGM_SHARED_READ);
1253 device_set_simple_property(dself, PROPERTY_CONCURRENCY,
1254 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
1255 g_value_unset(&response);
1256
1257 g_value_init(&response, STREAMING_REQUIREMENT_TYPE);
1258 g_value_set_enum(&response, STREAMING_REQUIREMENT_NONE);
1259 device_set_simple_property(dself, PROPERTY_STREAMING,
1260 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
1261 g_value_unset(&response);
1262
1263 g_value_init(&response, G_TYPE_BOOLEAN);
1264 g_value_set_boolean(&response, TRUE);
1265 device_set_simple_property(dself, PROPERTY_APPENDABLE,
1266 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
1267 g_value_unset(&response);
1268
1269 g_value_init(&response, G_TYPE_BOOLEAN);
1270 g_value_set_boolean(&response, TRUE);
1271 device_set_simple_property(dself, PROPERTY_PARTIAL_DELETION,
1272 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
1273 g_value_unset(&response);
1274
1275 g_value_init(&response, G_TYPE_BOOLEAN);
1276 g_value_set_boolean(&response, TRUE);
1277 device_set_simple_property(dself, PROPERTY_FULL_DELETION,
1278 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
1279 g_value_unset(&response);
1280
1281 g_value_init(&response, G_TYPE_BOOLEAN);
1282 g_value_set_boolean(&response, TRUE); /* well, there *is* no EOM on S3 .. */
1283 device_set_simple_property(dself, PROPERTY_LEOM,
1284 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
1285 g_value_unset(&response);
1286
1287 g_value_init(&response, G_TYPE_BOOLEAN);
1288 g_value_set_boolean(&response, FALSE);
1289 device_set_simple_property(dself, PROPERTY_ENFORCE_MAX_VOLUME_USAGE,
1290 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
1291 g_value_unset(&response);
1292
1293 g_value_init(&response, G_TYPE_BOOLEAN);
1294 g_value_set_boolean(&response, FALSE);
1295 device_set_simple_property(dself, PROPERTY_S3_SUBDOMAIN,
1296 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
1297 g_value_unset(&response);
1298
1299 g_value_init(&response, G_TYPE_BOOLEAN);
1300 g_value_set_boolean(&response, FALSE);
1301 device_set_simple_property(dself, PROPERTY_COMPRESSION,
1302 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
1303 g_value_unset(&response);
1304
1305 g_value_init(&response, MEDIA_ACCESS_MODE_TYPE);
1306 g_value_set_enum(&response, MEDIA_ACCESS_MODE_READ_WRITE);
1307 device_set_simple_property(dself, PROPERTY_MEDIUM_ACCESS_TYPE,
1308 &response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
1309 g_value_unset(&response);
1310
1311 }
1312
1313 static void
1314 s3_device_class_init(S3DeviceClass * c G_GNUC_UNUSED)
1315 {
1316 GObjectClass *g_object_class = (GObjectClass*) c;
1317 DeviceClass *device_class = (DeviceClass *)c;
1318
1319 parent_class = g_type_class_ref (TYPE_DEVICE);
1320
1321 device_class->open_device = s3_device_open_device;
1322 device_class->create = s3_device_create;
1323 device_class->read_label = s3_device_read_label;
1324 device_class->start = s3_device_start;
1325 device_class->finish = s3_device_finish;
1326 device_class->get_bytes_read = s3_device_get_bytes_read;
1327 device_class->get_bytes_written = s3_device_get_bytes_written;
1328
1329 device_class->start_file = s3_device_start_file;
1330 device_class->write_block = s3_device_write_block;
1331 device_class->finish_file = s3_device_finish_file;
1332
1333 device_class->init_seek_file = s3_device_init_seek_file;
1334 device_class->seek_file = s3_device_seek_file;
1335 device_class->seek_block = s3_device_seek_block;
1336 device_class->read_block = s3_device_read_block;
1337 device_class->recycle_file = s3_device_recycle_file;
1338
1339 device_class->erase = s3_device_erase;
1340 device_class->set_reuse = s3_device_set_reuse;
1341 device_class->set_no_reuse = s3_device_set_no_reuse;
1342
1343 g_object_class->finalize = s3_device_finalize;
1344 }
1345
1346 static void
1347 s3_device_base_init(
1348 S3DeviceClass *c)
1349 {
1350 DeviceClass *device_class = (DeviceClass *)c;
1351
1352 device_class_register_property(device_class, PROPERTY_S3_ACCESS_KEY,
1353 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1354 device_simple_property_get_fn,
1355 s3_device_set_access_key_fn);
1356
1357 device_class_register_property(device_class, PROPERTY_S3_SECRET_KEY,
1358 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1359 device_simple_property_get_fn,
1360 s3_device_set_secret_key_fn);
1361
1362 device_class_register_property(device_class, PROPERTY_S3_SESSION_TOKEN,
1363 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1364 device_simple_property_get_fn,
1365 s3_device_set_session_token_fn);
1366
1367 device_class_register_property(device_class, PROPERTY_SWIFT_ACCOUNT_ID,
1368 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1369 device_simple_property_get_fn,
1370 s3_device_set_swift_account_id_fn);
1371
1372 device_class_register_property(device_class, PROPERTY_SWIFT_ACCESS_KEY,
1373 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1374 device_simple_property_get_fn,
1375 s3_device_set_swift_access_key_fn);
1376
1377 device_class_register_property(device_class, PROPERTY_USERNAME,
1378 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1379 device_simple_property_get_fn,
1380 s3_device_set_username);
1381
1382 device_class_register_property(device_class, PROPERTY_PASSWORD,
1383 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1384 device_simple_property_get_fn,
1385 s3_device_set_password);
1386
1387 device_class_register_property(device_class, PROPERTY_TENANT_ID,
1388 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1389 device_simple_property_get_fn,
1390 s3_device_set_tenant_id);
1391
1392 device_class_register_property(device_class, PROPERTY_TENANT_NAME,
1393 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1394 device_simple_property_get_fn,
1395 s3_device_set_tenant_name);
1396
1397 device_class_register_property(device_class, PROPERTY_PROJECT_NAME,
1398 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1399 device_simple_property_get_fn,
1400 s3_device_set_project_name);
1401
1402 device_class_register_property(device_class, PROPERTY_DOMAIN_NAME,
1403 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1404 device_simple_property_get_fn,
1405 s3_device_set_domain_name);
1406
1407 device_class_register_property(device_class, PROPERTY_S3_HOST,
1408 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1409 device_simple_property_get_fn,
1410 s3_device_set_host_fn);
1411
1412 device_class_register_property(device_class, PROPERTY_S3_SERVICE_PATH,
1413 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1414 device_simple_property_get_fn,
1415 s3_device_set_service_path_fn);
1416
1417 device_class_register_property(device_class, PROPERTY_S3_USER_TOKEN,
1418 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1419 device_simple_property_get_fn,
1420 s3_device_set_user_token_fn);
1421
1422 device_class_register_property(device_class, PROPERTY_S3_BUCKET_LOCATION,
1423 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1424 device_simple_property_get_fn,
1425 s3_device_set_bucket_location_fn);
1426
1427 device_class_register_property(device_class, PROPERTY_S3_STORAGE_CLASS,
1428 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1429 device_simple_property_get_fn,
1430 s3_device_set_storage_class_fn);
1431
1432 device_class_register_property(device_class, PROPERTY_S3_SERVER_SIDE_ENCRYPTION,
1433 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1434 device_simple_property_get_fn,
1435 s3_device_set_server_side_encryption_fn);
1436
1437 device_class_register_property(device_class, PROPERTY_PROXY,
1438 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1439 device_simple_property_get_fn,
1440 s3_device_set_proxy_fn);
1441
1442 device_class_register_property(device_class, PROPERTY_SSL_CA_INFO,
1443 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1444 device_simple_property_get_fn,
1445 s3_device_set_ca_info_fn);
1446
1447 device_class_register_property(device_class, PROPERTY_VERBOSE,
1448 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1449 device_simple_property_get_fn,
1450 s3_device_set_verbose_fn);
1451
1452 device_class_register_property(device_class, PROPERTY_CREATE_BUCKET,
1453 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1454 device_simple_property_get_fn,
1455 s3_device_set_create_bucket_fn);
1456
1457 device_class_register_property(device_class, PROPERTY_READ_FROM_GLACIER,
1458 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1459 device_simple_property_get_fn,
1460 s3_device_set_read_from_glacier_fn);
1461
1462 device_class_register_property(device_class, PROPERTY_TRANSITION_TO_GLACIER,
1463 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1464 device_simple_property_get_fn,
1465 s3_device_set_transition_to_glacier_fn);
1466
1467 device_class_register_property(device_class, PROPERTY_STORAGE_API,
1468 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1469 device_simple_property_get_fn,
1470 s3_device_set_storage_api);
1471
1472 device_class_register_property(device_class, PROPERTY_OPENSTACK_SWIFT_API,
1473 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1474 device_simple_property_get_fn,
1475 s3_device_set_openstack_swift_api_fn);
1476
1477 device_class_register_property(device_class, PROPERTY_S3_MULTI_DELETE,
1478 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1479 device_simple_property_get_fn,
1480 s3_device_set_s3_multi_delete_fn);
1481
1482 device_class_register_property(device_class, PROPERTY_CHUNKED,
1483 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1484 device_simple_property_get_fn,
1485 s3_device_set_chunked_fn);
1486
1487 device_class_register_property(device_class, PROPERTY_S3_SSL,
1488 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1489 device_simple_property_get_fn,
1490 s3_device_set_ssl_fn);
1491
1492 device_class_register_property(device_class, PROPERTY_REUSE_CONNECTION,
1493 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1494 device_simple_property_get_fn,
1495 s3_device_set_reuse_connection_fn);
1496
1497 device_class_register_property(device_class, PROPERTY_TIMEOUT,
1498 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1499 device_simple_property_get_fn,
1500 s3_device_set_timeout_fn);
1501
1502 device_class_register_property(device_class, PROPERTY_MAX_SEND_SPEED,
1503 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1504 device_simple_property_get_fn,
1505 s3_device_set_max_send_speed_fn);
1506
1507 device_class_register_property(device_class, PROPERTY_MAX_RECV_SPEED,
1508 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1509 device_simple_property_get_fn,
1510 s3_device_set_max_recv_speed_fn);
1511
1512 device_class_register_property(device_class, PROPERTY_NB_THREADS_BACKUP,
1513 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1514 device_simple_property_get_fn,
1515 s3_device_set_nb_threads_backup);
1516
1517 device_class_register_property(device_class, PROPERTY_NB_THREADS_RECOVERY,
1518 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1519 device_simple_property_get_fn,
1520 s3_device_set_nb_threads_recovery);
1521
1522 device_class_register_property(device_class, PROPERTY_S3_MULTI_PART_UPLOAD,
1523 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1524 device_simple_property_get_fn,
1525 s3_device_set_s3_multi_part_upload);
1526
1527 device_class_register_property(device_class, PROPERTY_COMPRESSION,
1528 PROPERTY_ACCESS_GET_MASK,
1529 device_simple_property_get_fn,
1530 NULL);
1531
1532 device_class_register_property(device_class, PROPERTY_LEOM,
1533 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1534 device_simple_property_get_fn,
1535 property_set_leom_fn);
1536
1537 device_class_register_property(device_class, PROPERTY_MAX_VOLUME_USAGE,
1538 (PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_MASK) &
1539 (~ PROPERTY_ACCESS_SET_INSIDE_FILE_WRITE),
1540 device_simple_property_get_fn,
1541 s3_device_set_max_volume_usage_fn);
1542
1543 device_class_register_property(device_class, PROPERTY_ENFORCE_MAX_VOLUME_USAGE,
1544 (PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_MASK) &
1545 (~ PROPERTY_ACCESS_SET_INSIDE_FILE_WRITE),
1546 device_simple_property_get_fn,
1547 s3_device_set_enforce_max_volume_usage_fn);
1548
1549 device_class_register_property(device_class, PROPERTY_S3_SUBDOMAIN,
1550 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1551 device_simple_property_get_fn,
1552 s3_device_set_use_subdomain_fn);
1553
1554 device_class_register_property(device_class, PROPERTY_CLIENT_ID,
1555 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1556 device_simple_property_get_fn,
1557 s3_device_set_client_id_fn);
1558
1559 device_class_register_property(device_class, PROPERTY_CLIENT_SECRET,
1560 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1561 device_simple_property_get_fn,
1562 s3_device_set_client_secret_fn);
1563
1564 device_class_register_property(device_class, PROPERTY_REFRESH_TOKEN,
1565 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1566 device_simple_property_get_fn,
1567 s3_device_set_refresh_token_fn);
1568
1569 device_class_register_property(device_class, PROPERTY_PROJECT_ID,
1570 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1571 device_simple_property_get_fn,
1572 s3_device_set_project_id_fn);
1573
1574 device_class_register_property(device_class, PROPERTY_S3_REPS,
1575 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1576 device_simple_property_get_fn,
1577 s3_device_set_reps_fn);
1578
1579 device_class_register_property(device_class, PROPERTY_S3_REPS_BUCKET,
1580 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
1581 device_simple_property_get_fn,
1582 s3_device_set_reps_bucket_fn);
1583 }
1584
1585 static gboolean
1586 s3_device_set_access_key_fn(Device *p_self, DevicePropertyBase *base,
1587 GValue *val, PropertySurety surety, PropertySource source)
1588 {
1589 S3Device *self = S3_DEVICE(p_self);
1590
1591 amfree(self->access_key);
1592 self->access_key = g_value_dup_string(val);
1593 device_clear_volume_details(p_self);
1594
1595 return device_simple_property_set_fn(p_self, base, val, surety, source);
1596 }
1597
1598 static gboolean
1599 s3_device_set_secret_key_fn(Device *p_self, DevicePropertyBase *base,
1600 GValue *val, PropertySurety surety, PropertySource source)
1601 {
1602 S3Device *self = S3_DEVICE(p_self);
1603
1604 amfree(self->secret_key);
1605 self->secret_key = g_value_dup_string(val);
1606 device_clear_volume_details(p_self);
1607
1608 return device_simple_property_set_fn(p_self, base, val, surety, source);
1609 }
1610
1611 static gboolean
1612 s3_device_set_session_token_fn(Device *p_self, DevicePropertyBase *base,
1613 GValue *val, PropertySurety surety, PropertySource source)
1614 {
1615 S3Device *self = S3_DEVICE(p_self);
1616
1617 amfree(self->session_token);
1618 self->session_token = g_value_dup_string(val);
1619 device_clear_volume_details(p_self);
1620
1621 return device_simple_property_set_fn(p_self, base, val, surety, source);
1622 }
1623
1624 static gboolean
1625 s3_device_set_swift_account_id_fn(Device *p_self, DevicePropertyBase *base,
1626 GValue *val, PropertySurety surety, PropertySource source)
1627 {
1628 S3Device *self = S3_DEVICE(p_self);
1629
1630 amfree(self->swift_account_id);
1631 self->swift_account_id = g_value_dup_string(val);
1632 device_clear_volume_details(p_self);
1633
1634 return device_simple_property_set_fn(p_self, base, val, surety, source);
1635 }
1636
1637 static gboolean
1638 s3_device_set_swift_access_key_fn(Device *p_self, DevicePropertyBase *base,
1639 GValue *val, PropertySurety surety, PropertySource source)
1640 {
1641 S3Device *self = S3_DEVICE(p_self);
1642
1643 amfree(self->swift_access_key);
1644 self->swift_access_key = g_value_dup_string(val);
1645 device_clear_volume_details(p_self);
1646
1647 return device_simple_property_set_fn(p_self, base, val, surety, source);
1648 }
1649
1650 static gboolean
1651 s3_device_set_username(Device *p_self, DevicePropertyBase *base,
1652 GValue *val, PropertySurety surety, PropertySource source)
1653 {
1654 S3Device *self = S3_DEVICE(p_self);
1655
1656 amfree(self->username);
1657 self->username = g_value_dup_string(val);
1658 device_clear_volume_details(p_self);
1659
1660 return device_simple_property_set_fn(p_self, base, val, surety, source);
1661 }
1662
1663 static gboolean
1664 s3_device_set_password(Device *p_self, DevicePropertyBase *base,
1665 GValue *val, PropertySurety surety, PropertySource source)
1666 {
1667 S3Device *self = S3_DEVICE(p_self);
1668
1669 amfree(self->password);
1670 self->password = g_value_dup_string(val);
1671 device_clear_volume_details(p_self);
1672
1673 return device_simple_property_set_fn(p_self, base, val, surety, source);
1674 }
1675
1676 static gboolean
1677 s3_device_set_tenant_id(Device *p_self, DevicePropertyBase *base,
1678 GValue *val, PropertySurety surety, PropertySource source)
1679 {
1680 S3Device *self = S3_DEVICE(p_self);
1681
1682 amfree(self->tenant_id);
1683 self->tenant_id = g_value_dup_string(val);
1684 device_clear_volume_details(p_self);
1685
1686 return device_simple_property_set_fn(p_self, base, val, surety, source);
1687 }
1688
1689 static gboolean
1690 s3_device_set_tenant_name(Device *p_self, DevicePropertyBase *base,
1691 GValue *val, PropertySurety surety, PropertySource source)
1692 {
1693 S3Device *self = S3_DEVICE(p_self);
1694
1695 amfree(self->tenant_name);
1696 self->tenant_name = g_value_dup_string(val);
1697 device_clear_volume_details(p_self);
1698
1699 return device_simple_property_set_fn(p_self, base, val, surety, source);
1700 }
1701
1702 static gboolean
1703 s3_device_set_project_name(Device *p_self, DevicePropertyBase *base,
1704 GValue *val, PropertySurety surety, PropertySource source)
1705 {
1706 S3Device *self = S3_DEVICE(p_self);
1707
1708 amfree(self->project_name);
1709 self->project_name = g_value_dup_string(val);
1710 device_clear_volume_details(p_self);
1711
1712 return device_simple_property_set_fn(p_self, base, val, surety, source);
1713 }
1714
1715 static gboolean
1716 s3_device_set_domain_name(Device *p_self, DevicePropertyBase *base,
1717 GValue *val, PropertySurety surety, PropertySource source)
1718 {
1719 S3Device *self = S3_DEVICE(p_self);
1720
1721 amfree(self->domain_name);
1722 self->domain_name = g_value_dup_string(val);
1723 device_clear_volume_details(p_self);
1724
1725 return device_simple_property_set_fn(p_self, base, val, surety, source);
1726 }
1727
1728 static gboolean
1729 s3_device_set_host_fn(Device *p_self,
1730 DevicePropertyBase *base, GValue *val,
1731 PropertySurety surety, PropertySource source)
1732 {
1733 S3Device *self = S3_DEVICE(p_self);
1734
1735 amfree(self->host);
1736 self->host = g_value_dup_string(val);
1737 device_clear_volume_details(p_self);
1738
1739 return device_simple_property_set_fn(p_self, base, val, surety, source);
1740 }
1741
1742 static gboolean
1743 s3_device_set_service_path_fn(Device *p_self,
1744 DevicePropertyBase *base, GValue *val,
1745 PropertySurety surety, PropertySource source)
1746 {
1747 S3Device *self = S3_DEVICE(p_self);
1748
1749 amfree(self->service_path);
1750 self->service_path = g_value_dup_string(val);
1751 device_clear_volume_details(p_self);
1752
1753 return device_simple_property_set_fn(p_self, base, val, surety, source);
1754 }
1755
1756 static gboolean
1757 s3_device_set_user_token_fn(Device *p_self, DevicePropertyBase *base,
1758 GValue *val, PropertySurety surety, PropertySource source)
1759 {
1760 S3Device *self = S3_DEVICE(p_self);
1761
1762 amfree(self->user_token);
1763 self->user_token = g_value_dup_string(val);
1764 device_clear_volume_details(p_self);
1765
1766 return device_simple_property_set_fn(p_self, base, val, surety, source);
1767 }
1768
1769 static gboolean
1770 s3_device_set_bucket_location_fn(Device *p_self, DevicePropertyBase *base,
1771 GValue *val, PropertySurety surety, PropertySource source)
1772 {
1773 S3Device *self = S3_DEVICE(p_self);
1774 char *str_val = g_value_dup_string(val);
1775
1776 if (str_val[0] && self->use_ssl && !s3_curl_location_compat()) {
1777 device_set_error(p_self, g_strdup(_(
1778 "Location constraint given for Amazon S3 bucket, "
1779 "but libcurl is too old support wildcard certificates.")),
1780 DEVICE_STATUS_DEVICE_ERROR);
1781 goto fail;
1782 }
1783
1784 if (str_val[0] && !s3_bucket_location_compat(self->bucket)) {
1785 device_set_error(p_self, g_strdup_printf(_(
1786 "Location constraint given for Amazon S3 bucket, "
1787 "but the bucket name (%s) is not usable as a subdomain."),
1788 self->bucket),
1789 DEVICE_STATUS_DEVICE_ERROR);
1790 goto fail;
1791 }
1792
1793 amfree(self->bucket_location);
1794 self->bucket_location = str_val;
1795 device_clear_volume_details(p_self);
1796
1797 return device_simple_property_set_fn(p_self, base, val, surety, source);
1798 fail:
1799 g_free(str_val);
1800 return FALSE;
1801 }
1802
1803 static gboolean
1804 s3_device_set_storage_class_fn(Device *p_self, DevicePropertyBase *base,
1805 GValue *val, PropertySurety surety, PropertySource source)
1806 {
1807 S3Device *self = S3_DEVICE(p_self);
1808 char *str_val = g_value_dup_string(val);
1809
1810 amfree(self->storage_class);
1811 self->storage_class = str_val;
1812 device_clear_volume_details(p_self);
1813
1814 return device_simple_property_set_fn(p_self, base, val, surety, source);
1815 }
1816
1817 static gboolean
1818 s3_device_set_server_side_encryption_fn(Device *p_self, DevicePropertyBase *base,
1819 GValue *val, PropertySurety surety, PropertySource source)
1820 {
1821 S3Device *self = S3_DEVICE(p_self);
1822 char *str_val = g_value_dup_string(val);
1823
1824 amfree(self->server_side_encryption);
1825 self->server_side_encryption = str_val;
1826 device_clear_volume_details(p_self);
1827
1828 return device_simple_property_set_fn(p_self, base, val, surety, source);
1829 }
1830
1831 static gboolean
1832 s3_device_set_proxy_fn(Device *p_self, DevicePropertyBase *base,
1833 GValue *val, PropertySurety surety, PropertySource source)
1834 {
1835 S3Device *self = S3_DEVICE(p_self);
1836 char *str_val = g_value_dup_string(val);
1837
1838 amfree(self->proxy);
1839 self->proxy = str_val;
1840 device_clear_volume_details(p_self);
1841
1842 return device_simple_property_set_fn(p_self, base, val, surety, source);
1843 }
1844
1845 static gboolean
1846 s3_device_set_ca_info_fn(Device *p_self, DevicePropertyBase *base,
1847 GValue *val, PropertySurety surety, PropertySource source)
1848 {
1849 S3Device *self = S3_DEVICE(p_self);
1850
1851 amfree(self->ca_info);
1852 self->ca_info = g_value_dup_string(val);
1853 device_clear_volume_details(p_self);
1854
1855 return device_simple_property_set_fn(p_self, base, val, surety, source);
1856 }
1857
1858 static gboolean
1859 s3_device_set_verbose_fn(Device *p_self, DevicePropertyBase *base,
1860 GValue *val, PropertySurety surety, PropertySource source)
1861 {
1862 S3Device *self = S3_DEVICE(p_self);
1863 int thread;
1864
1865 self->verbose = g_value_get_boolean(val);
1866 /* Our S3 handle may not yet have been instantiated; if so, it will
1867 * get the proper verbose setting when it is created */
1868 if (self->s3t) {
1869 for (thread = 0; thread < self->nb_threads; thread++) {
1870 if (self->s3t[thread].s3)
1871 s3_verbose(self->s3t[thread].s3, self->verbose);
1872 }
1873 }
1874
1875 return device_simple_property_set_fn(p_self, base, val, surety, source);
1876 }
1877
1878 static gboolean
1879 s3_device_set_create_bucket_fn(Device *p_self, DevicePropertyBase *base,
1880 GValue *val, PropertySurety surety, PropertySource source)
1881 {
1882 S3Device *self = S3_DEVICE(p_self);
1883
1884 self->create_bucket = g_value_get_boolean(val);
1885
1886 return device_simple_property_set_fn(p_self, base, val, surety, source);
1887 }
1888
1889 static gboolean
1890 s3_device_set_read_from_glacier_fn(Device *p_self, DevicePropertyBase *base,
1891 GValue *val, PropertySurety surety, PropertySource source)
1892 {
1893 S3Device *self = S3_DEVICE(p_self);
1894
1895 self->read_from_glacier = g_value_get_boolean(val);
1896
1897 return device_simple_property_set_fn(p_self, base, val, surety, source);
1898 }
1899
1900 static gboolean
1901 s3_device_set_transition_to_glacier_fn(Device *p_self, DevicePropertyBase *base,
1902 GValue *val, PropertySurety surety, PropertySource source)
1903 {
1904 S3Device *self = S3_DEVICE(p_self);
1905
1906 self->transition_to_glacier = g_value_get_uint64(val);
1907
1908 return device_simple_property_set_fn(p_self, base, val, surety, source);
1909 }
1910
1911 static gboolean
1912 s3_device_set_storage_api(Device *p_self, DevicePropertyBase *base,
1913 GValue *val, PropertySurety surety, PropertySource source)
1914 {
1915 S3Device *self = S3_DEVICE(p_self);
1916
1917 const char *storage_api = g_value_get_string(val);
1918 if (g_str_equal(storage_api, "S3")) {
1919 self->s3_api = S3_API_S3;
1920 if (!self->set_s3_multi_delete)
1921 self->use_s3_multi_delete = 1;
1922 } else if (g_str_equal(storage_api, "SWIFT-1.0")) {
1923 self->s3_api = S3_API_SWIFT_1;
1924 if (!self->set_s3_multi_delete)
1925 self->use_s3_multi_delete = 0;
1926 } else if (g_str_equal(storage_api, "SWIFT-2.0")) {
1927 self->s3_api = S3_API_SWIFT_2;
1928 if (!self->set_s3_multi_delete)
1929 self->use_s3_multi_delete = 0;
1930 } else if (g_str_equal(storage_api, "SWIFT-3")) {
1931 self->s3_api = S3_API_SWIFT_3;
1932 if (!self->set_s3_multi_delete)
1933 self->use_s3_multi_delete = 1;
1934 } else if (g_str_equal(storage_api, "OAUTH2")) {
1935 self->s3_api = S3_API_OAUTH2;
1936 if (!self->set_s3_multi_delete)
1937 self->use_s3_multi_delete = 0;
1938 } else if (g_str_equal(storage_api, "AWS4")) {
1939 self->s3_api = S3_API_AWS4;
1940 if (!self->set_s3_multi_delete)
1941 self->use_s3_multi_delete = 1;
1942 } else if (g_str_equal(storage_api, "CASTOR")) {
1943 #if LIBCURL_VERSION_NUM >= 0x071301
1944 curl_version_info_data *info;
1945 if (!self->set_s3_multi_delete)
1946 self->use_s3_multi_delete = 0;
1947 /* check the runtime version too */
1948 info = curl_version_info(CURLVERSION_NOW);
1949 if (info->version_num >= 0x071301) {
1950 self->s3_api = S3_API_CASTOR;
1951 } else {
1952 device_set_error(p_self, g_strdup_printf(_(
1953 "Error setting STORAGE-API to castor "
1954 "(You must install libcurl 7.19.1 or newer)")),
1955 DEVICE_STATUS_DEVICE_ERROR);
1956 return FALSE;
1957 }
1958 #else
1959 device_set_error(p_self, g_strdup_printf(_(
1960 "Error setting STORAGE-API to castor "
1961 "This amanda is compiled with a too old libcurl, you must compile with libcurl 7.19.1 or newer")),
1962 DEVICE_STATUS_DEVICE_ERROR);
1963 return FALSE;
1964 #endif
1965 } else {
1966 g_debug("Invalid STORAGE_API, using \"S3\".");
1967 self->s3_api = S3_API_S3;
1968 }
1969
1970 return device_simple_property_set_fn(p_self, base, val, surety, source);
1971 }
1972
1973 static gboolean
1974 s3_device_set_openstack_swift_api_fn(Device *p_self, DevicePropertyBase *base,
1975 GValue *val, PropertySurety surety, PropertySource source)
1976 {
1977
1978 const gboolean openstack_swift_api = g_value_get_boolean(val);
1979 if (openstack_swift_api) {
1980 GValue storage_api_val;
1981 bzero(&storage_api_val, sizeof(GValue));
1982 g_value_init(&storage_api_val, G_TYPE_STRING);
1983 g_value_set_static_string(&storage_api_val, "SWIFT-1.0");
1984 return s3_device_set_storage_api(p_self, base, &storage_api_val,
1985 surety, source);
1986 }
1987 return TRUE;
1988 }
1989
1990 static gboolean
1991 s3_device_set_s3_multi_delete_fn(Device *p_self,
1992 DevicePropertyBase *base, GValue *val,
1993 PropertySurety surety, PropertySource source)
1994 {
1995 S3Device *self = S3_DEVICE(p_self);
1996
1997 self->use_s3_multi_delete = g_value_get_boolean(val);
1998
1999 return device_simple_property_set_fn(p_self, base, val, surety, source);
2000 }
2001
2002 static gboolean
2003 s3_device_set_ssl_fn(Device *p_self, DevicePropertyBase *base,
2004 GValue *val, PropertySurety surety, PropertySource source)
2005 {
2006 S3Device *self = S3_DEVICE(p_self);
2007 gboolean new_val;
2008 int thread;
2009
2010 new_val = g_value_get_boolean(val);
2011 /* Our S3 handle may not yet have been instantiated; if so, it will
2012 * get the proper use_ssl setting when it is created */
2013 if (self->s3t) {
2014 for (thread = 0; thread < self->nb_threads; thread++) {
2015 if (self->s3t[thread].s3 && !s3_use_ssl(self->s3t[thread].s3, new_val)) {
2016 device_set_error(p_self, g_strdup_printf(_(
2017 "Error setting S3 SSL/TLS use "
2018 "(tried to enable SSL/TLS for S3, but curl doesn't support it?)")),
2019 DEVICE_STATUS_DEVICE_ERROR);
2020 return FALSE;
2021 }
2022 }
2023 }
2024 self->use_ssl = new_val;
2025
2026 return device_simple_property_set_fn(p_self, base, val, surety, source);
2027 }
2028
2029 static gboolean
2030 s3_device_set_chunked_fn(Device *p_self, DevicePropertyBase *base,
2031 GValue *val, PropertySurety surety, PropertySource source)
2032 {
2033 S3Device *self = S3_DEVICE(p_self);
2034 gboolean new_val;
2035
2036 new_val = g_value_get_boolean(val);
2037
2038 self->chunked = new_val;
2039
2040 return device_simple_property_set_fn(p_self, base, val, surety, source);
2041 }
2042
2043 static gboolean
2044 s3_device_set_reuse_connection_fn(Device *p_self, DevicePropertyBase *base,
2045 GValue *val, PropertySurety surety, PropertySource source)
2046 {
2047 S3Device *self = S3_DEVICE(p_self);
2048
2049 self->reuse_connection = g_value_get_boolean(val);
2050
2051 return device_simple_property_set_fn(p_self, base, val, surety, source);
2052 }
2053
2054 static gboolean
2055 s3_device_set_timeout_fn(Device *p_self, DevicePropertyBase *base,
2056 GValue *val, PropertySurety surety, PropertySource source)
2057 {
2058 S3Device *self = S3_DEVICE(p_self);
2059
2060 self->timeout = g_value_get_uint64(val);
2061 if (self->timeout > 0 && self->timeout < 300)
2062 self->timeout = 300;
2063
2064 return device_simple_property_set_fn(p_self, base, val, surety, source);
2065 }
2066
2067 static gboolean
2068 s3_device_set_max_send_speed_fn(Device *p_self,
2069 DevicePropertyBase *base, GValue *val,
2070 PropertySurety surety, PropertySource source)
2071 {
2072 S3Device *self = S3_DEVICE(p_self);
2073 guint64 new_val;
2074 int thread;
2075
2076 new_val = g_value_get_uint64(val);
2077 if (new_val && new_val < 5120) {
2078 for (thread = 0; thread < self->nb_threads; thread++) {
2079 device_set_error(p_self,
2080 g_strdup("MAX-SEND-SPEED property is too low (minimum value is 5120)"),
2081 DEVICE_STATUS_DEVICE_ERROR);
2082 }
2083 return FALSE;
2084 }
2085 if (self->s3t) {
2086 for (thread = 0; thread < self->nb_threads; thread++) {
2087 if (self->s3t[thread].s3 && !s3_set_max_send_speed(self->s3t[thread].s3, new_val)) {
2088 device_set_error(p_self,
2089 g_strdup("Could not set S3 maximum send speed"),
2090 DEVICE_STATUS_DEVICE_ERROR);
2091 return FALSE;
2092 }
2093 }
2094 }
2095 self->max_send_speed = new_val;
2096
2097 return device_simple_property_set_fn(p_self, base, val, surety, source);
2098 }
2099
2100 static gboolean
2101 s3_device_set_max_recv_speed_fn(Device *p_self,
2102 DevicePropertyBase *base, GValue *val,
2103 PropertySurety surety, PropertySource source)
2104 {
2105 S3Device *self = S3_DEVICE(p_self);
2106 guint64 new_val;
2107 int thread;
2108
2109 new_val = g_value_get_uint64(val);
2110 if (new_val && new_val < 5120) {
2111 for (thread = 0; thread < self->nb_threads; thread++) {
2112 device_set_error(p_self,
2113 g_strdup("MAX-RECV-SPEED property is too low (minimum value is 5120)"),
2114 DEVICE_STATUS_DEVICE_ERROR);
2115 }
2116 return FALSE;
2117 }
2118 if (self->s3t) {
2119 for (thread = 0; thread < self->nb_threads; thread++) {
2120 if (self->s3t[thread].s3 &&
2121 !s3_set_max_recv_speed(self->s3t[thread].s3, new_val)) {
2122 device_set_error(p_self,
2123 g_strdup("Could not set S3 maximum recv speed"),
2124 DEVICE_STATUS_DEVICE_ERROR);
2125 return FALSE;
2126 }
2127 }
2128 }
2129 self->max_recv_speed = new_val;
2130
2131 return device_simple_property_set_fn(p_self, base, val, surety, source);
2132 }
2133
2134 static gboolean
2135 s3_device_set_nb_threads_backup(Device *p_self,
2136 DevicePropertyBase *base, GValue *val,
2137 PropertySurety surety, PropertySource source)
2138 {
2139 S3Device *self = S3_DEVICE(p_self);
2140 guint64 new_val;
2141
2142 new_val = g_value_get_uint64(val);
2143 self->nb_threads_backup = new_val;
2144 if (self->nb_threads_backup > self->nb_threads) {
2145 self->nb_threads = self->nb_threads_backup;
2146 }
2147
2148 return device_simple_property_set_fn(p_self, base, val, surety, source);
2149 }
2150
2151 static gboolean
2152 s3_device_set_nb_threads_recovery(Device *p_self,
2153 DevicePropertyBase *base, GValue *val,
2154 PropertySurety surety, PropertySource source)
2155 {
2156 S3Device *self = S3_DEVICE(p_self);
2157 guint64 new_val;
2158
2159 new_val = g_value_get_uint64(val);
2160 self->nb_threads_recovery = new_val;
2161 if (self->nb_threads_recovery > self->nb_threads) {
2162 self->nb_threads = self->nb_threads_recovery;
2163 }
2164
2165 return device_simple_property_set_fn(p_self, base, val, surety, source);
2166 }
2167
2168 static gboolean
2169 s3_device_set_s3_multi_part_upload(Device *p_self,
2170 DevicePropertyBase *base, GValue *val,
2171 PropertySurety surety, PropertySource source)
2172 {
2173 S3Device *self = S3_DEVICE(p_self);
2174
2175 self->use_s3_multi_part_upload = g_value_get_boolean(val);
2176
2177 return device_simple_property_set_fn(p_self, base, val, surety, source);
2178 }
2179
2180 static gboolean
2181 s3_device_set_max_volume_usage_fn(Device *p_self,
2182 DevicePropertyBase *base, GValue *val,
2183 PropertySurety surety, PropertySource source)
2184 {
2185 S3Device *self = S3_DEVICE(p_self);
2186
2187 self->volume_limit = g_value_get_uint64(val);
2188
2189 return device_simple_property_set_fn(p_self, base, val, surety, source);
2190
2191 }
2192
2193 static gboolean
2194 s3_device_set_enforce_max_volume_usage_fn(Device *p_self,
2195 DevicePropertyBase *base, GValue *val,
2196 PropertySurety surety, PropertySource source)
2197 {
2198 S3Device *self = S3_DEVICE(p_self);
2199
2200 self->enforce_volume_limit = g_value_get_boolean(val);
2201
2202 return device_simple_property_set_fn(p_self, base, val, surety, source);
2203
2204 }
2205
2206 static gboolean
2207 s3_device_set_use_subdomain_fn(Device *p_self,
2208 DevicePropertyBase *base, GValue *val,
2209 PropertySurety surety, PropertySource source)
2210 {
2211 S3Device *self = S3_DEVICE(p_self);
2212
2213 self->use_subdomain = g_value_get_boolean(val);
2214
2215 if (self->use_subdomain && !s3_bucket_location_compat(self->bucket)) {
2216 device_set_error(p_self, g_strdup_printf(_(
2217 "S3-SUBDOMAIN is set, "
2218 "but the bucket name (%s) is not usable as a subdomain, only [a-zo-9-] characters are allowed."),
2219 self->bucket),
2220 DEVICE_STATUS_DEVICE_ERROR);
2221 self->use_subdomain = FALSE;
2222 return FALSE;
2223 }
2224
2225 return device_simple_property_set_fn(p_self, base, val, surety, source);
2226 }
2227
2228 static gboolean
2229 property_set_leom_fn(Device *p_self,
2230 DevicePropertyBase *base, GValue *val,
2231 PropertySurety surety, PropertySource source)
2232 {
2233 S3Device *self = S3_DEVICE(p_self);
2234
2235 self->leom = g_value_get_boolean(val);
2236
2237 return device_simple_property_set_fn(p_self, base, val, surety, source);
2238 }
2239
2240 static gboolean
2241 s3_device_set_client_id_fn(Device *p_self,
2242 DevicePropertyBase *base, GValue *val,
2243 PropertySurety surety, PropertySource source)
2244 {
2245 S3Device *self = S3_DEVICE(p_self);
2246
2247 amfree(self->client_id);
2248 self->client_id = g_value_dup_string(val);
2249
2250 return device_simple_property_set_fn(p_self, base, val, surety, source);
2251 }
2252
2253 static gboolean
2254 s3_device_set_client_secret_fn(Device *p_self,
2255 DevicePropertyBase *base, GValue *val,
2256 PropertySurety surety, PropertySource source)
2257 {
2258 S3Device *self = S3_DEVICE(p_self);
2259
2260 amfree(self->client_secret);
2261 self->client_secret = g_value_dup_string(val);
2262
2263 return device_simple_property_set_fn(p_self, base, val, surety, source);
2264 }
2265
2266 static gboolean
2267 s3_device_set_refresh_token_fn(Device *p_self,
2268 DevicePropertyBase *base, GValue *val,
2269 PropertySurety surety, PropertySource source)
2270 {
2271 S3Device *self = S3_DEVICE(p_self);
2272
2273 amfree(self->refresh_token);
2274 self->refresh_token = g_value_dup_string(val);
2275
2276 return device_simple_property_set_fn(p_self, base, val, surety, source);
2277 }
2278
2279 static gboolean
2280 s3_device_set_project_id_fn(Device *p_self,
2281 DevicePropertyBase *base, GValue *val,
2282 PropertySurety surety, PropertySource source)
2283 {
2284 S3Device *self = S3_DEVICE(p_self);
2285
2286 amfree(self->project_id);
2287 self->project_id = g_value_dup_string(val);
2288
2289 return device_simple_property_set_fn(p_self, base, val, surety, source);
2290 }
2291
2292 static gboolean
2293 s3_device_set_reps_fn(Device *p_self, DevicePropertyBase *base,
2294 GValue *val, PropertySurety surety, PropertySource source)
2295 {
2296 S3Device *self = S3_DEVICE(p_self);
2297
2298 amfree(self->reps);
2299 self->reps = g_value_dup_string(val);
2300 device_clear_volume_details(p_self);
2301
2302 return device_simple_property_set_fn(p_self, base, val, surety, source);
2303 }
2304
2305 static gboolean
2306 s3_device_set_reps_bucket_fn(Device *p_self, DevicePropertyBase *base,
2307 GValue *val, PropertySurety surety, PropertySource source)
2308 {
2309 S3Device *self = S3_DEVICE(p_self);
2310
2311 amfree(self->reps_bucket);
2312 self->reps_bucket = g_value_dup_string(val);
2313 device_clear_volume_details(p_self);
2314
2315 return device_simple_property_set_fn(p_self, base, val, surety, source);
2316 }
2317
2318 static Device*
2319 s3_device_factory(char * device_name, char * device_type, char * device_node)
2320 {
2321 Device *rval;
2322 g_assert(g_str_equal(device_type, S3_DEVICE_NAME));
2323 rval = DEVICE(g_object_new(TYPE_S3_DEVICE, NULL));
2324
2325 device_open_device(rval, device_name, device_type, device_node);
2326 return rval;
2327 }
2328
2329 /*
2330 * Virtual function overrides
2331 */
2332
2333 static void
2334 s3_device_open_device(Device *pself, char *device_name,
2335 char * device_type, char * device_node)
2336 {
2337 S3Device *self = S3_DEVICE(pself);
2338 char * name_colon;
2339 GValue tmp_value;
2340
2341 pself->min_block_size = S3_DEVICE_MIN_BLOCK_SIZE;
2342 pself->max_block_size = S3_DEVICE_MAX_BLOCK_SIZE;
2343 pself->block_size = S3_DEVICE_DEFAULT_BLOCK_SIZE;
2344
2345 /* Device name may be bucket/prefix, to support multiple volumes in a
2346 * single bucket. */
2347 name_colon = strchr(device_node, '/');
2348 if (name_colon == NULL) {
2349 self->bucket = g_strdup(device_node);
2350 self->prefix = g_strdup("");
2351 } else {
2352 self->bucket = g_strndup(device_node, name_colon - device_node);
2353 self->prefix = g_strdup(name_colon + 1);
2354 }
2355
2356 if (self->bucket == NULL || self->bucket[0] == '\0') {
2357 device_set_error(pself,
2358 g_strdup_printf(_("Empty bucket name in device %s"), device_name),
2359 DEVICE_STATUS_DEVICE_ERROR);
2360 amfree(self->bucket);
2361 amfree(self->prefix);
2362 return;
2363 }
2364
2365 if (self->reps == NULL) {
2366 self->reps = g_strdup(S3_DEVICE_REPS_DEFAULT);
2367 }
2368
2369 if (self->reps_bucket == NULL) {
2370 self->reps_bucket = g_strdup(S3_DEVICE_REPS_BUCKET_DEFAULT);
2371 }
2372
2373 g_debug(_("S3 driver using bucket '%s', prefix '%s'"), self->bucket, self->prefix);
2374 g_debug("curl version: %s", curl_version());
2375 #ifdef LIBCURL_USE_OPENSSL
2376 g_debug("curl compiled for OPENSSL");
2377 #else
2378 #if defined LIBCURL_USE_GNUTLS
2379 g_debug("curl compiled for GNUTLS");
2380 #else
2381 g_debug("curl compiled for NSS");
2382 #endif
2383 #endif
2384
2385 /* default values */
2386 self->verbose = FALSE;
2387 self->s3_api = S3_API_UNKNOWN;
2388
2389 /* use SSL if available */
2390 self->use_ssl = s3_curl_supports_ssl();
2391 bzero(&tmp_value, sizeof(GValue));
2392 g_value_init(&tmp_value, G_TYPE_BOOLEAN);
2393 g_value_set_boolean(&tmp_value, self->use_ssl);
2394 device_set_simple_property(pself, device_property_s3_ssl.ID,
2395 &tmp_value, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DEFAULT);
2396
2397 /* reuse connection */
2398 self->reuse_connection = TRUE;
2399 bzero(&tmp_value, sizeof(GValue));
2400 g_value_init(&tmp_value, G_TYPE_BOOLEAN);
2401 g_value_set_boolean(&tmp_value, self->reuse_connection);
2402 device_set_simple_property(pself, device_property_reuse_connection.ID,
2403 &tmp_value, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DEFAULT);
2404
2405 /* timeout */
2406 self->timeout = 0;
2407 bzero(&tmp_value, sizeof(GValue));
2408 g_value_init(&tmp_value, G_TYPE_UINT64);
2409 g_value_set_uint64(&tmp_value, self->timeout);
2410 device_set_simple_property(pself, device_property_timeout.ID,
2411 &tmp_value, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DEFAULT);
2412
2413 /* Set default create_bucket */
2414 self->create_bucket = TRUE;
2415 bzero(&tmp_value, sizeof(GValue));
2416 g_value_init(&tmp_value, G_TYPE_BOOLEAN);
2417 g_value_set_boolean(&tmp_value, self->create_bucket);
2418 device_set_simple_property(pself, device_property_create_bucket.ID,
2419 &tmp_value, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DEFAULT);
2420
2421 if (parent_class->open_device) {
2422 parent_class->open_device(pself, device_name, device_type, device_node);
2423 }
2424 }
2425
2426 static gboolean
2427 s3_device_create(Device *pself)
2428 {
2429 S3Device *self = S3_DEVICE(pself);
2430 guint response_code;
2431 s3_error_code_t s3_error_code;
2432
2433 if (!setup_handle(self)) {
2434 /* setup_handle already set our error message */
2435 return FALSE;
2436 }
2437
2438 if (!s3_make_bucket(self->s3t[0].s3, self->bucket, self->project_id)) {
2439 s3_error(self->s3t[0].s3, NULL, &response_code, &s3_error_code, NULL, NULL, NULL);
2440
2441 device_set_error(pself,
2442 g_strdup_printf(_("While creating S3 bucket: %s"), s3_strerror(self->s3t[0].s3)),
2443 DEVICE_STATUS_DEVICE_ERROR);
2444 return FALSE;
2445 }
2446
2447 self->bucket_made = TRUE;
2448
2449 if (parent_class->create) {
2450 return parent_class->create(pself);
2451 }
2452 return TRUE;
2453 }
2454
2455 static void s3_device_finalize(GObject * obj_self) {
2456 S3Device *self = S3_DEVICE (obj_self);
2457 int thread;
2458
2459 if(G_OBJECT_CLASS(parent_class)->finalize)
2460 (* G_OBJECT_CLASS(parent_class)->finalize)(obj_self);
2461
2462 if (self->thread_pool_delete) {
2463 g_thread_pool_free(self->thread_pool_delete, 1, 1);
2464 self->thread_pool_delete = NULL;
2465 }
2466 if (self->thread_pool_write) {
2467 g_thread_pool_free(self->thread_pool_write, 1, 1);
2468 self->thread_pool_write = NULL;
2469 }
2470 if (self->thread_pool_read) {
2471 g_thread_pool_free(self->thread_pool_read, 1, 1);
2472 self->thread_pool_read = NULL;
2473 }
2474 if (self->thread_idle_mutex) {
2475 g_mutex_free(self->thread_idle_mutex);
2476 self->thread_idle_mutex = NULL;
2477 }
2478 if (self->thread_idle_cond) {
2479 g_cond_free(self->thread_idle_cond);
2480 self->thread_idle_cond = NULL;
2481 }
2482 if (self->s3t) {
2483 for (thread = 0; thread < self->nb_threads; thread++) {
2484 g_mutex_free(self->s3t[thread].now_mutex);
2485 if(self->s3t[thread].s3) s3_free(self->s3t[thread].s3);
2486 g_free(self->s3t[thread].curl_buffer.buffer);
2487 }
2488 g_free(self->s3t);
2489 }
2490 if (self->catalog_filename) {
2491 catalog_close(self);
2492 }
2493 if(self->bucket) g_free(self->bucket);
2494 if(self->prefix) g_free(self->prefix);
2495 if(self->access_key) g_free(self->access_key);
2496 if(self->secret_key) g_free(self->secret_key);
2497 if(self->session_token) g_free(self->session_token);
2498 if(self->swift_account_id) g_free(self->swift_account_id);
2499 if(self->swift_access_key) g_free(self->swift_access_key);
2500 if(self->username) g_free(self->username);
2501 if(self->password) g_free(self->password);
2502 if(self->tenant_id) g_free(self->tenant_id);
2503 if(self->tenant_name) g_free(self->tenant_name);
2504 if(self->project_name) g_free(self->project_name);
2505 if(self->domain_name) g_free(self->domain_name);
2506 if(self->host) g_free(self->host);
2507 if(self->service_path) g_free(self->service_path);
2508 if(self->user_token) g_free(self->user_token);
2509 if(self->bucket_location) g_free(self->bucket_location);
2510 if(self->storage_class) g_free(self->storage_class);
2511 if(self->server_side_encryption) g_free(self->server_side_encryption);
2512 if(self->proxy) g_free(self->proxy);
2513 if(self->ca_info) g_free(self->ca_info);
2514 if(self->reps) g_free(self->reps);
2515 if(self->reps_bucket) g_free(self->reps_bucket);
2516 }
2517
2518 static gboolean
2519 catalog_open(
2520 S3Device *self)
2521 {
2522 char *filename;
2523 char *dirname;
2524 FILE *file;
2525 char line[1025];
2526
2527 /* create the directory */
2528 filename = g_strdup_printf("bucket-%s", self->bucket);
2529 dirname = config_dir_relative(filename);
2530 if (mkdir(dirname, 0700) == -1 && errno != EEXIST) {
2531 g_debug("Can't create catalog directory '%s': %s",
2532 dirname, strerror(errno));
2533 return FALSE;
2534 }
2535 amfree(filename);
2536 amfree(dirname);
2537
2538 filename = g_strdup_printf("bucket-%s/%s", self->bucket, self->prefix);
2539 g_free(self->catalog_filename);
2540 self->catalog_filename = config_dir_relative(filename);
2541 g_free(filename);
2542 file = fopen(self->catalog_filename, "r");
2543 if (!file) {
2544 g_free(self->catalog_label);
2545 g_free(self->catalog_header);
2546 self->catalog_label = NULL;
2547 self->catalog_header = NULL;
2548 return TRUE;
2549 }
2550 if (!fgets(line, 1024, file)) {
2551 fclose(file);
2552 return FALSE;
2553 }
2554 if (line[strlen(line)-1] == '\n')
2555 line[strlen(line)-1] = '\0';
2556 g_free(self->catalog_label);
2557 self->catalog_label = g_strdup(line+7);
2558 if (!fgets(line, 1024, file)) {
2559 fclose(file);
2560 return FALSE;
2561 }
2562 if (line[strlen(line)-1] == '\n')
2563 line[strlen(line)-1] = '\0';
2564 g_free(self->catalog_header);
2565 self->catalog_header = g_strdup(line+8);
2566 fclose(file);
2567 return TRUE;
2568 }
2569
2570 static gboolean
2571 write_catalog(
2572 S3Device *self)
2573 {
2574 FILE *file;
2575
2576 if (!self->catalog_label || !self->catalog_header)
2577 return TRUE;
2578
2579 file = fopen(self->catalog_filename, "w");
2580 if (!file) {
2581 return FALSE;
2582 }
2583 g_fprintf(file,"LABEL: %s\n", self->catalog_label);
2584 g_fprintf(file,"HEADER: %s\n", self->catalog_header);
2585 fclose(file);
2586 return TRUE;
2587 }
2588
2589 static gboolean
2590 catalog_reset(
2591 S3Device *self,
2592 char *header,
2593 char *label)
2594 {
2595 g_free(self->catalog_header);
2596 self->catalog_header = quote_string(header);
2597 g_free(self->catalog_label);
2598 self->catalog_label = g_strdup(label);
2599
2600 return write_catalog(self);
2601 }
2602
2603 static gboolean
2604 catalog_remove(
2605 S3Device *self)
2606 {
2607 unlink(self->catalog_filename);
2608 amfree(self->catalog_filename);
2609 amfree(self->catalog_label);
2610 amfree(self->catalog_header);
2611 return TRUE;
2612 }
2613
2614 static gboolean
2615 catalog_close(
2616 S3Device *self)
2617 {
2618 gboolean result;
2619
2620 result = write_catalog(self);
2621 amfree(self->catalog_filename);
2622 amfree(self->catalog_label);
2623 amfree(self->catalog_header);
2624 return result;
2625 }
2626
2627 static gboolean
2628 setup_handle(S3Device * self) {
2629 Device *d_self = DEVICE(self);
2630 int thread;
2631 guint response_code;
2632 s3_error_code_t s3_error_code;
2633 CURLcode curl_code;
2634
2635 catalog_open(self);
2636
2637 if (self->s3_api == S3_API_UNKNOWN) {
2638 if (self->host && strlen(self->host) > 14 &&
2639 g_strncasecmp(self->host+strlen(self->host)-14, ".amazonaws.com", 14) == 0) {
2640 self->s3_api = S3_API_AWS4;
2641 } else {
2642 self->s3_api = S3_API_S3;
2643 }
2644 }
2645
2646 if (self->s3t == NULL) {
2647 if (self->s3_api == S3_API_S3) {
2648 if (self->access_key == NULL || self->access_key[0] == '\0') {
2649 device_set_error(d_self,
2650 g_strdup(_("No Amazon access key specified")),
2651 DEVICE_STATUS_DEVICE_ERROR);
2652 return FALSE;
2653 }
2654
2655 if (self->secret_key == NULL || self->secret_key[0] == '\0') {
2656 device_set_error(d_self,
2657 g_strdup(_("No Amazon secret key specified")),
2658 DEVICE_STATUS_DEVICE_ERROR);
2659 return FALSE;
2660 }
2661 } else if (self->s3_api == S3_API_SWIFT_1) {
2662 if (self->swift_account_id == NULL ||
2663 self->swift_account_id[0] == '\0') {
2664 device_set_error(d_self,
2665 g_strdup(_("No Swift account id specified")),
2666 DEVICE_STATUS_DEVICE_ERROR);
2667 return FALSE;
2668 }
2669 if (self->swift_access_key == NULL ||
2670 self->swift_access_key[0] == '\0') {
2671 device_set_error(d_self,
2672 g_strdup(_("No Swift access key specified")),
2673 DEVICE_STATUS_DEVICE_ERROR);
2674 return FALSE;
2675 }
2676 } else if (self->s3_api == S3_API_SWIFT_2) {
2677 if (!((self->username && self->password && self->tenant_id) ||
2678 (self->username && self->password && self->tenant_name) ||
2679 (self->access_key && self->secret_key && self->tenant_id) ||
2680 (self->access_key && self->secret_key && self->tenant_name))) {
2681 device_set_error(d_self,
2682 g_strdup(_("Missing authorization properties")),
2683 DEVICE_STATUS_DEVICE_ERROR);
2684 return FALSE;
2685 }
2686 } else if (self->s3_api == S3_API_SWIFT_3) {
2687 if (!(self->username && self->password)) {
2688 // self->project_name & self->domain_name have default value
2689 device_set_error(d_self,
2690 g_strdup(_("Missing authorization properties")),
2691 DEVICE_STATUS_DEVICE_ERROR);
2692 return FALSE;
2693 }
2694 } else if (self->s3_api == S3_API_OAUTH2) {
2695 if (self->client_id == NULL ||
2696 self->client_id[0] == '\0') {
2697 device_set_error(d_self,
2698 g_strdup(_("Missing client_id properties")),
2699 DEVICE_STATUS_DEVICE_ERROR);
2700 return FALSE;
2701 }
2702 if (self->client_secret == NULL ||
2703 self->client_secret[0] == '\0') {
2704 device_set_error(d_self,
2705 g_strdup(_("Missing client_secret properties")),
2706 DEVICE_STATUS_DEVICE_ERROR);
2707 return FALSE;
2708 }
2709 if (self->refresh_token == NULL ||
2710 self->refresh_token[0] == '\0') {
2711 device_set_error(d_self,
2712 g_strdup(_("Missing refresh_token properties")),
2713 DEVICE_STATUS_DEVICE_ERROR);
2714 return FALSE;
2715 }
2716 if (self->project_id == NULL ||
2717 self->project_id[0] == '\0') {
2718 device_set_error(d_self,
2719 g_strdup(_("Missing project_id properties")),
2720 DEVICE_STATUS_DEVICE_ERROR);
2721 return FALSE;
2722 }
2723 } else if (self->s3_api == S3_API_CASTOR) {
2724 self->use_s3_multi_delete = 0;
2725 self->use_subdomain = FALSE;
2726 if(self->service_path) {
2727 g_free(self->service_path);
2728 self->service_path = NULL;
2729 }
2730 }
2731
2732 self->s3t = g_new0(S3_by_thread, self->nb_threads);
2733 if (self->s3t == NULL) {
2734 device_set_error(d_self,
2735 g_strdup(_("Can't allocate S3Handle array")),
2736 DEVICE_STATUS_DEVICE_ERROR);
2737 return FALSE;
2738 }
2739
2740 self->thread_idle_cond = g_cond_new();
2741 self->thread_idle_mutex = g_mutex_new();
2742
2743 for (thread = 0; thread < self->nb_threads; thread++) {
2744 self->s3t[thread].idle = 1;
2745 self->s3t[thread].done = 1;
2746 self->s3t[thread].eof = FALSE;
2747 self->s3t[thread].errflags = DEVICE_STATUS_SUCCESS;
2748 self->s3t[thread].errmsg = NULL;
2749 self->s3t[thread].filename = NULL;
2750 self->s3t[thread].curl_buffer.buffer = NULL;
2751 self->s3t[thread].curl_buffer.buffer_len = 0;
2752 self->s3t[thread].timeout = 0;
2753 self->s3t[thread].now_mutex = g_mutex_new();
2754 self->s3t[thread].s3 = s3_open(self->access_key, self->secret_key,
2755 self->session_token,
2756 self->swift_account_id,
2757 self->swift_access_key,
2758 self->host, self->service_path,
2759 self->use_subdomain,
2760 self->user_token, self->bucket_location,
2761 self->storage_class, self->ca_info,
2762 self->server_side_encryption,
2763 self->proxy,
2764 self->s3_api,
2765 self->username,
2766 self->password,
2767 self->tenant_id,
2768 self->tenant_name,
2769 self->project_name,
2770 self->domain_name,
2771 self->client_id,
2772 self->client_secret,
2773 self->refresh_token,
2774 self->reuse_connection,
2775 self->read_from_glacier,
2776 self->timeout,
2777 self->reps, self->reps_bucket);
2778 if (self->s3t[thread].s3 == NULL) {
2779 device_set_error(d_self,
2780 g_strdup(_("Internal error creating S3 handle")),
2781 DEVICE_STATUS_DEVICE_ERROR);
2782 self->nb_threads = thread+1;
2783 return FALSE;
2784 }
2785 }
2786
2787 g_debug("Create %d threads", self->nb_threads);
2788 self->thread_pool_delete = g_thread_pool_new(s3_thread_delete_block,
2789 self, self->nb_threads, 0,
2790 NULL);
2791 self->thread_pool_write = g_thread_pool_new(s3_thread_write_block, self,
2792 self->nb_threads, 0, NULL);
2793 self->thread_pool_read = g_thread_pool_new(s3_thread_read_block, self,
2794 self->nb_threads, 0, NULL);
2795
2796 for (thread = 0; thread < self->nb_threads; thread++) {
2797 s3_verbose(self->s3t[thread].s3, self->verbose);
2798
2799 if (!s3_use_ssl(self->s3t[thread].s3, self->use_ssl)) {
2800 device_set_error(d_self, g_strdup_printf(_(
2801 "Error setting S3 SSL/TLS use "
2802 "(tried to enable SSL/TLS for S3, but curl doesn't support it?)")),
2803 DEVICE_STATUS_DEVICE_ERROR);
2804 return FALSE;
2805 }
2806
2807 if (self->max_send_speed && self->max_send_speed < 5120) {
2808 device_set_error(d_self,
2809 g_strdup("MAX-SEND-SPEED property is too low (minimum value is 5120)"),
2810 DEVICE_STATUS_DEVICE_ERROR);
2811 return FALSE;
2812 }
2813 if (self->max_send_speed &&
2814 !s3_set_max_send_speed(self->s3t[thread].s3,
2815 self->max_send_speed)) {
2816 device_set_error(d_self,
2817 g_strdup("Could not set S3 maximum send speed"),
2818 DEVICE_STATUS_DEVICE_ERROR);
2819 return FALSE;
2820 }
2821
2822 if (self->max_recv_speed && self->max_recv_speed < 5120) {
2823 device_set_error(d_self,
2824 g_strdup("MAX-RECV-SPEED property is too low (minimum value is 5120)"),
2825 DEVICE_STATUS_DEVICE_ERROR);
2826 return FALSE;
2827 }
2828 if (self->max_recv_speed &&
2829 !s3_set_max_recv_speed(self->s3t[thread].s3,
2830 self->max_recv_speed)) {
2831 device_set_error(d_self,
2832 g_strdup("Could not set S3 maximum recv speed"),
2833 DEVICE_STATUS_DEVICE_ERROR);
2834 return FALSE;
2835 }
2836 }
2837
2838 for (thread = 0; thread < self->nb_threads; thread++) {
2839 if (!s3_open2(self->s3t[thread].s3)) {
2840 if (self->s3_api == S3_API_SWIFT_1 ||
2841 self->s3_api == S3_API_SWIFT_2 ||
2842 self->s3_api == S3_API_SWIFT_3) {
2843 s3_error(self->s3t[0].s3, NULL, &response_code,
2844 &s3_error_code, NULL, &curl_code, NULL);
2845 device_set_error(d_self,
2846 g_strdup_printf(_("s3_open2 failed: %s"),
2847 s3_strerror(self->s3t[0].s3)),
2848 DEVICE_STATUS_DEVICE_ERROR);
2849 self->nb_threads = thread+1;
2850 return FALSE;
2851 } else {
2852 device_set_error(d_self,
2853 g_strdup("s3_open2 failed"),
2854 DEVICE_STATUS_DEVICE_ERROR);
2855 return FALSE;
2856 }
2857 }
2858 }
2859 }
2860
2861 return TRUE;
2862 }
2863
2864 static gboolean
2865 make_bucket(
2866 Device * pself)
2867 {
2868 S3Device *self = S3_DEVICE(pself);
2869 guint response_code;
2870 s3_error_code_t s3_error_code;
2871 CURLcode curl_code;
2872
2873 if (self->bucket_made) {
2874 return TRUE;
2875 }
2876
2877 if (s3_is_bucket_exists(self->s3t[0].s3, self->bucket, self->prefix, self->project_id)) {
2878 self->bucket_made = TRUE;
2879 abort_partial_upload(self);
2880 return TRUE;
2881 }
2882
2883 s3_error(self->s3t[0].s3, NULL, &response_code, &s3_error_code, NULL, &curl_code, NULL);
2884
2885 if (response_code == 0 && s3_error_code == 0 &&
2886 (curl_code == CURLE_COULDNT_CONNECT ||
2887 curl_code == CURLE_COULDNT_RESOLVE_HOST)) {
2888 device_set_error(pself,
2889 g_strdup_printf(_("While connecting to %s bucket: %s"),
2890 S3_name[self->s3_api], s3_strerror(self->s3t[0].s3)),
2891 DEVICE_STATUS_DEVICE_ERROR);
2892 return FALSE;
2893 }
2894
2895 if (!self->create_bucket) {
2896 device_set_error(pself,
2897 g_strdup_printf(_("Can't list bucket: %s"),
2898 s3_strerror(self->s3t[0].s3)),
2899 DEVICE_STATUS_DEVICE_ERROR);
2900 return FALSE;
2901 }
2902
2903 if (!s3_make_bucket(self->s3t[0].s3, self->bucket, self->project_id)) {
2904 s3_error(self->s3t[0].s3, NULL, &response_code, &s3_error_code, NULL, NULL, NULL);
2905
2906 /* if it isn't an expected error (bucket already exists),
2907 * return FALSE */
2908 if (response_code != 409 ||
2909 (s3_error_code != S3_ERROR_BucketAlreadyExists &&
2910 s3_error_code != S3_ERROR_BucketAlreadyOwnedByYou)) {
2911 device_set_error(pself,
2912 g_strdup_printf(_("While creating new S3 bucket: %s"), s3_strerror(self->s3t[0].s3)),
2913 DEVICE_STATUS_DEVICE_ERROR);
2914 return FALSE;
2915 }
2916 }
2917
2918 self->bucket_made = TRUE;
2919 abort_partial_upload(self);
2920 return TRUE;
2921 }
2922
2923 static int progress_func(
2924 void *thread_data,
2925 double dltotal G_GNUC_UNUSED,
2926 double dlnow,
2927 double ultotal G_GNUC_UNUSED,
2928 double ulnow)
2929 {
2930 S3_by_thread *s3t = (S3_by_thread *)thread_data;
2931 time_t now = time(NULL);
2932 int ret = 0;
2933 guint64 lnow;
2934
2935 if (dlnow < 1 && ulnow < 1) {
2936 return 0;
2937 }
2938
2939 g_mutex_lock(s3t->now_mutex);
2940 lnow = dlnow;
2941 if (s3t->dlnow != lnow) {
2942 s3t->dlnow = lnow;
2943 if (s3t->timeout > 0) {
2944 s3t->timeout = now + 300;
2945 }
2946 }
2947 lnow = ulnow;
2948 if (s3t->ulnow != lnow) {
2949 s3t->ulnow = lnow;
2950 if (s3t->timeout > 0) {
2951 s3t->timeout = now + 300;
2952 }
2953 }
2954 if (s3t->timeout > 0 && now > s3t->timeout) {
2955 g_debug("progress_func timeout");
2956 ret = -1;
2957 }
2958 g_mutex_unlock(s3t->now_mutex);
2959
2960 return ret;
2961 }
2962
2963 static DeviceStatusFlags
2964 s3_device_read_label(Device *pself) {
2965 S3Device *self = S3_DEVICE(pself);
2966 char *key;
2967 CurlBuffer buf = {NULL, 0, 0, S3_DEVICE_MAX_BLOCK_SIZE, TRUE, NULL, NULL};
2968 dumpfile_t *amanda_header;
2969 gboolean result;
2970
2971 /* note that this may be called from s3_device_start, when
2972 * self->access_mode is not ACCESS_NULL */
2973
2974 amfree(pself->volume_label);
2975 amfree(pself->volume_time);
2976 dumpfile_free(pself->volume_header);
2977 pself->volume_header = NULL;
2978
2979 if (device_in_error(self)) return pself->status;
2980
2981 if (!setup_handle(self)) {
2982 /* setup_handle already set our error message */
2983 return pself->status;
2984 }
2985 reset_thread(self);
2986
2987 if (self->catalog_label && self->catalog_header) {
2988 char *header_buf;
2989
2990 header_buf = unquote_string(self->catalog_header);
2991 amanda_header = g_new(dumpfile_t, 1);
2992 fh_init(amanda_header);
2993 if (strlen(header_buf) > 0) {
2994 parse_file_header(header_buf, amanda_header, strlen(header_buf));
2995 }
2996
2997 pself->header_block_size = strlen(header_buf);
2998 g_free(header_buf);
2999 pself->volume_header = amanda_header;
3000 } else {
3001 if (!make_bucket(pself)) {
3002 return pself->status;
3003 }
3004
3005 key = special_file_to_key(self, "tapestart", -1);
3006
3007 s3_device_init_seek_file(pself, 0);
3008 result = s3_read(self->s3t[0].s3, self->bucket, key, S3_BUFFER_WRITE_FUNCS,
3009 &buf, NULL, NULL);
3010 g_free(key);
3011 if (!result) {
3012 guint response_code;
3013 s3_error_code_t s3_error_code;
3014 s3_error(self->s3t[0].s3, NULL, &response_code, &s3_error_code,
3015 NULL, NULL, NULL);
3016 g_free(buf.buffer);
3017
3018 /* if it's an expected error (not found), just return FALSE */
3019 if (response_code == 404 &&
3020 (s3_error_code == S3_ERROR_None ||
3021 s3_error_code == S3_ERROR_NotFound ||
3022 s3_error_code == S3_ERROR_Unknown ||
3023 s3_error_code == S3_ERROR_NoSuchKey ||
3024 s3_error_code == S3_ERROR_NoSuchEntity ||
3025 s3_error_code == S3_ERROR_NoSuchBucket)) {
3026 g_debug(_("Amanda header not found while reading tapestart header (this is expected for empty tapes)"));
3027 device_set_error(pself,
3028 g_strdup(_("Amanda header not found -- unlabeled volume?")),
3029 DEVICE_STATUS_DEVICE_ERROR
3030 | DEVICE_STATUS_VOLUME_ERROR
3031 | DEVICE_STATUS_VOLUME_UNLABELED);
3032 return pself->status;
3033 }
3034
3035 /* otherwise, log it and return */
3036 device_set_error(pself,
3037 g_strdup_printf(_("While trying to read tapestart header: %s"),
3038 s3_strerror(self->s3t[0].s3)),
3039 DEVICE_STATUS_DEVICE_ERROR | DEVICE_STATUS_VOLUME_ERROR);
3040 return pself->status;
3041 }
3042
3043 /* handle an empty file gracefully */
3044 if (buf.buffer_len == 0) {
3045 device_set_error(pself, g_strdup(_("Empty header file")),
3046 DEVICE_STATUS_VOLUME_ERROR);
3047 g_free(buf.buffer);
3048 return pself->status;
3049 }
3050
3051 pself->header_block_size = buf.buffer_len;
3052 g_assert(buf.buffer != NULL);
3053 amanda_header = g_new(dumpfile_t, 1);
3054 parse_file_header(buf.buffer, amanda_header, buf.buffer_pos);
3055 pself->volume_header = amanda_header;
3056 g_free(buf.buffer);
3057
3058 if (amanda_header->type != F_TAPESTART) {
3059 device_set_error(pself, g_strdup(_("Invalid amanda header")),
3060 DEVICE_STATUS_VOLUME_ERROR);
3061 return pself->status;
3062 }
3063
3064 if (!self->catalog_label || self->catalog_header) {
3065 size_t header_size = 0;
3066 char *buf;
3067 buf = device_build_amanda_header(DEVICE(self), amanda_header,
3068 &header_size);
3069 catalog_reset(self, buf, amanda_header->name);
3070 g_free(buf);
3071 }
3072 }
3073
3074 pself->volume_label = g_strdup(amanda_header->name);
3075 pself->volume_time = g_strdup(amanda_header->datestamp);
3076 /* pself->volume_header is already set */
3077
3078 device_set_error(pself, NULL, DEVICE_STATUS_SUCCESS);
3079
3080 return pself->status;
3081 }
3082
3083 static gboolean
3084 s3_device_start (Device * pself, DeviceAccessMode mode,
3085 char * label, char * timestamp) {
3086 S3Device * self;
3087 GSList *keys;
3088 guint64 total_size = 0;
3089 gboolean result;
3090
3091 self = S3_DEVICE(pself);
3092
3093 if (device_in_error(self)) return FALSE;
3094
3095 if (!setup_handle(self)) {
3096 /* setup_handle already set our error message */
3097 return FALSE;
3098 }
3099
3100 reset_thread(self);
3101 pself->access_mode = mode;
3102 g_mutex_lock(pself->device_mutex);
3103 pself->in_file = FALSE;
3104 g_mutex_unlock(pself->device_mutex);
3105
3106 /* try creating the bucket, in case it doesn't exist */
3107 if (!make_bucket(pself)) {
3108 return FALSE;
3109 }
3110
3111 /* take care of any dirty work for this mode */
3112 switch (mode) {
3113 case ACCESS_READ:
3114 if (pself->volume_label == NULL && s3_device_read_label(pself) != DEVICE_STATUS_SUCCESS) {
3115 /* s3_device_read_label already set our error message */
3116 return FALSE;
3117 }
3118 break;
3119
3120 case ACCESS_WRITE:
3121 s3_device_set_reuse(pself);
3122 if (!delete_all_files(self)) {
3123 return FALSE;
3124 }
3125
3126 /* write a new amanda header */
3127 if (!write_amanda_header(self, label, timestamp)) {
3128 return FALSE;
3129 }
3130
3131 g_free(pself->volume_label);
3132 pself->volume_label = g_strdup(label);
3133 g_free(pself->volume_time);
3134 pself->volume_time = g_strdup(timestamp);
3135
3136 /* unset the VOLUME_UNLABELED flag, if it was set */
3137 device_set_error(pself, NULL, DEVICE_STATUS_SUCCESS);
3138 break;
3139
3140 case ACCESS_APPEND:
3141 if (pself->volume_label == NULL && s3_device_read_label(pself) != DEVICE_STATUS_SUCCESS) {
3142 /* s3_device_read_label already set our error message */
3143 return FALSE;
3144 } else {
3145 result = s3_list_keys(self->s3t[0].s3, self->bucket, NULL, self->prefix, NULL, &keys, &total_size);
3146 if(!result) {
3147 device_set_error(pself,
3148 g_strdup_printf(_("While listing S3 keys: %s"), s3_strerror(self->s3t[0].s3)),
3149 DEVICE_STATUS_DEVICE_ERROR|DEVICE_STATUS_VOLUME_ERROR);
3150 return FALSE;
3151 } else {
3152 self->volume_bytes = total_size;
3153 }
3154 }
3155 return seek_to_end(self);
3156 break;
3157
3158 case ACCESS_NULL:
3159 g_assert_not_reached();
3160 }
3161
3162 return TRUE;
3163 }
3164
3165 gint gint_cmp(gconstpointer a, gconstpointer b, gpointer data);
3166 gint
3167 gint_cmp(
3168 gconstpointer a,
3169 gconstpointer b,
3170 gpointer data G_GNUC_UNUSED)
3171 {
3172 gint ai = GPOINTER_TO_INT(a);
3173 gint bi = GPOINTER_TO_INT(b);
3174
3175 if (ai < bi)
3176 return -1;
3177 else if (ai > bi)
3178 return 1;
3179 else
3180 return 0;
3181 }
3182
3183 static gboolean
3184 s3_device_finish (
3185 Device * pself)
3186 {
3187 S3Device *self = S3_DEVICE(pself);
3188
3189 reset_thread(self);
3190
3191 /* we're not in a file anymore */
3192 pself->access_mode = ACCESS_NULL;
3193
3194 if (device_in_error(pself)) return FALSE;
3195
3196 return TRUE;
3197 }
3198
3199 /* functions for writing */
3200
3201
3202 static guint64
3203 s3_device_get_bytes_read(
3204 Device * pself)
3205 {
3206 S3Device *self = S3_DEVICE(pself);
3207 int thread;
3208 guint64 dltotal;
3209
3210 g_mutex_unlock(pself->device_mutex);
3211 /* Add per thread */
3212 g_mutex_lock(self->thread_idle_mutex);
3213 dltotal = self->dltotal;
3214 for (thread = 0; thread < self->nb_threads_recovery; thread++) {
3215 g_mutex_lock(self->s3t[thread].now_mutex);
3216 dltotal += self->s3t[thread].dlnow;
3217 g_mutex_unlock(self->s3t[thread].now_mutex);
3218 }
3219 g_mutex_unlock(self->thread_idle_mutex);
3220 g_mutex_lock(pself->device_mutex);
3221
3222 return dltotal;
3223 }
3224
3225
3226 static guint64
3227 s3_device_get_bytes_written(
3228 Device * pself)
3229 {
3230 S3Device *self = S3_DEVICE(pself);
3231 int thread;
3232 guint64 ultotal;
3233
3234 g_mutex_unlock(pself->device_mutex);
3235 /* Add per thread */
3236 g_mutex_lock(self->thread_idle_mutex);
3237 ultotal = self->ultotal;
3238 for (thread = 0; thread < self->nb_threads_backup; thread++) {
3239 g_mutex_lock(self->s3t[thread].now_mutex);
3240 ultotal += self->s3t[thread].ulnow;
3241 g_mutex_unlock(self->s3t[thread].now_mutex);
3242 }
3243 g_mutex_unlock(self->thread_idle_mutex);
3244 g_mutex_lock(pself->device_mutex);
3245
3246 return ultotal;
3247 }
3248
3249
3250 static gboolean
3251 s3_device_start_file (Device *pself, dumpfile_t *jobInfo) {
3252 S3Device *self = S3_DEVICE(pself);
3253 CurlBuffer amanda_header = {NULL, 0, 0, 0, TRUE, NULL, NULL};
3254 gboolean result;
3255 size_t header_size;
3256 char *key;
3257 int thread;
3258
3259 if (device_in_error(self)) return FALSE;
3260
3261 reset_thread(self);
3262 pself->is_eom = FALSE;
3263
3264 /* Set the blocksize to zero, since there's no header to skip (it's stored
3265 * in a distinct file, rather than block zero) */
3266 jobInfo->blocksize = 0;
3267
3268 /* Build the amanda header. */
3269 header_size = 0; /* no minimum size */
3270 amanda_header.buffer = device_build_amanda_header(pself, jobInfo,
3271 &header_size);
3272 if (amanda_header.buffer == NULL) {
3273 device_set_error(pself,
3274 g_strdup(_("Amanda file header won't fit in a single block!")),
3275 DEVICE_STATUS_DEVICE_ERROR);
3276 return FALSE;
3277 }
3278 amanda_header.buffer_len = header_size;
3279
3280 if(check_at_leom(self, header_size))
3281 pself->is_eom = TRUE;
3282
3283 if(check_at_peom(self, header_size)) {
3284 pself->is_eom = TRUE;
3285 device_set_error(pself,
3286 g_strdup(_("No space left on device")),
3287 DEVICE_STATUS_DEVICE_ERROR);
3288 g_free(amanda_header.buffer);
3289 return FALSE;
3290 }
3291
3292 for (thread = 0; thread < self->nb_threads; thread++) {
3293 self->s3t[thread].idle = 1;
3294 self->s3t[thread].ulnow = 0;
3295 }
3296
3297 /* set the file and block numbers correctly */
3298 pself->file = (pself->file > 0)? pself->file+1 : 1;
3299 pself->block = 0;
3300 g_mutex_lock(pself->device_mutex);
3301 pself->in_file = TRUE;
3302 pself->bytes_written = 0;
3303 g_mutex_unlock(pself->device_mutex);
3304 g_mutex_lock(self->thread_idle_mutex);
3305 self->ultotal = 0;
3306 g_mutex_unlock(self->thread_idle_mutex);
3307 /* write it out as a special block (not the 0th) */
3308 key = special_file_to_key(self, "filestart", pself->file);
3309 result = s3_upload(self->s3t[0].s3, self->bucket, key, FALSE,
3310 S3_BUFFER_READ_FUNCS,
3311 &amanda_header, NULL, NULL);
3312 g_free(amanda_header.buffer);
3313 g_free(key);
3314 if (!result) {
3315 device_set_error(pself,
3316 g_strdup_printf(_("While writing filestart header: %s"), s3_strerror(self->s3t[0].s3)),
3317 DEVICE_STATUS_DEVICE_ERROR | DEVICE_STATUS_VOLUME_ERROR);
3318 return FALSE;
3319 }
3320
3321 self->volume_bytes += header_size;
3322
3323 if (self->chunked) {
3324 self->filename = file_to_multi_part_key(self, pself->file);
3325 } else if (self->use_s3_multi_part_upload) {
3326 self->filename = file_to_multi_part_key(self, pself->file);
3327 self->uploadId = g_strdup(s3_initiate_multi_part_upload(self->s3t[0].s3,
3328 self->bucket, self->filename));
3329 self->part_etag = g_tree_new_full(gint_cmp, NULL, NULL, g_free);
3330 }
3331
3332 return TRUE;
3333 }
3334
3335 static DeviceWriteResult
3336 s3_device_write_block (Device * pself, guint size, gpointer data) {
3337 char *filename;
3338 S3Device * self = S3_DEVICE(pself);
3339 int idle_thread = 0;
3340 int thread = -1;
3341 int first_idle = -1;
3342 guint allocate;
3343
3344 g_assert (self != NULL);
3345 g_assert (data != NULL);
3346 if (device_in_error(self)) return WRITE_FAILED;
3347
3348 if(check_at_leom(self, size))
3349 pself->is_eom = TRUE;
3350
3351 if(check_at_peom(self, size)) {
3352 pself->is_eom = TRUE;
3353 device_set_error(pself,
3354 g_strdup(_("No space left on device")),
3355 DEVICE_STATUS_DEVICE_ERROR);
3356 return WRITE_FAILED;
3357 }
3358
3359 if (self->use_s3_multi_part_upload && self->uploadId) {
3360 filename = g_strdup(self->filename);
3361 } else if (self->chunked) {
3362 filename = g_strdup(self->filename);
3363 } else {
3364 filename = file_and_block_to_key(self, pself->file, pself->block);
3365 }
3366
3367 g_mutex_lock(self->thread_idle_mutex);
3368 if (self->chunked) {
3369 thread = 0;
3370 if (pself->block == 0) {
3371 allocate = size*2 + 1;
3372 } else {
3373 CurlBuffer *buf = &self->s3t[thread].curl_buffer;
3374
3375 g_mutex_lock(buf->mutex);
3376 // Check for enough space in the buffer
3377 while (1) {
3378 guint avail;
3379 if (buf->buffer_len > buf->buffer_pos) {
3380 avail = buf->buffer_pos + buf->max_buffer_size - buf->buffer_len;
3381 } else {
3382 avail = buf->buffer_pos - buf->buffer_len;
3383 }
3384 if (avail > size) {
3385 break;
3386 }
3387 g_cond_wait(buf->cond, buf->mutex);
3388 }
3389
3390 // Copy the new data to the buffer
3391 if (buf->buffer_len > buf->buffer_pos) {
3392 guint count_end = buf->max_buffer_size - buf->buffer_len;
3393 guint count_begin;
3394 if (count_end > size)
3395 count_end = size;
3396 memcpy(buf->buffer + buf->buffer_len, data, count_end);
3397 buf->buffer_len += count_end;
3398 count_begin = size - count_end;
3399 if (count_begin > 0) {
3400 memcpy(buf->buffer, data + count_end, count_begin);
3401 buf->buffer_len = count_begin;
3402 }
3403 } else {
3404 memcpy(buf->buffer + buf->buffer_len, data, size);
3405 buf->buffer_len += size;
3406 }
3407
3408 g_cond_broadcast(buf->cond);
3409 g_mutex_unlock(buf->mutex);
3410
3411 pself->block++;
3412 self->volume_bytes += size;
3413 g_mutex_unlock(self->thread_idle_mutex);
3414 return WRITE_SUCCEED;
3415 }
3416 } else {
3417 while (!idle_thread) {
3418 idle_thread = 0;
3419 for (thread = 0; thread < self->nb_threads_backup; thread++) {
3420 if (self->s3t[thread].idle == 1) {
3421 idle_thread++;
3422 /* Check if the thread is in error */
3423 if (self->s3t[thread].errflags != DEVICE_STATUS_SUCCESS) {
3424 device_set_error(pself, (char *)self->s3t[thread].errmsg,
3425 self->s3t[thread].errflags);
3426 self->s3t[thread].errflags = DEVICE_STATUS_SUCCESS;
3427 self->s3t[thread].errmsg = NULL;
3428 g_mutex_unlock(self->thread_idle_mutex);
3429 return WRITE_FAILED;
3430 }
3431 if (first_idle == -1) {
3432 first_idle = thread;
3433 break;
3434 }
3435 }
3436 }
3437 if (!idle_thread) {
3438 g_cond_wait(self->thread_idle_cond, self->thread_idle_mutex);
3439 }
3440 }
3441 thread = first_idle;
3442 allocate = size;
3443 }
3444
3445 if (self->s3t[thread].curl_buffer.buffer &&
3446 self->s3t[thread].curl_buffer.buffer_len < allocate) {
3447 g_free((char *)self->s3t[thread].curl_buffer.buffer);
3448 self->s3t[thread].curl_buffer.buffer = NULL;
3449 self->s3t[thread].curl_buffer.buffer_len = 0;
3450 self->s3t[thread].buffer_len = 0;
3451 }
3452 if (self->s3t[thread].curl_buffer.buffer == NULL) {
3453 self->s3t[thread].curl_buffer.buffer = g_try_malloc(allocate);
3454 if (self->s3t[thread].curl_buffer.buffer == NULL) {
3455 device_set_error(pself, g_strdup("Failed to allocate memory"),
3456 DEVICE_STATUS_DEVICE_ERROR);
3457 g_mutex_unlock(self->thread_idle_mutex);
3458 return WRITE_FAILED;
3459 }
3460 self->s3t[thread].curl_buffer.buffer_len = size;
3461 self->s3t[thread].buffer_len = size;
3462 }
3463 self->s3t[thread].idle = 0;
3464 self->s3t[thread].done = 0;
3465 memcpy((char *)self->s3t[thread].curl_buffer.buffer, data, size);
3466 self->s3t[thread].curl_buffer.buffer_pos = 0;
3467 self->s3t[thread].curl_buffer.buffer_len = size;
3468 self->s3t[thread].curl_buffer.max_buffer_size = allocate;
3469 if (self->chunked) {
3470 self->s3t[thread].curl_buffer.end_of_buffer = FALSE;
3471 self->s3t[thread].curl_buffer.mutex = g_mutex_new();
3472 self->s3t[thread].curl_buffer.cond = g_cond_new();
3473 } else {
3474 self->s3t[thread].curl_buffer.end_of_buffer = TRUE;
3475 self->s3t[thread].curl_buffer.mutex = NULL;
3476 self->s3t[thread].curl_buffer.cond = NULL;
3477 }
3478 self->s3t[thread].filename = filename;
3479 self->s3t[thread].uploadId = g_strdup(self->uploadId);
3480 self->s3t[thread].partNumber = pself->block + 1;
3481 g_mutex_unlock(self->thread_idle_mutex);
3482 g_thread_pool_push(self->thread_pool_write, &self->s3t[thread], NULL);
3483
3484 pself->block++;
3485 self->volume_bytes += size;
3486 return WRITE_SUCCEED;
3487 }
3488
3489 static void
3490 s3_thread_write_block(
3491 gpointer thread_data,
3492 gpointer data)
3493 {
3494 S3_by_thread *s3t = (S3_by_thread *)thread_data;
3495 Device *pself = (Device *)data;
3496 S3Device *self = S3_DEVICE(pself);
3497 gboolean result;
3498 char *etag = NULL;
3499
3500 if (s3t->uploadId) {
3501 g_mutex_lock(s3t->now_mutex);
3502 s3t->timeout = time(NULL) + 300;
3503 g_mutex_unlock(s3t->now_mutex);
3504 result = s3_part_upload(s3t->s3, self->bucket, (char *)s3t->filename,
3505 (char *)s3t->uploadId, s3t->partNumber, &etag,
3506 S3_BUFFER_READ_FUNCS,
3507 (CurlBuffer *)&s3t->curl_buffer,
3508 progress_func, s3t);
3509 g_mutex_lock(s3t->now_mutex);
3510 s3t->timeout = 0;
3511 g_mutex_unlock(s3t->now_mutex);
3512 } else {
3513 g_mutex_lock(s3t->now_mutex);
3514 s3t->timeout = time(NULL) + 300;
3515 g_mutex_unlock(s3t->now_mutex);
3516 result = s3_upload(s3t->s3, self->bucket, (char *)s3t->filename,
3517 self->chunked,
3518 S3_BUFFER_READ_FUNCS,
3519 (CurlBuffer *)&s3t->curl_buffer,
3520 progress_func, s3t);
3521 g_mutex_lock(s3t->now_mutex);
3522 s3t->timeout = 0;
3523 g_mutex_unlock(s3t->now_mutex);
3524 }
3525 g_free((void *)s3t->filename);
3526 g_free((void *)s3t->uploadId);
3527 s3t->filename = NULL;
3528 if (!result) {
3529 s3t->errflags = DEVICE_STATUS_DEVICE_ERROR | DEVICE_STATUS_VOLUME_ERROR;
3530 s3t->errmsg = g_strdup_printf(_("While writing data block to %s: %s"), S3_name[self->s3_api], s3_strerror(s3t->s3));
3531 }
3532 g_mutex_lock(self->thread_idle_mutex);
3533 if (result && self->uploadId && etag) {
3534 g_tree_insert(self->part_etag, GINT_TO_POINTER(s3t->partNumber), etag);
3535 } else {
3536 g_free(etag);
3537 }
3538 s3t->idle = 1;
3539 s3t->done = 1;
3540 if (result)
3541 self->ultotal += s3t->curl_buffer.buffer_len;
3542 s3t->curl_buffer.buffer_len = s3t->buffer_len;
3543 s3t->ulnow = 0;
3544 g_cond_broadcast(self->thread_idle_cond);
3545 g_mutex_unlock(self->thread_idle_mutex);
3546
3547 }
3548
3549 gboolean add_part_etag(gpointer key, gpointer value, gpointer data);
3550 gboolean
3551 add_part_etag(
3552 gpointer key,
3553 gpointer value,
3554 gpointer data)
3555 {
3556 int partnum = GPOINTER_TO_INT(key);
3557 char *etag = (char *)value;
3558 GString *buf = (GString *)data;
3559 g_string_append_printf(buf,
3560 " <Part>\n <PartNumber>%d</PartNumber>\n <ETag>%s</ETag>\n </Part>\n",
3561 partnum, etag);
3562 return FALSE;
3563 }
3564
3565
3566 static gboolean
3567 s3_device_finish_file (Device * pself) {
3568 S3Device *self = S3_DEVICE(pself);
3569
3570 /* Check all threads are done */
3571 int idle_thread = 0;
3572 int thread;
3573
3574 if (!pself->in_file)
3575 return TRUE;
3576
3577 if (self->chunked) {
3578 CurlBuffer *buf = &self->s3t[0].curl_buffer;
3579 g_mutex_lock(buf->mutex);
3580 buf->end_of_buffer = TRUE;
3581 g_cond_broadcast(buf->cond);
3582 g_mutex_unlock(buf->mutex);
3583 }
3584
3585 g_mutex_lock(self->thread_idle_mutex);
3586
3587 while (idle_thread != self->nb_threads) {
3588 idle_thread = 0;
3589 for (thread = 0; thread < self->nb_threads; thread++) {
3590 if (self->s3t[thread].idle == 1) {
3591 idle_thread++;
3592 }
3593 /* check thread status */
3594 if (self->s3t[thread].errflags != DEVICE_STATUS_SUCCESS) {
3595 device_set_error(pself, (char *)self->s3t[thread].errmsg,
3596 self->s3t[thread].errflags);
3597 self->s3t[thread].errflags = DEVICE_STATUS_SUCCESS;
3598 self->s3t[thread].errmsg = NULL;
3599 }
3600 }
3601 if (idle_thread != self->nb_threads) {
3602 g_cond_wait(self->thread_idle_cond, self->thread_idle_mutex);
3603 }
3604 }
3605 self->ultotal = 0;
3606 g_mutex_unlock(self->thread_idle_mutex);
3607 if (self->use_s3_multi_part_upload && self->uploadId) {
3608 CurlBuffer data;
3609 GString *buf = g_string_new("<CompleteMultipartUpload>\n");
3610 g_tree_foreach(self->part_etag, add_part_etag, buf);
3611 g_string_append_printf(buf, "</CompleteMultipartUpload>\n");
3612 data.buffer = buf->str;
3613 data.buffer_len = strlen(buf->str);
3614 data.buffer_pos = 0;
3615 data.max_buffer_size = data.buffer_len;
3616 data.end_of_buffer = FALSE;
3617 data.mutex = NULL;
3618 data.cond = NULL;
3619 s3_complete_multi_part_upload(self->s3t[0].s3,
3620 self->bucket, self->filename, self->uploadId,
3621 S3_BUFFER_READ_FUNCS, &data);
3622
3623 g_tree_destroy(self->part_etag);
3624 self->part_etag = NULL;
3625 g_free(self->filename);
3626 }
3627
3628 amfree(self->uploadId);
3629
3630 if (self->chunked) {
3631 CurlBuffer *buf = &self->s3t[0].curl_buffer;
3632 g_cond_free(buf->cond);
3633 buf->cond = NULL;
3634 g_mutex_free(buf->mutex);
3635 buf->mutex = NULL;
3636 }
3637
3638 /* we're not in a file anymore */
3639 g_mutex_lock(pself->device_mutex);
3640 pself->in_file = FALSE;
3641 pself->bytes_written = 0;;
3642 g_mutex_unlock(pself->device_mutex);
3643
3644 if (pself->status != DEVICE_STATUS_SUCCESS) {
3645 return FALSE;
3646 }
3647
3648 return TRUE;
3649 }
3650
3651 static gboolean
3652 s3_device_recycle_file(Device *pself, guint file) {
3653 S3Device *self = S3_DEVICE(pself);
3654 if (device_in_error(self)) return FALSE;
3655
3656 reset_thread(self);
3657 delete_file(self, file);
3658 s3_wait_thread_delete(self);
3659 return !device_in_error(self);
3660 /* delete_file already set our error message if necessary */
3661 }
3662
3663 static gboolean
3664 s3_device_erase(Device *pself) {
3665 S3Device *self = S3_DEVICE(pself);
3666 char *key = NULL;
3667 const char *errmsg = NULL;
3668 guint response_code;
3669 s3_error_code_t s3_error_code;
3670
3671 if (!setup_handle(self)) {
3672 /* error set by setup_handle */
3673 return FALSE;
3674 }
3675
3676 reset_thread(self);
3677 key = special_file_to_key(self, "tapestart", -1);
3678 if (!s3_delete(self->s3t[0].s3, self->bucket, key)) {
3679 s3_error(self->s3t[0].s3, &errmsg, NULL, NULL, NULL, NULL, NULL);
3680 device_set_error(pself,
3681 g_strdup(errmsg),
3682 DEVICE_STATUS_DEVICE_ERROR);
3683 return FALSE;
3684 }
3685 g_free(key);
3686
3687 dumpfile_free(pself->volume_header);
3688 pself->volume_header = NULL;
3689
3690 if (!delete_all_files(self))
3691 return FALSE;
3692
3693 device_set_error(pself, g_strdup("Unlabeled volume"),
3694 DEVICE_STATUS_VOLUME_UNLABELED);
3695
3696 if (self->create_bucket &&
3697 !s3_delete_bucket(self->s3t[0].s3, self->bucket)) {
3698 s3_error(self->s3t[0].s3, &errmsg, &response_code, &s3_error_code, NULL, NULL, NULL);
3699
3700 /*
3701 * ignore the error if the bucket isn't empty (there may be data from elsewhere)
3702 * or the bucket not existing (already deleted perhaps?)
3703 */
3704 if (!(
3705 (response_code == 409 && s3_error_code == S3_ERROR_BucketNotEmpty) ||
3706 (response_code == 404 && s3_error_code == S3_ERROR_NoSuchBucket))) {
3707
3708 device_set_error(pself,
3709 g_strdup(errmsg),
3710 DEVICE_STATUS_DEVICE_ERROR);
3711 return FALSE;
3712 }
3713 self->bucket_made = FALSE;
3714 }
3715 self->volume_bytes = 0;
3716 catalog_remove(self);
3717
3718 return TRUE;
3719 }
3720
3721 static gboolean
3722 s3_device_set_reuse(
3723 Device *dself)
3724 {
3725 S3Device *self = S3_DEVICE(dself);
3726 GSList *lifecycle = NULL, *life;
3727 lifecycle_rule *rule;
3728 gboolean removed = FALSE;
3729
3730 if (self->transition_to_glacier < 0 && !self->read_from_glacier) {
3731 return TRUE;
3732 }
3733
3734 if (device_in_error(self)) return dself->status;
3735
3736 if (!setup_handle(self)) {
3737 /* setup_handle already set our error message */
3738 return dself->status;
3739 }
3740
3741 reset_thread(self);
3742
3743 s3_get_lifecycle(self->s3t[0].s3, self->bucket, &lifecycle);
3744
3745 /* remove it if it exist */
3746 for (life = lifecycle; life != NULL; life = life->next) {
3747 rule = (lifecycle_rule *)life->data;
3748
3749 if (g_str_equal(rule->id, dself->volume_label)) {
3750 removed = TRUE;
3751 lifecycle = g_slist_delete_link(lifecycle, life);
3752 free_lifecycle_rule(rule);
3753 break;
3754 }
3755 }
3756 if (removed) {
3757 s3_put_lifecycle(self->s3t[0].s3, self->bucket, lifecycle);
3758 }
3759 return TRUE;
3760 }
3761
3762 static gboolean
3763 s3_device_set_no_reuse(
3764 Device *dself,
3765 char *label,
3766 char *datestamp)
3767 {
3768 S3Device *self = S3_DEVICE(dself);
3769 GSList *lifecycle = NULL, *life, *next_life, *prev_life = NULL;
3770 lifecycle_rule *rule;
3771 guint count = 0;
3772 GSList *to_remove = NULL;
3773 char *lifecycle_datestamp = NULL;
3774 time_t t;
3775 struct tm tmp;
3776
3777 if (self->transition_to_glacier < 0) {
3778 return TRUE;
3779 }
3780
3781 if (!label || !datestamp) {
3782 s3_device_read_label(dself);
3783 label = dself->volume_label;
3784 datestamp = dself->volume_time;
3785 }
3786
3787 if (device_in_error(self)) return dself->status;
3788
3789 if (!setup_handle(self)) {
3790 /* setup_handle already set our error message */
3791 return dself->status;
3792 }
3793 reset_thread(self);
3794
3795 s3_get_lifecycle(self->s3t[0].s3, self->bucket, &lifecycle);
3796
3797 /* remove it if it exist */
3798 for (life = lifecycle; life != NULL; life = next_life) {
3799 next_life = life->next;
3800
3801 rule = (lifecycle_rule *)life->data;
3802
3803 if (g_str_equal(rule->id, label)) {
3804 free_lifecycle_rule(rule);
3805 if (prev_life == NULL) {
3806 lifecycle = next_life;
3807 } else {
3808 prev_life->next = next_life;
3809 }
3810 //g_free(life);
3811 } else {
3812 if (!to_remove ||
3813 strcmp(datestamp, lifecycle_datestamp) < 0) {
3814 to_remove = life;
3815 g_free(lifecycle_datestamp);
3816 lifecycle_datestamp = g_strdup(datestamp);
3817 }
3818 prev_life = life;
3819 count++;
3820 }
3821 }
3822
3823 /* remove a lifecycle */
3824 if (count >= 999) {
3825 rule = (lifecycle_rule *)to_remove->data;
3826 free_lifecycle_rule(rule);
3827 lifecycle = g_slist_delete_link(lifecycle, to_remove);
3828 }
3829
3830 /* add it */
3831 rule = g_new0(lifecycle_rule, 1);
3832 rule->id = g_strdup(label);
3833 rule->prefix = g_strdup_printf("%sf", self->prefix);
3834 rule->status = g_strdup("Enabled");
3835 rule->transition = g_new0(lifecycle_action, 1);
3836 rule->transition->days = 0;
3837 t = time(NULL) + ((self->transition_to_glacier+1) * 86400);
3838 if (!gmtime_r(&t, &tmp)) perror("localtime");
3839 rule->transition->date = g_strdup_printf(
3840 "%04d-%02d-%02dT00:00:00.000Z",
3841 1900+tmp.tm_year, tmp.tm_mon+1, tmp.tm_mday);
3842 rule->transition->storage_class = g_strdup("GLACIER");
3843
3844 lifecycle = g_slist_append(lifecycle, rule);
3845 s3_put_lifecycle(self->s3t[0].s3, self->bucket, lifecycle);
3846
3847 return TRUE;
3848 }
3849
3850 /* functions for reading */
3851
3852 static gboolean
3853 s3_device_init_seek_file(
3854 Device *pself,
3855 guint file)
3856 {
3857 S3Device *self = S3_DEVICE(pself);
3858 gboolean result;
3859 GSList *objects;
3860 char *prefix;
3861 const char *errmsg = NULL;
3862
3863 if (!self->read_from_glacier) {
3864 return TRUE;
3865 }
3866
3867 /* get a list of all objects */
3868 if (file == 0) {
3869 prefix = special_file_to_key(self, "tapestart", -1);
3870 } else {
3871 prefix = file_to_prefix(self, file);
3872 }
3873 result = s3_list_keys(self->s3t[0].s3, self->bucket, NULL, prefix, NULL,
3874 &objects, NULL);
3875 g_free(prefix);
3876
3877 if (!result) {
3878 guint response_code;
3879 s3_error_code_t s3_error_code;
3880 s3_error(self->s3t[0].s3, &errmsg, &response_code, &s3_error_code,
3881 NULL, NULL, NULL);
3882
3883 device_set_error(pself,
3884 g_strdup_printf(_("failed to list objects: %s"), errmsg),
3885 DEVICE_STATUS_SUCCESS);
3886
3887 return FALSE;
3888 }
3889
3890 /* for all GLACIER objects */
3891 for (; objects; ) {
3892 s3_object *object = (s3_object *)objects->data;
3893 s3_head_t *head;
3894 objects = g_slist_remove(objects, objects->data);
3895 if (object->storage_class == S3_SC_GLACIER) {
3896 /* HEAD object */
3897 head = s3_head(self->s3t[0].s3, self->bucket, object->key);
3898 if (!head) {
3899 guint response_code;
3900 s3_error_code_t s3_error_code;
3901 s3_error(self->s3t[0].s3, &errmsg, &response_code,
3902 &s3_error_code, NULL, NULL, NULL);
3903
3904 device_set_error(pself,
3905 g_strdup_printf(
3906 _("failed to get head of objects '%s': %s"),
3907 object->key, errmsg),
3908 DEVICE_STATUS_SUCCESS);
3909
3910 return FALSE;
3911 }
3912 /* if it is not restored */
3913 if (!head->x_amz_restore) {
3914 /* init restore */
3915 result = s3_init_restore(self->s3t[0].s3, self->bucket,
3916 object->key);
3917 if (!result) {
3918 guint response_code;
3919 s3_error_code_t s3_error_code;
3920 s3_error(self->s3t[0].s3, &errmsg, &response_code,
3921 &s3_error_code, NULL, NULL, NULL);
3922
3923 device_set_error(pself,
3924 g_strdup_printf(_("failed to list objects: %s"),
3925 errmsg),
3926 DEVICE_STATUS_SUCCESS);
3927
3928 return FALSE;
3929 }
3930 }
3931 free_s3_head(head);
3932 }
3933 free_s3_object(object);
3934 }
3935 return TRUE;
3936
3937 }
3938
3939 static dumpfile_t*
3940 s3_device_seek_file(Device *pself, guint file) {
3941 S3Device *self = S3_DEVICE(pself);
3942 gboolean result;
3943 char *key;
3944 CurlBuffer buf = {NULL, 0, 0, S3_DEVICE_MAX_BLOCK_SIZE, TRUE, NULL, NULL};
3945 dumpfile_t *amanda_header;
3946 const char *errmsg = NULL;
3947 int thread;
3948 GSList *objects;
3949
3950 if (device_in_error(self)) return NULL;
3951
3952 reset_thread(self);
3953
3954 g_mutex_lock(pself->device_mutex);
3955 pself->file = file;
3956 pself->is_eof = FALSE;
3957 pself->block = 0;
3958 pself->in_file = FALSE;
3959 pself->bytes_read = 0;
3960 g_mutex_unlock(pself->device_mutex);
3961 g_mutex_lock(self->thread_idle_mutex);
3962 self->last_byte_read = -1;
3963 self->next_block_to_read = 0;
3964 self->next_byte_to_read = 0;
3965 self->dltotal = 0;
3966 g_mutex_unlock(self->thread_idle_mutex);
3967
3968 s3_device_init_seek_file(pself, file);
3969 /* read it in */
3970 key = special_file_to_key(self, "filestart", pself->file);
3971 result = s3_read(self->s3t[0].s3, self->bucket, key, S3_BUFFER_WRITE_FUNCS,
3972 &buf, NULL, NULL);
3973 g_free(key);
3974
3975 if (!result) {
3976 guint response_code;
3977 s3_error_code_t s3_error_code;
3978 s3_error(self->s3t[0].s3, &errmsg, &response_code, &s3_error_code, NULL, NULL, NULL);
3979
3980 /* if it's an expected error (not found), check what to do. */
3981 if (response_code == 404 &&
3982 (s3_error_code == S3_ERROR_None ||
3983 s3_error_code == S3_ERROR_NotFound ||
3984 s3_error_code == S3_ERROR_NoSuchKey ||
3985 s3_error_code == S3_ERROR_NoSuchEntity)) {
3986 int next_file;
3987 next_file = find_next_file(self, pself->file);
3988 if (next_file > 0) {
3989 /* Note short-circut of dispatcher. */
3990 return s3_device_seek_file(pself, next_file);
3991 } else if (next_file == 0) {
3992 /* No next file. Check if we are one past the end. */
3993 s3_device_init_seek_file(pself, pself->file - 1);
3994 key = special_file_to_key(self, "filestart", pself->file - 1);
3995 result = s3_read(self->s3t[0].s3, self->bucket, key,
3996 S3_BUFFER_WRITE_FUNCS, &buf, NULL, NULL);
3997 g_free(key);
3998 if (result) {
3999 /* pself->file, etc. are already correct */
4000 return make_tapeend_header();
4001 } else {
4002 device_set_error(pself,
4003 g_strdup(_("Attempt to read past tape-end file")),
4004 DEVICE_STATUS_SUCCESS);
4005 return NULL;
4006 }
4007 }
4008 } else {
4009 /* An unexpected error occured finding out if we are the last file. */
4010 device_set_error(pself,
4011 g_strdup(errmsg),
4012 DEVICE_STATUS_DEVICE_ERROR);
4013 return NULL;
4014 }
4015 }
4016
4017 /* and make a dumpfile_t out of it */
4018 g_assert(buf.buffer != NULL);
4019 amanda_header = g_new(dumpfile_t, 1);
4020 fh_init(amanda_header);
4021 parse_file_header(buf.buffer, amanda_header, buf.buffer_pos);
4022 g_free(buf.buffer);
4023
4024 switch (amanda_header->type) {
4025 case F_DUMPFILE:
4026 case F_CONT_DUMPFILE:
4027 case F_SPLIT_DUMPFILE:
4028 break;
4029
4030 default:
4031 device_set_error(pself,
4032 g_strdup(_("Invalid amanda header while reading file header")),
4033 DEVICE_STATUS_VOLUME_ERROR);
4034 g_free(amanda_header);
4035 return NULL;
4036 }
4037
4038 g_free(self->filename);
4039 self->filename = file_to_multi_part_key(self, pself->file);
4040 result = s3_list_keys(self->s3t[0].s3, self->bucket, NULL, self->filename,
4041 NULL, &objects, NULL);
4042
4043 if (objects) { /* multi-part */
4044 s3_object *part = (s3_object *)objects->data;
4045 self->object_size = part->size;
4046 slist_free_full(objects, free_s3_object);
4047 } else {
4048 g_free(self->filename);
4049 self->filename = NULL;
4050 self->object_size = 0;
4051 }
4052
4053 pself->in_file = TRUE;
4054 for (thread = 0; thread < self->nb_threads; thread++) {
4055 self->s3t[thread].idle = 1;
4056 self->s3t[thread].eof = FALSE;
4057 self->s3t[thread].ulnow = 0;
4058 }
4059 g_mutex_lock(pself->device_mutex);
4060 pself->in_file = TRUE;
4061 g_mutex_unlock(pself->device_mutex);
4062 return amanda_header;
4063 }
4064
4065 static gboolean
4066 s3_device_seek_block(Device *pself, guint64 block) {
4067 S3Device * self = S3_DEVICE(pself);
4068 if (device_in_error(pself)) return FALSE;
4069
4070 reset_thread(self);
4071 pself->block = block;
4072 self->last_byte_read = (block * pself->block_size) - 1;
4073 self->next_block_to_read = block;
4074 self->next_byte_to_read = block * pself->block_size;
4075 return TRUE;
4076 }
4077
4078 static void
4079 s3_start_read_ahead(
4080 Device * pself,
4081 int max_block,
4082 int size_req)
4083 {
4084 S3Device * self = S3_DEVICE(pself);
4085 char *key;
4086 int thread;
4087 guint64 range_min = 0;
4088 guint64 range_max = 0;
4089
4090 int allocate = size_req;
4091 if (self->chunked) {
4092 allocate = size_req*2 + 1;
4093 }
4094
4095 /* start a read ahead for each thread */
4096 for (thread = 0; thread < self->nb_threads_recovery; thread++) {
4097 S3_by_thread *s3t = &self->s3t[thread];
4098 if (s3t->idle) {
4099 if (self->filename) {
4100 if (max_block >= 0 &&
4101 self->next_byte_to_read > self->last_byte_read + max_block * size_req)
4102 break;
4103 range_min = self->next_byte_to_read;
4104 if (range_min >= self->object_size) {
4105 break;
4106 }
4107 if (self->chunked && max_block > 0) {
4108 range_max = range_min + max_block * size_req - 1;
4109 } else if (self->chunked && max_block < 0) {
4110 range_max = self->object_size-1;
4111 } else {
4112 range_max = range_min + size_req - 1;
4113 }
4114 if (range_max >= self->object_size) {
4115 range_max = self->object_size-1;
4116 }
4117 key = g_strdup(self->filename);
4118 } else {
4119 if (max_block >= 0 &&
4120 self->next_block_to_read >= (gint64)pself->block + max_block)
4121 break;
4122 key = file_and_block_to_key(self, pself->file,
4123 self->next_block_to_read);
4124 }
4125
4126 s3t->filename = key;
4127 s3t->range_min = range_min;
4128 s3t->range_max = range_max;
4129 s3t->done = 0;
4130 s3t->idle = 0;
4131 s3t->eof = FALSE;
4132 s3t->dlnow = 0;
4133 s3t->ulnow = 0;
4134 s3t->errflags = DEVICE_STATUS_SUCCESS;
4135 if (self->chunked ||
4136 (self->s3t[thread].curl_buffer.buffer &&
4137 (int)self->s3t[thread].curl_buffer.buffer_len < size_req)) {
4138 g_free(self->s3t[thread].curl_buffer.buffer);
4139 self->s3t[thread].curl_buffer.buffer = NULL;
4140 self->s3t[thread].curl_buffer.buffer_len = 0;
4141 self->s3t[thread].buffer_len = 0;
4142 }
4143 if (!self->s3t[thread].curl_buffer.buffer) {
4144 self->s3t[thread].curl_buffer.buffer = g_try_malloc(allocate);
4145 if (self->s3t[thread].curl_buffer.buffer == NULL) {
4146 s3t->done = 1;
4147 s3t->idle = 1;
4148 device_set_error(pself, g_strdup("Failed to allocate memory"),
4149 DEVICE_STATUS_DEVICE_ERROR);
4150 return;
4151 }
4152 self->s3t[thread].curl_buffer.buffer_len = allocate;
4153 self->s3t[thread].buffer_len = allocate;
4154 }
4155 s3t->curl_buffer.buffer_pos = 0;
4156 if (self->chunked) {
4157 self->s3t[thread].curl_buffer.buffer_len = 0;
4158 s3t->curl_buffer.max_buffer_size = allocate;
4159 s3t->curl_buffer.end_of_buffer = FALSE;
4160 s3t->curl_buffer.mutex = g_mutex_new();
4161 s3t->curl_buffer.cond = g_cond_new();
4162 } else {
4163 s3t->curl_buffer.max_buffer_size = S3_DEVICE_MAX_BLOCK_SIZE;
4164 s3t->curl_buffer.end_of_buffer = TRUE;
4165 s3t->curl_buffer.mutex = NULL;
4166 s3t->curl_buffer.cond = NULL;
4167 }
4168 self->next_block_to_read++;
4169 self->next_byte_to_read += size_req;
4170 g_thread_pool_push(self->thread_pool_read, s3t, NULL);
4171 }
4172 }
4173 }
4174
4175 static int
4176 s3_device_read_block (Device * pself, gpointer data, int *size_req, int max_block G_GNUC_UNUSED) {
4177 S3Device * self = S3_DEVICE(pself);
4178 char *key;
4179 int thread;
4180 int done = 0;
4181 int found = 0;
4182 guint64 range_min = 0;
4183 S3_by_thread *s3t = NULL;
4184
4185 g_assert (self != NULL);
4186 if (device_in_error(self)) return -1;
4187
4188 g_mutex_lock(self->thread_idle_mutex);
4189
4190 /* start a read ahead for each thread */
4191 s3_start_read_ahead(pself, max_block, *size_req);
4192 if (device_in_error(self)) {
4193 g_mutex_unlock(self->thread_idle_mutex);
4194 return -1;
4195 }
4196
4197 if (self->chunked) {
4198 CurlBuffer *buf = &self->s3t[0].curl_buffer;
4199 guint avail;
4200 guint size = *size_req;
4201
4202 g_mutex_unlock(self->thread_idle_mutex);
4203 g_mutex_lock(buf->mutex);
4204
4205 /* wait for the data to be available */
4206 while (1) {
4207 if (buf->buffer_len == buf->buffer_pos) {
4208 avail = 0;
4209 } else if (buf->buffer_len > buf->buffer_pos) {
4210 avail = buf->buffer_len - buf->buffer_pos;
4211 } else {
4212 avail = buf->max_buffer_size - buf->buffer_pos + buf->buffer_len;
4213 }
4214
4215 if (avail > size || buf->end_of_buffer)
4216 break;
4217
4218 g_cond_wait(buf->cond, buf->mutex);
4219 }
4220
4221 if (size > avail)
4222 size = avail;
4223 if (size > 0) {
4224 if (buf->buffer_len > buf->buffer_pos) {
4225 memcpy((char *)data, buf->buffer + buf->buffer_pos, size);
4226 buf->buffer_pos += size;
4227 } else {
4228 guint count_end = buf->max_buffer_size - buf->buffer_pos;
4229 guint count_begin;
4230
4231 if (count_end > size)
4232 count_end = size;
4233 memcpy((char *)data, buf->buffer + buf->buffer_pos, count_end);
4234 buf->buffer_pos += count_end;
4235 count_begin = size - count_end;
4236 if (count_begin > 0) {
4237 memcpy((char *)data + count_end, buf->buffer, count_begin);
4238 buf->buffer_pos = count_begin;
4239 }
4240 }
4241 }
4242 /* signal to add more data to the buffer */
4243 g_cond_broadcast(buf->cond);
4244 g_mutex_unlock(buf->mutex);
4245
4246 if (size == 0 && buf->end_of_buffer) {
4247 pself->is_eof = TRUE;
4248 pself->in_file = FALSE;
4249 device_set_error(pself, g_strdup(_("EOF")), DEVICE_STATUS_SUCCESS);
4250 return -1;
4251 }
4252 *size_req = size;
4253 pself->block++;
4254
4255 /* disable read_ahead thread if the complete chunk is read */
4256 if (self->chunked && max_block == 1) {
4257 S3_by_thread *s3t = &self->s3t[0];
4258 s3t->idle = TRUE;
4259 s3t->curl_buffer.end_of_buffer = FALSE;
4260 }
4261 return size;
4262 }
4263
4264 /* get the file*/
4265 if (self->filename) {
4266 key = g_strdup(self->filename);
4267 range_min = self->last_byte_read + 1;
4268 } else {
4269 key = file_and_block_to_key(self, pself->file, pself->block);
4270 }
4271 g_assert(key != NULL);
4272
4273 found = 0;
4274 /* find which thread read the key */
4275 for (thread = 0; thread < self->nb_threads_recovery; thread++) {
4276 s3t = &self->s3t[thread];
4277 if (!s3t->idle &&
4278 g_str_equal(key, (char *)s3t->filename) &&
4279 range_min == s3t->range_min) {
4280 found = 1;
4281 break;
4282 }
4283 }
4284
4285 if (!found) {
4286 /* return eof */
4287 g_free(key);
4288 pself->is_eof = TRUE;
4289 pself->in_file = FALSE;
4290 device_set_error(pself, g_strdup(_("EOF")), DEVICE_STATUS_SUCCESS);
4291 g_mutex_unlock(self->thread_idle_mutex);
4292 return -1;
4293 }
4294
4295 while (!done) {
4296 if (!s3t->done) {
4297 } else if (s3t->eof) {
4298 /* return eof */
4299 g_free(key);
4300 pself->is_eof = TRUE;
4301 pself->in_file = FALSE;
4302 device_set_error(pself, g_strdup(_("EOF")), DEVICE_STATUS_SUCCESS);
4303 g_mutex_unlock(self->thread_idle_mutex);
4304 return -1;
4305 } else if (s3t->errflags != DEVICE_STATUS_SUCCESS) {
4306 /* return the error */
4307 device_set_error(pself, (char *)s3t->errmsg, s3t->errflags);
4308 g_free(key);
4309 g_mutex_unlock(self->thread_idle_mutex);
4310 return -1;
4311 } else if ((guint)*size_req >= s3t->curl_buffer.buffer_pos) {
4312 /* return the buffer */
4313 g_mutex_unlock(self->thread_idle_mutex);
4314 memcpy(data, s3t->curl_buffer.buffer, s3t->curl_buffer.buffer_pos);
4315 *size_req = s3t->curl_buffer.buffer_pos;
4316 g_free(key);
4317 s3t->idle = 1;
4318 g_free((char *)s3t->filename);
4319 pself->block++;
4320 self->last_byte_read += *size_req;
4321 done = 1;
4322 g_mutex_lock(self->thread_idle_mutex);
4323 break;
4324 } else { /* buffer not enough large */
4325 *size_req = s3t->curl_buffer.buffer_len;
4326 g_free(key);
4327 g_mutex_unlock(self->thread_idle_mutex);
4328 return 0;
4329 }
4330
4331 if (!done) {
4332 g_cond_wait(self->thread_idle_cond, self->thread_idle_mutex);
4333 }
4334 }
4335
4336 /* start a read ahead for each thread */
4337 s3_start_read_ahead(pself, max_block-1, *size_req);
4338
4339 g_mutex_unlock(self->thread_idle_mutex);
4340
4341 return *size_req;
4342
4343 }
4344
4345 static void
4346 s3_thread_read_block(
4347 gpointer thread_data,
4348 gpointer data)
4349 {
4350 S3_by_thread *s3t = (S3_by_thread *)thread_data;
4351 Device *pself = (Device *)data;
4352 S3Device *self = S3_DEVICE(pself);
4353 gboolean result;
4354
4355 if (s3t->range_max > 0) {
4356 g_mutex_lock(s3t->now_mutex);
4357 s3t->timeout = time(NULL) + 300;
4358 g_mutex_unlock(s3t->now_mutex);
4359 result = s3_read_range(s3t->s3, self->bucket, (char *)s3t->filename,
4360 s3t->range_min, s3t->range_max,
4361 s3_buffer_write_func, s3_buffer_reset_func,
4362 (CurlBuffer *)&s3t->curl_buffer,
4363 progress_func, s3t);
4364 g_mutex_lock(s3t->now_mutex);
4365 s3t->timeout = 0;
4366 g_mutex_unlock(s3t->now_mutex);
4367 } else {
4368 g_mutex_lock(s3t->now_mutex);
4369 s3t->timeout = time(NULL) + 300;
4370 g_mutex_unlock(s3t->now_mutex);
4371 result = s3_read(s3t->s3, self->bucket, (char *)s3t->filename,
4372 s3_buffer_write_func, s3_buffer_reset_func,
4373 (CurlBuffer *)&s3t->curl_buffer,
4374 progress_func, s3t);
4375 g_mutex_lock(s3t->now_mutex);
4376 s3t->timeout = 0;
4377 g_mutex_unlock(s3t->now_mutex);
4378 }
4379
4380 if (s3t->curl_buffer.mutex) {
4381 g_mutex_lock(s3t->curl_buffer.mutex);
4382 s3t->curl_buffer.end_of_buffer = TRUE;
4383 g_cond_broadcast(s3t->curl_buffer.cond);
4384 g_mutex_unlock(s3t->curl_buffer.mutex);
4385 }
4386 g_mutex_lock(self->thread_idle_mutex);
4387 if (!result) {
4388 guint response_code;
4389 s3_error_code_t s3_error_code;
4390 s3_error(s3t->s3, NULL, &response_code, &s3_error_code, NULL, NULL, NULL);
4391 /* if it's an expected error (not found), just return -1 */
4392 if ((response_code == 404 &&
4393 (s3_error_code == S3_ERROR_None ||
4394 s3_error_code == S3_ERROR_NotFound ||
4395 s3_error_code == S3_ERROR_Unknown ||
4396 s3_error_code == S3_ERROR_NoSuchKey ||
4397 s3_error_code == S3_ERROR_NoSuchEntity)) ||
4398 (response_code == 416 &&
4399 s3_error_code == S3_ERROR_InvalidRange)) {
4400 s3t->eof = TRUE;
4401 } else {
4402
4403 /* otherwise, log it and return FALSE */
4404 s3t->errflags = DEVICE_STATUS_VOLUME_ERROR;
4405 s3t->errmsg = g_strdup_printf(_("While reading data block from S3: %s"),
4406 s3_strerror(s3t->s3));
4407 }
4408 s3t->eof = TRUE;
4409 } else {
4410 self->dltotal += s3t->curl_buffer.buffer_pos;
4411 }
4412 s3t->dlnow = 0;
4413 s3t->ulnow = 0;
4414 s3t->done = 1;
4415 g_cond_broadcast(self->thread_idle_cond);
4416 g_mutex_unlock(self->thread_idle_mutex);
4417
4418 return;
4419 }
4420
4421 static gboolean
4422 check_at_peom(S3Device *self, guint64 size)
4423 {
4424 if(self->enforce_volume_limit && (self->volume_limit > 0)) {
4425 guint64 newtotal = self->volume_bytes + size;
4426 if(newtotal > self->volume_limit) {
4427 return TRUE;
4428 }
4429 }
4430 return FALSE;
4431 }
4432
4433 static gboolean
4434 check_at_leom(S3Device *self, guint64 size)
4435 {
4436 guint64 block_size = DEVICE(self)->block_size;
4437 guint64 eom_warning_buffer = block_size *
4438 (EOM_EARLY_WARNING_ZONE_BLOCKS + self->nb_threads);
4439
4440 if(!self->leom)
4441 return FALSE;
4442
4443 if(self->enforce_volume_limit && (self->volume_limit > 0)) {
4444 guint64 newtotal = self->volume_bytes + size + eom_warning_buffer;
4445 if(newtotal > self->volume_limit) {
4446 return TRUE;
4447 }
4448 }
4449 return FALSE;
4450 }
4451
4452 static void
4453 reset_thread(
4454 S3Device *self)
4455 {
4456 int thread;
4457 int nb_done = 0;
4458
4459 if (self->thread_idle_mutex) {
4460 g_mutex_lock(self->thread_idle_mutex);
4461 while(nb_done != self->nb_threads) {
4462 nb_done = 0;
4463 for (thread = 0; thread < self->nb_threads; thread++) {
4464 if (self->s3t[thread].done == 1)
4465 nb_done++;
4466 }
4467 if (nb_done != self->nb_threads) {
4468 g_cond_wait(self->thread_idle_cond, self->thread_idle_mutex);
4469 }
4470 }
4471 g_mutex_unlock(self->thread_idle_mutex);
4472 }
4473 }