From db533b2d99af33464dfb1cc414e1f4d7ca424d69 Mon Sep 17 00:00:00 2001 From: Vreixo Formoso Date: Mon, 26 Nov 2007 21:31:54 +0100 Subject: [PATCH] Add a little program to test reading from a file using IsoFileSource. --- .bzrignore | 1 + Makefile.am | 7 +++++- test/cat.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++ test/lsl.c | 14 +++++++---- 4 files changed, 88 insertions(+), 6 deletions(-) create mode 100644 test/cat.c diff --git a/.bzrignore b/.bzrignore index 3dc9d9c..8f84308 100644 --- a/.bzrignore +++ b/.bzrignore @@ -22,3 +22,4 @@ test/iso *.lo *.la ./test/lsl +./test/cat diff --git a/Makefile.am b/Makefile.am index 4b15427..641c01e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -24,12 +24,17 @@ libinclude_HEADERS = \ ## Build test applications noinst_PROGRAMS = \ - test/lsl + test/lsl \ + test/cat test_lsl_CPPFLAGS = -Isrc test_lsl_LDADD = $(src_libisofs_la_OBJECTS) $(THREAD_LIBS) test_lsl_SOURCES = test/lsl.c +test_cat_CPPFLAGS = -Isrc +test_cat_LDADD = $(src_libisofs_la_OBJECTS) $(THREAD_LIBS) +test_cat_SOURCES = test/cat.c + ## Build unit test diff --git a/test/cat.c b/test/cat.c new file mode 100644 index 0000000..a0c52ae --- /dev/null +++ b/test/cat.c @@ -0,0 +1,72 @@ +/* + * 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. + * Outputs file contents to stdout! + */ + +int main(int argc, char **argv) +{ + int res; + IsoFilesystem *fs; + IsoFileSource *file; + struct stat info; + + if (argc != 2) { + fprintf(stderr, "Usage: cat /path/to/file\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], &file); + if (res < 0) { + fprintf(stderr, "Can't get file, err = %d\n", res); + return 1; + } + + res = file->lstat(file, &info); + if (res < 0) { + fprintf(stderr, "Can't stat file, err = %d\n", res); + return 1; + } + + if (S_ISDIR(info.st_mode)) { + fprintf(stderr, "Path refers to a directory!!\n"); + return 1; + } else { + char buf[1024]; + res = file->open(file); + if (res < 0) { + fprintf(stderr, "Can't open file, err = %d\n", res); + return 1; + } + while ((res = file->read(file, buf, 1024)) > 0) { + fwrite(buf, 1, res, stdout); + } + if (res < 0) { + fprintf(stderr, "Error reading, err = %d\n", res); + return 1; + } + file->close(file); + } + + return 0; +} diff --git a/test/lsl.c b/test/lsl.c index 3414e79..4c45ec7 100644 --- a/test/lsl.c +++ b/test/lsl.c @@ -11,6 +11,7 @@ #include #include +#include /* * Little test program to test filesystem implementations. @@ -40,8 +41,6 @@ print_permissions(mode_t mode) 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; @@ -61,8 +60,14 @@ print_file_src(IsoFileSource *file) print_type(info.st_mode); print_permissions(info.st_mode); name = file->get_name(file); - printf(" %s\n", name); + printf(" %s", name); free(name); + if (S_ISLNK(info.st_mode)) { + char buf[PATH_MAX]; + file->readlink(file, buf, PATH_MAX); + printf(" -> %s\n", buf); + } + printf("\n"); } int main(int argc, char **argv) @@ -74,7 +79,7 @@ int main(int argc, char **argv) struct stat info; if (argc != 2) { - fprintf(stderr, "Usage: iso /path/to/dir\n"); + fprintf(stderr, "Usage: lsl /path/to/file\n"); return 1; } @@ -97,7 +102,6 @@ int main(int argc, char **argv) return 1; } - //TODO check dir vs file if (S_ISDIR(info.st_mode)) { res = dir->open(dir); if (res < 0) {