Contiki 3.x
cfs.h
Go to the documentation of this file.
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  *
33  */
34 
35 /**
36  * \file
37  * CFS header file.
38  * \author
39  * Adam Dunkels <adam@sics.se>
40  *
41  */
42 
43 /**
44  * \addtogroup sys
45  * @{
46  */
47 
48 /**
49  * \defgroup cfs The Contiki file system interface
50  *
51  * The Contiki file system interface (CFS) defines an abstract API for
52  * reading directories and for reading and writing files. The CFS API
53  * is intentionally simple. The CFS API is modeled after the POSIX
54  * file API, and slightly simplified.
55  *
56  * @{
57  */
58 
59 #ifndef CFS_H_
60 #define CFS_H_
61 
62 #include "contiki.h"
63 
64 #ifndef CFS_CONF_OFFSET_TYPE
65 typedef int cfs_offset_t;
66 #else
67 typedef CFS_CONF_OFFSET_TYPE cfs_offset_t;
68 #endif
69 
70 struct cfs_dir {
71  char dummy_space[32];
72 };
73 
74 struct cfs_dirent {
75  char name[32];
76  cfs_offset_t size;
77 };
78 
79 /**
80  * Specify that cfs_open() should open a file for reading.
81  *
82  * This constant indicates to cfs_open() that a file should be opened
83  * for reading. CFS_WRITE should be used if the file is opened for
84  * writing, and CFS_READ + CFS_WRITE indicates that the file is opened
85  * for both reading and writing.
86  *
87  * \sa cfs_open()
88  */
89 #ifndef CFS_READ
90 #define CFS_READ 1
91 #endif
92 
93 /**
94  * Specify that cfs_open() should open a file for writing.
95  *
96  * This constant indicates to cfs_open() that a file should be opened
97  * for writing. CFS_READ should be used if the file is opened for
98  * reading, and CFS_READ + CFS_WRITE indicates that the file is opened
99  * for both reading and writing.
100  *
101  * \sa cfs_open()
102  */
103 #ifndef CFS_WRITE
104 #define CFS_WRITE 2
105 #endif
106 
107 /**
108  * Specify that cfs_open() should append written data to the file rather than overwriting it.
109  *
110  * This constant indicates to cfs_open() that a file that should be
111  * opened for writing gets written data appended to the end of the
112  * file. The default behaviour (without CFS_APPEND) is that the file
113  * is overwritten with the new data.
114  *
115  * \sa cfs_open()
116  */
117 #ifndef CFS_APPEND
118 #define CFS_APPEND 4
119 #endif
120 
121 /**
122  * Specify that cfs_seek() should compute the offset from the beginning of the file.
123  *
124  * \sa cfs_seek()
125  */
126 #ifndef CFS_SEEK_SET
127 #define CFS_SEEK_SET 0
128 #endif
129 
130 /**
131  * Specify that cfs_seek() should compute the offset from the current position of the file pointer.
132  *
133  * \sa cfs_seek()
134  */
135 #ifndef CFS_SEEK_CUR
136 #define CFS_SEEK_CUR 1
137 #endif
138 
139 /**
140  * Specify that cfs_seek() should compute the offset from the end of the file.
141  *
142  * \sa cfs_seek()
143  */
144 #ifndef CFS_SEEK_END
145 #define CFS_SEEK_END 2
146 #endif
147 
148 /**
149  * \brief Open a file.
150  * \param name The name of the file.
151  * \param flags CFS_READ, or CFS_WRITE/CFS_APPEND, or both.
152  * \return A file descriptor, if the file could be opened, or -1 if
153  * the file could not be opened.
154  *
155  * This function opens a file and returns a file
156  * descriptor for the opened file. If the file could not
157  * be opened, the function returns -1. The function can
158  * open a file for reading or writing, or both.
159  *
160  * An opened file must be closed with cfs_close().
161  *
162  * \sa CFS_READ
163  * \sa CFS_WRITE
164  * \sa cfs_close()
165  */
166 #ifndef cfs_open
167 CCIF int cfs_open(const char *name, int flags);
168 #endif
169 
170 /**
171  * \brief Close an open file.
172  * \param fd The file descriptor of the open file.
173  *
174  * This function closes a file that has previously been
175  * opened with cfs_open().
176  */
177 #ifndef cfs_close
178 CCIF void cfs_close(int fd);
179 #endif
180 
181 /**
182  * \brief Read data from an open file.
183  * \param fd The file descriptor of the open file.
184  * \param buf The buffer in which data should be read from the file.
185  * \param len The number of bytes that should be read.
186  * \return The number of bytes that was actually read from the file.
187  *
188  * This function reads data from an open file into a
189  * buffer. The file must have first been opened with
190  * cfs_open() and the CFS_READ flag.
191  */
192 #ifndef cfs_read
193 CCIF int cfs_read(int fd, void *buf, unsigned int len);
194 #endif
195 
196 /**
197  * \brief Write data to an open file.
198  * \param fd The file descriptor of the open file.
199  * \param buf The buffer from which data should be written to the file.
200  * \param len The number of bytes that should be written.
201  * \return The number of bytes that was actually written to the file.
202  *
203  * This function reads writes data from a memory buffer to
204  * an open file. The file must have been opened with
205  * cfs_open() and the CFS_WRITE flag.
206  */
207 #ifndef cfs_write
208 CCIF int cfs_write(int fd, const void *buf, unsigned int len);
209 #endif
210 
211 /**
212  * \brief Seek to a specified position in an open file.
213  * \param fd The file descriptor of the open file.
214  * \param offset A position, either relative or absolute, in the file.
215  * \param whence Determines how to interpret the offset parameter.
216  * \return The new position in the file, or (cfs_offset_t)-1 if the seek failed.
217  *
218  * This function moves the file position to the specified
219  * position in the file. The next byte that is read from
220  * or written to the file will be at the position given
221  * determined by the combination of the offset parameter
222  * and the whence parameter.
223  *
224  * \sa CFS_SEEK_CUR
225  * \sa CFS_SEEK_END
226  * \sa CFS_SEEK_SET
227  */
228 #ifndef cfs_seek
229 CCIF cfs_offset_t cfs_seek(int fd, cfs_offset_t offset, int whence);
230 #endif
231 
232 /**
233  * \brief Remove a file.
234  * \param name The name of the file.
235  * \retval 0 If the file was removed.
236  * \return -1 If the file could not be removed or if it doesn't exist.
237  */
238 #ifndef cfs_remove
239 CCIF int cfs_remove(const char *name);
240 #endif
241 
242 /**
243  * \brief Open a directory for reading directory entries.
244  * \param dirp A pointer to a struct cfs_dir that is filled in by the function.
245  * \param name The name of the directory.
246  * \return 0 or -1 if the directory could not be opened.
247  *
248  * \sa cfs_readdir()
249  * \sa cfs_closedir()
250  */
251 #ifndef cfs_opendir
252 CCIF int cfs_opendir(struct cfs_dir *dirp, const char *name);
253 #endif
254 
255 /**
256  * \brief Read a directory entry
257  * \param dirp A pointer to a struct cfs_dir that has been opened with cfs_opendir().
258  * \param dirent A pointer to a struct cfs_dirent that is filled in by cfs_readdir()
259  * \retval 0 If a directory entry was read.
260  * \retval -1 If no more directory entries can be read.
261  *
262  * \sa cfs_opendir()
263  * \sa cfs_closedir()
264  */
265 #ifndef cfs_readdir
266 CCIF int cfs_readdir(struct cfs_dir *dirp, struct cfs_dirent *dirent);
267 #endif
268 
269 /**
270  * \brief Close a directory opened with cfs_opendir().
271  * \param dirp A pointer to a struct cfs_dir that has been opened with cfs_opendir().
272  *
273  * \sa cfs_opendir()
274  * \sa cfs_readdir()
275  */
276 #ifndef cfs_closedir
277 CCIF void cfs_closedir(struct cfs_dir *dirp);
278 #endif
279 
280 #endif /* CFS_H_ */
281 
282 /** @} */
283 /** @} */
int cfs_open(const char *name, int flags)
Open a file.
Definition: cfs-coffee.c:996
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:1042
int cfs_readdir(struct cfs_dir *dir, struct cfs_dirent *record)
Read a directory entry.
Definition: cfs-coffee.c:1264
int cfs_remove(const char *name)
Remove a file.
Definition: cfs-coffee.c:1074
void cfs_closedir(struct cfs_dir *dir)
Close a directory opened with cfs_opendir().
Definition: cfs-coffee.c:1290
int cfs_opendir(struct cfs_dir *dir, const char *name)
Open a directory for reading directory entries.
Definition: cfs-coffee.c:1253
void cfs_close(int fd)
Close an open file.
Definition: cfs-coffee.c:1032