blob: 9bcc06f5f4cce9c1d845f2c8337049df8392598f [file] [log] [blame]
adamdunkelsa2f3c422004-09-12 20:24:53 +00001/*
2 * Copyright (c) 2004, Swedish Institute of Computer Science.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * This file is part of the Contiki operating system.
30 *
31 * Author: Adam Dunkels <adam@sics.se>
32 *
oliverschmidt3cf20b22005-04-10 19:23:06 +000033 * $Id: cfs-posix.c,v 1.4 2005/04/10 19:23:06 oliverschmidt Exp $
adamdunkelsa2f3c422004-09-12 20:24:53 +000034 */
adamdunkels8d64ae52004-08-09 20:38:26 +000035#include "contiki.h"
36
37#include "cfs.h"
38#include "cfs-service.h"
39
40#include <fcntl.h>
41#include <unistd.h>
42#include <sys/types.h>
43#include <sys/uio.h>
44#include <sys/types.h>
45#include <dirent.h>
46#include <string.h>
47
48static int s_open(const char *n, int f);
49static void s_close(int f);
50static int s_read(int f, char *b, unsigned int l);
51static int s_write(int f, char *b, unsigned int l);
52static int s_opendir(struct cfs_dir *p, const char *n);
53static int s_readdir(struct cfs_dir *p, struct cfs_dirent *e);
54static int s_closedir(struct cfs_dir *p);
55
56static const struct cfs_service_interface interface =
57 {
58 CFS_SERVICE_VERSION,
59 s_open,
60 s_close,
61 s_read,
62 s_write,
63 s_opendir,
64 s_readdir,
65 s_closedir
66 };
67
68EK_EVENTHANDLER(eventhandler, ev, data);
69EK_PROCESS(proc, CFS_SERVICE_NAME ": POSIX", EK_PRIO_NORMAL,
70 eventhandler, NULL, (void *)&interface);
71
72struct cfs_posix_dir {
73 DIR *dirp;
74};
75
76/*---------------------------------------------------------------------------*/
77EK_PROCESS_INIT(cfs_posix_init, arg)
78{
79 arg_free(arg);
80 ek_service_start(CFS_SERVICE_NAME, &proc);
81}
82/*---------------------------------------------------------------------------*/
83EK_EVENTHANDLER(eventhandler, ev, data)
84{
85 switch(ev) {
86 case EK_EVENT_INIT:
87 break;
88 case EK_EVENT_REQUEST_REPLACE:
89 ek_replace((struct ek_proc *)data, &interface);
90 break;
91 case EK_EVENT_REQUEST_EXIT:
92 ek_exit();
93 break;
94 }
95}
96/*---------------------------------------------------------------------------*/
97static int
98s_open(const char *n, int f)
99{
100 char filename[255];
101 sprintf(filename, "cfs-root/%s", n);
adamdunkelsf00bb862004-08-20 21:40:28 +0000102 if(f == CFS_READ) {
103 return open(filename, O_RDONLY);
104 } else {
oliverschmidt3cf20b22005-04-10 19:23:06 +0000105 return open(filename, O_CREAT|O_TRUNC|O_RDWR);
adamdunkelsf00bb862004-08-20 21:40:28 +0000106 }
adamdunkels8d64ae52004-08-09 20:38:26 +0000107}
108/*---------------------------------------------------------------------------*/
109static void
110s_close(int f)
111{
112 close(f);
113}
114/*---------------------------------------------------------------------------*/
115static int
116s_read(int f, char *b, unsigned int l)
117{
118 return read(f, b, l);
119}
120/*---------------------------------------------------------------------------*/
121static int
122s_write(int f, char *b, unsigned int l)
123{
124 return write(f, b, l);
125}
126/*---------------------------------------------------------------------------*/
127static int
128s_opendir(struct cfs_dir *p, const char *n)
129{
130 struct cfs_posix_dir *dir = (struct cfs_posix_dir *)p;
131 char dirname[255];
132
133 if(n == NULL) {
134 n = "";
135 }
136 sprintf(dirname, "cfs-root/%s", n);
137
138 dir->dirp = opendir(dirname);
139
140 return dir->dirp == NULL;
141}
142/*---------------------------------------------------------------------------*/
143static int
144s_readdir(struct cfs_dir *p, struct cfs_dirent *e)
145{
146 struct cfs_posix_dir *dir = (struct cfs_posix_dir *)p;
147 struct dirent *res;
148 int ret;
149
150 res = readdir(dir->dirp);
151 if(res == NULL) {
152 return 1;
153 }
154 strncpy(e->name, res->d_name, sizeof(e->name));
adamdunkelsf00bb862004-08-20 21:40:28 +0000155 e->size = 0;
adamdunkels8d64ae52004-08-09 20:38:26 +0000156 return 0;
157}
158/*---------------------------------------------------------------------------*/
159static int
160s_closedir(struct cfs_dir *p)
161{
162 struct cfs_posix_dir *dir = (struct cfs_posix_dir *)p;
163 closedir(dir->dirp);
164 return 1;
165}
166/*---------------------------------------------------------------------------*/