Skip to content

Commit

Permalink
Lecture 60 - Implementing the VFS fclose functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
nibblebits committed Oct 27, 2020
1 parent 80ac748 commit 75f7e7f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/fs/file.c
Expand Up @@ -198,6 +198,21 @@ int fstat(int fd, struct file_stat* stat)
return res;
}

int fclose(int fd)
{
int res = 0;
struct file_descriptor* desc = file_get_descriptor(fd);
if (!desc)
{
res = -EIO;
goto out;
}

res = desc->filesystem->close(desc->private);
out:
return res;
}

int fseek(int fd, int offset, FILE_SEEK_MODE whence)
{
int res = 0;
Expand Down
4 changes: 4 additions & 0 deletions src/fs/file.h
Expand Up @@ -34,6 +34,8 @@ typedef void*(*FS_OPEN_FUNCTION)(struct disk* disk, struct path_part* path, FILE
typedef int (*FS_READ_FUNCTION)(struct disk* disk, void* private, uint32_t size, uint32_t nmemb, char* out);
typedef int (*FS_RESOLVE_FUNCTION)(struct disk* disk);

typedef int (*FS_CLOSE_FUNCTION)(void* private);

typedef int (*FS_SEEK_FUNCTION)(void* private, uint32_t offset, FILE_SEEK_MODE seek_mode);


Expand All @@ -53,6 +55,7 @@ struct filesystem
FS_READ_FUNCTION read;
FS_SEEK_FUNCTION seek;
FS_STAT_FUNCTION stat;
FS_CLOSE_FUNCTION close;
char name[20];
};

Expand All @@ -76,6 +79,7 @@ int fopen(const char* filename, const char* mode_str);
int fseek(int fd, int offset, FILE_SEEK_MODE whence);
int fread(void* ptr, uint32_t size, uint32_t nmemb, int fd);
int fstat(int fd, struct file_stat* stat);
int fclose(int fd);

void fs_insert_filesystem(struct filesystem* filesystem);
struct filesystem* fs_resolve(struct disk* disk);
Expand Down

0 comments on commit 75f7e7f

Please sign in to comment.