1 /* $Id$ */
2 
3 /*
4  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/types.h>
20 
21 #include <string.h>
22 
23 #include "fdm.h"
24 
25 __dead void	cache_op_add(int, char **);
26 __dead void	cache_op_remove(int , char **);
27 __dead void	cache_op_list(int, char **);
28 __dead void	cache_op_dump(int, char **);
29 __dead void	cache_op_clear(int, char **);
30 
31 __dead void
cache_op(int argc,char ** argv)32 cache_op(int argc, char **argv)
33 {
34 	char	*cmd;
35 
36 	if (argc < 1)
37 		usage();
38 	cmd = argv[0];
39 	argc--;
40 	argv++;
41 
42 	if (strncmp(cmd, "add", strlen(cmd)) == 0)
43 		cache_op_add(argc, argv);
44 	if (strncmp(cmd, "remove", strlen(cmd)) == 0)
45 		cache_op_remove(argc, argv);
46 	if (strncmp(cmd, "list", strlen(cmd)) == 0)
47 		cache_op_list(argc, argv);
48 	if (strncmp(cmd, "dump", strlen(cmd)) == 0)
49 		cache_op_dump(argc, argv);
50 	if (strncmp(cmd, "clear", strlen(cmd)) == 0)
51 		cache_op_clear(argc, argv);
52 	usage();
53 }
54 
55 __dead void
cache_op_add(int argc,char ** argv)56 cache_op_add(int argc, char **argv)
57 {
58 	TDB_CONTEXT	*db;
59 
60 	if (argc != 2)
61 		usage();
62 
63 	if ((db = db_open(argv[0])) == NULL) {
64 		log_warn("%s", argv[0]);
65 		exit(1);
66 	}
67 
68 	if (db_add(db, argv[1]) != 0) {
69 		log_warnx("%s: cache error", argv[0]);
70 		exit(1);
71 	}
72 
73 	exit(0);
74 }
75 
76 __dead void
cache_op_remove(int argc,char ** argv)77 cache_op_remove(int argc, char **argv)
78 {
79 	TDB_CONTEXT	*db;
80 
81 	if (argc != 2)
82 		usage();
83 
84 	if ((db = db_open(argv[0])) == NULL) {
85 		log_warn("%s", argv[0]);
86 		exit(1);
87 	}
88 
89 	if (!db_contains(db, argv[1])) {
90 		log_warnx("%s: key not found: %s", argv[0], argv[1]);
91 		exit(1);
92 	}
93 	if (db_remove(db, argv[1]) != 0) {
94 		log_warnx("%s: cache error", argv[0]);
95 		exit(1);
96 	}
97 
98 	exit(0);
99 }
100 
101 __dead void
cache_op_list(int argc,char ** argv)102 cache_op_list(int argc, char **argv)
103 {
104 	struct cache	*cache;
105 	TDB_CONTEXT	*db;
106 
107 	if (argc == 1) {
108 		if ((db = db_open(argv[0])) == NULL) {
109 			log_warn("%s", argv[0]);
110 			exit(1);
111 		}
112 
113 		log_info("%s: %u keys", argv[0], db_size(db));
114 
115 		db_close(db);
116 		exit(0);
117 	}
118 
119 	if (argc != 0)
120 		usage();
121 
122 	TAILQ_FOREACH(cache, &conf.caches, entry) {
123 		if ((cache->db = db_open(cache->path)) == NULL) {
124 			log_warn("%s", cache->path);
125 			exit(1);
126 		}
127 
128 		log_info("%s: %u keys", cache->path, db_size(cache->db));
129 
130 		db_close(cache->db);
131 	}
132 
133 	exit(0);
134 }
135 
136 __dead void
cache_op_dump(int argc,char ** argv)137 cache_op_dump(int argc, char **argv)
138 {
139 	TDB_CONTEXT	*db;
140 
141 	if (argc != 1)
142 		usage();
143 
144 	if ((db = db_open(argv[0])) == NULL) {
145 		log_warn("%s", argv[0]);
146 		exit(1);
147 	}
148 
149 	if (db_print(db, log_info) != 0) {
150 		log_warnx("%s: cache error", argv[0]);
151 		exit(1);
152 	}
153 
154 	exit(0);
155 }
156 
157 __dead void
cache_op_clear(int argc,char ** argv)158 cache_op_clear(int argc, char **argv)
159 {
160 	TDB_CONTEXT	*db;
161 
162 	if (argc != 1)
163 		usage();
164 
165 	if ((db = db_open(argv[0])) == NULL) {
166 		log_warn("%s", argv[0]);
167 		exit(1);
168 	}
169 
170 	if (db_clear(db) != 0) {
171 		log_warnx("%s: cache error", argv[0]);
172 		exit(1);
173 	}
174 
175 	exit(0);
176 }
177 
178