2011-11-17 17:31:41 -05:00
|
|
|
/******************************************************************************
|
|
|
|
|
|
|
|
COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011.
|
|
|
|
|
|
|
|
This file is part of Sortix.
|
|
|
|
|
|
|
|
Sortix is free software: you can redistribute it and/or modify it under the
|
|
|
|
terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
version.
|
|
|
|
|
|
|
|
Sortix is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with Sortix. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
filesystem.cpp
|
|
|
|
Allows access to stored sequences of bytes in an orderly fashion.
|
|
|
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include "platform.h"
|
2011-11-21 06:19:57 -05:00
|
|
|
#include <libmaxsi/error.h>
|
2011-11-18 11:49:31 -05:00
|
|
|
#include <libmaxsi/memory.h>
|
2011-11-19 04:32:29 -05:00
|
|
|
#include <libmaxsi/string.h>
|
2011-11-17 17:31:41 -05:00
|
|
|
#include "syscall.h"
|
|
|
|
#include "process.h"
|
|
|
|
#include "filesystem.h"
|
2011-11-21 06:19:57 -05:00
|
|
|
#include "directory.h"
|
2011-11-17 17:31:41 -05:00
|
|
|
#include "mount.h"
|
|
|
|
|
2011-11-19 04:32:29 -05:00
|
|
|
using namespace Maxsi;
|
|
|
|
|
2011-11-17 17:31:41 -05:00
|
|
|
namespace Sortix
|
|
|
|
{
|
|
|
|
namespace FileSystem
|
|
|
|
{
|
|
|
|
Device* Open(const char* path, int flags, mode_t mode)
|
|
|
|
{
|
2011-11-21 06:19:57 -05:00
|
|
|
Process* process = CurrentProcess();
|
|
|
|
const char* wd = process->workingdir;
|
|
|
|
char* abs = Directory::MakeAbsolute(wd, path);
|
2011-11-22 11:26:47 -05:00
|
|
|
if ( !abs ) { Error::Set(ENOMEM); return NULL; }
|
2011-11-21 06:19:57 -05:00
|
|
|
|
2011-11-17 17:31:41 -05:00
|
|
|
size_t pathoffset = 0;
|
2011-11-21 06:19:57 -05:00
|
|
|
DevFileSystem* fs = Mount::WhichFileSystem(abs, &pathoffset);
|
|
|
|
if ( !fs ) { delete[] abs; return NULL; }
|
|
|
|
Device* result = fs->Open(abs + pathoffset, flags, mode);
|
|
|
|
delete[] abs;
|
2011-11-19 04:32:29 -05:00
|
|
|
return result;
|
2011-11-17 17:31:41 -05:00
|
|
|
}
|
|
|
|
|
2011-11-21 12:49:55 -05:00
|
|
|
bool Unlink(const char* path)
|
|
|
|
{
|
|
|
|
Process* process = CurrentProcess();
|
|
|
|
const char* wd = process->workingdir;
|
|
|
|
char* abs = Directory::MakeAbsolute(wd, path);
|
2011-11-22 11:26:47 -05:00
|
|
|
if ( !abs ) { Error::Set(ENOMEM); return false; }
|
2011-11-21 12:49:55 -05:00
|
|
|
|
|
|
|
size_t pathoffset = 0;
|
|
|
|
DevFileSystem* fs = Mount::WhichFileSystem(abs, &pathoffset);
|
|
|
|
if ( !fs ) { delete[] abs; return false; }
|
|
|
|
bool result = fs->Unlink(abs + pathoffset);
|
|
|
|
delete[] abs;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-11-17 17:31:41 -05:00
|
|
|
int SysOpen(const char* path, int flags, mode_t mode)
|
|
|
|
{
|
|
|
|
Process* process = CurrentProcess();
|
|
|
|
Device* dev = Open(path, flags, mode);
|
|
|
|
if ( !dev ) { return -1; /* TODO: errno */ }
|
|
|
|
int fd = process->descriptors.Allocate(dev);
|
|
|
|
if ( fd < 0 ) { dev->Unref(); }
|
2011-11-18 11:49:31 -05:00
|
|
|
return fd;
|
2011-11-17 17:31:41 -05:00
|
|
|
}
|
|
|
|
|
2011-11-21 12:49:55 -05:00
|
|
|
int SysUnlink(const char* path)
|
|
|
|
{
|
|
|
|
return Unlink(path) ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
2012-01-14 10:25:28 -05:00
|
|
|
int SysMkDir(const char* pathname, mode_t mode)
|
|
|
|
{
|
|
|
|
// TODO: Add the proper filesystem support!
|
|
|
|
Error::Set(ENOSYS);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SysRmDir(const char* pathname)
|
|
|
|
{
|
|
|
|
// TODO: Add the proper filesystem support!
|
|
|
|
Error::Set(ENOSYS);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-01-14 10:37:21 -05:00
|
|
|
int SysTruncate(const char* pathname, off_t length)
|
|
|
|
{
|
|
|
|
// TODO: Add the proper filesystem support!
|
|
|
|
Error::Set(ENOSYS);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SysFTruncate(const char* pathname, off_t length)
|
|
|
|
{
|
|
|
|
// TODO: Add the proper filesystem support!
|
|
|
|
Error::Set(ENOSYS);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-02-21 18:30:34 -05:00
|
|
|
int SysStat(const char* pathname, struct stat* st)
|
|
|
|
{
|
|
|
|
// TODO: Add the proper filesystem support!
|
|
|
|
Error::Set(ENOSYS);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SysFStat(int fd, struct stat* st)
|
|
|
|
{
|
|
|
|
// TODO: Add the proper filesystem support!
|
|
|
|
Error::Set(ENOSYS);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-11-17 17:31:41 -05:00
|
|
|
void Init()
|
|
|
|
{
|
|
|
|
Syscall::Register(SYSCALL_OPEN, (void*) SysOpen);
|
2011-11-21 12:49:55 -05:00
|
|
|
Syscall::Register(SYSCALL_UNLINK, (void*) SysUnlink);
|
2012-01-14 10:25:28 -05:00
|
|
|
Syscall::Register(SYSCALL_MKDIR, (void*) SysMkDir);
|
|
|
|
Syscall::Register(SYSCALL_RMDIR, (void*) SysRmDir);
|
2012-01-14 10:37:21 -05:00
|
|
|
Syscall::Register(SYSCALL_TRUNCATE, (void*) SysTruncate);
|
|
|
|
Syscall::Register(SYSCALL_FTRUNCATE, (void*) SysFTruncate);
|
2012-02-21 18:30:34 -05:00
|
|
|
Syscall::Register(SYSCALL_STAT, (void*) SysStat);
|
|
|
|
Syscall::Register(SYSCALL_FSTAT, (void*) SysFStat);
|
2011-11-17 17:31:41 -05:00
|
|
|
}
|
|
|
|
}
|
2011-11-18 11:49:31 -05:00
|
|
|
|
|
|
|
DevFileWrapper::DevFileWrapper(DevBuffer* buffer, int flags)
|
|
|
|
{
|
|
|
|
this->buffer = buffer;
|
|
|
|
this->flags = flags;
|
|
|
|
this->offset = 0;
|
|
|
|
this->buffer->Refer();
|
2012-01-08 08:15:51 -05:00
|
|
|
if ( flags & O_TRUNC ) { buffer->Resize(0); }
|
2011-11-18 11:49:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
DevFileWrapper::~DevFileWrapper()
|
|
|
|
{
|
|
|
|
buffer->Unref();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t DevFileWrapper::BlockSize()
|
|
|
|
{
|
|
|
|
return buffer->BlockSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
uintmax_t DevFileWrapper::Size()
|
|
|
|
{
|
|
|
|
return buffer->Size();
|
|
|
|
}
|
|
|
|
|
|
|
|
uintmax_t DevFileWrapper::Position()
|
|
|
|
{
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DevFileWrapper::Seek(uintmax_t position)
|
|
|
|
{
|
|
|
|
if ( !buffer->Seek(position) ) { return false; }
|
|
|
|
offset = position;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DevFileWrapper::Resize(uintmax_t size)
|
|
|
|
{
|
|
|
|
return buffer->Resize(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t DevFileWrapper::Read(byte* dest, size_t count)
|
|
|
|
{
|
|
|
|
// TODO: Enforce read permission!
|
|
|
|
if ( !buffer->Seek(offset) ) { return -1; }
|
|
|
|
ssize_t result = buffer->Read(dest, count);
|
|
|
|
if ( result < 0 ) { return result; }
|
|
|
|
offset += result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t DevFileWrapper::Write(const byte* src, size_t count)
|
|
|
|
{
|
|
|
|
// TODO: Enforce write permission!
|
|
|
|
if ( !buffer->Seek(offset) ) { return -1; }
|
|
|
|
ssize_t result = buffer->Write(src, count);
|
|
|
|
if ( result < 0 ) { return result; }
|
|
|
|
offset += result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DevFileWrapper::IsReadable()
|
|
|
|
{
|
|
|
|
// TODO: Enforce read permission!
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DevFileWrapper::IsWritable()
|
|
|
|
{
|
|
|
|
// TODO: Enforce write permission!
|
|
|
|
return true;
|
|
|
|
}
|
2011-11-17 17:31:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|