2 * Copyright (C) 2009 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
8 * \file osltar.c A simple file archiver with many limitations. Do not use it.
9 * It is just an * example to illustrate programming with libosl.
17 #include <sys/types.h>
20 #include <sys/types.h>
26 /** The columns of the osl table. */
38 * \cond We want doxygen to create documentation for certain static functions.
39 * So we use __static__ for such functions.
41 #define __static__ static
45 * Compare two osl objects of string type.
47 * \param obj1 Pointer to the first object.
48 * \param obj2 Pointer to the second object.
50 * \return It returns an integer less than, equal to, or greater than zero if
51 * \a obj1 is found, respectively, to be less than, to match, or be greater than
54 * \sa strcmp(3), strncmp(3), osl_compare_func.
56 __static__ int string_compare(const struct osl_object *obj1, const struct osl_object *obj2)
58 const char *str1 = (const char *)obj1->data;
59 const char *str2 = (const char *)obj2->data;
60 return strcmp(str1, str2);
63 static struct osl_column_description tar_table_cols[] = {
65 .storage_type = OSL_MAPPED_STORAGE,
66 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
68 .compare_function = string_compare,
71 .storage_type = OSL_MAPPED_STORAGE,
77 static struct osl_table *table;
79 static struct osl_table_description tar_table_desc = {
81 .num_columns = NUM_OT_COLUMNS,
83 .column_descriptions = tar_table_cols,
86 static void print_usage_and_die(void)
88 fprintf(stderr, "usage:\n\tosltar c <db_dir> <data_dir>\n");
89 fprintf(stderr, "\tosltar x <db_dir> file1 [file2...]\n");
90 fprintf(stderr, "\tosltar t <db_dir>\n");
95 * Open a file and map it into memory.
97 static int mmap_full_file(const char *path, void **map,
100 int fd, ret, mmap_prot, mmap_flags;
101 struct stat file_status;
103 mmap_prot = PROT_READ;
104 mmap_flags = MAP_PRIVATE;
105 ret = open(path, O_RDONLY, 0);
107 fprintf(stderr, "open: %s\n", strerror(errno));
111 if (fstat(fd, &file_status) < 0) {
113 fprintf(stderr, "fstat: %s\n", strerror(errno));
116 *size = file_status.st_size;
118 fprintf(stderr, "can not add empty file: %s\n", path);
121 *map = mmap(NULL, *size, mmap_prot, mmap_flags, fd, 0);
122 if (*map == MAP_FAILED) {
123 fprintf(stderr, "map failed: %s\n", path);
134 static int add_file(char *name)
137 struct osl_object objs[NUM_OT_COLUMNS] = {
140 .size = strlen(name) + 1
143 printf("%s\n", name);
144 ret = mmap_full_file(name, &objs[OTC_DATA].data,
145 &objs[OTC_DATA].size);
148 return osl_add_row(table, objs);
151 static int populate_table(const char *dirname)
153 struct dirent *entry;
155 int ret = chdir(dirname);
158 fprintf(stderr, "chdir: %s\n", strerror(errno));
163 fprintf(stderr, "opendir: %s\n", strerror(errno));
166 while ((entry = readdir(dir))) {
170 if (!strcmp(entry->d_name, "."))
172 if (!strcmp(entry->d_name, ".."))
174 if (lstat(entry->d_name, &s) == -1)
179 ret = add_file(entry->d_name);
189 static int com_create(int argc, char **argv)
195 print_usage_and_die();
196 if (lstat(argv[2], &statbuf) == -1) {
197 fprintf(stderr, "no such dir: %s\n", argv[2]);
200 if (!S_ISDIR(statbuf.st_mode)) {
201 fprintf(stderr, "not a dir: %s\n", argv[2]);
204 tar_table_desc.dir = argv[1];
205 ret = osl_create_table(&tar_table_desc);
207 fprintf(stderr, "failed to create table\n");
211 ret = osl_open_table(&tar_table_desc, &table);
213 fprintf(stderr, "osl_open_table: %s\n", osl_strerror(-ret));
216 ret = populate_table(argv[2]);
217 osl_close_table(table, OSL_MARK_CLEAN);
218 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
222 * Write the whole buffer to a file descriptor.
224 static ssize_t write_all(int fd, const void *buf, size_t size)
228 ssize_t ret = write(fd, b, size);
230 fprintf(stderr, "open: %s\n", strerror(errno));
241 * Open a file, write the given buffer and close the file.
243 static int write_file(const char *filename, const void *buf, size_t size)
247 ret = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0644);
249 fprintf(stderr, "open: %s\n", strerror(errno));
254 printf("%s\n", filename);
255 ret = write_all(fd, buf, size);
264 static int extract_file(char *name)
268 struct osl_object obj = {.data = name, .size = strlen(name) + 1};
270 ret = osl_get_row(table, OTC_NAME, &obj, &row);
272 fprintf(stderr, "osl_get_row(%s): %s\n", name,
276 ret = osl_get_object(table, row, OTC_DATA, &obj);
278 fprintf(stderr, "osl_get_object: %s\n", osl_strerror(-ret));
281 return write_file(name, obj.data, obj.size);
284 static int com_extract(int argc, char **argv)
289 print_usage_and_die();
290 tar_table_desc.dir = argv[1];
291 ret = osl_open_table(&tar_table_desc, &table);
293 fprintf(stderr, "osl_open_table: %s\n", osl_strerror(-ret));
297 for (i = 2; i < argc; i++) {
298 ret = extract_file(argv[i]);
303 osl_close_table(table, OSL_MARK_CLEAN);
307 static int list_entry(struct osl_row *row, void *data)
309 struct osl_object obj;
310 int ret = osl_get_object(table, row, OTC_NAME, &obj);
312 fprintf(stderr, "osl_get_object: %s\n", osl_strerror(-ret));
315 printf("%s\n", (char *)obj.data);
319 static int com_list(int argc, char **argv)
324 print_usage_and_die();
325 tar_table_desc.dir = argv[1];
326 ret = osl_open_table(&tar_table_desc, &table);
328 fprintf(stderr, "osl_open_table: %s\n", osl_strerror(-ret));
332 ret = osl_rbtree_loop(table, OTC_NAME, NULL, list_entry);
333 osl_close_table(table, OSL_MARK_CLEAN);
338 * The osltar main funcion.
340 * \param argc Usual argument count.
341 * \param argv Usual argument vector.
343 int main(int argc, char **argv)
346 print_usage_and_die();
347 if (!strcmp(argv[1], "c"))
348 return com_create(argc - 1, argv + 1);
349 if (!strcmp(argv[1], "x"))
350 return com_extract(argc - 1, argv + 1);
351 if (!strcmp(argv[1], "t"))
352 return com_list(argc - 1, argv + 1);
353 print_usage_and_die();