Make printf format specifiers 32/64-bit clean.
[bluesky.git] / bluesky / serialize.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 <inttypes.h>
11 #include <glib.h>
12 #include <string.h>
13
14 #include "bluesky.h"
15
16 /* Serialization of in-memory filesystem data structures to bytestrings which
17  * can be written to persistent storage.  All data is stored in little-endian
18  * format. */
19
20 /* Magic signature for serialized inodes. */
21
22 #define INODE_MAGIC 0xa6832100943d71e6ULL
23
24 struct serialized_inode {
25     uint64_t signature;         /* INODE_MAGIC */
26     int32_t type;
27     uint32_t mode;
28     uint32_t uid, gid;
29     uint32_t nlink;
30     uint64_t inum;
31     uint64_t change_count;
32     int64_t atime;
33     int64_t ctime;
34     int64_t mtime;
35     int64_t ntime;
36 } __attribute__((packed));
37
38 void bluesky_serialize_inode(GString *out, BlueSkyInode *inode)
39 {
40     struct serialized_inode buf;
41
42     buf.signature = GUINT64_TO_LE(INODE_MAGIC);
43     buf.type = GUINT32_TO_LE(inode->type);
44     buf.mode = GUINT32_TO_LE(inode->mode);
45     buf.uid = GUINT32_TO_LE(inode->uid);
46     buf.gid = GUINT32_TO_LE(inode->gid);
47     buf.nlink = GUINT32_TO_LE(inode->nlink);
48     buf.inum = GUINT64_TO_LE(inode->inum);
49     buf.change_count = GUINT64_TO_LE(inode->change_count);
50     buf.atime = GINT64_TO_LE(inode->atime);
51     buf.ctime = GINT64_TO_LE(inode->ctime);
52     buf.mtime = GINT64_TO_LE(inode->mtime);
53     buf.ntime = GINT64_TO_LE(inode->ntime);
54
55     g_string_append_len(out, (gchar *)&buf, sizeof(buf));
56
57     switch (inode->type) {
58     case BLUESKY_REGULAR:
59     {
60         uint64_t size = GUINT64_TO_LE(inode->size);
61         g_string_append_len(out, (gchar *)&size, sizeof(uint64_t));
62         for (int i = 0; i < inode->blocks->len; i++) {
63             BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
64             if (b->ref != NULL)
65                 g_string_append(out, b->ref);
66             g_string_append_c(out, '\0');
67         }
68         break;
69     }
70
71     case BLUESKY_DIRECTORY:
72     {
73         uint32_t seq;
74         uint64_t inum;
75         GSequenceIter *i = g_sequence_get_begin_iter(inode->dirents);
76
77         while (!g_sequence_iter_is_end(i)) {
78             BlueSkyDirent *d = g_sequence_get(i);
79
80             seq = GUINT32_TO_LE(d->cookie);
81             inum = GUINT64_TO_LE(d->inum);
82             g_string_append_len(out, (gchar *)&seq, sizeof(uint32_t));
83             g_string_append_len(out, (gchar *)&inum, sizeof(uint64_t));
84             g_string_append(out, d->name);
85             g_string_append_c(out, '\0');
86
87             i = g_sequence_iter_next(i);
88         }
89
90         seq = GUINT32_TO_LE(0);
91         g_string_append_len(out, (gchar *)&seq, sizeof(uint32_t));
92
93         break;
94     }
95     default:
96         g_warning("Serialization for inode type %d not implemented!\n",
97                   inode->type);
98     }
99 }
100
101 BlueSkyInode *bluesky_deserialize_inode(BlueSkyFS *fs, const gchar *buf)
102 {
103     struct serialized_inode *raw = (struct serialized_inode *)buf;
104
105     if (GUINT64_FROM_LE(raw->signature) != INODE_MAGIC)
106         return NULL;
107
108     BlueSkyInode *inode = bluesky_new_inode(GUINT64_FROM_LE(raw->inum),
109                                             fs, GUINT32_FROM_LE(raw->type));
110
111     inode->mode = GUINT32_FROM_LE(raw->mode);
112     inode->uid = GUINT32_FROM_LE(raw->uid);
113     inode->gid = GUINT32_FROM_LE(raw->gid);
114     inode->nlink = GUINT32_FROM_LE(raw->nlink);
115     inode->change_count = GUINT64_FROM_LE(raw->change_count);
116     inode->atime = GINT64_FROM_LE(raw->atime);
117     inode->ctime = GINT64_FROM_LE(raw->ctime);
118     inode->mtime = GINT64_FROM_LE(raw->mtime);
119     inode->ntime = GINT64_FROM_LE(raw->ntime);
120
121     buf += sizeof(struct serialized_inode);
122
123     g_print("Deserializing inode %lld...\n", (long long)inode->inum);
124
125     /* TODO: Bounds checking */
126     switch (inode->type) {
127     case BLUESKY_REGULAR:
128         inode->size = GINT64_FROM_LE(*(uint64_t *)buf);
129         buf += sizeof(uint64_t);
130         g_array_set_size(inode->blocks,
131                          (inode->size + BLUESKY_BLOCK_SIZE - 1)
132                           / BLUESKY_BLOCK_SIZE);
133         for (int i = 0; i < inode->blocks->len; i++) {
134             BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
135             b->type = BLUESKY_BLOCK_REF;
136             b->ref = g_strdup(buf);
137             buf += strlen(b->ref) + 1;
138         }
139         break;
140
141     case BLUESKY_DIRECTORY:
142     {
143         struct serialized_dirent {
144             uint32_t seq;
145             uint64_t inum;
146             gchar name[0];
147         } __attribute__((packed));
148
149         struct serialized_dirent *d = (struct serialized_dirent *)buf;
150         while (GUINT32_FROM_LE(d->seq) != 0) {
151             BlueSkyDirent *dirent = g_new(BlueSkyDirent, 1);
152             dirent->cookie = GUINT64_FROM_LE(d->seq);
153             dirent->inum = GUINT64_FROM_LE(d->inum);
154             dirent->name = g_strdup(d->name);
155
156             g_sequence_insert_sorted(inode->dirents, dirent,
157                                      bluesky_dirent_compare, NULL);
158             g_hash_table_insert(inode->dirhash, dirent->name, dirent);
159
160             g_print("  dirent[%08x]: %s -> %"PRIu64"\n",
161                     dirent->cookie, dirent->name, dirent->inum);
162
163             buf = strchr(d->name, '\0') + 1;
164             d = (struct serialized_dirent *)buf;
165         }
166         break;
167     }
168     default:
169         g_warning("Deserialization for inode type %d not implemented!\n",
170                   inode->type);
171     }
172
173     return inode;
174 }