Skip to content

Commit

Permalink
Merge pull request #464 from fjtrujy/fix_removeFile
Browse files Browse the repository at this point in the history
Apply normalized to all functions
  • Loading branch information
uyjulian authored Dec 1, 2023
2 parents 7efb3b4 + 32ae6b5 commit c278d6b
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 68 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
25 changes: 2 additions & 23 deletions ee/libcglue/samples/regress/runner.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,18 @@

extern int libc_add_tests(test_suite *p);

#ifdef _EE
int iop_ret = 0;

void iop_start(void)
static void iop_start(void)
{
SifInitRpc(0);

sbv_patch_fileio();
/*
SifLoadStartModule("rom0:FILEIO", 0, NULL, &iop_ret);
if (iop_ret < 0)
return;
SifLoadStartModule("host:iomanX.irx", 0, NULL, &iop_ret);
if (iop_ret < 0)
return;
SifLoadStartModule("host:fileXio.irx", 0, NULL, &iop_ret);
if (iop_ret < 0)
return;
fileXioInit();
*/
}
#endif

int main(int argc, char *argv[])
{
test_suite suite;

#ifdef _EE
/* Check the IOP setup status. */
if (iop_ret < 0)
return 1;
#endif
iop_start();

/* initialize test suite */
init_testsuite(&suite);
Expand Down
28 changes: 20 additions & 8 deletions ee/libcglue/samples/regress/stdio_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@ static const char *test_fopen_fclose(void *arg)
return NULL;
}

static const char *test_create_remove(void *arg)
{
FILE *fp = fopen((char *)arg, "wt");
if (fp == NULL)
{
return "failed to create test file";
}

fclose(fp);

if (remove((char *)arg) != 0)
{
return "failed to remove test file";
}

printf("\nSUCCESS: all checks passed\n");
return NULL;
}

static const char *test_fgets(void *arg)
{
char buf[64], *ret;
Expand Down Expand Up @@ -428,25 +447,18 @@ int libc_add_tests(test_suite *p)
const char *textfile, *textfile2, *invalidtextfile;
const char *dir, *dir2;

#ifdef _EE
textfile = "host:testfiles/dummy";
textfile2 = "host:testfiles/dummy2";
invalidtextfile = "host:testfiles/invalidtextfile"; // This file shouldn't exist
dir = "host:testfiles/";
dir2 = "host:dummydir/";
#else
textfile = "testfiles/dummy";
textfile2 = "testfiles/dummy2";
invalidtextfile = "testfiles/invalidtextfile";
dir = "testfiles/";
dir2 = "dummydir/";
#endif

/* If testing using usbd/usbhdfsd or ps2hdd/ps2fs, this adds a 10
second delay so that the devices can initialize fully.
sleep(10); */

add_test(p, "fopen, fclose\n", test_fopen_fclose, (void *)textfile);
add_test(p, "create, remove\n", test_create_remove, (void *)textfile2);
add_test(p, "fgets\n", test_fgets, (void *)textfile);
add_test(p, "fread\n", test_fread, (void *)textfile);
add_test(p, "fwrite\n", test_fwrite, (void *)textfile2);
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 c278d6b

Please sign in to comment.