Eina: introduce API eina_file_path_relative() to uniformize this check

This commit is contained in:
Vincent Torri 2023-12-05 11:36:07 +01:00 committed by Carsten Haitzler (Rasterman)
parent cc66b5d011
commit 8272318d71
19 changed files with 64 additions and 193 deletions

View File

@ -833,7 +833,7 @@ efreet_desktop_command_path_absolute(const char *path)
int len = 0;
/* relative url */
if (path[0] != '/')
if (eina_file_path_relative(path))
{
if (!(buf = malloc(size))) return NULL;
if (!getcwd(buf, size))

View File

@ -3125,7 +3125,7 @@ efreet_menu_path_get(Efreet_Menu_Internal *internal, const char *suffix)
size_t len;
/* see if we've got an absolute or relative path */
if (suffix[0] == '/')
if (!eina_file_path_relative(suffix))
snprintf(path, sizeof(path), "%s", suffix);
else

View File

@ -245,6 +245,19 @@ struct _Eina_File_Line
*/
#define EINA_FILE_DIR_LIST_CB(function) ((Eina_File_Dir_List_Cb)function)
/**
* @brief Determines if a path is relative or absolute.
*
* @param[in] path The path to check.
*
* @return #EINA_TRUE if the path is relative, #EINA_FALSE otherwise.
*
* The implementation simply checks if the first char in the path is
* '/' on POSIX systems. On Windows, absolute paths begin with '\' or
* 'C:\' (or other letter). If it is not, the path is considered relative.
* If @p path is @c NULL, this function returns #EINA_FALSE.
*/
EINA_API Eina_Bool eina_file_path_relative(const char *path);
/**
* @brief Lists all the files on the directory by calling the function for every file found.

View File

@ -178,18 +178,6 @@ struct _Eina_Lines_Iterator
/** Macro for logging Eina debug messages */
#define DBG(...) EINA_LOG_DOM_DBG(_eina_file_log_dom, __VA_ARGS__)
/**
* @brief Determines if a path is relative or absolute.
* The implementation simply checks if the first char in the path is '/'. If it
* is not, the path is considered relative.
*
* @param[in] path The path to check.
*
* @return EINA_TRUE if the path is relative, EINA_FALSE otherwise.
*
*/
Eina_Bool eina_file_path_relative(const char *path);
/**
* @brief Gets the current directory and optionally appends a path to it.
* If a string was passed in via the @p path parameter, it will

View File

@ -521,13 +521,6 @@ eina_file_mmap_faulty(void *addr, long page_size)
* Simplified logic for portability layer with eina_file_common *
* ================================================================ */
Eina_Bool
eina_file_path_relative(const char *path)
{
if (*path != '/') return EINA_TRUE;
return EINA_FALSE;
}
Eina_Tmpstr *
eina_file_current_directory_get(const char *path, size_t len)
{
@ -562,6 +555,15 @@ eina_file_cleanup(Eina_Tmpstr *path)
EINA_API Eina_Bool
eina_file_path_relative(const char *path)
{
if (!path)
return EINA_FALSE;
return *path != '/';
}
EINA_API Eina_Bool
eina_file_dir_list(const char *dir,
Eina_Bool recursive,

View File

@ -515,12 +515,6 @@ _eina_file_mkdtemp(char *__template)
* Simplified logic for portability layer with eina_file_common *
* ================================================================ */
Eina_Bool
eina_file_path_relative(const char *path)
{
return !evil_path_is_absolute(path);
}
Eina_Tmpstr *
eina_file_current_directory_get(const char *path, size_t len)
{
@ -559,6 +553,29 @@ eina_file_cleanup(Eina_Tmpstr *path)
* API *
*============================================================================*/
EINA_API Eina_Bool
eina_file_path_relative(const char *path)
{
/* see
* https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#paths
* absolute path if:
* - is an UNC path (begins with \\)
* - has a drive letter (C:\). \ is important here, otherwise it is relative
* - begins with \
*/
if (!path || *path == '\\')
return EINA_FALSE;
if ((((*path >= 'a') && (*path <= 'z')) ||
((*path >= 'A') && (*path <= 'Z'))) &&
(path[1] == ':') &&
((path[2] == '\\') || (path[2] == '/')))
return EINA_FALSE;
return EINA_TRUE;
}
EINA_API Eina_Bool
eina_file_dir_list(const char *dir,
Eina_Bool recursive,

View File

@ -43,7 +43,7 @@
#ifdef _WIN32
# include <direct.h> /* getcwd */
# include <evil_private.h> /* path_is_absolute realpath dladdr */
# include <evil_private.h> /* realpath dladdr */
#endif
#include "eina_config.h"
@ -166,16 +166,6 @@ _path_sep_fix(char *buf)
#endif
}
static Eina_Bool
_path_absolute_check(const char *path)
{
#ifdef _WIN32
return evil_path_is_absolute(path);
#else
return (path[0] == EINA_PATH_SEP_C);
#endif
}
static int
_fallback(Eina_Prefix *pfx, const char *pkg_bin, const char *pkg_lib,
const char *pkg_data, const char *pkg_locale, const char *envprefix)
@ -284,7 +274,7 @@ _try_argv(Eina_Prefix *pfx, const char *argv0)
char buf[PATH_MAX], buf2[PATH_MAX];
/* 1. is argv0 abs path? */
if (_path_absolute_check(argv0))
if (!eina_file_path_relative(argv0))
{
if (access(argv0, X_OK) == 0)
{
@ -579,7 +569,7 @@ eina_prefix_new(const char *argv0, void *symbol, const char *envprefix,
{
if (info_dl.dli_fname)
{
if (_path_absolute_check(info_dl.dli_fname))
if (!eina_file_path_relative(info_dl.dli_fname))
{
INF("dladdr for symbol=%p: %s", symbol, info_dl.dli_fname);
char *rlink = realpath(info_dl.dli_fname, NULL);

View File

@ -2164,25 +2164,6 @@ _icon_size_min_get(Evas_Object *image)
return MAX(16, MIN(w, h));
}
/* FIXME: move this code to ecore */
#ifdef _WIN32
static Eina_Bool
_path_is_absolute(const char *path)
{
//TODO: Check if this works with all absolute paths in windows
return (isalpha(*path)) && (*(path + 1) == ':') &&
((*(path + 2) == '\\') || (*(path + 2) == '/'));
}
#else
static Eina_Bool
_path_is_absolute(const char *path)
{
return *path == '/';
}
#endif
static Eina_Bool
_internal_efl_ui_image_icon_set(Evas_Object *obj, const char *name, Eina_Bool *fdo)
{
@ -2217,7 +2198,7 @@ _internal_efl_ui_image_icon_set(Evas_Object *obj, const char *name, Eina_Bool *f
else
eina_stringshare_replace(&sd->stdicon, NULL);
if (_path_is_absolute(name))
if (!eina_file_path_relative(name))
{
if (fdo)
*fdo = EINA_FALSE;

View File

@ -2903,25 +2903,6 @@ _icon_size_min_get(Evas_Object *image)
return MAX(16, MIN(w, h));
}
/* FIXME: move this code to ecore */
#ifdef _WIN32
static Eina_Bool
_path_is_absolute(const char *path)
{
//TODO: Check if this works with all absolute paths in windows
return (isalpha(*path)) && (*(path + 1) == ':') &&
((*(path + 2) == '\\') || (*(path + 2) == '/'));
}
#else
static Eina_Bool
_path_is_absolute(const char *path)
{
return *path == '/';
}
#endif
static Eina_Bool
_internal_efl_ui_image_zoomable_icon_set(Evas_Object *obj, const char *name, Eina_Bool *fdo, Eina_Bool resize)
{
@ -2956,7 +2937,7 @@ _internal_efl_ui_image_zoomable_icon_set(Evas_Object *obj, const char *name, Ein
else
eina_stringshare_replace(&sd->stdicon, NULL);
if (_path_is_absolute(name))
if (!eina_file_path_relative(name))
{
if (fdo)
*fdo = EINA_FALSE;

View File

@ -28,25 +28,6 @@ static const Evas_Smart_Cb_Description _smart_callbacks[] = {
{NULL, NULL}
};
/* FIXME: move this code to ecore */
#ifdef _WIN32
static Eina_Bool
_path_is_absolute(const char *path)
{
//TODO: Check if this works with all absolute paths in windows
return (isalpha(*path)) && (*(path + 1) == ':') &&
((*(path + 2) == '\\') || (*(path + 2) == '/'));
}
#else
static Eina_Bool
_path_is_absolute(const char *path)
{
return *path == '/';
}
#endif
static inline int
_icon_size_min_get(Evas_Object *icon)
{
@ -462,7 +443,7 @@ _internal_elm_icon_standard_set(Evas_Object *obj,
return EINA_TRUE;
}
if (_path_is_absolute(name))
if (!eina_file_path_relative(name))
{
if (fdo)
*fdo = EINA_FALSE;

View File

@ -1308,7 +1308,7 @@ elm_quicklaunch_exe_path_get(const char *exe, const char *cwd)
const char *pathitr;
const Eina_List *l;
char buf[PATH_MAX];
if (exe[0] == '/') return strdup(exe);
if (!eina_file_path_relative(exe)) return strdup(exe);
if (cwd)
pathlist = eina_list_append(pathlist, eina_stringshare_add(cwd));
else

View File

@ -203,7 +203,7 @@ _elm_module_add(const char *name, const char *as)
{
Elm_Module *m;
if (name[0] == '/') return NULL;
if (!eina_file_path_relative(name)) return NULL;
m = eina_hash_find(modules, name);
if (m)

View File

@ -3,7 +3,7 @@
#endif
#ifdef _WIN32
# include <evil_private.h> /* strsep evil_path_absolute */
# include <evil_private.h> /* strsep */
#endif
#define EFL_ACCESS_OBJECT_PROTECTED
@ -1816,11 +1816,7 @@ elm_prefs_file_set(Eo *obj, const char *file, const char *page)
sd->file = eina_stringshare_printf("%s/%s", prefix, "preferences.epb");
else
{
#ifndef _WIN32
if (*file != '/') /* relative */
#else
if (!evil_path_is_absolute(file)) /* relative */
#endif
if (eina_file_path_relative(file))
sd->file = eina_stringshare_printf("%s/%s", prefix, file);
else
sd->file = eina_stringshare_add(file);

View File

@ -107,10 +107,9 @@ _elm_theme_file_item_add(Eina_Inlist **files, const char *item, Eina_Bool prepen
home = eina_environment_home_get();
buf = eina_strbuf_new();
if ((item[0] == '/') ||
if (!eina_file_path_relative(item) ||
((item[0] == '.') && (item[1] == '/')) ||
((item[0] == '.') && (item[1] == '.') && (item[2] == '/')) ||
((isalpha(item[0])) && (item[1] == ':')))
((item[0] == '.') && (item[1] == '.') && (item[2] == '/')))
{
f = eina_file_open(item, EINA_FALSE);
if (!f) goto on_error;
@ -898,9 +897,9 @@ elm_theme_list_item_path_get(const char *f, Eina_Bool *in_search_path)
if (!home) home = "";
}
if ((f[0] == '/') || ((f[0] == '.') && (f[1] == '/')) ||
((f[0] == '.') && (f[1] == '.') && (f[2] == '/')) ||
((isalpha(f[0])) && (f[1] == ':')))
if (!eina_file_path_relative(f) ||
((f[0] == '.') && (f[1] == '/')) ||
((f[0] == '.') && (f[1] == '.') && (f[2] == '/')))
{
if (in_search_path) *in_search_path = EINA_FALSE;
return strdup(f);

View File

@ -2,10 +2,6 @@
# include <config.h>
#endif
#ifdef _WIN32
# include <evil_private.h> /* evil_path_is_absolute */
#endif
#include <Eet.h>
#ifdef HAVE_FONTCONFIG
@ -67,17 +63,6 @@ static FcConfig *fc_config = NULL;
/* get the casefold feature! */
#include <unistd.h>
#include <sys/param.h>
int
_file_path_is_full_path(const char *path)
{
if (!path) return 0;
#ifdef _WIN32
if (evil_path_is_absolute(path)) return 1;
#else
if (path[0] == '/') return 1;
#endif
return 0;
}
static DATA64
_file_modified_time(const char *file)
@ -787,7 +772,7 @@ evas_font_load(const Eina_List *font_paths, int hinting, Evas_Font_Description *
}
if (!font) /* Source load failed */
{
if (_file_path_is_full_path((char *)nm)) /* Try filename */
if (!eina_file_path_relative((char *)nm)) /* Try filename */
font = (Evas_Font_Set *)evas_common_font_load((char *)nm, size, wanted_rend, bitmap_scalable);
else /* search font path */
{
@ -856,7 +841,7 @@ evas_font_load(const Eina_List *font_paths, int hinting, Evas_Font_Description *
}
if (!ok)
{
if (_file_path_is_full_path((char *)nm))
if (!eina_file_path_relative((char *)nm))
evas_common_font_add((RGBA_Font *)font, (char *)nm, size, wanted_rend, bitmap_scalable);
else
{

View File

@ -15,10 +15,6 @@
#include <unistd.h>
#include <sys/param.h>
#ifdef _WIN32
# include <evil_private.h> /* evil_path_is_absolute */
#endif
#include "evas_common_private.h"
#include "evas_private.h"
@ -28,18 +24,6 @@
# define EVAS_PATH_SEPARATOR "/"
#endif
int
evas_file_path_is_full_path(const char *path)
{
if (!path) return 0;
#ifdef _WIN32
if (evil_path_is_absolute(path)) return 1;
#else
if (path[0] == '/') return 1;
#endif
return 0;
}
char *
evas_file_path_join(const char *path, const char *end)
{

View File

@ -2,7 +2,6 @@
#define _EVAS_PATH_H
int evas_file_path_is_full_path (const char *path);
char *evas_file_path_join (const char *path, const char *end);
int evas_file_path_exists (const char *path);
int evas_file_path_is_file (const char *path);

View File

@ -188,25 +188,3 @@ _evil_last_error_display(const char *fct)
{
fprintf(stderr, "[Evil] [%s] ERROR: %s\n", fct, evil_last_error_get());
}
EVIL_API int
evil_path_is_absolute(const char *path)
{
size_t length;
if (!path)
return 0;
if (*path == '/' || *path == '\\') return 1;
length = strlen(path);
if (length < 3) return 0;
if ((((*path >= 'a') && (*path <= 'z')) ||
((*path >= 'A') && (*path <= 'Z'))) &&
(path[1] == ':') &&
((path[2] == '/') || (path[2] == '\\')))
return 1;
return 0;
}

View File

@ -81,27 +81,4 @@ EVIL_API const char *evil_format_message(long err);
EVIL_API const char *evil_last_error_get(void);
/**
* @brief check if the given path is absolute.
*
* @param path The path to check.
* @return 1 if the given path is absolute, 0 otherwise.
*
* Check if the path @p path is absolute or not. An absolute path must
* begin with a letter (upper or lower case), followed by by the char
* ':', followed by the char '/' or '\'. If @p path is absolute this
* function returns 1, otherwise it returns 0. If @p path is @c NULL,
* it returns 0.
*
* Conformity: Non applicable.
*
* Supported OS: Windows 95, Windows 98, Windows Me, Windows NT, Windows 2000,
* Windows XP.
*
* @since 1.7
*
* @ingroup Evil
*/
EVIL_API int evil_path_is_absolute(const char *path);
#endif /* __EVIL_UTIL_H__ */