1 /*             ----> DO NOT REMOVE THE FOLLOWING NOTICE <----
2  *
3  *                 Copyright (c) 2014-2015 Datalight, Inc.
4  *                     All Rights Reserved Worldwide.
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; use version 2 of the License.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but "AS-IS," WITHOUT ANY WARRANTY; without even the implied warranty
12  *  of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License along
16  *  with this program; if not, write to the Free Software Foundation, Inc.,
17  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 /*  Businesses and individuals that for commercial or other reasons cannot
21  *  comply with the terms of the GPLv2 license may obtain a commercial license
22  *  before incorporating Reliance Edge into proprietary software for
23  *  distribution in any form.  Visit http://www.datalight.com/reliance-edge for
24  *  more information.
25  */
26 
27 /** @file
28  *  @brief Interface for the Reliance Edge POSIX-like API.
29  *
30  *  The POSIX-like file system API is the primary file system API for
31  *  Reliance Edge, which supports the full functionality of the file system.
32  *  This API aims to be compatible with POSIX where reasonable, but it is
33  *  simplified considerably to meet the needs of resource-constrained embedded
34  *  systems.  The API has also been extended to provide access to the unique
35  *  features of Reliance Edge, and to cover areas (like mountins and formatting)
36  *  which do not have APIs in the POSIX specification.
37  */
38 #ifndef REDPOSIX_H
39     #define REDPOSIX_H
40 
41 /*  This header is intended for application use; some applications are written
42  *  in C++.
43  */
44     #ifdef __cplusplus
45     extern "C" {
46     #endif
47 
48     #include <redconf.h>
49 
50     #if REDCONF_API_POSIX == 1
51 
52         #include <redtypes.h>
53         #include "redapimacs.h"
54         #include "rederrno.h"
55         #include "redstat.h"
56 
57 /** Open for reading only. */
58         #define RED_O_RDONLY    0x00000001U
59 
60 /** Open for writing only. */
61         #define RED_O_WRONLY    0x00000002U
62 
63 /** Open for reading and writing. */
64         #define RED_O_RDWR      0x00000004U
65 
66 /** File offset for all writes is end-of-file. */
67         #define RED_O_APPEND    0x00000008U
68 
69 /** Create the file. */
70         #define RED_O_CREAT     0x00000010U
71 
72 /** Error if path already exists. */
73         #define RED_O_EXCL      0x00000020U
74 
75 /** Truncate file to size zero. */
76         #define RED_O_TRUNC     0x00000040U
77 
78 
79 /** @brief Last file system error (errno).
80  *
81  *  Under normal circumstances, each task using the file system has an
82  *  independent `red_errno` value.  Applications do not need to worry about
83  *  one task obliterating an error value that another task needed to read.  The
84  *  value is initially zero.  When one of the POSIX-like APIs return an
85  *  indication of error, `red_errno` is set to an error value.
86  *
87  *  In some circumstances, `red_errno` will be a global errno location which
88  *  is shared by multiple tasks.  If the calling task is not registered as a
89  *  file system user and all of the task slots are full, there can be no
90  *  task-specific errno, so the global errno is used.  Likewise, if the file
91  *  system driver is uninitialized, there are no registered file system users
92  *  and `red_errno` always refers to the global errno.  Under these
93  *  circumstances, multiple tasks manipulating `red_errno` could be
94  *  problematic.  When the task count is set to one, `red_errno` always refers
95  *  to the global errno.
96  *
97  *  Note that `red_errno` is usable as an lvalue; i.e., in addition to reading
98  *  the error value, the error value can be set:
99  *
100  *  ~~~{.c}
101  *  red_errno = 0;
102  *  ~~~
103  */
104         #define red_errno    ( *red_errnoptr() )
105 
106 
107 /** @brief Positions from which to seek within a file.
108  */
109         typedef enum
110         {
111             /*  0/1/2 are the traditional values for SET/CUR/END, respectively.  Prior
112              *  to the release of Unix System V in 1983, the SEEK_* symbols did not
113              *  exist and C programs hard-coded the 0/1/2 values with those meanings.
114              */
115             RED_SEEK_SET = 0, /**< Set file offset to given offset. */
116             RED_SEEK_CUR = 1, /**< Set file offset to current offset plus signed offset. */
117             RED_SEEK_END = 2  /**< Set file offset to EOF plus signed offset. */
118         } REDWHENCE;
119 
120 
121         #if REDCONF_API_POSIX_READDIR == 1
122 
123 /** @brief Opaque directory handle.
124  */
125             typedef struct sREDHANDLE REDDIR;
126 
127 
128 /** @brief Directory entry information.
129  */
130             typedef struct
131             {
132                 uint32_t d_ino;                       /**< File serial number (inode number). */
133                 char d_name[ REDCONF_NAME_MAX + 1U ]; /**< Name of entry. */
134                 REDSTAT d_stat;                       /**< File information (POSIX extension). */
135             } REDDIRENT;
136         #endif /* if REDCONF_API_POSIX_READDIR == 1 */
137 
138 
139         int32_t red_init( void );
140         int32_t red_uninit( void );
141         int32_t red_mount( const char * pszVolume );
142         int32_t red_umount( const char * pszVolume );
143         #if ( REDCONF_READ_ONLY == 0 ) && ( REDCONF_API_POSIX_FORMAT == 1 )
144             int32_t red_format( const char * pszVolume );
145         #endif
146         #if REDCONF_READ_ONLY == 0
147             int32_t red_transact( const char * pszVolume );
148         #endif
149         #if REDCONF_READ_ONLY == 0
150             int32_t red_settransmask( const char * pszVolume,
151                                       uint32_t ulEventMask );
152         #endif
153         int32_t red_gettransmask( const char * pszVolume,
154                                   uint32_t * pulEventMask );
155         int32_t red_statvfs( const char * pszVolume,
156                              REDSTATFS * pStatvfs );
157         int32_t red_open( const char * pszPath,
158                           uint32_t ulOpenMode );
159         #if ( REDCONF_READ_ONLY == 0 ) && ( REDCONF_API_POSIX_UNLINK == 1 )
160             int32_t red_unlink( const char * pszPath );
161         #endif
162         #if ( REDCONF_READ_ONLY == 0 ) && ( REDCONF_API_POSIX_MKDIR == 1 )
163             int32_t red_mkdir( const char * pszPath );
164         #endif
165         #if ( REDCONF_READ_ONLY == 0 ) && ( REDCONF_API_POSIX_RMDIR == 1 )
166             int32_t red_rmdir( const char * pszPath );
167         #endif
168         #if ( REDCONF_READ_ONLY == 0 ) && ( REDCONF_API_POSIX_RENAME == 1 )
169             int32_t red_rename( const char * pszOldPath,
170                                 const char * pszNewPath );
171         #endif
172         #if ( REDCONF_READ_ONLY == 0 ) && ( REDCONF_API_POSIX_LINK == 1 )
173             int32_t red_link( const char * pszPath,
174                               const char * pszHardLink );
175         #endif
176         int32_t red_close( int32_t iFildes );
177         int32_t red_read( int32_t iFildes,
178                           void * pBuffer,
179                           uint32_t ulLength );
180         #if REDCONF_READ_ONLY == 0
181             int32_t red_write( int32_t iFildes,
182                                const void * pBuffer,
183                                uint32_t ulLength );
184         #endif
185         #if REDCONF_READ_ONLY == 0
186             int32_t red_fsync( int32_t iFildes );
187         #endif
188         int64_t red_lseek( int32_t iFildes,
189                            int64_t llOffset,
190                            REDWHENCE whence );
191         #if ( REDCONF_READ_ONLY == 0 ) && ( REDCONF_API_POSIX_FTRUNCATE == 1 )
192             int32_t red_ftruncate( int32_t iFildes,
193                                    uint64_t ullSize );
194         #endif
195         int32_t red_fstat( int32_t iFildes,
196                            REDSTAT * pStat );
197         #if REDCONF_API_POSIX_READDIR == 1
198             REDDIR * red_opendir( const char * pszPath );
199             REDDIRENT * red_readdir( REDDIR * pDirStream );
200             void red_rewinddir( REDDIR * pDirStream );
201             int32_t red_closedir( REDDIR * pDirStream );
202         #endif
203         REDSTATUS * red_errnoptr( void );
204 
205     #endif /* REDCONF_API_POSIX */
206 
207 
208     #ifdef __cplusplus
209 }
210     #endif
211 
212 #endif /* ifndef REDPOSIX_H */
213