Newer
Older
ubixos / src / sys / vfs / file.c
@reddawg reddawg on 18 Jun 2004 7 KB UbixOS PreRelease
/*****************************************************************************************
 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 <lib/kmalloc.h>
#include <lib/string.h>
#include <vmm/paging.h>
#include <lib/kprintf.h>

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

Function: int fclose(fileDescriptor *fd);
Description: This Will Close And Free A File Descriptor
Notes:

************************************************************************/
int fclose(fileDescriptor *fd) {
  fileDescriptor *tmpFd = 0x0;
  /* Search For File Descriptor */
  for (tmpFd=fdTable;tmpFd;tmpFd=tmpFd->next) {
    if (tmpFd == fd) {
      /* If Fd Is The First FD Then Reset fdTable */
      if (tmpFd == fdTable) {
        fdTable = tmpFd->next;
        if (fdTable != 0x0) {
          fdTable->prev = 0x0;
          }
        kfree(fd->buffer);
        kfree(fd);
        systemVitals->openFiles--;
        return(1);
        }
      else {
        tmpFd->prev->next = tmpFd->next;
        tmpFd->next->prev = tmpFd->prev;
        kfree(fd->buffer);
        kfree(fd);
        systemVitals->openFiles--;
        return(1);
        }
      }    
    }
  /* Return NULL If Descriptor Was Not Found */
  return(0x0);
  }

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

Function: void sysMkDir(const char *path)
Description: This Will Create A New Directory
Notes:

************************************************************************/
int sysMkDir(const char *path) {
  fileDescriptor *tmpFd  = 0x0;
  char *dir = 0x0,*mountPoint = 0x0;

  
  /* Allocate Memory For File Descriptor */
  tmpFd = (fileDescriptor *)kmalloc(sizeof(fileDescriptor));
  tmpFd->mode = fileRead;

  dir = (char *)strtok((char *)path,"@");
  mountPoint = strtok(NULL,"\n");

  if (mountPoint == 0x0) {
    tmpFd->mp = _current->oInfo.container;
    }
  else {
    tmpFd->mp = findMount(mountPoint);
    }
  if (tmpFd->mp == 0x0) {
    kpanic("Invalid Container\n");
    return(0x1);
    }
  if (tmpFd->mp->fs->vfsOpenFile(":",tmpFd) == 1) {
    tmpFd->buffer = (char *)kmalloc(4096);
    sprintf(tmpFd->fileName,"%s",":");
    /* Set Its Status To Open */
    tmpFd->status = fdOpen;
    /* Initial File Offset Is Zero */
    tmpFd->offset = 0x0;
    }
  else {
    return(0x0);
    }
  if (tmpFd->mp->fs->vfsMakeDir(dir,tmpFd) != 0x0) {
    kpanic("Error Creating Directory\n");
    }
  else {
    kpanic("Made Directory\n");
    }  
  kfree(tmpFd->buffer);
  kfree(tmpFd);
  return(0x0);
  }


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

Function: int unlink(const char *node)
Description: This will unlink a file
Notes:

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

int unlink(const char *node) {
  char *path = 0x0,*mountPoint = 0x0;
  struct mountPoints *mp = 0x0;

  path = (char *)strtok((char *)node,"@");
  mountPoint = strtok(NULL,"\n");
  if (mountPoint == 0x0) {
    mp = findMount("sys"); /* _current->oInfo.container; */
    }
  else {
    mp = findMount(mountPoint);
    }
  if (mp == 0x0) {
    //kpanic("Mount Point Bad");
    return(0x0);
    }
  mp->fs->vfsUnlink(path,mp);
  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 = 0;
  fileDescriptor *tmpFd  = 0x0;
  char *path = 0x0,*mountPoint = 0x0;
  /* Allocate Memory For File Descriptor */
  tmpFd = (fileDescriptor *)kmalloc(sizeof(fileDescriptor));

  path = (char *)strtok((char *)file,"@");
  mountPoint = strtok(NULL,"\n");
  if (mountPoint == 0x0) {
    tmpFd->mp = findMount("sys"); /* _current->oInfo.container; */
    }
  else {
    tmpFd->mp = findMount(mountPoint);
    }
  if (tmpFd->mp == 0x0) {
    //kpanic("Mount Point Bad");
    kprintf("Mount Point Bad\n");
    return(0x0);
    }
  /* This Will Set Up The Descriptor Modes */
  tmpFd->mode = 0x0;
  while ('\0' != flags[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;
      }
    i++;
    }
  /* Search For The File */
  if (tmpFd->mp->fs->vfsOpenFile((char *)file,tmpFd) == 1) {
    /* If The File Is Found Then Set Up The Descriptor */
    tmpFd->buffer = (char *)kmalloc(4096);
    sprintf(tmpFd->fileName,"%s",file);
    /* Set Its Status To Open */
    tmpFd->status = fdOpen;
    /* Initial File Offset Is Zero */
    tmpFd->offset = 0x0;
    /* Pointer To Next Descriptor Is NULL */
    tmpFd->prev = 0x0;
    /* If This Is The First File Descriptor Then Set It Up To Be Starting Fd */
    if (fdTable == 0x0) {
      fdTable     = tmpFd;
      tmpFd->next = 0x0;
      tmpFd->prev = 0x0;
      }
    else {
      /* If There Is Already A Starting FD Set This Up As The Last One */
      tmpFd->next   = fdTable;
      fdTable->prev = tmpFd;
      tmpFd->prev   = 0x0;
      fdTable       = tmpFd;
      }
    /* Increment Number Of Open Files */
    systemVitals->openFiles++;
    
    /* Return The FD */
    return(fdTable);
    }
  else {
    /* If The File Was Not Found Free The Memory */
    kfree(tmpFd);
    }
  /* Return NULL */
  return(0x0);
  }

/***
 $Log$
 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
 ***/