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