From b2732904150dd9d0ff176440c0ce4e3655f1f000 Mon Sep 17 00:00:00 2001 From: Vreixo Formoso Date: Thu, 3 Jan 2008 00:19:39 +0100 Subject: [PATCH] Little demo program that outputs a read iso image. --- demo/iso_read.c | 104 ++++++++++++++++++++++++++++++++++++++++++++++++ src/fs_image.c | 7 +++- 2 files changed, 110 insertions(+), 1 deletion(-) diff --git a/demo/iso_read.c b/demo/iso_read.c index cbf87a8..3ba3781 100644 --- a/demo/iso_read.c +++ b/demo/iso_read.c @@ -8,16 +8,109 @@ #include #include #include +#include #include "messages.h" #include "libisofs.h" #include "fs_image.h" +static void +print_permissions(mode_t mode) +{ + char perm[10]; + + //TODO suid, sticky... + + perm[9] = '\0'; + perm[8] = mode & S_IXOTH ? 'x' : '-'; + perm[7] = mode & S_IWOTH ? 'w' : '-'; + perm[6] = mode & S_IROTH ? 'r' : '-'; + perm[5] = mode & S_IXGRP ? 'x' : '-'; + perm[4] = mode & S_IWGRP ? 'w' : '-'; + perm[3] = mode & S_IRGRP ? 'r' : '-'; + perm[2] = mode & S_IXUSR ? 'x' : '-'; + perm[1] = mode & S_IWUSR ? 'w' : '-'; + perm[0] = mode & S_IRUSR ? 'r' : '-'; + printf(" %s ",perm); +} + +static void +print_type(mode_t mode) +{ + switch(mode & S_IFMT) { + case S_IFSOCK: printf("[S] "); break; + case S_IFLNK: printf("[L] "); break; + case S_IFREG: printf("[R] "); break; + case S_IFBLK: printf("[B] "); break; + case S_IFDIR: printf("[D] "); break; + case S_IFIFO: printf("[F] "); break; + } +} + +static void +print_file_src(IsoFileSource *file) +{ + struct stat info; + char *name; + iso_file_source_lstat(file, &info); + print_type(info.st_mode); + print_permissions(info.st_mode); + //printf(" {%ld,%ld} ", (long)info.st_dev, (long)info.st_ino); + name = iso_file_source_get_name(file); + printf(" %s", name); + free(name); + if (S_ISLNK(info.st_mode)) { + char buf[PATH_MAX]; + iso_file_source_readlink(file, buf, PATH_MAX); + printf(" -> %s\n", buf); + } + printf("\n"); +} + +static void +print_dir(IsoFileSource *dir, int level) +{ + int ret, i; + IsoFileSource *file; + struct stat info; + char *sp = alloca(level * 2 + 1); + + for (i = 0; i < level * 2; i += 2) { + sp[i] = '|'; + sp[i+1] = ' '; + } + + sp[level * 2-1] = '-'; + sp[level * 2] = '\0'; + + ret = iso_file_source_open(dir); + if (ret < 0) { + printf ("Can't open dir %d\n", ret); + } + while ((ret = iso_file_source_readdir(dir, &file)) == 1) { + printf("%s", sp); + print_file_src(file); + ret = iso_file_source_lstat(file, &info); + if (ret < 0) { + break; + } + if (S_ISDIR(info.st_mode)) { + print_dir(file, level + 1); + } + iso_file_source_unref(file); + } + iso_file_source_close(dir); + if (ret < 0) { + printf ("Can't print dir\n"); + } +} + int main(int argc, char **argv) { int result; IsoImageFilesystem *fs; IsoDataSource *src; + IsoFileSource *root; struct iso_read_opts opts = { 0, /* block */ 0, /* norock */ @@ -54,8 +147,19 @@ int main(int argc, char **argv) printf ("Error creating filesystem\n"); return 1; } + + result = fs->fs.get_root((IsoFilesystem*)fs, &root); + if (result < 0) { + printf ("Can't get root %d\n", result); + return 1; + } + //print_file_src(root); + print_dir(root, 0); + iso_file_source_unref(root); + fs->close(fs); iso_filesystem_unref((IsoFilesystem*)fs); iso_data_source_unref(src); + libiso_msgs_destroy(&opts.messenger, 0); return 0; } diff --git a/src/fs_image.c b/src/fs_image.c index ea293a9..098827a 100644 --- a/src/fs_image.c +++ b/src/fs_image.c @@ -406,7 +406,11 @@ int ifs_close(IsoFileSource *src) } if (data->opened == 2) { - /* close a dir, free all pending pre-allocated children */ + /* + * close a dir, free all pending pre-allocated children. + * not that we don't need to close the filesystem, it was already + * closed + */ child_list_free((struct child_list*) data->data.content); data->data.content = NULL; data->opened = 0; @@ -1214,6 +1218,7 @@ int iso_image_filesystem_new(IsoDataSource *src, struct iso_read_opts *opts, ifs->close = ifs_fs_close; ifs->fs.data = data; + ifs->fs.refcount = 1; ifs->fs.get_root = ifs_get_root; ifs->fs.get_by_path = ifs_get_by_path; ifs->fs.free = ifs_fs_free;