aboutsummaryrefslogtreecommitdiffstats
path: root/mkdbfs.c
blob: 618154a2c0b86b3c5bcded4f1779ae0c4d974e8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * Maintained by Jeff Garzik <jgarzik@pobox.com>
 *
 * Copyright 2006-2007 Red Hat, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <time.h>
#include <glib.h>
#include <db.h>
#include "dbfs.h"

static int make_root_dir(DB_TXN *txn)
{
	struct dbfs_inode *ino;
	guint64 curtime;
	int rc;

	/* allocate an empty inode */
	ino = g_new0(struct dbfs_inode, 1);
	ino->raw_ino_size = sizeof(struct dbfs_raw_inode);
	ino->raw_inode = malloc(ino->raw_ino_size);
	memset(ino->raw_inode, 0, ino->raw_ino_size);

	ino->raw_inode->ino = GUINT64_TO_LE(1);
	ino->raw_inode->mode = GUINT32_TO_LE(S_IFDIR | 0755);
	ino->raw_inode->nlink = GUINT32_TO_LE(2);
	curtime = GUINT64_TO_LE(time(NULL));
	ino->raw_inode->ctime = curtime;
	ino->raw_inode->atime = curtime;
	ino->raw_inode->mtime = curtime;

	rc = dbfs_inode_write(txn, ino);
	if (rc)
		goto err_die;

	rc = dbfs_dir_new(txn, 1, 1, ino);
	if (rc)
		goto err_die;

	dbfs_inode_free(ino);
	return 0;

err_die:
	errno = -rc;
	perror("dbfs_inode_write");
	return 1;
}

int main (int argc, char *argv[])
{
	struct dbfs *fs = dbfs_new();
	DB_TXN *txn;
	int pgm_rc = 0;

	gfs = fs;

	if (!fs)
		return 1;

	int rc = dbfs_open(fs, DB_CREATE, DB_CREATE, "mkdbfs", FALSE);
	if (rc) {
		perror("mkdbfs open");
		return 1;
	}

	rc = fs->env->txn_begin(fs->env, NULL, &txn, 0);
	if (rc) {
		perror("mkdbfs txn_begin");
		pgm_rc = 1;
		goto out;
	}

	rc = make_root_dir(txn);
	if (rc) {
		txn->abort(txn);
		pgm_rc = 1;
		goto out;
	}

	rc = txn->commit(txn, 0);
	if (rc) {
		perror("mkdbfs txn_commit");
		pgm_rc = 1;
		goto out;
	}

out:
	dbfs_close(fs);
	return pgm_rc;
}