From 49976ca388dfb66cce600e0b2169cae0c675f0e7 Mon Sep 17 00:00:00 2001 From: Vincent Torri Date: Fri, 29 Mar 2024 05:41:17 +0100 Subject: [PATCH] use eina_file_access() instead of access() if possible --- src/lib/ecore_evas/ecore_evas_module.c | 11 +-- src/lib/ecore_file/ecore_file.c | 98 +------------------------- src/lib/eet/eet_lib.c | 2 + src/lib/eeze/eeze_disk.c | 18 ++--- src/lib/eeze/eeze_disk_libmount.c | 2 +- src/lib/eina/eina_prefix.c | 6 +- src/lib/elementary/elm_main.c | 4 +- src/lib/ethumb/ethumb.c | 2 +- 8 files changed, 22 insertions(+), 121 deletions(-) diff --git a/src/lib/ecore_evas/ecore_evas_module.c b/src/lib/ecore_evas/ecore_evas_module.c index 4fdd8f70b0..0a4c6de997 100644 --- a/src/lib/ecore_evas/ecore_evas_module.c +++ b/src/lib/ecore_evas/ecore_evas_module.c @@ -22,15 +22,6 @@ static Eina_Module *_ecore_evas_vnc = NULL; # define ECORE_EVAS_ENGINE_NAME "module.so" #endif -static inline Eina_Bool -_file_exists(const char *file) -{ - if (!file) return EINA_FALSE; - - if (access(file, F_OK) == -1) return EINA_FALSE; - return EINA_TRUE; -} - static Eina_Module * _ecore_evas_vnc_server_module_try_load(const char *prefix, @@ -219,7 +210,7 @@ _ecore_evas_available_engines_get(void) eina_strbuf_append_printf(buf, "%s/%s/" ECORE_EVAS_ENGINE_NAME, info->path, MODULE_ARCH); - if (_file_exists(eina_strbuf_string_get(buf))) + if (eina_file_access(eina_strbuf_string_get(buf), EINA_FILE_ACCESS_MODE_EXIST)) { const char *name; diff --git a/src/lib/ecore_file/ecore_file.c b/src/lib/ecore_file/ecore_file.c index 66bdfe542e..a1a0a27874 100644 --- a/src/lib/ecore_file/ecore_file.c +++ b/src/lib/ecore_file/ecore_file.c @@ -606,111 +606,19 @@ ecore_file_dir_get(const char *file) EAPI Eina_Bool ecore_file_can_read(const char *file) { - if (!file) return EINA_FALSE; - if (!access(file, R_OK)) return EINA_TRUE; - return EINA_FALSE; + return eina_file_access(file, EINA_FILE_ACCESS_MODE_READ); } EAPI Eina_Bool ecore_file_can_write(const char *file) { - if (!file) return EINA_FALSE; - if (!access(file, W_OK)) return EINA_TRUE; - return EINA_FALSE; + return eina_file_access(file, EINA_FILE_ACCESS_MODE_WRITE); } EAPI Eina_Bool ecore_file_can_exec(const char *file) { -#ifdef _WIN32 - HANDLE h; - HANDLE fm; - char *base; - char *base_nt; - LARGE_INTEGER sz; - WORD characteristics; -#endif - - if (!file || !*file) return EINA_FALSE; - -#ifdef _WIN32 - /* - * we parse the file to check if it is a PE file (EXE or DLL) - * and we finally check whether it's a DLL or not. - * Reference : - * https://docs.microsoft.com/en-us/windows/win32/debug/pe-format - */ - h = CreateFile(file, GENERIC_READ, FILE_SHARE_READ, NULL, - OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); - if (h == INVALID_HANDLE_VALUE) - goto test_bat; - - if (!GetFileSizeEx(h, &sz)) - goto close_h; - - /* a PE file must have at least the DOS and NT headers */ - if (sz.QuadPart < (LONGLONG)(sizeof(IMAGE_DOS_HEADER) + sizeof(IMAGE_NT_HEADERS))) - goto close_h; - - fm = CreateFileMapping(h, NULL, PAGE_READONLY, 0, 0, NULL); - if (fm == NULL) - goto close_h; - - base = (char *)MapViewOfFile(fm, FILE_MAP_READ, 0, 0, 0); - CloseHandle(fm); - if (base == NULL) - goto close_h; - - /* - * the PE file begins with the DOS header. - * First magic number : the DOS header must begin with a DOS magic number, - * that is "MZ", that is 0x5a4d, stored in a WORD. - */ - if (*((WORD *)base) != 0x5a4d) - goto unmap_view; - - /* - * The position of the NT header is located at the offset 0x3c. - */ - base_nt = base + *((DWORD *)(base + 0x3c)); - /* - * The NT header begins with the magic number "PE\0\0", that is - * 0x00004550, stored in a DWORD. - */ - if (*((DWORD *)base_nt) != 0x00004550) - goto unmap_view; - - /* - * to get informations about executable (EXE or DLL), we look at - * the 'Characteristics' member of the NT header, located at the offset - * 22 (4 for the magic number, 18 for the offset) from base_nt. - * https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#characteristics - */ - characteristics = *((WORD *)(base_nt + 4 + 18)); - - UnmapViewOfFile(base); - CloseHandle(h); - - /* - * 0x0002 : if set, EXE or DLL - * 0x2000 : if set, DLL - */ - if ((characteristics & 0x0002) && !(characteristics & 0x2000)) - return EINA_TRUE; - unmap_view: - UnmapViewOfFile(base); - close_h: - CloseHandle(h); - test_bat: - /* - * a .bat file, considered as an executable, is only a text file, - * so we rely on the extension. Not the best but we cannot do more. - */ - return eina_str_has_extension(file, ".bat"); -#else - if (!access(file, X_OK)) return EINA_TRUE; -#endif - return EINA_FALSE; + return eina_file_access(file, EINA_FILE_ACCESS_MODE_EXEC); } EAPI char * diff --git a/src/lib/eet/eet_lib.c b/src/lib/eet/eet_lib.c index 59b4e49201..8794b221ae 100644 --- a/src/lib/eet/eet_lib.c +++ b/src/lib/eet/eet_lib.c @@ -1568,6 +1568,7 @@ eet_open(const char *file, { if (mode == EET_FILE_MODE_READ_WRITE) { + /* do not use eina_file_access() here */ ret = access(file, W_OK); if ((ret != 0) && (errno != ENOENT)) return NULL; } @@ -1606,6 +1607,7 @@ open_error: size = 0; fp = NULL; + /* do not use eina_file_access() here */ ret = access(file, W_OK); if ((ret != 0) && (errno != ENOENT)) return NULL; } diff --git a/src/lib/eeze/eeze_disk.c b/src/lib/eeze/eeze_disk.c index 9747b9801a..35a01ff255 100644 --- a/src/lib/eeze/eeze_disk.c +++ b/src/lib/eeze/eeze_disk.c @@ -460,23 +460,23 @@ eeze_disk_removable_get(Eeze_Disk *disk) EAPI Eina_Bool eeze_disk_can_mount(void) { - if (sizeof(EEZE_MOUNT_BIN) == sizeof("")) - return EINA_FALSE; - return access(EEZE_MOUNT_BIN, X_OK | R_OK) == 0; + return eina_file_access(EEZE_MOUNT_BIN, + EINA_FILE_ACCESS_MODE_EXEC | + EINA_FILE_ACCESS_MODE_READ); } EAPI Eina_Bool eeze_disk_can_unmount(void) { - if (sizeof(EEZE_UNMOUNT_BIN) == sizeof("")) - return EINA_FALSE; - return access(EEZE_UNMOUNT_BIN, X_OK | R_OK) == 0; + return eina_file_access(EEZE_UNMOUNT_BIN, + EINA_FILE_ACCESS_MODE_EXEC | + EINA_FILE_ACCESS_MODE_READ); } EAPI Eina_Bool eeze_disk_can_eject(void) { - if (sizeof(EEZE_EJECT_BIN) == sizeof("")) - return EINA_FALSE; - return access(EEZE_EJECT_BIN, X_OK | R_OK) == 0; + return eina_file_access(EEZE_EJECT_BIN, + EINA_FILE_ACCESS_MODE_EXEC | + EINA_FILE_ACCESS_MODE_READ); } diff --git a/src/lib/eeze/eeze_disk_libmount.c b/src/lib/eeze/eeze_disk_libmount.c index 9cc1980bb2..fdc9538042 100644 --- a/src/lib/eeze/eeze_disk_libmount.c +++ b/src/lib/eeze/eeze_disk_libmount.c @@ -56,7 +56,7 @@ static Eina_Bool _eeze_mount_lock_mtab(void) { // DBG("Locking mlock: %s", mnt_lock_get_linkfile(_eeze_mtab_lock)); - if (EINA_LIKELY(access("/etc/mtab", W_OK))) + if (EINA_LIKELY(!eina_file_access("/etc/mtab", EINA_FILE_ACCESS_MODE_WRITE))) { INF("Insufficient privs for mtab lock, continuing without lock"); return EINA_TRUE; diff --git a/src/lib/eina/eina_prefix.c b/src/lib/eina/eina_prefix.c index 89aa78ce9f..252c28056f 100644 --- a/src/lib/eina/eina_prefix.c +++ b/src/lib/eina/eina_prefix.c @@ -276,7 +276,7 @@ _try_argv(Eina_Prefix *pfx, const char *argv0) /* 1. is argv0 abs path? */ if (!eina_file_path_relative(argv0)) { - if (access(argv0, X_OK) == 0) + if (eina_file_access(argv0, EINA_FILE_ACCESS_MODE_EXEC)) { INF("Executable argv0 is full path = %s", argv0); STRDUP_REP(pfx->exe_path, argv0); @@ -296,7 +296,7 @@ _try_argv(Eina_Prefix *pfx, const char *argv0) eina_file_path_join(joined, len, buf2, argv0); if (realpath(joined, buf)) { - if (access(buf, X_OK) == 0) + if (eina_file_access(buf, EINA_FILE_ACCESS_MODE_EXEC)) { INF("Executable relative argv0=%s, cwd=%s, realpath=%s", argv0, buf2, buf); @@ -340,7 +340,7 @@ _try_argv(Eina_Prefix *pfx, const char *argv0) strcpy(buf2 + len + 1, argv0); if (realpath(buf2, buf)) { - if (access(buf, X_OK) == 0) + if (eina_file_access(buf, EINA_FILE_ACCESS_MODE_EXEC)) { STRDUP_REP(pfx->exe_path, buf); INF("Path %s is executable", pfx->exe_path); diff --git a/src/lib/elementary/elm_main.c b/src/lib/elementary/elm_main.c index 0fed8c28fe..b1a355ab69 100644 --- a/src/lib/elementary/elm_main.c +++ b/src/lib/elementary/elm_main.c @@ -1044,7 +1044,7 @@ elm_quicklaunch_prepare(int argc, strcat(p, "../lib/"); strcat(p, exename); strcat(p, LIBEXT); - if (access(exe2, R_OK | X_OK) != 0) + if (!eina_file_access(exe2, EINA_FILE_ACCESS_MODE_EXEC | EINA_FILE_ACCESS_MODE_READ)) ELM_SAFE_FREE(exe2, free); /* Try linking to executable first. Works with PIE files. */ @@ -1348,7 +1348,7 @@ elm_quicklaunch_exe_path_get(const char *exe, const char *cwd) EINA_LIST_FOREACH(pathlist, l, pathitr) { snprintf(buf, sizeof(buf), "%s/%s", pathitr, exe); - if (!access(buf, R_OK | X_OK)) return strdup(buf); + if (eina_file_access(buf, EINA_FILE_ACCESS_MODE_EXEC | EINA_FILE_ACCESS_MODE_READ)) return strdup(buf); } return NULL; } diff --git a/src/lib/ethumb/ethumb.c b/src/lib/ethumb/ethumb.c index aa4e824c28..9df7453b31 100644 --- a/src/lib/ethumb/ethumb.c +++ b/src/lib/ethumb/ethumb.c @@ -852,7 +852,7 @@ ethumb_file_set(Ethumb *e, const char *path, const char *key) sanitized_path = eina_file_path_sanitize(path); DBG("ethumb=%p, path=%s, key=%s", e, sanitized_path ? sanitized_path : "", key ? key : ""); - if (sanitized_path && access(sanitized_path, R_OK)) + if (sanitized_path && !eina_file_access(sanitized_path, EINA_FILE_ACCESS_MODE_READ)) { free(sanitized_path); return EINA_FALSE;