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