Fix a bug where at may be inside buf.
[vcfs.git] / mkfs.vc.c
CommitLineData
d5cf5351 1#include <stdlib.h>
2#include <stdio.h>
3#include <unistd.h>
4#include <fcntl.h>
5#include <time.h>
6#include <string.h>
7#include <errno.h>
8#include <sys/stat.h>
9
10#include "utils.h"
11#include "store.h"
12#include "blocktree.h"
13#include "vcfs.h"
14
15int main(int argc, char **argv)
16{
17 struct store *st;
18 struct inode root;
19 struct revrec frev;
20 struct dentry dots;
21 time_t now;
22 int fd;
23 char tbuf[1024];
24
25 if(argc < 2) {
26 fprintf(stderr, "usage; mkfs.vc DIR\n");
27 exit(1);
28 }
29 if(mkfstore(argv[1]))
30 exit(1);
31 if((st = newfstore(argv[1])) == NULL)
32 exit(1);
33
34 now = time(NULL);
35
36 memset(&dots, 0, sizeof(dots));
37 dots.inode = 0;
38
39 root.mode = S_IFDIR | 0755;
40 root.mtime = root.ctime = now;
41 root.size = 2;
42 root.uid = getuid();
43 root.gid = getgid();
44 root.links = 2;
45 root.data.d = 0;
46 root.xattr.d = 0;
47 strcpy(dots.name, ".");
34b0b353 48 if(btput(st, &root.data, 0, &dots, sizeof(dots) - sizeof(dots.name) + 2, DIRBLSIZE)) {
d5cf5351 49 fprintf(stderr, "mkfs.vc: could not create root directory entries: %s\n", strerror(errno));
50 exit(1);
51 }
52 strcpy(dots.name, "..");
34b0b353 53 if(btput(st, &root.data, 1, &dots, sizeof(dots) - sizeof(dots.name) + 3, DIRBLSIZE)) {
d5cf5351 54 fprintf(stderr, "mkfs.vc: could not create root directory entries: %s\n", strerror(errno));
55 exit(1);
56 }
57
58 frev.ct = now;
59 frev.root.d = 0;
34b0b353 60 if(btput(st, &frev.root, 0, &root, sizeof(root), INOBLSIZE)) {
d5cf5351 61 fprintf(stderr, "mkfs.vc: could not store root directory inode: %s\n", strerror(errno));
62 exit(1);
63 }
64 releasestore(st);
65
66 snprintf(tbuf, sizeof(tbuf), "%s/revs", argv[1]);
67 if((fd = open(tbuf, O_WRONLY | O_CREAT | O_EXCL, 0600)) < 0) {
68 fprintf(stderr, "mkfs.vc: could not create revision database: %s\n", strerror(errno));
69 exit(1);
70 }
71 if(writeall(fd, &frev, sizeof(frev), 0)) {
72 fprintf(stderr, "mkfs.vc: could not write initial revision: %s\n", strerror(errno));
73 exit(1);
74 }
75 fsync(fd);
76 close(fd);
77 return(0);
78}