13 #ifdef HAVE_FEATURES_H
14 # include <features.h>
19 #include "ecore_file_private.h"
21 int _ecore_file_log_dom = -1;
22 static int _ecore_file_init_count = 0;
24 /* externally accessible functions */
27 * @addtogroup Ecore_File_Group Ecore_File - Files and directories convenience functions
33 * @brief Initialize the Ecore_File library.
35 * @return 1 or greater on success, 0 on error.
37 * This function sets up Ecore_File and the services it will use
38 * (monitoring, downloading, PATH related feature). It returns 0 on
39 * failure, otherwise it returns the number of times it has already
42 * When Ecore_File is not used anymore, call ecore_file_shutdown()
43 * to shut down the Ecore_File library.
48 if (++_ecore_file_init_count != 1)
49 return _ecore_file_init_count;
50 _ecore_file_log_dom = eina_log_domain_register
51 ("ecore_file", ECORE_FILE_DEFAULT_LOG_COLOR);
52 if(_ecore_file_log_dom < 0)
54 EINA_LOG_ERR("Impossible to create a log domain for the ecore file module.");
55 return --_ecore_file_init_count;
57 ecore_file_path_init();
58 ecore_file_monitor_init();
59 ecore_file_download_init();
61 /* FIXME: were the tests disabled for a good reason ? */
64 if (!ecore_file_monitor_init())
65 goto shutdown_ecore_file_path;
67 if (!ecore_file_download_init())
68 goto shutdown_ecore_file_monitor;
71 return _ecore_file_init_count;
74 shutdown_ecore_file_monitor:
75 ecore_file_monitor_shutdown();
76 shutdown_ecore_file_path:
77 ecore_file_path_shutdown();
79 return --_ecore_file_init_count;
84 * @brief Shut down the Ecore_File library.
86 * @return 0 when the library is completely shut down, 1 or
89 * This function shuts down the Ecore_File library. It returns 0 when it has
90 * been called the same number of times than ecore_file_init(). In that case
91 * it shuts down all the services it uses.
96 if (--_ecore_file_init_count != 0)
97 return _ecore_file_init_count;
99 ecore_file_download_shutdown();
100 ecore_file_monitor_shutdown();
101 ecore_file_path_shutdown();
102 eina_log_domain_unregister(_ecore_file_log_dom);
103 _ecore_file_log_dom = -1;
104 return _ecore_file_init_count;
108 * @brief Get the time of the last modification to the given file.
110 * @param file The name of the file.
111 * @return Return the time of the last data modification, or 0 on
114 * This function returns the time of the last modification of
115 * @p file. On failure, it returns 0.
118 ecore_file_mod_time(const char *file)
122 if (stat(file, &st) < 0) return 0;
127 * @brief Get the size of the given file.
129 * @param file The name of the file.
130 * @return Return the size of the file in bytes, or 0 on failure.
132 * This function returns the size of @p file in bytes. On failure, it
136 ecore_file_size(const char *file)
140 if (stat(file, &st) < 0) return 0;
145 * @brief Check if the given file exists.
147 * @param file The name of the file.
148 * @return Return EINA_TRUE if the file exists, EINA_FALSE otherwise.
150 * This function returns EINA_TRUE if @p file exists on local filesystem,
151 * EINA_FALSE otherwise.
154 ecore_file_exists(const char *file)
157 if (!file) return EINA_FALSE;
159 /*Workaround so that "/" returns a true, otherwise we can't monitor "/" in ecore_file_monitor*/
160 if (stat(file, &st) < 0 && strcmp(file, "/")) return EINA_FALSE;
165 * @brief Check if the given file is a directory.
167 * @param file The name of the file.
168 * @return Return EINA_TRUE if the file exists and is a directory,
169 * EINA_FALSE otherwise.
171 * This function returns EINA_TRUE if @p file exists exists and is a
172 * directory on local filesystem, EINA_FALSE otherwise.
175 ecore_file_is_dir(const char *file)
179 if (stat(file, &st) < 0) return EINA_FALSE;
180 if (S_ISDIR(st.st_mode)) return EINA_TRUE;
184 static mode_t default_mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
187 * @brief Create a new directory.
189 * @param dir The name of the directory to create
190 * @return EINA_TRUE on successful creation, EINA_FALSE otherwise.
192 * This function creates the directory @p dir with the mode S_IRUSR |
193 * S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH. On
194 * success, it returns EINA_TRUE, EINA_FALSE otherwise.
197 ecore_file_mkdir(const char *dir)
199 if (mkdir(dir, default_mode) < 0) return EINA_FALSE;
204 * @brief Create complete directory in a batch.
206 * @param dirs The list of directories, null terminated.
207 * @return The number of successful directories created, -1 if dirs is
210 * This function creates all the directories that are in the null
211 * terminated array @p dirs. The function loops over the directories
212 * and call ecore_file_mkdir(). This function returns -1 if @p dirs is
213 * @c NULL, otherwise if returns the number of suceesfully created
217 ecore_file_mkdirs(const char **dirs)
221 if (!dirs) return -1;
223 for (; *dirs; dirs++)
224 if (ecore_file_mkdir(*dirs))
230 * @brief Create complete list of sub-directories in a batch (optimized).
232 * @param base The base directory to act on.
233 * @param subdirs The list of directories, null terminated.
234 * @return number of successful directories created, -1 on failure.
236 * This function creates all the directories that are in the null
237 * terminated array @p dirs in the @p base directory. If @p base does
238 * not exist, it will be created. The function loops over the directories
239 * and call ecore_file_mkdir(). The whole path of the directories must
240 * exist. So if base/a/b/c wants to be created, @p subdirs must
241 * contain "a", "a/b" and "a/b/c", in that order. This function
242 * returns -1 if @p dirs or @p base are @c NULL, or if @p base is
243 * empty ("\0"). It returns 0 is @p base is not a directory or
244 * invalid, or if it can't be created. Otherwise if returns the number
245 * of suceesfully created directories.
248 ecore_file_mksubdirs(const char *base, const char **subdirs)
250 #ifndef HAVE_ATFILE_SOURCE
259 if (!subdirs) return -1;
260 if ((!base) || (base[0] == '\0')) return -1;
262 if ((!ecore_file_is_dir(base)) && (!ecore_file_mkpath(base)))
265 #ifndef HAVE_ATFILE_SOURCE
266 baselen = eina_strlcpy(buf, base, sizeof(buf));
267 if ((baselen < 1) || (baselen + 1 >= (int)sizeof(buf)))
270 if (buf[baselen - 1] != '/')
283 for (; *subdirs; subdirs++)
287 #ifndef HAVE_ATFILE_SOURCE
288 eina_strlcpy(buf + baselen, *subdirs, sizeof(buf) - baselen);
289 if (stat(buf, &st) == 0)
291 if (fstatat(fd, *subdirs, &st, 0) == 0)
294 if (S_ISDIR(st.st_mode))
304 #ifndef HAVE_ATFILE_SOURCE
305 if (mkdir(buf, default_mode) == 0)
307 if (mkdirat(fd, *subdirs, default_mode) == 0)
317 #ifdef HAVE_ATFILE_SOURCE
325 * @brief Delete the given directory.
327 * @param dir The name of the directory to delete.
328 * @return EINA_TRUE on success, EINA_FALSE otherwise.
330 * This function deletes @p dir. It returns EINA_TRUE on success,
331 * EINA_FALSE otherwise.
334 ecore_file_rmdir(const char *dir)
336 if (rmdir(dir) < 0) return EINA_FALSE;
341 * @brief Delete the given file.
343 * @param file The name of the file to delete.
344 * @return EINA_TRUE on success, EINA_FALSE otherwise.
346 * This function deletes @p file. It returns EINA_TRUE on success,
347 * EINA_FALSE otherwise.
350 ecore_file_unlink(const char *file)
352 if (unlink(file) < 0) return EINA_FALSE;
357 * @brief Remove the given file or directory.
359 * @param file The name of the file or directory to delete.
360 * @return EINA_TRUE on success, EINA_FALSE otherwise.
362 * This function removes @p file. It returns EINA_TRUE on success,
363 * EINA_FALSE otherwise.
366 ecore_file_remove(const char *file)
368 if (remove(file) < 0) return EINA_FALSE;
373 * @brief Delete the given directory and all its contents.
375 * @param dir The name of the directory to delete.
376 * @return EINA_TRUE on success, EINA_FALSE otherwise.
378 * This function delete @p dir and all its contents. If @p dir is a
379 * link only the link is removed. It returns EINA_TRUE on success,
380 * EINA_FALSE otherwise.
383 ecore_file_recursive_rm(const char *dir)
387 char path[PATH_MAX], buf[PATH_MAX];
391 if (readlink(dir, buf, sizeof(buf)) > 0)
392 return ecore_file_unlink(dir);
394 ret = stat(dir, &st);
395 if ((ret == 0) && (S_ISDIR(st.st_mode)))
398 if (stat(dir, &st) == -1) return EINA_FALSE;
402 while ((dp = readdir(dirp)))
404 if ((strcmp(dp->d_name, ".")) && (strcmp(dp->d_name, "..")))
406 snprintf(path, PATH_MAX, "%s/%s", dir, dp->d_name);
407 if (!ecore_file_recursive_rm(path))
413 if (!ecore_file_rmdir(dir)) ret = 0;
421 if (ret == -1) return EINA_FALSE;
422 return ecore_file_unlink(dir);
426 static inline Eina_Bool
427 _ecore_file_mkpath_if_not_exists(const char *path)
431 /* Windows: path like C: or D: etc are valid, but stat() returns an error */
433 if ((strlen(path) == 2) &&
434 ((path[0] >= 'a' && path[0] <= 'z') ||
435 (path[0] >= 'A' && path[0] <= 'Z')) &&
440 if (stat(path, &st) < 0)
441 return ecore_file_mkdir(path);
442 else if (!S_ISDIR(st.st_mode))
449 * @brief Create a complete path.
451 * @param path The path to create
452 * @return EINA_TRUE on success, EINA_FALSE otherwise.
454 * This function create @p path and all the subdirectories it
455 * contains. The separator is '/' or '\'. If @p path exists, this
456 * function returns EINA_TRUE immediatly. It returns EINA_TRUE on
457 * success, EINA_FALSE otherwise.
460 ecore_file_mkpath(const char *path)
465 if (ecore_file_is_dir(path))
468 for (i = 0; path[i] != '\0'; ss[i] = path[i], i++)
470 if (i == sizeof(ss) - 1) return EINA_FALSE;
471 if (((path[i] == '/') || (path[i] == '\\')) && (i > 0))
474 if (!_ecore_file_mkpath_if_not_exists(ss))
479 return _ecore_file_mkpath_if_not_exists(ss);
483 * @brief Create complete paths in a batch.
485 * @param paths list of paths, null terminated.
486 * @return number of successful paths created, -1 if paths is NULL.
488 * This function creates all the directories that are in the null
489 * terminated array @p paths. The function loops over the directories
490 * and call ecore_file_mkpath(), hence on Windows, '\' must be
491 * replaced by '/' before calling that function. This function
492 * returns -1 if @p paths is @c NULL. Otherwise if returns the number
493 * of suceesfully created directories.
496 ecore_file_mkpaths(const char **paths)
500 if (!paths) return -1;
502 for (; *paths; paths++)
503 if (ecore_file_mkpath(*paths))
509 * @brief Copy the given file to the given destination.
511 * @param src The name of the source file.
512 * @param dst The name of the destination file.
513 * @return EINA_TRUE on success, EINA_FALSE otherwise.
515 * This function copies @p src to @p dst. If the absolute path name of
516 * @p src and @p dst can not be computed, or if they are equal, or if
517 * the copy fails, the function returns EINA_FALSE, otherwise it
521 ecore_file_cp(const char *src, const char *dst)
525 char realpath1[PATH_MAX], realpath2[PATH_MAX];
527 Eina_Bool ret = EINA_TRUE;
529 if (!realpath(src, realpath1)) return EINA_FALSE;
530 if (realpath(dst, realpath2) && !strcmp(realpath1, realpath2)) return EINA_FALSE;
532 f1 = fopen(src, "rb");
533 if (!f1) return EINA_FALSE;
534 f2 = fopen(dst, "wb");
540 while ((num = fread(buf, 1, sizeof(buf), f1)) > 0)
542 if (fwrite(buf, 1, num, f2) != num) ret = EINA_FALSE;
550 * @brief Move the given file to the given destination.
552 * @param src The name of the source file.
553 * @param dst The name of the destination file.
554 * @return EINA_TRUE on success, EINA_FALSE otherwise.
556 * This function moves @p src to @p dst. It returns EINA_TRUE on
557 * success, EINA_FALSE otherwise.
560 ecore_file_mv(const char *src, const char *dst)
565 if (rename(src, dst))
567 // File cannot be moved directly because
568 // it resides on a different mount point.
573 // Make sure this is a regular file before
574 // we do anything fancy.
576 if (S_ISREG(st.st_mode))
580 dir = ecore_file_dir_get(dst);
581 // Since we can't directly rename, try to
582 // copy to temp file in the dst directory
584 snprintf(buf, sizeof(buf), "%s/.%s.tmp.XXXXXX",
585 dir, ecore_file_file_get(dst));
596 if (!ecore_file_cp(src, buf))
599 // Set file permissions of temp file to match src
600 chmod(buf, st.st_mode);
602 // Try to atomically move temp file to dst
603 if (rename(buf, dst))
605 // If we still cannot atomically move
606 // do a normal copy and hope for the best.
607 if (!ecore_file_cp(buf, dst))
611 // Delete temporary file and src
612 ecore_file_unlink(buf);
613 ecore_file_unlink(src);
628 * @brief Create a symbolic link.
630 * @param src The name of the file to link.
631 * @param dest The name of link.
632 * @return EINA_TRUE on success, EINA_FALSE otherwise.
634 * This function create the symbolic link @p dest of @p src. This
635 * function does not work on Windows. It returns EINA_TRUE on success,
636 * EINA_FALSE otherwise.
639 ecore_file_symlink(const char *src, const char *dest)
641 if (!symlink(src, dest)) return EINA_TRUE;
647 * @brief Get the canonicalized absolute path name.
649 * @param file The file path.
650 * @return The canonicalized absolute pathname or an empty string on
653 * This function returns the absolute path name of @p file as a newly
654 * allocated string. If @p file is @c NULL, or on error, this function
655 * returns an empty string. Otherwise, it returns the absolute path
656 * name. When not needed anymore, the returned value must be freed.
659 ecore_file_realpath(const char *file)
664 * Some implementations of realpath do not conform to the SUS.
665 * And as a result we must prevent a null arg from being passed.
667 if (!file) return strdup("");
668 if (!realpath(file, buf)) return strdup("");
674 * Get the filename from a given path.
676 * @param path The complete path.
677 * @return The file name.
679 * This function returns the file name of @p path. If @p path is
680 * @c NULL, the functions returns @c NULL.
683 ecore_file_file_get(const char *path)
687 if (!path) return NULL;
688 if ((result = strrchr(path, '/'))) result++;
689 else result = (char *)path;
694 * @brief Get the directory where the given file resides.
696 * @param file The name of the file.
697 * @return The directory name.
699 * This function returns the directory where @p file resides as anewly
700 * allocated string. If @p file is @c NULL or on error, this function
701 * returns @c NULL. When not needed anymore, the returned value must
705 ecore_file_dir_get(const char *file)
710 if (!file) return NULL;
711 strncpy(buf, file, PATH_MAX);
712 buf[PATH_MAX - 1] = 0;
718 * @brief Check if the given file can be read.
720 * @param file The name of the file.
721 * @return EINA_TRUE if the file is readable, EINA_FALSE otherwise.
723 * This function returns EINA_TRUE if @p file can be read, EINA_FALSE
727 ecore_file_can_read(const char *file)
729 if (!file) return EINA_FALSE;
730 if (!access(file, R_OK)) return EINA_TRUE;
735 * @brief Check if the given file can be written.
737 * @param file The name of the file.
738 * @return EINA_TRUE if the file is writable, EINA_FALSE otherwise.
740 * This function returns EINA_TRUE if @p file can be written, EINA_FALSE
744 ecore_file_can_write(const char *file)
746 if (!file) return EINA_FALSE;
747 if (!access(file, W_OK)) return EINA_TRUE;
752 * @bbrief Check if the given file can be executed.
754 * @param file The name of the file.
755 * @return EINA_TRUE if the file can be executed, EINA_FALSE otherwise.
757 * This function returns EINA_TRUE if @p file can be executed, EINA_FALSE
761 ecore_file_can_exec(const char *file)
763 if (!file) return EINA_FALSE;
764 if (!access(file, X_OK)) return EINA_TRUE;
769 * @brief Get the path pointed by the given link.
771 * @param lnk The name of the link.
772 * @return The path pointed by link or NULL.
774 * This function returns the path pointed by @p link as a newly
775 * allocated string. This function does not work on Windows. On
776 * failure, the function returns @c NULL. When not needed anymore, the
777 * returned value must be freed.
780 ecore_file_readlink(const char *lnk)
785 if ((count = readlink(lnk, buf, sizeof(buf) - 1)) < 0) return NULL;
791 * @brief Get the list of the files and directories in the given
794 * @param dir The name of the directory to list
795 * @return Return an Eina_List containing all the files in the directory;
796 * on failure it returns NULL.
798 * This function returns a list of allocated strings of all the files
799 * and directories contained in @p dir. The list will be sorted with
800 * strcoll as compare function. That means that you may want to set
801 * the current locale for the category LC_COLLATE with
802 * setlocale(). For more information see the manual pages of strcoll
803 * and setlocale. The list will not contain the directory entries for
804 * '.' and '..'. On failure, @c NULL is returned. When not needed
805 * anymore, the list elements must be freed.
808 ecore_file_ls(const char *dir)
813 Eina_List *list = NULL;
816 if (!dirp) return NULL;
818 while ((dp = readdir(dirp)))
820 if ((strcmp(dp->d_name, ".")) && (strcmp(dp->d_name, "..")))
822 f = strdup(dp->d_name);
823 list = eina_list_append(list, f);
828 list = eina_list_sort(list, eina_list_count(list), EINA_COMPARE_CB(strcoll));
834 * @brief Return the executable from the given command.
836 * @param app The application command, with parameters.
838 * This function returns the executable from @p app as a newly
839 * allocated string. Arguments are removed and escae characters are
840 * handled. If @p app is @c NULL, or on failure, the function returns
841 * @c NULL. When not needed anymore, the returned value must be freed.
844 ecore_file_app_exe_get(const char *app)
846 char *p, *pp, *exe1 = NULL, *exe2 = NULL;
848 int in_quot_dbl = 0, in_quot_sing = 0, restart = 0;
850 if (!app) return NULL;
854 while ((*p) && (isspace(*p))) p++;
863 else if (in_quot_dbl)
874 if ((isspace(*p)) && (!((p > app) && (p[-1] != '\\'))))
880 if (exe2 == exe1) return NULL;
889 homedir = getenv("HOME");
890 if (!homedir) return NULL;
891 len = strlen(homedir);
893 exe = malloc(len + exe2 - exe1 + 2);
894 if (!exe) return NULL;
898 strcpy(exe, homedir);
900 if (*(pp - 1) != '/')
910 exe = malloc(exe2 - exe1 + 1);
911 if (!exe) return NULL;
930 else if (in_quot_dbl)
936 /* technically this is wrong. double quotes also accept
947 /* technically we should handle special chars:
951 if ((p > exe1) && (p[-1] == '\\'))
959 else if ((p > exe1) && (*p == '='))
969 else if (isspace(*p))
989 * @brief Add the escape sequence ('\\') to the given file name.
991 * @param filename The file name.
992 * @return The file name with special characters escaped.
994 * This function adds the escape sequence ('\\') to the given file
995 * name and returns the result as a newly allocated string. If the
996 * length of the returned string is longer than PATH_MAX, or on
997 * failure, @c NULL is returned. When not needed anymore, the returned
998 * value must be freed.
1001 ecore_file_escape_name(const char *filename)
1011 if ((q - buf) > (PATH_MAX - 6)) return NULL;
1013 (*p == ' ') || (*p == '\t') || (*p == '\n') ||
1014 (*p == '\\') || (*p == '\'') || (*p == '\"') ||
1015 (*p == ';') || (*p == '!') || (*p == '#') ||
1016 (*p == '$') || (*p == '%') || (*p == '&') ||
1017 (*p == '*') || (*p == '(') || (*p == ')') ||
1018 (*p == '[') || (*p == ']') || (*p == '{') ||
1019 (*p == '}') || (*p == '|') || (*p == '<') ||
1020 (*p == '>') || (*p == '?')
1035 * @bried Remove the extension from the given file name.
1037 * @param path The name of the file.
1038 * @return A newly allocated string with the extension stripped out or
1041 * This function removes the extension from @p path and returns the
1042 * result as a newly allocated string. If @p path is @c NULL, or on
1043 * failure, the function returns @c NULL. When not needed anymore, the
1044 * returned value must be freed.
1047 ecore_file_strip_ext(const char *path)
1049 char *p, *file = NULL;
1054 p = strrchr(path, '.');
1056 file = strdup(path);
1059 file = malloc(((p - path) + 1) * sizeof(char));
1062 memcpy(file, path, (p - path));
1071 * @brief Check if the given directory is empty.
1073 * @param dir The name of the directory to check.
1074 * @return 1 if directory is empty, 0 if it has at least one file or
1075 * -1 in case of errors.
1077 * This functions checks if @p dir is empty. The '.' and '..' files
1078 * will be ignored. If @p dir is empty, 1 is returned, if it contains
1079 * at least 1 file, 0 is returned. On failure, -1 is returned.
1082 ecore_file_dir_is_empty(const char *dir)
1087 dirp = opendir(dir);
1088 if (!dirp) return -1;
1090 while ((dp = readdir(dirp)))
1092 if ((strcmp(dp->d_name, ".")) && (strcmp(dp->d_name, "..")))