Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion include/dirent.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,21 @@ struct dirent {
};


extern struct dirent *readdir(DIR *s);
extern struct dirent *readdir(DIR *dirp);


extern DIR *opendir(const char *dirname);


extern DIR *fdopendir(int fd);


extern void seekdir(DIR *dirp, long loc);


extern long telldir(DIR *dirp);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep two empty lines between function prototypes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed.



extern void rewinddir(DIR *dirp);


Expand Down
4 changes: 2 additions & 2 deletions stdio/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ FILE *fdopen(int fd, const char *mode)
}

/* POSIX: check if mode argument is allowed by the file access mode of the FD (not necessarily exactly the same) */
fdm &= 0x7;
if ((fdm != O_RDWR) && (fdm != (m & 0x7))) {
fdm &= O_ACCMODE;
if ((fdm != O_RDWR) && (fdm != (m & O_ACCMODE))) {
errno = EINVAL;
return NULL;
}
Expand Down
109 changes: 96 additions & 13 deletions unistd/dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <sys/stat.h>
#include <sys/file.h>
#include <posix/utils.h>
#include <fcntl.h>


static struct {
Expand Down Expand Up @@ -347,36 +348,47 @@ char *realpath(const char *path, char *resolved_path)
}


struct dirent *readdir(DIR *s)
struct dirent *readdir(DIR *dirp)
{
if (s->dirent == NULL) {
if ((s->dirent = calloc(1, sizeof(struct dirent) + NAME_MAX + 1)) == NULL)
if (dirp == NULL) {
errno = EBADF;
return NULL;
}

if (dirp->dirent == NULL) {
if ((dirp->dirent = calloc(1, sizeof(struct dirent) + NAME_MAX + 1)) == NULL) {
errno = -ENOMEM;
return NULL;
}
}

msg_t msg = {
.type = mtReaddir,
.oid = s->oid,
.i.readdir.offs = s->pos,
.o.data = s->dirent,
.oid = dirp->oid,
.i.readdir.offs = dirp->pos,
.o.data = dirp->dirent,
.o.size = sizeof(struct dirent) + NAME_MAX + 1
};

if (msgSend(s->oid.port, &msg) < 0) {
free(s->dirent);
s->dirent = NULL;
if (msgSend(dirp->oid.port, &msg) < 0) {
free(dirp->dirent);
dirp->dirent = NULL;
errno = EIO;
return NULL; /* EIO */
}

if (msg.o.err < 0) {
free(s->dirent);
s->dirent = NULL;
free(dirp->dirent);
dirp->dirent = NULL;
if (msg.o.err != -ENOENT) {
errno = -msg.o.err;
}
return NULL;
}

s->pos += s->dirent->d_reclen;
dirp->pos += dirp->dirent->d_reclen;

return s->dirent;
return dirp->dirent;
}


Expand Down Expand Up @@ -440,8 +452,75 @@ DIR *opendir(const char *dirname)
}


DIR *fdopendir(int fd)
{
DIR *s;
struct stat statbuf;
int fd_flags;

fd_flags = fcntl(fd, F_GETFL);
if (fd_flags < 0) {
return NULL;
}

if (fstat(fd, &statbuf) < 0) {
return NULL;
}

msg_t msg = {
.type = mtGetAttr,
.oid = { .port = statbuf.st_dev, .id = statbuf.st_ino },
.i.attr.type = atType,
};

if ((msgSend(statbuf.st_dev, &msg) < 0) || (msg.o.err < 0)) {
errno = EIO;
return NULL; /* EIO */
}

if (msg.o.attr.val != otDir) {
errno = ENOTDIR;
return NULL; /* ENOTDIR */
}

if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0) {
return NULL;
}

s = calloc(1, sizeof(DIR));
if (s == NULL) {
errno = ENOMEM;
return NULL;
}
s->oid = msg.oid;
s->dirent = NULL;

return s;
}


void seekdir(DIR *dirp, long loc)
{
assert(dirp != NULL);

dirp->pos = loc;
}


long telldir(DIR *dirp)
{
if (dirp == NULL) {
return SET_ERRNO(-EBADF);
}

return dirp->pos;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The function should handle the case where dirp is NULL. According to the POSIX standard, if dirp does not refer to a valid directory stream, telldir() should return -1 and set errno to EBADF.

	if (dirp == NULL) {
		errno = EBADF;
		return -1;
	}
	return dirp->pos;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this behavior exists in Linux, POSIX does not specify it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please look carefully at the condition.

Also, I agree that POSIX does not specify how to report the error, but: should we deviate from the "common" errno setting, as is the case with other directory functions in this file? We don't even have this function documented in p-r-doc, so using an approach other than errno seems counter-intuitive. Please either use errno or make a PR to p-r-doc (which will eventually be needed, though).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed

}


void rewinddir(DIR *dirp)
{
assert(dirp != NULL);

dirp->pos = 0;
}

Expand All @@ -450,6 +529,10 @@ int closedir(DIR *dirp)
{
int ret = 0;

if (dirp == NULL) {
return SET_ERRNO(-EBADF);
}

msg_t msg = {
.type = mtClose,
.oid = dirp->oid
Expand Down
Loading