2 * Copyright (C) 2009 Andre Noll <maan@systemlinux.org>
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 * We want doxygen to create documentation for certain static functions. So we
39 * use __static__ for such functions.
41 #define __static__ static
44 * Compare two osl objects of string type.
46 * \param obj1 Pointer to the first object.
47 * \param obj2 Pointer to the second object.
49 * \return It returns an integer less than, equal to, or greater than zero if
50 * \a obj1 is found, respectively, to be less than, to match, or be greater than
53 * \sa strcmp(3), strncmp(3), osl_compare_func.
55 __static__ int string_compare(const struct osl_object *obj1, const struct osl_object *obj2)
57 const char *str1 = (const char *)obj1->data;
58 const char *str2 = (const char *)obj2->data;
59 return strcmp(str1, str2);
62 static struct osl_column_description tar_table_cols[] = {
64 .storage_type = OSL_MAPPED_STORAGE,
65 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
67 .compare_function = string_compare,
70 .storage_type = OSL_MAPPED_STORAGE,
76 static struct osl_table *table;
78 static struct osl_table_description tar_table_desc = {
80 .num_columns = NUM_OT_COLUMNS,
82 .column_descriptions = tar_table_cols,
85 static void print_usage_and_die(void)
87 fprintf(stderr, "usage:\n\tosltar c <db_dir> <data_dir>\n");
88 fprintf(stderr, "\tosltar x <db_dir> file1 [file2...]\n");
89 fprintf(stderr, "\tosltar t <db_dir>\n");
94 * Open a file and map it into memory.
96 static int mmap_full_file(const char *path, void **map,
99 int fd, ret, mmap_prot, mmap_flags;
100 struct stat file_status;
102 mmap_prot = PROT_READ;
103 mmap_flags = MAP_PRIVATE;
104 ret = open(path, O_RDONLY, 0);
106 fprintf(stderr, "open: %s\n", strerror(errno));
110 if (fstat(fd, &file_status) < 0) {
112 fprintf(stderr, "fstat: %s\n", strerror(errno));
115 *size = file_status.st_size;
117 fprintf(stderr, "can not add empty file: %s\n", path);
120 *map = mmap(NULL, *size, mmap_prot, mmap_flags, fd, 0);
121 if (*map == MAP_FAILED) {
122 fprintf(stderr, "map failed: %s\n", path);
133 static int add_file(char *name)
136 struct osl_object objs[NUM_OT_COLUMNS] = {
139 .size = strlen(name) + 1
142 printf("%s\n", name);
143 ret = mmap_full_file(name, &objs[OTC_DATA].data,
144 &objs[OTC_DATA].size);
147 return osl_add_row(table, objs);
150 static int populate_table(const char *dirname)
152 struct dirent *entry;
154 int ret = chdir(dirname);
157 fprintf(stderr, "chdir: %s\n", strerror(errno));
162 fprintf(stderr, "opendir: %s\n", strerror(errno));
165 while ((entry = readdir(dir))) {
169 if (!strcmp(entry->d_name, "."))
171 if (!strcmp(entry->d_name, ".."))
173 if (lstat(entry->d_name, &s) == -1)
178 ret = add_file(entry->d_name);
188 static int com_create(int argc, char **argv)
194 print_usage_and_die();
195 if (lstat(argv[2], &statbuf) == -1) {
196 fprintf(stderr, "no such dir: %s\n", argv[2]);
199 if (!S_ISDIR(statbuf.st_mode)) {
200 fprintf(stderr, "not a dir: %s\n", argv[2]);
203 tar_table_desc.dir = argv[1];
204 ret = osl_create_table(&tar_table_desc);
206 fprintf(stderr, "failed to create table\n");
210 ret = osl_open_table(&tar_table_desc, &table);
212 fprintf(stderr, "osl_open_table: %s\n", osl_strerror(-ret));
215 ret = populate_table(argv[2]);
216 osl_close_table(table, OSL_MARK_CLEAN);
217 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
221 * Write the whole buffer to a file descriptor.
223 ssize_t write_all(int fd, const void *buf, size_t size)
227 ssize_t ret = write(fd, b, size);
229 fprintf(stderr, "open: %s\n", strerror(errno));
240 * Open a file, write the given buffer and close the file.
242 static int write_file(const char *filename, const void *buf, size_t size)
246 ret = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0644);
248 fprintf(stderr, "open: %s\n", strerror(errno));
253 printf("%s\n", filename);
254 ret = write_all(fd, buf, size);
263 static int extract_file(char *name)
267 struct osl_object obj = {.data = name, .size = strlen(name) + 1};
269 ret = osl_get_row(table, OTC_NAME, &obj, &row);
271 fprintf(stderr, "osl_get_row(%s): %s\n", name,
275 ret = osl_get_object(table, row, OTC_DATA, &obj);
277 fprintf(stderr, "osl_get_object: %s\n", osl_strerror(-ret));
280 return write_file(name, obj.data, obj.size);
283 static int com_extract(int argc, char **argv)
288 print_usage_and_die();
289 tar_table_desc.dir = argv[1];
290 ret = osl_open_table(&tar_table_desc, &table);
292 fprintf(stderr, "osl_open_table: %s\n", osl_strerror(-ret));
296 for (i = 2; i < argc; i++) {
297 ret = extract_file(argv[i]);
302 osl_close_table(table, OSL_MARK_CLEAN);
306 static int list_entry(struct osl_row *row, void *data)
308 struct osl_object obj;
309 int ret = osl_get_object(table, row, OTC_NAME, &obj);
311 fprintf(stderr, "osl_get_object: %s\n", osl_strerror(-ret));
314 printf("%s\n", (char *)obj.data);
318 static int com_list(int argc, char **argv)
323 print_usage_and_die();
324 tar_table_desc.dir = argv[1];
325 ret = osl_open_table(&tar_table_desc, &table);
327 fprintf(stderr, "osl_open_table: %s\n", osl_strerror(-ret));
331 ret = osl_rbtree_loop(table, OTC_NAME, NULL, list_entry);
332 osl_close_table(table, OSL_MARK_CLEAN);
337 * The osltar main funcion.
339 * \param argc Usual argument count.
340 * \param argv Usual argument vector.
342 int main(int argc, char **argv)
345 print_usage_and_die();
346 if (!strcmp(argv[1], "c"))
347 return com_create(argc - 1, argv + 1);
348 if (!strcmp(argv[1], "x"))
349 return com_extract(argc - 1, argv + 1);
350 if (!strcmp(argv[1], "t"))
351 return com_list(argc - 1, argv + 1);
352 print_usage_and_die();