Contiki-Inga 3.x
cfs.h
Go to the documentation of this file.
1 /**
2  * \addtogroup sys
3  * @{
4  */
5 
6 /**
7  * \defgroup cfs The Contiki file system interface
8  *
9  * The Contiki file system interface (CFS) defines an abstract API for
10  * reading directories and for reading and writing files. The CFS API
11  * is intentionally simple. The CFS API is modeled after the POSIX
12  * file API, and slightly simplified.
13  *
14  * @{
15  */
16 
17 /**
18  * \file
19  * CFS header file.
20  * \author
21  * Adam Dunkels <adam@sics.se>
22  *
23  */
24 
25 /*
26  * Copyright (c) 2004, Swedish Institute of Computer Science.
27  * All rights reserved.
28  *
29  * Redistribution and use in source and binary forms, with or without
30  * modification, are permitted provided that the following conditions
31  * are met:
32  * 1. Redistributions of source code must retain the above copyright
33  * notice, this list of conditions and the following disclaimer.
34  * 2. Redistributions in binary form must reproduce the above copyright
35  * notice, this list of conditions and the following disclaimer in the
36  * documentation and/or other materials provided with the distribution.
37  * 3. Neither the name of the Institute nor the names of its contributors
38  * may be used to endorse or promote products derived from this software
39  * without specific prior written permission.
40  *
41  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * This file is part of the Contiki operating system.
54  *
55  * Author: Adam Dunkels <adam@sics.se>
56  *
57  */
58 #ifndef CFS_H_
59 #define CFS_H_
60 
61 #include "contiki.h"
62 
63 /* Include cfs arch header to have CFS_CONF_OFFSET_TYPE defined properly */
64 #ifdef CFS_NONE
65 #elif defined CFS_POSIX
66 #elif defined CFS_COFFEE
67 #include "cfs-coffee-arch.h"
68 #elif defined CFS_FAT
69 #include "diskio-arch.h"
70 #endif
71 
72 #ifndef CFS_CONF_OFFSET_TYPE
73 typedef int cfs_offset_t;
74 #else
75 typedef CFS_CONF_OFFSET_TYPE cfs_offset_t;
76 #endif
77 
78 struct cfs_dir {
79  char dummy_space[32];
80 };
81 
82 struct cfs_dirent {
83  char name[32];
84  cfs_offset_t size;
85 };
86 
87 /**
88  * Specify that cfs_open() should open a file for reading.
89  *
90  * This constant indicates to cfs_open() that a file should be opened
91  * for reading. CFS_WRITE should be used if the file is opened for
92  * writing, and CFS_READ + CFS_WRITE indicates that the file is opened
93  * for both reading and writing.
94  *
95  * \sa cfs_open()
96  */
97 #ifndef CFS_READ
98 #define CFS_READ 1
99 #endif
100 
101 /**
102  * Specify that cfs_open() should open a file for writing.
103  *
104  * This constant indicates to cfs_open() that a file should be opened
105  * for writing. CFS_READ should be used if the file is opened for
106  * reading, and CFS_READ + CFS_WRITE indicates that the file is opened
107  * for both reading and writing.
108  *
109  * \sa cfs_open()
110  */
111 #ifndef CFS_WRITE
112 #define CFS_WRITE 2
113 #endif
114 
115 /**
116  * Specify that cfs_open() should append written data to the file rather than overwriting it.
117  *
118  * This constant indicates to cfs_open() that a file that should be
119  * opened for writing gets written data appended to the end of the
120  * file. The default behaviour (without CFS_APPEND) is that the file
121  * is overwritten with the new data.
122  *
123  * \sa cfs_open()
124  */
125 #ifndef CFS_APPEND
126 #define CFS_APPEND 4
127 #endif
128 
129 /**
130  * Specify that cfs_seek() should compute the offset from the beginning of the file.
131  *
132  * \sa cfs_seek()
133  */
134 #ifndef CFS_SEEK_SET
135 #define CFS_SEEK_SET 0
136 #endif
137 
138 /**
139  * Specify that cfs_seek() should compute the offset from the current position of the file pointer.
140  *
141  * \sa cfs_seek()
142  */
143 #ifndef CFS_SEEK_CUR
144 #define CFS_SEEK_CUR 1
145 #endif
146 
147 /**
148  * Specify that cfs_seek() should compute the offset from the end of the file.
149  *
150  * \sa cfs_seek()
151  */
152 #ifndef CFS_SEEK_END
153 #define CFS_SEEK_END 2
154 #endif
155 
156 /**
157  * \brief Open a file.
158  * \param name The name of the file.
159  * \param flags CFS_READ, or CFS_WRITE/CFS_APPEND, or both.
160  * \return A file descriptor, if the file could be opened, or -1 if
161  * the file could not be opened.
162  *
163  * This function opens a file and returns a file
164  * descriptor for the opened file. If the file could not
165  * be opened, the function returns -1. The function can
166  * open a file for reading or writing, or both.
167  *
168  * An opened file must be closed with cfs_close().
169  *
170  * \sa CFS_READ
171  * \sa CFS_WRITE
172  * \sa cfs_close()
173  */
174 #ifndef cfs_open
175 CCIF int cfs_open(const char *name, int flags);
176 #endif
177 
178 /**
179  * \brief Close an open file.
180  * \param fd The file descriptor of the open file.
181  *
182  * This function closes a file that has previously been
183  * opened with cfs_open().
184  */
185 #ifndef cfs_close
186 CCIF void cfs_close(int fd);
187 #endif
188 
189 /**
190  * \brief Read data from an open file.
191  * \param fd The file descriptor of the open file.
192  * \param buf The buffer in which data should be read from the file.
193  * \param len The number of bytes that should be read.
194  * \return The number of bytes that was actually read from the file.
195  *
196  * This function reads data from an open file into a
197  * buffer. The file must have first been opened with
198  * cfs_open() and the CFS_READ flag.
199  */
200 #ifndef cfs_read
201 CCIF int cfs_read(int fd, void *buf, unsigned int len);
202 #endif
203 
204 /**
205  * \brief Write data to an open file.
206  * \param fd The file descriptor of the open file.
207  * \param buf The buffer from which data should be written to the file.
208  * \param len The number of bytes that should be written.
209  * \return The number of bytes that was actually written to the file.
210  *
211  * This function reads writes data from a memory buffer to
212  * an open file. The file must have been opened with
213  * cfs_open() and the CFS_WRITE flag.
214  */
215 #ifndef cfs_write
216 CCIF int cfs_write(int fd, const void *buf, unsigned int len);
217 #endif
218 
219 /**
220  * \brief Seek to a specified position in an open file.
221  * \param fd The file descriptor of the open file.
222  * \param offset A position, either relative or absolute, in the file.
223  * \param whence Determines how to interpret the offset parameter.
224  * \return The new position in the file, or (cfs_offset_t)-1 if the seek failed.
225  *
226  * This function moves the file position to the specified
227  * position in the file. The next byte that is read from
228  * or written to the file will be at the position given
229  * determined by the combination of the offset parameter
230  * and the whence parameter.
231  *
232  * \sa CFS_SEEK_CUR
233  * \sa CFS_SEEK_END
234  * \sa CFS_SEEK_SET
235  */
236 #ifndef cfs_seek
237 CCIF cfs_offset_t cfs_seek(int fd, cfs_offset_t offset, int whence);
238 #endif
239 
240 /**
241  * \brief Remove a file.
242  * \param name The name of the file.
243  * \retval 0 If the file was removed.
244  * \return -1 If the file could not be removed or if it doesn't exist.
245  */
246 #ifndef cfs_remove
247 CCIF int cfs_remove(const char *name);
248 #endif
249 
250 /**
251  * \brief Open a directory for reading directory entries.
252  * \param dirp A pointer to a struct cfs_dir that is filled in by the function.
253  * \param name The name of the directory.
254  * \return 0 or -1 if the directory could not be opened.
255  *
256  * \sa cfs_readdir()
257  * \sa cfs_closedir()
258  */
259 #ifndef cfs_opendir
260 CCIF int cfs_opendir(struct cfs_dir *dirp, const char *name);
261 #endif
262 
263 /**
264  * \brief Read a directory entry
265  * \param dirp A pointer to a struct cfs_dir that has been opened with cfs_opendir().
266  * \param dirent A pointer to a struct cfs_dirent that is filled in by cfs_readdir()
267  * \retval 0 If a directory entry was read.
268  * \retval -1 If no more directory entries can be read.
269  *
270  * \sa cfs_opendir()
271  * \sa cfs_closedir()
272  */
273 #ifndef cfs_readdir
274 CCIF int cfs_readdir(struct cfs_dir *dirp, struct cfs_dirent *dirent);
275 #endif
276 
277 /**
278  * \brief Close a directory opened with cfs_opendir().
279  * \param dirp A pointer to a struct cfs_dir that has been opened with cfs_opendir().
280  *
281  * \sa cfs_opendir()
282  * \sa cfs_readdir()
283  */
284 #ifndef cfs_closedir
285 CCIF void cfs_closedir(struct cfs_dir *dirp);
286 #endif
287 
288 #endif /* CFS_H_ */
289 
290 /** @} */
291 /** @} */