file.c
vfs/vfs.h
vfs/file.h
ubixos/sched.h
ubixos/vitals.h
ubixos/kpanic.h
ubixos/spinlock.h
lib/kmalloc.h
lib/string.h
vmm/paging.h
lib/kprintf.h
assert.h
sys/thread.h
ubixos/kpanic.h
ubixos/sched.h
ubixos/spinlock.h
ubixos/vitals.h
ubixos/types.h
sys/_types.h
lib/string.h
vmm/paging.h
ubixos/time.h
vfs/file.h
sys/tss.h
lib/kmalloc.h
vfs/vfs.h
vfs/mount.h
lib/kprintf.h
assert.h
ubixos/ubthread.h
ubixfs/dirCache.h
sys/sysproto.h
sys/signal.h
ubixos/tty.h
sys/io.h
src/sys/vfs/file.c
int int fclose (fileDescriptor *fd) fclose fileDescriptor * fd assert fileDescriptorStruct::buffer fdTable fdTable_lock kfree fileDescriptorStruct::next NULL vitalsStruct::openFiles fileDescriptorStruct::prev spinLock spinUnlock systemVitals x1 elf_loadfile execFile kmod_load ldEnable sys_exec sysExec sysFclose sysMkDir systemTask
int int feof (fileDescriptor *fd) feof fileDescriptor * fd fdEof fileDescriptorStruct::status
int int fgetc (fileDescriptor *fd) fgetc fileDescriptor * fd vfs_mountPoint::fs fileDescriptorStruct::mp fileDescriptorStruct::offset fileSystem::vfsRead sysFgetc
fileDescriptor * fileDescriptor* fopen (const char *file, const char *flags) fopen const char * file const char * flags fileDescriptorStruct::buffer fdOpen fdTable fdTable_lock fileAppend fileBinary fileDescriptorStruct::fileName fileRead fileWrite vfs_mountPoint::fs kfree kmalloc kprintf fileDescriptorStruct::mode fileDescriptorStruct::mp fileDescriptorStruct::next NULL fileDescriptorStruct::offset vitalsStruct::openFiles fileDescriptorStruct::prev spinLock spinUnlock sprintf fileDescriptorStruct::status strcpy strstr strtok systemVitals vfs_findMount fileSystem::vfsOpenFile elf_loadfile execFile kmod_load ldEnable sys_exec sys_open sysExec sysFopen sysMkDir
int int fputc (int ch, fileDescriptor *fd) fputc int ch fileDescriptor * fd vfs_mountPoint::fs fileDescriptorStruct::mp fileDescriptorStruct::offset fileSystem::vfsWrite
size_t size_t fread (void *ptr, size_t size, size_t nmemb, fileDescriptor *fd) fread void * ptr size_t size size_t nmemb fileDescriptor * fd assert vfs_mountPoint::fs fileDescriptorStruct::mp fileDescriptorStruct::offset fileSystem::vfsRead elf_loadfile execFile kmod_load ldEnable read sys_exec sysExec sysFread
int int fseek (fileDescriptor *tmpFd, long offset, int whence) fseek fileDescriptor * tmpFd long offset int whence fileDescriptorStruct::offset elf_loadfile execFile kmod_load ldEnable sys_exec sysExec
size_t size_t fwrite (void *ptr, int size, int nmemb, fileDescriptor *fd) fwrite void * ptr int size int nmemb fileDescriptor * fd vfs_mountPoint::fs fileDescriptorStruct::mp fileDescriptorStruct::offset fileSystem::vfsWrite sysFwrite
void void sysChDir (const char *path) sysChDir const char * path _current osInfo::cwd taskStruct::oInfo sprintf strstr
void void sysFclose (userFileDescriptor *userFd, int *status) sysFclose userFileDescriptor * userFd int * status fclose userFileDescriptorStruct::fd NULL
void void sysFgetc (int *ptr, userFileDescriptor *userFd) sysFgetc int * ptr userFileDescriptor * userFd _current userFileDescriptorStruct::fd fgetc getch sched_yield taskStruct::term tty_foreground
void void sysFopen (const char *file, char *flags, userFileDescriptor *userFd) sysFopen const char * file char * flags userFileDescriptor * userFd userFileDescriptorStruct::fd userFileDescriptorStruct::fdSize fopen kprintf NULL fileDescriptorStruct::size
void void sysFread (void *data, long size, userFileDescriptor *userFd) sysFread void * data long size userFileDescriptor * userFd userFileDescriptorStruct::fd fread NULL
void void sysFseek (userFileDescriptor *userFd, long offset, int whence) sysFseek userFileDescriptor * userFd long offset int whence userFileDescriptorStruct::fd NULL fileDescriptorStruct::offset
void void sysFwrite (char *ptr, int size, userFileDescriptor *userFd) sysFwrite char * ptr int size userFileDescriptor * userFd _current userFileDescriptorStruct::fd fwrite taskStruct::term tty_print
void void sysMkDir (const char *path) sysMkDir const char * path _current osInfo::cwd fclose fopen vfs_mountPoint::fs kprintf fileDescriptorStruct::mp NULL taskStruct::oInfo sprintf strstr strtok fileSystem::vfsMakeDir
void void sysRmDir () sysRmDir
void void sysUnlink (const char *path, int *retVal) sysUnlink const char * path int * retVal unlink
int int unlink (const char *node) unlink const char * node vfs_mountPoint::fs vfs_mountPoint::mountPoint NULL strtok vfs_findMount fileSystem::vfsUnlink sysUnlink
fileDescriptor * fileDescriptor* fdTable fdTable 0x0 fclose fopen
spinLock_t spinLock_t fdTable_lock fdTable_lock SPIN_LOCK_INITIALIZER fclose fopen

    /*****************************************************************************************
    Copyright(c)2002TheUbixOSProject
    Allrightsreserved.

    Redistributionanduseinsourceandbinaryforms,withorwithoutmodification,are
    permittedprovidedthatthefollowingconditionsaremet:

    Redistributionsofsourcecodemustretaintheabovecopyrightnotice,thislistof
    conditions,thefollowingdisclaimerandthelistofauthors.Redistributionsinbinary
    formmustreproducetheabovecopyrightnotice,thislistofconditions,thefollowing
    disclaimerandthelistofauthorsinthedocumentationand/orothermaterialsprovided
    withthedistribution.NeitherthenameoftheUbixOSProjectnorthenamesofits
    contributorsmaybeusedtoendorseorpromoteproductsderivedfromthissoftware
    withoutspecificpriorwrittenpermission.

    THISSOFTWAREISPROVIDEDBYTHECOPYRIGHTHOLDERSANDCONTRIBUTORS"ASIS"ANDANY
    EXPRESSORIMPLIEDWARRANTIES,INCLUDING,BUTNOTLIMITEDTO,THEIMPLIEDWARRANTIESOF
    MERCHANTABILITYANDFITNESSFORAPARTICULARPURPOSEAREDISCLAIMED.INNOEVENTSHALL
    THECOPYRIGHTOWNERORCONTRIBUTORSBELIABLEFORANYDIRECT,INDIRECT,INCIDENTAL,
    SPECIAL,EXEMPLARY,ORCONSEQUENTIALDAMAGES(INCLUDING,BUTNOTLIMITEDTO,PROCUREMENT
    OFSUBSTITUTEGOODSORSERVICES;LOSSOFUSE,DATA,ORPROFITS;ORBUSINESSINTERRUPTION)
    HOWEVERCAUSEDANDONANYTHEORYOFLIABILITY,WHETHERINCONTRACT,STRICTLIABILITY,OR
    TORT(INCLUDINGNEGLIGENCEOROTHERWISE)ARISINGINANYWAYOUTOFTHEUSEOFTHIS
    SOFTWARE,EVENIFADVISEDOFTHEPOSSIBILITYOFSUCHDAMAGE.

    $Id: file_8c.xml 88 2016-01-12 00:11:29Z reddawg $

    *****************************************************************************************/

    #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>

    staticspinLock_tfdTable_lock=SPIN_LOCK_INITIALIZER;

    fileDescriptor*fdTable=0x0;

    /*USER*/

    voidsysFwrite(char*ptr,intsize,userFileDescriptor*userFd){
    if(userFd==0x0){
    tty_print(ptr,_current->term);
    }
    else{
    fwrite(ptr,size,1,userFd->fd);
    }
    return;
    }

    voidsysFgetc(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("WakingTask:%i\n",tty_foreground->owner);
    sched_setStatus(tty_foreground->owner,READY);
    kprintf("SleepingTask:%i\n",_current->id);
    sched_setStatus(_current->id,WAIT);
    sched_yield();
    }
    */
    }
    }
    else{
    ptr[0]=(int)fgetc(tmpFd);
    }
    }

    voidsysRmDir(){
    return;
    }

    voidsysFseek(userFileDescriptor*userFd,longoffset,intwhence){
    //TODO:coredump?
    if(userFd==NULL)
    return;
    if(userFd->fd==NULL)
    return;

    userFd->fd->offset=offset+whence;
    }

    voidsysChDir(constchar*path){
    if(strstr(path,":")==0x0){
    sprintf(_current->oInfo.cwd,"%s%s",_current->oInfo.cwd,path);
    }
    else{
    sprintf(_current->oInfo.cwd,path);
    }
    }

    voidsysUnlink(constchar*path,int*retVal){
    *retVal=unlink(path);
    }

    /************************************************************************

    Function:voidsysFopen();
    Description:OpensAFileDescriptorForAUserTask
    Notes:

    ************************************************************************/
    voidsysFopen(constchar*file,char*flags,userFileDescriptor*userFd){
    if(userFd==NULL)
    kprintf("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:voidsysFread();
    Description:ReadsSIZEBytesFromTheuserFdIntoDATA
    Notes:

    ************************************************************************/
    voidsysFread(void*data,longsize,userFileDescriptor*userFd){
    /*TODO:coredump?*/
    if(userFd==NULL)
    return;
    if(userFd->fd==NULL)
    return;
    fread(data,size,1,userFd->fd);
    return;
    }

    /************************************************************************

    Function:voidsysFclse();
    Description:ClosesAFileDescriptorForAUserTask
    Notes:

    ************************************************************************/
    voidsysFclose(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_tfread(void*ptr,size_tsize,size_tnmemb,fileDescriptor*fd){
    size_ti=0x0;

    if(fd==0x0)
    return(0x0);

    if(nmemb==0x0)
    nmemb=1;//TempFix

    assert(fd);
    assert(fd->mp);
    assert(fd->mp->fs);

    i=fd->mp->fs->vfsRead(fd,ptr,fd->offset,size*nmemb);

    fd->offset+=size*nmemb;

    //kprintf("fread:%i:%i",i,size*nmemb);

    return(i);
    }

    size_tfwrite(void*ptr,intsize,intnmemb,fileDescriptor*fd){
    if(fd!=0x0){
    fd->mp->fs->vfsWrite(fd,ptr,fd->offset,size*nmemb);
    fd->offset+=size*nmemb;
    }
    return(0x0);
    }

    intfseek(fileDescriptor*tmpFd,longoffset,intwhence){
    tmpFd->offset=offset+whence;
    return(tmpFd->offset);
    }

    /************************************************************************

    Function:intfeof(fileDescriptor*fd)
    Description:CheckAFileDescriptorForEOFAndReturnResult
    Notes:

    ************************************************************************/
    intfeof(fileDescriptor*fd){
    if(fd->status==fdEof){
    return(-1);
    }
    return(0);
    }

    /************************************************************************

    Function:intfputc(intch,fileDescriptor*fd)
    Description:ThisWillWriteCharacterToFD
    Notes:

    ************************************************************************/
    intfputc(intch,fileDescriptor*fd){
    if(fd!=0x0){
    ch=fd->mp->fs->vfsWrite(fd,(char*)ch,fd->offset,1);
    fd->offset++;
    return(ch);
    }
    /*ReturnNULLIfFDIsNotFound*/
    return(0x0);
    }

    /************************************************************************

    Function:intfgetc(fileDescriptor*fd)
    Description:ThisWillReturnTheNextCharacterInAFDStream
    Notes:

    ************************************************************************/
    intfgetc(fileDescriptor*fd){
    intch=0x0;
    /*IfFoundReturnNextChar*/
    if(fd!=0x0){
    fd->mp->fs->vfsRead(fd,(char*)&ch,fd->offset,1);
    fd->offset++;
    return(ch);
    }

    /*ReturnNULLIfFDIsNotFound*/
    return(0x0);
    }

    /************************************************************************

    Function:fileDescriptor*fopen(constchar*file,contchar*flags)
    Description:ThisWillOpenAFileAndReturnAFileDescriptor
    Notes:

    08/05/02-JustStartedARewriteOfThisFunctionShouldWorkOutWell

    ************************************************************************/

    fileDescriptor*fopen(constchar*file,constchar*flags){
    inti=0x0;
    char*path=0x0;
    char*mountPoint=0x0;
    charfileName[1024];
    fileDescriptor*tmpFd=0x0;

    /*AllocateMemoryForFileDescriptor*/
    if((tmpFd=(fileDescriptor*)kmalloc(sizeof(fileDescriptor)))==0x0){
    kprintf("Error:tmpFd==NULL,File:%s,Line:%i\n",__FILE__,__LINE__);
    return(NULL);
    }

    strcpy(fileName,file);

    if(strstr(fileName,":")){
    mountPoint=(char*)strtok((char*)&fileName,":");
    path=strtok(NULL,"\n");
    }
    else{
    path=fileName;
    }

    if(path[0]=='/')
    strcpy(tmpFd->fileName,path);
    else
    sprintf(tmpFd->fileName,"/%s",path);

    /*Findourmountpointorsetdefaulttosys*/
    if(mountPoint==0x0)
    tmpFd->mp=vfs_findMount("sys");
    else
    tmpFd->mp=vfs_findMount(mountPoint);

    if(tmpFd->mp==0x0){
    kprintf("MountPointBad\n");
    return(0x0);
    }

    /*ThisWillSetUpTheDescriptorModes*/
    tmpFd->mode=0;
    for(i=0;'\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:
    kprintf("Invalidmode'%c'forfopen\n",flags[i]);
    break;
    }
    }
    /*SearchForTheFile*/
    if(tmpFd->mp->fs->vfsOpenFile(tmpFd->fileName,tmpFd)==0x1){
    /*IfTheFileIsFoundThenSetUpTheDescriptor*/

    /*inordertosaveresourceswewillallocatethebufferlaterwhenitisneeded*/

    tmpFd->buffer=(char*)kmalloc(4096);
    if(tmpFd->buffer==0x0)
    {
    kfree(tmpFd);
    kprintf("Error:tmpFd->buffer==NULL,File:%s,Line:%i\n",__FILE__,__LINE__);
    spinUnlock(&fdTable_lock);
    return0x1;
    }
    /*SetItsStatusToOpen*/
    tmpFd->status=fdOpen;

    /*InitialFileOffsetIsZero*/
    tmpFd->offset=0;
    tmpFd->prev=0x0;

    /*wedonotwanttobeinaspinlocklongerthanweneedto,so
    ithasbeenmovedtohere.*/
    spinLock(&fdTable_lock);

    /*IncrementNumberOfOpenFiles*/
    systemVitals->openFiles++;

    tmpFd->next=fdTable;

    if(fdTable!=0x0)
    fdTable->prev=tmpFd;

    fdTable=tmpFd;

    spinUnlock(&fdTable_lock);

    /*ReturnTheFD*/
    return(tmpFd);
    }
    else{
    kfree(tmpFd->buffer);
    kfree(tmpFd);
    spinUnlock(&fdTable_lock);
    kprintf("FileNotFound?%s\n",file);
    return(NULL);
    }

    /*ReturnNULL*/
    return(0x0);
    }

    /************************************************************************

    Function:intfclose(fileDescriptor*fd);
    Description:ThisWillCloseAndFreeAFileDescriptor
    Notes:

    ************************************************************************/
    intfclose(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;

    systemVitals->openFiles--;
    spinUnlock(&fdTable_lock);
    if(tmpFd->buffer!=NULL)
    kfree(tmpFd->buffer);
    kfree(tmpFd);
    return(0x0);
    }
    }

    spinUnlock(&fdTable_lock);
    return(0x1);
    }

    /*UBU*/

    /************************************************************************

    Function:voidsysMkDir(constchar*path)
    Description:ThisWillCreateANewDirectory
    Notes:

    ************************************************************************/
    voidsysMkDir(constchar*path){
    fileDescriptor*tmpFD=0x0;
    chartmpDir[1024];
    charrootPath[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("InvalidMountPoint\n");
    }
    tmpFD->mp->fs->vfsMakeDir(dir,tmpFD);

    fclose(tmpFD);

    return;
    }

    /************************************************************************

    Function:intunlink(constchar*node)
    Description:Thiswillunlinkafile
    Notes:

    ************************************************************************/

    intunlink(constchar*node){
    char*path=0x0,*mountPoint=0x0;
    structvfs_mountPoint*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("MountPointBad");
    return(0x0);
    }
    mp->fs->vfsUnlink(path,mp);
    return(0x0);
    }

    /***
    END
    ***/