add WITH_SENDFILE profiling data (from Pierre Belanger)
[Samba.git] / source / smbd / dir.c
blob3d77fd64ce416f84ee4b791baf7ce55a37aa37c3
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 int access_mode;
699 NTSTATUS status;
700 uint32 access_granted;
702 ZERO_STRUCT(ste);
705 * If user is a member of the Admin group
706 * we never hide files from them.
709 if (conn->admin_user)
710 return True;
712 /* If we can't stat it does not show it */
713 if (vfs_stat(conn, name, &ste) != 0)
714 return False;
716 /* Pseudo-open the file (note - no fd's created). */
718 if(S_ISDIR(ste.st_mode))
719 fsp = open_directory(conn, name, &ste, 0, SET_DENY_MODE(DENY_NONE), (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_OPEN),
720 unix_mode(conn,aRONLY|aDIR, name), &smb_action);
721 else
722 fsp = open_file_shared1(conn, name, &ste, FILE_READ_ATTRIBUTES, SET_DENY_MODE(DENY_NONE),
723 (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_OPEN), 0, 0, &access_mode, &smb_action);
725 if (!fsp)
726 return False;
728 /* Get NT ACL -allocated in main loop talloc context. No free needed here. */
729 sd_size = conn->vfs_ops.fget_nt_acl(fsp, fsp->fd, &psd);
730 close_file(fsp, False);
732 /* No access if SD get failed. */
733 if (!sd_size)
734 return False;
736 return se_access_check(psd, current_user.nt_user_token, FILE_READ_DATA,
737 &access_granted, &status);
740 /*******************************************************************
741 Open a directory.
742 ********************************************************************/
744 void *OpenDir(connection_struct *conn, char *name, BOOL use_veto)
746 Dir *dirp;
747 char *n;
748 DIR *p = conn->vfs_ops.opendir(conn,dos_to_unix_static(name));
749 int used=0;
751 if (!p)
752 return(NULL);
753 dirp = (Dir *)malloc(sizeof(Dir));
754 if (!dirp) {
755 DEBUG(0,("Out of memory in OpenDir\n"));
756 conn->vfs_ops.closedir(conn,p);
757 return(NULL);
760 dirp->pos = dirp->numentries = dirp->mallocsize = 0;
761 dirp->data = dirp->current = NULL;
763 while (True) {
764 int l;
765 BOOL normal_entry = True;
767 if (used == 0) {
768 n = ".";
769 normal_entry = False;
770 } else if (used == 2) {
771 n = "..";
772 normal_entry = False;
773 } else {
774 n = vfs_readdirname(conn, p);
775 if (n == NULL)
776 break;
777 if ((strcmp(".",n) == 0) ||(strcmp("..",n) == 0))
778 continue;
779 normal_entry = True;
782 l = strlen(n)+1;
784 /* Return value of vfs_readdirname has already gone through
785 unix_to_dos() */
787 /* If it's a vetoed file, pretend it doesn't even exist */
788 if (normal_entry && use_veto && conn && IS_VETO_PATH(conn, n))
789 continue;
791 /* Honour _hide unreadable_ option */
792 if (normal_entry && conn && lp_hideunreadable(SNUM(conn))) {
793 char *entry;
794 int ret=0;
796 if (asprintf(&entry, "%s/%s/%s", conn->origpath, name, n) > 0) {
797 ret = user_can_read_file(conn, entry);
798 SAFE_FREE(entry);
800 if (!ret)
801 continue;
804 if (used + l > dirp->mallocsize) {
805 int s = MAX(used+l,used+2000);
806 char *r;
807 r = (char *)Realloc(dirp->data,s);
808 if (!r) {
809 DEBUG(0,("Out of memory in OpenDir\n"));
810 break;
812 dirp->data = r;
813 dirp->mallocsize = s;
814 dirp->current = dirp->data;
816 pstrcpy(dirp->data+used,n);
817 used += l;
818 dirp->numentries++;
821 conn->vfs_ops.closedir(conn,p);
822 return((void *)dirp);
826 /*******************************************************************
827 Close a directory.
828 ********************************************************************/
830 void CloseDir(void *p)
832 Dir *dirp = (Dir *)p;
833 if (!dirp) return;
834 SAFE_FREE(dirp->data);
835 SAFE_FREE(dirp);
838 /*******************************************************************
839 Read from a directory.
840 ********************************************************************/
842 char *ReadDirName(void *p)
844 char *ret;
845 Dir *dirp = (Dir *)p;
847 if (!dirp || !dirp->current || dirp->pos >= dirp->numentries) return(NULL);
849 ret = dirp->current;
850 dirp->current = skip_string(dirp->current,1);
851 dirp->pos++;
853 return(ret);
857 /*******************************************************************
858 Seek a dir.
859 ********************************************************************/
861 BOOL SeekDir(void *p,int pos)
863 Dir *dirp = (Dir *)p;
865 if (!dirp) return(False);
867 if (pos < dirp->pos) {
868 dirp->current = dirp->data;
869 dirp->pos = 0;
872 while (dirp->pos < pos && ReadDirName(p)) ;
874 return(dirp->pos == pos);
877 /*******************************************************************
878 Tell a dir position.
879 ********************************************************************/
881 int TellDir(void *p)
883 Dir *dirp = (Dir *)p;
885 if (!dirp) return(-1);
887 return(dirp->pos);
890 /*******************************************************************************
891 This section manages a global directory cache.
892 (It should probably be split into a separate module. crh)
893 ********************************************************************************/
895 typedef struct {
896 ubi_dlNode node;
897 char *path;
898 char *name;
899 char *dname;
900 int snum;
901 } dir_cache_entry;
903 static ubi_dlNewList( dir_cache );
905 /*****************************************************************************
906 Add an entry to the directory cache.
907 Input: path -
908 name -
909 dname -
910 snum -
911 Output: None.
912 *****************************************************************************/
914 void DirCacheAdd( char *path, char *name, char *dname, int snum )
916 int pathlen;
917 int namelen;
918 dir_cache_entry *entry;
920 /* Allocate the structure & string space in one go so that it can be freed
921 * in one call to free().
923 pathlen = strlen( path ) +1; /* Bytes required to store path (with nul). */
924 namelen = strlen( name ) +1; /* Bytes required to store name (with nul). */
925 entry = (dir_cache_entry *)malloc( sizeof( dir_cache_entry )
926 + pathlen
927 + namelen
928 + strlen( dname ) +1 );
929 if( NULL == entry ) /* Not adding to the cache is not fatal, */
930 return; /* so just return as if nothing happened. */
932 /* Set pointers correctly and load values. */
933 entry->path = pstrcpy( (char *)&entry[1], path);
934 entry->name = pstrcpy( &(entry->path[pathlen]), name);
935 entry->dname = pstrcpy( &(entry->name[namelen]), dname);
936 entry->snum = snum;
938 /* Add the new entry to the linked list. */
939 (void)ubi_dlAddHead( dir_cache, entry );
940 DEBUG( 4, ("Added dir cache entry %s %s -> %s\n", path, name, dname ) );
942 /* Free excess cache entries. */
943 while( DIRCACHESIZE < dir_cache->count )
944 safe_free( ubi_dlRemTail( dir_cache ) );
948 /*****************************************************************************
949 Search for an entry to the directory cache.
950 Input: path -
951 name -
952 snum -
953 Output: The dname string of the located entry, or NULL if the entry was
954 not found.
956 Notes: This uses a linear search, which is is okay because of
957 the small size of the cache. Use a splay tree or hash
958 for large caches.
959 *****************************************************************************/
961 char *DirCacheCheck( char *path, char *name, int snum )
963 dir_cache_entry *entry;
965 for( entry = (dir_cache_entry *)ubi_dlFirst( dir_cache );
966 NULL != entry;
967 entry = (dir_cache_entry *)ubi_dlNext( entry ) )
969 if( entry->snum == snum
970 && 0 == strcmp( name, entry->name )
971 && 0 == strcmp( path, entry->path ) )
973 DEBUG(4, ("Got dir cache hit on %s %s -> %s\n",path,name,entry->dname));
974 return( entry->dname );
978 return(NULL);
981 /*****************************************************************************
982 Remove all cache entries which have an snum that matches the input.
983 Input: snum -
984 Output: None.
985 *****************************************************************************/
987 void DirCacheFlush(int snum)
989 dir_cache_entry *entry;
990 ubi_dlNodePtr next;
992 for(entry = (dir_cache_entry *)ubi_dlFirst( dir_cache );
993 NULL != entry; ) {
994 next = ubi_dlNext( entry );
995 if( entry->snum == snum )
996 safe_free( ubi_dlRemThis( dir_cache, entry ) );
997 entry = (dir_cache_entry *)next;