Skip to content

Commit

Permalink
Apply normalized to all functions
Browse files Browse the repository at this point in the history
  • Loading branch information
fjtrujy committed Nov 30, 2023
1 parent e98959a commit 32ae6b5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 37 deletions.
2 changes: 1 addition & 1 deletion ee/libcglue/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ PS2SDKAPI_OBJS = _ps2sdk_close.o _ps2sdk_open.o _ps2sdk_read.o _ps2sdk_lseek.o _
_ps2sdk_remove.o _ps2sdk_rename.o _ps2sdk_mkdir.o _ps2sdk_rmdir.o _ps2sdk_stat.o _ps2sdk_readlink.o _ps2sdk_symlink.o \
_ps2sdk_dopen.o _ps2sdk_dread.o _ps2sdk_dclose.o

GLUE_OBJS = __direct_pwd.o __dummy_passwd.o __transform_errno.o __transform64_errno.o compile_time_check.o _open.o _close.o _read.o _write.o \
GLUE_OBJS = __direct_pwd.o __dummy_passwd.o __transform_errno.o __transform64_errno.o compile_time_check.o __normalized_path.o _open.o _close.o _read.o _write.o \
_stat.o lstat.o _fstat.o access.o _fcntl.o getdents.o _lseek.o lseek64.o chdir.o mkdir.o \
rmdir.o _link.o _unlink.o _rename.o getcwd.o _getpid.o _kill.o _fork.o _wait.o _sbrk.o _gettimeofday.o _times.o ftime.o clock_getres.o clock_gettime.o clock_settime.o \
truncate.o symlink.o readlink.o utime.o fchown.o getrandom.o getentropy.o _isatty.o chmod.o fchmod.o fchmodat.o pathconf.o \
Expand Down
91 changes: 55 additions & 36 deletions ee/libcglue/src/glue.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ void compile_time_check() {
}
#endif

#ifdef F__open
/* Normalize a pathname by removing . and .. components, duplicated /, etc. */
static char* normalize_path(const char *path_name)
#ifdef F___normalized_path
/* Sanitize a pathname by removing . and .. components, duplicated /, etc. */
static char* sanitize_path(const char *path_name)
{
int i, j;
int first, next;
Expand Down Expand Up @@ -200,31 +200,12 @@ static int isCdromPath(const char *path)
return !strncmp(path, "cdrom0:", 7) || !strncmp(path, "cdrom:", 6);
}

int _open(const char *buf, int flags, ...) {
int iop_flags = 0;
int is_dir = 0;
int iop_fd, fd;

// newlib frags differ from iop flags
if ((flags & 3) == O_RDONLY) iop_flags |= IOP_O_RDONLY;
if ((flags & 3) == O_WRONLY) iop_flags |= IOP_O_WRONLY;
if ((flags & 3) == O_RDWR ) iop_flags |= IOP_O_RDWR;
if (flags & O_NONBLOCK) iop_flags |= IOP_O_NBLOCK;
if (flags & O_APPEND) iop_flags |= IOP_O_APPEND;
if (flags & O_CREAT) iop_flags |= IOP_O_CREAT;
if (flags & O_TRUNC) iop_flags |= IOP_O_TRUNC;
if (flags & O_EXCL) iop_flags |= IOP_O_EXCL;
//if (flags & O_???) iop_flags |= IOP_O_NOWAIT;
if (flags & O_DIRECTORY) {
iop_flags |= IOP_O_DIROPEN;
is_dir = 1;
}

char *t_fname = normalize_path(buf);
char b_fname[FILENAME_MAX];
char *__normalized_path(const char *originalPath)
{
const char *buf = sanitize_path(originalPath);
static char b_fname[FILENAME_MAX];

if (!strchr(buf, ':')) { // filename doesn't contain device
t_fname = b_fname;
if (buf[0] == '/' || buf[0] == '\\') { // does it contain root ?
char *device_end = strchr(__direct_pwd, ':');
if (device_end) { // yes, let's strip pwd a bit to keep device only
Expand Down Expand Up @@ -268,6 +249,34 @@ int _open(const char *buf, int flags, ...) {
}
}

return (char *)b_fname;
}
#else
extern char *__normalized_path(const char *path_name);
#endif

#ifdef F__open
int _open(const char *buf, int flags, ...) {
int iop_flags = 0;
int is_dir = 0;
int iop_fd, fd;

// newlib frags differ from iop flags
if ((flags & 3) == O_RDONLY) iop_flags |= IOP_O_RDONLY;
if ((flags & 3) == O_WRONLY) iop_flags |= IOP_O_WRONLY;
if ((flags & 3) == O_RDWR ) iop_flags |= IOP_O_RDWR;
if (flags & O_NONBLOCK) iop_flags |= IOP_O_NBLOCK;
if (flags & O_APPEND) iop_flags |= IOP_O_APPEND;
if (flags & O_CREAT) iop_flags |= IOP_O_CREAT;
if (flags & O_TRUNC) iop_flags |= IOP_O_TRUNC;
if (flags & O_EXCL) iop_flags |= IOP_O_EXCL;
//if (flags & O_???) iop_flags |= IOP_O_NOWAIT;
if (flags & O_DIRECTORY) {
iop_flags |= IOP_O_DIROPEN;
is_dir = 1;
}

char *t_fname = __normalized_path(buf);
iop_fd = is_dir ? _ps2sdk_dopen(t_fname) : _ps2sdk_open(t_fname, iop_flags);
if (iop_fd >= 0) {
fd = __fdman_get_new_descriptor();
Expand Down Expand Up @@ -385,13 +394,15 @@ int _write(int fd, const void *buf, size_t nbytes) {

#ifdef F__stat
int _stat(const char *path, struct stat *buf) {
return __transform_errno(_ps2sdk_stat(path, buf));
const char *normalized = __normalized_path(path);
return __transform_errno(_ps2sdk_stat(normalized, buf));
}
#endif

#ifdef F_lstat
int lstat(const char *path, struct stat *buf) {
return __transform_errno(stat(path, buf));
const char *normalized = __normalized_path(path);
return __transform_errno(stat(normalized, buf));
}
#endif

Expand Down Expand Up @@ -580,20 +591,23 @@ off64_t lseek64(int fd, off64_t offset, int whence)

#ifdef F_chdir
int chdir(const char *path) {
strcpy(__direct_pwd, path);
const char *normalized = __normalized_path(path);
strcpy(__direct_pwd, normalized);
return 0;
}
#endif

#ifdef F_mkdir
int mkdir(const char *path, mode_t mode) {
return __transform_errno(_ps2sdk_mkdir(path, mode));
const char *normalized = __normalized_path(path);
return __transform_errno(_ps2sdk_mkdir(normalized, mode));
}
#endif

#ifdef F_rmdir
int rmdir(const char *path) {
return __transform_errno(_ps2sdk_rmdir(path));
const char *normalized = __normalized_path(path);
return __transform_errno(_ps2sdk_rmdir(normalized));
}
#endif

Expand All @@ -606,14 +620,16 @@ int _link(const char *old, const char *new) {

#ifdef F__unlink
int _unlink(const char *path) {
errno = ENOSYS;
return -1; /* not supported */
const char *normalized = __normalized_path(path);
return __transform_errno(_ps2sdk_remove(normalized));
}
#endif

#ifdef F__rename
int _rename(const char *old, const char *new) {
return __transform_errno(_ps2sdk_rename(old, new));
const char *normalized_old = __normalized_path(old);
const char *normalized_new = __normalized_path(new);
return __transform_errno(_ps2sdk_rename(normalized_old, normalized_new));
}
#endif

Expand Down Expand Up @@ -815,14 +831,17 @@ int truncate(const char *path, off_t length)
#ifdef F_symlink
int symlink(const char *target, const char *linkpath)
{
return __transform_errno(_ps2sdk_symlink(target, linkpath));
const char *normalized_target = __normalized_path(target);
const char *normalized_linkpath = __normalized_path(linkpath);
return __transform_errno(_ps2sdk_symlink(normalized_target, normalized_linkpath));
}
#endif

#ifdef F_readlink
ssize_t readlink(const char *path, char *buf, size_t bufsiz)
{
return _ps2sdk_readlink(path, buf, bufsiz);
const char *normalized = __normalized_path(path);
return __transform_errno(_ps2sdk_readlink(normalized, buf, bufsiz));
}
#endif

Expand Down

0 comments on commit 32ae6b5

Please sign in to comment.