Skip to content

Commit

Permalink
u
Browse files Browse the repository at this point in the history
  • Loading branch information
v-atamanenko committed Oct 26, 2021
1 parent ba7a92d commit 84ac99a
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 14 deletions.
33 changes: 32 additions & 1 deletion newlib/libc/sys/vita/dirent.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ DEALINGS IN THE SOFTWARE.

#include <psp2/types.h>
#include <psp2/io/dirent.h>
#include <psp2/io/devctl.h>
#include <psp2/kernel/threadmgr.h>

#include "vitadescriptor.h"

#define SCE_ERRNO_MASK 0xFF
#define MAXNAMLEN 256

Expand Down Expand Up @@ -92,6 +95,34 @@ DIR *opendir(const char *dirname)
return dirp;
}

DIR *fdopendir(int fd)
{
int ret;
DescriptorTranslation *fdmap = __vita_fd_grab(fd);

if (!fdmap) {
errno = EBADF;
return NULL;
}

DIR *dirp;

if ((dirp = (DIR *)malloc(sizeof(DIR))) == NULL) {
errno = ENOMEM;

__vita_fd_drop(fdmap);
__vita_release_descriptor(fd);
return NULL;
}

dirp->uid = fdmap->sce_uid;
dirp->index = 0;

__vita_fd_drop(fdmap);
errno = 0;
return dirp;
}

struct dirent *readdir(DIR *dirp)
{
if (!dirp)
Expand Down Expand Up @@ -178,4 +209,4 @@ long int telldir(DIR *dirp)
}

return dirp->index;
}
}
34 changes: 34 additions & 0 deletions newlib/libc/sys/vita/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,17 @@ DEALINGS IN THE SOFTWARE.
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/dirent.h>
#include <sys/stat.h>
#include <sys/unistd.h>

#include <psp2/types.h>
#include <psp2/io/stat.h>
#include <psp2/io/fcntl.h>

#include "vitadescriptor.h"

#define SCE_ERRNO_MASK 0xFF

Expand All @@ -50,3 +56,31 @@ int rmdir(const char *pathname)
errno = 0;
return 0;
}

int fsync (int __fd)
{
int ret;

DescriptorTranslation *fdmap = __vita_fd_grab(__fd);

if (!fdmap) {
errno = EBADF;
return -1;
}

ret = sceIoSyncByFd(fdmap->sce_uid, 0);
__vita_fd_drop(fdmap);

if (ret < 0) {
errno = ret & SCE_ERRNO_MASK;
return -1;
}

errno = 0;
return ret;
}

char * realpath (const char *__restrict path, char *__restrict resolved_path) {
strcpy(resolved_path, path);
return resolved_path;
}
6 changes: 4 additions & 2 deletions newlib/libc/sys/vita/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <string.h>

#include <psp2/io/fcntl.h>
#include <psp2/io/dirent.h>
#include <psp2/kernel/threadmgr.h>
#include <psp2/net/net.h>

Expand Down Expand Up @@ -192,10 +193,11 @@ int __vita_fd_drop(DescriptorTranslation *map)
{
case VITA_DESCRIPTOR_FILE:
case VITA_DESCRIPTOR_TTY:
{
ret = sceIoClose(map->sce_uid);
break;
}
case VITA_DESCRIPTOR_DIR:
ret = sceIoDclose(map->sce_uid);
break;
case VITA_DESCRIPTOR_SOCKET:
ret = sceNetSocketClose(map->sce_uid);
if (ret < 0) {
Expand Down
43 changes: 36 additions & 7 deletions newlib/libc/sys/vita/syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ _write_r(struct _reent * reent, int fd, const void *buf, size_t nbytes)
case VITA_DESCRIPTOR_TTY:
ret = sceIoWrite(fdmap->sce_uid, buf, nbytes);
break;
case VITA_DESCRIPTOR_DIR:
__vita_fd_drop(fdmap);
reent->_errno = EBADF;
return -1;
case VITA_DESCRIPTOR_SOCKET:
ret = sceNetSend(fdmap->sce_uid, buf, nbytes, 0);
if (ret < 0) {
Expand Down Expand Up @@ -181,6 +185,7 @@ _lseek_r(struct _reent *reent, int fd, _off_t ptr, int dir)
break;
case VITA_DESCRIPTOR_TTY:
case VITA_DESCRIPTOR_SOCKET:
case VITA_DESCRIPTOR_DIR:
ret = EBADF;
break;
}
Expand Down Expand Up @@ -233,13 +238,32 @@ _open_r(struct _reent *reent, const char *file, int flags, int mode)
{
int ret;
int isdir = 0;
flags = _fcntl2sony(flags);
int sce_flags;
fprintf(stderr, "flagsoriginal %x\n",flags);
sce_flags = _fcntl2sony(flags);
fprintf(stderr, "sceflags %x\n", sce_flags);

ret = sceIoOpen(file, sce_flags, 0666);

fprintf(stderr, "sceIoOpen %x\n", ret);

ret = sceIoOpen(file, flags, 0666);
if ((ret < 0) && ((ret & SCE_ERRNO_MASK) == EISDIR)) {
if ((flags & O_RDWR) || (flags & O_WRONLY)) {
fprintf(stderr, "flags %x\n", flags);
reent->_errno = EINVAL;
return -1;
}

if (ret & SCE_ERRNO_MASK == EISDIR) {
isdir = 1;
ret = sceIoDopen(file);

if (((flags & O_CREAT) || (flags & O_EXCL)) && ret >= 0) {
fprintf(stderr, "flagscrexl %x\n", flags);
reent->_errno = EEXIST;
return -1;
}

fprintf(stderr, "sceIoDopen %x\n", ret);
}

if (ret < 0) {
Expand All @@ -250,18 +274,18 @@ _open_r(struct _reent *reent, const char *file, int flags, int mode)
int fd = __vita_acquire_descriptor();

if (fd < 0) {
if (!isdir) {
sceIoClose(ret);
} else {
if (isdir) {
sceIoDclose(ret);
} else {
sceIoClose(ret);
}

reent->_errno = EMFILE;
return -1;
}

__vita_fdmap[fd]->sce_uid = ret;
__vita_fdmap[fd]->type = VITA_DESCRIPTOR_FILE;
__vita_fdmap[fd]->type = isdir ? VITA_DESCRIPTOR_DIR : VITA_DESCRIPTOR_FILE;

reent->_errno = 0;
return fd;
Expand All @@ -284,6 +308,10 @@ _read_r(struct _reent *reent, int fd, void *ptr, size_t len)
case VITA_DESCRIPTOR_FILE:
ret = sceIoRead(fdmap->sce_uid, ptr, len);
break;
case VITA_DESCRIPTOR_DIR:
__vita_fd_drop(fdmap);
reent->_errno = EBADF;
return -1;
case VITA_DESCRIPTOR_SOCKET:
ret = sceNetRecv(fdmap->sce_uid, ptr, len, 0);

Expand Down Expand Up @@ -381,6 +409,7 @@ _fstat_r(struct _reent *reent, int fd, struct stat *st)
{
case VITA_DESCRIPTOR_TTY:
case VITA_DESCRIPTOR_FILE:
case VITA_DESCRIPTOR_DIR:
ret = sceIoGetstatByFd(fdmap->sce_uid, &stat);
break;
case VITA_DESCRIPTOR_SOCKET:
Expand Down
7 changes: 4 additions & 3 deletions newlib/libc/sys/vita/truncate.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ int truncate(const char *path, off_t length)

int ftruncate(int fd, off_t length)
{
struct _reent *reent = _REENT;
struct _reent *reent = _REENT;
struct SceIoStat stat = {0};
stat.st_size = length;
int ret;

DescriptorTranslation *fdmap = __vita_fd_grab(fd);

if (!fdmap) {
Expand All @@ -71,6 +71,7 @@ int ftruncate(int fd, off_t length)
break;
case VITA_DESCRIPTOR_TTY:
case VITA_DESCRIPTOR_SOCKET:
case VITA_DESCRIPTOR_DIR:
ret = EBADF;
break;
}
Expand All @@ -84,4 +85,4 @@ int ftruncate(int fd, off_t length)

reent->_errno = 0;
return 0;
}
}
3 changes: 2 additions & 1 deletion newlib/libc/sys/vita/vitadescriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ typedef enum
{
VITA_DESCRIPTOR_FILE,
VITA_DESCRIPTOR_SOCKET,
VITA_DESCRIPTOR_TTY
VITA_DESCRIPTOR_TTY,
VITA_DESCRIPTOR_DIR
} DescriptorTypes;

typedef struct
Expand Down

0 comments on commit 84ac99a

Please sign in to comment.