- if (st.st_size == 0) {
- char *buf = (char *) malloc(sizeof(struct meta));
- memset(buf, 0, sizeof(struct meta));
- write(blerg->meta_fd, buf, sizeof(struct meta));
+ if (st.st_size < sizeof(struct meta)) {
+ // Fill the difference in size between sizeof(struct meta) and
+ // the file size with nulls. This allows seamless upgrades as
+ // long as struct meta only adds members.
+ int len = sizeof(struct meta) - st.st_size;
+ char *buf = (char *) malloc(len);
+ memset(buf, 0, len);
+ int tmpfd = dup(blerg->meta_fd);
+ FILE* tmp = fdopen(tmpfd, "a");
+ fwrite(buf, len, 1, tmp);
+ fclose(tmp);