Skip to content

Commit

Permalink
Merge pull request ps2dev#509 from uyjulian/fdmap_improvements_240118
Browse files Browse the repository at this point in the history
Fdmap improvements 240118
  • Loading branch information
fjtrujy authored Jan 18, 2024
2 parents 5408d48 + 7f0a7fe commit d29a4e0
Show file tree
Hide file tree
Showing 6 changed files with 270 additions and 170 deletions.
15 changes: 13 additions & 2 deletions ee/libcglue/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ SLEEP_OBJS = nanosleep.o

SJIS_OBJS = isSpecialSJIS.o isSpecialASCII.o strcpy_ascii.o strcpy_sjis.o

CWD_OBJS = __cwd.o __get_drive.o getcwd.o __path_absolute.o __init_cwd.o
CWD_OBJS = __cwd.o __cwd_len.o __get_drive.o getcwd.o __path_absolute.o __init_cwd.o

PS2SDKAPI_OBJS = \
__fioOpsInitialize.o \
Expand Down Expand Up @@ -96,7 +96,18 @@ GLUE_OBJS = \
geteuid.o \
getpwuid.o \
getpwnam.o \
libcglue_get_fd_info.o
libcglue_get_fd_info.o \
ps2sdk_get_iop_fd.o \
ps2sdk_get_iop_filename.o \
_ps2sdk_close.o \
_ps2sdk_dclose.o \
_ps2sdk_read.o \
_ps2sdk_lseek.o \
_ps2sdk_lseek64.o \
_ps2sdk_write.o \
_ps2sdk_ioctl.o \
_ps2sdk_ioctl2.o \
_ps2sdk_dread.o

LOCK_OBJS = __retarget_lock_init.o __retarget_lock_acquire.o __retarget_lock_release.o __retarget_lock_try_acquire.o __retarget_lock_close.o \
__retarget_lock_init_recursive.o __retarget_lock_acquire_recursive.o __retarget_lock_release_recursive.o __retarget_lock_try_acquire_recursive.o __retarget_lock_close_recursive.o
Expand Down
152 changes: 13 additions & 139 deletions ee/libcglue/include/ps2sdkapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ typedef int (*_libcglue_fdman_lseek_cb_t)(void *userdata, int offset, int whence
typedef int64_t (*_libcglue_fdman_lseek64_cb_t)(void *userdata, int64_t offset, int whence);
typedef int (*_libcglue_fdman_write_cb_t)(void *userdata, const void *buf, int nbytes);
typedef int (*_libcglue_fdman_ioctl_cb_t)(void *userdata, int request, void *data);
typedef int (*_libcglue_fdman_ioctl2_cb_t)(void *userdata, int request, void *arg, unsigned int arglen, void *buf, unsigned int buflen);
typedef int (*_libcglue_fdman_dread_cb_t)(void *userdata, struct dirent *dir);
typedef int (*_libcglue_fdman_fcntl_f_setfl_cb_t)(void *userdata, int newfl);
typedef int (*_libcglue_fdman_accept_cb_t)(void *userdata, struct _libcglue_fdman_fd_info_ *info, struct sockaddr *addr, socklen_t *addrlen);
Expand All @@ -56,6 +57,7 @@ typedef struct _libcglue_fdman_fd_ops_
_libcglue_fdman_lseek64_cb_t lseek64;
_libcglue_fdman_write_cb_t write;
_libcglue_fdman_ioctl_cb_t ioctl;
_libcglue_fdman_ioctl2_cb_t ioctl2;
_libcglue_fdman_dread_cb_t dread;
_libcglue_fdman_fcntl_f_setfl_cb_t fcntl_f_setfl;
_libcglue_fdman_accept_cb_t accept;
Expand Down Expand Up @@ -182,144 +184,16 @@ int __libcglue_init_stdio(_libcglue_fdman_fd_info_t *info, int fd);
/* The fd we provide to final user aren't actually the same than IOP's fd
* so this function allow you to get actual IOP's fd from public fd
*/
static inline int ps2sdk_get_iop_fd(int fd)
{
_libcglue_fdman_fd_info_t *fdinfo;
fdinfo = libcglue_get_fd_info(fd);
if (fdinfo == NULL)
{
return -EBADF;
}
if (fdinfo->ops == NULL || fdinfo->ops->getfd == NULL)
{
return -ENOSYS;
}
return fdinfo->ops->getfd(fdinfo->userdata);
}

static inline char *ps2sdk_get_iop_filename(int fd)
{
_libcglue_fdman_fd_info_t *fdinfo;
fdinfo = libcglue_get_fd_info(fd);
if (fdinfo == NULL)
{
return NULL;
}
if (fdinfo->ops == NULL || fdinfo->ops->getfilename == NULL)
{
return NULL;
}
return fdinfo->ops->getfilename(fdinfo->userdata);
}

static inline int _ps2sdk_close(int fd)
{
_libcglue_fdman_fd_info_t *fdinfo;
fdinfo = libcglue_get_fd_info(fd);
if (fdinfo == NULL)
{
return -EBADF;
}
if (fdinfo->ops == NULL || fdinfo->ops->close == NULL)
{
return -ENOSYS;
}
return fdinfo->ops->close(fdinfo->userdata);
}

static inline int _ps2sdk_dclose(int fd)
{
return _ps2sdk_close(fd);
}

static inline int _ps2sdk_read(int fd, void *buf, int nbytes)
{
_libcglue_fdman_fd_info_t *fdinfo;
fdinfo = libcglue_get_fd_info(fd);
if (fdinfo == NULL)
{
return -EBADF;
}
if (fdinfo->ops == NULL || fdinfo->ops->read == NULL)
{
return -ENOSYS;
}
return fdinfo->ops->read(fdinfo->userdata, buf, nbytes);
}

static inline int _ps2sdk_lseek(int fd, int offset, int whence)
{
_libcglue_fdman_fd_info_t *fdinfo;
fdinfo = libcglue_get_fd_info(fd);
if (fdinfo == NULL)
{
return -EBADF;
}
if (fdinfo->ops == NULL || fdinfo->ops->lseek == NULL)
{
return -ENOSYS;
}
return fdinfo->ops->lseek(fdinfo->userdata, offset, whence);
}

static inline int64_t _ps2sdk_lseek64(int fd, int64_t offset, int whence)
{
_libcglue_fdman_fd_info_t *fdinfo;
fdinfo = libcglue_get_fd_info(fd);
if (fdinfo == NULL)
{
return -EBADF;
}
if (fdinfo->ops == NULL || fdinfo->ops->lseek64 == NULL)
{
return -ENOSYS;
}
return fdinfo->ops->lseek64(fdinfo->userdata, offset, whence);
}

static inline int _ps2sdk_write(int fd, const void *buf, int nbytes)
{
_libcglue_fdman_fd_info_t *fdinfo;
fdinfo = libcglue_get_fd_info(fd);
if (fdinfo == NULL)
{
return -EBADF;
}
if (fdinfo->ops == NULL || fdinfo->ops->write == NULL)
{
return -ENOSYS;
}
return fdinfo->ops->write(fdinfo->userdata, buf, nbytes);
}

static inline int _ps2sdk_ioctl(int fd, int request, void *data)
{
_libcglue_fdman_fd_info_t *fdinfo;
fdinfo = libcglue_get_fd_info(fd);
if (fdinfo == NULL)
{
return -EBADF;
}
if (fdinfo->ops == NULL || fdinfo->ops->ioctl == NULL)
{
return -ENOSYS;
}
return fdinfo->ops->ioctl(fdinfo->userdata, request, data);
}

static inline int _ps2sdk_dread(int fd, struct dirent *dir)
{
_libcglue_fdman_fd_info_t *fdinfo;
fdinfo = libcglue_get_fd_info(fd);
if (fdinfo == NULL)
{
return -EBADF;
}
if (fdinfo->ops == NULL || fdinfo->ops->dread == NULL)
{
return -ENOSYS;
}
return fdinfo->ops->dread(fdinfo->userdata, dir);
}
extern int ps2sdk_get_iop_fd(int fd);
extern char *ps2sdk_get_iop_filename(int fd);
extern int _ps2sdk_close(int fd);
extern int _ps2sdk_dclose(int fd);
extern int _ps2sdk_read(int fd, void *buf, int nbytes);
extern int _ps2sdk_lseek(int fd, int offset, int whence);
extern int64_t _ps2sdk_lseek64(int fd, int64_t offset, int whence);
extern int _ps2sdk_write(int fd, const void *buf, int nbytes);
extern int _ps2sdk_ioctl(int fd, int request, void *data);
extern int _ps2sdk_ioctl2(int fd, int request, void *arg, unsigned int arglen, void *buf, unsigned int buflen);
extern int _ps2sdk_dread(int fd, struct dirent *dir);

#endif /* __PS2SDKAPI_H__ */
55 changes: 35 additions & 20 deletions ee/libcglue/src/cwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ char __cwd[MAXNAMLEN + 1] = { 0 };
extern char __cwd[MAXNAMLEN + 1];
#endif

#ifdef F___cwd_len
/* the length of the present working directory variable. */
size_t __cwd_len = 0;
#else
extern size_t __cwd_len;
#endif

#define defaultCWD "host:"

enum SeparatorType {
Expand Down Expand Up @@ -71,12 +78,13 @@ char *getcwd(char *buf, size_t size)
return NULL;
}

if(strlen(__cwd) >= size) {
if(__cwd_len >= size) {
errno = ERANGE;
return NULL;
}

strncpy(buf, __cwd, size);
memcpy(buf, __cwd, __cwd_len);
buf[__cwd_len] = '\x00';
return buf;
}
#endif
Expand All @@ -89,22 +97,24 @@ static int __path_normalize(char *out, int len, int posixSeparator)
int i, j;
int first, next;
char separator = posixSeparator ? '/' : '\\';
size_t out_len = strnlen(out, len);

out_len += 2;
if (out_len >= len) return -10;

/* First append "/" to make the rest easier */
out[out_len - 1] = separator;
out[out_len] = 0;

// Convert separators to the specified one
for (size_t i = 0; i < len; i++) {
for (size_t i = 0; i < out_len; i++) {
if (out[i] == '/' || out[i] == '\\') {
out[i] = separator;
}
}

/* First append "/" to make the rest easier */
if (strlen(out) + 1 >= len) return -10;
out[strlen(out)] = separator;
out[strlen(out)] = 0;


/* Convert "//" to "/" */
for(i = 0; out[i+1]; i++) {
for(i = 0; (i < out_len) && out[i+1]; i++) {
if(out[i] == separator && out[i+1] == separator) {
for(j=i+1; out[j]; j++)
out[j] = out[j+1];
Expand All @@ -113,7 +123,7 @@ static int __path_normalize(char *out, int len, int posixSeparator)
}

/* Convert "/./" to "/" */
for(i = 0; out[i] && out[i+1] && out[i+2]; i++) {
for(i = 0; (i < out_len) && out[i] && out[i+1] && out[i+2]; i++) {
if(out[i] == separator && out[i+1] == '.' && out[i+2] == separator) {
for(j = i+1; out[j]; j++)
out[j] = out[j+2];
Expand All @@ -124,7 +134,7 @@ static int __path_normalize(char *out, int len, int posixSeparator)
/* Convert "/asdf/../" to "/" until we can't anymore. Also
* convert leading "/../" to "/" */
first = next = 0;
while(1) {
while((first < out_len) && (next < out_len)) {
/* If a "../" follows, remove it and the parent */
if(out[next+1] && out[next+1] == '.' &&
out[next+2] && out[next+2] == '.' &&
Expand All @@ -143,7 +153,7 @@ static int __path_normalize(char *out, int len, int posixSeparator)
}

/* Remove trailing "/" just if it's not the root */
for(i = 1; out[i]; i++)
for(i = 1; (i < out_len) && out[i]; i++)
continue;
if(i > 1 && out[i-1] == separator)
out[i-1] = 0;
Expand All @@ -156,33 +166,38 @@ int __path_absolute(const char *in, char *out, int len)
{
int dr;
enum SeparatorType separatorType;
size_t in_len;

in_len = strlen(in);
/* See what the relative URL starts with */
dr = __get_drive(in, &separatorType);
char separator = separatorType == SeparatorTypePOSIX ? '/' : '\\';

if(dr > 0 && (separatorType == SeparatorTypeNone || in[dr] == separator)) {
/* It starts with "drive:" and has no separator or "drive:/", so it's already absolute */
if (strlen(in) >= len) return -1;
if (in_len >= len) return -1;
strncpy(out, in, len);
} else if(dr > 0 && in[dr - 1] == ':') {
/* It starts with "drive:", so it's already absoulte, however it misses the "/" after unit */
if (strlen(in) + 1 >= len) return -2;
if (in_len + 1 >= len) return -2;
strncpy(out, in, dr);
out[dr] = separator;
strncpy(out + dr + 1, in + dr, len - dr - 1);
} else if(in[0] == '\\' || in[0] == '/') {
/* It's absolute, but missing the drive, so use cwd's drive */
if(strlen(__cwd) + strlen(in) >= len) return -3;
strncpy(out, __cwd, len);
if(__cwd_len + in_len >= len) return -3;
memcpy(out, __cwd, __cwd_len);
out[__cwd_len] = '\x00';
dr = __get_drive(out, &separatorType);
if(dr < 0) dr = 0;
out[dr] = 0;
strncat(out, in, len);
} else {
/* It's not absolute, so append it to the current cwd */
if(strlen(__cwd) + 1 + strlen(in) >= len) return -5;
strncpy(out, __cwd, len);
strncat(out, &separator, 1);
if(__cwd_len + 1 + in_len >= len) return -5;
memcpy(out, __cwd, __cwd_len);
out[__cwd_len] = separator;
out[__cwd_len + 1] = '\x00';
strncat(out, in, len);
}

Expand Down
Loading

0 comments on commit d29a4e0

Please sign in to comment.