/*****************************************************************************************
Copyright (c) 2002 The UbixOS Project
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of
conditions, the following disclaimer and the list of authors. Redistributions in binary
form must reproduce the above copyright notice, this list of conditions, the following
disclaimer and the list of authors in the documentation and/or other materials provided
with the distribution. Neither the name of the UbixOS Project nor the names of its
contributors may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
$Id$
*****************************************************************************************/
#include <vfs/vfs.h>
#include <vfs/file.h>
#include <ubixos/sched.h>
#include <ubixos/vitals.h>
#include <ubixos/kpanic.h>
#include <ubixos/spinlock.h>
#include <lib/kmalloc.h>
#include <lib/string.h>
#include <vmm/paging.h>
#include <lib/kprintf.h>
#include <assert.h>
static spinLock_t fdTable_lock = SPIN_LOCK_INITIALIZER;
fileDescriptor *fdTable = 0x0;
/* USER */
void sysFwrite(char *ptr,int size,userFileDescriptor *userFd) {
if (userFd == 0x0) {
tty_print(ptr,_current->term);
}
else {
fwrite(ptr,size,1,userFd->fd);
}
return;
}
void sysFgetc(int *ptr,userFileDescriptor *userFd) {
fileDescriptor *tmpFd = 0x0;
tmpFd = userFd->fd;
if (userFd->fd == 0x0) {
while (1) {
if (_current->term == tty_foreground) {
if ((*ptr = getch()) != 0x0)
return;
sched_yield();
}
else {
sched_yield();
}
/*
else {
kprintf("Waking Task: %i\n",tty_foreground->owner);
sched_setStatus(tty_foreground->owner,READY);
kprintf("Sleeping Task: %i\n",_current->id);
sched_setStatus(_current->id,WAIT);
sched_yield();
}
*/
}
}
else {
ptr[0] = (int) fgetc(tmpFd);
}
}
void sysRmDir() {
return;
}
void sysFseek(userFileDescriptor *userFd,long offset,int whence) {
// TODO : coredump?
if (userFd == NULL)
return;
if (userFd->fd == NULL)
return;
userFd->fd->offset = offset+whence;
}
void sysChDir(const char *path) {
if (strstr(path,":") == 0x0) {
sprintf(_current->oInfo.cwd,"%s%s",_current->oInfo.cwd,path);
}
else {
sprintf(_current->oInfo.cwd,path);
}
}
void sysUnlink(const char *path,int *retVal) {
*retVal = unlink(path);
}
/************************************************************************
Function: void sysFopen();
Description: Opens A File Descriptor For A User Task
Notes:
************************************************************************/
void sysFopen(const char *file,char *flags,userFileDescriptor *userFd) {
if (userFd == 0x0)
kpanic("Error: userFd == NULL, File: %s, Line: %i\n",__FILE__,__LINE__);
userFd->fd = fopen(file,flags);
if (userFd->fd != 0x0) {
userFd->fdSize = userFd->fd->size;
}
/* Return */
return;
}
/************************************************************************
Function: void sysFread();
Description: Reads SIZE Bytes From The userFd Into DATA
Notes:
************************************************************************/
void sysFread(void *data,long size,userFileDescriptor *userFd) {
/* TODO : coredump? */
if (userFd == NULL)
return;
if (userFd->fd == NULL)
return;
fread(data,size,1,userFd->fd);
/* Return */
return;
}
/************************************************************************
Function: void sysFclse();
Description: Closes A File Descriptor For A User Task
Notes:
************************************************************************/
void sysFclose(userFileDescriptor *userFd,int *status) {
if (userFd == NULL)
{
*status = -1;
return;
}
if (userFd->fd == NULL)
{
*status = -1;
return;
}
*status = fclose(userFd->fd);
/* Return */
return;
}
/* KERNEL */
size_t fread(void *ptr,int size,int nmemb,fileDescriptor *fd) {
if (fd == 0x0)
return(0x0);
if (nmemb == 0x0) nmemb = 1; //Temp Fix
assert(fd);
//kprintf("fd->fileName: %s:%i\n",fd->fileName,_current->id);
assert(fd->mp);
assert(fd->mp->fs);
fd->mp->fs->vfsRead(fd,ptr,fd->offset,size * nmemb);
fd->offset += size * nmemb;
return(size * nmemb);
}
size_t fwrite(void *ptr,int size,int nmemb,fileDescriptor *fd) {
if (fd != 0x0) {
fd->mp->fs->vfsWrite(fd,ptr,fd->offset,size * nmemb);
fd->offset += size * nmemb;
}
return(0x0);
}
int fseek(fileDescriptor *tmpFd,long offset,int whence) {
tmpFd->offset = offset+whence;
return(tmpFd->offset);
}
/************************************************************************
Function: int feof(fileDescriptor *fd)
Description: Check A File Descriptor For EOF And Return Result
Notes:
************************************************************************/
int feof(fileDescriptor *fd) {
if (fd->status == fdEof) {
return(-1);
}
return(0);
}
/************************************************************************
Function: int fputc(int ch,fileDescriptor *fd)
Description: This Will Write Character To FD
Notes:
************************************************************************/
int fputc(int ch,fileDescriptor *fd) {
if (fd != 0x0) {
ch = fd->mp->fs->vfsWrite(fd,(char *)ch,fd->offset,1);
fd->offset++;
return(ch);
}
/* Return NULL If FD Is Not Found */
return(0x0);
}
/************************************************************************
Function: int fgetc(fileDescriptor *fd)
Description: This Will Return The Next Character In A FD Stream
Notes:
************************************************************************/
int fgetc(fileDescriptor *fd) {
int ch = 0x0;
/* If Found Return Next Char */
if (fd != 0x0) {
fd->mp->fs->vfsRead(fd,(char *)&ch,fd->offset,1);
fd->offset++;
return(ch);
}
/* Return NULL If FD Is Not Found */
return(0x0);
}
/************************************************************************
Function: fileDescriptor *fopen(const char *file,cont char *flags)
Description: This Will Open A File And Return A File Descriptor
Notes:
08/05/02 - Just Started A Rewrite Of This Function Should Work Out Well
************************************************************************/
fileDescriptor *fopen(const char *file,const char *flags) {
int i = 0x0;
char *path = 0x0;
char *mountPoint = 0x0;
char fileName[1024];
fileDescriptor *tmpFd = 0x0;
spinLock(&fdTable_lock);
/* Allocate Memory For File Descriptor */
if ((tmpFd = (fileDescriptor *)kmalloc(sizeof(fileDescriptor))) == 0x0) {
kprintf("Error: tmpFd == NULL, File: %s, Line: %i\n",__FILE__,__LINE__);
spinUnlock(&fdTable_lock);
return(0x0);
}
sprintf(fileName,file);
if (strstr(file,":")) {
mountPoint = (char *)strtok((char *)&fileName,":");
path = strtok(NULL,"\n");
}
else {
path = &fileName;
}
if (path[0] == '/') {
sprintf(tmpFd->fileName,path);
}
else {
sprintf(tmpFd->fileName,"/%s",path);
}
/* Find our mount point or set default to sys */
if (mountPoint == 0x0) {
tmpFd->mp = vfs_findMount("sys");
}
else {
tmpFd->mp = vfs_findMount(mountPoint);
}
if (tmpFd->mp == 0x0) {
kprintf("Mount Point Bad\n");
spinUnlock(&fdTable_lock);
return(0x0);
}
/* This Will Set Up The Descriptor Modes */
tmpFd->mode = 0x0;
for (i = 0x0;'\0' != flags[i];i++) {
switch(flags[i]) {
case 'w':
case 'W':
tmpFd->mode |= fileWrite;
break;
case 'r':
case 'R':
tmpFd->mode |= fileRead;
break;
case 'b':
case 'B':
tmpFd->mode |= fileBinary;
break;
case 'a':
case 'A':
tmpFd->mode |= fileAppend;
break;
default:
break;
}
}
/* Search For The File */
if (tmpFd->mp->fs->vfsOpenFile(tmpFd->fileName,tmpFd) == 0x1) {
/* If The File Is Found Then Set Up The Descriptor */
tmpFd->buffer = (char *)kmalloc(0x1000);
/* Set Its Status To Open */
tmpFd->status = fdOpen;
/* Initial File Offset Is Zero */
tmpFd->offset = 0x0;
/* Increment Number Of Open Files */
systemVitals->openFiles++;
tmpFd->prev = 0x0;
tmpFd->next = fdTable;
if (fdTable != 0x0)
fdTable->prev = tmpFd;
fdTable = tmpFd;
/* Return The FD */
spinUnlock(&fdTable_lock);
return(tmpFd);
}
else {
/* If The File Was Not Found Free The Memory */
kfree(tmpFd);
}
/* Return NULL */
spinUnlock(&fdTable_lock);
return(0x0);
}
/************************************************************************
Function: int fclose(fileDescriptor *fd);
Description: This Will Close And Free A File Descriptor
Notes:
************************************************************************/
int fclose(fileDescriptor *fd) {
fileDescriptor *tmpFd = 0x0;
assert(fd);
spinLock(&fdTable_lock);
for (tmpFd = fdTable;tmpFd != 0x0;tmpFd = tmpFd->next) {
if (tmpFd == fd) {
if (tmpFd->prev)
tmpFd->prev->next = tmpFd->next;
if (tmpFd->next)
tmpFd->next->prev = tmpFd->prev;
if (tmpFd == fdTable)
fdTable = tmpFd->next;
kfree(tmpFd->buffer);
kfree(tmpFd);
spinUnlock(&fdTable_lock);
return(0x0);
}
}
spinUnlock(&fdTable_lock);
return(0x1);
}
/* UBU */
/************************************************************************
Function: void sysMkDir(const char *path)
Description: This Will Create A New Directory
Notes:
************************************************************************/
void sysMkDir(const char *path) {
fileDescriptor *tmpFD = 0x0;
char tmpDir[1024];
char rootPath[256];
char *dir = 0x0;//UBU*mountPoint = 0x0;
char *tmp = 0x0;
rootPath[0] = '\0';
dir = (char *)path;
if (strstr(path,":") == 0x0) {
sprintf(tmpDir,"%s%s",_current->oInfo.cwd,path);
dir = (char *)&tmpDir;
}
while (strstr(dir,"/")) {
if (rootPath[0] == 0x0)
sprintf(rootPath,"%s/",strtok(dir,"/"));
else
sprintf(rootPath,"%s%s/",rootPath,strtok(dir,"/"));
tmp = strtok(NULL,"\n");
dir = tmp;
}
//kprintf("rootPath: [%s]\n",rootPath);
tmpFD = fopen(rootPath,"rb");
if (tmpFD->mp == 0x0) {
kprintf("Invalid Mount Point\n");
}
tmpFD->mp->fs->vfsMakeDir(dir,tmpFD);
fclose(tmpFD);
return;
}
/************************************************************************
Function: int unlink(const char *node)
Description: This will unlink a file
Notes:
************************************************************************/
int unlink(const char *node) {
char *path = 0x0,*mountPoint = 0x0;
vfs_mountPoint_t *mp = 0x0;
path = (char *)strtok((char *)node,"@");
mountPoint = strtok(NULL,"\n");
if (mountPoint == 0x0) {
mp = vfs_findMount("sys"); /* _current->oInfo.container; */
}
else {
mp = vfs_findMount(mountPoint);
}
if (mp == 0x0) {
//kpanic("Mount Point Bad");
return(0x0);
}
mp->fs->vfsUnlink(path,mp);
return(0x0);
}
/***
$Log$
Revision 1.23 2004/09/11 12:59:21 apwillia
Fixed file syscalls so fdisk does not kernel panic
Revision 1.22 2004/09/11 12:11:11 reddawg
Cleaning up the VFS more changes to follow...
Revision 1.21 2004/08/21 17:36:57 reddawg
updates: converted to software task switching however it is not working yet
Revision 1.20 2004/08/14 11:23:02 reddawg
Changes
Revision 1.19 2004/08/09 12:58:05 reddawg
let me know when you got the surce
Revision 1.18 2004/08/06 22:32:16 reddawg
Ubix Works Again
Revision 1.16 2004/07/28 15:05:43 reddawg
Major:
Pages now have strict security enforcement.
Many null dereferences have been resolved.
When apps loaded permissions set for pages rw and ro
Revision 1.15 2004/07/27 19:24:31 flameshadow
chg: reduced the number of debugging statements in the kernel.
Revision 1.14 2004/07/27 04:05:20 flameshadow
chg: kinda fixed it. Added bunches of debug info
Revision 1.13 2004/07/23 09:10:06 reddawg
ubixfs: cleaned up some functions played with the caching a bit
vfs: renamed a bunch of functions
cleaned up a few misc bugs
Revision 1.12 2004/07/22 22:37:03 reddawg
Caching is working now the FS is extremely fast but needs to be optimized to do 32bit copies over 8bit
Revision 1.11 2004/07/22 19:43:36 reddawg
ok we are absolute now...... ?
Revision 1.10 2004/07/21 10:02:09 reddawg
devfs: renamed functions
device system: renamed functions
fdc: fixed a few potential bugs and cleaned up some unused variables
strol: fixed definition
endtask: made it print out freepage debug info
kmalloc: fixed a huge memory leak we had some unhandled descriptor insertion so some descriptors were lost
ld: fixed a pointer conversion
file: cleaned up a few unused variables
sched: broke task deletion
kprintf: fixed ogPrintf definition
Revision 1.9 2004/07/18 05:24:15 reddawg
Fixens
Revision 1.8 2004/06/29 03:59:48 reddawg
Fixed a few issues with subdirectories they are working much better now
Revision 1.7 2004/06/29 00:18:49 reddawg
Sub Dirs
Revision 1.6 2004/06/28 23:12:58 reddawg
file format now container:/path/to/file
Revision 1.5 2004/06/16 19:56:35 reddawg
Moved fclose
Revision 1.4 2004/06/14 12:20:54 reddawg
notes: many bugs repaired and ld works 100% now.
Revision 1.3 2004/06/01 00:04:53 reddawg
Try now mark
Revision 1.2 2004/05/19 04:07:43 reddawg
kmalloc(size,pid) no more it is no kmalloc(size); the way it should of been
Revision 1.1.1.1 2004/04/15 12:06:53 reddawg
UbixOS v1.0
Revision 1.12 2004/04/13 16:36:34 reddawg
Changed our copyright, it is all now under a BSD-Style license
END
***/