From 1478904aaf07f9bf498a08b375433e77edc7daba Mon Sep 17 00:00:00 2001 From: Vreixo Formoso Date: Mon, 26 Nov 2007 21:16:38 +0100 Subject: [PATCH] Implement get_name in FileSource, and add a little test program. --- .bzrignore | 1 + Makefile.am | 8 ++-- src/fs_local.c | 24 ++++++++++ src/fsource.h | 8 ++++ test/iso.c | 16 ------- test/lsl.c | 123 +++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 160 insertions(+), 20 deletions(-) delete mode 100644 test/iso.c create mode 100644 test/lsl.c diff --git a/.bzrignore b/.bzrignore index 3589eae..3dc9d9c 100644 --- a/.bzrignore +++ b/.bzrignore @@ -21,3 +21,4 @@ Makefile test/iso *.lo *.la +./test/lsl diff --git a/Makefile.am b/Makefile.am index 90830a9..4b15427 100644 --- a/Makefile.am +++ b/Makefile.am @@ -24,11 +24,11 @@ libinclude_HEADERS = \ ## Build test applications noinst_PROGRAMS = \ - test/iso + test/lsl -test_iso_CPPFLAGS = -Isrc -test_iso_LDADD = $(src_libisofs_la_OBJECTS) $(THREAD_LIBS) -test_iso_SOURCES = test/iso.c +test_lsl_CPPFLAGS = -Isrc +test_lsl_LDADD = $(src_libisofs_la_OBJECTS) $(THREAD_LIBS) +test_lsl_SOURCES = test/lsl.c ## Build unit test diff --git a/src/fs_local.c b/src/fs_local.c index ac69b0d..b4bdfce 100644 --- a/src/fs_local.c +++ b/src/fs_local.c @@ -21,6 +21,7 @@ #include #include #include +#include #include typedef struct @@ -43,6 +44,18 @@ const char* lfs_get_path(IsoFileSource *src) return data->path; } +static +char* lfs_get_name(IsoFileSource *src) +{ + char *name, *p; + _LocalFsFileSource *data; + data = src->data; + p = strdup(data->path); /* because basename() might modify its arg */ + name = strdup(basename(p)); + free(p); + return name; +} + static int lfs_lstat(IsoFileSource *src, struct stat *info) { @@ -336,11 +349,22 @@ int iso_file_source_new_lfs(const char *path, IsoFileSource **src) /* fill struct */ data->path = strdup(path); + { + /* remove trailing '/' */ + int len = strlen(path); + if (len > 1) { + /* don't remove / for root! */ + if (path[len-1] == '/') { + data->path[len-1] = '\0'; + } + } + } data->openned = 0; lfs_src->refcount = 1; lfs_src->data = data; lfs_src->get_path = lfs_get_path; + lfs_src->get_name = lfs_get_name; lfs_src->lstat = lfs_lstat; lfs_src->open = lfs_open; lfs_src->close = lfs_close; diff --git a/src/fsource.h b/src/fsource.h index 7eb9efb..4542c87 100644 --- a/src/fsource.h +++ b/src/fsource.h @@ -58,6 +58,14 @@ struct Iso_File_Source * freed by the user. */ const char* (*get_path)(IsoFileSource *src); + + /** + * Get the name of the file, with the dir component of the path. + * + * @return + * the name of the file, it should be freed when no more needed. + */ + char* (*get_name)(IsoFileSource *src); /** * Get information about the file. diff --git a/test/iso.c b/test/iso.c deleted file mode 100644 index d257f89..0000000 --- a/test/iso.c +++ /dev/null @@ -1,16 +0,0 @@ -/* - * Copyright (c) 2007 Vreixo Formoso - * - * This file is part of the libisofs project; you can redistribute it and/or - * modify it under the terms of the GNU General Public License version 2 as - * published by the Free Software Foundation. See COPYING file for details. - */ - -#include "libisofs.h" - - -int main(int argc, char **argv) -{ - IsoNode *node; - -} \ No newline at end of file diff --git a/test/lsl.c b/test/lsl.c new file mode 100644 index 0000000..3414e79 --- /dev/null +++ b/test/lsl.c @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2007 Vreixo Formoso + * + * This file is part of the libisofs project; you can redistribute it and/or + * modify it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. See COPYING file for details. + */ + +#include "libisofs.h" +#include "fsource.h" + +#include +#include + +/* + * Little test program to test filesystem implementations. + * + */ + +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) +{ + char type[1]; + + 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; + file->lstat(file, &info); + print_type(info.st_mode); + print_permissions(info.st_mode); + name = file->get_name(file); + printf(" %s\n", name); + free(name); +} + +int main(int argc, char **argv) +{ + int res; + IsoFilesystem *fs; + IsoFileSource *dir; + IsoFileSource *file; + struct stat info; + + if (argc != 2) { + fprintf(stderr, "Usage: iso /path/to/dir\n"); + return 1; + } + + /* create filesystem object */ + res = iso_local_filesystem_new(&fs); + if (res < 0) { + fprintf(stderr, "Can't get local fs object, err = %d\n", res); + return 1; + } + + res = fs->get_by_path(fs, argv[1], &dir); + if (res < 0) { + fprintf(stderr, "Can't get file, err = %d\n", res); + return 1; + } + + res = dir->lstat(dir, &info); + if (res < 0) { + fprintf(stderr, "Can't stat file, err = %d\n", res); + return 1; + } + + //TODO check dir vs file + if (S_ISDIR(info.st_mode)) { + res = dir->open(dir); + if (res < 0) { + fprintf(stderr, "Can't open file, err = %d\n", res); + return 1; + } + + while (dir->readdir(dir, &file) == 1) { + print_file_src(file); + } + + res = dir->close(dir); + if (res < 0) { + fprintf(stderr, "Can't close file, err = %d\n", res); + return 1; + } + } else { + print_file_src(dir); + } + + + return 0; +}