1 /* EINA - EFL data type library
2 * Copyright (C) 2007-2008 Jorge Luis Zapata Muga
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library;
17 * if not, see <http://www.gnu.org/licenses/>.
27 #include "eina_types.h"
28 #include "eina_array.h"
29 #include "eina_iterator.h"
33 * @page eina_file_example_01_page
34 * @dontinclude eina_file_01.c
36 * For brevity includes, variable declarations and initialization was omitted
37 * from this page, however the full source code can be seen @ref
38 * eina_file_example_01 "here".
40 * Here we have a simple callback to print the name of a file and the path that
45 * We can use this callback in the following call:
46 * @skipline eina_file_dir_list
48 * The above was a way to print the files in a directory, but it is not the only
50 * @until iterator_free
52 * And now two ways to get more information than just file names:
53 * @until iterator_free
54 * @until iterator_free
56 * The above ways of getting files on a list may produce the same output, but
57 * they have an important difference, eina_file_direct_ls() will @b not call
58 * stat, this means that on some systems it might not have file type
59 * information. On the other hand it might be faster than eina_file_stat_ls().
62 * @page eina_file_example_01
63 * @include eina_file_01.c
64 * @example eina_file_01.c
67 * @addtogroup Eina_Tools_Group Tools
72 * @addtogroup Eina_File_Group File
74 * @brief Functions to handle files and directories.
76 * This functions make it easier to do a number o file and directory operations
77 * such as getting the list of files in a directory, spliting paths and finding
78 * out file size and type.
80 * @warning All functions in this group are @b blocking which means they make
81 * take a long time to return, use them carefully.
83 * See an example @ref eina_file_example_01_page "here".
89 * @typedef Eina_File_Direct_Info
90 * A typedef to #_Eina_File_Direct_Info.
92 typedef struct _Eina_File_Direct_Info Eina_File_Direct_Info;
96 * A typedef to #_Eina_Stat.
99 typedef struct _Eina_Stat Eina_Stat;
102 * @typedef Eina_File_Lines
103 * A typedef to #_Eina_File_Lines.
105 typedef struct _Eina_File_Line Eina_File_Line;
108 * @typedef Eina_File_Dir_List_Cb
109 * Type for a callback to be called when iterating over the files of a
111 * @param The file name EXCLUDING the path
112 * @param path The path passed to eina_file_dir_list()
113 * @param data The data passed to eina_file_dir_list()
115 typedef void (*Eina_File_Dir_List_Cb)(const char *name, const char *path, void *data);
118 * @typedef Eina_File_Type
119 * file type in Eina_File_Direct_Info.
122 EINA_FILE_UNKNOWN, /**< Unknown file type. */
123 EINA_FILE_FIFO, /**< Named pipe (FIFO) type (unused on Windows). */
124 EINA_FILE_CHR, /**< Character device type (unused on Windows). */
125 EINA_FILE_DIR, /**< Directory type. */
126 EINA_FILE_BLK, /**< Block device type (unused on Windows). */
127 EINA_FILE_REG, /**< Regular file type. */
128 EINA_FILE_LNK, /**< Symbolic link type. */
129 EINA_FILE_SOCK, /**< UNIX domain socket type (unused on Windows). */
130 EINA_FILE_WHT /**< Whiteout file type (unused on Windows). */
133 typedef struct _Eina_File Eina_File;
135 * @typedef Eina_File_Populate
136 * File access type used in Eina_File_Direct_info.
139 EINA_FILE_RANDOM, /**< Advise random memory access to the mapped memory. */
140 EINA_FILE_SEQUENTIAL, /**< Advise sequential memory access to the mapped memory. */
141 EINA_FILE_WILLNEED, /**< Advise need for all the mapped memory. */
142 EINA_FILE_POPULATE /**< Request all the mapped memory. */
143 } Eina_File_Populate;
145 /* Why do this? Well PATH_MAX may vary from when eina itself is compiled
146 * to when the app using eina is compiled. exposing the path buffer below
147 * can't safely and portably vary based on how/when you compile. it should
148 * always be the same for both eina inside AND for apps outside that use eina
149 * so define this to 8192 - most PATH_MAX values are like 4096 or 1024 (with
150 * windows i think being 260), so 8192 should cover almost all cases. there
151 * is a possibility that PATH_MAX could be more than 8192. if anyone spots
152 * a path_max that is bigger - let us know, but, for now we will assume
153 * it never happens */
156 * @brief The constant defined as the highest value for PATH_MAX.
158 #define EINA_PATH_MAX 8192
160 * @struct _Eina_File_Direct_Info
161 * A structure to store informations of a path.
163 struct _Eina_File_Direct_Info
165 size_t path_length; /**< size of the whole path */
166 size_t name_length; /**< size of the filename/basename component */
167 size_t name_start; /**< where the filename/basename component starts */
168 Eina_File_Type type; /**< file type */
169 char path[EINA_PATH_MAX]; /**< the path */
174 * A structure to store informations of a path.
179 unsigned long int dev;
180 unsigned long int ino;
185 unsigned long int rdev;
186 unsigned long int size;
187 unsigned long int blksize;
188 unsigned long int blocks;
189 unsigned long int atime;
190 unsigned long int atimensec;
191 unsigned long int mtime;
192 unsigned long int mtimensec;
193 unsigned long int ctime;
194 unsigned long int ctimensec;
198 * @struct _Eina_File_Line
199 * A structure to store information of line
202 struct _Eina_File_Line
207 unsigned long long length;
211 * @def EINA_FILE_DIR_LIST_CB
212 * @brief cast to an #Eina_File_Dir_List_Cb.
214 * @param function The function to cast.
216 * This macro casts @p function to Eina_File_Dir_List_Cb.
218 #define EINA_FILE_DIR_LIST_CB(function) ((Eina_File_Dir_List_Cb)function)
222 * @brief List all files on the directory calling the function for every file found.
224 * @param dir The directory name.
225 * @param recursive Iterate recursively in the directory.
226 * @param cb The callback to be called.
227 * @param data The data to pass to the callback.
228 * @return #EINA_TRUE on success, #EINA_FALSE otherwise.
230 * This function calls @p cb for each file that is in @p dir. To have @p cb
231 * called on files that are in subdirectories of @p dir @p recursive should
232 * be #EINA_TRUE. In other words if @p recursive is #EINA_FALSE, only direct children
233 * of @p dir will be operated on, if @p recursive is #EINA_TRUE the entire tree
234 * of files that is below @p dir will be operated on.
236 * If @p cb or @p dir are @c NULL, or if @p dir is a string of size 0,
237 * or if @p dir can not be opened, this function returns #EINA_FALSE
238 * immediately. otherwise, it returns #EINA_TRUE.
240 EAPI Eina_Bool eina_file_dir_list(const char *dir,
242 Eina_File_Dir_List_Cb cb,
243 void *data) EINA_ARG_NONNULL(1, 3);
246 * @brief Split a path according to the delimiter of the filesystem.
248 * @param path The path to split.
249 * @return An array of the parts of the path to split.
251 * This function splits @p path according to the delimiter of the used
252 * filesystem. If @p path is @c NULL or if the array can not be
253 * created, @c NULL is returned, otherwise, an array with each part of @p path
256 EAPI Eina_Array *eina_file_split(char *path) EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(1) EINA_MALLOC;
259 * @brief Get an iterator to list the content of a directory.
261 * @param dir The name of the directory to list
262 * @return Return an Eina_Iterator that will walk over the files and directories
263 * in @p dir. On failure it will return @c NULL.
265 * Returns an iterator for shared strings, the name of each file in @p dir will
266 * only be fetched when advancing the iterator, which means there is very little
267 * cost associated with creating the list and stopping halfway through it.
269 * @warning The iterator will hand the user a stringshared value with the full
270 * path. The user must free the string using eina_stringshare_del() on it.
272 * @note The container for the iterator is of type DIR*.
273 * @note The iterator will walk over '.' and '..' without returning them.
275 * @see eina_file_direct_ls()
277 EAPI Eina_Iterator *eina_file_ls(const char *dir) EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(1) EINA_MALLOC;
280 * @brief Get an iterator to list the content of a directory, with direct
283 * @param dir The name of the directory to list
285 * @return Return an Eina_Iterator that will walk over the files and
286 * directory in the pointed directory. On failure it will
289 * Returns an iterator for Eina_File_Direct_Info, the name of each file in @p
290 * dir will only be fetched when advancing the iterator, which means there is
291 * cost associated with creating the list and stopping halfway through it.
293 * @warning The Eina_File_Direct_Info returned by the iterator <b>must not</b>
294 * be modified in any way.
295 * @warning When the iterator is advanced or deleted the Eina_File_Direct_Info
296 * returned is no longer valid.
298 * @note The container for the iterator is of type DIR*.
299 * @note The iterator will walk over '.' and '..' without returning them.
300 * @note The difference between this function and eina_file_direct_ls() is that
301 * it guarantees the file type information will be correct incurring a
302 * possible performance penalty.
304 * @see eina_file_direct_ls()
306 EAPI Eina_Iterator *eina_file_stat_ls(const char *dir) EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(1) EINA_MALLOC;
309 * @brief Use information provided by Eina_Iterator of eina_file_stat_ls or eina_file_direct_ls
310 * to call stat in the most efficient way on your system.
312 * @param container The container returned by the Eina_Iterator using eina_iterator_container_get().
313 * @param info The content of the current Eina_File_Direct_Info provided by the Eina_Iterator
314 * @param buf Where to put the result of the stat
315 * @return On success @c 0 is returned, On error @c -1 is returned and errno is set appropriately.
317 * This function calls fstatat or stat depending on what your system supports. This makes it efficient and simple
318 * to use on your side without complex detection already done inside Eina on what the system can do.
320 * @see eina_file_direct_ls()
321 * @see eina_file_stat_ls()
324 EAPI int eina_file_statat(void *container, Eina_File_Direct_Info *info, Eina_Stat *buf) EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(1, 2, 3);
327 * @brief Get an iterator to list the content of a directory, with direct
330 * @param dir The name of the directory to list
332 * @return Return an Eina_Iterator that will walk over the files and
333 * directory in the pointed directory. On failure it will
336 * Returns an iterator for Eina_File_Direct_Info, the name of each file in @p
337 * dir will only be fetched when advancing the iterator, which means there is
338 * cost associated with creating the list and stopping halfway through it.
340 * @warning If readdir_r doesn't contain file type information file type will
342 * @warning The Eina_File_Direct_Info returned by the iterator <b>must not</b>
343 * be modified in any way.
344 * @warning When the iterator is advanced or deleted the Eina_File_Direct_Info
345 * returned is no longer valid.
347 * @note The container for the iterator is of type DIR*.
348 * @note The iterator will walk over '.' and '..' without returning them.
349 * @note The difference between this function and eina_file_stat_ls() is that
350 * it may not get the file type information however it is likely to be
353 * @see eina_file_ls()
355 EAPI Eina_Iterator *eina_file_direct_ls(const char *dir) EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(1) EINA_MALLOC;
358 * @brief Sanitize file path.
360 * @param path The path to sanitize
362 * @return an allocated string with the sanitized path.
364 * This function take care of adding the current working directory if it's a
365 * relative path and also remove all '..' and '//' reference in the original
370 EAPI char *eina_file_path_sanitize(const char *path);
373 * @brief Get a read-only handler to a file.
375 * @param name Filename to open
376 * @param shared Requested a shm
377 * @return Eina_File handle to the file
379 * Opens a file in read-only mode. @p name should be an absolute path. An
380 * Eina_File handle can be shared among multiple instances if @p shared
385 EAPI Eina_File *eina_file_open(const char *name, Eina_Bool shared) EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(1) EINA_MALLOC;
388 * @brief Unref file handler.
390 * @param file File handler to unref.
392 * Decrement file's refcount and if it reaches zero close it.
396 EAPI void eina_file_close(Eina_File *file);
399 * @brief Get file size at open time.
401 * @param file The file handler to request the size from.
402 * @return The length of the file.
406 EAPI size_t eina_file_size_get(Eina_File *file);
409 * @brief Get the last modification time of an open file.
411 * @param file The file handler to request the modification time from.
412 * @return The last modification time.
416 EAPI time_t eina_file_mtime_get(Eina_File *file);
419 * @brief Get the filename of an open file.
421 * @param file The file handler to request the name from.
422 * @return Stringshared filename of the file.
426 EAPI const char *eina_file_filename_get(Eina_File *file);
429 * @brief Get the eXtended attribute of an open file.
431 * @param file The file handler to request the eXtended attribute from.
432 * @return an iterator.
434 * The iterator will list all eXtended attribute name without allocating
435 * them, so you need to copy them yourself if needed.
439 EAPI Eina_Iterator *eina_file_xattr_get(Eina_File *file);
442 * @brief Get the eXtended attribute of an open file.
444 * @param file The file handler to request the eXtended attribute from.
445 * @return an iterator.
447 * The iterator will list all eXtended attribute without allocating
448 * them, so you need to copy them yourself if needed. It is returning
449 * Eina_Xattr structure.
453 EAPI Eina_Iterator *eina_file_xattr_value_get(Eina_File *file);
456 * @brief Map all the file to a buffer.
458 * @param file The file handler to map in memory
459 * @param rule The rule to apply to the mapped memory
460 * @return A pointer to a buffer that map all the file content. @c NULL if it fail.
464 EAPI void *eina_file_map_all(Eina_File *file, Eina_File_Populate rule);
467 * @brief Map a part of the file.
469 * @param file The file handler to map in memory
470 * @param rule The rule to apply to the mapped memory
471 * @param offset The offset inside the file
472 * @param length The length of the memory to map
473 * @return A valid pointer to the system memory with @p length valid byte in it. And @c NULL if not inside the file or anything else goes wrong.
475 * This does handle refcounting so it will share map that target the same memory area.
479 EAPI void *eina_file_map_new(Eina_File *file, Eina_File_Populate rule,
480 unsigned long int offset, unsigned long int length);
483 * @brief Unref and unmap memory if possible.
485 * @param file The file handler to unmap memory from.
486 * @param map Memory map to unref and unmap.
490 EAPI void eina_file_map_free(Eina_File *file, void *map);
493 * @brief Map line by line in memory efficiently with an Eina_Iterator
494 * @param file The file to run over
495 * @return an Eina_Iterator that will produce @typedef Eina_File_Lines.
497 * This function return an iterator that will act like fgets without the
498 * useless memcpy. Be aware that once eina_iterator_next has been called,
499 * nothing garanty you that the memory will still be mapped.
503 EAPI Eina_Iterator *eina_file_map_lines(Eina_File *file);
506 * @brief Tell if their was an IO error during the life of a mmaped file
508 * @param file The file handler to the mmaped file.
509 * @param map Memory map to check if an error occurred on it.
510 * @return #EINA_TRUE if there was an IO error, #EINA_FALSE otherwise.
514 EAPI Eina_Bool eina_file_map_faulted(Eina_File *file, void *map);
524 #endif /* EINA_FILE_H_ */