10 #include <ashd/utils.h>
15 static struct charvbuf files;
16 static int verbose = 0;
18 static void opendb(char *path)
23 envpath = strdup(path);
24 if((p = strrchr(envpath, '/')) == NULL) {
26 envpath = strdup(".");
30 if((ret = db_env_create(&env, 0)) != 0) {
31 fprintf(stderr, "statdbput: could not create db environment handle: %s\n", db_strerror(ret));
34 if((ret = env->open(env, envpath, DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN, 0666)) != 0) {
35 fprintf(stderr, "statdbput: environment %s: %s\n", envpath, db_strerror(ret));
38 env->set_lk_detect(env, DB_LOCK_RANDOM);
39 if((ret = db_create(&db, env, 0)) != 0) {
40 fprintf(stderr, "statdbput: could not create db handle: %s\n", db_strerror(ret));
43 if((ret = db->open(db, NULL, path, NULL, DB_HASH, DB_AUTO_COMMIT | DB_CREATE, 0666)) != 0) {
44 fprintf(stderr, "statdbput: %s: %s\n", path, db_strerror(ret));
49 static void bufcatbe(struct charbuf *buf, intmax_t num, int nb)
53 bufcatbe(buf, num >> 8, nb - 1);
54 bufadd(*buf, (uint8_t)(num & 0xff));
57 static int dofile2(int fd, char *name, time_t mtime, char *ctype)
65 bufcatbe(&buf, mtime, 8);
66 bufcatstr2(buf, ctype);
68 sizebuf(buf, buf.d + 4096);
69 if((ret = read(fd, buf.b + buf.d, buf.s - buf.d)) < 0) {
70 fprintf(stderr, "statdbput: %s: %s\n", name, strerror(errno));
78 k = (DBT){.data = name, .size = strlen(name)};
79 v = (DBT){.data = buf.b, .size = buf.d};
80 ret = db->put(db, txn, &k, &v, 0);
83 if(ret == DB_LOCK_DEADLOCK)
85 fprintf(stderr, "statdbput: %s: %s\n", name, db_strerror(ret));
89 fprintf(stderr, "put: %s\n", name);
94 static int dofile(char *path, char *name, char *ctype)
100 if((fd = open(path, O_RDONLY)) < 0) {
101 fprintf(stderr, "statdbput: %s: %s\n", path, strerror(errno));
105 fprintf(stderr, "statdbput: %s: %s\n", path, strerror(errno));
110 if((p = strrchr(path, '/')) != NULL)
115 ret = dofile2(fd, name, sb.st_mtime, ctype);
118 bufadd(files, sstrdup(path));
122 static int dodir(char *path, char *ctype)
128 struct charbuf fnbuf;
130 if((dir = opendir(path)) == NULL) {
131 fprintf(stderr, "statdbput: %s: %s\n", path, strerror(errno));
136 while((dent = readdir(dir)) != NULL) {
138 bprintf(&fnbuf, "%s/%s", path, dent->d_name);
140 if(stat(fnbuf.b, &sb)) {
141 fprintf(stderr, "statdbput: %s: %s\n", fnbuf.b, strerror(errno));
145 if(S_ISREG(sb.st_mode)) {
146 ret = dofile(fnbuf.b, NULL, ctype);
158 static void usage(FILE *out)
160 fprintf(out, "usage: statdbput [-hvD] [-n NAME] DB CONTENT-TYPE {FILE|-}...\n");
161 fprintf(out, " statdbput [-hvD] [-d] DB CONTENT-TYPE DIR...\n");
164 int main(int argc, char **argv)
168 char *name, *ctype, *dbpath;
172 while((c = getopt(argc, argv, "+hvDdn:")) >= 0) {
194 if(optind > argc - 3) {
198 dbpath = argv[optind++];
199 ctype = argv[optind++];
202 if((ret = env->txn_begin(env, NULL, &txn, 0)) != 0) {
203 fprintf(stderr, "statdbput: could not begin transaction in %s: %s\n", dbpath, db_strerror(ret));
209 for(a = optind; a < argc; a++) {
210 ret = dodir(argv[a], ctype);
217 for(a = optind; a < argc; a++) {
218 if(!strcmp(argv[a], "-")) {
220 fprintf(stderr, "statdbput: must give -n when putting stdin\n");
223 ret = dofile2(0, name, time(NULL), ctype);
226 ret = dofile(argv[a], name, ctype);
236 fprintf(stderr, "deadlocked, restarting and trying again\n");
237 for(i = 0; i < files.d; i++)
245 if((ret = txn->commit(txn, 0)) != 0) {
246 fprintf(stderr, "statdbput: could not commit transaction in %s: %s\n", dbpath, db_strerror(ret));
250 for(i = 0; i < files.d; i++) {
252 fprintf(stderr, "unlink %s\n", files.b[i]);
253 if(unlink(files.b[i]))
254 fprintf(stderr, "statdbput: unlink %s: %s\n", files.b[i], strerror(errno));