first pass at updating head branch to be to be the same as the SAMBA_2_0 branch
[ira/wip.git] / source / smbd / dir.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    Directory handling routines
5    Copyright (C) Andrew Tridgell 1992-1998
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 extern int DEBUGLEVEL;
25
26 /*
27    This module implements directory related functions for Samba.
28 */
29
30 typedef struct _dptr_struct {
31         struct _dptr_struct *next, *prev;
32         int dnum;
33         uint16 spid;
34         connection_struct *conn;
35         void *ptr;
36         BOOL expect_close;
37         char *wcard; /* Field only used for trans2_ searches */
38         uint16 attr; /* Field only used for trans2_ searches */
39         char *path;
40 } dptr_struct;
41
42 static struct bitmap *dptr_bmap;
43 static dptr_struct *dirptrs;
44
45 static int dptrs_open = 0;
46
47 #define INVALID_DPTR_KEY (-3)
48
49 /****************************************************************************
50  Initialise the dir bitmap.
51 ****************************************************************************/
52
53 void init_dptrs(void)
54 {
55   static BOOL dptrs_init=False;
56
57   if (dptrs_init)
58     return;
59
60   dptr_bmap = bitmap_allocate(MAX_DIRECTORY_HANDLES);
61
62   if (!dptr_bmap)
63     exit_server("out of memory in init_dptrs\n");
64
65   dptrs_init = True;
66 }
67
68 /****************************************************************************
69  Idle a dptr - the directory is closed but the control info is kept.
70 ****************************************************************************/
71
72 static void dptr_idle(dptr_struct *dptr)
73 {
74   if (dptr->ptr) {
75     DEBUG(4,("Idling dptr dnum %d\n",dptr->dnum));
76     dptrs_open--;
77     CloseDir(dptr->ptr);
78     dptr->ptr = NULL;
79   }
80 }
81
82 /****************************************************************************
83  Idle the oldest dptr.
84 ****************************************************************************/
85
86 static void dptr_idleoldest(void)
87 {
88   dptr_struct *dptr;
89
90   /*
91    * Go to the end of the list.
92    */
93   for(dptr = dirptrs; dptr && dptr->next; dptr = dptr->next)
94     ;
95
96   if(!dptr) {
97     DEBUG(0,("No dptrs available to idle ?\n"));
98     return;
99   }
100
101   /*
102    * Idle the oldest pointer.
103    */
104
105   for(; dptr; dptr = dptr->prev) {
106     if (dptr->ptr) {
107       dptr_idle(dptr);
108       return;
109     }
110   }
111 }
112
113 /****************************************************************************
114  Get the dptr_struct for a dir index.
115 ****************************************************************************/
116
117 static dptr_struct *dptr_get(int key, BOOL forclose)
118 {
119   dptr_struct *dptr;
120
121   for(dptr = dirptrs; dptr; dptr = dptr->next) {
122     if(dptr->dnum == key) {
123       if (!forclose && !dptr->ptr) {
124         if (dptrs_open >= MAX_OPEN_DIRECTORIES)
125           dptr_idleoldest();
126         DEBUG(4,("Reopening dptr key %d\n",key));
127         if ((dptr->ptr = OpenDir(dptr->conn, dptr->path, True)))
128           dptrs_open++;
129       }
130       DLIST_PROMOTE(dirptrs,dptr);
131       return dptr;
132     }
133   }
134   return(NULL);
135 }
136
137 /****************************************************************************
138  Get the dptr ptr for a dir index.
139 ****************************************************************************/
140
141 static void *dptr_ptr(int key)
142 {
143   dptr_struct *dptr = dptr_get(key, False);
144
145   if (dptr)
146     return(dptr->ptr);
147   return(NULL);
148 }
149
150 /****************************************************************************
151  Get the dir path for a dir index.
152 ****************************************************************************/
153
154 char *dptr_path(int key)
155 {
156   dptr_struct *dptr = dptr_get(key, False);
157
158   if (dptr)
159     return(dptr->path);
160   return(NULL);
161 }
162
163 /****************************************************************************
164  Get the dir wcard for a dir index (lanman2 specific).
165 ****************************************************************************/
166
167 char *dptr_wcard(int key)
168 {
169   dptr_struct *dptr = dptr_get(key, False);
170
171   if (dptr)
172     return(dptr->wcard);
173   return(NULL);
174 }
175
176 /****************************************************************************
177  Set the dir wcard for a dir index (lanman2 specific).
178  Returns 0 on ok, 1 on fail.
179 ****************************************************************************/
180
181 BOOL dptr_set_wcard(int key, char *wcard)
182 {
183   dptr_struct *dptr = dptr_get(key, False);
184
185   if (dptr) {
186     dptr->wcard = wcard;
187     return True;
188   }
189   return False;
190 }
191
192 /****************************************************************************
193  Set the dir attrib for a dir index (lanman2 specific).
194  Returns 0 on ok, 1 on fail.
195 ****************************************************************************/
196
197 BOOL dptr_set_attr(int key, uint16 attr)
198 {
199   dptr_struct *dptr = dptr_get(key, False);
200
201   if (dptr) {
202     dptr->attr = attr;
203     return True;
204   }
205   return False;
206 }
207
208 /****************************************************************************
209  Get the dir attrib for a dir index (lanman2 specific)
210 ****************************************************************************/
211
212 uint16 dptr_attr(int key)
213 {
214   dptr_struct *dptr = dptr_get(key, False);
215
216   if (dptr)
217     return(dptr->attr);
218   return(0);
219 }
220
221 /****************************************************************************
222  Close a dptr (internal func).
223 ****************************************************************************/
224
225 static void dptr_close_internal(dptr_struct *dptr)
226 {
227   DEBUG(4,("closing dptr key %d\n",dptr->dnum));
228
229   DLIST_REMOVE(dirptrs, dptr);
230
231   /* 
232    * Free the dnum in the bitmap. Remember the dnum value is always 
233    * biased by one with respect to the bitmap.
234    */
235
236   if(bitmap_query( dptr_bmap, dptr->dnum - 1) != True) {
237     DEBUG(0,("dptr_close_internal : Error - closing dnum = %d and bitmap not set !\n",
238                         dptr->dnum ));
239   }
240
241   bitmap_clear(dptr_bmap, dptr->dnum - 1);
242
243   if (dptr->ptr) {
244     CloseDir(dptr->ptr);
245     dptrs_open--;
246   }
247
248   /* Lanman 2 specific code */
249   if (dptr->wcard)
250     free(dptr->wcard);
251   string_set(&dptr->path,"");
252   free((char *)dptr);
253 }
254
255 /****************************************************************************
256  Close a dptr given a key.
257 ****************************************************************************/
258
259 void dptr_close(int *key)
260 {
261   dptr_struct *dptr;
262
263   if(*key == INVALID_DPTR_KEY)
264     return;
265
266   /* OS/2 seems to use -1 to indicate "close all directories" */
267   if (*key == -1) {
268     dptr_struct *next;
269     for(dptr = dirptrs; dptr; dptr = next) {
270       next = dptr->next;
271       dptr_close_internal(dptr);
272     }
273     *key = INVALID_DPTR_KEY;
274     return;
275   }
276
277   dptr = dptr_get(*key, True);
278
279   if (!dptr) {
280     DEBUG(0,("Invalid key %d given to dptr_close\n", *key));
281     return;
282   }
283
284   dptr_close_internal(dptr);
285
286   *key = INVALID_DPTR_KEY;
287 }
288
289 /****************************************************************************
290  Close all dptrs for a cnum.
291 ****************************************************************************/
292
293 void dptr_closecnum(connection_struct *conn)
294 {
295   dptr_struct *dptr, *next;
296   for(dptr = dirptrs; dptr; dptr = next) {
297     next = dptr->next;
298     if (dptr->conn == conn)
299       dptr_close_internal(dptr);
300   }
301 }
302
303 /****************************************************************************
304  Idle all dptrs for a cnum.
305 ****************************************************************************/
306
307 void dptr_idlecnum(connection_struct *conn)
308 {
309   dptr_struct *dptr;
310   for(dptr = dirptrs; dptr; dptr = dptr->next) {
311     if (dptr->conn == conn && dptr->ptr)
312       dptr_idle(dptr);
313   }
314 }
315
316 /****************************************************************************
317  Close a dptr that matches a given path, only if it matches the spid also.
318 ****************************************************************************/
319
320 void dptr_closepath(char *path,uint16 spid)
321 {
322   dptr_struct *dptr, *next;
323   for(dptr = dirptrs; dptr; dptr = next) {
324     next = dptr->next;
325     if (spid == dptr->spid && strequal(dptr->path,path))
326       dptr_close_internal(dptr);
327   }
328 }
329
330 /****************************************************************************
331  Start a directory listing.
332 ****************************************************************************/
333
334 static BOOL start_dir(connection_struct *conn,char *directory)
335 {
336   DEBUG(5,("start_dir dir=%s\n",directory));
337
338   if (!check_name(directory,conn))
339     return(False);
340   
341   if (! *directory)
342     directory = ".";
343
344   conn->dirptr = OpenDir(conn, directory, True);
345   if (conn->dirptr) {    
346     dptrs_open++;
347     string_set(&conn->dirpath,directory);
348     return(True);
349   }
350   
351   return(False);
352 }
353
354 /****************************************************************************
355  Try and close the oldest handle not marked for
356  expect close in the hope that the client has
357  finished with that one.
358 ****************************************************************************/
359
360 static void dptr_close_oldest(BOOL old)
361 {
362   dptr_struct *dptr;
363
364   /*
365    * Go to the end of the list.
366    */
367   for(dptr = dirptrs; dptr && dptr->next; dptr = dptr->next)
368     ;
369
370   if(!dptr) {
371     DEBUG(0,("No old dptrs available to close oldest ?\n"));
372     return;
373   }
374
375   /*
376    * If 'old' is true, close the oldest oldhandle dnum (ie. 1 < dnum < 256) that
377    * does not have expect_close set. If 'old' is false, close
378    * one of the new dnum handles.
379    */
380
381   for(; dptr; dptr = dptr->prev) {
382     if ((old && (dptr->dnum < 256) && !dptr->expect_close) ||
383         (!old && (dptr->dnum > 255))) {
384       dptr_close_internal(dptr);
385       return;
386     }
387   }
388 }
389
390 /****************************************************************************
391  Create a new dir ptr. If the flag old_handle is true then we must allocate
392  from the bitmap range 0 - 255 as old SMBsearch directory handles are only
393  one byte long. If old_handle is false we allocate from the range
394  256 - MAX_DIRECTORY_HANDLES. We bias the number we return by 1 to ensure
395  a directory handle is never zero. All the above is folklore taught to
396  me at Andrew's knee.... :-) :-). JRA.
397 ****************************************************************************/
398
399 int dptr_create(connection_struct *conn,char *path, BOOL old_handle, BOOL expect_close,uint16 spid)
400 {
401   dptr_struct *dptr;
402
403   if (!start_dir(conn,path))
404     return(-2); /* Code to say use a unix error return code. */
405
406   if (dptrs_open >= MAX_OPEN_DIRECTORIES)
407     dptr_idleoldest();
408
409   dptr = (dptr_struct *)malloc(sizeof(dptr_struct));
410   if(!dptr) {
411     DEBUG(0,("malloc fail in dptr_create.\n"));
412     return -1;
413   }
414
415   ZERO_STRUCTP(dptr);
416
417   if(old_handle) {
418
419     /*
420      * This is an old-style SMBsearch request. Ensure the
421      * value we return will fit in the range 1-255.
422      */
423
424     dptr->dnum = bitmap_find(dptr_bmap, 0);
425
426     if(dptr->dnum == -1 || dptr->dnum > 254) {
427
428       /*
429        * Try and close the oldest handle not marked for
430        * expect close in the hope that the client has
431        * finished with that one.
432        */
433
434       dptr_close_oldest(True);
435
436       /* Now try again... */
437       dptr->dnum = bitmap_find(dptr_bmap, 0);
438
439       if(dptr->dnum == -1 || dptr->dnum > 254) {
440         DEBUG(0,("dptr_create: returned %d: Error - all old dirptrs in use ?\n", dptr->dnum));
441         free((char *)dptr);
442         return -1;
443       }
444     }
445   } else {
446
447     /*
448      * This is a new-style trans2 request. Allocate from
449      * a range that will return 256 - MAX_DIRECTORY_HANDLES.
450      */
451
452     dptr->dnum = bitmap_find(dptr_bmap, 255);
453
454     if(dptr->dnum == -1 || dptr->dnum < 255) {
455
456       /*
457        * Try and close the oldest handle close in the hope that
458        * the client has finished with that one. This will only
459        * happen in the case of the Win98 client bug where it leaks
460        * directory handles.
461        */
462
463       dptr_close_oldest(False);
464
465       /* Now try again... */
466       dptr->dnum = bitmap_find(dptr_bmap, 255);
467
468       if(dptr->dnum == -1 || dptr->dnum < 255) {
469         DEBUG(0,("dptr_create: returned %d: Error - all new dirptrs in use ?\n", dptr->dnum));
470         free((char *)dptr);
471         return -1;
472       }
473     }
474   }
475
476   bitmap_set(dptr_bmap, dptr->dnum);
477
478   dptr->dnum += 1; /* Always bias the dnum by one - no zero dnums allowed. */
479
480   dptr->ptr = conn->dirptr;
481   string_set(&dptr->path,path);
482   dptr->conn = conn;
483   dptr->spid = spid;
484   dptr->expect_close = expect_close;
485   dptr->wcard = NULL; /* Only used in lanman2 searches */
486   dptr->attr = 0; /* Only used in lanman2 searches */
487
488   DLIST_ADD(dirptrs, dptr);
489
490   DEBUG(3,("creating new dirptr %d for path %s, expect_close = %d\n",
491            dptr->dnum,path,expect_close));  
492
493   return(dptr->dnum);
494 }
495
496 #define DPTR_MASK ((uint32)(((uint32)1)<<31))
497
498 /****************************************************************************
499  Fill the 5 byte server reserved dptr field.
500 ****************************************************************************/
501
502 BOOL dptr_fill(char *buf1,unsigned int key)
503 {
504   unsigned char *buf = (unsigned char *)buf1;
505   void *p = dptr_ptr(key);
506   uint32 offset;
507   if (!p) {
508     DEBUG(1,("filling null dirptr %d\n",key));
509     return(False);
510   }
511   offset = TellDir(p);
512   DEBUG(6,("fill on key %u dirptr 0x%lx now at %d\n",key,
513            (long)p,(int)offset));
514   buf[0] = key;
515   SIVAL(buf,1,offset | DPTR_MASK);
516   return(True);
517 }
518
519
520 /****************************************************************************
521  Return True if the offset is at zero.
522 ****************************************************************************/
523
524 BOOL dptr_zero(char *buf)
525 {
526   return((IVAL(buf,1)&~DPTR_MASK) == 0);
527 }
528
529 /****************************************************************************
530  Fetch the dir ptr and seek it given the 5 byte server field.
531 ****************************************************************************/
532
533 void *dptr_fetch(char *buf,int *num)
534 {
535   unsigned int key = *(unsigned char *)buf;
536   void *p = dptr_ptr(key);
537   uint32 offset;
538   if (!p) {
539     DEBUG(3,("fetched null dirptr %d\n",key));
540     return(NULL);
541   }
542   *num = key;
543   offset = IVAL(buf,1)&~DPTR_MASK;
544   SeekDir(p,offset);
545   DEBUG(3,("fetching dirptr %d for path %s at offset %d\n",
546            key,dptr_path(key),offset));
547   return(p);
548 }
549
550 /****************************************************************************
551  Fetch the dir ptr.
552 ****************************************************************************/
553
554 void *dptr_fetch_lanman2(int dptr_num)
555 {
556   void *p = dptr_ptr(dptr_num);
557
558   if (!p) {
559     DEBUG(3,("fetched null dirptr %d\n",dptr_num));
560     return(NULL);
561   }
562   DEBUG(3,("fetching dirptr %d for path %s\n",dptr_num,dptr_path(dptr_num)));
563   return(p);
564 }
565
566 /****************************************************************************
567  Check a filetype for being valid.
568 ****************************************************************************/
569
570 BOOL dir_check_ftype(connection_struct *conn,int mode,SMB_STRUCT_STAT *st,int dirtype)
571 {
572   if (((mode & ~dirtype) & (aHIDDEN | aSYSTEM | aDIR)) != 0)
573     return False;
574   return True;
575 }
576
577 /****************************************************************************
578  Get an 8.3 directory entry.
579 ****************************************************************************/
580
581 BOOL get_dir_entry(connection_struct *conn,char *mask,int dirtype,char *fname,
582                    SMB_OFF_T *size,int *mode,time_t *date,BOOL check_descend)
583 {
584   char *dname;
585   BOOL found = False;
586   SMB_STRUCT_STAT sbuf;
587   pstring path;
588   pstring pathreal;
589   BOOL isrootdir;
590   pstring filename;
591   BOOL needslash;
592
593   *path = *pathreal = *filename = 0;
594
595   isrootdir = (strequal(conn->dirpath,"./") ||
596                strequal(conn->dirpath,".") ||
597                strequal(conn->dirpath,"/"));
598   
599   needslash = ( conn->dirpath[strlen(conn->dirpath) -1] != '/');
600
601   if (!conn->dirptr)
602     return(False);
603   
604   while (!found)
605   {
606     BOOL filename_is_mask = False;
607     dname = ReadDirName(conn->dirptr);
608
609     DEBUG(6,("readdir on dirptr 0x%lx now at offset %d\n",
610           (long)conn->dirptr,TellDir(conn->dirptr)));
611       
612     if (dname == NULL) 
613       return(False);
614       
615     pstrcpy(filename,dname);      
616
617     if ((filename_is_mask = (strcmp(filename,mask) == 0)) ||
618         (name_map_mangle(filename,True,False,SNUM(conn)) &&
619          mask_match(filename,mask,False,False)))
620     {
621       if (isrootdir && (strequal(filename,"..") || strequal(filename,".")))
622         continue;
623
624       pstrcpy(fname,filename);
625       *path = 0;
626       pstrcpy(path,conn->dirpath);
627       if(needslash)
628         pstrcat(path,"/");
629       pstrcpy(pathreal,path);
630       pstrcat(path,fname);
631       pstrcat(pathreal,dname);
632       if (dos_stat(pathreal,&sbuf) != 0) 
633       {
634         DEBUG(5,("Couldn't stat 1 [%s]. Error = %s\n",path, strerror(errno) ));
635         continue;
636       }
637
638       if (check_descend && !strequal(fname,".") && !strequal(fname,".."))
639         continue;
640           
641       *mode = dos_mode(conn,pathreal,&sbuf);
642
643       if (!dir_check_ftype(conn,*mode,&sbuf,dirtype)) 
644       {
645         DEBUG(5,("[%s] attribs didn't match %x\n",filename,dirtype));
646         continue;
647       }
648
649       if (!filename_is_mask)
650       {
651         /* Now we can allow the mangled cache to be updated */
652         pstrcpy(filename,dname);
653         name_map_mangle(filename,True,True,SNUM(conn));
654       }
655
656       *size = sbuf.st_size;
657       *date = sbuf.st_mtime;
658
659       DEBUG(5,("get_dir_entry found %s fname=%s\n",pathreal,fname));
660           
661       found = True;
662     }
663   }
664
665   return(found);
666 }
667
668
669
670 typedef struct
671 {
672   int pos;
673   int numentries;
674   int mallocsize;
675   char *data;
676   char *current;
677 } Dir;
678
679
680 /*******************************************************************
681  Open a directory.
682 ********************************************************************/
683
684 void *OpenDir(connection_struct *conn, char *name, BOOL use_veto)
685 {
686   Dir *dirp;
687   char *n;
688   DIR *p = dos_opendir(name);
689   int used=0;
690
691   if (!p) return(NULL);
692   dirp = (Dir *)malloc(sizeof(Dir));
693   if (!dirp) {
694     closedir(p);
695     return(NULL);
696   }
697   dirp->pos = dirp->numentries = dirp->mallocsize = 0;
698   dirp->data = dirp->current = NULL;
699
700   while ((n = dos_readdirname(p)))
701   {
702     int l = strlen(n)+1;
703
704     /* If it's a vetoed file, pretend it doesn't even exist */
705     if (use_veto && conn && IS_VETO_PATH(conn, n)) continue;
706
707     if (used + l > dirp->mallocsize) {
708       int s = MAX(used+l,used+2000);
709       char *r;
710       r = (char *)Realloc(dirp->data,s);
711       if (!r) {
712         DEBUG(0,("Out of memory in OpenDir\n"));
713         break;
714       }
715       dirp->data = r;
716       dirp->mallocsize = s;
717       dirp->current = dirp->data;
718     }
719     pstrcpy(dirp->data+used,n);
720     used += l;
721     dirp->numentries++;
722   }
723
724   closedir(p);
725   return((void *)dirp);
726 }
727
728
729 /*******************************************************************
730  Close a directory.
731 ********************************************************************/
732
733 void CloseDir(void *p)
734 {
735   Dir *dirp = (Dir *)p;
736   if (!dirp) return;    
737   if (dirp->data) free(dirp->data);
738   free(dirp);
739 }
740
741 /*******************************************************************
742  Read from a directory.
743 ********************************************************************/
744
745 char *ReadDirName(void *p)
746 {
747   char *ret;
748   Dir *dirp = (Dir *)p;
749
750   if (!dirp || !dirp->current || dirp->pos >= dirp->numentries) return(NULL);
751
752   ret = dirp->current;
753   dirp->current = skip_string(dirp->current,1);
754   dirp->pos++;
755
756   return(ret);
757 }
758
759
760 /*******************************************************************
761  Seek a dir.
762 ********************************************************************/
763
764 BOOL SeekDir(void *p,int pos)
765 {
766   Dir *dirp = (Dir *)p;
767
768   if (!dirp) return(False);
769
770   if (pos < dirp->pos) {
771     dirp->current = dirp->data;
772     dirp->pos = 0;
773   }
774
775   while (dirp->pos < pos && ReadDirName(p)) ;
776
777   return(dirp->pos == pos);
778 }
779
780 /*******************************************************************
781  Tell a dir position.
782 ********************************************************************/
783
784 int TellDir(void *p)
785 {
786   Dir *dirp = (Dir *)p;
787
788   if (!dirp) return(-1);
789   
790   return(dirp->pos);
791 }
792
793
794 /* -------------------------------------------------------------------------- **
795  * This section manages a global directory cache.
796  * (It should probably be split into a separate module.  crh)
797  * -------------------------------------------------------------------------- **
798  */
799
800 typedef struct
801   {
802   ubi_dlNode  node;
803   char       *path;
804   char       *name;
805   char       *dname;
806   int         snum;
807   } dir_cache_entry;
808
809 static ubi_dlNewList( dir_cache );
810
811 void DirCacheAdd( char *path, char *name, char *dname, int snum )
812   /* ------------------------------------------------------------------------ **
813    * Add an entry to the directory cache.
814    *
815    *  Input:  path  -
816    *          name  -
817    *          dname -
818    *          snum  -
819    *
820    *  Output: None.
821    *
822    * ------------------------------------------------------------------------ **
823    */
824   {
825   int               pathlen;
826   int               namelen;
827   dir_cache_entry  *entry;
828
829   /* Allocate the structure & string space in one go so that it can be freed
830    * in one call to free().
831    */
832   pathlen = strlen( path ) +1;  /* Bytes required to store path (with nul). */
833   namelen = strlen( name ) +1;  /* Bytes required to store name (with nul). */
834   entry = (dir_cache_entry *)malloc( sizeof( dir_cache_entry )
835                                    + pathlen
836                                    + namelen
837                                    + strlen( dname ) +1 );
838   if( NULL == entry )   /* Not adding to the cache is not fatal,  */
839     return;             /* so just return as if nothing happened. */
840
841   /* Set pointers correctly and load values. */
842   entry->path  = pstrcpy( (char *)&entry[1],       path);
843   entry->name  = pstrcpy( &(entry->path[pathlen]), name);
844   entry->dname = pstrcpy( &(entry->name[namelen]), dname);
845   entry->snum  = snum;
846
847   /* Add the new entry to the linked list. */
848   (void)ubi_dlAddHead( dir_cache, entry );
849   DEBUG( 4, ("Added dir cache entry %s %s -> %s\n", path, name, dname ) );
850
851   /* Free excess cache entries. */
852   while( DIRCACHESIZE < dir_cache->count )
853     free( ubi_dlRemTail( dir_cache ) );
854
855   } /* DirCacheAdd */
856
857
858 char *DirCacheCheck( char *path, char *name, int snum )
859   /* ------------------------------------------------------------------------ **
860    * Search for an entry to the directory cache.
861    *
862    *  Input:  path  -
863    *          name  -
864    *          snum  -
865    *
866    *  Output: The dname string of the located entry, or NULL if the entry was
867    *          not found.
868    *
869    *  Notes:  This uses a linear search, which is is okay because of
870    *          the small size of the cache.  Use a splay tree or hash
871    *          for large caches.
872    *
873    * ------------------------------------------------------------------------ **
874    */
875   {
876   dir_cache_entry *entry;
877
878   for( entry = (dir_cache_entry *)ubi_dlFirst( dir_cache );
879        NULL != entry;
880        entry = (dir_cache_entry *)ubi_dlNext( entry ) )
881     {
882     if( entry->snum == snum
883         && 0 == strcmp( name, entry->name )
884         && 0 == strcmp( path, entry->path ) )
885       {
886       DEBUG(4, ("Got dir cache hit on %s %s -> %s\n",path,name,entry->dname));
887       return( entry->dname );
888       }
889     }
890
891   return(NULL);
892   } /* DirCacheCheck */
893
894 void DirCacheFlush(int snum)
895   /* ------------------------------------------------------------------------ **
896    * Remove all cache entries which have an snum that matches the input.
897    *
898    *  Input:  snum  -
899    *
900    *  Output: None.
901    *
902    * ------------------------------------------------------------------------ **
903    */
904 {
905         dir_cache_entry *entry;
906         ubi_dlNodePtr    next;
907
908         for(entry = (dir_cache_entry *)ubi_dlFirst( dir_cache ); 
909             NULL != entry; )  {
910                 next = ubi_dlNext( entry );
911                 if( entry->snum == snum )
912                         free( ubi_dlRemThis( dir_cache, entry ) );
913                 entry = (dir_cache_entry *)next;
914         }
915 } /* DirCacheFlush */
916
917 /* -------------------------------------------------------------------------- **
918  * End of the section that manages the global directory cache.
919  * -------------------------------------------------------------------------- **
920  */
921
922