Skip to content
Closed
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
1 change: 1 addition & 0 deletions sys/fs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include $(RIOTBASE)/Makefile.base
80 changes: 80 additions & 0 deletions sys/fs/fs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright (C) 2014 Martin Lenders
* Copyright (C) 2015 Lucas Jenß <lucas@x3ro.de>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
#include <errno.h>
#include <string.h>

#include "fs.h"

static fs_table_t fs_table[FS_TABLE_SIZE];

const fs_op_table_t fs_op_table[1];

static fs_table_t *fs_search_entry(const char *path)
{
fs_table_t *tmp = NULL;
size_t tmp_len = 0;

for (int i = 0; i < FS_TABLE_SIZE; i++) {
if (fs_table[i].mp_len >= tmp_len &&
strncmp(fs_table[i].mount_point, path, fs_table[i].mp_len) == 0) {
tmp = &(fs_table[i]);
tmp_len = fs_table[i].mp_len;
}
}

return tmp;
}

int fs_mount(const char *mount_point, int device, fs_type_t type)
{
if (strlen(mount_point) + 1 > FS_MOUNT_POINT_LEN) {
errno = ENAMETOOLONG;
return -ENAMETOOLONG;
}

int i = 0;
for (i = 0; fs_table[i].type != FS_NONE && i < FS_TABLE_SIZE; i++);

if (i == FS_TABLE_SIZE) {
errno = ENOMEM;
return -ENOMEM;
}

strncpy(fs_table[i].mount_point, mount_point, FS_MOUNT_POINT_LEN);
fs_table[i].mp_len = strlen(mount_point);

if (fs_table[i].mp_len > FS_MOUNT_POINT_LEN) {
fs_table[i].mp_len = FS_MOUNT_POINT_LEN;
}

fs_table[i].device = device;
fs_table[i].type = type;

return 0;
}

int fs_unmount(const char *mount_point, int device)
{
if (!mount_point && !device) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

&& -> ||

errno = ENOENT;
return -ENOENT;
}

for (int i = 0; i < FS_TABLE_SIZE; i++) {
if ((device && fs_table[i].device == device) ||
(mount_point && !strncmp(fs_table[i].mount_point, mount_point,
strlen(fs_table[i].mount_point)))) {
fs_table[i].type = FS_NONE;
return 0;
}
}

errno = EINVAL;
return -EINVAL;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm not exactly familiar with the semantics of these return codes, but from what I've read it seems that fs_unmount should return -ENOENT here and -EINVAL above. Can somebody please clarify why is is done the other way around here? 😃

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think it seems to follow the Linux behaviour of ENOENT for a bad mount point and EINVAL for not a mount point.

Linux umount(2) manpage:

ERRORS

EAGAIN A call to umount2() specifying MNT_EXPIRE successfully marked an unbusy filesystem as expired.

EBUSY target could not be unmounted because it is busy.

EFAULT target points outside the user address space.

EINVAL target is not a mount point.

EINVAL umount2() was called with MNT_EXPIRE and either MNT_DETACH or MNT_FORCE.

EINVAL (since Linux 2.6.34) umount2() was called with an invalid flag value in flags.

ENAMETOOLONG A pathname was longer than MAXPATHLEN.

ENOENT A pathname was empty or had a nonexistent component.

ENOMEM The kernel could not allocate a free page to copy filenames or data into.

EPERM The caller does not have the required privileges.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ah thanks, that makes a lot of sense. I tried to match what I found here with the behavior of the function 😕 I'll check the Linux behavior more thoroughly from now on :)

}
Loading