Implement get_name in FileSource, and add a little test program.

This commit is contained in:
Vreixo Formoso 2007-11-26 21:16:38 +01:00
parent e034f287bf
commit 1478904aaf
6 changed files with 160 additions and 20 deletions

View File

@ -21,3 +21,4 @@ Makefile
test/iso test/iso
*.lo *.lo
*.la *.la
./test/lsl

View File

@ -24,11 +24,11 @@ libinclude_HEADERS = \
## Build test applications ## Build test applications
noinst_PROGRAMS = \ noinst_PROGRAMS = \
test/iso test/lsl
test_iso_CPPFLAGS = -Isrc test_lsl_CPPFLAGS = -Isrc
test_iso_LDADD = $(src_libisofs_la_OBJECTS) $(THREAD_LIBS) test_lsl_LDADD = $(src_libisofs_la_OBJECTS) $(THREAD_LIBS)
test_iso_SOURCES = test/iso.c test_lsl_SOURCES = test/lsl.c
## Build unit test ## Build unit test

View File

@ -21,6 +21,7 @@
#include <errno.h> #include <errno.h>
#include <dirent.h> #include <dirent.h>
#include <unistd.h> #include <unistd.h>
#include <libgen.h>
#include <string.h> #include <string.h>
typedef struct typedef struct
@ -43,6 +44,18 @@ const char* lfs_get_path(IsoFileSource *src)
return data->path; 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 static
int lfs_lstat(IsoFileSource *src, struct stat *info) 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 */ /* fill struct */
data->path = strdup(path); 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; data->openned = 0;
lfs_src->refcount = 1; lfs_src->refcount = 1;
lfs_src->data = data; lfs_src->data = data;
lfs_src->get_path = lfs_get_path; lfs_src->get_path = lfs_get_path;
lfs_src->get_name = lfs_get_name;
lfs_src->lstat = lfs_lstat; lfs_src->lstat = lfs_lstat;
lfs_src->open = lfs_open; lfs_src->open = lfs_open;
lfs_src->close = lfs_close; lfs_src->close = lfs_close;

View File

@ -59,6 +59,14 @@ struct Iso_File_Source
*/ */
const char* (*get_path)(IsoFileSource *src); 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. * Get information about the file.
* @return * @return

View File

@ -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;
}

123
test/lsl.c Normal file
View File

@ -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 <stdio.h>
#include <stdlib.h>
/*
* 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;
}