Newer
Older
ubixos / src / sys / ubixfs / ubixfs.c
@reddawg reddawg on 3 Oct 2002 4 KB Making Headway
/**************************************************************************************
 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 <ubixfs/ubixfs.h>
#include <ubixfs/file.h>
#include <drivers/fdc.h>
#include <drivers/video.h>
#include <ubixos/types.h>
#include <ubixos/kmalloc.h>
#include <vmm/paging.h>

struct blockAllocationTableEntry *blockAllocationTable = 0x0;
struct ubixDiskLabel *diskLabel = 0x0;

void initUbixFS() {
  diskLabel = (struct ubixDiskLabel *)kmalloc(512);
  readBlock(1,(char *)diskLabel,(long)1);
  if ((diskLabel->magicNum == UBIXDISKMAGIC) && (diskLabel->magicNum2 == UBIXDISKMAGIC)) {
    blockAllocationTable = (struct blockAllocationTableEntry *)kmalloc(diskLabel->partitions[0].p_bsize * 512);
    readBlock(diskLabel->partitions[0].p_offset,(char *)blockAllocationTable,diskLabel->partitions[0].p_bsize);
    kprintf("UbixFS Initialized\n");
    }
  else {   
    kprintf("Insert System Disk\n");
    initUbixFS();
    }
  //Return
  return;
  }

int kstrcmp(char *str1, char *str2) {
  while (*str1 == *str2 && *str1!=0 && *str2!=0) {
    str1++;
    str2++;
    }
  if (*str1 == *str2)
    return(0);
  else if (*str1 > *str2)
    return(1);
  else
    return(-1);
  //if(*str1 < *str2) return -1;    
  }

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

Function: int getFileByte(fileDescriptor *fd,long offset);
Description: Return The Byte At Offset For File Descriptor
Notes:

************************************************************************/
int getFileByte(fileDescriptor *fd,long offset) {
  int ch = 0x0,blockCount = 0x0,batIndex = fd->start,i = 0x0;
  short bSect = 0x0;
  blockCount = (offset/4096);
  //Find Out Which File Block We Should Be In
  for (i=1;i<=blockCount;i++) {
    batIndex = blockAllocationTable[batIndex].nextBlock;
    }
  //Calculate Which Block Sector We Should Load
//  bSect = ((offset - ((i-1)*4096))/512);
//  kprintf("bSect: [%i],realSector: [%i]\n",bSect,blockAllocationTable[batIndex].realSector);
  //Fix FD Status So We Dont Load A Sector Again
  //  if ((offset%512 == 0) && (fd->status == fdRead)) {
    if ((offset%4096 == 0) && (fd->status == fdRead)) {
      fd->status = fdOpen;
      }
  //If The Offset Is Less Then The File Size Return The Char At It
  if (offset < fd->size) {
    //If FD Status Is Not fdRead Then Load A Sector From Disk
    if (fd->status != fdRead) {
      readBlock(diskLabel->partitions[0].p_offset+blockAllocationTable[batIndex].realSector+bSect,fd->buffer,8);
      fd->status = fdRead;
      }
    //This Sets The Char To Return
    ch = fd->buffer[offset-(blockCount*4096)];
    }
  else {
    //Set File EOF If There Is Nothing To Do
    ch = -1;
    fd->status = fdEof;
    }
  //Return
  return(ch);
  }
    
int findFile(char *file,fileDescriptor *fd) {
  int x=0;
  bool ret = TRUE;
  struct directoryEntry *dirEntry = (struct directoryEntry *)kmalloc(4096);
  ret = (bool)readBlock((diskLabel->partitions[0].p_offset+blockAllocationTable[0].realSector),(char *)dirEntry,8);
  for (x=0;x<(4096/sizeof(struct directoryEntry));x++) {
    if ((int)!kstrcmp(dirEntry[x].fileName,file)) {
      fd->start = dirEntry[x].startCluster;
      fd->size = dirEntry[x].size;
      kfree(dirEntry);
      return((int)1);
      }
    }
  kfree(dirEntry);
  return((int)0);
  }