17 #include <sys/types.h> 20 #include <sys/types.h> 41 #define __static__ static 58 const char *str1 = (
const char *)obj1->
data;
59 const char *str2 = (
const char *)obj2->
data;
60 return strcmp(str1, str2);
77 static struct osl_table *table;
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");
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)
140 .size = strlen(name) + 1
143 printf(
"%s\n", name);
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];
207 fprintf(stderr,
"failed to create table\n");
213 fprintf(stderr,
"osl_open_table: %s\n",
osl_strerror(-ret));
216 ret = populate_table(argv[2]);
218 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
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));
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)
272 fprintf(stderr,
"osl_get_row(%s): %s\n", name,
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];
293 fprintf(stderr,
"osl_open_table: %s\n",
osl_strerror(-ret));
297 for (i = 2; i < argc; i++) {
298 ret = extract_file(argv[i]);
307 static int list_entry(
struct osl_row *row,
void *
data)
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];
328 fprintf(stderr,
"osl_open_table: %s\n",
osl_strerror(-ret));
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();
int osl_add_row(struct osl_table *t, struct osl_object *objects)
int osl_rbtree_loop(const struct osl_table *t, unsigned col_num, void *private_data, osl_rbtree_loop_func *func)
int osl_close_table(struct osl_table *t, enum osl_close_flags flags)
int osl_get_object(const struct osl_table *t, const struct osl_row *row, unsigned col_num, struct osl_object *object)
int main(int argc, char **argv)
const char * osl_strerror(int num)
__static__ int string_compare(const struct osl_object *obj1, const struct osl_object *obj2)
int osl_open_table(const struct osl_table_description *desc, struct osl_table **result)
int osl_get_row(const struct osl_table *t, unsigned col_num, const struct osl_object *obj, struct osl_row **result)
int osl_create_table(const struct osl_table_description *desc)