Drop some debugging messages.
[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     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     g_print("S3 operation complete, status=%s, now=%ld\n",
72             S3_get_status_name(status), bluesky_now_hires());
73
74     if (status == 0) {
75         info->success = 1;
76     }
77
78     if (errorDetails != NULL) {
79         g_print("  Error message: %s\n", errorDetails->message);
80     }
81 }
82
83 static void s3store_task(gpointer a, gpointer s)
84 {
85     BlueSkyStoreAsync *async = (BlueSkyStoreAsync *)a;
86     S3Store *store = (S3Store *)s;
87
88     async->status = ASYNC_RUNNING;
89
90     if (async->op == STORE_OP_GET) {
91         struct get_info info;
92         info.buf = g_string_new("");
93         info.success = 0;
94
95         struct S3GetObjectHandler handler;
96         handler.responseHandler.propertiesCallback = s3store_properties_callback;
97         handler.responseHandler.completeCallback = s3store_response_callback;
98         handler.getObjectDataCallback = s3store_get_handler;
99
100         S3_get_object(&store->bucket, async->key, NULL, 0, 0, NULL,
101                       &handler, &info);
102
103         if (info.success) {
104             BlueSkyRCStr *raw, *decrypted;
105             raw = bluesky_string_new_from_gstring(info.buf);
106             decrypted = bluesky_crypt_decrypt(raw, store->encryption_key);
107             bluesky_string_unref(raw);
108             async->data = decrypted;
109             async->result = 0;
110         } else {
111             g_string_free(info.buf, TRUE);
112         }
113
114     } else if (async->op == STORE_OP_PUT) {
115         BlueSkyRCStr *encrypted = bluesky_crypt_encrypt(async->data,
116                                                         store->encryption_key);
117
118         struct put_info info;
119         info.val = encrypted;
120         info.offset = 0;
121
122         struct S3PutObjectHandler handler;
123         handler.responseHandler.propertiesCallback
124             = s3store_properties_callback;
125         handler.responseHandler.completeCallback = s3store_response_callback;
126         handler.putObjectDataCallback = s3store_put_handler;
127
128         S3_put_object(&store->bucket, async->key, encrypted->len, NULL, NULL,
129                       &handler, &info);
130
131         bluesky_string_unref(encrypted);
132
133         async->result = 0;
134     }
135
136     bluesky_store_async_mark_complete(async);
137     bluesky_store_async_unref(async);
138 }
139
140 static gpointer s3store_new()
141 {
142     S3Store *store = g_new(S3Store, 1);
143     store->thread_pool = g_thread_pool_new(s3store_task, store, -1, FALSE,
144                                            NULL);
145     store->bucket.bucketName = "mvrable-bluesky";
146     store->bucket.protocol = S3ProtocolHTTP;
147     store->bucket.uriStyle = S3UriStylePath;
148     store->bucket.accessKeyId = getenv("AWS_ACCESS_KEY_ID");
149     store->bucket.secretAccessKey = getenv("AWS_SECRET_ACCESS_KEY");
150
151     const char *key = getenv("BLUESKY_KEY");
152     if (key == NULL) {
153         g_error("Encryption key not defined; please set BLUESKY_KEY environment variable");
154         exit(1);
155     }
156
157     bluesky_crypt_hash_key(key, store->encryption_key);
158
159     g_print("Initializing S3 with bucket %s, access key %s, encryption key %s\n",
160             store->bucket.bucketName, store->bucket.accessKeyId, key);
161
162     return store;
163 }
164
165 static void s3store_destroy(gpointer store)
166 {
167     g_free(store);
168 }
169
170 static void s3store_submit(gpointer s, BlueSkyStoreAsync *async)
171 {
172     S3Store *store = (S3Store *)s;
173     g_return_if_fail(async->status == ASYNC_NEW);
174     g_return_if_fail(async->op != STORE_OP_NONE);
175
176     switch (async->op) {
177     case STORE_OP_GET:
178     case STORE_OP_PUT:
179         async->status = ASYNC_PENDING;
180         bluesky_store_async_ref(async);
181         g_thread_pool_push(store->thread_pool, async, NULL);
182         break;
183
184     default:
185         g_warning("Uknown operation type for S3Store: %d\n", async->op);
186         bluesky_store_async_mark_complete(async);
187         break;
188     }
189 }
190
191 static void s3store_cleanup(gpointer store, BlueSkyStoreAsync *async)
192 {
193     GString *buf = (GString *)async->store_private;
194
195     if (buf != NULL) {
196         g_string_free(buf, TRUE);
197         async->store_private = NULL;
198     }
199 }
200
201 static BlueSkyStoreImplementation store_impl = {
202     .create = s3store_new,
203     .destroy = s3store_destroy,
204     .submit = s3store_submit,
205     .cleanup = s3store_cleanup,
206 };
207
208 void bluesky_store_init_s3(void)
209 {
210     S3_initialize(NULL, S3_INIT_ALL);
211     bluesky_store_register(&store_impl, "s3");
212 }