]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | Copyright (C) 2010-2012 Proxmox Server Solutions GmbH | |
3 | ||
4 | This program is free software: you can redistribute it and/or modify | |
5 | it under the terms of the GNU Affero General Public License as published by | |
6 | the Free Software Foundation, either version 3 of the License, or | |
7 | (at your option) any later version. | |
8 | ||
9 | This program is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | GNU Affero General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU Affero General Public License | |
15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | ||
17 | Author: Dietmar Maurer <dietmar@proxmox.com> | |
18 | ||
19 | Note: we use this with the installer to create the initial db | |
20 | without starting pmxcfs/fuse | |
21 | */ | |
22 | ||
23 | #include <stdio.h> | |
24 | #include <stdlib.h> | |
25 | #include <glib.h> | |
26 | #include <errno.h> | |
27 | #include <string.h> | |
28 | #include <check.h> | |
29 | #include <errno.h> | |
30 | #include <unistd.h> | |
31 | #include <sys/types.h> | |
32 | #include <dirent.h> | |
33 | ||
34 | #include "cfs-utils.h" | |
35 | #include "status.h" | |
36 | #include "memdb.h" | |
37 | ||
38 | cfs_t cfs = { | |
39 | .debug = 0, | |
40 | .nodename = "dummy", | |
41 | }; | |
42 | ||
43 | static memdb_t *memdb; | |
44 | ||
45 | static void | |
46 | usage_error(void) | |
47 | { | |
48 | fprintf(stderr, "Usage: create_pmxcfs_db /a/dir /a/filename.db\n"); | |
49 | exit(-1); | |
50 | } | |
51 | ||
52 | int | |
53 | main(int argc, char *argv[]) | |
54 | { | |
55 | cfs_status_init(); | |
56 | ||
57 | if (argc != 3) { | |
58 | usage_error(); | |
59 | } | |
60 | ||
61 | const char *dir_name = argv[1]; | |
62 | const char *dbfile = argv[2]; | |
63 | ||
64 | DIR *dh = opendir(dir_name); | |
65 | if (!dh) { | |
66 | perror("unable to open dir"); | |
67 | exit(-1); | |
68 | } | |
69 | memdb = memdb_open(dbfile); | |
70 | ||
71 | struct dirent *de; | |
72 | time_t ctime = time(NULL); | |
73 | ||
74 | while((de = readdir(dh))) { | |
75 | if (de->d_type != DT_REG) { | |
76 | continue; | |
77 | } | |
78 | ||
79 | char *cdata = NULL; | |
80 | gsize clen = 0; | |
81 | char *fn = g_strdup_printf("%s/%s", dir_name, de->d_name); | |
82 | if (g_file_get_contents(fn, &cdata, &clen, NULL)) { | |
83 | //printf("FOUND %ld %s\n", clen, fn); | |
84 | if (memdb_create(memdb, de->d_name, 0, ctime) != 0) { | |
85 | fprintf(stderr, "memdb_create '%s' failed\n", de->d_name); | |
86 | exit(-1); | |
87 | } | |
88 | if (memdb_write(memdb, de->d_name, 0, ctime, cdata, clen, 0, 1) != clen) { | |
89 | fprintf(stderr, "memdb_write '%s' failed\n", de->d_name); | |
90 | exit(-1); | |
91 | } | |
92 | ||
93 | } | |
94 | g_free(fn); | |
95 | } | |
96 | ||
97 | memdb_close(memdb); | |
98 | ||
99 | closedir(dh); | |
100 | } |