| 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 |
#ifndef __S3_H__ |
| 24 |
#define __S3_H__ |
| 25 |
#include <glib.h> |
| 26 |
#include <curl/curl.h> |
| 27 |
|
| 28 |
/* |
| 29 |
* Data types |
| 30 |
*/ |
| 31 |
|
| 32 |
typedef enum { |
| 33 |
S3_API_UNKNOWN, |
| 34 |
S3_API_S3, |
| 35 |
S3_API_SWIFT_1, |
| 36 |
S3_API_SWIFT_2, |
| 37 |
S3_API_SWIFT_3, |
| 38 |
S3_API_OAUTH2, |
| 39 |
S3_API_CASTOR, |
| 40 |
S3_API_AWS4 |
| 41 |
} S3_api; |
| 42 |
|
| 43 |
extern char *S3_name[]; |
| 44 |
extern char *S3_bucket_name[]; |
| 45 |
|
| 46 |
typedef enum { |
| 47 |
S3_SC_STANDARD, |
| 48 |
S3_SC_STANDARD_IA, |
| 49 |
S3_SC_REDUCED_REDUNDANCY, |
| 50 |
S3_SC_GLACIER |
| 51 |
} StorageClass; |
| 52 |
|
| 53 |
/* An opaque handle. S3Handles should only be accessed from a single |
| 54 |
* thread at any given time, although it is fine to use different handles |
| 55 |
* in different threads simultaneously. */ |
| 56 |
typedef struct S3Handle S3Handle; |
| 57 |
|
| 58 |
/* Callback function to read data to upload |
| 59 |
* |
| 60 |
* @note this is the same as CURLOPT_READFUNCTION |
| 61 |
* |
| 62 |
* @param data: The pointer to write data to |
| 63 |
* @param size: The size of each "element" of the data buffer in bytes |
| 64 |
* @param nmemb: The number of elements in the data buffer. |
| 65 |
* So, the buffer's size is size*nmemb bytes. |
| 66 |
* @param stream: The read_data (an opaque pointer) |
| 67 |
* |
| 68 |
* @return The number of bytes written to the buffer, |
| 69 |
* CURL_READFUNC_PAUSE to pause, or CURL_READFUNC_ABORT to abort. |
| 70 |
* Return 0 only if there's no more data to be uploaded. |
| 71 |
*/ |
| 72 |
typedef size_t (*s3_read_func)(void *data, size_t size, size_t nmemb, void *stream); |
| 73 |
|
| 74 |
/* This function is called to get size of the upload data |
| 75 |
* |
| 76 |
* @param data: The write_data (opaque pointer) |
| 77 |
* |
| 78 |
* @return The number of bytes of data, negative for error |
| 79 |
*/ |
| 80 |
typedef size_t (*s3_size_func)(void *data); |
| 81 |
|
| 82 |
/* This function is called to get MD5 hash of the upload data |
| 83 |
* |
| 84 |
* @param data: The write_data (opaque pointer) |
| 85 |
* |
| 86 |
* @return The MD5 hash, NULL on error |
| 87 |
*/ |
| 88 |
typedef GByteArray* (*s3_md5_func)(void *data); |
| 89 |
|
| 90 |
/* This function is called to reset an upload or download data stream |
| 91 |
* to the beginning |
| 92 |
* |
| 93 |
* @param data: The read_data or write_data (opaque pointer) |
| 94 |
* |
| 95 |
* @return The number of bytes of data, negative for error |
| 96 |
*/ |
| 97 |
typedef void (*s3_reset_func)(void *data); |
| 98 |
|
| 99 |
/* Callback function to write data that's been downloaded |
| 100 |
* |
| 101 |
* @note this is the same as CURLOPT_WRITEFUNCTION |
| 102 |
* |
| 103 |
* @param data: The pointer to read data from |
| 104 |
* @param size: The size of each "element" of the data buffer in bytes |
| 105 |
* @param nmemb: The number of elements in the data buffer. |
| 106 |
* So, the buffer's size is size*nmemb bytes. |
| 107 |
* @param stream: the write_data (an opaque pointer) |
| 108 |
* |
| 109 |
* @return The number of bytes written to the buffer or |
| 110 |
* CURL_WRITEFUNC_PAUSE to pause. |
| 111 |
* If it's the number of bytes written, it should match the buffer size |
| 112 |
*/ |
| 113 |
typedef size_t (*s3_write_func)(void *data, size_t size, size_t nmemb, void *stream); |
| 114 |
|
| 115 |
/** |
| 116 |
* Callback function to track progress |
| 117 |
* |
| 118 |
* @note this is the same as CURLOPT_PROGRESSFUNCTION |
| 119 |
* |
| 120 |
* @param data: The progress_data |
| 121 |
* @param dltotal: The total number of bytes to downloaded |
| 122 |
* @param dlnow: The current number of bytes downloaded |
| 123 |
* @param ultotal: The total number of bytes to downloaded |
| 124 |
* @param ulnow: The current number of bytes downloaded |
| 125 |
* |
| 126 |
* @return 0 to continue, non-zero to abort. |
| 127 |
*/ |
| 128 |
typedef curl_progress_callback s3_progress_func; |
| 129 |
|
| 130 |
/* |
| 131 |
* Constants |
| 132 |
*/ |
| 133 |
|
| 134 |
/* These are assumed to be already URL-escaped. */ |
| 135 |
# define STS_BASE_URL "https://ls.amazonaws.com/" |
| 136 |
# define STS_PRODUCT_TOKEN "{ProductToken}AAAGQXBwVGtu4geoGybuwuk8VEEPzJ9ZANpu0yzbf9g4Gs5Iarzff9B7qaDBEEaWcAzWpcN7zmdMO765jOtEFc4DWTRNkpPSzUnTdkHbdYUamath73OreaZtB86jy/JF0gsHZfhxeKc/3aLr8HNT//DsX3r272zYHLDPWWUbFguOwqNjllnt6BshYREx59l8RrWABLSa37dyJeN+faGvz3uQxiDakZRn3LfInOE6d9+fTFl50LPoP08LCqI/SJfpouzWix7D/cep3Jq8yYNyM1rgAOTF7/wh7r8OuPDLJ/xZUDLfykePIAM=" |
| 137 |
|
| 138 |
/* This preprocessor magic will enumerate constants named S3_ERROR_XxxYyy for |
| 139 |
* each of the errors in parentheses. |
| 140 |
* |
| 141 |
* see http://docs.amazonwebservices.com/AmazonS3/latest/API/ErrorResponses.html |
| 142 |
* for Amazon's breakdown of error responses. |
| 143 |
*/ |
| 144 |
#define S3_ERROR_LIST \ |
| 145 |
S3_ERROR(None), \ |
| 146 |
S3_ERROR(Accepted), \ |
| 147 |
S3_ERROR(AccessDenied), \ |
| 148 |
S3_ERROR(AccountProblem), \ |
| 149 |
S3_ERROR(AllAccessDisabled), \ |
| 150 |
S3_ERROR(AmbiguousGrantByEmailAddress), \ |
| 151 |
S3_ERROR(AuthenticationRequired), \ |
| 152 |
S3_ERROR(BadDigest), \ |
| 153 |
S3_ERROR(BucketAlreadyExists), \ |
| 154 |
S3_ERROR(BucketAlreadyOwnedByYou), \ |
| 155 |
S3_ERROR(BucketNotEmpty), \ |
| 156 |
S3_ERROR(Conflict), \ |
| 157 |
S3_ERROR(Created), \ |
| 158 |
S3_ERROR(CredentialsNotSupported), \ |
| 159 |
S3_ERROR(CrossLocationLoggingProhibited), \ |
| 160 |
S3_ERROR(EntityTooSmall), \ |
| 161 |
S3_ERROR(EntityTooLarge), \ |
| 162 |
S3_ERROR(ExpiredToken), \ |
| 163 |
S3_ERROR(Forbidden), \ |
| 164 |
S3_ERROR(IllegalVersioningConfigurationException), \ |
| 165 |
S3_ERROR(IncompleteBody), \ |
| 166 |
S3_ERROR(IncorrectNumberOfFilesInPostRequest), \ |
| 167 |
S3_ERROR(InlineDataTooLarge), \ |
| 168 |
S3_ERROR(InternalError), \ |
| 169 |
S3_ERROR(InvalidAccessKeyId), \ |
| 170 |
S3_ERROR(InvalidAddressingHeader), \ |
| 171 |
S3_ERROR(InvalidArgument), \ |
| 172 |
S3_ERROR(InvalidBucketName), \ |
| 173 |
S3_ERROR(InvalidBucketState), \ |
| 174 |
S3_ERROR(InvalidDigest), \ |
| 175 |
S3_ERROR(InvalidLocationConstraint), \ |
| 176 |
S3_ERROR(InvalidPart), \ |
| 177 |
S3_ERROR(InvalidPartOrder), \ |
| 178 |
S3_ERROR(InvalidPayer), \ |
| 179 |
S3_ERROR(InvalidPolicyDocument), \ |
| 180 |
S3_ERROR(InvalidObjectState), \ |
| 181 |
S3_ERROR(InvalidRange), \ |
| 182 |
S3_ERROR(InvalidRequest), \ |
| 183 |
S3_ERROR(InvalidSecurity), \ |
| 184 |
S3_ERROR(InvalidSOAPRequest), \ |
| 185 |
S3_ERROR(InvalidStorageClass), \ |
| 186 |
S3_ERROR(InvalidTargetBucketForLogging), \ |
| 187 |
S3_ERROR(InvalidToken), \ |
| 188 |
S3_ERROR(InvalidURI), \ |
| 189 |
S3_ERROR(KeyTooLong), \ |
| 190 |
S3_ERROR(MalformedACLError), \ |
| 191 |
S3_ERROR(MalformedPOSTRequest), \ |
| 192 |
S3_ERROR(MalformedXML), \ |
| 193 |
S3_ERROR(MaxMessageLengthExceeded), \ |
| 194 |
S3_ERROR(MaxPostPreDataLengthExceededError), \ |
| 195 |
S3_ERROR(MetadataTooLarge), \ |
| 196 |
S3_ERROR(MethodNotAllowed), \ |
| 197 |
S3_ERROR(MissingAttachment), \ |
| 198 |
S3_ERROR(MissingContentLength), \ |
| 199 |
S3_ERROR(MissingRequestBodyError), \ |
| 200 |
S3_ERROR(MissingSecurityElement), \ |
| 201 |
S3_ERROR(MissingSecurityHeader), \ |
| 202 |
S3_ERROR(NoLoggingStatusForKey), \ |
| 203 |
S3_ERROR(NoSuchBucket), \ |
| 204 |
S3_ERROR(NoSuchEntity), \ |
| 205 |
S3_ERROR(NoSuchKey), \ |
| 206 |
S3_ERROR(NoSuchLifecycleConfiguration), \ |
| 207 |
S3_ERROR(NoSuchUpload), \ |
| 208 |
S3_ERROR(NoSuchVersion), \ |
| 209 |
S3_ERROR(NotImplemented), \ |
| 210 |
S3_ERROR(NotSignedUp), \ |
| 211 |
S3_ERROR(NotSuchBucketPolicy), \ |
| 212 |
S3_ERROR(OperationAborted), \ |
| 213 |
S3_ERROR(PermanentRedirect), \ |
| 214 |
S3_ERROR(PreconditionFailed), \ |
| 215 |
S3_ERROR(Redirect), \ |
| 216 |
S3_ERROR(RestoreAlreadyInProgress), \ |
| 217 |
S3_ERROR(RequestIsNotMultiPartContent), \ |
| 218 |
S3_ERROR(RequestTimeout), \ |
| 219 |
S3_ERROR(RequestTimeTooSkewed), \ |
| 220 |
S3_ERROR(RequestTorrentOfBucketError), \ |
| 221 |
S3_ERROR(SignatureDoesNotMatch), \ |
| 222 |
S3_ERROR(ServiceUnavailable), \ |
| 223 |
S3_ERROR(SlowDown), \ |
| 224 |
S3_ERROR(TemporaryRedirect), \ |
| 225 |
S3_ERROR(TokenRefreshRequired), \ |
| 226 |
S3_ERROR(TooManyBuckets), \ |
| 227 |
S3_ERROR(Unauthorized), \ |
| 228 |
S3_ERROR(UnexpectedContent), \ |
| 229 |
S3_ERROR(Unknown), \ |
| 230 |
S3_ERROR(UnresolvableGrantByEmailAddress), \ |
| 231 |
S3_ERROR(UserKeyMustBeSpecified), \ |
| 232 |
S3_ERROR(RegionNotFound), \ |
| 233 |
S3_ERROR(NotFound), \ |
| 234 |
S3_ERROR(END) |
| 235 |
|
| 236 |
typedef enum { |
| 237 |
#define S3_ERROR(NAME) S3_ERROR_ ## NAME |
| 238 |
S3_ERROR_LIST |
| 239 |
#undef S3_ERROR |
| 240 |
} s3_error_code_t; |
| 241 |
|
| 242 |
/* |
| 243 |
* Functions |
| 244 |
*/ |
| 245 |
|
| 246 |
/* Does this install of curl support SSL? |
| 247 |
* |
| 248 |
* @returns: boolean |
| 249 |
*/ |
| 250 |
gboolean |
| 251 |
s3_curl_supports_ssl(void); |
| 252 |
|
| 253 |
/* Checks if the version of libcurl being used supports and checks |
| 254 |
* wildcard certificates correctly (used for the subdomains required |
| 255 |
* by location constraints). |
| 256 |
* |
| 257 |
* @returns: true if the version of libcurl is new enough |
| 258 |
*/ |
| 259 |
gboolean |
| 260 |
s3_curl_location_compat(void); |
| 261 |
|
| 262 |
/* Checks if a bucket name is compatible with setting a location |
| 263 |
* constraint. |
| 264 |
* |
| 265 |
* @note This doesn't guarantee that bucket name is entirely valid, |
| 266 |
* just that using it as one (or more) subdomain(s) of s3.amazonaws.com |
| 267 |
* won't fail; that would prevent the reporting of useful messages from |
| 268 |
* the service. |
| 269 |
* |
| 270 |
* @param bucket: the bucket name |
| 271 |
* @returns: true if the bucket name is compatible |
| 272 |
*/ |
| 273 |
gboolean |
| 274 |
s3_bucket_location_compat(const char *bucket); |
| 275 |
|
| 276 |
/* Initialize S3 operation |
| 277 |
* |
| 278 |
* If an error occurs in this function, diagnostic information is |
| 279 |
* printed to stderr. |
| 280 |
* |
| 281 |
* @returns: false if an error occurred |
| 282 |
*/ |
| 283 |
gboolean |
| 284 |
s3_init(void); |
| 285 |
|
| 286 |
/* Set up an S3Handle. |
| 287 |
* |
| 288 |
* The concept of a bucket is defined by the Amazon S3 API. |
| 289 |
* See: "Components of Amazon S3" - API Version 2006-03-01 pg. 8 |
| 290 |
* |
| 291 |
* @param access_key: the secret key for Amazon Web Services |
| 292 |
* @param secret_key: the secret key for Amazon Web Services |
| 293 |
* @param user_token: the user token for Amazon DevPay |
| 294 |
* @param bucket_location: the location constraint for buckets |
| 295 |
* @param storage_class: the storage class for new objects |
| 296 |
* @param ca_info: the path to pass to libcurl as the certificate authority. |
| 297 |
* see curl_easy_setopt() CURLOPT_CAINFO for more |
| 298 |
* @returns: the new S3Handle |
| 299 |
*/ |
| 300 |
S3Handle * |
| 301 |
s3_open(const char * access_key, const char *secret_key, |
| 302 |
const char *session_token, |
| 303 |
const char *swift_account_id, const char *swift_access_key, |
| 304 |
const char *host, |
| 305 |
const char *service_path, gboolean use_subdomain, |
| 306 |
const char * user_token, |
| 307 |
const char * bucket_location, const char * storage_class, |
| 308 |
const char * ca_info, const char * server_side_encryption, |
| 309 |
const char *proxy, |
| 310 |
const S3_api s3_api, |
| 311 |
const char *username, |
| 312 |
const char *password, |
| 313 |
const char *tenant_id, |
| 314 |
const char *tenant_name, |
| 315 |
const char *project_name, |
| 316 |
const char *domain_name, |
| 317 |
const char *client_id, |
| 318 |
const char *client_secret, |
| 319 |
const char *refresh_token, |
| 320 |
const gboolean reuse_connection, |
| 321 |
const gboolean read_from_glacier, |
| 322 |
const long timeout, |
| 323 |
const char *reps, |
| 324 |
const char *reps_bucket); |
| 325 |
|
| 326 |
/* latest step of setting up the S3Handle. |
| 327 |
* |
| 328 |
* Must be done after all properties are set. |
| 329 |
* |
| 330 |
* @param hdl: the S3Handle to set up. |
| 331 |
* @returns: false if an error occured |
| 332 |
*/ |
| 333 |
gboolean |
| 334 |
s3_open2(S3Handle *hdl); |
| 335 |
|
| 336 |
/* Deallocate an S3Handle |
| 337 |
* |
| 338 |
* @param hdl: the S3Handle object |
| 339 |
*/ |
| 340 |
void |
| 341 |
s3_free(S3Handle *hdl); |
| 342 |
|
| 343 |
/* Reset the information about the last request, including |
| 344 |
* freeing any allocated memory. The S3Handle itself is not |
| 345 |
* freed and may be used again. This function is called |
| 346 |
* automatically as needed, and should be called to free memory |
| 347 |
* when the handle will not be used for some time. |
| 348 |
* |
| 349 |
* @param hdl: the S3Handle object |
| 350 |
*/ |
| 351 |
void |
| 352 |
s3_reset(S3Handle *hdl); |
| 353 |
|
| 354 |
/* Get the error information for the last operation |
| 355 |
* |
| 356 |
* All results are returned via result parameters. If any parameter is |
| 357 |
* NULL, that result will not be returned. Caller is not responsible for |
| 358 |
* freeing any returned strings, although the results are only valid until |
| 359 |
* the next call to an S3 function with this handle. |
| 360 |
* |
| 361 |
* @param hdl: the S3Handle object |
| 362 |
* @param message: (result) the error message, or NULL if none exists |
| 363 |
* @param response_code: (result) the HTTP response code (or 0 if none exists) |
| 364 |
* @param s3_error_code: (result) the S3 error code (see constants, above) |
| 365 |
* @param s3_error_name: (result) the S3 error name (e.g., "RequestTimeout"), |
| 366 |
* or NULL if none exists |
| 367 |
* @param curl_code: (result) the curl error code (or 0 if none exists) |
| 368 |
* @param num_retries: (result) number of retries |
| 369 |
*/ |
| 370 |
void |
| 371 |
s3_error(S3Handle *hdl, |
| 372 |
const char **message, |
| 373 |
guint *response_code, |
| 374 |
s3_error_code_t *s3_error_code, |
| 375 |
const char **s3_error_name, |
| 376 |
CURLcode *curl_code, |
| 377 |
guint *num_retries); |
| 378 |
|
| 379 |
/* Control verbose output of HTTP transactions, etc. |
| 380 |
* |
| 381 |
* @param hdl: the S3Handle object |
| 382 |
* @param verbose: if true, send HTTP transactions, etc. to debug output |
| 383 |
*/ |
| 384 |
void |
| 385 |
s3_verbose(S3Handle *hdl, |
| 386 |
gboolean verbose); |
| 387 |
|
| 388 |
/* Control the use of SSL with HTTP transactions. |
| 389 |
* |
| 390 |
* @param hdl: the S3Handle object |
| 391 |
* @param use_ssl: if true, use SSL (if curl supports it) |
| 392 |
* @returns: true if the setting is valid |
| 393 |
*/ |
| 394 |
gboolean |
| 395 |
s3_use_ssl(S3Handle *hdl, gboolean use_ssl); |
| 396 |
|
| 397 |
/* Control the throttling of S3 uploads. Only supported with curl >= 7.15.5. |
| 398 |
* |
| 399 |
* @param hdl: the S3Handle object |
| 400 |
* @param max_send_speed: max speed (bytes/sec) at which to send |
| 401 |
* @returns: true if the setting is valid |
| 402 |
*/ |
| 403 |
gboolean |
| 404 |
s3_set_max_send_speed(S3Handle *hdl, guint64 max_send_speed); |
| 405 |
|
| 406 |
/* Control the throttling of S3 downloads. Only supported with curl >= 7.15.5. |
| 407 |
* |
| 408 |
* @param hdl: the S3Handle object |
| 409 |
* @param max_recv_speed: max speed (bytes/sec) at which to receive |
| 410 |
* @returns: true if the setting is valid |
| 411 |
*/ |
| 412 |
gboolean |
| 413 |
s3_set_max_recv_speed(S3Handle *hdl, guint64 max_recv_speed); |
| 414 |
|
| 415 |
/* Get the error information from the last operation on this handle, |
| 416 |
* formatted as a string. |
| 417 |
* |
| 418 |
* Caller is responsible for freeing the resulting string. |
| 419 |
* |
| 420 |
* @param hdl: the S3Handle object |
| 421 |
* @returns: string, or NULL if no error occurred |
| 422 |
*/ |
| 423 |
char * |
| 424 |
s3_strerror(S3Handle *hdl); |
| 425 |
|
| 426 |
/* Perform an upload. |
| 427 |
* |
| 428 |
* When this function returns, KEY and BUFFER remain the |
| 429 |
* responsibility of the caller. |
| 430 |
* |
| 431 |
* @param hdl: the S3Handle object |
| 432 |
* @param bucket: the bucket to which the upload should be made |
| 433 |
* @param key: the key to which the upload should be made |
| 434 |
* @param read_func: the callback for reading data |
| 435 |
* @param reset_func: the callback for to reset reading data |
| 436 |
* @param size_func: the callback to get the number of bytes to upload |
| 437 |
* @param md5_func: the callback to get the MD5 hash of the data to upload |
| 438 |
* @param read_data: pointer to pass to the above functions |
| 439 |
* @param progress_func: the callback for progress information |
| 440 |
* @param progress_data: pointer to pass to C{progress_func} |
| 441 |
* |
| 442 |
* @returns: false if an error ocurred |
| 443 |
*/ |
| 444 |
gboolean |
| 445 |
s3_upload(S3Handle *hdl, |
| 446 |
const char *bucket, |
| 447 |
const char *key, |
| 448 |
const gboolean chunked, |
| 449 |
s3_read_func read_func, |
| 450 |
s3_reset_func reset_func, |
| 451 |
s3_size_func size_func, |
| 452 |
s3_md5_func md5_func, |
| 453 |
gpointer read_data, |
| 454 |
s3_progress_func progress_func, |
| 455 |
gpointer progress_data); |
| 456 |
|
| 457 |
/* Perform a part upload. |
| 458 |
* |
| 459 |
* When this function returns, KEY and BUFFER remain the |
| 460 |
* responsibility of the caller. |
| 461 |
* |
| 462 |
* @param hdl: the S3Handle object |
| 463 |
* @param bucket: the bucket to which the upload should be made |
| 464 |
* @param key: the key to which the upload should be made |
| 465 |
* @param uploadId: the UploadId |
| 466 |
* @param partNumber: the part number |
| 467 |
* @param etag: return the resulting etag. |
| 468 |
* @param read_func: the callback for reading data |
| 469 |
* @param reset_func: the callback for to reset reading data |
| 470 |
* @param size_func: the callback to get the number of bytes to upload |
| 471 |
* @param md5_func: the callback to get the MD5 hash of the data to upload |
| 472 |
* @param read_data: pointer to pass to the above functions |
| 473 |
* @param progress_func: the callback for progress information |
| 474 |
* @param progress_data: pointer to pass to C{progress_func} |
| 475 |
* |
| 476 |
* @returns: false if an error ocurred |
| 477 |
*/ |
| 478 |
gboolean |
| 479 |
s3_part_upload(S3Handle *hdl, |
| 480 |
const char *bucket, |
| 481 |
const char *key, |
| 482 |
const char *uploadID, |
| 483 |
int partNumber, |
| 484 |
char **etag, |
| 485 |
s3_read_func read_func, |
| 486 |
s3_reset_func reset_func, |
| 487 |
s3_size_func size_func, |
| 488 |
s3_md5_func md5_func, |
| 489 |
gpointer read_data, |
| 490 |
s3_progress_func progress_func, |
| 491 |
gpointer progress_data); |
| 492 |
|
| 493 |
/* Initiate a multi part upload. |
| 494 |
* |
| 495 |
* @param hdl: the S3Handle object |
| 496 |
* @param bucket: the bucket to which the upload should be made |
| 497 |
* @param key: the key to which the upload should be made |
| 498 |
* |
| 499 |
* @returns: string, the uploadId or NULL on failure |
| 500 |
*/ |
| 501 |
char * |
| 502 |
s3_initiate_multi_part_upload( |
| 503 |
S3Handle *hdl, |
| 504 |
const char *bucket, |
| 505 |
const char *key); |
| 506 |
|
| 507 |
/* Complete a multi part upload. |
| 508 |
* |
| 509 |
* @param hdl: the S3Handle object |
| 510 |
* @param bucket: the bucket to which the upload should be made |
| 511 |
* @param key: the key to which the upload should be made |
| 512 |
* @param read_func: the callback for reading data |
| 513 |
* |
| 514 |
* @returns: false if an error ocurred |
| 515 |
*/ |
| 516 |
gboolean |
| 517 |
s3_complete_multi_part_upload( |
| 518 |
S3Handle *hdl, |
| 519 |
const char *bucket, |
| 520 |
const char *key, |
| 521 |
const char *uploadId, |
| 522 |
s3_read_func read_func, |
| 523 |
s3_reset_func reset_func, |
| 524 |
s3_size_func size_func, |
| 525 |
s3_md5_func md5_func, |
| 526 |
gpointer read_data); |
| 527 |
|
| 528 |
gboolean |
| 529 |
s3_abort_multi_part_upload( |
| 530 |
S3Handle *hdl, |
| 531 |
const char *bucket, |
| 532 |
const char *key, |
| 533 |
const char *uploadId); |
| 534 |
|
| 535 |
typedef struct { |
| 536 |
char *key; |
| 537 |
char *uploadId; |
| 538 |
char *prefix; |
| 539 |
guint64 size; |
| 540 |
StorageClass storage_class; |
| 541 |
} s3_object; |
| 542 |
void free_s3_object(gpointer part); |
| 543 |
|
| 544 |
typedef struct { |
| 545 |
char *key; |
| 546 |
char *x_amz_expiration; |
| 547 |
char *x_amz_restore; |
| 548 |
} s3_head_t; |
| 549 |
void free_s3_head(s3_head_t *head); |
| 550 |
|
| 551 |
typedef struct lifecycle_action { |
| 552 |
gint days; |
| 553 |
char *date; |
| 554 |
char *storage_class; |
| 555 |
} lifecycle_action; |
| 556 |
|
| 557 |
typedef struct lifecycle_rule { |
| 558 |
char *id; |
| 559 |
char *filter; |
| 560 |
char *prefix; |
| 561 |
char *status; |
| 562 |
lifecycle_action *transition; |
| 563 |
lifecycle_action *expiration; |
| 564 |
} lifecycle_rule; |
| 565 |
|
| 566 |
void free_lifecycle_rule(gpointer data); |
| 567 |
void free_lifecycle(GSList *lifecycle); |
| 568 |
|
| 569 |
|
| 570 |
/* List all of the files matching the pseudo-glob C{PREFIX*DELIMITER*}, |
| 571 |
* returning only that portion which matches C{PREFIX*DELIMITER}. S3 supports |
| 572 |
* this particular semantics, making it quite efficient. The returned list |
| 573 |
* should be freed by the caller. |
| 574 |
* |
| 575 |
* @param hdl: the S3Handle object |
| 576 |
* @param bucket: the bucket to list |
| 577 |
* @param prefix: the prefix |
| 578 |
* @param delimiter: delimiter (any length string) |
| 579 |
* @param list: (output) the list of files |
| 580 |
* @param total_size: (output) sum of size of files |
| 581 |
* @returns: FALSE if an error occurs |
| 582 |
*/ |
| 583 |
gboolean |
| 584 |
s3_list_keys(S3Handle *hdl, |
| 585 |
const char *bucket, |
| 586 |
const char *subresource, |
| 587 |
const char *prefix, |
| 588 |
const char *delimiter, |
| 589 |
GSList **list, |
| 590 |
guint64 *total_size); |
| 591 |
|
| 592 |
/* Init a restore from s3 for an object |
| 593 |
* |
| 594 |
* @param hdl: the S3Handle object |
| 595 |
* @param bucket: the bucket to list |
| 596 |
* @param key: the key to get the head from |
| 597 |
* @returns: FALSE if an error occurs |
| 598 |
*/ |
| 599 |
gboolean |
| 600 |
s3_init_restore(S3Handle *hdl, |
| 601 |
const char *bucket, |
| 602 |
const char *key); |
| 603 |
|
| 604 |
/* get the head of an object |
| 605 |
* |
| 606 |
* @param hdl: the S3Handle object |
| 607 |
* @param bucket: the bucket to list |
| 608 |
* @param key: the key to get the head from |
| 609 |
* @returns: FALSE if an error occurs |
| 610 |
*/ |
| 611 |
s3_head_t * |
| 612 |
s3_head(S3Handle *hdl, |
| 613 |
const char *bucket, |
| 614 |
const char *key); |
| 615 |
|
| 616 |
/* Read an entire file, passing the contents to write_func buffer |
| 617 |
* by buffer. |
| 618 |
* |
| 619 |
* @param hdl: the S3Handle object |
| 620 |
* @param bucket: the bucket to read from |
| 621 |
* @param key: the key to read from |
| 622 |
* @param write_func: the callback for writing data |
| 623 |
* @param reset_func: the callback for to reset writing data |
| 624 |
* @param write_data: pointer to pass to C{write_func} |
| 625 |
* @param progress_func: the callback for progress information |
| 626 |
* @param progress_data: pointer to pass to C{progress_func} |
| 627 |
* @returns: FALSE if an error occurs |
| 628 |
*/ |
| 629 |
gboolean |
| 630 |
s3_read(S3Handle *hdl, |
| 631 |
const char *bucket, |
| 632 |
const char *key, |
| 633 |
s3_write_func write_func, |
| 634 |
s3_reset_func reset_func, |
| 635 |
gpointer write_data, |
| 636 |
s3_progress_func progress_func, |
| 637 |
gpointer progress_data); |
| 638 |
|
| 639 |
/* Read a range of bytes from a file, passing the contents to write_func buffer |
| 640 |
* by buffer. |
| 641 |
* |
| 642 |
* @param hdl: the S3Handle object |
| 643 |
* @param bucket: the bucket to read from |
| 644 |
* @param key: the key to read from |
| 645 |
* @param range_begin: |
| 646 |
* @param range_ned: |
| 647 |
* @param write_func: the callback for writing data |
| 648 |
* @param reset_func: the callback for to reset writing data |
| 649 |
* @param write_data: pointer to pass to C{write_func} |
| 650 |
* @param progress_func: the callback for progress information |
| 651 |
* @param progress_data: pointer to pass to C{progress_func} |
| 652 |
* @returns: FALSE if an error occurs |
| 653 |
*/ |
| 654 |
gboolean |
| 655 |
s3_read_range(S3Handle *hdl, |
| 656 |
const char *bucket, |
| 657 |
const char *key, |
| 658 |
const guint64 range_begin, |
| 659 |
const guint64 range_end, |
| 660 |
s3_write_func write_func, |
| 661 |
s3_reset_func reset_func, |
| 662 |
gpointer write_data, |
| 663 |
s3_progress_func progress_func, |
| 664 |
gpointer progress_data); |
| 665 |
|
| 666 |
/* Delete a file. |
| 667 |
* |
| 668 |
* @param hdl: the S3Handle object |
| 669 |
* @param bucket: the bucket to delete from |
| 670 |
* @param key: the key to delete |
| 671 |
* @returns: FALSE if an error occurs; a non-existent file is I{not} considered an error. |
| 672 |
*/ |
| 673 |
gboolean |
| 674 |
s3_delete(S3Handle *hdl, |
| 675 |
const char *bucket, |
| 676 |
const char *key); |
| 677 |
|
| 678 |
/* Delete multiple file. |
| 679 |
* |
| 680 |
* @param hdl: the S3Handle object |
| 681 |
* @param bucket: the bucket to delete from |
| 682 |
* @param key: the key array to delete |
| 683 |
* @returns: 0 on sucess, 1 if multi_delete is not supported, 2 if an error |
| 684 |
* occurs; a non-existent file is I{not} considered an error. |
| 685 |
*/ |
| 686 |
int |
| 687 |
s3_multi_delete(S3Handle *hdl, |
| 688 |
const char *bucket, |
| 689 |
GSList *objects); |
| 690 |
|
| 691 |
/* Create a bucket. |
| 692 |
* |
| 693 |
* @param hdl: the S3Handle object |
| 694 |
* @param bucket: the bucket to create |
| 695 |
* @returns: FALSE if an error occurs |
| 696 |
*/ |
| 697 |
gboolean |
| 698 |
s3_make_bucket(S3Handle *hdl, |
| 699 |
const char *bucket, |
| 700 |
const char *project_id); |
| 701 |
|
| 702 |
/* Check if a bucket exists. |
| 703 |
* |
| 704 |
* @param hdl: the S3Handle object |
| 705 |
* @param bucket: the bucket to create |
| 706 |
* @returns: FALSE if an error occur |
| 707 |
*/ |
| 708 |
gboolean |
| 709 |
s3_is_bucket_exists(S3Handle *hdl, |
| 710 |
const char *bucket, |
| 711 |
const char *prefix, |
| 712 |
const char *project_id); |
| 713 |
|
| 714 |
/* Delete a bucket |
| 715 |
* |
| 716 |
* @note A bucket can not be deleted if it still contains keys |
| 717 |
* |
| 718 |
* @param hdl: the S3Handle object |
| 719 |
* @param bucket: the bucket to delete |
| 720 |
* @returns: FALSE if an error occurs |
| 721 |
*/ |
| 722 |
gboolean |
| 723 |
s3_delete_bucket(S3Handle *hdl, |
| 724 |
const char *bucket); |
| 725 |
|
| 726 |
/* Attempt a RefreshAWSSecurityToken on a token; if it succeeds, the old |
| 727 |
* token will be freed and replaced by the new. If it fails, the old |
| 728 |
* token is left unchanged and FALSE is returned. */ |
| 729 |
gboolean sts_refresh_token(char ** token, const char * directory); |
| 730 |
|
| 731 |
/* These functions are for if you want to use curl on your own. You get more |
| 732 |
* control, but it's a lot of work that way: |
| 733 |
*/ |
| 734 |
/* There is two use, simple buffer and circle buffer |
| 735 |
*/ |
| 736 |
/* simple buffer |
| 737 |
* buffer: pointer to the buffer |
| 738 |
* buffer_len: size of the allocated buffer |
| 739 |
* buffer_pos: size use in the buffer (from buffer to buffer+buffer_pos) |
| 740 |
* max_buffer_size: maximum size the buffer can be reallocated. |
| 741 |
* end_of_buffer: unused |
| 742 |
* mutex: NULL |
| 743 |
* cond: unused |
| 744 |
*/ |
| 745 |
/* circle buffer (use for chunked transfer-encodig) |
| 746 |
* buffer: pointer to the buffer |
| 747 |
* buffer_len: indice of the last byte+1 use in the buffer |
| 748 |
* buffer_pos: indice of the first byte use in the buffer |
| 749 |
* max_buffer_size: allocated size of the buffer |
| 750 |
* end_of_buffer: TRUE once reach end of data. |
| 751 |
* mutex: !NULL |
| 752 |
* cond: !NULL |
| 753 |
* |
| 754 |
* buffer_len == buffer_pos: buffer is empty |
| 755 |
* The data in use are |
| 756 |
* buffer_len > buffer_pos: from buffer_pos to buffer_len |
| 757 |
* buffer_len < buffer_pos: from buffer_pos to max_buffer_size |
| 758 |
* and from 0 to buffer_len |
| 759 |
*/ |
| 760 |
typedef struct { |
| 761 |
char *buffer; |
| 762 |
guint buffer_len; |
| 763 |
guint buffer_pos; |
| 764 |
guint max_buffer_size; |
| 765 |
gboolean end_of_buffer; |
| 766 |
GMutex *mutex; |
| 767 |
GCond *cond; |
| 768 |
} CurlBuffer; |
| 769 |
|
| 770 |
#define S3_BUFFER_READ_FUNCS s3_buffer_read_func, s3_buffer_reset_func, s3_buffer_size_func, s3_buffer_md5_func |
| 771 |
|
| 772 |
#define S3_BUFFER_WRITE_FUNCS s3_buffer_write_func, s3_buffer_reset_func |
| 773 |
|
| 774 |
/* a CURLOPT_READFUNCTION to read data from a buffer. */ |
| 775 |
size_t |
| 776 |
s3_buffer_read_func(void *ptr, size_t size, size_t nmemb, void * stream); |
| 777 |
|
| 778 |
size_t |
| 779 |
s3_buffer_size_func(void *stream); |
| 780 |
|
| 781 |
GByteArray* |
| 782 |
s3_buffer_md5_func(void *stream); |
| 783 |
|
| 784 |
void |
| 785 |
s3_buffer_reset_func(void *stream); |
| 786 |
|
| 787 |
#define S3_EMPTY_READ_FUNCS s3_empty_read_func, NULL, s3_empty_size_func, s3_empty_md5_func |
| 788 |
|
| 789 |
/* a CURLOPT_WRITEFUNCTION to write data to a buffer. */ |
| 790 |
size_t |
| 791 |
s3_buffer_write_func(void *ptr, size_t size, size_t nmemb, void *stream); |
| 792 |
|
| 793 |
/* a CURLOPT_READFUNCTION that writes nothing. */ |
| 794 |
size_t |
| 795 |
s3_empty_read_func(void *ptr, size_t size, size_t nmemb, void * stream); |
| 796 |
|
| 797 |
size_t |
| 798 |
s3_empty_size_func(void *stream); |
| 799 |
|
| 800 |
GByteArray* |
| 801 |
s3_empty_md5_func(void *stream); |
| 802 |
|
| 803 |
#define S3_COUNTER_WRITE_FUNCS s3_counter_write_func, s3_counter_reset_func |
| 804 |
|
| 805 |
/* a CURLOPT_WRITEFUNCTION to write data that just counts data. |
| 806 |
* s3_write_data should be NULL or a pointer to an gint64. |
| 807 |
*/ |
| 808 |
size_t |
| 809 |
s3_counter_write_func(void *ptr, size_t size, size_t nmemb, void *stream); |
| 810 |
|
| 811 |
void |
| 812 |
s3_counter_reset_func(void *stream); |
| 813 |
|
| 814 |
#ifdef _WIN32 |
| 815 |
/* a CURLOPT_READFUNCTION to read data from a file. */ |
| 816 |
size_t |
| 817 |
s3_file_read_func(void *ptr, size_t size, size_t nmemb, void * stream); |
| 818 |
|
| 819 |
size_t |
| 820 |
s3_file_size_func(void *stream); |
| 821 |
|
| 822 |
GByteArray* |
| 823 |
s3_file_md5_func(void *stream); |
| 824 |
|
| 825 |
size_t |
| 826 |
s3_file_reset_func(void *stream); |
| 827 |
|
| 828 |
/* a CURLOPT_WRITEFUNCTION to write data to a file. */ |
| 829 |
size_t |
| 830 |
s3_file_write_func(void *ptr, size_t size, size_t nmemb, void *stream); |
| 831 |
#endif |
| 832 |
|
| 833 |
gboolean |
| 834 |
s3_get_lifecycle(S3Handle *hdl, const char *bucket, GSList **lifecycle); |
| 835 |
|
| 836 |
gboolean |
| 837 |
s3_put_lifecycle(S3Handle *hdl, const char *bucket, GSList *lifecycle); |
| 838 |
|
| 839 |
/* Adds a null termination to a buffer. */ |
| 840 |
void terminate_buffer(CurlBuffer *); |
| 841 |
|
| 842 |
#endif |