#include #include #include #include #include static bool delete_only = false; bool template_insert(DB *dbp, char *path, char *template) { int r = 0; DBT key, data; memset(&key, 0, sizeof(DBT)); memset(&data, 0, sizeof(DBT)); key.data = path; key.size = strlen(path)+1; dbp->del(dbp, NULL, &key, 0); if (delete_only) return true; data.data = template; data.size = strlen(template)+1; if ((r = dbp->put(dbp, NULL, &key, &data, 0)) != 0) { dbp->err(dbp, r, "failed to insert template"); return false; } return true; } #define MAX_TEMPLATE_SIZE 32768 int main(int argc, char **argv) { evapp_db *db = NULL; evapp_ctx *ctx = evapp_new("boiler", NULL); bool result = false; FILE *fp; char tmpl[MAX_TEMPLATE_SIZE+1]; size_t size = 0; if (argc != 4) { fprintf(stderr, "Usage:\n%s /path/to/db uri template.cs\n", argv[0]); return 1; } if (strcmp(argv[3], "delete")) { fp = fopen(argv[3], "r"); if (!fp) { fprintf(stderr, "[main] failed to open template file: %s\n", argv[3]); return 1; } /* XXX: add warnings if the template size is too large */ memset(tmpl, 0, sizeof(tmpl)); if ((size = fread(tmpl, 1, sizeof(tmpl), fp)) == 0) { fprintf(stderr, "[main] template file '%s' appears empty\n", argv[3]); return 1; } fclose(fp); tmpl[size] = '\0'; fprintf(stdout, "[main] read %zu bytes\n", size); } if (ctx == NULL) { fprintf(stderr, "[main] failed to initialize evapp context.\n"); return 1; } if (!evapp_dbenv_init(ctx, argv[1])) { fprintf(stderr, "[main] failed to initialize database\n"); return 1; } if (!evapp_db_add(ctx, "templates", "templates", NULL, NULL, NULL, NULL, NULL, NULL, NULL)) { /* not marshalled */ fprintf(stderr, "[main] failed to add the templates table\n"); return 1; } db = evapp_db_select(ctx, "templates"); result = template_insert(db->p, argv[2], tmpl); evapp_destroy(ctx); return 0; }