e34fafd2b8feac87c0736b418ecbd66c87e7af47
[bluesky.git] / bluesky / store-s3.c
1 /* Blue Sky: File Systems in the Cloud
2  *
3  * Copyright (C) 2009  The Regents of the University of California
4  * Written by Michael Vrable <mvrable@cs.ucsd.edu>
5  *
6  * TODO: Licensing
7  */
8
9 #include <stdint.h>
10 #include <stdlib.h>
11 #include <glib.h>
12 #include <string.h>
13
14 #include "bluesky-private.h"
15 #include "libs3.h"
16
17 /* Interface to Amazon S3 storage. */
18
19 typedef struct {
20     GThreadPool *thread_pool;
21     S3BucketContext bucket;
22     uint8_t encryption_key[CRYPTO_KEY_SIZE];
23 } S3Store;
24
25 typedef struct {
26     enum { S3_GET, S3_PUT } op;
27     gchar *key;
28     BlueSkyRCStr *data;
29 } S3Op;
30
31 struct get_info {
32     int success;
33     GString *buf;
34 };
35
36 struct put_info {
37     BlueSkyRCStr *val;
38     gint offset;
39 };
40
41 static S3Status s3store_get_handler(int bufferSize, const char *buffer,
42                                     void *callbackData)
43 {
44     struct get_info *info = (struct get_info *)callbackData;
45     g_string_append_len(info->buf, buffer, bufferSize);
46     return S3StatusOK;
47 }
48
49 static int s3store_put_handler(int bufferSize, char *buffer,
50                                void *callbackData)
51 {
52     struct put_info *info = (struct put_info *)callbackData;
53     gint bytes = MIN(bufferSize, (int)(info->val->len - info->offset));
54     memcpy(buffer, (char *)info->val->data + info->offset, bytes);
55     info->offset += bytes;
56     return bytes;
57 }
58
59 static S3Status s3store_properties_callback(const S3ResponseProperties *properties,
60                                      void *callbackData)
61 {
62     return S3StatusOK;
63 }
64
65 static void s3store_response_callback(S3Status status,
66                                const S3ErrorDetails *errorDetails,
67                                void *callbackData)
68 {
69     struct get_info *info = (struct get_info *)callbackData;
70
71     if (status == 0) {
72         info->success = 1;
73     }
74
75     if (errorDetails != NULL && errorDetails->message != NULL) {
76         g_print("  Error message: %s\n", errorDetails->message);
77     }
78 }
79
80 static void s3store_task(gpointer a, gpointer s)
81 {
82     BlueSkyStoreAsync *async = (BlueSkyStoreAsync *)a;
83     S3Store *store = (S3Store *)s;
84
85     async->status = ASYNC_RUNNING;
86     async->exec_time = bluesky_now_hires();
87
88     if (async->op == STORE_OP_GET) {
89         struct get_info info;
90         info.buf = g_string_new("");
91         info.success = 0;
92
93         struct S3GetObjectHandler handler;
94         handler.responseHandler.propertiesCallback = s3store_properties_callback;
95         handler.responseHandler.completeCallback = s3store_response_callback;
96         handler.getObjectDataCallback = s3store_get_handler;
97
98         S3_get_object(&store->bucket, async->key, NULL, 0, 0, NULL,
99                       &handler, &info);
100
101         if (info.success) {
102             async->data = bluesky_string_new_from_gstring(info.buf);
103             async->result = 0;
104         } else {
105             g_string_free(info.buf, TRUE);
106         }
107
108     } else if (async->op == STORE_OP_PUT) {
109         struct put_info info;
110         info.val = async->data;
111         info.offset = 0;
112
113         struct S3PutObjectHandler handler;
114         handler.responseHandler.propertiesCallback
115             = s3store_properties_callback;
116         handler.responseHandler.completeCallback = s3store_response_callback;
117         handler.putObjectDataCallback = s3store_put_handler;
118
119         S3_put_object(&store->bucket, async->key, async->data->len, NULL, NULL,
120                       &handler, &info);
121
122         async->result = 0;
123     }
124
125     bluesky_store_async_mark_complete(async);
126     bluesky_store_async_unref(async);
127 }
128
129 static gpointer s3store_new(const gchar *path)
130 {
131     S3Store *store = g_new(S3Store, 1);
132     store->thread_pool = g_thread_pool_new(s3store_task, store, -1, FALSE,
133                                            NULL);
134     store->bucket.bucketName = "mvrable-bluesky";
135     store->bucket.protocol = S3ProtocolHTTP;
136     store->bucket.uriStyle = S3UriStylePath;
137     store->bucket.accessKeyId = getenv("AWS_ACCESS_KEY_ID");
138     store->bucket.secretAccessKey = getenv("AWS_SECRET_ACCESS_KEY");
139
140     const char *key = getenv("BLUESKY_KEY");
141     if (key == NULL) {
142         g_error("Encryption key not defined; please set BLUESKY_KEY environment variable");
143         exit(1);
144     }
145
146     bluesky_crypt_hash_key(key, store->encryption_key);
147
148     g_print("Initializing S3 with bucket %s, access key %s, encryption key %s\n",
149             store->bucket.bucketName, store->bucket.accessKeyId, key);
150
151     return store;
152 }
153
154 static void s3store_destroy(gpointer store)
155 {
156     g_free(store);
157 }
158
159 static void s3store_submit(gpointer s, BlueSkyStoreAsync *async)
160 {
161     S3Store *store = (S3Store *)s;
162     g_return_if_fail(async->status == ASYNC_NEW);
163     g_return_if_fail(async->op != STORE_OP_NONE);
164
165     switch (async->op) {
166     case STORE_OP_GET:
167     case STORE_OP_PUT:
168         async->status = ASYNC_PENDING;
169         bluesky_store_async_ref(async);
170         g_thread_pool_push(store->thread_pool, async, NULL);
171         break;
172
173     default:
174         g_warning("Uknown operation type for S3Store: %d\n", async->op);
175         bluesky_store_async_mark_complete(async);
176         break;
177     }
178 }
179
180 static void s3store_cleanup(gpointer store, BlueSkyStoreAsync *async)
181 {
182     GString *buf = (GString *)async->store_private;
183
184     if (buf != NULL) {
185         g_string_free(buf, TRUE);
186         async->store_private = NULL;
187     }
188 }
189
190 static BlueSkyStoreImplementation store_impl = {
191     .create = s3store_new,
192     .destroy = s3store_destroy,
193     .submit = s3store_submit,
194     .cleanup = s3store_cleanup,
195 };
196
197 void bluesky_store_init_s3(void)
198 {
199     S3_initialize(NULL, S3_INIT_ALL);
200     bluesky_store_register(&store_impl, "s3");
201 }