Skip to content

Commit

Permalink
dirent: implemented closedir(), telldir(), and seekdir()
Browse files Browse the repository at this point in the history
  • Loading branch information
jewelcodes committed Sep 26, 2024
1 parent 7c78e67 commit 12703c9
Showing 1 changed file with 55 additions and 1 deletion.
56 changes: 55 additions & 1 deletion src/dirent.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,58 @@ int readdir_r(Thread *t, uint64_t id, DIR *dir, struct dirent *entry, struct dir
int status = requestServer(t, cmd);
free(cmd);
return status;
}
}

void seekdir(Thread *t, DIR *dir, long position) {
Process *p = getProcess(t->pid);
if(!p) return;

int dd = (int) dir & ~(DIRECTORY_DESCRIPTOR_FLAG);
if(dd < 0 || dd >= MAX_IO_DESCRIPTORS) return;
if(!p->io[dd].valid || p->io[dd].type != IO_DIRECTORY) return;

DirectoryDescriptor *descriptor = (DirectoryDescriptor *) p->io[dd].data;
if(!descriptor) return;

descriptor->position = position;
return;
}

long telldir(Thread *t, DIR *dir) {
Process *p = getProcess(t->pid);
if(!p) return -ESRCH;

int dd = (int) dir & ~(DIRECTORY_DESCRIPTOR_FLAG);
if(dd < 0 || dd >= MAX_IO_DESCRIPTORS) return -EBADF;
if(!p->io[dd].valid || p->io[dd].type != IO_DIRECTORY) return -EBADF;

DirectoryDescriptor *descriptor = (DirectoryDescriptor *) p->io[dd].data;
if(!descriptor) return -EBADF;

return descriptor->position;
}

int closedir(Thread *t, DIR *dir) {
Process *p = getProcess(t->pid);
if(!p) return -ESRCH;

int dd = (int) dir & ~(DIRECTORY_DESCRIPTOR_FLAG);
if(dd < 0 || dd >= MAX_IO_DESCRIPTORS) return -EBADF;
if(!p->io[dd].valid || p->io[dd].type != IO_DIRECTORY) return -EBADF;

DirectoryDescriptor *descriptor = (DirectoryDescriptor *) p->io[dd].data;
if(!descriptor) return -EBADF;

// destroy the descriptor
if(!p->io[dd].clone)
free(p->io[dd].data);

p->io[dd].valid = false;
p->io[dd].type = 0;
p->io[dd].flags = 0;
p->io[dd].data = NULL;
p->io[dd].clone = false;
p->iodCount--;

return 0;
}

0 comments on commit 12703c9

Please sign in to comment.