blob: 57065993328f048a68caf47825319d9e02292a1c [file] [log] [blame]
adamdunkels8d64ae52004-08-09 20:38:26 +00001#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
14static int s_open(const char *n, int f);
15static void s_close(int f);
16static int s_read(int f, char *b, unsigned int l);
17static int s_write(int f, char *b, unsigned int l);
18static int s_opendir(struct cfs_dir *p, const char *n);
19static int s_readdir(struct cfs_dir *p, struct cfs_dirent *e);
20static int s_closedir(struct cfs_dir *p);
21
22static 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
34EK_EVENTHANDLER(eventhandler, ev, data);
35EK_PROCESS(proc, CFS_SERVICE_NAME ": POSIX", EK_PRIO_NORMAL,
36 eventhandler, NULL, (void *)&interface);
37
38struct cfs_posix_dir {
39 DIR *dirp;
40};
41
42/*---------------------------------------------------------------------------*/
43EK_PROCESS_INIT(cfs_posix_init, arg)
44{
45 arg_free(arg);
46 ek_service_start(CFS_SERVICE_NAME, &proc);
47}
48/*---------------------------------------------------------------------------*/
49EK_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/*---------------------------------------------------------------------------*/
63static int
64s_open(const char *n, int f)
65{
66 char filename[255];
67 sprintf(filename, "cfs-root/%s", n);
adamdunkelsf00bb862004-08-20 21:40:28 +000068 if(f == CFS_READ) {
69 return open(filename, O_RDONLY);
70 } else {
71 return open(filename, O_CREAT|O_RDWR);
72 }
adamdunkels8d64ae52004-08-09 20:38:26 +000073}
74/*---------------------------------------------------------------------------*/
75static void
76s_close(int f)
77{
78 close(f);
79}
80/*---------------------------------------------------------------------------*/
81static int
82s_read(int f, char *b, unsigned int l)
83{
84 return read(f, b, l);
85}
86/*---------------------------------------------------------------------------*/
87static int
88s_write(int f, char *b, unsigned int l)
89{
90 return write(f, b, l);
91}
92/*---------------------------------------------------------------------------*/
93static int
94s_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/*---------------------------------------------------------------------------*/
109static int
110s_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));
adamdunkelsf00bb862004-08-20 21:40:28 +0000121 e->size = 0;
adamdunkels8d64ae52004-08-09 20:38:26 +0000122 return 0;
123}
124/*---------------------------------------------------------------------------*/
125static int
126s_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/*---------------------------------------------------------------------------*/