Skip to content

Commit

Permalink
Many improvements in the new file abstraction layer, still work in pr…
Browse files Browse the repository at this point in the history
…ogress though
  • Loading branch information
Terraneo Federico committed Jul 14, 2013
1 parent bbca560 commit 8c88e3e
Show file tree
Hide file tree
Showing 20 changed files with 846 additions and 66 deletions.
6 changes: 0 additions & 6 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@

#include <cstdio>
#include "miosix.h"
#include "filesystem/devfs/devfs.h"

using namespace std;
using namespace miosix;

int main()
{
//iprintf("Hello world, write your application here\n");
intrusive_ref_ptr<DevFs> dfs(new DevFs);
FilesystemManager& fsm=FilesystemManager::instance();
fsm.kmount("/dev",dfs);
FileDescriptorTable fdt;

}
1 change: 1 addition & 0 deletions miosix/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ kernel/scheduler/edf/edf_scheduler.cpp \
filesystem/filesystem.cpp \
filesystem/file_access.cpp \
filesystem/file.cpp \
filesystem/mountpointfs/mountpointfs.cpp \
filesystem/devfs/devfs.cpp \
filesystem/fat32/ff.c \
filesystem/fat32/diskio.cpp \
Expand Down
22 changes: 22 additions & 0 deletions miosix/_doc/textdoc/Changelog.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
Changelog for Miosix np embedded OS

- File abstraction layer now handles stdin/stdout/stderr and passes the
testsuite, still does not handle fat32 and echo on console is not working.
- File abstraction layer: added MountpointFs as the default root filesystem
to allow mounting other filesystems.
- Added operator[] to StringPart.
- File abstraction layer: added pure virtual mkdir() to FilesystemBase.
- Renamed DIR struct in FatFs code into _DIR_ to avoid conflicts with
struct DIR in dirent.h
- File abstraction layer: added ConsoleDevice class to store the default
console device passed by the board support package.
- File abstraction layer: added ConsoleAdapter class to map the old
Console class into the new file abstraction. Will be removed when all
bsp will upgrade to the new model.
- File abstraction layer: correctly handling the one FiledescriptorTable
model when WITH_PROCESSES is undefined (processes branch not merged yet).
- File abstraction layer: now FileDescriptorTable's constructor opens
/dev/console as file descriptors 0, 1 and 2 (stdin, stdout and stderr).
- Now all non reentrant syscalls use CReentrancyAccessor::getReent() to set
the errno variable of the thread they're called in.
- Added option in libc_integration to route the filesystem-related syscalls
to the old one filesystem model, or the new file abstraction layer allowing
multiple mountpoint model. The new model will gradually replace the old one.
- Added -DCOMPILING_MIOSIX to CFLAGS and CXXFLAGS, to allow the introduction
of private headers.
- getMaxHeap() in libc_integration.h is now in the miosix namespace
Expand Down
1 change: 1 addition & 0 deletions miosix/config/miosix_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ namespace miosix {
#define SYNC_AFTER_WRITE

/// Maximum number of open files. Trying to open more will fail.
/// Cannot be lower than 3, as the first three are stdin, stdout, stderr
const unsigned char MAX_OPEN_FILES=8;

//
Expand Down
17 changes: 15 additions & 2 deletions miosix/filesystem/devfs/devfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,16 @@ namespace miosix {

DevFs::DevFs()
{
files[StringPart("null")]=intrusive_ref_ptr<FileBase>(new NullFile);
files[StringPart("zero")]=intrusive_ref_ptr<FileBase>(new ZeroFile);
string null="/null";
string zero="/zero";
string console="/console";

files[StringPart(null)]=intrusive_ref_ptr<FileBase>(new NullFile);
files[StringPart(zero)]=intrusive_ref_ptr<FileBase>(new ZeroFile);

intrusive_ref_ptr<FileBase> consoleDev=ConsoleDevice::instance().get();
if(consoleDev) files[StringPart(console)]=consoleDev;
else files[StringPart(console)]=files[StringPart(null)];
}

int DevFs::open(intrusive_ref_ptr<FileBase>& file, StringPart& name, int flags,
Expand All @@ -61,6 +69,11 @@ int DevFs::lstat(StringPart& name, struct stat *pstat)
return 0;
}

int DevFs::mkdir(StringPart& name, int mode)
{
return -EACCES; // No directories support in DevFs yet
}

bool DevFs::areAllFilesClosed()
{
Lock<Mutex> l(mutex);
Expand Down
11 changes: 9 additions & 2 deletions miosix/filesystem/devfs/devfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#define DEVFS_H

#include <map>
#include <string>
#include "filesystem/file.h"
#include "filesystem/file_access.h"

Expand Down Expand Up @@ -80,14 +79,22 @@ class DevFs : public FilesystemBase
virtual int open(intrusive_ref_ptr<FileBase>& file, StringPart& name,
int flags, int mode);

/**
/**
* Obtain information on a file, identified by a path name. Does not follow
* symlinks
* \param name path name, relative to the local filesystem
* \param pstat file information is stored here
* \return 0 on success, or a negative number on failure
*/
virtual int lstat(StringPart& name, struct stat *pstat);

/**
* Create a directory
* \param name directory name
* \param mode directory permissions
* \return 0 on success, or a negative number on failure
*/
virtual int mkdir(StringPart& name, int mode);

/**
* \internal
Expand Down
24 changes: 12 additions & 12 deletions miosix/filesystem/fat32/ff.c
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ DWORD clust2sect ( /* !=0: sector number, 0: failed - invalid cluster# */

static
BOOL next_dir_entry ( /* TRUE: successful, FALSE: could not move next */
DIR *dj /* Pointer to directory object */
_DIR_ *dj /* Pointer to directory object */
)
{
DWORD clust;
Expand Down Expand Up @@ -498,7 +498,7 @@ char make_dirfile ( /* 1: error - detected an invalid format, '\0'or'/': next c

static
FRESULT trace_path ( /* FR_OK(0): successful, !=0: error code */
DIR *dj, /* Pointer to directory object to return last directory */
_DIR_ *dj, /* Pointer to directory object to return last directory */
char *fn, /* Pointer to last segment name to return {file(8),ext(3),attr(1)} */
const char *path, /* Full-path string to trace a file or directory */
BYTE **dir /* Pointer to pointer to found entry to retutn */
Expand Down Expand Up @@ -558,7 +558,7 @@ FRESULT trace_path ( /* FR_OK(0): successful, !=0: error code */
#if !_FS_READONLY
static
FRESULT reserve_direntry ( /* FR_OK: successful, FR_DENIED: no free entry, FR_RW_ERROR: a disk error occured */
DIR *dj, /* Target directory to create new entry */
_DIR_ *dj, /* Target directory to create new entry */
BYTE **dir /* Pointer to pointer to created entry to retutn */
)
{
Expand Down Expand Up @@ -820,7 +820,7 @@ FRESULT f_open (
)
{
FRESULT res;
DIR dj;
_DIR_ dj;
BYTE *dir;
char fn[8+3+1];

Expand Down Expand Up @@ -1230,7 +1230,7 @@ FRESULT f_lseek (
/*-----------------------------------------------------------------------*/

FRESULT f_opendir (
DIR *dj, /* Pointer to directory object to create */
_DIR_ *dj, /* Pointer to directory object to create */
const char *path /* Pointer to the directory path */
)
{
Expand Down Expand Up @@ -1267,7 +1267,7 @@ FRESULT f_opendir (
/*-----------------------------------------------------------------------*/

FRESULT f_readdir (
DIR *dj, /* Pointer to the directory object */
_DIR_ *dj, /* Pointer to the directory object */
FILINFO *finfo /* Pointer to file information to return */
)
{
Expand Down Expand Up @@ -1307,7 +1307,7 @@ FRESULT f_stat (
)
{
FRESULT res;
DIR dj;
_DIR_ dj;
BYTE *dir;
char fn[8+3+1];

Expand Down Expand Up @@ -1444,7 +1444,7 @@ FRESULT f_unlink (
)
{
FRESULT res;
DIR dj;
_DIR_ dj;
BYTE *dir, *sdir;
DWORD dclust, dsect;
char fn[8+3+1];
Expand Down Expand Up @@ -1492,7 +1492,7 @@ FRESULT f_mkdir (
)
{
FRESULT res;
DIR dj;
_DIR_ dj;
BYTE *dir, *fw, n;
char fn[8+3+1];
DWORD sect, dsect, dclust, pclust, tim;
Expand Down Expand Up @@ -1559,7 +1559,7 @@ FRESULT f_chmod (
)
{
FRESULT res;
DIR dj;
_DIR_ dj;
BYTE *dir;
char fn[8+3+1];

Expand Down Expand Up @@ -1593,7 +1593,7 @@ FRESULT f_utime (
)
{
FRESULT res;
DIR dj;
_DIR_ dj;
BYTE *dir;
char fn[8+3+1];

Expand Down Expand Up @@ -1627,7 +1627,7 @@ FRESULT f_rename (
)
{
FRESULT res;
DIR dj;
_DIR_ dj;
DWORD sect_old;
BYTE *dir_old, *dir_new, direntry[32-11];
char fn[8+3+1];
Expand Down
6 changes: 3 additions & 3 deletions miosix/filesystem/fat32/ff.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ typedef struct _DIR {
DWORD sclust; /* Start cluster */
DWORD clust; /* Current cluster */
DWORD sect; /* Current sector */
} DIR;
} _DIR_;


/* File object structure */
Expand Down Expand Up @@ -205,8 +205,8 @@ FRESULT f_read (FIL*, void*, UINT, UINT*); /* Read data from a file */
FRESULT f_write (FIL*, const void*, UINT, UINT*); /* Write data to a file */
FRESULT f_lseek (FIL*, DWORD); /* Move file pointer of a file object */
FRESULT f_close (FIL*); /* Close an open file object */
FRESULT f_opendir (DIR*, const char*); /* Open an existing directory */
FRESULT f_readdir (DIR*, FILINFO*); /* Read a directory item */
FRESULT f_opendir (_DIR_*, const char*); /* Open an existing directory */
FRESULT f_readdir (_DIR_*, FILINFO*); /* Read a directory item */
FRESULT f_stat (const char*, FILINFO*); /* Get file status */
FRESULT f_getfree (const char*, DWORD*, FATFS**); /* Get number of free clusters on the drive */
FRESULT f_truncate (FIL*); /* Truncate file */
Expand Down
25 changes: 25 additions & 0 deletions miosix/filesystem/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include "file.h"
#include <stdio.h>
#include <interfaces/console.h> //FIXME: remove
#include "file_access.h"

namespace miosix {
Expand Down Expand Up @@ -222,4 +223,28 @@ int TerminalDevice::isatty() const { return device->isatty(); }

int TerminalDevice::sync() { return device->sync(); }

// FIXME temporary -- begin
ssize_t ConsoleAdapter::write(const void *data, size_t len)
{
Console::write(reinterpret_cast<const char*>(data),len);
return len;
}
ssize_t ConsoleAdapter::read(void *data, size_t len)
{
char *d=reinterpret_cast<char*>(data);
for(size_t i=0;i<len;i++) *d++=Console::readChar();
return len;
}
off_t ConsoleAdapter::lseek(off_t pos, int whence) { return -EBADF; }
int ConsoleAdapter::fstat(struct stat *pstat) const
{
memset(pstat,0,sizeof(struct stat));
pstat->st_mode=S_IFCHR;//Character device
pstat->st_blksize=0; //Defualt file buffer equals to BUFSIZ
return 0;
}
int ConsoleAdapter::isatty() const { return true; }
int ConsoleAdapter::sync() { return 0; }
// FIXME temporary -- end

} //namespace miosix
15 changes: 15 additions & 0 deletions miosix/filesystem/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,21 @@ class TerminalDevice : public FileBase
bool skipNewline;
};

/**
* FIXME remove this when removing Console interface!
*/
class ConsoleAdapter : public FileBase
{
public:
ConsoleAdapter() : FileBase(intrusive_ref_ptr<FilesystemBase>()) {}
virtual ssize_t write(const void *data, size_t len);
virtual ssize_t read(void *data, size_t len);
virtual off_t lseek(off_t pos, int whence);
virtual int fstat(struct stat *pstat) const;
virtual int isatty() const;
virtual int sync();
};

} //namespace miosix

#endif //FILE_H
53 changes: 53 additions & 0 deletions miosix/filesystem/file_access.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include <vector>
#include <climits>
#include "file_access.h"
#include "devfs/devfs.h"
#include "mountpointfs/mountpointfs.h"

using namespace std;

Expand Down Expand Up @@ -93,6 +95,10 @@ FilesystemBase::~FilesystemBase() {}
FileDescriptorTable::FileDescriptorTable() : mutex(Mutex::RECURSIVE), cwd("/")
{
FilesystemManager::instance().addFileDescriptorTable(this);
//We need to open stdin, stdout, stderr.
//We're relying on open to choose the lowest numbered files slot
open("/dev/console",0,0); //FIXME: flags is wrong
files[1]=files[2]=files[0];
}

FileDescriptorTable::FileDescriptorTable(const FileDescriptorTable& rhs)
Expand Down Expand Up @@ -626,6 +632,7 @@ int FilesystemManager::umount(const char* path, bool force)
//descriptor table (4 bytes), and because umount happens infrequently.
//Note that since we are locking the same mutex used by resolvePath(),
//other threads can't open new files concurrently while we check
#ifdef WITH_PROCESSES
list<FileDescriptorTable*>::iterator it3;
for(it3=fileTables.begin();it3!=fileTables.end();++it3)
{
Expand All @@ -642,6 +649,20 @@ int FilesystemManager::umount(const char* path, bool force)
}
}
}
#else //WITH_PROCESSES
for(int i=0;i<MAX_OPEN_FILES;i++)
{
intrusive_ref_ptr<FileBase> file=getFileDescriptorTable().getFile(i);
if(!file) continue;
vector<fsIt>::iterator it4;
for(it4=fsToUmount.begin();it4!=fsToUmount.end();++it4)
{
if(file->getParent()!=(*it4)->second) continue;
if(force==false) return -EBUSY;
getFileDescriptorTable().close(i);//If forced umount, close the file
}
}
#endif //WITH_PROCESSES

//Now there should be no more files belonging to the filesystems to umount,
//but check if it is really so, as there is a possible race condition
Expand Down Expand Up @@ -686,4 +707,36 @@ ResolvedPath FilesystemManager::resolvePath(string& path, bool followLastSymlink
return pr.resolvePath(path,followLastSymlink);
}

FilesystemManager::FilesystemManager()
{
//TODO: move to BSPs
intrusive_ref_ptr<FilesystemBase> rootFs(new MountpointFs);
string dev="/dev";
StringPart sp(dev);
rootFs->mkdir(sp,0755);
kmount("/",rootFs);
kmount("/dev",intrusive_ref_ptr<FilesystemBase>(new DevFs));
}

//
// class ConsoleDevice
//

ConsoleDevice& ConsoleDevice::instance()
{
static ConsoleDevice singleton;
return singleton;
}

FileDescriptorTable& getFileDescriptorTable()
{
#ifdef WITH_PROCESSES
//Something like
return Thread::getCurrentThread()->getProcess()->getFileTable();
#else //WITH_PROCESSES
static FileDescriptorTable fileTable; ///< The only file table
return fileTable;
#endif //WITH_PROCESSES
}

} //namespace miosix
Loading

0 comments on commit 8c88e3e

Please sign in to comment.