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;
52 return --_ecore_file_init_count;
54 _ecore_file_log_dom = eina_log_domain_register
55 ("ecore_file", ECORE_FILE_DEFAULT_LOG_COLOR);
56 if(_ecore_file_log_dom < 0)
58 EINA_LOG_ERR("Impossible to create a log domain for the ecore file module.");
59 return --_ecore_file_init_count;
61 ecore_file_path_init();
62 ecore_file_monitor_init();
63 ecore_file_download_init();
65 /* FIXME: were the tests disabled for a good reason ? */
68 if (!ecore_file_monitor_init())
69 goto shutdown_ecore_file_path;
71 if (!ecore_file_download_init())
72 goto shutdown_ecore_file_monitor;
75 return _ecore_file_init_count;
78 shutdown_ecore_file_monitor:
79 ecore_file_monitor_shutdown();
80 shutdown_ecore_file_path:
81 ecore_file_path_shutdown();
83 return --_ecore_file_init_count;
88 * @brief Shut down the Ecore_File library.
90 * @return 0 when the library is completely shut down, 1 or
93 * This function shuts down the Ecore_File library. It returns 0 when it has
94 * been called the same number of times than ecore_file_init(). In that case
95 * it shuts down all the services it uses.
100 if (--_ecore_file_init_count != 0)
101 return _ecore_file_init_count;
103 ecore_file_download_shutdown();
104 ecore_file_monitor_shutdown();
105 ecore_file_path_shutdown();
107 eina_log_domain_unregister(_ecore_file_log_dom);
108 _ecore_file_log_dom = -1;
112 return _ecore_file_init_count;
116 * @brief Get the time of the last modification to the given file.
118 * @param file The name of the file.
119 * @return Return the time of the last data modification, or 0 on
122 * This function returns the time of the last modification of
123 * @p file. On failure, it returns 0.
126 ecore_file_mod_time(const char *file)
130 if (stat(file, &st) < 0) return 0;
135 * @brief Get the size of the given file.
137 * @param file The name of the file.
138 * @return Return the size of the file in bytes, or 0 on failure.
140 * This function returns the size of @p file in bytes. On failure, it
144 ecore_file_size(const char *file)
148 if (stat(file, &st) < 0) return 0;
153 * @brief Check if the given file exists.
155 * @param file The name of the file.
156 * @return Return EINA_TRUE if the file exists, EINA_FALSE otherwise.
158 * This function returns EINA_TRUE if @p file exists on local filesystem,
159 * EINA_FALSE otherwise.
162 ecore_file_exists(const char *file)
165 if (!file) return EINA_FALSE;
167 /*Workaround so that "/" returns a true, otherwise we can't monitor "/" in ecore_file_monitor*/
168 if (stat(file, &st) < 0 && strcmp(file, "/")) return EINA_FALSE;
173 * @brief Check if the given file is a directory.
175 * @param file The name of the file.
176 * @return Return EINA_TRUE if the file exists and is a directory,
177 * EINA_FALSE otherwise.
179 * This function returns EINA_TRUE if @p file exists exists and is a
180 * directory on local filesystem, EINA_FALSE otherwise.
183 ecore_file_is_dir(const char *file)
187 if (stat(file, &st) < 0) return EINA_FALSE;
188 if (S_ISDIR(st.st_mode)) return EINA_TRUE;
192 static mode_t default_mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
195 * @brief Create a new directory.
197 * @param dir The name of the directory to create
198 * @return EINA_TRUE on successful creation, EINA_FALSE otherwise.
200 * This function creates the directory @p dir with the mode S_IRUSR |
201 * S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH. On
202 * success, it returns EINA_TRUE, EINA_FALSE otherwise.
205 ecore_file_mkdir(const char *dir)
207 if (mkdir(dir, default_mode) < 0) return EINA_FALSE;
212 * @brief Create complete directory in a batch.
214 * @param dirs The list of directories, null terminated.
215 * @return The number of successful directories created, -1 if dirs is
218 * This function creates all the directories that are in the null
219 * terminated array @p dirs. The function loops over the directories
220 * and call ecore_file_mkdir(). This function returns -1 if @p dirs is
221 * @c NULL, otherwise if returns the number of suceesfully created
225 ecore_file_mkdirs(const char **dirs)
229 if (!dirs) return -1;
231 for (; *dirs; dirs++)
232 if (ecore_file_mkdir(*dirs))
238 * @brief Create complete list of sub-directories in a batch (optimized).
240 * @param base The base directory to act on.
241 * @param subdirs The list of directories, null terminated.
242 * @return number of successful directories created, -1 on failure.
244 * This function creates all the directories that are in the null
245 * terminated array @p dirs in the @p base directory. If @p base does
246 * not exist, it will be created. The function loops over the directories
247 * and call ecore_file_mkdir(). The whole path of the directories must
248 * exist. So if base/a/b/c wants to be created, @p subdirs must
249 * contain "a", "a/b" and "a/b/c", in that order. This function
250 * returns -1 if @p dirs or @p base are @c NULL, or if @p base is
251 * empty ("\0"). It returns 0 is @p base is not a directory or
252 * invalid, or if it can't be created. Otherwise if returns the number
253 * of suceesfully created directories.
256 ecore_file_mksubdirs(const char *base, const char **subdirs)
258 #ifndef HAVE_ATFILE_SOURCE
267 if (!subdirs) return -1;
268 if ((!base) || (base[0] == '\0')) return -1;
270 if ((!ecore_file_is_dir(base)) && (!ecore_file_mkpath(base)))
273 #ifndef HAVE_ATFILE_SOURCE
274 baselen = eina_strlcpy(buf, base, sizeof(buf));
275 if ((baselen < 1) || (baselen + 1 >= (int)sizeof(buf)))
278 if (buf[baselen - 1] != '/')
291 for (; *subdirs; subdirs++)
295 #ifndef HAVE_ATFILE_SOURCE
296 eina_strlcpy(buf + baselen, *subdirs, sizeof(buf) - baselen);
297 if (stat(buf, &st) == 0)
299 if (fstatat(fd, *subdirs, &st, 0) == 0)
302 if (S_ISDIR(st.st_mode))
312 #ifndef HAVE_ATFILE_SOURCE
313 if (mkdir(buf, default_mode) == 0)
315 if (mkdirat(fd, *subdirs, default_mode) == 0)
325 #ifdef HAVE_ATFILE_SOURCE
333 * @brief Delete the given directory.
335 * @param dir The name of the directory to delete.
336 * @return EINA_TRUE on success, EINA_FALSE otherwise.
338 * This function deletes @p dir. It returns EINA_TRUE on success,
339 * EINA_FALSE otherwise.
342 ecore_file_rmdir(const char *dir)
344 if (rmdir(dir) < 0) return EINA_FALSE;
349 * @brief Delete the given file.
351 * @param file The name of the file to delete.
352 * @return EINA_TRUE on success, EINA_FALSE otherwise.
354 * This function deletes @p file. It returns EINA_TRUE on success,
355 * EINA_FALSE otherwise.
358 ecore_file_unlink(const char *file)
360 if (unlink(file) < 0) return EINA_FALSE;
365 * @brief Remove the given file or directory.
367 * @param file The name of the file or directory to delete.
368 * @return EINA_TRUE on success, EINA_FALSE otherwise.
370 * This function removes @p file. It returns EINA_TRUE on success,
371 * EINA_FALSE otherwise.
374 ecore_file_remove(const char *file)
376 if (remove(file) < 0) return EINA_FALSE;
381 * @brief Delete the given directory and all its contents.
383 * @param dir The name of the directory to delete.
384 * @return EINA_TRUE on success, EINA_FALSE otherwise.
386 * This function delete @p dir and all its contents. If @p dir is a
387 * link only the link is removed. It returns EINA_TRUE on success,
388 * EINA_FALSE otherwise.
391 ecore_file_recursive_rm(const char *dir)
395 char path[PATH_MAX], buf[PATH_MAX];
399 if (readlink(dir, buf, sizeof(buf)) > 0)
400 return ecore_file_unlink(dir);
402 ret = stat(dir, &st);
403 if ((ret == 0) && (S_ISDIR(st.st_mode)))
406 if (stat(dir, &st) == -1) return EINA_FALSE;
410 while ((dp = readdir(dirp)))
412 if ((strcmp(dp->d_name, ".")) && (strcmp(dp->d_name, "..")))
414 snprintf(path, PATH_MAX, "%s/%s", dir, dp->d_name);
415 if (!ecore_file_recursive_rm(path))
421 if (!ecore_file_rmdir(dir)) ret = 0;
429 if (ret == -1) return EINA_FALSE;
430 return ecore_file_unlink(dir);
434 static inline Eina_Bool
435 _ecore_file_mkpath_if_not_exists(const char *path)
439 /* Windows: path like C: or D: etc are valid, but stat() returns an error */
441 if ((strlen(path) == 2) &&
442 ((path[0] >= 'a' && path[0] <= 'z') ||
443 (path[0] >= 'A' && path[0] <= 'Z')) &&
448 if (stat(path, &st) < 0)
449 return ecore_file_mkdir(path);
450 else if (!S_ISDIR(st.st_mode))
457 * @brief Create a complete path.
459 * @param path The path to create
460 * @return EINA_TRUE on success, EINA_FALSE otherwise.
462 * This function creates @p path and all the subdirectories it
463 * contains. The separator is '/' or '\'. If @p path exists, this
464 * function returns EINA_TRUE immediately. It returns EINA_TRUE on
465 * success, EINA_FALSE otherwise.
468 ecore_file_mkpath(const char *path)
473 if (ecore_file_is_dir(path))
476 for (i = 0; path[i] != '\0'; ss[i] = path[i], i++)
478 if (i == sizeof(ss) - 1) return EINA_FALSE;
479 if (((path[i] == '/') || (path[i] == '\\')) && (i > 0))
482 if (!_ecore_file_mkpath_if_not_exists(ss))
487 return _ecore_file_mkpath_if_not_exists(ss);
491 * @brief Create complete paths in a batch.
493 * @param paths list of paths, null terminated.
494 * @return number of successful paths created, -1 if paths is NULL.
496 * This function creates all the directories that are in the null
497 * terminated array @p paths. The function loops over the directories
498 * and call ecore_file_mkpath(), hence on Windows, '\' must be
499 * replaced by '/' before calling that function. This function
500 * returns -1 if @p paths is @c NULL. Otherwise if returns the number
501 * of suceesfully created directories.
504 ecore_file_mkpaths(const char **paths)
508 if (!paths) return -1;
510 for (; *paths; paths++)
511 if (ecore_file_mkpath(*paths))
517 * @brief Copy the given file to the given destination.
519 * @param src The name of the source file.
520 * @param dst The name of the destination file.
521 * @return EINA_TRUE on success, EINA_FALSE otherwise.
523 * This function copies @p src to @p dst. If the absolute path name of
524 * @p src and @p dst can not be computed, or if they are equal, or if
525 * the copy fails, the function returns EINA_FALSE, otherwise it
529 ecore_file_cp(const char *src, const char *dst)
533 char realpath1[PATH_MAX], realpath2[PATH_MAX];
535 Eina_Bool ret = EINA_TRUE;
537 if (!realpath(src, realpath1)) return EINA_FALSE;
538 if (realpath(dst, realpath2) && !strcmp(realpath1, realpath2)) return EINA_FALSE;
540 f1 = fopen(src, "rb");
541 if (!f1) return EINA_FALSE;
542 f2 = fopen(dst, "wb");
548 while ((num = fread(buf, 1, sizeof(buf), f1)) > 0)
550 if (fwrite(buf, 1, num, f2) != num) ret = EINA_FALSE;
558 * @brief Move the given file to the given destination.
560 * @param src The name of the source file.
561 * @param dst The name of the destination file.
562 * @return EINA_TRUE on success, EINA_FALSE otherwise.
564 * This function moves @p src to @p dst. It returns EINA_TRUE on
565 * success, EINA_FALSE otherwise.
568 ecore_file_mv(const char *src, const char *dst)
573 if (rename(src, dst))
575 // File cannot be moved directly because
576 // it resides on a different mount point.
581 // Make sure this is a regular file before
582 // we do anything fancy.
584 if (S_ISREG(st.st_mode))
588 dir = ecore_file_dir_get(dst);
589 // Since we can't directly rename, try to
590 // copy to temp file in the dst directory
592 snprintf(buf, sizeof(buf), "%s/.%s.tmp.XXXXXX",
593 dir, ecore_file_file_get(dst));
604 if (!ecore_file_cp(src, buf))
607 // Set file permissions of temp file to match src
608 chmod(buf, st.st_mode);
610 // Try to atomically move temp file to dst
611 if (rename(buf, dst))
613 // If we still cannot atomically move
614 // do a normal copy and hope for the best.
615 if (!ecore_file_cp(buf, dst))
619 // Delete temporary file and src
620 ecore_file_unlink(buf);
621 ecore_file_unlink(src);
636 * @brief Create a symbolic link.
638 * @param src The name of the file to link.
639 * @param dest The name of link.
640 * @return EINA_TRUE on success, EINA_FALSE otherwise.
642 * This function create the symbolic link @p dest of @p src. This
643 * function does not work on Windows. It returns EINA_TRUE on success,
644 * EINA_FALSE otherwise.
647 ecore_file_symlink(const char *src, const char *dest)
649 if (!symlink(src, dest)) return EINA_TRUE;
655 * @brief Get the canonicalized absolute path name.
657 * @param file The file path.
658 * @return The canonicalized absolute pathname or an empty string on
661 * This function returns the absolute path name of @p file as a newly
662 * allocated string. If @p file is @c NULL, or on error, this function
663 * returns an empty string. Otherwise, it returns the absolute path
664 * name. When not needed anymore, the returned value must be freed.
667 ecore_file_realpath(const char *file)
672 * Some implementations of realpath do not conform to the SUS.
673 * And as a result we must prevent a null arg from being passed.
675 if (!file) return strdup("");
676 if (!realpath(file, buf)) return strdup("");
682 * Get the filename from a given path.
684 * @param path The complete path.
685 * @return The file name.
687 * This function returns the file name of @p path. If @p path is
688 * @c NULL, the functions returns @c NULL.
691 ecore_file_file_get(const char *path)
695 if (!path) return NULL;
696 if ((result = strrchr(path, '/'))) result++;
697 else result = (char *)path;
702 * @brief Get the directory where the given file resides.
704 * @param file The name of the file.
705 * @return The directory name.
707 * This function returns the directory where @p file resides as anewly
708 * allocated string. If @p file is @c NULL or on error, this function
709 * returns @c NULL. When not needed anymore, the returned value must
713 ecore_file_dir_get(const char *file)
718 if (!file) return NULL;
719 strncpy(buf, file, PATH_MAX);
720 buf[PATH_MAX - 1] = 0;
726 * @brief Check if the given file can be read.
728 * @param file The name of the file.
729 * @return EINA_TRUE if the file is readable, EINA_FALSE otherwise.
731 * This function returns EINA_TRUE if @p file can be read, EINA_FALSE
735 ecore_file_can_read(const char *file)
737 if (!file) return EINA_FALSE;
738 if (!access(file, R_OK)) return EINA_TRUE;
743 * @brief Check if the given file can be written.
745 * @param file The name of the file.
746 * @return EINA_TRUE if the file is writable, EINA_FALSE otherwise.
748 * This function returns EINA_TRUE if @p file can be written, EINA_FALSE
752 ecore_file_can_write(const char *file)
754 if (!file) return EINA_FALSE;
755 if (!access(file, W_OK)) return EINA_TRUE;
760 * @bbrief Check if the given file can be executed.
762 * @param file The name of the file.
763 * @return EINA_TRUE if the file can be executed, EINA_FALSE otherwise.
765 * This function returns EINA_TRUE if @p file can be executed, EINA_FALSE
769 ecore_file_can_exec(const char *file)
771 if (!file) return EINA_FALSE;
772 if (!access(file, X_OK)) return EINA_TRUE;
777 * @brief Get the path pointed by the given link.
779 * @param lnk The name of the link.
780 * @return The path pointed by link or NULL.
782 * This function returns the path pointed by @p link as a newly
783 * allocated string. This function does not work on Windows. On
784 * failure, the function returns @c NULL. When not needed anymore, the
785 * returned value must be freed.
788 ecore_file_readlink(const char *lnk)
793 if ((count = readlink(lnk, buf, sizeof(buf) - 1)) < 0) return NULL;
799 * @brief Get the list of the files and directories in the given
802 * @param dir The name of the directory to list
803 * @return Return an Eina_List containing all the files in the directory;
804 * on failure it returns NULL.
806 * This function returns a list of allocated strings of all the files
807 * and directories contained in @p dir. The list will be sorted with
808 * strcoll as compare function. That means that you may want to set
809 * the current locale for the category LC_COLLATE with
810 * setlocale(). For more information see the manual pages of strcoll
811 * and setlocale. The list will not contain the directory entries for
812 * '.' and '..'. On failure, @c NULL is returned. When not needed
813 * anymore, the list elements must be freed.
816 ecore_file_ls(const char *dir)
821 Eina_List *list = NULL;
824 if (!dirp) return NULL;
826 while ((dp = readdir(dirp)))
828 if ((strcmp(dp->d_name, ".")) && (strcmp(dp->d_name, "..")))
830 f = strdup(dp->d_name);
831 list = eina_list_append(list, f);
836 list = eina_list_sort(list, eina_list_count(list), EINA_COMPARE_CB(strcoll));
842 * @brief Return the executable from the given command.
844 * @param app The application command, with parameters.
846 * This function returns the executable from @p app as a newly
847 * allocated string. Arguments are removed and escae characters are
848 * handled. If @p app is @c NULL, or on failure, the function returns
849 * @c NULL. When not needed anymore, the returned value must be freed.
852 ecore_file_app_exe_get(const char *app)
854 char *p, *pp, *exe1 = NULL, *exe2 = NULL;
856 int in_quot_dbl = 0, in_quot_sing = 0, restart = 0;
858 if (!app) return NULL;
862 while ((*p) && (isspace(*p))) p++;
871 else if (in_quot_dbl)
882 if ((isspace(*p)) && (!((p > app) && (p[-1] != '\\'))))
888 if (exe2 == exe1) return NULL;
897 homedir = getenv("HOME");
898 if (!homedir) return NULL;
899 len = strlen(homedir);
901 exe = malloc(len + exe2 - exe1 + 2);
902 if (!exe) return NULL;
906 strcpy(exe, homedir);
908 if (*(pp - 1) != '/')
918 exe = malloc(exe2 - exe1 + 1);
919 if (!exe) return NULL;
938 else if (in_quot_dbl)
944 /* technically this is wrong. double quotes also accept
955 /* technically we should handle special chars:
959 if ((p > exe1) && (p[-1] == '\\'))
967 else if ((p > exe1) && (*p == '='))
977 else if (isspace(*p))
997 * @brief Add the escape sequence ('\\') to the given file name.
999 * @param filename The file name.
1000 * @return The file name with special characters escaped.
1002 * This function adds the escape sequence ('\\') to the given file
1003 * name and returns the result as a newly allocated string. If the
1004 * length of the returned string is longer than PATH_MAX, or on
1005 * failure, @c NULL is returned. When not needed anymore, the returned
1006 * value must be freed.
1009 ecore_file_escape_name(const char *filename)
1019 if ((q - buf) > (PATH_MAX - 6)) return NULL;
1021 (*p == ' ') || (*p == '\t') || (*p == '\n') ||
1022 (*p == '\\') || (*p == '\'') || (*p == '\"') ||
1023 (*p == ';') || (*p == '!') || (*p == '#') ||
1024 (*p == '$') || (*p == '%') || (*p == '&') ||
1025 (*p == '*') || (*p == '(') || (*p == ')') ||
1026 (*p == '[') || (*p == ']') || (*p == '{') ||
1027 (*p == '}') || (*p == '|') || (*p == '<') ||
1028 (*p == '>') || (*p == '?')
1043 * @bried Remove the extension from the given file name.
1045 * @param path The name of the file.
1046 * @return A newly allocated string with the extension stripped out or
1049 * This function removes the extension from @p path and returns the
1050 * result as a newly allocated string. If @p path is @c NULL, or on
1051 * failure, the function returns @c NULL. When not needed anymore, the
1052 * returned value must be freed.
1055 ecore_file_strip_ext(const char *path)
1057 char *p, *file = NULL;
1062 p = strrchr(path, '.');
1064 file = strdup(path);
1067 file = malloc(((p - path) + 1) * sizeof(char));
1070 memcpy(file, path, (p - path));
1079 * @brief Check if the given directory is empty.
1081 * @param dir The name of the directory to check.
1082 * @return 1 if directory is empty, 0 if it has at least one file or
1083 * -1 in case of errors.
1085 * This functions checks if @p dir is empty. The '.' and '..' files
1086 * will be ignored. If @p dir is empty, 1 is returned, if it contains
1087 * at least 1 file, 0 is returned. On failure, -1 is returned.
1090 ecore_file_dir_is_empty(const char *dir)
1095 dirp = opendir(dir);
1096 if (!dirp) return -1;
1098 while ((dp = readdir(dirp)))
1100 if ((strcmp(dp->d_name, ".")) && (strcmp(dp->d_name, "..")))