Contiki 2.6

cfs.h

Go to the documentation of this file.
00001 /**
00002  * \addtogroup sys
00003  * @{
00004  */
00005 
00006 /**
00007  * \defgroup cfs The Contiki file system interface
00008  *
00009  * The Contiki file system interface (CFS) defines an abstract API for
00010  * reading directories and for reading and writing files. The CFS API
00011  * is intentionally simple. The CFS API is modeled after the POSIX
00012  * file API, and slightly simplified.
00013  *
00014  * @{
00015  */
00016 
00017 /**
00018  * \file
00019  *         CFS header file.
00020  * \author
00021  *         Adam Dunkels <adam@sics.se>
00022  *
00023  */
00024 
00025 /*
00026  * Copyright (c) 2004, Swedish Institute of Computer Science.
00027  * All rights reserved.
00028  *
00029  * Redistribution and use in source and binary forms, with or without
00030  * modification, are permitted provided that the following conditions
00031  * are met:
00032  * 1. Redistributions of source code must retain the above copyright
00033  *    notice, this list of conditions and the following disclaimer.
00034  * 2. Redistributions in binary form must reproduce the above copyright
00035  *    notice, this list of conditions and the following disclaimer in the
00036  *    documentation and/or other materials provided with the distribution.
00037  * 3. Neither the name of the Institute nor the names of its contributors
00038  *    may be used to endorse or promote products derived from this software
00039  *    without specific prior written permission.
00040  *
00041  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
00042  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00043  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00044  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
00045  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00046  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00047  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00048  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00049  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00050  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00051  * SUCH DAMAGE.
00052  *
00053  * This file is part of the Contiki operating system.
00054  *
00055  * Author: Adam Dunkels <adam@sics.se>
00056  *
00057  * $Id: cfs.h,v 1.18 2009/03/01 12:28:39 oliverschmidt Exp $
00058  */
00059 #ifndef __CFS_H__
00060 #define __CFS_H__
00061 
00062 #include "contiki.h"
00063 
00064 #ifndef CFS_CONF_OFFSET_TYPE
00065 typedef int cfs_offset_t;
00066 #else
00067 typedef CFS_CONF_OFFSET_TYPE cfs_offset_t;
00068 #endif
00069 
00070 struct cfs_dir {
00071   char dummy_space[32];
00072 };
00073 
00074 struct cfs_dirent {
00075   char name[32];
00076   cfs_offset_t size;
00077 };
00078 
00079 /**
00080  * Specify that cfs_open() should open a file for reading.
00081  *
00082  * This constant indicates to cfs_open() that a file should be opened
00083  * for reading. CFS_WRITE should be used if the file is opened for
00084  * writing, and CFS_READ + CFS_WRITE indicates that the file is opened
00085  * for both reading and writing.
00086  *
00087  * \sa cfs_open()
00088  */
00089 #ifndef CFS_READ
00090 #define CFS_READ  1
00091 #endif
00092 
00093 /**
00094  * Specify that cfs_open() should open a file for writing.
00095  *
00096  * This constant indicates to cfs_open() that a file should be opened
00097  * for writing. CFS_READ should be used if the file is opened for
00098  * reading, and CFS_READ + CFS_WRITE indicates that the file is opened
00099  * for both reading and writing.
00100  *
00101  * \sa cfs_open()
00102  */
00103 #ifndef CFS_WRITE
00104 #define CFS_WRITE 2
00105 #endif
00106 
00107 /**
00108  * Specify that cfs_open() should append written data to the file rather than overwriting it.
00109  *
00110  * This constant indicates to cfs_open() that a file that should be
00111  * opened for writing gets written data appended to the end of the
00112  * file. The default behaviour (without CFS_APPEND) is that the file
00113  * is overwritten with the new data.
00114  *
00115  * \sa cfs_open()
00116  */
00117 #ifndef CFS_APPEND
00118 #define CFS_APPEND 4
00119 #endif
00120 
00121 /**
00122  * Specify that cfs_seek() should compute the offset from the beginning of the file.
00123  *
00124  * \sa cfs_seek()
00125  */
00126 #ifndef CFS_SEEK_SET
00127 #define CFS_SEEK_SET 0
00128 #endif
00129 
00130 /**
00131  * Specify that cfs_seek() should compute the offset from the current position of the file pointer.
00132  *
00133  * \sa cfs_seek()
00134  */
00135 #ifndef CFS_SEEK_CUR
00136 #define CFS_SEEK_CUR 1
00137 #endif
00138 
00139 /**
00140  * Specify that cfs_seek() should compute the offset from the end of the file.
00141  *
00142  * \sa cfs_seek()
00143  */
00144 #ifndef CFS_SEEK_END
00145 #define CFS_SEEK_END 2
00146 #endif
00147 
00148 /**
00149  * \brief      Open a file.
00150  * \param name The name of the file.
00151  * \param flags CFS_READ, or CFS_WRITE/CFS_APPEND, or both.
00152  * \return     A file descriptor, if the file could be opened, or -1 if
00153  *             the file could not be opened.
00154  *
00155  *             This function opens a file and returns a file
00156  *             descriptor for the opened file. If the file could not
00157  *             be opened, the function returns -1. The function can
00158  *             open a file for reading or writing, or both.
00159  *
00160  *             An opened file must be closed with cfs_close().
00161  *
00162  * \sa         CFS_READ
00163  * \sa         CFS_WRITE
00164  * \sa         cfs_close()
00165  */
00166 #ifndef cfs_open
00167 CCIF int cfs_open(const char *name, int flags);
00168 #endif
00169 
00170 /**
00171  * \brief      Close an open file.
00172  * \param fd   The file descriptor of the open file.
00173  *
00174  *             This function closes a file that has previously been
00175  *             opened with cfs_open().
00176  */
00177 #ifndef cfs_close
00178 CCIF void cfs_close(int fd);
00179 #endif
00180 
00181 /**
00182  * \brief      Read data from an open file.
00183  * \param fd   The file descriptor of the open file.
00184  * \param buf  The buffer in which data should be read from the file.
00185  * \param len  The number of bytes that should be read.
00186  * \return     The number of bytes that was actually read from the file.
00187  *
00188  *             This function reads data from an open file into a
00189  *             buffer. The file must have first been opened with
00190  *             cfs_open() and the CFS_READ flag.
00191  */
00192 #ifndef cfs_read
00193 CCIF int cfs_read(int fd, void *buf, unsigned int len);
00194 #endif
00195 
00196 /**
00197  * \brief      Write data to an open file.
00198  * \param fd   The file descriptor of the open file.
00199  * \param buf  The buffer from which data should be written to the file.
00200  * \param len  The number of bytes that should be written.
00201  * \return     The number of bytes that was actually written to the file.
00202  *
00203  *             This function reads writes data from a memory buffer to
00204  *             an open file. The file must have been opened with
00205  *             cfs_open() and the CFS_WRITE flag.
00206  */
00207 #ifndef cfs_write
00208 CCIF int cfs_write(int fd, const void *buf, unsigned int len);
00209 #endif
00210 
00211 /**
00212  * \brief      Seek to a specified position in an open file.
00213  * \param fd   The file descriptor of the open file.
00214  * \param offset A position, either relative or absolute, in the file.
00215  * \param whence Determines how to interpret the offset parameter.
00216  * \return     The new position in the file, or (cfs_offset_t)-1 if the seek failed.
00217  *
00218  *             This function moves the file position to the specified
00219  *             position in the file. The next byte that is read from
00220  *             or written to the file will be at the position given 
00221  *             determined by the combination of the offset parameter 
00222  *             and the whence parameter.
00223  *
00224  * \sa         CFS_SEEK_CUR
00225  * \sa         CFS_SEEK_END
00226  * \sa         CFS_SEEK_SET
00227  */
00228 #ifndef cfs_seek
00229 CCIF cfs_offset_t cfs_seek(int fd, cfs_offset_t offset, int whence);
00230 #endif
00231 
00232 /**
00233  * \brief      Remove a file.
00234  * \param name The name of the file.
00235  * \retval 0   If the file was removed.
00236  * \return -1  If the file could not be removed or if it doesn't exist.
00237  */
00238 #ifndef cfs_remove
00239 CCIF int cfs_remove(const char *name);
00240 #endif
00241 
00242 /**
00243  * \brief      Open a directory for reading directory entries.
00244  * \param dirp A pointer to a struct cfs_dir that is filled in by the function.
00245  * \param name The name of the directory.
00246  * \return     0 or -1 if the directory could not be opened.
00247  *
00248  * \sa         cfs_readdir()
00249  * \sa         cfs_closedir()
00250  */
00251 #ifndef cfs_opendir
00252 CCIF int cfs_opendir(struct cfs_dir *dirp, const char *name);
00253 #endif
00254 
00255 /**
00256  * \brief      Read a directory entry
00257  * \param dirp A pointer to a struct cfs_dir that has been opened with cfs_opendir().
00258  * \param dirent A pointer to a struct cfs_dirent that is filled in by cfs_readdir()
00259  * \retval 0   If a directory entry was read.
00260  * \retval -1  If no more directory entries can be read.
00261  *
00262  * \sa         cfs_opendir()
00263  * \sa         cfs_closedir()
00264  */
00265 #ifndef cfs_readdir
00266 CCIF int cfs_readdir(struct cfs_dir *dirp, struct cfs_dirent *dirent);
00267 #endif
00268 
00269 /**
00270  * \brief      Close a directory opened with cfs_opendir().
00271  * \param dirp A pointer to a struct cfs_dir that has been opened with cfs_opendir().
00272  *
00273  * \sa         cfs_opendir()
00274  * \sa         cfs_readdir()
00275  */
00276 #ifndef cfs_closedir
00277 CCIF void cfs_closedir(struct cfs_dir *dirp);
00278 #endif
00279 
00280 #endif /* __CFS_H__ */
00281 
00282 /** @} */
00283 /** @} */