S3Store cleanup.
[bluesky.git] / bluesky / s3store.c
index 083023e..d742e3b 100644 (file)
@@ -28,42 +28,16 @@ typedef struct {
     BlueSkyRCStr *data;
 } S3Op;
 
-static void s3store_task(gpointer s, gpointer o);
-
-static gpointer s3store_new()
-{
-    S3Store *store = g_new(S3Store, 1);
-    store->thread_pool = g_thread_pool_new(s3store_task, store, -1, FALSE,
-                                           NULL);
-    store->bucket.bucketName = "mvrable-bluesky";
-    store->bucket.protocol = S3ProtocolHTTP;
-    store->bucket.uriStyle = S3UriStylePath;
-    store->bucket.accessKeyId = getenv("AWS_ACCESS_KEY_ID");
-    store->bucket.secretAccessKey = getenv("AWS_SECRET_ACCESS_KEY");
-
-    const char *key = getenv("BLUESKY_KEY");
-    if (key == NULL) {
-        g_error("Encryption key not defined; please set BLUESKY_KEY environment variable");
-        exit(1);
-    }
-
-    bluesky_crypt_hash_key(key, store->encryption_key);
-
-    g_print("Initializing S3 with bucket %s, access key %s, encryption key %s\n",
-            store->bucket.bucketName, store->bucket.accessKeyId, key);
-
-    return store;
-}
-
-static void s3store_destroy(gpointer store)
-{
-    g_free(store);
-}
-
 struct get_info {
+    int success;
     GString *buf;
 };
 
+struct put_info {
+    BlueSkyRCStr *val;
+    gint offset;
+};
+
 static S3Status s3store_get_handler(int bufferSize, const char *buffer,
                                     void *callbackData)
 {
@@ -72,11 +46,6 @@ static S3Status s3store_get_handler(int bufferSize, const char *buffer,
     return S3StatusOK;
 }
 
-struct put_info {
-    BlueSkyRCStr *val;
-    gint offset;
-};
-
 static int s3store_put_handler(int bufferSize, char *buffer,
                                void *callbackData)
 {
@@ -98,8 +67,15 @@ static void s3store_response_callback(S3Status status,
                                const S3ErrorDetails *errorDetails,
                                void *callbackData)
 {
+    struct get_info *info = (struct get_info *)callbackData;
+
     g_print("S3 operation complete, status=%s, now=%ld\n",
             S3_get_status_name(status), bluesky_now_hires());
+
+    if (status == 0) {
+        info->success = 1;
+    }
+
     if (errorDetails != NULL) {
         g_print("  Error message: %s\n", errorDetails->message);
     }
@@ -111,6 +87,7 @@ static BlueSkyRCStr *s3store_get(gpointer s, const gchar *key)
 
     struct get_info info;
     info.buf = g_string_new("");
+    info.success = 0;
 
     struct S3GetObjectHandler handler;
     handler.responseHandler.propertiesCallback = s3store_properties_callback;
@@ -121,8 +98,13 @@ static BlueSkyRCStr *s3store_get(gpointer s, const gchar *key)
     S3_get_object(&store->bucket, key, NULL, 0, 0, NULL,
                   &handler, &info);
 
+    if (!info.success) {
+        g_string_free(info.buf, TRUE);
+        return NULL;
+    }
+
     BlueSkyRCStr *raw, *decrypted;
-    raw = bluesky_string_new_from_string(info.buf);
+    raw = bluesky_string_new_from_gstring(info.buf);
     decrypted = bluesky_crypt_decrypt(raw, store->encryption_key);
     bluesky_string_unref(raw);
     return decrypted;
@@ -177,6 +159,36 @@ static void s3store_task(gpointer o, gpointer s)
     g_print("Finish task...\n");
 }
 
+static gpointer s3store_new()
+{
+    S3Store *store = g_new(S3Store, 1);
+    store->thread_pool = g_thread_pool_new(s3store_task, store, -1, FALSE,
+                                           NULL);
+    store->bucket.bucketName = "mvrable-bluesky";
+    store->bucket.protocol = S3ProtocolHTTP;
+    store->bucket.uriStyle = S3UriStylePath;
+    store->bucket.accessKeyId = getenv("AWS_ACCESS_KEY_ID");
+    store->bucket.secretAccessKey = getenv("AWS_SECRET_ACCESS_KEY");
+
+    const char *key = getenv("BLUESKY_KEY");
+    if (key == NULL) {
+        g_error("Encryption key not defined; please set BLUESKY_KEY environment variable");
+        exit(1);
+    }
+
+    bluesky_crypt_hash_key(key, store->encryption_key);
+
+    g_print("Initializing S3 with bucket %s, access key %s, encryption key %s\n",
+            store->bucket.bucketName, store->bucket.accessKeyId, key);
+
+    return store;
+}
+
+static void s3store_destroy(gpointer store)
+{
+    g_free(store);
+}
+
 static BlueSkyStoreImplementation store_impl = {
     .create = s3store_new,
     .destroy = s3store_destroy,