don't forget the NULL
[Samba.git] / source / smbd / dir.c
blobf1db06b33a3ee07092592326c5243045330c50da
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 Directory handling routines
5 Copyright (C) Andrew Tridgell 1992-1998
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.
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.
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.
22 #include "includes.h"
25 This module implements directory related functions for Samba.
28 typedef struct _dptr_struct {
29 struct _dptr_struct *next, *prev;
30 int dnum;
31 uint16 spid;
32 connection_struct *conn;
33 void *ptr;
34 BOOL expect_close;
35 char *wcard; /* Field only used for trans2_ searches */
36 uint16 attr; /* Field only used for trans2_ searches */
37 char *path;
38 } dptr_struct;
40 static struct bitmap *dptr_bmap;
41 static dptr_struct *dirptrs;
43 static int dptrs_open = 0;
45 #define INVALID_DPTR_KEY (-3)
47 /****************************************************************************
48 Initialise the dir bitmap.
49 ****************************************************************************/
51 void init_dptrs(void)
53 static BOOL dptrs_init=False;
55 if (dptrs_init)
56 return;
58 dptr_bmap = bitmap_allocate(MAX_DIRECTORY_HANDLES);
60 if (!dptr_bmap)
61 exit_server("out of memory in init_dptrs\n");
63 dptrs_init = True;
66 /****************************************************************************
67 Idle a dptr - the directory is closed but the control info is kept.
68 ****************************************************************************/
70 static void dptr_idle(dptr_struct *dptr)
72 if (dptr->ptr) {
73 DEBUG(4,("Idling dptr dnum %d\n",dptr->dnum));
74 dptrs_open--;
75 CloseDir(dptr->ptr);
76 dptr->ptr = NULL;
80 /****************************************************************************
81 Idle the oldest dptr.
82 ****************************************************************************/
84 static void dptr_idleoldest(void)
86 dptr_struct *dptr;
89 * Go to the end of the list.
91 for(dptr = dirptrs; dptr && dptr->next; dptr = dptr->next)
94 if(!dptr) {
95 DEBUG(0,("No dptrs available to idle ?\n"));
96 return;
100 * Idle the oldest pointer.
103 for(; dptr; dptr = dptr->prev) {
104 if (dptr->ptr) {
105 dptr_idle(dptr);
106 return;
111 /****************************************************************************
112 Get the dptr_struct for a dir index.
113 ****************************************************************************/
115 static dptr_struct *dptr_get(int key, BOOL forclose)
117 dptr_struct *dptr;
119 for(dptr = dirptrs; dptr; dptr = dptr->next) {
120 if(dptr->dnum == key) {
121 if (!forclose && !dptr->ptr) {
122 if (dptrs_open >= MAX_OPEN_DIRECTORIES)
123 dptr_idleoldest();
124 DEBUG(4,("Reopening dptr key %d\n",key));
125 if ((dptr->ptr = OpenDir(dptr->conn, dptr->path, True)))
126 dptrs_open++;
128 DLIST_PROMOTE(dirptrs,dptr);
129 return dptr;
132 return(NULL);
135 /****************************************************************************
136 Get the dptr ptr for a dir index.
137 ****************************************************************************/
139 static void *dptr_ptr(int key)
141 dptr_struct *dptr = dptr_get(key, False);
143 if (dptr)
144 return(dptr->ptr);
145 return(NULL);
148 /****************************************************************************
149 Get the dir path for a dir index.
150 ****************************************************************************/
152 char *dptr_path(int key)
154 dptr_struct *dptr = dptr_get(key, False);
156 if (dptr)
157 return(dptr->path);
158 return(NULL);
161 /****************************************************************************
162 Get the dir wcard for a dir index (lanman2 specific).
163 ****************************************************************************/
165 char *dptr_wcard(int key)
167 dptr_struct *dptr = dptr_get(key, False);
169 if (dptr)
170 return(dptr->wcard);
171 return(NULL);
174 /****************************************************************************
175 Set the dir wcard for a dir index (lanman2 specific).
176 Returns 0 on ok, 1 on fail.
177 ****************************************************************************/
179 BOOL dptr_set_wcard(int key, char *wcard)
181 dptr_struct *dptr = dptr_get(key, False);
183 if (dptr) {
184 dptr->wcard = wcard;
185 return True;
187 return False;
190 /****************************************************************************
191 Set the dir attrib for a dir index (lanman2 specific).
192 Returns 0 on ok, 1 on fail.
193 ****************************************************************************/
195 BOOL dptr_set_attr(int key, uint16 attr)
197 dptr_struct *dptr = dptr_get(key, False);
199 if (dptr) {
200 dptr->attr = attr;
201 return True;
203 return False;
206 /****************************************************************************
207 Get the dir attrib for a dir index (lanman2 specific)
208 ****************************************************************************/
210 uint16 dptr_attr(int key)
212 dptr_struct *dptr = dptr_get(key, False);
214 if (dptr)
215 return(dptr->attr);
216 return(0);
219 /****************************************************************************
220 Close a dptr (internal func).
221 ****************************************************************************/
223 static void dptr_close_internal(dptr_struct *dptr)
225 DEBUG(4,("closing dptr key %d\n",dptr->dnum));
227 DLIST_REMOVE(dirptrs, dptr);
230 * Free the dnum in the bitmap. Remember the dnum value is always
231 * biased by one with respect to the bitmap.
234 if(bitmap_query( dptr_bmap, dptr->dnum - 1) != True) {
235 DEBUG(0,("dptr_close_internal : Error - closing dnum = %d and bitmap not set !\n",
236 dptr->dnum ));
239 bitmap_clear(dptr_bmap, dptr->dnum - 1);
241 if (dptr->ptr) {
242 CloseDir(dptr->ptr);
243 dptrs_open--;
246 /* Lanman 2 specific code */
247 SAFE_FREE(dptr->wcard);
248 string_set(&dptr->path,"");
249 SAFE_FREE(dptr);
252 /****************************************************************************
253 Close a dptr given a key.
254 ****************************************************************************/
256 void dptr_close(int *key)
258 dptr_struct *dptr;
260 if(*key == INVALID_DPTR_KEY)
261 return;
263 /* OS/2 seems to use -1 to indicate "close all directories" */
264 if (*key == -1) {
265 dptr_struct *next;
266 for(dptr = dirptrs; dptr; dptr = next) {
267 next = dptr->next;
268 dptr_close_internal(dptr);
270 *key = INVALID_DPTR_KEY;
271 return;
274 dptr = dptr_get(*key, True);
276 if (!dptr) {
277 DEBUG(0,("Invalid key %d given to dptr_close\n", *key));
278 return;
281 dptr_close_internal(dptr);
283 *key = INVALID_DPTR_KEY;
286 /****************************************************************************
287 Close all dptrs for a cnum.
288 ****************************************************************************/
290 void dptr_closecnum(connection_struct *conn)
292 dptr_struct *dptr, *next;
293 for(dptr = dirptrs; dptr; dptr = next) {
294 next = dptr->next;
295 if (dptr->conn == conn)
296 dptr_close_internal(dptr);
300 /****************************************************************************
301 Idle all dptrs for a cnum.
302 ****************************************************************************/
304 void dptr_idlecnum(connection_struct *conn)
306 dptr_struct *dptr;
307 for(dptr = dirptrs; dptr; dptr = dptr->next) {
308 if (dptr->conn == conn && dptr->ptr)
309 dptr_idle(dptr);
313 /****************************************************************************
314 Close a dptr that matches a given path, only if it matches the spid also.
315 ****************************************************************************/
317 void dptr_closepath(char *path,uint16 spid)
319 dptr_struct *dptr, *next;
320 for(dptr = dirptrs; dptr; dptr = next) {
321 next = dptr->next;
322 if (spid == dptr->spid && strequal(dptr->path,path))
323 dptr_close_internal(dptr);
327 /****************************************************************************
328 Start a directory listing.
329 ****************************************************************************/
331 static BOOL start_dir(connection_struct *conn,char *directory)
333 DEBUG(5,("start_dir dir=%s\n",directory));
335 if (!check_name(directory,conn))
336 return(False);
338 if (! *directory)
339 directory = ".";
341 conn->dirptr = OpenDir(conn, directory, True);
342 if (conn->dirptr) {
343 dptrs_open++;
344 string_set(&conn->dirpath,directory);
345 return(True);
348 return(False);
351 /****************************************************************************
352 Try and close the oldest handle not marked for
353 expect close in the hope that the client has
354 finished with that one.
355 ****************************************************************************/
357 static void dptr_close_oldest(BOOL old)
359 dptr_struct *dptr;
362 * Go to the end of the list.
364 for(dptr = dirptrs; dptr && dptr->next; dptr = dptr->next)
367 if(!dptr) {
368 DEBUG(0,("No old dptrs available to close oldest ?\n"));
369 return;
373 * If 'old' is true, close the oldest oldhandle dnum (ie. 1 < dnum < 256) that
374 * does not have expect_close set. If 'old' is false, close
375 * one of the new dnum handles.
378 for(; dptr; dptr = dptr->prev) {
379 if ((old && (dptr->dnum < 256) && !dptr->expect_close) ||
380 (!old && (dptr->dnum > 255))) {
381 dptr_close_internal(dptr);
382 return;
387 /****************************************************************************
388 Create a new dir ptr. If the flag old_handle is true then we must allocate
389 from the bitmap range 0 - 255 as old SMBsearch directory handles are only
390 one byte long. If old_handle is false we allocate from the range
391 256 - MAX_DIRECTORY_HANDLES. We bias the number we return by 1 to ensure
392 a directory handle is never zero. All the above is folklore taught to
393 me at Andrew's knee.... :-) :-). JRA.
394 ****************************************************************************/
396 int dptr_create(connection_struct *conn,char *path, BOOL old_handle, BOOL expect_close,uint16 spid)
398 dptr_struct *dptr;
400 if (!start_dir(conn,path))
401 return(-2); /* Code to say use a unix error return code. */
403 if (dptrs_open >= MAX_OPEN_DIRECTORIES)
404 dptr_idleoldest();
406 dptr = (dptr_struct *)malloc(sizeof(dptr_struct));
407 if(!dptr) {
408 DEBUG(0,("malloc fail in dptr_create.\n"));
409 return -1;
412 ZERO_STRUCTP(dptr);
414 if(old_handle) {
417 * This is an old-style SMBsearch request. Ensure the
418 * value we return will fit in the range 1-255.
421 dptr->dnum = bitmap_find(dptr_bmap, 0);
423 if(dptr->dnum == -1 || dptr->dnum > 254) {
426 * Try and close the oldest handle not marked for
427 * expect close in the hope that the client has
428 * finished with that one.
431 dptr_close_oldest(True);
433 /* Now try again... */
434 dptr->dnum = bitmap_find(dptr_bmap, 0);
436 if(dptr->dnum == -1 || dptr->dnum > 254) {
437 DEBUG(0,("dptr_create: returned %d: Error - all old dirptrs in use ?\n", dptr->dnum));
438 SAFE_FREE(dptr);
439 return -1;
442 } else {
445 * This is a new-style trans2 request. Allocate from
446 * a range that will return 256 - MAX_DIRECTORY_HANDLES.
449 dptr->dnum = bitmap_find(dptr_bmap, 255);
451 if(dptr->dnum == -1 || dptr->dnum < 255) {
454 * Try and close the oldest handle close in the hope that
455 * the client has finished with that one. This will only
456 * happen in the case of the Win98 client bug where it leaks
457 * directory handles.
460 dptr_close_oldest(False);
462 /* Now try again... */
463 dptr->dnum = bitmap_find(dptr_bmap, 255);
465 if(dptr->dnum == -1 || dptr->dnum < 255) {
466 DEBUG(0,("dptr_create: returned %d: Error - all new dirptrs in use ?\n", dptr->dnum));
467 SAFE_FREE(dptr);
468 return -1;
473 bitmap_set(dptr_bmap, dptr->dnum);
475 dptr->dnum += 1; /* Always bias the dnum by one - no zero dnums allowed. */
477 dptr->ptr = conn->dirptr;
478 string_set(&dptr->path,path);
479 dptr->conn = conn;
480 dptr->spid = spid;
481 dptr->expect_close = expect_close;
482 dptr->wcard = NULL; /* Only used in lanman2 searches */
483 dptr->attr = 0; /* Only used in lanman2 searches */
485 DLIST_ADD(dirptrs, dptr);
487 DEBUG(3,("creating new dirptr %d for path %s, expect_close = %d\n",
488 dptr->dnum,path,expect_close));
490 return(dptr->dnum);
493 /****************************************************************************
494 Fill the 5 byte server reserved dptr field.
495 ****************************************************************************/
497 BOOL dptr_fill(char *buf1,unsigned int key)
499 unsigned char *buf = (unsigned char *)buf1;
500 void *p = dptr_ptr(key);
501 uint32 offset;
502 if (!p) {
503 DEBUG(1,("filling null dirptr %d\n",key));
504 return(False);
506 offset = TellDir(p);
507 DEBUG(6,("fill on key %u dirptr 0x%lx now at %d\n",key,
508 (long)p,(int)offset));
509 buf[0] = key;
510 SIVAL(buf,1,offset | DPTR_MASK);
511 return(True);
514 /****************************************************************************
515 Fetch the dir ptr and seek it given the 5 byte server field.
516 ****************************************************************************/
518 void *dptr_fetch(char *buf,int *num)
520 unsigned int key = *(unsigned char *)buf;
521 void *p = dptr_ptr(key);
522 uint32 offset;
523 if (!p) {
524 DEBUG(3,("fetched null dirptr %d\n",key));
525 return(NULL);
527 *num = key;
528 offset = IVAL(buf,1)&~DPTR_MASK;
529 SeekDir(p,offset);
530 DEBUG(3,("fetching dirptr %d for path %s at offset %d\n",
531 key,dptr_path(key),offset));
532 return(p);
535 /****************************************************************************
536 Fetch the dir ptr.
537 ****************************************************************************/
539 void *dptr_fetch_lanman2(int dptr_num)
541 void *p = dptr_ptr(dptr_num);
543 if (!p) {
544 DEBUG(3,("fetched null dirptr %d\n",dptr_num));
545 return(NULL);
547 DEBUG(3,("fetching dirptr %d for path %s\n",dptr_num,dptr_path(dptr_num)));
548 return(p);
551 /****************************************************************************
552 Check a filetype for being valid.
553 ****************************************************************************/
555 BOOL dir_check_ftype(connection_struct *conn,int mode,SMB_STRUCT_STAT *st,int dirtype)
557 int mask;
559 /* Check the "may have" search bits. */
560 if (((mode & ~dirtype) & (aHIDDEN | aSYSTEM | aDIR)) != 0)
561 return False;
563 /* Check the "must have" bits, which are the may have bits shifted eight */
564 /* If must have bit is set, the file/dir can not be returned in search unless the matching
565 file attribute is set */
566 mask = ((dirtype >> 8) & (aDIR|aARCH|aRONLY|aHIDDEN|aSYSTEM)); /* & 0x37 */
567 if(mask) {
568 if((mask & (mode & (aDIR|aARCH|aRONLY|aHIDDEN|aSYSTEM))) == mask) /* check if matching attribute present */
569 return True;
570 else
571 return False;
574 return True;
577 static BOOL mangle_mask_match(connection_struct *conn, char *filename, char *mask)
579 mangle_map(filename,True,False,SNUM(conn));
580 return mask_match(filename,mask,False);
583 /****************************************************************************
584 Get an 8.3 directory entry.
585 ****************************************************************************/
587 BOOL get_dir_entry(connection_struct *conn,char *mask,int dirtype,char *fname,
588 SMB_OFF_T *size,int *mode,time_t *date,BOOL check_descend)
590 char *dname;
591 BOOL found = False;
592 SMB_STRUCT_STAT sbuf;
593 pstring path;
594 pstring pathreal;
595 BOOL isrootdir;
596 pstring filename;
597 BOOL needslash;
599 *path = *pathreal = *filename = 0;
601 isrootdir = (strequal(conn->dirpath,"./") ||
602 strequal(conn->dirpath,".") ||
603 strequal(conn->dirpath,"/"));
605 needslash = ( conn->dirpath[strlen(conn->dirpath) -1] != '/');
607 if (!conn->dirptr)
608 return(False);
610 while (!found)
612 dname = ReadDirName(conn->dirptr);
614 DEBUG(6,("readdir on dirptr 0x%lx now at offset %d\n",
615 (long)conn->dirptr,TellDir(conn->dirptr)));
617 if (dname == NULL)
618 return(False);
620 pstrcpy(filename,dname);
622 /* notice the special *.* handling. This appears to be the only difference
623 between the wildcard handling in this routine and in the trans2 routines.
624 see masktest for a demo
626 if ((strcmp(mask,"*.*") == 0) ||
627 mask_match(filename,mask,False) ||
628 mangle_mask_match(conn,filename,mask))
630 if (isrootdir && (strequal(filename,"..") || strequal(filename,".")))
631 continue;
633 if (!mangle_is_8_3(filename, False)) {
634 mangle_map(filename,True,False,SNUM(conn));
637 pstrcpy(fname,filename);
638 *path = 0;
639 pstrcpy(path,conn->dirpath);
640 if(needslash)
641 pstrcat(path,"/");
642 pstrcpy(pathreal,path);
643 pstrcat(path,fname);
644 pstrcat(pathreal,dname);
645 if (conn->vfs_ops.stat(conn,dos_to_unix_static(pathreal), &sbuf) != 0)
647 DEBUG(5,("Couldn't stat 1 [%s]. Error = %s\n",path, strerror(errno) ));
648 continue;
651 *mode = dos_mode(conn,pathreal,&sbuf);
653 if (!dir_check_ftype(conn,*mode,&sbuf,dirtype))
655 DEBUG(5,("[%s] attribs didn't match %x\n",filename,dirtype));
656 continue;
659 *size = sbuf.st_size;
660 *date = sbuf.st_mtime;
662 DEBUG(3,("get_dir_entry mask=[%s] found %s fname=%s\n",mask, pathreal,fname));
664 found = True;
668 return(found);
673 typedef struct
675 int pos;
676 int numentries;
677 int mallocsize;
678 char *data;
679 char *current;
680 } Dir;
684 /*******************************************************************
685 check to see if a user can read a file. This is only approximate,
686 it is used as part of the "hide unreadable" option. Don't
687 use it for anything security sensitive
688 ********************************************************************/
690 static BOOL user_can_read_file(connection_struct *conn, char *name)
692 extern struct current_user current_user;
693 SMB_STRUCT_STAT ste;
694 SEC_DESC *psd = NULL;
695 size_t sd_size;
696 files_struct *fsp;
697 int smb_action;
698 NTSTATUS status;
699 uint32 access_granted;
701 ZERO_STRUCT(ste);
704 * If user is a member of the Admin group
705 * we never hide files from them.
708 if (conn->admin_user)
709 return True;
711 /* If we can't stat it does not show it */
712 if (vfs_stat(conn, name, &ste) != 0)
713 return False;
715 /* Pseudo-open the file (note - no fd's created). */
717 if(S_ISDIR(ste.st_mode))
718 fsp = open_directory(conn, name, &ste, 0, SET_DENY_MODE(DENY_NONE), (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_OPEN),
719 unix_mode(conn,aRONLY|aDIR, name), &smb_action);
720 else
721 fsp = open_file_stat(conn, name, &ste);
723 if (!fsp)
724 return False;
726 /* Get NT ACL -allocated in main loop talloc context. No free needed here. */
727 sd_size = conn->vfs_ops.fget_nt_acl(fsp, fsp->fd, &psd);
728 close_file(fsp, True);
730 /* No access if SD get failed. */
731 if (!sd_size)
732 return False;
734 return se_access_check(psd, current_user.nt_user_token, FILE_READ_DATA,
735 &access_granted, &status);
738 /*******************************************************************
739 Open a directory.
740 ********************************************************************/
742 void *OpenDir(connection_struct *conn, char *name, BOOL use_veto)
744 Dir *dirp;
745 char *n;
746 DIR *p = conn->vfs_ops.opendir(conn,dos_to_unix_static(name));
747 int used=0;
749 if (!p)
750 return(NULL);
751 dirp = (Dir *)malloc(sizeof(Dir));
752 if (!dirp) {
753 DEBUG(0,("Out of memory in OpenDir\n"));
754 conn->vfs_ops.closedir(conn,p);
755 return(NULL);
758 dirp->pos = dirp->numentries = dirp->mallocsize = 0;
759 dirp->data = dirp->current = NULL;
761 while (True) {
762 int l;
763 BOOL normal_entry = True;
765 if (used == 0) {
766 n = ".";
767 normal_entry = False;
768 } else if (used == 2) {
769 n = "..";
770 normal_entry = False;
771 } else {
772 n = vfs_readdirname(conn, p);
773 if (n == NULL)
774 break;
775 if ((strcmp(".",n) == 0) ||(strcmp("..",n) == 0))
776 continue;
777 normal_entry = True;
780 l = strlen(n)+1;
782 /* Return value of vfs_readdirname has already gone through
783 unix_to_dos() */
785 /* If it's a vetoed file, pretend it doesn't even exist */
786 if (normal_entry && use_veto && conn && IS_VETO_PATH(conn, n))
787 continue;
789 /* Honour _hide unreadable_ option */
790 if (normal_entry && conn && lp_hideunreadable(SNUM(conn))) {
791 char *entry;
792 int ret=0;
794 if (asprintf(&entry, "%s/%s/%s", conn->origpath, name, n) > 0) {
795 ret = user_can_read_file(conn, entry);
796 SAFE_FREE(entry);
798 if (!ret)
799 continue;
802 if (used + l > dirp->mallocsize) {
803 int s = MAX(used+l,used+2000);
804 char *r;
805 r = (char *)Realloc(dirp->data,s);
806 if (!r) {
807 DEBUG(0,("Out of memory in OpenDir\n"));
808 break;
810 dirp->data = r;
811 dirp->mallocsize = s;
812 dirp->current = dirp->data;
814 pstrcpy(dirp->data+used,n);
815 used += l;
816 dirp->numentries++;
819 conn->vfs_ops.closedir(conn,p);
820 return((void *)dirp);
824 /*******************************************************************
825 Close a directory.
826 ********************************************************************/
828 void CloseDir(void *p)
830 Dir *dirp = (Dir *)p;
831 if (!dirp) return;
832 SAFE_FREE(dirp->data);
833 SAFE_FREE(dirp);
836 /*******************************************************************
837 Read from a directory.
838 ********************************************************************/
840 char *ReadDirName(void *p)
842 char *ret;
843 Dir *dirp = (Dir *)p;
845 if (!dirp || !dirp->current || dirp->pos >= dirp->numentries) return(NULL);
847 ret = dirp->current;
848 dirp->current = skip_string(dirp->current,1);
849 dirp->pos++;
851 return(ret);
855 /*******************************************************************
856 Seek a dir.
857 ********************************************************************/
859 BOOL SeekDir(void *p,int pos)
861 Dir *dirp = (Dir *)p;
863 if (!dirp) return(False);
865 if (pos < dirp->pos) {
866 dirp->current = dirp->data;
867 dirp->pos = 0;
870 while (dirp->pos < pos && ReadDirName(p)) ;
872 return(dirp->pos == pos);
875 /*******************************************************************
876 Tell a dir position.
877 ********************************************************************/
879 int TellDir(void *p)
881 Dir *dirp = (Dir *)p;
883 if (!dirp) return(-1);
885 return(dirp->pos);
888 /*******************************************************************************
889 This section manages a global directory cache.
890 (It should probably be split into a separate module. crh)
891 ********************************************************************************/
893 typedef struct {
894 ubi_dlNode node;
895 char *path;
896 char *name;
897 char *dname;
898 int snum;
899 } dir_cache_entry;
901 static ubi_dlNewList( dir_cache );
903 /*****************************************************************************
904 Add an entry to the directory cache.
905 Input: path -
906 name -
907 dname -
908 snum -
909 Output: None.
910 *****************************************************************************/
912 void DirCacheAdd( char *path, char *name, char *dname, int snum )
914 int pathlen;
915 int namelen;
916 dir_cache_entry *entry;
918 /* Allocate the structure & string space in one go so that it can be freed
919 * in one call to free().
921 pathlen = strlen( path ) +1; /* Bytes required to store path (with nul). */
922 namelen = strlen( name ) +1; /* Bytes required to store name (with nul). */
923 entry = (dir_cache_entry *)malloc( sizeof( dir_cache_entry )
924 + pathlen
925 + namelen
926 + strlen( dname ) +1 );
927 if( NULL == entry ) /* Not adding to the cache is not fatal, */
928 return; /* so just return as if nothing happened. */
930 /* Set pointers correctly and load values. */
931 entry->path = memcpy( (char *)&entry[1], path, strlen(path)+1 );
932 entry->name = memcpy( &(entry->path[pathlen]), name, strlen(name)+1 );
933 entry->dname = memcpy( &(entry->name[namelen]), dname, strlen(dname)+1 );
934 entry->snum = snum;
936 /* Add the new entry to the linked list. */
937 (void)ubi_dlAddHead( dir_cache, entry );
938 DEBUG( 4, ("Added dir cache entry %s %s -> %s\n", path, name, dname ) );
940 /* Free excess cache entries. */
941 while( DIRCACHESIZE < dir_cache->count )
942 safe_free( ubi_dlRemTail( dir_cache ) );
946 /*****************************************************************************
947 Search for an entry to the directory cache.
948 Input: path -
949 name -
950 snum -
951 Output: The dname string of the located entry, or NULL if the entry was
952 not found.
954 Notes: This uses a linear search, which is is okay because of
955 the small size of the cache. Use a splay tree or hash
956 for large caches.
957 *****************************************************************************/
959 char *DirCacheCheck( char *path, char *name, int snum )
961 dir_cache_entry *entry;
963 for( entry = (dir_cache_entry *)ubi_dlFirst( dir_cache );
964 NULL != entry;
965 entry = (dir_cache_entry *)ubi_dlNext( entry ) )
967 if( entry->snum == snum
968 && entry->name && 0 == strcmp( name, entry->name )
969 && entry->path && 0 == strcmp( path, entry->path ) )
971 DEBUG(4, ("Got dir cache hit on %s %s -> %s\n",path,name,entry->dname));
972 return( entry->dname );
976 return(NULL);
979 /*****************************************************************************
980 Remove all cache entries which have an snum that matches the input.
981 Input: snum -
982 Output: None.
983 *****************************************************************************/
985 void DirCacheFlush(int snum)
987 dir_cache_entry *entry;
988 ubi_dlNodePtr next;
990 for(entry = (dir_cache_entry *)ubi_dlFirst( dir_cache );
991 NULL != entry; ) {
992 next = ubi_dlNext( entry );
993 if( entry->snum == snum )
994 safe_free( ubi_dlRemThis( dir_cache, entry ) );
995 entry = (dir_cache_entry *)next;