S3Store cleanup.
[bluesky.git] / bluesky / s3store.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     g_print("(Properties callback)\n");
63     return S3StatusOK;
64 }
65
66 static void s3store_response_callback(S3Status status,
67                                const S3ErrorDetails *errorDetails,
68                                void *callbackData)
69 {
70     struct get_info *info = (struct get_info *)callbackData;
71
72     g_print("S3 operation complete, status=%s, now=%ld\n",
73             S3_get_status_name(status), bluesky_now_hires());
74
75     if (status == 0) {
76         info->success = 1;
77     }
78
79     if (errorDetails != NULL) {
80         g_print("  Error message: %s\n", errorDetails->message);
81     }
82 }
83
84 static BlueSkyRCStr *s3store_get(gpointer s, const gchar *key)
85 {
86     S3Store *store = (S3Store *)s;
87
88     struct get_info info;
89     info.buf = g_string_new("");
90     info.success = 0;
91
92     struct S3GetObjectHandler handler;
93     handler.responseHandler.propertiesCallback = s3store_properties_callback;
94     handler.responseHandler.completeCallback = s3store_response_callback;
95     handler.getObjectDataCallback = s3store_get_handler;
96
97     g_print("Starting fetch of %s from S3...\n", key);
98     S3_get_object(&store->bucket, key, NULL, 0, 0, NULL,
99                   &handler, &info);
100
101     if (!info.success) {
102         g_string_free(info.buf, TRUE);
103         return NULL;
104     }
105
106     BlueSkyRCStr *raw, *decrypted;
107     raw = bluesky_string_new_from_gstring(info.buf);
108     decrypted = bluesky_crypt_decrypt(raw, store->encryption_key);
109     bluesky_string_unref(raw);
110     return decrypted;
111 }
112
113 static void s3store_put(gpointer s, const gchar *key, BlueSkyRCStr *val)
114 {
115     S3Store *store = (S3Store *)s;
116
117     S3Op *op = g_new(S3Op, 1);
118     op->op = S3_PUT;
119     op->key = g_strdup(key);
120     bluesky_string_ref(val);
121     op->data = val;
122
123     g_thread_pool_push(store->thread_pool, op, NULL);
124 }
125
126 static void s3store_task(gpointer o, gpointer s)
127 {
128     S3Store *store = (S3Store *)s;
129     S3Op *op = (S3Op *)o;
130
131     g_print("Start task [key=%s]...\n", op->key);
132
133     if (op->op == S3_PUT) {
134         BlueSkyRCStr *encrypted = bluesky_crypt_encrypt(op->data,
135                                                         store->encryption_key);
136
137         struct put_info info;
138         info.val = encrypted;
139         info.offset = 0;
140
141         struct S3PutObjectHandler handler;
142         handler.responseHandler.propertiesCallback
143             = s3store_properties_callback;
144         handler.responseHandler.completeCallback = s3store_response_callback;
145         handler.putObjectDataCallback = s3store_put_handler;
146
147         g_print("Starting store of %s to S3 at %ld...\n",
148                 op->key, bluesky_now_hires());
149         S3_put_object(&store->bucket, op->key, encrypted->len, NULL, NULL,
150                       &handler, &info);
151
152         bluesky_string_unref(encrypted);
153     }
154
155     bluesky_string_unref(op->data);
156     g_free(op->key);
157     g_free(op);
158
159     g_print("Finish task...\n");
160 }
161
162 static gpointer s3store_new()
163 {
164     S3Store *store = g_new(S3Store, 1);
165     store->thread_pool = g_thread_pool_new(s3store_task, store, -1, FALSE,
166                                            NULL);
167     store->bucket.bucketName = "mvrable-bluesky";
168     store->bucket.protocol = S3ProtocolHTTP;
169     store->bucket.uriStyle = S3UriStylePath;
170     store->bucket.accessKeyId = getenv("AWS_ACCESS_KEY_ID");
171     store->bucket.secretAccessKey = getenv("AWS_SECRET_ACCESS_KEY");
172
173     const char *key = getenv("BLUESKY_KEY");
174     if (key == NULL) {
175         g_error("Encryption key not defined; please set BLUESKY_KEY environment variable");
176         exit(1);
177     }
178
179     bluesky_crypt_hash_key(key, store->encryption_key);
180
181     g_print("Initializing S3 with bucket %s, access key %s, encryption key %s\n",
182             store->bucket.bucketName, store->bucket.accessKeyId, key);
183
184     return store;
185 }
186
187 static void s3store_destroy(gpointer store)
188 {
189     g_free(store);
190 }
191
192 static BlueSkyStoreImplementation store_impl = {
193     .create = s3store_new,
194     .destroy = s3store_destroy,
195     .get = s3store_get,
196     .put = s3store_put,
197 };
198
199 void bluesky_store_init_s3(void)
200 {
201     S3_initialize(NULL, S3_INIT_ALL);
202     bluesky_store_register(&store_impl, "s3");
203 }