2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.1 1995-08-31 14:50:24 adam
8 * New simple file index tool.
16 #include <sys/types.h>
25 static int key_fd = -1;
26 #define KEY_BUF_SIZE 100000
31 static char *base_path = NULL;
33 void key_open (const char *fname)
38 if ((key_fd = open (fname, O_RDWR|O_CREAT, 0666)) == -1)
40 log (LOG_FATAL|LOG_ERRNO, "Creat %s", fname);
43 if (!(key_buf = malloc (KEY_BUF_SIZE)))
45 log (LOG_FATAL|LOG_ERRNO, "malloc");
49 if (!(file_idx = dict_open ("fileidx", 10, 1)))
51 log (LOG_FATAL, "dict_open fail of %s", "fileidx");
54 file_key = dict_lookup (file_idx, ".");
56 memcpy (&sysno_next, (char*)file_key+1, sizeof(sysno_next));
66 dict_insert (file_idx, ".", sizeof(sysno_next), &sysno_next);
67 dict_close (file_idx);
76 while (i < key_offset)
78 w = write (key_fd, key_buf + i, key_offset - i);
81 log (LOG_FATAL|LOG_ERRNO, "Write key fail");
89 void key_write (int cmd, struct it_key *k, const char *str)
92 size_t slen = strlen(str);
94 if (key_offset + sizeof(*k) + slen >= KEY_BUF_SIZE - 2)
96 memcpy (key_buf + key_offset, &x, 1);
98 memcpy (key_buf + key_offset, k, sizeof(*k));
99 key_offset += sizeof(*k);
100 memcpy (key_buf + key_offset, str, slen+1);
101 key_offset += slen+1;
104 void text_extract (SYSNO sysno, int cmd, const char *fname)
112 log (LOG_DEBUG, "Text extract of %d", sysno);
114 inf = fopen (fname, "r");
117 log (LOG_WARN|LOG_ERRNO, "open %s", fname);
120 while ((c=getc (inf)) != EOF)
123 while (i < 254 && c != EOF && isalnum(c))
134 key_write (cmd, &k, w);
142 void file_extract (int cmd, struct stat *fs, const char *fname)
148 const char *file_type;
151 log (LOG_DEBUG, "%c %s", cmd, fname);
152 for (i = strlen(fname); --i >= 0; )
158 else if (fname[i] == '.')
160 strcpy (ext, fname+i+1);
163 sprintf (ext_res, "fileExtension.%s", ext);
164 if (!(file_type = res_get (common_resource, ext_res)))
167 file_info = dict_lookup (file_idx, fname);
170 sysno = sysno_next++;
171 dict_insert (file_idx, fname, sizeof(sysno), &sysno);
174 memcpy (&sysno, (char*) file_info+1, sizeof(sysno));
175 if (!strcmp (file_type, "text"))
176 text_extract (sysno, cmd, fname);
179 static void repository_extract_r (int cmd, char *rep)
186 rep_len = strlen(rep);
192 log (LOG_WARN|LOG_ERRNO, "opendir %s", rep);
195 log (LOG_FATAL|LOG_ERRNO, "opendir %s", rep);
198 if (rep[rep_len-1] != '/')
202 while ((dent = readdir (dir)))
204 strcpy (rep +rep_len+1, dent->d_name);
206 switch (fs.st_mode & S_IFMT)
209 file_extract (cmd, &fs, rep);
212 if (strcmp (dent->d_name, ".") && strcmp(dent->d_name, ".."))
213 repository_extract_r (cmd, rep);
220 void repository_update_r (int cmd, const char *rep, const char *with_rep)
225 void repository_traverse (int cmd, const char *rep)
230 strcpy (rep_tmp1, rep);
233 strcpy (rep_tmp2, base_path);
234 repository_update_r (cmd, rep_tmp1, rep_tmp2);
237 repository_extract_r (cmd, rep_tmp1);
241 int main (int argc, char **argv)
249 while ((ret = options ("b:v:", argv, argc, &arg)) != -2)
257 common_resource = res_open (base_name);
258 if (!common_resource)
260 log (LOG_FATAL, "Cannot open resource `%s'", base_name);
264 else if(cmd == 0) /* command */
266 if (!strcmp (arg, "add"))
270 else if (!strcmp (arg, "del"))
276 log (LOG_FATAL, "Unknown command: %s", arg);
282 key_open ("keys.tmp");
283 repository_traverse (cmd, arg);
289 log_init (log_mask_str(arg), prog, NULL);
297 log (LOG_FATAL, "Unknown option '-%s'", arg);