18 #ifdef HAVE_FEATURES_H
19 # include <features.h>
24 #ifdef HAVE_ATFILE_SOURCE
28 #include "ecore_file_private.h"
30 int _ecore_file_log_dom = -1;
31 static int _ecore_file_init_count = 0;
33 /* externally accessible functions */
36 * @addtogroup Ecore_File_Group Ecore_File - Files and directories convenience functions
42 * @brief Initialize the Ecore_File library.
44 * @return 1 or greater on success, 0 on error.
46 * This function sets up Ecore_File and the services it will use
47 * (monitoring, downloading, PATH related feature). It returns 0 on
48 * failure, otherwise it returns the number of times it has already
51 * When Ecore_File is not used anymore, call ecore_file_shutdown()
52 * to shut down the Ecore_File library.
57 if (++_ecore_file_init_count != 1)
58 return _ecore_file_init_count;
61 return --_ecore_file_init_count;
63 _ecore_file_log_dom = eina_log_domain_register
64 ("ecore_file", ECORE_FILE_DEFAULT_LOG_COLOR);
65 if(_ecore_file_log_dom < 0)
67 EINA_LOG_ERR("Impossible to create a log domain for the ecore file module.");
68 return --_ecore_file_init_count;
70 ecore_file_path_init();
71 ecore_file_monitor_init();
72 ecore_file_download_init();
74 /* FIXME: were the tests disabled for a good reason ? */
77 if (!ecore_file_monitor_init())
78 goto shutdown_ecore_file_path;
80 if (!ecore_file_download_init())
81 goto shutdown_ecore_file_monitor;
84 return _ecore_file_init_count;
87 shutdown_ecore_file_monitor:
88 ecore_file_monitor_shutdown();
89 shutdown_ecore_file_path:
90 ecore_file_path_shutdown();
92 return --_ecore_file_init_count;
97 * @brief Shut down the Ecore_File library.
99 * @return 0 when the library is completely shut down, 1 or
102 * This function shuts down the Ecore_File library. It returns 0 when it has
103 * been called the same number of times than ecore_file_init(). In that case
104 * it shuts down all the services it uses.
107 ecore_file_shutdown()
109 if (--_ecore_file_init_count != 0)
110 return _ecore_file_init_count;
112 ecore_file_download_shutdown();
113 ecore_file_monitor_shutdown();
114 ecore_file_path_shutdown();
116 eina_log_domain_unregister(_ecore_file_log_dom);
117 _ecore_file_log_dom = -1;
121 return _ecore_file_init_count;
125 * @brief Get the time of the last modification to the given file.
127 * @param file The name of the file.
128 * @return Return the time of the last data modification, or 0 on
131 * This function returns the time of the last modification of
132 * @p file. On failure, it returns 0.
135 ecore_file_mod_time(const char *file)
139 if (stat(file, &st) < 0) return 0;
144 * @brief Get the size of the given file.
146 * @param file The name of the file.
147 * @return Return the size of the file in bytes, or 0 on failure.
149 * This function returns the size of @p file in bytes. On failure, it
153 ecore_file_size(const char *file)
157 if (stat(file, &st) < 0) return 0;
162 * @brief Check if the given file exists.
164 * @param file The name of the file.
165 * @return @c EINA_TRUE if the @p file exists, @c EINA_FALSE otherwise.
167 * This function returns @c EINA_TRUE if @p file exists on local filesystem,
168 * @c EINA_FALSE otherwise.
171 ecore_file_exists(const char *file)
174 if (!file) return EINA_FALSE;
176 /*Workaround so that "/" returns a true, otherwise we can't monitor "/" in ecore_file_monitor*/
177 if (stat(file, &st) < 0 && strcmp(file, "/")) return EINA_FALSE;
182 * @brief Check if the given file is a directory.
184 * @param file The name of the file.
185 * @return @c EINA_TRUE if the file exists and is a directory, @c EINA_FALSE
188 * This function returns @c EINA_TRUE if @p file exists exists and is a
189 * directory on local filesystem, @c EINA_FALSE otherwise.
192 ecore_file_is_dir(const char *file)
196 if (stat(file, &st) < 0) return EINA_FALSE;
197 if (S_ISDIR(st.st_mode)) return EINA_TRUE;
201 static mode_t default_mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
204 * @brief Create a new directory.
206 * @param dir The name of the directory to create
207 * @return @c EINA_TRUE on successful creation, @c EINA_FALSE otherwise.
209 * This function creates the directory @p dir with the mode S_IRUSR |
210 * S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH. On
211 * success, it returns @c EINA_TRUE, @c EINA_FALSE otherwise.
214 ecore_file_mkdir(const char *dir)
216 if (mkdir(dir, default_mode) < 0) return EINA_FALSE;
221 * @brief Create complete directory in a batch.
223 * @param dirs The list of directories, null terminated.
224 * @return The number of successful directories created, -1 if dirs is
227 * This function creates all the directories that are in the null
228 * terminated array @p dirs. The function loops over the directories
229 * and call ecore_file_mkdir(). This function returns -1 if @p dirs is
230 * @c NULL, otherwise if returns the number of suceesfully created
234 ecore_file_mkdirs(const char **dirs)
238 if (!dirs) return -1;
240 for (; *dirs; dirs++)
241 if (ecore_file_mkdir(*dirs))
247 * @brief Create complete list of sub-directories in a batch (optimized).
249 * @param base The base directory to act on.
250 * @param subdirs The list of directories, null terminated.
251 * @return number of successful directories created, -1 on failure.
253 * This function creates all the directories that are in the null
254 * terminated array @p dirs in the @p base directory. If @p base does
255 * not exist, it will be created. The function loops over the directories
256 * and call ecore_file_mkdir(). The whole path of the directories must
257 * exist. So if base/a/b/c wants to be created, @p subdirs must
258 * contain "a", "a/b" and "a/b/c", in that order. This function
259 * returns -1 if @p dirs or @p base are @c NULL, or if @p base is
260 * empty ("\0"). It returns 0 is @p base is not a directory or
261 * invalid, or if it can't be created. Otherwise if returns the number
262 * of suceesfully created directories.
265 ecore_file_mksubdirs(const char *base, const char **subdirs)
267 #ifndef HAVE_ATFILE_SOURCE
276 if (!subdirs) return -1;
277 if ((!base) || (base[0] == '\0')) return -1;
279 if ((!ecore_file_is_dir(base)) && (!ecore_file_mkpath(base)))
282 #ifndef HAVE_ATFILE_SOURCE
283 baselen = eina_strlcpy(buf, base, sizeof(buf));
284 if ((baselen < 1) || (baselen + 1 >= (int)sizeof(buf)))
287 if (buf[baselen - 1] != '/')
300 for (; *subdirs; subdirs++)
304 #ifndef HAVE_ATFILE_SOURCE
305 eina_strlcpy(buf + baselen, *subdirs, sizeof(buf) - baselen);
306 if (stat(buf, &st) == 0)
308 if (fstatat(fd, *subdirs, &st, 0) == 0)
311 if (S_ISDIR(st.st_mode))
321 #ifndef HAVE_ATFILE_SOURCE
322 if (mkdir(buf, default_mode) == 0)
324 if (mkdirat(fd, *subdirs, default_mode) == 0)
334 #ifdef HAVE_ATFILE_SOURCE
342 * @brief Delete the given directory.
344 * @param dir The name of the directory to delete.
345 * @return @c EINA_TRUE on success, @c EINA_FALSE otherwise.
347 * This function deletes @p dir. It returns @c EINA_TRUE on success,
348 * @c EINA_FALSE otherwise.
351 ecore_file_rmdir(const char *dir)
353 if (rmdir(dir) < 0) return EINA_FALSE;
358 * @brief Delete the given file.
360 * @param file The name of the file to delete.
361 * @return @c EINA_TRUE on success, @c EINA_FALSE otherwise.
363 * This function deletes @p file. It returns @c EINA_TRUE on success,
364 * @c EINA_FALSE otherwise.
367 ecore_file_unlink(const char *file)
369 if (unlink(file) < 0) return EINA_FALSE;
374 * @brief Remove the given file or directory.
376 * @param file The name of the file or directory to delete.
377 * @return @c EINA_TRUE on success, @c EINA_FALSE otherwise.
379 * This function removes @p file. It returns @c EINA_TRUE on success,
380 * @c EINA_FALSE otherwise.
383 ecore_file_remove(const char *file)
385 if (remove(file) < 0) return EINA_FALSE;
390 * @brief Delete the given directory and all its contents.
392 * @param dir The name of the directory to delete.
393 * @return @c EINA_TRUE on success, @c EINA_FALSE otherwise.
395 * This function delete @p dir and all its contents. If @p dir is a
396 * link only the link is removed. It returns @c EINA_TRUE on success,
397 * @c EINA_FALSE otherwise.
400 ecore_file_recursive_rm(const char *dir)
407 if (readlink(dir, buf, sizeof(buf) - 1) > 0)
408 return ecore_file_unlink(dir);
410 ret = stat(dir, &st);
411 if ((ret == 0) && (S_ISDIR(st.st_mode)))
413 Eina_File_Direct_Info *info;
416 if (stat(dir, &st) == -1) return EINA_FALSE; /* WOOT: WHY ARE WE CALLING STAT TWO TIMES ??? */
418 it = eina_file_direct_ls(dir);
419 EINA_ITERATOR_FOREACH(it, info)
421 if (!ecore_file_recursive_rm(info->path))
424 eina_iterator_free(it);
426 if (!ecore_file_rmdir(dir)) ret = 0;
434 if (ret == -1) return EINA_FALSE;
435 return ecore_file_unlink(dir);
439 static inline Eina_Bool
440 _ecore_file_mkpath_if_not_exists(const char *path)
444 /* Windows: path like C: or D: etc are valid, but stat() returns an error */
446 if ((strlen(path) == 2) &&
447 ((path[0] >= 'a' && path[0] <= 'z') ||
448 (path[0] >= 'A' && path[0] <= 'Z')) &&
453 if (stat(path, &st) < 0)
454 return ecore_file_mkdir(path);
455 else if (!S_ISDIR(st.st_mode))
462 * @brief Create a complete path.
464 * @param path The path to create
465 * @return @c EINA_TRUE on success, @c EINA_FALSE otherwise.
467 * This function creates @p path and all the subdirectories it
468 * contains. The separator is '/' or '\'. If @p path exists, this
469 * function returns @c EINA_TRUE immediately. It returns @c EINA_TRUE on
470 * success, @c EINA_FALSE otherwise.
473 ecore_file_mkpath(const char *path)
478 if (ecore_file_is_dir(path))
481 for (i = 0; path[i] != '\0'; ss[i] = path[i], i++)
483 if (i == sizeof(ss) - 1) return EINA_FALSE;
484 if (((path[i] == '/') || (path[i] == '\\')) && (i > 0))
487 if (!_ecore_file_mkpath_if_not_exists(ss))
492 return _ecore_file_mkpath_if_not_exists(ss);
496 * @brief Create complete paths in a batch.
498 * @param paths list of paths, null terminated.
499 * @return number of successful paths created, -1 if paths is NULL.
501 * This function creates all the directories that are in the null
502 * terminated array @p paths. The function loops over the directories
503 * and call ecore_file_mkpath(), hence on Windows, '\' must be
504 * replaced by '/' before calling that function. This function
505 * returns -1 if @p paths is @c NULL. Otherwise if returns the number
506 * of suceesfully created directories.
509 ecore_file_mkpaths(const char **paths)
513 if (!paths) return -1;
515 for (; *paths; paths++)
516 if (ecore_file_mkpath(*paths))
522 * @brief Copy the given file to the given destination.
524 * @param src The name of the source file.
525 * @param dst The name of the destination file.
526 * @return @c EINA_TRUE on success, @c EINA_FALSE otherwise.
528 * This function copies @p src to @p dst. If the absolute path name of
529 * @p src and @p dst can not be computed, or if they are equal, or if
530 * the copy fails, the function returns @c EINA_FALSE, otherwise it
531 * returns @c EINA_TRUE.
534 ecore_file_cp(const char *src, const char *dst)
538 char realpath1[PATH_MAX], realpath2[PATH_MAX];
540 Eina_Bool ret = EINA_TRUE;
542 if (!realpath(src, realpath1)) return EINA_FALSE;
543 if (realpath(dst, realpath2) && !strcmp(realpath1, realpath2)) return EINA_FALSE;
545 f1 = fopen(src, "rb");
546 if (!f1) return EINA_FALSE;
547 f2 = fopen(dst, "wb");
553 while ((num = fread(buf, 1, sizeof(buf), f1)) > 0)
555 if (fwrite(buf, 1, num, f2) != num) ret = EINA_FALSE;
563 * @brief Move the given file to the given destination.
565 * @param src The name of the source file.
566 * @param dst The name of the destination file.
567 * @return @c EINA_TRUE on success, @c EINA_FALSE otherwise.
569 * This function moves @p src to @p dst. It returns @c EINA_TRUE on
570 * success, @c EINA_FALSE otherwise.
573 ecore_file_mv(const char *src, const char *dst)
578 if (rename(src, dst))
580 // File cannot be moved directly because
581 // it resides on a different mount point.
586 // Make sure this is a regular file before
587 // we do anything fancy.
589 if (S_ISREG(st.st_mode))
593 dir = ecore_file_dir_get(dst);
594 // Since we can't directly rename, try to
595 // copy to temp file in the dst directory
597 snprintf(buf, sizeof(buf), "%s/.%s.tmp.XXXXXX",
598 dir, ecore_file_file_get(dst));
601 if (fd < 0) goto FAIL;
605 if (!ecore_file_cp(src, buf))
608 // Set file permissions of temp file to match src
609 chmod(buf, st.st_mode);
611 // Try to atomically move temp file to dst
612 if (rename(buf, dst))
614 // If we still cannot atomically move
615 // do a normal copy and hope for the best.
616 if (!ecore_file_cp(buf, dst))
620 // Delete temporary file and src
621 ecore_file_unlink(buf);
622 ecore_file_unlink(src);
637 * @brief Create a symbolic link.
639 * @param src The name of the file to link.
640 * @param dest The name of link.
641 * @return @c EINA_TRUE on success, @c EINA_FALSE otherwise.
643 * This function create the symbolic link @p dest of @p src. This
644 * function does not work on Windows. It returns @c EINA_TRUE on success,
645 * @c EINA_FALSE otherwise.
648 ecore_file_symlink(const char *src, const char *dest)
650 if (!symlink(src, dest)) return EINA_TRUE;
656 * @brief Get the canonicalized absolute path name.
658 * @param file The file path.
659 * @return The canonicalized absolute pathname or an empty string on
662 * This function returns the absolute path name of @p file as a newly
663 * allocated string. If @p file is @c NULL, or on error, this function
664 * returns an empty string. Otherwise, it returns the absolute path
665 * name. When not needed anymore, the returned value must be freed.
668 ecore_file_realpath(const char *file)
673 * Some implementations of realpath do not conform to the SUS.
674 * And as a result we must prevent a null arg from being passed.
676 if (!file) return strdup("");
677 if (!realpath(file, buf)) return strdup("");
683 * Get the filename from a given path.
685 * @param path The complete path.
686 * @return The file name.
688 * This function returns the file name of @p path. If @p path is
689 * @c NULL, the functions returns @c NULL.
692 ecore_file_file_get(const char *path)
696 if (!path) return NULL;
697 if ((result = strrchr(path, '/'))) result++;
698 else result = (char *)path;
703 * @brief Get the directory where the given file resides.
705 * @param file The name of the file.
706 * @return The directory name.
708 * This function returns the directory where @p file resides as anewly
709 * allocated string. If @p file is @c NULL or on error, this function
710 * returns @c NULL. When not needed anymore, the returned value must
714 ecore_file_dir_get(const char *file)
719 if (!file) return NULL;
720 strncpy(buf, file, PATH_MAX);
721 buf[PATH_MAX - 1] = 0;
727 * @brief Check if the given file can be read.
729 * @param file The name of the file.
730 * @return @c EINA_TRUE if the @p file is readable, @c EINA_FALSE otherwise.
732 * This function returns @c EINA_TRUE if @p file can be read, @c EINA_FALSE
736 ecore_file_can_read(const char *file)
738 if (!file) return EINA_FALSE;
739 if (!access(file, R_OK)) return EINA_TRUE;
744 * @brief Check if the given file can be written.
746 * @param file The name of the file.
747 * @return @c EINA_TRUE if the @p file is writable, @c EINA_FALSE otherwise.
749 * This function returns @c EINA_TRUE if @p file can be written, @c EINA_FALSE
753 ecore_file_can_write(const char *file)
755 if (!file) return EINA_FALSE;
756 if (!access(file, W_OK)) return EINA_TRUE;
761 * @brief Check if the given file can be executed.
763 * @param file The name of the file.
764 * @return @c EINA_TRUE if the @p file can be executed, @c EINA_FALSE
767 * This function returns @c EINA_TRUE if @p file can be executed, @c EINA_FALSE
771 ecore_file_can_exec(const char *file)
773 if (!file) return EINA_FALSE;
774 if (!access(file, X_OK)) return EINA_TRUE;
779 * @brief Get the path pointed by the given link.
781 * @param lnk The name of the link.
782 * @return The path pointed by link or NULL.
784 * This function returns the path pointed by @p link as a newly
785 * allocated string. This function does not work on Windows. On
786 * failure, the function returns @c NULL. When not needed anymore, the
787 * returned value must be freed.
790 ecore_file_readlink(const char *lnk)
795 if ((count = readlink(lnk, buf, sizeof(buf) - 1)) < 0) return NULL;
801 * @brief Get the list of the files and directories in the given
804 * @param dir The name of the directory to list
805 * @return Return an Eina_List containing all the files in the directory;
806 * on failure it returns NULL.
808 * This function returns a list of allocated strings of all the files
809 * and directories contained in @p dir. The list will be sorted with
810 * strcoll as compare function. That means that you may want to set
811 * the current locale for the category LC_COLLATE with
812 * setlocale(). For more information see the manual pages of strcoll
813 * and setlocale. The list will not contain the directory entries for
814 * '.' and '..'. On failure, @c NULL is returned. When not needed
815 * anymore, the list elements must be freed.
818 ecore_file_ls(const char *dir)
820 Eina_File_Direct_Info *info;
822 Eina_List *list = NULL;
824 ls = eina_file_direct_ls(dir);
825 if (!ls) return NULL;
827 EINA_ITERATOR_FOREACH(ls, info)
831 f = strdup(info->path + info->name_start);
832 list = eina_list_append(list, f);
834 eina_iterator_free(ls);
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.
845 * @return The executable from @p app as a newly allocated string. Arguments
846 * are removed and escape characters are handled. If @p app is @c NULL, or
847 * on failure, the function returns @c NULL. When not needed anymore, the
848 * returned value must be freed.
851 ecore_file_app_exe_get(const char *app)
853 char *p, *pp, *exe1 = NULL, *exe2 = NULL;
855 int in_quot_dbl = 0, in_quot_sing = 0, restart = 0;
857 if (!app) return NULL;
861 while ((*p) && (isspace((unsigned char)*p))) p++;
870 else if (in_quot_dbl)
881 if ((isspace((unsigned char)*p)) && ((p <= app) || (p[-1] == '\\')))
887 if (exe2 == exe1) return NULL;
896 homedir = getenv("HOME");
897 if (!homedir) return NULL;
898 len = strlen(homedir);
900 exe = malloc(len + exe2 - exe1 + 2);
901 if (!exe) return NULL;
905 strcpy(exe, homedir);
907 if (*(pp - 1) != '/')
917 exe = malloc(exe2 - exe1 + 1);
918 if (!exe) return NULL;
937 else if (in_quot_dbl)
943 /* technically this is wrong. double quotes also accept
954 /* technically we should handle special chars:
958 if ((p > exe1) && (p[-1] == '\\'))
966 else if ((p > exe1) && (*p == '='))
976 else if (isspace((unsigned char)*p))
996 * @brief Add the escape sequence ('\\') to the given file name.
998 * @param filename The file name.
999 * @return The file name with special characters escaped.
1001 * This function adds the escape sequence ('\\') to the given file
1002 * name and returns the result as a newly allocated string. If the
1003 * length of the returned string is longer than PATH_MAX, or on
1004 * failure, @c NULL is returned. When not needed anymore, the returned
1005 * value must be freed.
1008 ecore_file_escape_name(const char *filename)
1014 EINA_SAFETY_ON_NULL_RETURN_VAL(filename, NULL);
1020 if ((q - buf) > (PATH_MAX - 6)) return NULL;
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 == '>') || (*p == '?')
1034 else if (*p == '\t')
1045 else if (*p == '\n')
1066 * @brief Remove the extension from the given file name.
1068 * @param path The name of the file.
1069 * @return A newly allocated string with the extension stripped out or
1070 * @c NULL on errors.
1072 * This function removes the extension from @p path and returns the
1073 * result as a newly allocated string. If @p path is @c NULL, or on
1074 * failure, the function returns @c NULL. When not needed anymore, the
1075 * returned value must be freed.
1078 ecore_file_strip_ext(const char *path)
1080 char *p, *file = NULL;
1085 p = strrchr(path, '.');
1087 file = strdup(path);
1090 file = malloc(((p - path) + 1) * sizeof(char));
1093 memcpy(file, path, (p - path));
1102 * @brief Check if the given directory is empty.
1104 * @param dir The name of the directory to check.
1105 * @return @c 1 if directory is empty, @c 0 if it has at least one file or
1106 * @c -1 in case of errors.
1108 * This functions checks if @p dir is empty. The '.' and '..' files
1109 * will be ignored. If @p dir is empty, 1 is returned, if it contains
1110 * at least one file, @c 0 is returned. On failure, @c -1 is returned.
1113 ecore_file_dir_is_empty(const char *dir)
1115 Eina_File_Direct_Info *info;
1118 it = eina_file_direct_ls(dir);
1121 EINA_ITERATOR_FOREACH(it, info)
1123 eina_iterator_free(it);
1127 eina_iterator_free(it);