Remove g_thread_init() calls.
[platform/upstream/evolution-data-server.git] / camel / camel-index-control.c
1
2 /* Command to manually force a compression/dump of an index file */
3
4 #ifdef HAVE_CONFIG_H
5 #include <config.h>
6 #endif
7
8 #include <ctype.h>
9 #include <errno.h>
10 #include <fcntl.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include <sys/stat.h>
16 #include <sys/types.h>
17
18 #include "camel-list-utils.h"
19 #include "camel-object.h"
20 #include "camel-text-index.h"
21
22 extern gint camel_init (const gchar *certdb_dir, gboolean nss_init);
23
24 G_GNUC_NORETURN static void
25 do_usage (gchar *argv0)
26 {
27         fprintf(stderr, "Usage: %s [ compress | dump | info ] file(s) ...\n", argv0);
28         fprintf(stderr, " compress - compress (an) index file(s)\n");
29         fprintf(stderr, " dump - dump (an) index file's content to stdout\n");
30         fprintf(stderr, " info - dump summary info to stdout\n");
31         exit (1);
32 }
33
34 static gint
35 do_compress (gint argc,
36              gchar **argv)
37 {
38         gint i;
39         CamelIndex *idx;
40
41         for (i = 2; i < argc; i++) {
42                 printf("Opening index file: %s\n", argv[i]);
43                 idx = (CamelIndex *) camel_text_index_new (argv[i], O_RDWR);
44                 if (idx) {
45                         printf(" Compressing ...\n");
46                         if (camel_index_compress (idx) == -1) {
47                                 g_object_unref (idx);
48                                 return 1;
49                         }
50                         g_object_unref (idx);
51                 } else {
52                         printf(" Failed: %s\n", g_strerror (errno));
53                         return 1;
54                 }
55         }
56         return 0;
57 }
58
59 static gint
60 do_dump (gint argc,
61          gchar **argv)
62 {
63         gint i;
64         CamelIndex *idx;
65
66         for (i = 2; i < argc; i++) {
67                 printf("Opening index file: %s\n", argv[i]);
68                 idx = (CamelIndex *) camel_text_index_new (argv[i], O_RDONLY);
69                 if (idx) {
70                         printf(" Dumping ...\n");
71                         camel_text_index_dump ((CamelTextIndex *) idx);
72                         g_object_unref (idx);
73                 } else {
74                         printf(" Failed: %s\n", g_strerror (errno));
75                         return 1;
76                 }
77         }
78         return 0;
79 }
80
81 static gint
82 do_info (gint argc,
83          gchar **argv)
84 {
85         gint i;
86         CamelIndex *idx;
87
88         for (i = 2; i < argc; i++) {
89                 printf("Opening index file: %s\n", argv[i]);
90                 idx = (CamelIndex *) camel_text_index_new (argv[i], O_RDONLY);
91                 if (idx) {
92                         camel_text_index_info ((CamelTextIndex *) idx);
93                         g_object_unref (idx);
94                 } else {
95                         printf(" Failed: %s\n", g_strerror (errno));
96                         return 0;
97                 }
98         }
99         return 1;
100 }
101
102 static gint
103 do_check (gint argc,
104           gchar **argv)
105 {
106         gint i;
107         CamelIndex *idx;
108
109         for (i = 2; i < argc; i++) {
110                 printf("Opening index file: %s\n", argv[i]);
111                 idx = (CamelIndex *) camel_text_index_new (argv[i], O_RDONLY);
112                 if (idx) {
113                         camel_text_index_validate ((CamelTextIndex *) idx);
114                         g_object_unref (idx);
115                 } else {
116                         printf(" Failed: %s\n", g_strerror (errno));
117                         return 0;
118                 }
119         }
120         return 1;
121 }
122
123 static gint do_perf (gint argc, gchar **argv);
124
125 gint main (gint argc, gchar **argv)
126 {
127         if (argc < 2)
128                 do_usage (argv[0]);
129
130         camel_init (NULL, 0);
131
132         if (!strcmp(argv[1], "compress"))
133                 return do_compress (argc, argv);
134         else if (!strcmp(argv[1], "dump"))
135                 return do_dump (argc, argv);
136         else if (!strcmp(argv[1], "info"))
137                 return do_info (argc, argv);
138         else if (!strcmp(argv[1], "check"))
139                 return do_check (argc, argv);
140         else if (!strcmp(argv[1], "perf"))
141                 return do_perf (argc, argv);
142
143         do_usage (argv[0]);
144         return 1;
145 }
146
147 #include <dirent.h>
148 #include "camel-stream-null.h"
149 #include "camel-stream-filter.h"
150 #include "camel-mime-filter-index.h"
151 #include "camel-stream-fs.h"
152
153 static gint
154 do_perf (gint argc,
155          gchar **argv)
156 {
157         CamelIndex *idx;
158         DIR *dir;
159         const gchar *path = "/home/notzed/evolution/local/Inbox/mbox/cur";
160         struct dirent *d;
161         CamelStream *null, *filter, *stream;
162         CamelMimeFilter *filter_index;
163         gchar *name;
164         CamelIndexName *idn;
165
166         dir = opendir (path);
167         if (dir == NULL) {
168                 perror("open dir");
169                 return 1;
170         }
171
172         idx = (CamelIndex *) camel_text_index_new (
173                 "/tmp/index", O_TRUNC|O_CREAT|O_RDWR);
174         if (idx == NULL) {
175                 perror("open index");
176                 closedir (dir);
177                 return 1;
178         }
179
180         null = camel_stream_null_new ();
181         filter = camel_stream_filter_new (null);
182         g_object_unref (null);
183         filter_index = camel_mime_filter_index_new (idx);
184         camel_stream_filter_add ((CamelStreamFilter *) filter, filter_index);
185
186         while ((d = readdir (dir))) {
187                 printf("indexing '%s'\n", d->d_name);
188
189                 idn = camel_index_add_name (idx, d->d_name);
190                 camel_mime_filter_index_set_name (
191                         CAMEL_MIME_FILTER_INDEX (filter_index), idn);
192                 name = g_strdup_printf("%s/%s", path, d->d_name);
193                 stream = camel_stream_fs_new_with_name (name, O_RDONLY, 0, NULL);
194                 camel_stream_write_to_stream (stream, filter, NULL, NULL);
195                 g_object_unref (stream);
196                 g_free (name);
197
198                 camel_index_write_name (idx, idn);
199                 g_object_unref (idn);
200                 camel_mime_filter_index_set_name (
201                         CAMEL_MIME_FILTER_INDEX (filter_index), NULL);
202         }
203
204         closedir (dir);
205
206         camel_index_sync (idx);
207         g_object_unref (idx);
208
209         g_object_unref (filter);
210         g_object_unref (filter_index);
211
212         return 0;
213 }