Pedanitic warning check fixes. Pointer arithmetic, anonymus unions etc
[lwext4.git] / ext4.h
1 /*\r
2  * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)\r
3  * All rights reserved.\r
4  *\r
5  * Redistribution and use in source and binary forms, with or without\r
6  * modification, are permitted provided that the following conditions\r
7  * are met:\r
8  *\r
9  * - Redistributions of source code must retain the above copyright\r
10  *   notice, this list of conditions and the following disclaimer.\r
11  * - Redistributions in binary form must reproduce the above copyright\r
12  *   notice, this list of conditions and the following disclaimer in the\r
13  *   documentation and/or other materials provided with the distribution.\r
14  * - The name of the author may not be used to endorse or promote products\r
15  *   derived from this software without specific prior written permission.\r
16  *\r
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\r
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\r
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\r
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\r
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
27  */\r
28 \r
29 /** @addtogroup lwext4\r
30  * @{\r
31  */\r
32 /**\r
33  * @file  ext4.h\r
34  * @brief Ext4 high level operations (files, directories, mount points...).\r
35  *        Client has to include only this file.\r
36  */\r
37 \r
38 #ifndef EXT4_H_\r
39 #define EXT4_H_\r
40 \r
41 #include <ext4_config.h>\r
42 #include <ext4_blockdev.h>\r
43 #include <stdint.h>\r
44 \r
45 /********************************FILE OPEN FLAGS*****************************/\r
46 \r
47 #ifndef O_RDONLY\r
48 #define O_RDONLY    00\r
49 #endif\r
50 \r
51 #ifndef O_WRONLY\r
52 #define O_WRONLY    01\r
53 #endif\r
54 \r
55 #ifndef O_RDWR\r
56 #define O_RDWR      02\r
57 #endif\r
58 \r
59 #ifndef O_CREAT\r
60 #define O_CREAT     0100\r
61 #endif\r
62 \r
63 #ifndef O_EXCL\r
64 #define O_EXCL      0200\r
65 #endif\r
66 \r
67 #ifndef O_TRUNC\r
68 #define O_TRUNC     01000\r
69 #endif\r
70 \r
71 #ifndef O_APPEND\r
72 #define O_APPEND    02000\r
73 #endif\r
74 \r
75 /********************************FILE SEEK FLAGS*****************************/\r
76 \r
77 #ifndef SEEK_SET\r
78 #define SEEK_SET    0\r
79 #endif\r
80 \r
81 #ifndef SEEK_CUR\r
82 #define SEEK_CUR    1\r
83 #endif\r
84 \r
85 #ifndef SEEK_END\r
86 #define SEEK_END    2\r
87 #endif\r
88 \r
89 /********************************OS LOCK INFERFACE***************************/\r
90 \r
91 /**@brief   OS dependent lock interface.*/\r
92 struct ext4_lock {\r
93 \r
94     /**@brief   Lock access to mount point*/\r
95     void (*lock)(void);\r
96 \r
97     /**@brief   Unlock access to mount point*/\r
98     void (*unlock)(void);\r
99 };\r
100 \r
101 \r
102 /********************************FILE DESCRIPTOR*****************************/\r
103 \r
104 /**@brief   File descriptor*/\r
105 typedef struct ext4_file {\r
106 \r
107     /**@brief   Mount point handle.*/\r
108     struct ext4_mountpoint *mp;\r
109 \r
110     /**@brief   File inode id*/\r
111     uint32_t inode;\r
112 \r
113     /**@brief   Open flags.*/\r
114     uint32_t flags;\r
115 \r
116     /**@brief   File size.*/\r
117     uint64_t fsize;\r
118 \r
119     /**@brief   File position*/\r
120     uint64_t fpos;\r
121 }ext4_file;\r
122 \r
123 /*****************************DIRECTORY DESCRIPTOR***************************/\r
124 /**@brief   Directory entry types. Copy from ext4_types.h*/\r
125 enum  {\r
126     EXT4_DIRENTRY_UNKNOWN = 0,\r
127     EXT4_DIRENTRY_REG_FILE,\r
128     EXT4_DIRENTRY_DIR,\r
129     EXT4_DIRENTRY_CHRDEV,\r
130     EXT4_DIRENTRY_BLKDEV,\r
131     EXT4_DIRENTRY_FIFO,\r
132     EXT4_DIRENTRY_SOCK,\r
133     EXT4_DIRENTRY_SYMLINK\r
134 };\r
135 \r
136 /**@brief   Directory entry descriptor. Copy from ext4_types.h*/\r
137 typedef struct {\r
138     uint32_t inode;\r
139     uint16_t entry_length;\r
140     uint8_t name_length;\r
141     uint8_t inode_type;\r
142     uint8_t name[255];\r
143 }ext4_direntry;\r
144 \r
145 typedef struct  {\r
146     /**@brief   File descriptor*/\r
147     ext4_file f;\r
148     /**@brief   Current directory entry.*/\r
149     ext4_direntry de;\r
150 }ext4_dir;\r
151 \r
152 /********************************MOUNT OPERATIONS****************************/\r
153 \r
154 /**@brief   Register a block device to a name.\r
155  *          @warning Block device has to be filled by\r
156  *          @ref EXT4_BLOCKDEV_STATIC_INSTANCE. Block cache may be created\r
157  *          @ref EXT4_BCACHE_STATIC_INSTANCE.\r
158  *          Block cache may by created automatically when bc parameter is 0.\r
159  * @param   bd block device\r
160  * @param   bd block device cache (0 = automatic cache mode)\r
161  * @param   dev_name register name\r
162  * @param   standard error code*/\r
163 int ext4_device_register(struct ext4_blockdev *bd, struct ext4_bcache *bc,\r
164         const char *dev_name);\r
165 \r
166 /**@brief   Mount a block device with EXT4 partition to the mount point.\r
167  * @param   dev_name block device name (@ref ext4_device_register)\r
168  * @param   mount_point mount point, for example\r
169  *          -   /\r
170  *          -   /my_partition/\r
171  *          -   /my_second_partition/\r
172  *\r
173  * @return standard error code */\r
174 int ext4_mount(const char * dev_name,  char *mount_point);\r
175 \r
176 /**@brief   Umount operation.\r
177  * @param   mount_point mount name\r
178  * @return  standard error code */\r
179 int ext4_umount(char *mount_point);\r
180 \r
181 \r
182 /**@brief   Some of the filesystem stats.*/\r
183 struct ext4_mount_stats {\r
184     uint32_t inodes_count;\r
185     uint32_t free_inodes_count;\r
186     uint64_t blocks_count;\r
187     uint64_t free_blocks_count;\r
188 \r
189     uint32_t block_size;\r
190     uint32_t block_group_count;\r
191     uint32_t blocks_per_group;\r
192     uint32_t inodes_per_group;\r
193 \r
194     char volume_name[16];\r
195 };\r
196 \r
197 /**@brief   Get file system params.\r
198  * @param   mount_point mount path\r
199  * @param   stats ext fs stats\r
200  * @return  standard error code */\r
201 int ext4_mount_point_stats(const char *mount_point,\r
202     struct ext4_mount_stats *stats);\r
203 \r
204 \r
205 /**@brief   Enable/disable write back cache mode.\r
206  * @warning Default model of cache is write trough. It means that when You do:\r
207  *\r
208  *          ext4_fopen(...);\r
209  *          ext4_fwrie(...);\r
210  *                           < --- data is flushed to physical drive\r
211  *\r
212  *          When you do:\r
213  *          ext4_cache_write_back(..., 1);\r
214  *          ext4_fopen(...);\r
215  *          ext4_fwrie(...);\r
216  *                           < --- data is NOT flushed to physical drive\r
217  *          ext4_cache_write_back(..., 0);\r
218  *                           < --- when write back mode is disabled all\r
219  *                                 cache data will be flushed\r
220  * To enable write back mode permanently just call this function\r
221  * once after ext4_mount (and disable before ext4_umount).\r
222  *\r
223  * Some of the function use write back cache mode internally.\r
224  * If you enable write back mode twice you have to disable it twice\r
225  * to flush all data:\r
226  *\r
227  *      ext4_cache_write_back(..., 1);\r
228  *      ext4_cache_write_back(..., 1);\r
229  *\r
230  *      ext4_cache_write_back(..., 0);\r
231  *      ext4_cache_write_back(..., 0);\r
232  *\r
233  * Write back mode is useful when you want to create a lot of empty\r
234  * files/directories.\r
235  *\r
236  * @param   path mount point path\r
237  * @param   on enable/disable\r
238  *\r
239  * @return  standard error code */\r
240 int ext4_cache_write_back(const char *path, bool on);\r
241 \r
242 /********************************FILE OPERATIONS*****************************/\r
243 \r
244 /**@brief   Remove file by path.\r
245  * @param   path path to file\r
246  * @return  standard error code */\r
247 int ext4_fremove(const char *path);\r
248 \r
249 /**@brief   File open function.\r
250  * @param   filename, (has to start from mount point)\r
251  *          /my_partition/my_file\r
252  * @param   flags open file flags\r
253  *  |---------------------------------------------------------------|\r
254  *  |   r or rb                 O_RDONLY                            |\r
255  *  |---------------------------------------------------------------|\r
256  *  |   w or wb                 O_WRONLY|O_CREAT|O_TRUNC            |\r
257  *  |---------------------------------------------------------------|\r
258  *  |   a or ab                 O_WRONLY|O_CREAT|O_APPEND           |\r
259  *  |---------------------------------------------------------------|\r
260  *  |   r+ or rb+ or r+b        O_RDWR                              |\r
261  *  |---------------------------------------------------------------|\r
262  *  |   w+ or wb+ or w+b        O_RDWR|O_CREAT|O_TRUNC              |\r
263  *  |---------------------------------------------------------------|\r
264  *  |   a+ or ab+ or a+b        O_RDWR|O_CREAT|O_APPEND             |\r
265  *  |---------------------------------------------------------------|\r
266  *\r
267  * @return  standard error code*/\r
268 int ext4_fopen (ext4_file *f, const char *path, const char *flags);\r
269 \r
270 /**@brief   File close function.\r
271  * @param   f file handle\r
272  * @return  standard error code*/\r
273 int ext4_fclose(ext4_file *f);\r
274 \r
275 /**@brief   Read data from file.\r
276  * @param   f file handle\r
277  * @param   buf output buffer\r
278  * @param   size bytes to read\r
279  * @param   rcnt bytes read (may be NULL)\r
280  * @return  standard error code*/\r
281 int ext4_fread (ext4_file *f, void *buf, uint32_t size, uint32_t *rcnt);\r
282 \r
283 /**@brief   Write data to file.\r
284  * @param   f file handle\r
285  * @param   buf data to write\r
286  * @param   size write length\r
287  * @param   wcnt bytes written (may be NULL)\r
288  * @return  standard error code*/\r
289 int ext4_fwrite(ext4_file *f, void *buf, uint32_t size, uint32_t *wcnt);\r
290 \r
291 /**@brief   File seek operation.\r
292  * @param   f file handle\r
293  * @param   offset offset to seek\r
294  * @param   origin seek type:\r
295  *              @ref SEEK_SET\r
296  *              @ref SEEK_CUR\r
297  *              @ref SEEK_END\r
298  * @return  standard error code*/\r
299 int ext4_fseek (ext4_file *f, uint64_t offset, uint32_t origin);\r
300 \r
301 /**@brief   Get file position.\r
302  * @param   f file handle\r
303  * @return  actual file position */\r
304 uint64_t ext4_ftell (ext4_file *f);\r
305 \r
306 /**@brief   Get file size.\r
307  * @param   f file handle\r
308  * @return  file size */\r
309 uint64_t ext4_fsize (ext4_file *f);\r
310 \r
311 /*********************************DIRECTORY OPERATION***********************/\r
312 \r
313 /**@brief   Recursive directory remove.\r
314  * @param   path directory path to remove\r
315  * @return  standard error code*/\r
316 int ext4_dir_rm(const char *path);\r
317 \r
318 /**@brief   Create new directory.\r
319  * @param   name new directory name\r
320  * @return  standard error code*/\r
321 int ext4_dir_mk(const char *path);\r
322 \r
323 /**@brief   Directory open.\r
324  * @param   d directory handle\r
325  * @param   path directory path\r
326  * @return  standard error code*/\r
327 int ext4_dir_open (ext4_dir *d, const char *path);\r
328 \r
329 /**@brief   Directory close.\r
330  * @param   d directory handle\r
331  * @return  standard error code*/\r
332 int ext4_dir_close(ext4_dir *d);\r
333 \r
334 \r
335 /**@brief   Return directory entry by id.\r
336  * @param   d directory handle\r
337  * @param   id entry id\r
338  * @return  directory entry id (NULL id no entry)*/\r
339 ext4_direntry* ext4_dir_entry_get(ext4_dir *d, uint32_t id);\r
340 \r
341 #endif /* EXT4_H_ */\r
342 \r
343 /**\r
344  * @}\r
345  */\r