UbixOS V2  2.0
dirCache.c
Go to the documentation of this file.
1 /*****************************************************************************************
2  Copyright (c) 2002, 2016 The UbixOS Project
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without modification, are
6  permitted provided that the following conditions are met:
7 
8  Redistributions of source code must retain the above copyright notice, this list of
9  conditions, the following disclaimer and the list of authors. Redistributions in binary
10  form must reproduce the above copyright notice, this list of conditions, the following
11  disclaimer and the list of authors in the documentation and/or other materials provided
12  with the distribution. Neither the name of the UbixOS Project nor the names of its
13  contributors may be used to endorse or promote products derived from this software
14  without specific prior written permission.
15 
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
17  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
19  THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21  OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
23  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26  $Id: dirCache.c 169 2016-01-20 01:10:22Z reddawg $
27 
28  *****************************************************************************************/
29 
30 #include <assert.h>
31 #include <ubixfs/dirCache.h>
32 #include <ubixfs/ubixfs.h>
33 #include <lib/kmalloc.h>
34 #include <lib/kprintf.h>
35 #include <string.h>
36 
37 #include <ubixos/spinlock.h>
38 
39 static struct spinLock dca_spinLock = SPIN_LOCK_INITIALIZER;
40 
41 static struct directoryEntry *
42 ubixfs_findName( struct directoryEntry * dirList, uInt32 size, char * name ) {
43  unsigned int i;
44 
45  if ( dirList == NULL || name == NULL )
46  return NULL;
47  //UBU kprintf("dirList: [0x%X],name: [0x%X]\n",dirList,name);
48  for ( i = 0; i < (size / sizeof(struct directoryEntry)); i++ ) {
49  if ( strcmp( dirList[i].fileName, name ) == 0 )
50  return &dirList[i];
51  } /* for */
52  return NULL;
53 } /* ubixfs_findName */
54 
55 struct cacheNode *
56 ubixfs_cacheFind( struct cacheNode * head, char * name ) {
57  struct cacheNode * tmp = head;
58  struct directoryEntry * dirList = NULL;
59  unsigned int i = 0x0;
60  char dirName[256];
61  char * nextDir = NULL;
62  /* kprintf("looking for %s\n", name); */
63  assert( name );
64  assert( head );
65  assert( *name );
66  spinLock( &dca_spinLock );
67  spinUnlock( &dca_spinLock );
68  if ( name == NULL || head == NULL )
69  return NULL;
70  if ( *name == '\0' )
71  return NULL;
72 
73  /*
74  * walk down the tree recursively until we find the node we're looking
75  * for
76  */
77  i = 0;
78  while ( name[i] != '\0' && name[i] != '/' && i < sizeof(dirName) ) {
79  dirName[i] = name[i];
80  i++;
81  } /* while */
82  assert( i < sizeof(dirName) );
83  if ( i == sizeof(dirName) )
84  return NULL;
85 
86  if ( i == 0 )
87  dirName[i++] = '/';
88 
89  dirName[i] = '\0';
90 
91  nextDir = &name[i];
92  if ( *nextDir == '/' )
93  nextDir++;
94 
95  /*
96  * nextDir points to the next dir
97  * name points to a null terminated directory name
98  * if nextDir isn't null, then make sure that this dir is present
99  */
100  /* kprintf("nextdir: %s -- dirName: %s\n", nextDir, dirName); */
101  if ( *nextDir != '\0' ) {
102  while ( tmp != NULL ) {
103  //UBU kprintf("tmp->name: [0x%X],dirName: [0x%X]\n",tmp->name,dirName);
104  if ( strcmp( tmp->name, dirName ) == 0 ) {
105 
106  if ( (*tmp->attributes & typeFile) == typeFile
107  || tmp->fileListHead == NULL ) {
108 
109  /* if we're here, then there are no subdirs cached to look through */
110  dirList = ubixfs_findName( (struct directoryEntry *) head->info,
111  *head->size, nextDir );
112  if ( dirList == NULL )
113  return NULL;
114  /* kprintf("creating new node %s", dirList->fileName); */
115  tmp = ubixfs_cacheAdd( tmp, ubixfs_cacheNew( dirList->fileName ) );
116  tmp->attributes = &dirList->attributes;
117  tmp->permissions = &dirList->permissions;
118  tmp->size = (int *) &dirList->size;
119  /* kprintf(" size: %d\n", *tmp->size); */
120  tmp->startCluster = &dirList->startCluster;
121  tmp->present = 0;
122  return tmp;
123  }
124  else {
125  return ubixfs_cacheFind( tmp->fileListHead, nextDir );
126  }
127  } /* if */
128  tmp = tmp->next;
129  } /* while */
130  /* it wasn't present, return NULL */
131  return NULL;
132  } /* if */
133 
134  /*
135  * if nextDir was null, then we're at the bottom level. Look for the
136  * dir listing here
137  */
138  while ( tmp != NULL ) {
139 
140  assert( tmp->name );
141  assert( name );
142  /* don't forget to check to see if it's a directory */
143  //UBU kprintf("tmpName: [0x%X], name: [0x%X]\n",tmp->name,name);
144  if ( strcmp( tmp->name, name ) == 0 ) {
145 
146  /*
147  * we found the node. Move it to the front of the list
148  * (if it isn't already)
149  */
150 #if 0
151  assert(tmp->parent);
152  if (tmp != tmp->parent->fileListHead) {
153 
154  /* if we're the tail, point the tail to our prev */
155  if (tmp == tmp->parent->fileListTail) {
156  tmp->parent->fileListTail = tmp->prev;
157  } /* if */
158 
159  if (tmp->next != NULL) tmp->next->prev = tmp->prev;
160  if (tmp->prev != NULL) tmp->prev->next = tmp->next;
161  tmp->next = tmp->parent->fileListHead;
162  tmp->prev = NULL;
163  tmp->parent->fileListHead = tmp;
164  } /* if */
165 #endif
166  return tmp;
167  } /* if */
168  tmp = tmp->next;
169  } /* while */
170  /* if we're here, then one level of the dir isn't cached */
171 
172  tmp = head->parent;
173  assert( tmp );
174  assert( tmp->info );
175  dirList = ubixfs_findName( (struct directoryEntry *) tmp->info,
176  *tmp->size, name );
177  if ( dirList == NULL )
178  return NULL;
179  /* kprintf("creating new node/size %s/%d", dirList->fileName, dirList->size);*/
180  tmp = ubixfs_cacheAdd( tmp, ubixfs_cacheNew( dirList->fileName ) );
181  tmp->attributes = &dirList->attributes;
182  tmp->permissions = &dirList->permissions;
183  tmp->size = (int *) &dirList->size;
184  /* kprintf(" size: %d\n", *tmp->size); */
185  tmp->startCluster = &dirList->startCluster;
186  tmp->present = 0;
187  return tmp;
188 #if 0
189  return NULL; /* couldn't find it */
190 #endif
191 } /* ubixfs_cacheFind */
192 
193 struct cacheNode *
194 ubixfs_cacheNew( const char * name ) {
195  struct cacheNode * tmp = kmalloc( sizeof(struct cacheNode) );
196  assert( tmp );
197  tmp->parent = tmp;
198  tmp->prev = tmp->next = tmp->fileListHead = tmp->fileListTail = NULL;
199  tmp->info = NULL;
200  tmp->size = NULL;
201  tmp->present = tmp->dirty = 0;
202  tmp->startCluster = NULL;
203  tmp->attributes = NULL;
204  tmp->permissions = NULL;
205  tmp->name = (char *) kmalloc( strlen( name ) + 1 );
206  strcpy( tmp->name, name );
207  return tmp;
208 } /* ubixfs_cacheNew */
209 
210 void
211 ubixfs_cacheDelete( struct cacheNode ** head ) {
212  struct cacheNode * tmp = NULL;
213  struct cacheNode * del = NULL;
214 
215  if ( head == NULL )
216  return;
217  if ( *head == NULL )
218  return;
219 
220  tmp = *head;
221  while ( tmp != NULL ) {
222  /* if there are any child nodes, delete them first */
223 
224  /*
225  * the following commented out ``if'' statement is redundant, since it
226  * will be caught with the above checks
227  */
228  /* if (tmp->fileListHead != NULL) */
230 
231  kfree( tmp->info );
232  kfree( tmp->name );
233  del = tmp;
234  tmp = tmp->next;
235  kfree( del );
236  } /* while */
237  *head = NULL;
238  return;
239 } /* deleteNode */
240 #if 0
241 void
242 addNode(struct cacheNode ** node, struct cacheNode * newNode) {
243  if (node == NULL) return;
244  newNode->next = *node;
245  if (*node != NULL) (*node)->prev = newNode;
246  newNode->prev = NULL;
247  *node = newNode;
248  return;
249 } /* addNode */
250 #endif
251 
252 struct cacheNode *
253 ubixfs_cacheAdd( struct cacheNode *node, struct cacheNode * newNode ) {
254  struct cacheNode * tmp;
255 
256  assert( node );
257  spinLock( &dca_spinLock );
258  newNode->parent = node;
259  newNode->next = node->fileListHead;
260  newNode->prev = NULL;
261  if ( node->fileListHead == NULL )
262  node->fileListTail = newNode;
263  else
264  node->fileListHead->prev = newNode;
265 
266  node->fileListHead = newNode;
267  tmp = node->fileListHead;
268  spinUnlock( &dca_spinLock );
269  return tmp;
270 } /* ubixfs_cacheAdd */
271 
272 /***
273  $Log: dirCache.c,v $
274  Revision 1.1.1.1 2006/06/01 12:46:17 reddawg
275  ubix2
276 
277  Revision 1.2 2005/10/12 00:13:37 reddawg
278  Removed
279 
280  Revision 1.1.1.1 2005/09/26 17:24:40 reddawg
281  no message
282 
283  Revision 1.30 2004/08/14 11:23:02 reddawg
284  Changes
285 
286  Revision 1.29 2004/08/09 12:58:05 reddawg
287  let me know when you got the surce
288 
289  Revision 1.28 2004/08/01 17:58:39 flameshadow
290  chg: fixed string allocation bug in ubixfs_cacheNew()
291 
292  Revision 1.27 2004/07/28 17:24:13 flameshadow
293  chg: no comment
294 
295  Revision 1.26 2004/07/28 17:07:29 flameshadow
296  chg: re-added moving cached nodes to the front of the list when found
297  add: added an assert() in ubixfs.c
298 
299  Revision 1.25 2004/07/27 19:24:31 flameshadow
300  chg: reduced the number of debugging statements in the kernel.
301 
302  Revision 1.24 2004/07/27 12:02:01 reddawg
303  chg: fixed marks bug readFile did a lookup which is why it looked like it was loopping so much
304 
305  Revision 1.23 2004/07/27 09:05:43 flameshadow
306  chg: fixed file not found bug. Still can't find looping issue
307 
308  Revision 1.22 2004/07/27 04:05:20 flameshadow
309  chg: kinda fixed it. Added bunches of debug info
310 
311  Revision 1.21 2004/07/25 22:21:52 flameshadow
312  chg: re-enabled kprintf() in ubixfs_cacheFind()
313 
314  Revision 1.20 2004/07/24 17:19:24 flameshadow
315  chg: Temporarily disabled the moving of the found cache node to the front
316  of the list. It seems to cause problems later (race condition, possibly)
317 
318  Revision 1.19 2004/07/22 23:01:51 reddawg
319  Ok checking in before I sleep
320 
321  Revision 1.18 2004/07/22 19:54:50 flameshadow
322  chg: works now. Thanx ubu
323 
324  Revision 1.17 2004/07/22 19:01:59 flameshadow
325  chg: more directory and file caching
326 
327  Revision 1.16 2004/07/22 16:34:32 flameshadow
328  add: file and dir caching kinda work
329 
330  Revision 1.15 2004/07/21 22:43:18 flameshadow
331  one more try
332 
333  Revision 1.14 2004/07/21 22:42:25 flameshadow
334  try it now
335 
336  Revision 1.13 2004/07/21 22:40:27 flameshadow
337  weird
338 
339  Revision 1.12 2004/07/21 22:18:37 flameshadow
340  chg: renamed subDirsHead/Tail members of cacheNode to fileListHead/Tail
341 
342  Revision 1.11 2004/07/21 22:12:22 flameshadow
343  add: attributes tag in cacheNode
344  add: setting of attributes in ubixfx_cacheNew() and ubixfs_cacheFind()
345 
346  Revision 1.10 2004/07/21 22:07:18 flameshadow
347  chg: renamed caching functions (again)
348 
349  Revision 1.9 2004/07/21 22:00:04 flameshadow
350  chg: ubixfws_dirCacheAdd now returns a pointer to the node it added
351  chg: minor fix in ubixfs_dirCacheFind()
352 
353  Revision 1.6 2004/07/21 14:43:14 flameshadow
354  add: added cwc (current working container) to the osInfo strut
355 
356  Revision 1.5 2004/07/20 19:21:30 reddawg
357  You like leaving out $Log: dirCache.c,v $
358  You like leaving out Revision 1.1.1.1 2006/06/01 12:46:17 reddawg
359  You like leaving out ubix2
360  You like leaving out
361  You like leaving out Revision 1.2 2005/10/12 00:13:37 reddawg
362  You like leaving out Removed
363  You like leaving out
364  You like leaving out Revision 1.1.1.1 2005/09/26 17:24:40 reddawg
365  You like leaving out no message
366  You like leaving out
367  You like leaving out Revision 1.30 2004/08/14 11:23:02 reddawg
368  You like leaving out Changes
369  You like leaving out
370  You like leaving out Revision 1.29 2004/08/09 12:58:05 reddawg
371  You like leaving out let me know when you got the surce
372  You like leaving out
373  You like leaving out Revision 1.28 2004/08/01 17:58:39 flameshadow
374  You like leaving out chg: fixed string allocation bug in ubixfs_cacheNew()
375  You like leaving out
376  You like leaving out Revision 1.27 2004/07/28 17:24:13 flameshadow
377  You like leaving out chg: no comment
378  You like leaving out
379  You like leaving out Revision 1.26 2004/07/28 17:07:29 flameshadow
380  You like leaving out chg: re-added moving cached nodes to the front of the list when found
381  You like leaving out add: added an assert() in ubixfs.c
382  You like leaving out
383  You like leaving out Revision 1.25 2004/07/27 19:24:31 flameshadow
384  You like leaving out chg: reduced the number of debugging statements in the kernel.
385  You like leaving out
386  You like leaving out Revision 1.24 2004/07/27 12:02:01 reddawg
387  You like leaving out chg: fixed marks bug readFile did a lookup which is why it looked like it was loopping so much
388  You like leaving out
389  You like leaving out Revision 1.23 2004/07/27 09:05:43 flameshadow
390  You like leaving out chg: fixed file not found bug. Still can't find looping issue
391  You like leaving out
392  You like leaving out Revision 1.22 2004/07/27 04:05:20 flameshadow
393  You like leaving out chg: kinda fixed it. Added bunches of debug info
394  You like leaving out
395  You like leaving out Revision 1.21 2004/07/25 22:21:52 flameshadow
396  You like leaving out chg: re-enabled kprintf() in ubixfs_cacheFind()
397  You like leaving out
398  You like leaving out Revision 1.20 2004/07/24 17:19:24 flameshadow
399  You like leaving out chg: Temporarily disabled the moving of the found cache node to the front
400  You like leaving out of the list. It seems to cause problems later (race condition, possibly)
401  You like leaving out
402  You like leaving out Revision 1.19 2004/07/22 23:01:51 reddawg
403  You like leaving out Ok checking in before I sleep
404  You like leaving out
405  You like leaving out Revision 1.18 2004/07/22 19:54:50 flameshadow
406  You like leaving out chg: works now. Thanx ubu
407  You like leaving out
408  You like leaving out Revision 1.17 2004/07/22 19:01:59 flameshadow
409  You like leaving out chg: more directory and file caching
410  You like leaving out
411  You like leaving out Revision 1.16 2004/07/22 16:34:32 flameshadow
412  You like leaving out add: file and dir caching kinda work
413  You like leaving out
414  You like leaving out Revision 1.15 2004/07/21 22:43:18 flameshadow
415  You like leaving out one more try
416  You like leaving out
417  You like leaving out Revision 1.14 2004/07/21 22:42:25 flameshadow
418  You like leaving out try it now
419  You like leaving out
420  You like leaving out Revision 1.13 2004/07/21 22:40:27 flameshadow
421  You like leaving out weird
422  You like leaving out
423  You like leaving out Revision 1.12 2004/07/21 22:18:37 flameshadow
424  You like leaving out chg: renamed subDirsHead/Tail members of cacheNode to fileListHead/Tail
425  You like leaving out
426  You like leaving out Revision 1.11 2004/07/21 22:12:22 flameshadow
427  You like leaving out add: attributes tag in cacheNode
428  You like leaving out add: setting of attributes in ubixfx_cacheNew() and ubixfs_cacheFind()
429  You like leaving out
430  You like leaving out Revision 1.10 2004/07/21 22:07:18 flameshadow
431  You like leaving out chg: renamed caching functions (again)
432  You like leaving out
433  You like leaving out Revision 1.9 2004/07/21 22:00:04 flameshadow
434  You like leaving out chg: ubixfws_dirCacheAdd now returns a pointer to the node it added
435  You like leaving out chg: minor fix in ubixfs_dirCacheFind()
436  You like leaving out
437  You like leaving out Revision 1.6 2004/07/21 14:43:14 flameshadow
438  You like leaving out add: added cwc (current working container) to the osInfo strut
439  You like leaving out so i don't know what is going on eh?
440 
441  END
442  ***/
443 
cacheNode::dirty
int dirty
Definition: dirCache.h:45
spinlock.h
directoryEntry::permissions
uInt16 permissions
Definition: ubixfs.h:106
strcpy
char * strcpy(char *, const char *)
uInt32
unsigned long int uInt32
Definition: objgfx30.h:49
ubixfs_cacheAdd
struct cacheNode * ubixfs_cacheAdd(struct cacheNode *node, struct cacheNode *newNode)
Definition: dirCache.c:252
string.h
directoryEntry::fileName
char fileName[256]
Definition: ubixfs.h:107
kfree
void kfree(void *baseAddr)
Definition: kmalloc.c:342
strcmp
int strcmp(const char *str1, const char *str2)
assert
#define assert(e)
Definition: assert.h:64
assert.h
dirCache.h
spinUnlock
void spinUnlock(spinLock_t *lock)
Definition: spinlock.c:36
ubixfs_cacheNew
struct cacheNode * ubixfs_cacheNew(const char *name)
Definition: dirCache.c:193
directoryEntry::attributes
uInt16 attributes
Definition: ubixfs.h:105
strlen
int strlen(const char *str)
Definition: strlen.c:55
directoryEntry
Definition: ubixfs.h:98
SPIN_LOCK_INITIALIZER
#define SPIN_LOCK_INITIALIZER
Definition: spinlock.h:36
cacheNode::info
void * info
Definition: dirCache.h:42
cacheNode::prev
struct cacheNode * prev
Definition: dirCache.h:37
spinLock
void spinLock(spinLock_t *lock)
Definition: spinlock.c:55
cacheNode::fileListHead
struct cacheNode * fileListHead
Definition: dirCache.h:40
cacheNode::parent
struct cacheNode * parent
Definition: dirCache.h:39
ubixfs_cacheDelete
void ubixfs_cacheDelete(struct cacheNode **head)
Definition: dirCache.c:210
kprintf.h
cacheNode::permissions
uInt16 * permissions
Definition: dirCache.h:48
cacheNode::fileListTail
struct cacheNode * fileListTail
Definition: dirCache.h:41
cacheNode::startCluster
uInt32 * startCluster
Definition: dirCache.h:46
directoryEntry::startCluster
uInt32 startCluster
Definition: ubixfs.h:99
cacheNode::present
int present
Definition: dirCache.h:44
cacheNode::size
int * size
Definition: dirCache.h:43
cacheNode::attributes
uInt16 * attributes
Definition: dirCache.h:47
ubixfs_cacheFind
struct cacheNode * ubixfs_cacheFind(struct cacheNode *head, char *name)
Definition: dirCache.c:55
name
const char * name
Definition: pci.c:37
ubixfs.h
kmalloc
void * kmalloc(uInt32 len)
Definition: kmalloc.c:241
spinLock
Definition: spinlock.h:41
typeFile
#define typeFile
Definition: ubixfs.h:47
cacheNode::name
char * name
Definition: dirCache.h:36
directoryEntry::size
uInt32 size
Definition: ubixfs.h:100
cacheNode::next
struct cacheNode * next
Definition: dirCache.h:38
kmalloc.h
cacheNode
Definition: dirCache.h:35
NULL
#define NULL
Definition: fat_string.h:17