From 846707bed01f3c599df7bbac3229e134509e49ad Mon Sep 17 00:00:00 2001 From: diamant3 Date: Thu, 13 Jun 2024 21:06:13 +0800 Subject: [PATCH 1/5] add realpath --- src/libcglue/Makefile.am | 3 ++- src/libcglue/glue.c | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/libcglue/Makefile.am b/src/libcglue/Makefile.am index c8c2126a..8b8df2d7 100755 --- a/src/libcglue/Makefile.am +++ b/src/libcglue/Makefile.am @@ -29,7 +29,8 @@ GLUE_OBJS = __dummy_passwd.o __psp_heap_blockid.o __psp_free_heap.o _fork.o _wai _rename.o _getpid.o _kill.o _sbrk.o _gettimeofday.o _times.o ftime.o clock_getres.o clock_gettime.o clock_settime.o \ _isatty.o symlink.o truncate.o chmod.o fchmod.o pathconf.o readlink.o utime.o fchown.o _getentropy.o getpwuid.o \ fsync.o getpwnam.o getuid.o geteuid.o basename.o statvfs.o \ - openat.o renameat.o fchmodat.o fstatat.o mkdirat.o faccessat.o fchownat.o linkat.o readlinkat.o unlinkat.o + openat.o renameat.o fchmodat.o fstatat.o mkdirat.o faccessat.o fchownat.o linkat.o readlinkat.o unlinkat.o \ + realpath.o INIT_OBJS = __libpthreadglue_init.o __libcglue_init.o __libcglue_deinit.o _exit.o abort.o exit.o diff --git a/src/libcglue/glue.c b/src/libcglue/glue.c index 8554ce0f..5e2b9343 100755 --- a/src/libcglue/glue.c +++ b/src/libcglue/glue.c @@ -1179,3 +1179,25 @@ int unlinkat(int dirfd, const char *pathname, int flags) } } #endif /* F_unlinkat */ + +#ifdef F_realpath +char *realpath(const char *path, char *resolved_path) +{ + if (path == NULL) { + errno = EINVAL; + return NULL; + } + + if (resolved_path == NULL) { + resolved_path = malloc(PATH_MAX); + if (resolved_path == NULL) { + errno = ENOMEM; + return NULL; + } + } + + __path_absolute(path, resolved_path, PATH_MAX); + + return resolved_path; +} +#endif /* F_realpath */ \ No newline at end of file From 9f226208084ceecec5f99e5a25d05c782b9a7bf4 Mon Sep 17 00:00:00 2001 From: diamant3 Date: Sat, 15 Jun 2024 13:39:35 +0800 Subject: [PATCH 2/5] handle some errno --- src/libcglue/glue.c | 57 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/src/libcglue/glue.c b/src/libcglue/glue.c index 5e2b9343..d0371656 100755 --- a/src/libcglue/glue.c +++ b/src/libcglue/glue.c @@ -1188,6 +1188,63 @@ char *realpath(const char *path, char *resolved_path) return NULL; } + if (strlen(path) > PATH_MAX) { + errno = ENAMETOOLONG; + return NULL; + } + + /* check the length of every component of path arg */ + char component[NAME_MAX + 1]; + const char *start = path; + const char *end; + while (*start != '\0') { + end = start; + + // find the next '/' + while (*end != '/' && *end != '\0') { end++; } + + // compute path component length + size_t len = end - start; + if (len > NAME_MAX) { + errno = ENAMETOOLONG; + return NULL; + } + + // copy start to component buffer + memcpy(component, start, len); + component[len] = '\0'; + + // move to the next component + if (*end == '/') { start = end + 1; } + else { start = end; } + } + + /* check if file or directory exist */ + struct stat st; + if (stat(path, &st) == 0) { + if (S_ISREG(st.st_mode)) { + SceUID uid = sceIoOpen(path, PSP_O_RDONLY, 0644); + if(uid < 0) { + errno = ENOENT; + return NULL; + } + sceIoClose(uid); + } else if (S_ISDIR(st.st_mode)) { + SceUID uid = sceIoDopen(path); + if(uid < 0) { + errno = ENOTDIR; + return NULL; + } + sceIoDclose(uid); + } else { + errno = ENOENT; + return NULL; + } + } else { + return NULL; + } + + // if resolved_path arg is NULL, use malloc instead if (resolved_path == NULL) { resolved_path = malloc(PATH_MAX); if (resolved_path == NULL) { From 14f61b44d0de6b4b967adfa69a7d78229a95e1c0 Mon Sep 17 00:00:00 2001 From: diamant3 Date: Sat, 15 Jun 2024 15:06:19 +0800 Subject: [PATCH 3/5] unnecessary check --- src/libcglue/glue.c | 26 -------------------------- 1 file changed, 26 deletions(-) diff --git a/src/libcglue/glue.c b/src/libcglue/glue.c index d0371656..38dabe9e 100755 --- a/src/libcglue/glue.c +++ b/src/libcglue/glue.c @@ -1193,32 +1193,6 @@ char *realpath(const char *path, char *resolved_path) return NULL; } - /* check the length of every component of path arg */ - char component[NAME_MAX + 1]; - const char *start = path; - const char *end; - while (*start != '\0') { - end = start; - - // find the next '/' - while (*end != '/' && *end != '\0') { end++; } - - // compute path component length - size_t len = end - start; - if (len > NAME_MAX) { - errno = ENAMETOOLONG; - return NULL; - } - - // copy start to component buffer - memcpy(component, start, len); - component[len] = '\0'; - - // move to the next component - if (*end == '/') { start = end + 1; } - else { start = end; } - } - /* check if file or directory exist */ struct stat st; if (stat(path, &st) == 0) { From 070f1cdab1f724ad9e5d2a246cf404f5a2a27839 Mon Sep 17 00:00:00 2001 From: diamant3 Date: Sat, 15 Jun 2024 17:20:07 +0800 Subject: [PATCH 4/5] use psp specific functions --- src/libcglue/glue.c | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/libcglue/glue.c b/src/libcglue/glue.c index 38dabe9e..576925a7 100755 --- a/src/libcglue/glue.c +++ b/src/libcglue/glue.c @@ -1194,28 +1194,23 @@ char *realpath(const char *path, char *resolved_path) } /* check if file or directory exist */ - struct stat st; - if (stat(path, &st) == 0) { - if (S_ISREG(st.st_mode)) { - SceUID uid = sceIoOpen(path, PSP_O_RDONLY, 0644); + SceIoStat psp_stat; + if (sceIoGetstat(path, &psp_stat) >= 0) { + if (FIO_S_ISREG(psp_stat.st_mode)) { + SceUID uid = sceIoOpen(path, PSP_O_RDONLY, 0777); if(uid < 0) { errno = ENOENT; return NULL; } sceIoClose(uid); - } else if (S_ISDIR(st.st_mode)) { + } else if (FIO_S_ISDIR(psp_stat.st_mode)) { SceUID uid = sceIoDopen(path); if(uid < 0) { errno = ENOTDIR; return NULL; } sceIoDclose(uid); - } else { - errno = ENOENT; - return NULL; } - } else { - return NULL; } // if resolved_path arg is NULL, use malloc instead From 5292e311b68daa578d2f3fc405a688f635292027 Mon Sep 17 00:00:00 2001 From: diamant3 Date: Sun, 16 Jun 2024 13:34:01 +0800 Subject: [PATCH 5/5] simplify file/dir exist check --- src/libcglue/glue.c | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/src/libcglue/glue.c b/src/libcglue/glue.c index 576925a7..a8fe5738 100755 --- a/src/libcglue/glue.c +++ b/src/libcglue/glue.c @@ -1194,28 +1194,15 @@ char *realpath(const char *path, char *resolved_path) } /* check if file or directory exist */ - SceIoStat psp_stat; - if (sceIoGetstat(path, &psp_stat) >= 0) { - if (FIO_S_ISREG(psp_stat.st_mode)) { - SceUID uid = sceIoOpen(path, PSP_O_RDONLY, 0777); - if(uid < 0) { - errno = ENOENT; - return NULL; - } - sceIoClose(uid); - } else if (FIO_S_ISDIR(psp_stat.st_mode)) { - SceUID uid = sceIoDopen(path); - if(uid < 0) { - errno = ENOTDIR; - return NULL; - } - sceIoDclose(uid); - } + struct stat st; + if (stat(path, &st) < 0) { + errno = ENOENT; + return NULL; } // if resolved_path arg is NULL, use malloc instead if (resolved_path == NULL) { - resolved_path = malloc(PATH_MAX); + resolved_path = (char *)malloc(PATH_MAX * sizeof(char)); if (resolved_path == NULL) { errno = ENOMEM; return NULL;