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


struct cacheNode *
ubixfs_dirCacheFind(struct cacheNode * head, char * name) {
  struct cacheNode * tmp = head;
  unsigned int i;
  char dirName[256];
  char * nextDir = NULL;

  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) 
        return ubixfs_dirCacheFind(tmp->subDirsHead, nextDir);
      tmp = tmp->next;
    } /* while */
    /* if we're here, then one level of the dir isn't cached */
    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)
       */
      if (tmp != tmp->parent->subDirsHead) {
        assert(tmp->parent);

        /* if we're the tail, point the tail to our prev */
        if (tmp == tmp->parent->subDirsTail) {
          tmp->parent->subDirsTail = 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->subDirsHead;
        tmp->prev = NULL;
        tmp->parent->subDirsHead = tmp;
      } /* if */
      return tmp;
    } /* if */
    tmp = tmp->next;
  } /* while */

  return NULL;  /* couldn't find it */
} /* ubixfs_dirCacheFind */

struct cacheNode * 
ubixfs_dirCacheNew(const char * name) {
  struct cacheNode * tmp = kmalloc(sizeof(struct cacheNode));
  assert(tmp);
  tmp->parent = tmp;
  tmp->prev = tmp->next = tmp->subDirsHead = tmp->subDirsTail = NULL;
  tmp->dirList = NULL;
  tmp->dirListEntryCount = tmp->dirty = tmp->dirBlock = 0;
  tmp->name = (char *)kmalloc(strlen(name));
  strcpy(tmp->name, name);
  return tmp;
} /* ubixfs_dirCacheNew */

void
ubixfs_dirCacheDelete(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->subDirsHead != NULL) */
    ubixfs_dirCacheDelete(&tmp->subDirsHead);

    kfree(tmp->dirList);
    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

void
ubixfs_dirCacheAdd(struct cacheNode *node, struct cacheNode * newNode) {
  struct cacheNode * tmp;

  assert(node);
  newNode->parent = node;
  newNode->next = node->subDirsHead;
  newNode->prev = NULL;
  if (node->subDirsHead == NULL) 
    node->subDirsTail = newNode;
  else
    node->subDirsHead->prev = newNode;

  node->subDirsHead = newNode;
  tmp = node->subDirsHead;
  return;
} /* ubixfs_dirCacheAdd */

/***
 $Log$
 END
 ***/
 

