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