packvfs
changeset 2:dc23ab0545a6
- 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
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 04 Nov 2013 03:50:55 +0200 |
parents | a0b3b6682d92 |
children | ef6c1472607f |
files | .hgignore Makefile pvfsh/.clang_complete pvfsh/Makefile pvfsh/src/main.c src/pvfs.c |
diffstat | 6 files changed, 316 insertions(+), 11 deletions(-) [+] |
line diff
1.1 --- a/.hgignore Mon Nov 04 01:01:22 2013 +0200 1.2 +++ b/.hgignore Mon Nov 04 03:50:55 2013 +0200 1.3 @@ -2,3 +2,4 @@ 1.4 \.d$ 1.5 \.swp$ 1.6 ^libpackvfs\. 1.7 +/pvfsh$
2.1 --- a/Makefile Mon Nov 04 01:01:22 2013 +0200 2.2 +++ b/Makefile Mon Nov 04 03:50:55 2013 +0200 2.3 @@ -24,6 +24,9 @@ 2.4 2.5 CFLAGS = -pedantic -Wall $(dbg) $(opt) $(pic) 2.6 2.7 +.PHONY: all 2.8 +all: $(lib_so) $(lib_a) 2.9 + 2.10 $(lib_so): $(obj) 2.11 $(CC) -o $@ $(shared) $(obj) $(LDFLAGS) 2.12 2.13 @@ -35,3 +38,26 @@ 2.14 .PHONY: clean 2.15 clean: 2.16 rm -f $(obj) $(lib_a) 2.17 + 2.18 +.PHONY: install 2.19 +install: all 2.20 + mkdir -p $(INSTDIR)$(PREFIX)/include $(INSTDIR)$(PREFIX)/lib 2.21 + cp src/pvfs.h $(INSTDIR)$(PREFIX)/include/pvfs.h 2.22 + cp $(lib_a) $(INSTDIR)$(PREFIX)/lib/$(lib_a) 2.23 + cp $(lib_so) $(INSTDIR)$(PREFIX)/lib/$(lib_so) 2.24 + [ -n "$(devlink)" ] && \ 2.25 + cd $(INSTDIR)$(PREFIX)/lib && \ 2.26 + rm -f $(soname) $(devlink) && \ 2.27 + ln -s $(lib_so) $(soname) && \ 2.28 + ln -s $(soname) $(devlink) || \ 2.29 + true 2.30 + 2.31 +.PHONY: uninstall 2.32 +uninstall: 2.33 + rm -f $(INSTDIR)$(PREFIX)/include/pvfs.h 2.34 + rm -f $(INSTDIR)$(PREFIX)/lib/$(lib_a) 2.35 + rm -f $(INSTDIR)$(PREFIX)/lib/$(lib_so) 2.36 + [ -n "$(devlink)" ] && \ 2.37 + rm -f $(INSTDIR)$(PREFIX)/lib/$(soname) && \ 2.38 + rm -f $(INSTDIR)$(PREFIX)/lib/$(devlink) || \ 2.39 + true
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/pvfsh/.clang_complete Mon Nov 04 03:50:55 2013 +0200 3.3 @@ -0,0 +1,2 @@ 3.4 +-Isrc 3.5 +-I../src
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/pvfsh/Makefile Mon Nov 04 03:50:55 2013 +0200 4.3 @@ -0,0 +1,13 @@ 4.4 +src = $(wildcard src/*.c) 4.5 +obj = $(src:.c=.o) 4.6 +bin = pvfsh 4.7 + 4.8 +CFLAGS = -pedantic -Wall -g 4.9 +LDFLAGS = -lpackvfs 4.10 + 4.11 +$(bin): $(obj) 4.12 + $(CC) -o $@ $(obj) $(LDFLAGS) 4.13 + 4.14 +.PHONY: clean 4.15 +clean: 4.16 + rm -f $(obj) $(bin)
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/pvfsh/src/main.c Mon Nov 04 03:50:55 2013 +0200 5.3 @@ -0,0 +1,183 @@ 5.4 +#include <stdio.h> 5.5 +#include <stdlib.h> 5.6 +#include <string.h> 5.7 +#include <ctype.h> 5.8 +#include "pvfs.h" 5.9 + 5.10 +char *strip_wspace(char *str); 5.11 +char **tokenize(char *str); 5.12 +void cmdproc(int argc, char **argv); 5.13 + 5.14 +int main(void) 5.15 +{ 5.16 + int i; 5.17 + char buf[512]; 5.18 + 5.19 + printf("welcome to packvfs shell\n"); 5.20 + 5.21 + fputs("> ", stdout); 5.22 + fflush(stdout); 5.23 + 5.24 + while(fgets(buf, sizeof buf, stdin)) { 5.25 + char *inp = strip_wspace(buf); 5.26 + 5.27 + if(inp && *inp && *inp != '#') { 5.28 + char **tokens = tokenize(inp); 5.29 + for(i=0; tokens[i]; i++); 5.30 + 5.31 + cmdproc(i, tokens); 5.32 + } 5.33 + 5.34 + fputs("> ", stdout); 5.35 + fflush(stdout); 5.36 + } 5.37 + putchar('\n'); 5.38 + 5.39 + return 0; 5.40 +} 5.41 + 5.42 +char *strip_wspace(char *str) 5.43 +{ 5.44 + char *end = str + strlen(str) - 1; 5.45 + 5.46 + while(*str && isspace(*str)) str++; 5.47 + 5.48 + while(isspace(*end)) end--; 5.49 + end[1] = 0; 5.50 + 5.51 + return str; 5.52 +} 5.53 + 5.54 +#define SEP " \t\v\n\r" 5.55 + 5.56 +char **tokenize(char *str) 5.57 +{ 5.58 + int idx = 1; 5.59 + static char *argv[256]; 5.60 + 5.61 + argv[0] = strtok(str, SEP); 5.62 + while((argv[idx] = strtok(0, SEP))) idx++; 5.63 + 5.64 + argv[idx] = 0; 5.65 + return argv; 5.66 +} 5.67 + 5.68 +void cmd_chdir(int argc, char **argv); 5.69 +void cmd_list(int argc, char **argv); 5.70 +void cmd_train(int argc, char **argv); 5.71 +void cmd_cat(int argc, char **argv); 5.72 +void cmd_pwd(int argc, char **argv); 5.73 + 5.74 +static struct { const char *cmd; void (*proc)(int, char**); } cmdlist[] = { 5.75 + {"cd", cmd_chdir}, 5.76 + {"chdir", cmd_chdir}, 5.77 + {"ls", cmd_list}, 5.78 + {"sl", cmd_train}, 5.79 + {"cat", cmd_cat}, 5.80 + {"pwd", cmd_pwd}, 5.81 + {0, 0} 5.82 +}; 5.83 + 5.84 +void cmdproc(int argc, char **argv) 5.85 +{ 5.86 + int i; 5.87 + 5.88 + for(i=0; cmdlist[i].cmd; i++) { 5.89 + if(strcmp(argv[0], cmdlist[i].cmd) == 0) { 5.90 + cmdlist[i].proc(argc, argv); 5.91 + return; 5.92 + } 5.93 + } 5.94 + 5.95 + fprintf(stderr, "unknown command: %s\n", argv[0]); 5.96 +} 5.97 + 5.98 +void cmd_chdir(int argc, char **argv) 5.99 +{ 5.100 + if(argv[1]) { 5.101 + if(pvfs_chdir(argv[1]) == -1) { 5.102 + fprintf(stderr, "failed to change into directory: %s\n", argv[1]); 5.103 + } 5.104 + } else { 5.105 + fprintf(stderr, "syntax: cd <path>\n"); 5.106 + } 5.107 +} 5.108 + 5.109 +static int lscmp(const void *a, const void *b) 5.110 +{ 5.111 + return strcmp(*(char**)a, *(char**)b); 5.112 +} 5.113 + 5.114 +void cmd_list(int argc, char **argv) 5.115 +{ 5.116 + char *dirpath = "."; 5.117 + PVFS_DIR *dir; 5.118 + struct pvfs_dirent *dent; 5.119 + char **entries; 5.120 + int i, num_entries = 0; 5.121 + 5.122 + if(argv[1]) { 5.123 + dirpath = argv[1]; 5.124 + } 5.125 + 5.126 + if(!(dir = pvfs_opendir(dirpath))) { 5.127 + fprintf(stderr, "failed to open directory\n"); 5.128 + return; 5.129 + } 5.130 + 5.131 + while((dent = pvfs_readdir(dir))) { 5.132 + num_entries++; 5.133 + } 5.134 + 5.135 + if(!(entries = malloc(num_entries * sizeof *entries))) { 5.136 + fprintf(stderr, "out of memory\n"); 5.137 + pvfs_closedir(dir); 5.138 + return; 5.139 + } 5.140 + 5.141 + i = 0; 5.142 + pvfs_rewinddir(dir); 5.143 + while((dent = pvfs_readdir(dir))) { 5.144 + entries[i++] = strdup(dent->d_name); 5.145 + } 5.146 + pvfs_closedir(dir); 5.147 + 5.148 + qsort(entries, num_entries, sizeof *entries, lscmp); 5.149 + /*(int (*)(const void*, const void*))strcmp);*/ 5.150 + 5.151 + for(i=0; i<num_entries; i++) { 5.152 + puts(entries[i]); 5.153 + free(entries[i]); 5.154 + } 5.155 + free(entries); 5.156 +} 5.157 + 5.158 +void cmd_train(int argc, char **argv) 5.159 +{ 5.160 + printf("sorry, we're all out of trains at the moment\n"); 5.161 +} 5.162 + 5.163 +void cmd_cat(int argc, char **argv) 5.164 +{ 5.165 + int i; 5.166 + char buf[512]; 5.167 + 5.168 + for(i=1; i<argc; i++) { 5.169 + size_t sz; 5.170 + PVFS_FILE *fp = pvfs_fopen(argv[i], "rb"); 5.171 + if(!fp) { 5.172 + fprintf(stderr, "failed to open file: %s\n", argv[i]); 5.173 + continue; 5.174 + } 5.175 + 5.176 + while((sz = pvfs_fread(buf, 1, sizeof buf, fp)) > 0) { 5.177 + fwrite(buf, 1, sz, stdout); 5.178 + } 5.179 + fflush(stdout); 5.180 + } 5.181 +} 5.182 + 5.183 +void cmd_pwd(int argc, char **argv) 5.184 +{ 5.185 + printf("not implemented yet!\n"); 5.186 +}
6.1 --- a/src/pvfs.c Mon Nov 04 01:01:22 2013 +0200 6.2 +++ b/src/pvfs.c Mon Nov 04 03:50:55 2013 +0200 6.3 @@ -153,12 +153,43 @@ 6.4 return -1; 6.5 } 6.6 6.7 -int pvfs_fseek(PVFS_FILE *fp, long offset, int whence); 6.8 -long pvfs_ftell(PVFS_FILE *fp); 6.9 -void pvfs_rewind(PVFS_FILE *fp); 6.10 +int pvfs_fseek(PVFS_FILE *fp, long offset, int whence) 6.11 +{ 6.12 + if(fp->real) { 6.13 + return fseek(fp->fp, offset, whence); 6.14 + } 6.15 6.16 -size_t pvfs_fread(void *buf, size_t size, size_t nitems, PVFS_FILE *fp); 6.17 -size_t pvfs_fwrite(void *buf, size_t size, size_t nitems, PVFS_FILE *fp); 6.18 + return -1; /* TODO */ 6.19 +} 6.20 + 6.21 +long pvfs_ftell(PVFS_FILE *fp) 6.22 +{ 6.23 + if(fp->real) { 6.24 + return ftell(fp->fp); 6.25 + } 6.26 + return -1; /* TODO */ 6.27 +} 6.28 + 6.29 +void pvfs_rewind(PVFS_FILE *fp) 6.30 +{ 6.31 + pvfs_fseek(fp, 0, SEEK_SET); 6.32 +} 6.33 + 6.34 +size_t pvfs_fread(void *buf, size_t size, size_t nitems, PVFS_FILE *fp) 6.35 +{ 6.36 + if(fp->real) { 6.37 + return fread(buf, size, nitems, fp->fp); 6.38 + } 6.39 + return 0; /* TODO */ 6.40 +} 6.41 + 6.42 +size_t pvfs_fwrite(void *buf, size_t size, size_t nitems, PVFS_FILE *fp) 6.43 +{ 6.44 + if(fp->real) { 6.45 + return fwrite(buf, size, nitems, fp->fp); 6.46 + } 6.47 + return 0; /* TODO */ 6.48 +} 6.49 6.50 int pvfs_fgetc(PVFS_FILE *fp) 6.51 { 6.52 @@ -221,6 +252,7 @@ 6.53 6.54 va_start(ap, fmt); 6.55 res = pvfs_vfprintf(fp, fmt, ap); 6.56 + 6.57 va_end(ap); 6.58 return res; 6.59 } 6.60 @@ -230,9 +262,30 @@ 6.61 return 0; /* TODO */ 6.62 } 6.63 6.64 -void pvfs_clearerr(PVFS_FILE *fp); 6.65 -int pvfs_feof(PVFS_FILE *fp); 6.66 -int pvfs_ferror(PVFS_FILE *fp); 6.67 +void pvfs_clearerr(PVFS_FILE *fp) 6.68 +{ 6.69 + if(fp->real) { 6.70 + clearerr(fp->fp); 6.71 + return; 6.72 + } 6.73 + /* TODO */ 6.74 +} 6.75 + 6.76 +int pvfs_feof(PVFS_FILE *fp) 6.77 +{ 6.78 + if(fp->real) { 6.79 + return feof(fp->fp); 6.80 + } 6.81 + return 0; /* TODO */ 6.82 +} 6.83 + 6.84 +int pvfs_ferror(PVFS_FILE *fp) 6.85 +{ 6.86 + if(fp->real) { 6.87 + return ferror(fp->fp); 6.88 + } 6.89 + return -1; /* TODO */ 6.90 +} 6.91 6.92 /* POSIX stuff */ 6.93 PVFS_DIR *pvfs_opendir(const char *dirname) 6.94 @@ -274,8 +327,35 @@ 6.95 return -1; 6.96 } 6.97 6.98 -struct pvfs_dirent *pvfs_readdir(PVFS_DIR *dir); 6.99 -void pvfs_rewinddir(PVFS_DIR *dir); 6.100 +struct pvfs_dirent *pvfs_readdir(PVFS_DIR *dir) 6.101 +{ 6.102 + static struct pvfs_dirent dent; 6.103 6.104 -int pvfs_stat(const char *path, struct pvfs_stat *buf); 6.105 + if(dir->real) { 6.106 + struct dirent *real_dent = readdir(dir->dir); 6.107 + if(!real_dent) { 6.108 + pvfs_errno = errno; 6.109 + return 0; 6.110 + } 6.111 6.112 + strncpy(dent.d_name, real_dent->d_name, sizeof dent.d_name - 1); 6.113 + dent.d_name[sizeof dent.d_name - 1] = 0; 6.114 + 6.115 + return &dent; 6.116 + } 6.117 + return 0; /* TODO */ 6.118 +} 6.119 + 6.120 +void pvfs_rewinddir(PVFS_DIR *dir) 6.121 +{ 6.122 + if(dir->real) { 6.123 + rewinddir(dir->dir); 6.124 + return; 6.125 + } 6.126 + /* TODO */ 6.127 +} 6.128 + 6.129 +int pvfs_stat(const char *path, struct pvfs_stat *buf) 6.130 +{ 6.131 + return -1; /* TODO */ 6.132 +}