# HG changeset patch # User John Tsiombikas # Date 1383529855 -7200 # Node ID dc23ab0545a636e3d13be200780ac8d34c3a141b # Parent a0b3b6682d923f562959b0ea90d91542214c6cef - fleshed out some more fucntions with the code for the case where file/dir are real - added install/uninstall targets to the makefile - added pvfsh (packvfs shell) test program diff -r a0b3b6682d92 -r dc23ab0545a6 .hgignore --- a/.hgignore Mon Nov 04 01:01:22 2013 +0200 +++ b/.hgignore Mon Nov 04 03:50:55 2013 +0200 @@ -2,3 +2,4 @@ \.d$ \.swp$ ^libpackvfs\. +/pvfsh$ diff -r a0b3b6682d92 -r dc23ab0545a6 Makefile --- a/Makefile Mon Nov 04 01:01:22 2013 +0200 +++ b/Makefile Mon Nov 04 03:50:55 2013 +0200 @@ -24,6 +24,9 @@ CFLAGS = -pedantic -Wall $(dbg) $(opt) $(pic) +.PHONY: all +all: $(lib_so) $(lib_a) + $(lib_so): $(obj) $(CC) -o $@ $(shared) $(obj) $(LDFLAGS) @@ -35,3 +38,26 @@ .PHONY: clean clean: rm -f $(obj) $(lib_a) + +.PHONY: install +install: all + mkdir -p $(INSTDIR)$(PREFIX)/include $(INSTDIR)$(PREFIX)/lib + cp src/pvfs.h $(INSTDIR)$(PREFIX)/include/pvfs.h + cp $(lib_a) $(INSTDIR)$(PREFIX)/lib/$(lib_a) + cp $(lib_so) $(INSTDIR)$(PREFIX)/lib/$(lib_so) + [ -n "$(devlink)" ] && \ + cd $(INSTDIR)$(PREFIX)/lib && \ + rm -f $(soname) $(devlink) && \ + ln -s $(lib_so) $(soname) && \ + ln -s $(soname) $(devlink) || \ + true + +.PHONY: uninstall +uninstall: + rm -f $(INSTDIR)$(PREFIX)/include/pvfs.h + rm -f $(INSTDIR)$(PREFIX)/lib/$(lib_a) + rm -f $(INSTDIR)$(PREFIX)/lib/$(lib_so) + [ -n "$(devlink)" ] && \ + rm -f $(INSTDIR)$(PREFIX)/lib/$(soname) && \ + rm -f $(INSTDIR)$(PREFIX)/lib/$(devlink) || \ + true diff -r a0b3b6682d92 -r dc23ab0545a6 pvfsh/.clang_complete --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pvfsh/.clang_complete Mon Nov 04 03:50:55 2013 +0200 @@ -0,0 +1,2 @@ +-Isrc +-I../src diff -r a0b3b6682d92 -r dc23ab0545a6 pvfsh/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pvfsh/Makefile Mon Nov 04 03:50:55 2013 +0200 @@ -0,0 +1,13 @@ +src = $(wildcard src/*.c) +obj = $(src:.c=.o) +bin = pvfsh + +CFLAGS = -pedantic -Wall -g +LDFLAGS = -lpackvfs + +$(bin): $(obj) + $(CC) -o $@ $(obj) $(LDFLAGS) + +.PHONY: clean +clean: + rm -f $(obj) $(bin) diff -r a0b3b6682d92 -r dc23ab0545a6 pvfsh/src/main.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pvfsh/src/main.c Mon Nov 04 03:50:55 2013 +0200 @@ -0,0 +1,183 @@ +#include +#include +#include +#include +#include "pvfs.h" + +char *strip_wspace(char *str); +char **tokenize(char *str); +void cmdproc(int argc, char **argv); + +int main(void) +{ + int i; + char buf[512]; + + printf("welcome to packvfs shell\n"); + + fputs("> ", stdout); + fflush(stdout); + + while(fgets(buf, sizeof buf, stdin)) { + char *inp = strip_wspace(buf); + + if(inp && *inp && *inp != '#') { + char **tokens = tokenize(inp); + for(i=0; tokens[i]; i++); + + cmdproc(i, tokens); + } + + fputs("> ", stdout); + fflush(stdout); + } + putchar('\n'); + + return 0; +} + +char *strip_wspace(char *str) +{ + char *end = str + strlen(str) - 1; + + while(*str && isspace(*str)) str++; + + while(isspace(*end)) end--; + end[1] = 0; + + return str; +} + +#define SEP " \t\v\n\r" + +char **tokenize(char *str) +{ + int idx = 1; + static char *argv[256]; + + argv[0] = strtok(str, SEP); + while((argv[idx] = strtok(0, SEP))) idx++; + + argv[idx] = 0; + return argv; +} + +void cmd_chdir(int argc, char **argv); +void cmd_list(int argc, char **argv); +void cmd_train(int argc, char **argv); +void cmd_cat(int argc, char **argv); +void cmd_pwd(int argc, char **argv); + +static struct { const char *cmd; void (*proc)(int, char**); } cmdlist[] = { + {"cd", cmd_chdir}, + {"chdir", cmd_chdir}, + {"ls", cmd_list}, + {"sl", cmd_train}, + {"cat", cmd_cat}, + {"pwd", cmd_pwd}, + {0, 0} +}; + +void cmdproc(int argc, char **argv) +{ + int i; + + for(i=0; cmdlist[i].cmd; i++) { + if(strcmp(argv[0], cmdlist[i].cmd) == 0) { + cmdlist[i].proc(argc, argv); + return; + } + } + + fprintf(stderr, "unknown command: %s\n", argv[0]); +} + +void cmd_chdir(int argc, char **argv) +{ + if(argv[1]) { + if(pvfs_chdir(argv[1]) == -1) { + fprintf(stderr, "failed to change into directory: %s\n", argv[1]); + } + } else { + fprintf(stderr, "syntax: cd \n"); + } +} + +static int lscmp(const void *a, const void *b) +{ + return strcmp(*(char**)a, *(char**)b); +} + +void cmd_list(int argc, char **argv) +{ + char *dirpath = "."; + PVFS_DIR *dir; + struct pvfs_dirent *dent; + char **entries; + int i, num_entries = 0; + + if(argv[1]) { + dirpath = argv[1]; + } + + if(!(dir = pvfs_opendir(dirpath))) { + fprintf(stderr, "failed to open directory\n"); + return; + } + + while((dent = pvfs_readdir(dir))) { + num_entries++; + } + + if(!(entries = malloc(num_entries * sizeof *entries))) { + fprintf(stderr, "out of memory\n"); + pvfs_closedir(dir); + return; + } + + i = 0; + pvfs_rewinddir(dir); + while((dent = pvfs_readdir(dir))) { + entries[i++] = strdup(dent->d_name); + } + pvfs_closedir(dir); + + qsort(entries, num_entries, sizeof *entries, lscmp); + /*(int (*)(const void*, const void*))strcmp);*/ + + for(i=0; i 0) { + fwrite(buf, 1, sz, stdout); + } + fflush(stdout); + } +} + +void cmd_pwd(int argc, char **argv) +{ + printf("not implemented yet!\n"); +} diff -r a0b3b6682d92 -r dc23ab0545a6 src/pvfs.c --- a/src/pvfs.c Mon Nov 04 01:01:22 2013 +0200 +++ b/src/pvfs.c Mon Nov 04 03:50:55 2013 +0200 @@ -153,12 +153,43 @@ return -1; } -int pvfs_fseek(PVFS_FILE *fp, long offset, int whence); -long pvfs_ftell(PVFS_FILE *fp); -void pvfs_rewind(PVFS_FILE *fp); +int pvfs_fseek(PVFS_FILE *fp, long offset, int whence) +{ + if(fp->real) { + return fseek(fp->fp, offset, whence); + } -size_t pvfs_fread(void *buf, size_t size, size_t nitems, PVFS_FILE *fp); -size_t pvfs_fwrite(void *buf, size_t size, size_t nitems, PVFS_FILE *fp); + return -1; /* TODO */ +} + +long pvfs_ftell(PVFS_FILE *fp) +{ + if(fp->real) { + return ftell(fp->fp); + } + return -1; /* TODO */ +} + +void pvfs_rewind(PVFS_FILE *fp) +{ + pvfs_fseek(fp, 0, SEEK_SET); +} + +size_t pvfs_fread(void *buf, size_t size, size_t nitems, PVFS_FILE *fp) +{ + if(fp->real) { + return fread(buf, size, nitems, fp->fp); + } + return 0; /* TODO */ +} + +size_t pvfs_fwrite(void *buf, size_t size, size_t nitems, PVFS_FILE *fp) +{ + if(fp->real) { + return fwrite(buf, size, nitems, fp->fp); + } + return 0; /* TODO */ +} int pvfs_fgetc(PVFS_FILE *fp) { @@ -221,6 +252,7 @@ va_start(ap, fmt); res = pvfs_vfprintf(fp, fmt, ap); + va_end(ap); return res; } @@ -230,9 +262,30 @@ return 0; /* TODO */ } -void pvfs_clearerr(PVFS_FILE *fp); -int pvfs_feof(PVFS_FILE *fp); -int pvfs_ferror(PVFS_FILE *fp); +void pvfs_clearerr(PVFS_FILE *fp) +{ + if(fp->real) { + clearerr(fp->fp); + return; + } + /* TODO */ +} + +int pvfs_feof(PVFS_FILE *fp) +{ + if(fp->real) { + return feof(fp->fp); + } + return 0; /* TODO */ +} + +int pvfs_ferror(PVFS_FILE *fp) +{ + if(fp->real) { + return ferror(fp->fp); + } + return -1; /* TODO */ +} /* POSIX stuff */ PVFS_DIR *pvfs_opendir(const char *dirname) @@ -274,8 +327,35 @@ return -1; } -struct pvfs_dirent *pvfs_readdir(PVFS_DIR *dir); -void pvfs_rewinddir(PVFS_DIR *dir); +struct pvfs_dirent *pvfs_readdir(PVFS_DIR *dir) +{ + static struct pvfs_dirent dent; -int pvfs_stat(const char *path, struct pvfs_stat *buf); + if(dir->real) { + struct dirent *real_dent = readdir(dir->dir); + if(!real_dent) { + pvfs_errno = errno; + return 0; + } + strncpy(dent.d_name, real_dent->d_name, sizeof dent.d_name - 1); + dent.d_name[sizeof dent.d_name - 1] = 0; + + return &dent; + } + return 0; /* TODO */ +} + +void pvfs_rewinddir(PVFS_DIR *dir) +{ + if(dir->real) { + rewinddir(dir->dir); + return; + } + /* TODO */ +} + +int pvfs_stat(const char *path, struct pvfs_stat *buf) +{ + return -1; /* TODO */ +}