+/* A specialized function for loading an object from the cloud, used just by
+ * the cleaner. When the cleaner runs, it will produce new objects that have
+ * the same object ID as before, differing only in the outgoing link locations
+ * as a result of cleaning. We can't use the normal functions for loading
+ * objects since those can only deal with one version of an object. Instead,
+ * we load the cleaned object with this function, and then merge the state into
+ * the official object as needed.
+ *
+ * The bluesky_cleaner_deserialize function is like
+ * bluesky_deserialize_cloudlog, but does a more limited deserialization and
+ * keeps the returned item entirely separate from any in-memory BlueSkyCloudLog
+ * objects. */
+
+typedef struct {
+ BlueSkyCloudLogType type;
+ BlueSkyCloudID id;
+ BlueSkyCloudPointer location;
+ uint64_t inum;
+ GArray *links;
+} BlueSkyCleanerItem;
+
+typedef struct {
+ BlueSkyCloudID id;
+ BlueSkyCloudPointer location;
+} BlueSkyCleanerLink;
+
+static BlueSkyCleanerItem *bluesky_cleaner_deserialize(BlueSkyRCStr *raw)
+{
+ const char *data = raw->data;
+ size_t len = raw->len;
+ const char *data1, *data2, *data3;
+ size_t len1, len2, len3;
+
+ g_assert(len > 4);
+ if (len < sizeof(struct cloudlog_header)
+ || memcmp(data, CLOUDLOG_MAGIC, 4) != 0)
+ {
+ g_warning("Deserializing garbage cloud log item from cleaner!");
+ return NULL;
+ };
+
+ struct cloudlog_header *header = (struct cloudlog_header *)data;
+ len1 = GUINT32_FROM_LE(header->size1);
+ len2 = GUINT32_FROM_LE(header->size2);
+ len3 = GUINT32_FROM_LE(header->size3);
+ data1 = data + sizeof(struct cloudlog_header);
+ data2 = data1 + len1;
+ data3 = data2 + len2;
+ g_assert(data3 + len3 - data <= len);
+
+ BlueSkyCleanerItem *item = g_new0(BlueSkyCleanerItem, 1);
+ item->type = header->type - '0';
+ item->inum = GUINT64_FROM_LE(header->inum);
+ memcpy(&item->id, &header->id, sizeof(BlueSkyCloudID));
+
+ int link_count = len2 / sizeof(BlueSkyCloudID);
+ g_print("Outgoing links: %d\n", link_count);
+ item->links = g_array_new(FALSE, TRUE, sizeof(BlueSkyCleanerLink));
+ for (int i = 0; i < link_count; i++) {
+ BlueSkyCleanerLink link;
+
+ g_assert(len2 >= sizeof(link.id));
+ memcpy(&link.id, data2, sizeof(link.id));
+ data2 += sizeof(link.id); len2 -= sizeof(link.id);
+
+ g_assert(len3 >= sizeof(link.location));
+ memcpy(&link.location, data3, sizeof(link.location));
+ data3 += sizeof(link.location); len3 -= sizeof(link.location);
+
+ g_array_append_val(item->links, link);
+ }
+
+ return item;
+}
+
+static void bluesky_cleaner_item_free(BlueSkyCleanerItem *item)
+{
+ if (item == NULL)
+ return;
+ g_array_unref(item->links);
+ g_free(item);
+}
+