oliverschmidt | 88d30bd | 2005-02-15 15:05:33 +0000 | [diff] [blame] | 1 | /* |
| 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 | * |
oliverschmidt | bab003f | 2005-05-08 10:40:32 +0000 | [diff] [blame] | 33 | * $Id: cfs-win32.c,v 1.4 2005/05/08 10:40:32 oliverschmidt Exp $ |
oliverschmidt | 88d30bd | 2005-02-15 15:05:33 +0000 | [diff] [blame] | 34 | */ |
| 35 | #include "contiki.h" |
| 36 | |
| 37 | #include "cfs.h" |
| 38 | #include "cfs-service.h" |
| 39 | |
| 40 | #define WIN32_LEAN_AND_MEAN |
| 41 | #include <windows.h> |
| 42 | #include <stdlib.h> |
| 43 | #include <stdio.h> |
| 44 | #include <io.h> |
| 45 | #include <fcntl.h> |
| 46 | |
| 47 | static int s_open(const char *n, int f); |
| 48 | static void s_close(int f); |
| 49 | static int s_read(int f, char *b, unsigned int l); |
| 50 | static int s_write(int f, char *b, unsigned int l); |
| 51 | static int s_opendir(struct cfs_dir *p, const char *n); |
| 52 | static int s_readdir(struct cfs_dir *p, struct cfs_dirent *e); |
| 53 | static int s_closedir(struct cfs_dir *p); |
| 54 | |
| 55 | static const struct cfs_service_interface interface = |
| 56 | { |
| 57 | CFS_SERVICE_VERSION, |
| 58 | s_open, |
| 59 | s_close, |
| 60 | s_read, |
| 61 | s_write, |
| 62 | s_opendir, |
| 63 | s_readdir, |
| 64 | s_closedir |
| 65 | }; |
| 66 | |
| 67 | EK_EVENTHANDLER(eventhandler, ev, data); |
| 68 | EK_PROCESS(proc, CFS_SERVICE_NAME, EK_PRIO_NORMAL, |
| 69 | eventhandler, NULL, (void *)&interface); |
| 70 | |
| 71 | struct cfs_win32_dir { |
| 72 | HANDLE handle; |
| 73 | unsigned char* name; |
| 74 | unsigned int size; |
| 75 | }; |
| 76 | |
| 77 | /*---------------------------------------------------------------------------*/ |
| 78 | EK_PROCESS_INIT(cfs_win32_init, arg) |
| 79 | { |
| 80 | arg_free(arg); |
| 81 | ek_service_start(CFS_SERVICE_NAME, &proc); |
| 82 | } |
| 83 | /*---------------------------------------------------------------------------*/ |
| 84 | EK_EVENTHANDLER(eventhandler, ev, data) |
| 85 | { |
| 86 | switch(ev) { |
| 87 | case EK_EVENT_INIT: |
| 88 | break; |
| 89 | case EK_EVENT_REQUEST_REPLACE: |
| 90 | ek_replace((struct ek_proc *)data, &interface); |
| 91 | break; |
| 92 | case EK_EVENT_REQUEST_EXIT: |
| 93 | ek_exit(); |
| 94 | break; |
| 95 | } |
| 96 | } |
| 97 | /*---------------------------------------------------------------------------*/ |
| 98 | static int |
| 99 | s_open(const char *n, int f) |
| 100 | { |
| 101 | if(f == CFS_READ) { |
| 102 | return open(n, O_RDONLY); |
| 103 | } else { |
oliverschmidt | e23e63e | 2005-04-10 19:24:08 +0000 | [diff] [blame] | 104 | return open(n, O_CREAT|O_TRUNC|O_RDWR); |
oliverschmidt | 88d30bd | 2005-02-15 15:05:33 +0000 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | /*---------------------------------------------------------------------------*/ |
| 108 | static void |
| 109 | s_close(int f) |
| 110 | { |
| 111 | close(f); |
| 112 | } |
| 113 | /*---------------------------------------------------------------------------*/ |
| 114 | static int |
| 115 | s_read(int f, char *b, unsigned int l) |
| 116 | { |
| 117 | return read(f, b, l); |
| 118 | } |
| 119 | /*---------------------------------------------------------------------------*/ |
| 120 | static int |
| 121 | s_write(int f, char *b, unsigned int l) |
| 122 | { |
| 123 | return write(f, b, l); |
| 124 | } |
| 125 | /*---------------------------------------------------------------------------*/ |
| 126 | static int |
| 127 | s_opendir(struct cfs_dir *p, const char *n) |
| 128 | { |
| 129 | struct cfs_win32_dir *dir = (struct cfs_win32_dir *)p; |
| 130 | WIN32_FIND_DATA data; |
| 131 | char dirname[MAX_PATH]; |
| 132 | |
| 133 | if(n == NULL) { |
| 134 | n = ""; |
| 135 | } |
oliverschmidt | bab003f | 2005-05-08 10:40:32 +0000 | [diff] [blame] | 136 | sprintf(dirname, "%s/*", n); |
oliverschmidt | 88d30bd | 2005-02-15 15:05:33 +0000 | [diff] [blame] | 137 | |
| 138 | dir->handle = FindFirstFile(dirname, &data); |
| 139 | if(dir->handle == INVALID_HANDLE_VALUE) { |
| 140 | dir->name = NULL; |
| 141 | return 1; |
| 142 | } |
| 143 | |
| 144 | dir->name = strdup(data.cFileName); |
oliverschmidt | 527dd2a | 2005-04-22 23:20:07 +0000 | [diff] [blame] | 145 | dir->size = ((data.nFileSizeLow + 511) / 512) % 1000; |
oliverschmidt | 88d30bd | 2005-02-15 15:05:33 +0000 | [diff] [blame] | 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | /*---------------------------------------------------------------------------*/ |
| 150 | static int |
| 151 | s_readdir(struct cfs_dir *p, struct cfs_dirent *e) |
| 152 | { |
| 153 | struct cfs_win32_dir *dir = (struct cfs_win32_dir *)p; |
| 154 | WIN32_FIND_DATA data; |
| 155 | |
| 156 | if(dir->name == NULL) { |
| 157 | return 1; |
| 158 | } |
| 159 | |
| 160 | strncpy(e->name, dir->name, sizeof(e->name)); |
| 161 | e->size = dir->size; |
| 162 | |
| 163 | free(dir->name); |
| 164 | if(FindNextFile(dir->handle, &data) == 0) { |
| 165 | dir->name = NULL; |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | dir->name = strdup(data.cFileName); |
oliverschmidt | 527dd2a | 2005-04-22 23:20:07 +0000 | [diff] [blame] | 170 | dir->size = ((data.nFileSizeLow + 511) / 512) % 1000; |
oliverschmidt | 88d30bd | 2005-02-15 15:05:33 +0000 | [diff] [blame] | 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | /*---------------------------------------------------------------------------*/ |
| 175 | static int |
| 176 | s_closedir(struct cfs_dir *p) |
| 177 | { |
| 178 | struct cfs_win32_dir *dir = (struct cfs_win32_dir *)p; |
| 179 | |
| 180 | free(dir->name); |
| 181 | FindClose(dir->handle); |
| 182 | |
| 183 | return 1; |
| 184 | } |
| 185 | /*---------------------------------------------------------------------------*/ |