Add const keyword to mountpoint parameter
[lwext4.git] / lwext4 / 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 \r
44 #include <stdint.h>\r
45 \r
46 /********************************FILE OPEN FLAGS*****************************/\r
47 \r
48 #ifndef O_RDONLY\r
49 #define O_RDONLY 00\r
50 #endif\r
51 \r
52 #ifndef O_WRONLY\r
53 #define O_WRONLY 01\r
54 #endif\r
55 \r
56 #ifndef O_RDWR\r
57 #define O_RDWR 02\r
58 #endif\r
59 \r
60 #ifndef O_CREAT\r
61 #define O_CREAT 0100\r
62 #endif\r
63 \r
64 #ifndef O_EXCL\r
65 #define O_EXCL 0200\r
66 #endif\r
67 \r
68 #ifndef O_TRUNC\r
69 #define O_TRUNC 01000\r
70 #endif\r
71 \r
72 #ifndef O_APPEND\r
73 #define O_APPEND 02000\r
74 #endif\r
75 \r
76 /********************************FILE SEEK FLAGS*****************************/\r
77 \r
78 #ifndef SEEK_SET\r
79 #define SEEK_SET 0\r
80 #endif\r
81 \r
82 #ifndef SEEK_CUR\r
83 #define SEEK_CUR 1\r
84 #endif\r
85 \r
86 #ifndef SEEK_END\r
87 #define SEEK_END 2\r
88 #endif\r
89 \r
90 /********************************OS LOCK INFERFACE***************************/\r
91 \r
92 /**@brief   OS dependent lock interface.*/\r
93 struct ext4_lock {\r
94 \r
95     /**@brief   Lock access to mount point*/\r
96     void (*lock)(void);\r
97 \r
98     /**@brief   Unlock access to mount point*/\r
99     void (*unlock)(void);\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     /**@brief   Next entry offset*/\r
151     uint64_t next_off;\r
152 } ext4_dir;\r
153 \r
154 /********************************MOUNT OPERATIONS****************************/\r
155 \r
156 /**@brief   Register a block device to a name.\r
157  *          @warning Block device has to be filled by\r
158  *          @ref EXT4_BLOCKDEV_STATIC_INSTANCE. Block cache may be created\r
159  *          @ref EXT4_BCACHE_STATIC_INSTANCE.\r
160  *          Block cache may by created automatically when bc parameter is 0.\r
161  * @param   bd block device\r
162  * @param   bd block device cache (0 = automatic cache mode)\r
163  * @param   dev_name register name\r
164  * @param   standard error code*/\r
165 int ext4_device_register(struct ext4_blockdev *bd, struct ext4_bcache *bc,\r
166                          const char *dev_name);\r
167 \r
168 /**@brief   Mount a block device with EXT4 partition to the mount point.\r
169  * @param   dev_name block device name (@ref ext4_device_register)\r
170  * @param   mount_point mount point, for example\r
171  *          -   /\r
172  *          -   /my_partition/\r
173  *          -   /my_second_partition/\r
174  *\r
175  * @return standard error code */\r
176 int ext4_mount(const char *dev_name, const char *mount_point);\r
177 \r
178 /**@brief   Umount operation.\r
179  * @param   mount_point mount name\r
180  * @return  standard error code */\r
181 int ext4_umount(const char *mount_point);\r
182 \r
183 /**@brief   Some of the filesystem stats.*/\r
184 struct ext4_mount_stats {\r
185     uint32_t inodes_count;\r
186     uint32_t free_inodes_count;\r
187     uint64_t blocks_count;\r
188     uint64_t free_blocks_count;\r
189 \r
190     uint32_t block_size;\r
191     uint32_t block_group_count;\r
192     uint32_t blocks_per_group;\r
193     uint32_t inodes_per_group;\r
194 \r
195     char volume_name[16];\r
196 };\r
197 \r
198 /**@brief   Get file system params.\r
199  * @param   mount_point mount path\r
200  * @param   stats ext fs stats\r
201  * @return  standard error code */\r
202 int ext4_mount_point_stats(const char *mount_point,\r
203                            struct ext4_mount_stats *stats);\r
204 \r
205 /**@brief   Setup OS lock routines.\r
206  * @param   mount_point mount path\r
207  * @param   locks - lock and unlock functions\r
208  * @return  standard error code */\r
209 int ext4_mount_setup_locks(const char *mount_point,\r
210                            const struct ext4_lock *locks);\r
211 \r
212 /**@brief   Enable/disable write back cache mode.\r
213  * @warning Default model of cache is write trough. It means that when You do:\r
214  *\r
215  *          ext4_fopen(...);\r
216  *          ext4_fwrie(...);\r
217  *                           < --- data is flushed to physical drive\r
218  *\r
219  *          When you do:\r
220  *          ext4_cache_write_back(..., 1);\r
221  *          ext4_fopen(...);\r
222  *          ext4_fwrie(...);\r
223  *                           < --- data is NOT flushed to physical drive\r
224  *          ext4_cache_write_back(..., 0);\r
225  *                           < --- when write back mode is disabled all\r
226  *                                 cache data will be flushed\r
227  * To enable write back mode permanently just call this function\r
228  * once after ext4_mount (and disable before ext4_umount).\r
229  *\r
230  * Some of the function use write back cache mode internally.\r
231  * If you enable write back mode twice you have to disable it twice\r
232  * to flush all data:\r
233  *\r
234  *      ext4_cache_write_back(..., 1);\r
235  *      ext4_cache_write_back(..., 1);\r
236  *\r
237  *      ext4_cache_write_back(..., 0);\r
238  *      ext4_cache_write_back(..., 0);\r
239  *\r
240  * Write back mode is useful when you want to create a lot of empty\r
241  * files/directories.\r
242  *\r
243  * @param   path mount point path\r
244  * @param   on enable/disable\r
245  *\r
246  * @return  standard error code */\r
247 int ext4_cache_write_back(const char *path, bool on);\r
248 \r
249 /********************************FILE OPERATIONS*****************************/\r
250 \r
251 /**@brief   Remove file by path.\r
252  * @param   path path to file\r
253  * @return  standard error code */\r
254 int ext4_fremove(const char *path);\r
255 \r
256 /**@brief   File open function.\r
257  * @param   filename, (has to start from mount point)\r
258  *          /my_partition/my_file\r
259  * @param   flags open file flags\r
260  *  |---------------------------------------------------------------|\r
261  *  |   r or rb                 O_RDONLY                            |\r
262  *  |---------------------------------------------------------------|\r
263  *  |   w or wb                 O_WRONLY|O_CREAT|O_TRUNC            |\r
264  *  |---------------------------------------------------------------|\r
265  *  |   a or ab                 O_WRONLY|O_CREAT|O_APPEND           |\r
266  *  |---------------------------------------------------------------|\r
267  *  |   r+ or rb+ or r+b        O_RDWR                              |\r
268  *  |---------------------------------------------------------------|\r
269  *  |   w+ or wb+ or w+b        O_RDWR|O_CREAT|O_TRUNC              |\r
270  *  |---------------------------------------------------------------|\r
271  *  |   a+ or ab+ or a+b        O_RDWR|O_CREAT|O_APPEND             |\r
272  *  |---------------------------------------------------------------|\r
273  *\r
274  * @return  standard error code*/\r
275 int ext4_fopen(ext4_file *f, const char *path, const char *flags);\r
276 \r
277 /**@brief   File close function.\r
278  * @param   f file handle\r
279  * @return  standard error code*/\r
280 int ext4_fclose(ext4_file *f);\r
281 \r
282 /**@brief   Read data from file.\r
283  * @param   f file handle\r
284  * @param   buf output buffer\r
285  * @param   size bytes to read\r
286  * @param   rcnt bytes read (may be NULL)\r
287  * @return  standard error code*/\r
288 int ext4_fread(ext4_file *f, void *buf, uint32_t size, uint32_t *rcnt);\r
289 \r
290 /**@brief   Write data to file.\r
291  * @param   f file handle\r
292  * @param   buf data to write\r
293  * @param   size write length\r
294  * @param   wcnt bytes written (may be NULL)\r
295  * @return  standard error code*/\r
296 int ext4_fwrite(ext4_file *f, const void *buf, uint32_t size, uint32_t *wcnt);\r
297 \r
298 /**@brief   File seek operation.\r
299  * @param   f file handle\r
300  * @param   offset offset to seek\r
301  * @param   origin seek type:\r
302  *              @ref SEEK_SET\r
303  *              @ref SEEK_CUR\r
304  *              @ref SEEK_END\r
305  * @return  standard error code*/\r
306 int ext4_fseek(ext4_file *f, uint64_t offset, uint32_t origin);\r
307 \r
308 /**@brief   Get file position.\r
309  * @param   f file handle\r
310  * @return  actual file position */\r
311 uint64_t ext4_ftell(ext4_file *f);\r
312 \r
313 /**@brief   Get file size.\r
314  * @param   f file handle\r
315  * @return  file size */\r
316 uint64_t ext4_fsize(ext4_file *f);\r
317 \r
318 /*********************************DIRECTORY OPERATION***********************/\r
319 \r
320 /**@brief   Recursive directory remove.\r
321  * @param   path directory path to remove\r
322  * @return  standard error code*/\r
323 int ext4_dir_rm(const char *path);\r
324 \r
325 /**@brief   Create new directory.\r
326  * @param   name new directory name\r
327  * @return  standard error code*/\r
328 int ext4_dir_mk(const char *path);\r
329 \r
330 /**@brief   Directory open.\r
331  * @param   d directory handle\r
332  * @param   path directory path\r
333  * @return  standard error code*/\r
334 int ext4_dir_open(ext4_dir *d, const char *path);\r
335 \r
336 /**@brief   Directory close.\r
337  * @param   d directory handle\r
338  * @return  standard error code*/\r
339 int ext4_dir_close(ext4_dir *d);\r
340 \r
341 /**@brief   Return next directory entry.\r
342  * @param   d directory handle\r
343  * @param   id entry id\r
344  * @return  directory entry id (NULL if no entry)*/\r
345 ext4_direntry *ext4_dir_entry_next(ext4_dir *d);\r
346 \r
347 #endif /* EXT4_H_ */\r
348 \r
349 /**\r
350  * @}\r
351  */\r