adamdunkels | 8d64ae5 | 2004-08-09 20:38:26 +0000 | [diff] [blame] | 1 | #include "contiki.h" |
| 2 | |
| 3 | #include "cfs.h" |
| 4 | #include "cfs-service.h" |
| 5 | |
| 6 | #include <fcntl.h> |
| 7 | #include <unistd.h> |
| 8 | #include <sys/types.h> |
| 9 | #include <sys/uio.h> |
| 10 | #include <sys/types.h> |
| 11 | #include <dirent.h> |
| 12 | #include <string.h> |
| 13 | |
| 14 | static int s_open(const char *n, int f); |
| 15 | static void s_close(int f); |
| 16 | static int s_read(int f, char *b, unsigned int l); |
| 17 | static int s_write(int f, char *b, unsigned int l); |
| 18 | static int s_opendir(struct cfs_dir *p, const char *n); |
| 19 | static int s_readdir(struct cfs_dir *p, struct cfs_dirent *e); |
| 20 | static int s_closedir(struct cfs_dir *p); |
| 21 | |
| 22 | static const struct cfs_service_interface interface = |
| 23 | { |
| 24 | CFS_SERVICE_VERSION, |
| 25 | s_open, |
| 26 | s_close, |
| 27 | s_read, |
| 28 | s_write, |
| 29 | s_opendir, |
| 30 | s_readdir, |
| 31 | s_closedir |
| 32 | }; |
| 33 | |
| 34 | EK_EVENTHANDLER(eventhandler, ev, data); |
| 35 | EK_PROCESS(proc, CFS_SERVICE_NAME ": POSIX", EK_PRIO_NORMAL, |
| 36 | eventhandler, NULL, (void *)&interface); |
| 37 | |
| 38 | struct cfs_posix_dir { |
| 39 | DIR *dirp; |
| 40 | }; |
| 41 | |
| 42 | /*---------------------------------------------------------------------------*/ |
| 43 | EK_PROCESS_INIT(cfs_posix_init, arg) |
| 44 | { |
| 45 | arg_free(arg); |
| 46 | ek_service_start(CFS_SERVICE_NAME, &proc); |
| 47 | } |
| 48 | /*---------------------------------------------------------------------------*/ |
| 49 | EK_EVENTHANDLER(eventhandler, ev, data) |
| 50 | { |
| 51 | switch(ev) { |
| 52 | case EK_EVENT_INIT: |
| 53 | break; |
| 54 | case EK_EVENT_REQUEST_REPLACE: |
| 55 | ek_replace((struct ek_proc *)data, &interface); |
| 56 | break; |
| 57 | case EK_EVENT_REQUEST_EXIT: |
| 58 | ek_exit(); |
| 59 | break; |
| 60 | } |
| 61 | } |
| 62 | /*---------------------------------------------------------------------------*/ |
| 63 | static int |
| 64 | s_open(const char *n, int f) |
| 65 | { |
| 66 | char filename[255]; |
| 67 | sprintf(filename, "cfs-root/%s", n); |
adamdunkels | f00bb86 | 2004-08-20 21:40:28 +0000 | [diff] [blame^] | 68 | if(f == CFS_READ) { |
| 69 | return open(filename, O_RDONLY); |
| 70 | } else { |
| 71 | return open(filename, O_CREAT|O_RDWR); |
| 72 | } |
adamdunkels | 8d64ae5 | 2004-08-09 20:38:26 +0000 | [diff] [blame] | 73 | } |
| 74 | /*---------------------------------------------------------------------------*/ |
| 75 | static void |
| 76 | s_close(int f) |
| 77 | { |
| 78 | close(f); |
| 79 | } |
| 80 | /*---------------------------------------------------------------------------*/ |
| 81 | static int |
| 82 | s_read(int f, char *b, unsigned int l) |
| 83 | { |
| 84 | return read(f, b, l); |
| 85 | } |
| 86 | /*---------------------------------------------------------------------------*/ |
| 87 | static int |
| 88 | s_write(int f, char *b, unsigned int l) |
| 89 | { |
| 90 | return write(f, b, l); |
| 91 | } |
| 92 | /*---------------------------------------------------------------------------*/ |
| 93 | static int |
| 94 | s_opendir(struct cfs_dir *p, const char *n) |
| 95 | { |
| 96 | struct cfs_posix_dir *dir = (struct cfs_posix_dir *)p; |
| 97 | char dirname[255]; |
| 98 | |
| 99 | if(n == NULL) { |
| 100 | n = ""; |
| 101 | } |
| 102 | sprintf(dirname, "cfs-root/%s", n); |
| 103 | |
| 104 | dir->dirp = opendir(dirname); |
| 105 | |
| 106 | return dir->dirp == NULL; |
| 107 | } |
| 108 | /*---------------------------------------------------------------------------*/ |
| 109 | static int |
| 110 | s_readdir(struct cfs_dir *p, struct cfs_dirent *e) |
| 111 | { |
| 112 | struct cfs_posix_dir *dir = (struct cfs_posix_dir *)p; |
| 113 | struct dirent *res; |
| 114 | int ret; |
| 115 | |
| 116 | res = readdir(dir->dirp); |
| 117 | if(res == NULL) { |
| 118 | return 1; |
| 119 | } |
| 120 | strncpy(e->name, res->d_name, sizeof(e->name)); |
adamdunkels | f00bb86 | 2004-08-20 21:40:28 +0000 | [diff] [blame^] | 121 | e->size = 0; |
adamdunkels | 8d64ae5 | 2004-08-09 20:38:26 +0000 | [diff] [blame] | 122 | return 0; |
| 123 | } |
| 124 | /*---------------------------------------------------------------------------*/ |
| 125 | static int |
| 126 | s_closedir(struct cfs_dir *p) |
| 127 | { |
| 128 | struct cfs_posix_dir *dir = (struct cfs_posix_dir *)p; |
| 129 | closedir(dir->dirp); |
| 130 | return 1; |
| 131 | } |
| 132 | /*---------------------------------------------------------------------------*/ |