2 * Copyright (c) 2008,2009 Zmanda, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published
6 * by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18 * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
24 #include <curl/curl.h>
30 /* An opaque handle. S3Handles should only be accessed from a single
31 * thread at any given time, although it is fine to use different handles
32 * in different threads simultaneously. */
33 typedef struct S3Handle S3Handle;
35 /* Callback function to read data to upload
37 * @note this is the same as CURLOPT_READFUNCTION
39 * @param data: The pointer to write data to
40 * @param size: The size of each "element" of the data buffer in bytes
41 * @param nmemb: The number of elements in the data buffer.
42 * So, the buffer's size is size*nmemb bytes.
43 * @param stream: The read_data (an opaque pointer)
45 * @return The number of bytes written to the buffer,
46 * CURL_READFUNC_PAUSE to pause, or CURL_READFUNC_ABORT to abort.
47 * Return 0 only if there's no more data to be uploaded.
49 typedef size_t (*s3_read_func)(void *data, size_t size, size_t nmemb, void *stream);
51 /* This function is called to get size of the upload data
53 * @param data: The write_data (opaque pointer)
55 * @return The number of bytes of data, negative for error
57 typedef size_t (*s3_size_func)(void *data);
59 /* This function is called to get MD5 hash of the upload data
61 * @param data: The write_data (opaque pointer)
63 * @return The MD5 hash, NULL on error
65 typedef GByteArray* (*s3_md5_func)(void *data);
67 /* This function is called to reset an upload or download data stream
70 * @param data: The read_data or write_data (opaque pointer)
72 * @return The number of bytes of data, negative for error
74 typedef void (*s3_reset_func)(void *data);
76 /* Callback function to write data that's been downloaded
78 * @note this is the same as CURLOPT_WRITEFUNCTION
80 * @param data: The pointer to read data from
81 * @param size: The size of each "element" of the data buffer in bytes
82 * @param nmemb: The number of elements in the data buffer.
83 * So, the buffer's size is size*nmemb bytes.
84 * @param stream: the write_data (an opaque pointer)
86 * @return The number of bytes written to the buffer or
87 * CURL_WRITEFUNC_PAUSE to pause.
88 * If it's the number of bytes written, it should match the buffer size
90 typedef size_t (*s3_write_func)(void *data, size_t size, size_t nmemb, void *stream);
93 * Callback function to track progress
95 * @note this is the same as CURLOPT_PROGRESSFUNCTION
97 * @param data: The progress_data
98 * @param dltotal: The total number of bytes to downloaded
99 * @param dlnow: The current number of bytes downloaded
100 * @param ultotal: The total number of bytes to downloaded
101 * @param ulnow: The current number of bytes downloaded
103 * @return 0 to continue, non-zero to abort.
105 typedef curl_progress_callback s3_progress_func;
111 /* These are assumed to be already URL-escaped. */
112 # define STS_BASE_URL "https://ls.amazonaws.com/"
113 # define STS_PRODUCT_TOKEN "{ProductToken}AAAGQXBwVGtu4geoGybuwuk8VEEPzJ9ZANpu0yzbf9g4Gs5Iarzff9B7qaDBEEaWcAzWpcN7zmdMO765jOtEFc4DWTRNkpPSzUnTdkHbdYUamath73OreaZtB86jy/JF0gsHZfhxeKc/3aLr8HNT//DsX3r272zYHLDPWWUbFguOwqNjllnt6BshYREx59l8RrWABLSa37dyJeN+faGvz3uQxiDakZRn3LfInOE6d9+fTFl50LPoP08LCqI/SJfpouzWix7D/cep3Jq8yYNyM1rgAOTF7/wh7r8OuPDLJ/xZUDLfykePIAM="
115 /* This preprocessor magic will enumerate constants named S3_ERROR_XxxYyy for
116 * each of the errors in parentheses.
118 * see http://docs.amazonwebservices.com/AmazonS3/2006-03-01/ErrorCodeList.html
119 * for Amazon's breakdown of error responses.
121 #define S3_ERROR_LIST \
123 S3_ERROR(AccountProblem), \
124 S3_ERROR(AllAccessDisabled), \
125 S3_ERROR(AmbiguousGrantByEmailAddress), \
126 S3_ERROR(OperationAborted), \
127 S3_ERROR(BadDigest), \
128 S3_ERROR(BucketAlreadyExists), \
129 S3_ERROR(BucketAlreadyOwnedByYou), \
130 S3_ERROR(BucketNotEmpty), \
131 S3_ERROR(CredentialsNotSupported), \
132 S3_ERROR(EntityTooLarge), \
133 S3_ERROR(IncompleteBody), \
134 S3_ERROR(InternalError), \
135 S3_ERROR(InvalidAccessKeyId), \
136 S3_ERROR(InvalidArgument), \
137 S3_ERROR(InvalidBucketName), \
138 S3_ERROR(InvalidDigest), \
139 S3_ERROR(InvalidRange), \
140 S3_ERROR(InvalidSecurity), \
141 S3_ERROR(InvalidSOAPRequest), \
142 S3_ERROR(InvalidStorageClass), \
143 S3_ERROR(InvalidTargetBucketForLogging), \
144 S3_ERROR(KeyTooLong), \
145 S3_ERROR(InvalidURI), \
146 S3_ERROR(MalformedACLError), \
147 S3_ERROR(MaxMessageLengthExceeded), \
148 S3_ERROR(MetadataTooLarge), \
149 S3_ERROR(MethodNotAllowed), \
150 S3_ERROR(MissingAttachment), \
151 S3_ERROR(MissingContentLength), \
152 S3_ERROR(MissingSecurityElement), \
153 S3_ERROR(MissingSecurityHeader), \
154 S3_ERROR(NoLoggingStatusForKey), \
155 S3_ERROR(NoSuchBucket), \
156 S3_ERROR(NoSuchKey), \
157 S3_ERROR(NotImplemented), \
158 S3_ERROR(NotSignedUp), \
159 S3_ERROR(PreconditionFailed), \
160 S3_ERROR(RequestTimeout), \
161 S3_ERROR(RequestTimeTooSkewed), \
162 S3_ERROR(RequestTorrentOfBucketError), \
163 S3_ERROR(SignatureDoesNotMatch), \
164 S3_ERROR(TooManyBuckets), \
165 S3_ERROR(UnexpectedContent), \
166 S3_ERROR(UnresolvableGrantByEmailAddress), \
171 #define S3_ERROR(NAME) S3_ERROR_ ## NAME
180 /* Does this install of curl support SSL?
185 s3_curl_supports_ssl(void);
187 /* Checks if the version of libcurl being used supports and checks
188 * wildcard certificates correctly (used for the subdomains required
189 * by location constraints).
191 * @returns: true if the version of libcurl is new enough
194 s3_curl_location_compat(void);
196 /* Checks if a bucket name is compatible with setting a location
199 * @note This doesn't guarantee that bucket name is entirely valid,
200 * just that using it as one (or more) subdomain(s) of s3.amazonaws.com
201 * won't fail; that would prevent the reporting of useful messages from
204 * @param bucket: the bucket name
205 * @returns: true if the bucket name is compatible
208 s3_bucket_location_compat(const char *bucket);
210 /* Initialize S3 operation
212 * If an error occurs in this function, diagnostic information is
215 * @returns: false if an error occurred
220 /* Set up an S3Handle.
222 * The concept of a bucket is defined by the Amazon S3 API.
223 * See: "Components of Amazon S3" - API Version 2006-03-01 pg. 8
225 * @param access_key: the secret key for Amazon Web Services
226 * @param secret_key: the secret key for Amazon Web Services
227 * @param user_token: the user token for Amazon DevPay
228 * @param bucket_location: the location constraint for buckets
229 * @param ca_info: the path to pass to libcurl as the certificate authority.
230 * see curl_easy_setopt() CURLOPT_CAINFO for more
231 * @returns: the new S3Handle
234 s3_open(const char * access_key, const char *secret_key, const char * user_token,
235 const char * bucket_location, const char * ca_info);
237 /* Deallocate an S3Handle
239 * @param hdl: the S3Handle object
242 s3_free(S3Handle *hdl);
244 /* Reset the information about the last request, including
245 * freeing any allocated memory. The S3Handle itself is not
246 * freed and may be used again. This function is called
247 * automatically as needed, and should be called to free memory
248 * when the handle will not be used for some time.
250 * @param hdl: the S3Handle object
253 s3_reset(S3Handle *hdl);
255 /* Get the error information for the last operation
257 * All results are returned via result parameters. If any parameter is
258 * NULL, that result will not be returned. Caller is not responsible for
259 * freeing any returned strings, although the results are only valid until
260 * the next call to an S3 function with this handle.
262 * @param hdl: the S3Handle object
263 * @param message: (result) the error message, or NULL if none exists
264 * @param response_code: (result) the HTTP response code (or 0 if none exists)
265 * @param s3_error_code: (result) the S3 error code (see constants, above)
266 * @param s3_error_name: (result) the S3 error name (e.g., "RequestTimeout"),
267 * or NULL if none exists
268 * @param curl_code: (result) the curl error code (or 0 if none exists)
269 * @param num_retries: (result) number of retries
272 s3_error(S3Handle *hdl,
273 const char **message,
274 guint *response_code,
275 s3_error_code_t *s3_error_code,
276 const char **s3_error_name,
280 /* Control verbose output of HTTP transactions, etc.
282 * @param hdl: the S3Handle object
283 * @param verbose: if true, send HTTP transactions, etc. to debug output
286 s3_verbose(S3Handle *hdl,
289 /* Control the use of SSL with HTTP transactions.
291 * @param hdl: the S3Handle object
292 * @param use_ssl: if true, use SSL (if curl supports it)
293 * @returns: true if the setting is valid
296 s3_use_ssl(S3Handle *hdl, gboolean use_ssl);
298 /* Control the throttling of S3 uploads. Only supported with curl >= 7.15.5.
300 * @param hdl: the S3Handle object
301 * @param max_send_speed: max speed (bytes/sec) at which to send
302 * @returns: true if the setting is valid
305 s3_set_max_send_speed(S3Handle *hdl, guint64 max_send_speed);
307 /* Control the throttling of S3 downloads. Only supported with curl >= 7.15.5.
309 * @param hdl: the S3Handle object
310 * @param max_recv_speed: max speed (bytes/sec) at which to receive
311 * @returns: true if the setting is valid
314 s3_set_max_recv_speed(S3Handle *hdl, guint64 max_recv_speed);
316 /* Get the error information from the last operation on this handle,
317 * formatted as a string.
319 * Caller is responsible for freeing the resulting string.
321 * @param hdl: the S3Handle object
322 * @returns: string, or NULL if no error occurred
325 s3_strerror(S3Handle *hdl);
327 /* Perform an upload.
329 * When this function returns, KEY and BUFFER remain the
330 * responsibility of the caller.
332 * @param hdl: the S3Handle object
333 * @param bucket: the bucket to which the upload should be made
334 * @param key: the key to which the upload should be made
335 * @param read_func: the callback for reading data
336 * @param reset_func: the callback for to reset reading data
337 * @param size_func: the callback to get the number of bytes to upload
338 * @param md5_func: the callback to get the MD5 hash of the data to upload
339 * @param read_data: pointer to pass to the above functions
340 * @param progress_func: the callback for progress information
341 * @param progress_data: pointer to pass to C{progress_func}
343 * @returns: false if an error ocurred
346 s3_upload(S3Handle *hdl,
349 s3_read_func read_func,
350 s3_reset_func reset_func,
351 s3_size_func size_func,
352 s3_md5_func md5_func,
354 s3_progress_func progress_func,
355 gpointer progress_data);
357 /* List all of the files matching the pseudo-glob C{PREFIX*DELIMITER*},
358 * returning only that portion which matches C{PREFIX*DELIMITER}. S3 supports
359 * this particular semantics, making it quite efficient. The returned list
360 * should be freed by the caller.
362 * @param hdl: the S3Handle object
363 * @param bucket: the bucket to list
364 * @param prefix: the prefix
365 * @param delimiter: delimiter (any length string)
366 * @param list: (output) the list of files
367 * @returns: FALSE if an error occurs
370 s3_list_keys(S3Handle *hdl,
373 const char *delimiter,
376 /* Read an entire file, passing the contents to write_func buffer
379 * @param hdl: the S3Handle object
380 * @param bucket: the bucket to read from
381 * @param key: the key to read from
382 * @param write_func: the callback for writing data
383 * @param reset_func: the callback for to reset writing data
384 * @param write_data: pointer to pass to C{write_func}
385 * @param progress_func: the callback for progress information
386 * @param progress_data: pointer to pass to C{progress_func}
387 * @returns: FALSE if an error occurs
390 s3_read(S3Handle *hdl,
393 s3_write_func write_func,
394 s3_reset_func reset_func,
396 s3_progress_func progress_func,
397 gpointer progress_data);
401 * @param hdl: the S3Handle object
402 * @param bucket: the bucket to delete from
403 * @param key: the key to delete
404 * @returns: FALSE if an error occurs; a non-existent file is I{not} considered an error.
407 s3_delete(S3Handle *hdl,
413 * @param hdl: the S3Handle object
414 * @param bucket: the bucket to create
415 * @returns: FALSE if an error occurs
418 s3_make_bucket(S3Handle *hdl,
423 * @note A bucket can not be deleted if it still contains keys
425 * @param hdl: the S3Handle object
426 * @param bucket: the bucket to delete
427 * @returns: FALSE if an error occurs
430 s3_delete_bucket(S3Handle *hdl,
433 /* Attempt a RefreshAWSSecurityToken on a token; if it succeeds, the old
434 * token will be freed and replaced by the new. If it fails, the old
435 * token is left unchanged and FALSE is returned. */
436 gboolean sts_refresh_token(char ** token, const char * directory);
438 /* These functions are for if you want to use curl on your own. You get more
439 * control, but it's a lot of work that way: */
444 guint max_buffer_size;
447 #define S3_BUFFER_READ_FUNCS s3_buffer_read_func, s3_buffer_reset_func, s3_buffer_size_func, s3_buffer_md5_func
449 #define S3_BUFFER_WRITE_FUNCS s3_buffer_write_func, s3_buffer_reset_func
451 /* a CURLOPT_READFUNCTION to read data from a buffer. */
453 s3_buffer_read_func(void *ptr, size_t size, size_t nmemb, void * stream);
456 s3_buffer_size_func(void *stream);
459 s3_buffer_md5_func(void *stream);
462 s3_buffer_reset_func(void *stream);
464 #define S3_EMPTY_READ_FUNCS s3_empty_read_func, NULL, s3_empty_size_func, s3_empty_md5_func
466 /* a CURLOPT_WRITEFUNCTION to write data to a buffer. */
468 s3_buffer_write_func(void *ptr, size_t size, size_t nmemb, void *stream);
470 /* a CURLOPT_READFUNCTION that writes nothing. */
472 s3_empty_read_func(void *ptr, size_t size, size_t nmemb, void * stream);
475 s3_empty_size_func(void *stream);
478 s3_empty_md5_func(void *stream);
480 #define S3_COUNTER_WRITE_FUNCS s3_counter_write_func, s3_counter_reset_func
482 /* a CURLOPT_WRITEFUNCTION to write data that just counts data.
483 * s3_write_data should be NULL or a pointer to an gint64.
486 s3_counter_write_func(void *ptr, size_t size, size_t nmemb, void *stream);
489 s3_counter_reset_func(void *stream);
492 /* a CURLOPT_READFUNCTION to read data from a file. */
494 s3_file_read_func(void *ptr, size_t size, size_t nmemb, void * stream);
497 s3_file_size_func(void *stream);
500 s3_file_md5_func(void *stream);
503 s3_file_reset_func(void *stream);
505 /* a CURLOPT_WRITEFUNCTION to write data to a file. */
507 s3_file_write_func(void *ptr, size_t size, size_t nmemb, void *stream);
510 /* Adds a null termination to a buffer. */
511 void terminate_buffer(CurlBuffer *);