Newer
Older
ubixos / src / sys / ubixfs / dirCache.c
@reddawg reddawg on 22 Jul 2004 10 KB Ok checking in before I sleep
/*****************************************************************************************
 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 <assert.h>
#include <ubixfs/dirCache.h>
#include <lib/kmalloc.h>
#include <lib/kprintf.h>
#include <lib/string.h>

static
struct directoryEntry *
ubixfs_findName(struct directoryEntry * dirList, char * name) {
  int i;
  if (dirList == NULL || name == NULL) return NULL;
  for (i = 0; dirList[i].fileName != NULL; i++) {
    if (strcmp(dirList[i].fileName, name) == 0) return &dirList[i];
  } /* for */
  return NULL;
} /* ubixfs_findName */

struct cacheNode *
ubixfs_cacheFind(struct cacheNode * head, char * name) {
  struct cacheNode * tmp = head;
  struct directoryEntry * dirList = NULL;
  unsigned int i = 0x0;
  char dirName[256];
  char * nextDir = NULL;
//kprintf("looking for %s\n", name);
assert(name);
assert(head);
assert(*name);
  if (name == NULL || head == NULL) return NULL;
  if (*name == '\0') return NULL;

  /* 
   * walk down the tree recursively until we find the node we're looking
   * for
   */
  i = 0;
  while (name[i] != '\0' && name[i] != '/' && i < sizeof(dirName)) {
    dirName[i] = name[i];
    i++;
  } /* while */
  assert(i < sizeof(dirName));
  if (i == sizeof(dirName)) return NULL;

  if (i == 0) dirName[i++] = '/';

  dirName[i] = '\0';
  
  nextDir = &name[i];
  if (*nextDir == '/') nextDir++;

  /* 
   * nextDir points to the next dir
   * name points to a null terminated directory name
   * if nextDir isn't null, then make sure that this dir is present
   */

  if (*nextDir != '\0') {
    while (tmp != NULL) {
      if (strcmp(tmp->name, dirName) == 0) {
        if ((tmp->attributes & typeFile) == typeFile 
            || tmp->fileListHead == NULL) {

          /* if we're here, then there are no subdirs cached to look through */
          dirList = ubixfs_findName((struct directoryEntry *)head->info,nextDir);
          if (dirList == NULL) return NULL;
kprintf("creating new node %s", dirList->fileName);
          tmp = ubixfs_cacheAdd(tmp, ubixfs_cacheNew(dirList->fileName));
          tmp->attributes = dirList->attributes;
          tmp->permissions = dirList->permissions;
          tmp->size = dirList->size;
kprintf("   size: %d\n", tmp->size);
          tmp->startCluster = dirList->startCluster;
          tmp->present = 0;
          return tmp;
        } else return ubixfs_cacheFind(tmp->fileListHead, nextDir);
      } /* if */
      tmp = tmp->next;
    } /* while */
    return NULL;
  } /* if */ 

  /*
   * if nextDir was null, then we're at the bottom level. Look for the
   * dir listing here 
   */
  while (tmp != NULL) {
    assert(tmp->name);
/* don't forget to check to see if it's a directory */
    if (strcmp(tmp->name, name) == 0) {
      /* 
       * we found the node. Move it to the front of the list 
       * (if it isn't already)
       */
      assert(tmp->parent);
      if (tmp != tmp->parent->fileListHead) {

        /* if we're the tail, point the tail to our prev */
        if (tmp == tmp->parent->fileListTail) {
          tmp->parent->fileListTail = tmp->prev;
        } /* if */

        if (tmp->next != NULL) tmp->next->prev = tmp->prev;
        if (tmp->prev != NULL) tmp->prev->next = tmp->next;
        tmp->next = tmp->parent->fileListHead;
        tmp->prev = NULL;
        tmp->parent->fileListHead = tmp;
      } /* if */
      return tmp;
    } /* if */
    tmp = tmp->next;
  } /* while */
    /* if we're here, then one level of the dir isn't cached */

    tmp = head->parent;
    assert(tmp->info);
    dirList = ubixfs_findName((struct directoryEntry *)tmp->info, name);
    if (dirList == NULL) return NULL;
kprintf("creating new node %s", dirList->fileName);
    tmp = ubixfs_cacheAdd(tmp, ubixfs_cacheNew(dirList->fileName));
    tmp->attributes = dirList->attributes;
    tmp->permissions = dirList->permissions;
    tmp->size = dirList->size;
kprintf("   size: %d\n", tmp->size);
    tmp->startCluster = dirList->startCluster;
    tmp->present = 0;
    return tmp;
#if 0
  return NULL;  /* couldn't find it */
#endif
} /* ubixfs_cacheFind */

struct cacheNode * 
ubixfs_cacheNew(const char * name) {
  struct cacheNode * tmp = kmalloc(sizeof(struct cacheNode));
  assert(tmp);
  tmp->parent = tmp;
  tmp->prev = tmp->next = tmp->fileListHead = tmp->fileListTail = NULL;
  tmp->info = NULL;
  tmp->size = tmp->present = tmp->dirty = tmp->startCluster = 0;
  tmp->attributes = tmp->permissions = 0;
  tmp->name = (char *)kmalloc(strlen(name));
  strcpy(tmp->name, name);
  return tmp;
} /* ubixfs_cacheNew */

void
ubixfs_cacheDelete(struct cacheNode ** head) {
  struct cacheNode * tmp = NULL;
  struct cacheNode * del = NULL;

  if (head == NULL) return;
  if (*head == NULL) return;

  tmp = *head;
  while (tmp != NULL) {
    /* if there are any child nodes, delete them first */

    /* 
     * the following commented out ``if'' statement is redundant, since it 
     * will be caught with the above checks
     */
    /* if (tmp->fileListHead != NULL) */
    ubixfs_cacheDelete(&tmp->fileListHead);

    kfree(tmp->info);
    kfree(tmp->name);
    del = tmp;
    tmp = tmp->next;
    kfree(del);
  } /* while */
  *head = NULL;
  return;
} /* deleteNode */
#if 0
void
addNode(struct cacheNode ** node, struct cacheNode * newNode) {
  if (node == NULL) return;
  newNode->next = *node;
  if (*node != NULL) (*node)->prev = newNode;
  newNode->prev = NULL;
  *node = newNode;
  return;
} /* addNode */
#endif

struct cacheNode *
ubixfs_cacheAdd(struct cacheNode *node, struct cacheNode * newNode) {
  struct cacheNode * tmp;

  assert(node);
  newNode->parent = node;
  newNode->next = node->fileListHead;
  newNode->prev = NULL;
  if (node->fileListHead == NULL) 
    node->fileListTail = newNode;
  else
    node->fileListHead->prev = newNode;

  node->fileListHead = newNode;
  tmp = node->fileListHead;
  return tmp;
} /* ubixfs_cacheAdd */

/***
 $Log$
 Revision 1.18  2004/07/22 19:54:50  flameshadow
 chg: works now. Thanx ubu

 Revision 1.17  2004/07/22 19:01:59  flameshadow
 chg: more directory and file caching

 Revision 1.16  2004/07/22 16:34:32  flameshadow
 add: file and dir caching kinda work

 Revision 1.15  2004/07/21 22:43:18  flameshadow
 one more try

 Revision 1.14  2004/07/21 22:42:25  flameshadow
 try it now

 Revision 1.13  2004/07/21 22:40:27  flameshadow
 weird

 Revision 1.12  2004/07/21 22:18:37  flameshadow
 chg: renamed subDirsHead/Tail members of cacheNode to fileListHead/Tail

 Revision 1.11  2004/07/21 22:12:22  flameshadow
 add: attributes tag in cacheNode
 add: setting of attributes in ubixfx_cacheNew() and ubixfs_cacheFind()

 Revision 1.10  2004/07/21 22:07:18  flameshadow
 chg: renamed caching functions (again)

 Revision 1.9  2004/07/21 22:00:04  flameshadow
 chg: ubixfws_dirCacheAdd now returns a pointer to the node it added
 chg: minor fix in ubixfs_dirCacheFind()

 Revision 1.6  2004/07/21 14:43:14  flameshadow
 add: added cwc (current working container) to the osInfo strut

 Revision 1.5  2004/07/20 19:21:30  reddawg
 You like leaving out $Log$
 You like leaving out Revision 1.18  2004/07/22 19:54:50  flameshadow
 You like leaving out chg: works now. Thanx ubu
 You like leaving out
 You like leaving out Revision 1.17  2004/07/22 19:01:59  flameshadow
 You like leaving out chg: more directory and file caching
 You like leaving out
 You like leaving out Revision 1.16  2004/07/22 16:34:32  flameshadow
 You like leaving out add: file and dir caching kinda work
 You like leaving out
 You like leaving out Revision 1.15  2004/07/21 22:43:18  flameshadow
 You like leaving out one more try
 You like leaving out
 You like leaving out Revision 1.14  2004/07/21 22:42:25  flameshadow
 You like leaving out try it now
 You like leaving out
 You like leaving out Revision 1.13  2004/07/21 22:40:27  flameshadow
 You like leaving out weird
 You like leaving out
 You like leaving out Revision 1.12  2004/07/21 22:18:37  flameshadow
 You like leaving out chg: renamed subDirsHead/Tail members of cacheNode to fileListHead/Tail
 You like leaving out
 You like leaving out Revision 1.11  2004/07/21 22:12:22  flameshadow
 You like leaving out add: attributes tag in cacheNode
 You like leaving out add: setting of attributes in ubixfx_cacheNew() and ubixfs_cacheFind()
 You like leaving out
 You like leaving out Revision 1.10  2004/07/21 22:07:18  flameshadow
 You like leaving out chg: renamed caching functions (again)
 You like leaving out
 You like leaving out Revision 1.9  2004/07/21 22:00:04  flameshadow
 You like leaving out chg: ubixfws_dirCacheAdd now returns a pointer to the node it added
 You like leaving out chg: minor fix in ubixfs_dirCacheFind()
 You like leaving out
 You like leaving out Revision 1.6  2004/07/21 14:43:14  flameshadow
 You like leaving out add: added cwc (current working container) to the osInfo strut
 You like leaving out so i don't know what is going on eh?

 END
 ***/