Fix spurious check of unix home directory during session_setup when add user script...
[Samba.git] / source / smbd / dir.c
blob34aec3dd69df2cd210e5a3b217355a2fe43ca960
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 const char *dir2;
335 DEBUG(5,("start_dir dir=%s\n",directory));
337 if (!check_name(directory,conn))
338 return(False);
340 if (! *directory)
341 dir2 = ".";
342 else
343 dir2 = directory;
345 conn->dirptr = OpenDir(conn, dir2, True);
346 if (conn->dirptr) {
347 dptrs_open++;
348 string_set(&conn->dirpath,dir2);
349 return(True);
352 return(False);
355 /****************************************************************************
356 Try and close the oldest handle not marked for
357 expect close in the hope that the client has
358 finished with that one.
359 ****************************************************************************/
361 static void dptr_close_oldest(BOOL old)
363 dptr_struct *dptr;
366 * Go to the end of the list.
368 for(dptr = dirptrs; dptr && dptr->next; dptr = dptr->next)
371 if(!dptr) {
372 DEBUG(0,("No old dptrs available to close oldest ?\n"));
373 return;
377 * If 'old' is true, close the oldest oldhandle dnum (ie. 1 < dnum < 256) that
378 * does not have expect_close set. If 'old' is false, close
379 * one of the new dnum handles.
382 for(; dptr; dptr = dptr->prev) {
383 if ((old && (dptr->dnum < 256) && !dptr->expect_close) ||
384 (!old && (dptr->dnum > 255))) {
385 dptr_close_internal(dptr);
386 return;
391 /****************************************************************************
392 Create a new dir ptr. If the flag old_handle is true then we must allocate
393 from the bitmap range 0 - 255 as old SMBsearch directory handles are only
394 one byte long. If old_handle is false we allocate from the range
395 256 - MAX_DIRECTORY_HANDLES. We bias the number we return by 1 to ensure
396 a directory handle is never zero. All the above is folklore taught to
397 me at Andrew's knee.... :-) :-). JRA.
398 ****************************************************************************/
400 int dptr_create(connection_struct *conn,char *path, BOOL old_handle, BOOL expect_close,uint16 spid)
402 dptr_struct *dptr;
404 if (!start_dir(conn,path))
405 return(-2); /* Code to say use a unix error return code. */
407 if (dptrs_open >= MAX_OPEN_DIRECTORIES)
408 dptr_idleoldest();
410 dptr = (dptr_struct *)malloc(sizeof(dptr_struct));
411 if(!dptr) {
412 DEBUG(0,("malloc fail in dptr_create.\n"));
413 return -1;
416 ZERO_STRUCTP(dptr);
418 if(old_handle) {
421 * This is an old-style SMBsearch request. Ensure the
422 * value we return will fit in the range 1-255.
425 dptr->dnum = bitmap_find(dptr_bmap, 0);
427 if(dptr->dnum == -1 || dptr->dnum > 254) {
430 * Try and close the oldest handle not marked for
431 * expect close in the hope that the client has
432 * finished with that one.
435 dptr_close_oldest(True);
437 /* Now try again... */
438 dptr->dnum = bitmap_find(dptr_bmap, 0);
440 if(dptr->dnum == -1 || dptr->dnum > 254) {
441 DEBUG(0,("dptr_create: returned %d: Error - all old dirptrs in use ?\n", dptr->dnum));
442 SAFE_FREE(dptr);
443 return -1;
446 } else {
449 * This is a new-style trans2 request. Allocate from
450 * a range that will return 256 - MAX_DIRECTORY_HANDLES.
453 dptr->dnum = bitmap_find(dptr_bmap, 255);
455 if(dptr->dnum == -1 || dptr->dnum < 255) {
458 * Try and close the oldest handle close in the hope that
459 * the client has finished with that one. This will only
460 * happen in the case of the Win98 client bug where it leaks
461 * directory handles.
464 dptr_close_oldest(False);
466 /* Now try again... */
467 dptr->dnum = bitmap_find(dptr_bmap, 255);
469 if(dptr->dnum == -1 || dptr->dnum < 255) {
470 DEBUG(0,("dptr_create: returned %d: Error - all new dirptrs in use ?\n", dptr->dnum));
471 SAFE_FREE(dptr);
472 return -1;
477 bitmap_set(dptr_bmap, dptr->dnum);
479 dptr->dnum += 1; /* Always bias the dnum by one - no zero dnums allowed. */
481 dptr->ptr = conn->dirptr;
482 string_set(&dptr->path,path);
483 dptr->conn = conn;
484 dptr->spid = spid;
485 dptr->expect_close = expect_close;
486 dptr->wcard = NULL; /* Only used in lanman2 searches */
487 dptr->attr = 0; /* Only used in lanman2 searches */
489 DLIST_ADD(dirptrs, dptr);
491 DEBUG(3,("creating new dirptr %d for path %s, expect_close = %d\n",
492 dptr->dnum,path,expect_close));
494 return(dptr->dnum);
497 /****************************************************************************
498 Fill the 5 byte server reserved dptr field.
499 ****************************************************************************/
501 BOOL dptr_fill(char *buf1,unsigned int key)
503 unsigned char *buf = (unsigned char *)buf1;
504 void *p = dptr_ptr(key);
505 uint32 offset;
506 if (!p) {
507 DEBUG(1,("filling null dirptr %d\n",key));
508 return(False);
510 offset = TellDir(p);
511 DEBUG(6,("fill on key %u dirptr 0x%lx now at %d\n",key,
512 (long)p,(int)offset));
513 buf[0] = key;
514 SIVAL(buf,1,offset | DPTR_MASK);
515 return(True);
518 /****************************************************************************
519 Fetch the dir ptr and seek it given the 5 byte server field.
520 ****************************************************************************/
522 void *dptr_fetch(char *buf,int *num)
524 unsigned int key = *(unsigned char *)buf;
525 void *p = dptr_ptr(key);
526 uint32 offset;
527 if (!p) {
528 DEBUG(3,("fetched null dirptr %d\n",key));
529 return(NULL);
531 *num = key;
532 offset = IVAL(buf,1)&~DPTR_MASK;
533 SeekDir(p,offset);
534 DEBUG(3,("fetching dirptr %d for path %s at offset %d\n",
535 key,dptr_path(key),offset));
536 return(p);
539 /****************************************************************************
540 Fetch the dir ptr.
541 ****************************************************************************/
543 void *dptr_fetch_lanman2(int dptr_num)
545 void *p = dptr_ptr(dptr_num);
547 if (!p) {
548 DEBUG(3,("fetched null dirptr %d\n",dptr_num));
549 return(NULL);
551 DEBUG(3,("fetching dirptr %d for path %s\n",dptr_num,dptr_path(dptr_num)));
552 return(p);
555 /****************************************************************************
556 Check a filetype for being valid.
557 ****************************************************************************/
559 BOOL dir_check_ftype(connection_struct *conn,int mode,SMB_STRUCT_STAT *st,int dirtype)
561 int mask;
563 /* Check the "may have" search bits. */
564 if (((mode & ~dirtype) & (aHIDDEN | aSYSTEM | aDIR)) != 0)
565 return False;
567 /* Check the "must have" bits, which are the may have bits shifted eight */
568 /* If must have bit is set, the file/dir can not be returned in search unless the matching
569 file attribute is set */
570 mask = ((dirtype >> 8) & (aDIR|aARCH|aRONLY|aHIDDEN|aSYSTEM)); /* & 0x37 */
571 if(mask) {
572 if((mask & (mode & (aDIR|aARCH|aRONLY|aHIDDEN|aSYSTEM))) == mask) /* check if matching attribute present */
573 return True;
574 else
575 return False;
578 return True;
581 static BOOL mangle_mask_match(connection_struct *conn, char *filename, char *mask)
583 mangle_map(filename,True,False,SNUM(conn));
584 return mask_match(filename,mask,False);
587 /****************************************************************************
588 Get an 8.3 directory entry.
589 ****************************************************************************/
591 BOOL get_dir_entry(connection_struct *conn,char *mask,int dirtype,char *fname,
592 SMB_OFF_T *size,int *mode,time_t *date,BOOL check_descend)
594 char *dname;
595 BOOL found = False;
596 SMB_STRUCT_STAT sbuf;
597 pstring path;
598 pstring pathreal;
599 BOOL isrootdir;
600 pstring filename;
601 BOOL needslash;
603 *path = *pathreal = *filename = 0;
605 isrootdir = (strequal(conn->dirpath,"./") ||
606 strequal(conn->dirpath,".") ||
607 strequal(conn->dirpath,"/"));
609 needslash = ( conn->dirpath[strlen(conn->dirpath) -1] != '/');
611 if (!conn->dirptr)
612 return(False);
614 while (!found)
616 dname = ReadDirName(conn->dirptr);
618 DEBUG(6,("readdir on dirptr 0x%lx now at offset %d\n",
619 (long)conn->dirptr,TellDir(conn->dirptr)));
621 if (dname == NULL)
622 return(False);
624 pstrcpy(filename,dname);
626 /* notice the special *.* handling. This appears to be the only difference
627 between the wildcard handling in this routine and in the trans2 routines.
628 see masktest for a demo
630 if ((strcmp(mask,"*.*") == 0) ||
631 mask_match(filename,mask,False) ||
632 mangle_mask_match(conn,filename,mask))
634 if (isrootdir && (strequal(filename,"..") || strequal(filename,".")))
635 continue;
637 if (!mangle_is_8_3(filename, False)) {
638 mangle_map(filename,True,False,SNUM(conn));
641 pstrcpy(fname,filename);
642 *path = 0;
643 pstrcpy(path,conn->dirpath);
644 if(needslash)
645 pstrcat(path,"/");
646 pstrcpy(pathreal,path);
647 pstrcat(path,fname);
648 pstrcat(pathreal,dname);
649 if (conn->vfs_ops.stat(conn,dos_to_unix_static(pathreal), &sbuf) != 0)
651 DEBUG(5,("Couldn't stat 1 [%s]. Error = %s\n",path, strerror(errno) ));
652 continue;
655 *mode = dos_mode(conn,pathreal,&sbuf);
657 if (!dir_check_ftype(conn,*mode,&sbuf,dirtype))
659 DEBUG(5,("[%s] attribs didn't match %x\n",filename,dirtype));
660 continue;
663 *size = sbuf.st_size;
664 *date = sbuf.st_mtime;
666 DEBUG(3,("get_dir_entry mask=[%s] found %s fname=%s\n",mask, pathreal,fname));
668 found = True;
672 return(found);
677 typedef struct
679 int pos;
680 int numentries;
681 int mallocsize;
682 char *data;
683 char *current;
684 } Dir;
688 /*******************************************************************
689 check to see if a user can read a file. This is only approximate,
690 it is used as part of the "hide unreadable" option. Don't
691 use it for anything security sensitive
692 ********************************************************************/
694 static BOOL user_can_read_file(connection_struct *conn, char *name)
696 extern struct current_user current_user;
697 SMB_STRUCT_STAT ste;
698 SEC_DESC *psd = NULL;
699 size_t sd_size;
700 files_struct *fsp;
701 int smb_action;
702 NTSTATUS status;
703 uint32 access_granted;
705 ZERO_STRUCT(ste);
708 * If user is a member of the Admin group
709 * we never hide files from them.
712 if (conn->admin_user)
713 return True;
715 /* If we can't stat it does not show it */
716 if (vfs_stat(conn, name, &ste) != 0)
717 return False;
719 /* Pseudo-open the file (note - no fd's created). */
721 if(S_ISDIR(ste.st_mode))
722 fsp = open_directory(conn, name, &ste, 0, SET_DENY_MODE(DENY_NONE), (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_OPEN),
723 unix_mode(conn,aRONLY|aDIR, name), &smb_action);
724 else
725 fsp = open_file_stat(conn, name, &ste);
727 if (!fsp)
728 return False;
730 /* Get NT ACL -allocated in main loop talloc context. No free needed here. */
731 sd_size = conn->vfs_ops.fget_nt_acl(fsp, fsp->fd, &psd);
732 close_file(fsp, True);
734 /* No access if SD get failed. */
735 if (!sd_size)
736 return False;
738 return se_access_check(psd, current_user.nt_user_token, FILE_READ_DATA,
739 &access_granted, &status);
742 /*******************************************************************
743 Open a directory.
744 ********************************************************************/
746 void *OpenDir(connection_struct *conn, const char *name, BOOL use_veto)
748 Dir *dirp;
749 const char *n;
750 DIR *p = conn->vfs_ops.opendir(conn,dos_to_unix_static(name));
751 int used=0;
753 if (!p)
754 return(NULL);
755 dirp = (Dir *)malloc(sizeof(Dir));
756 if (!dirp) {
757 DEBUG(0,("Out of memory in OpenDir\n"));
758 conn->vfs_ops.closedir(conn,p);
759 return(NULL);
762 dirp->pos = dirp->numentries = dirp->mallocsize = 0;
763 dirp->data = dirp->current = NULL;
765 while (True) {
766 int l;
767 BOOL normal_entry = True;
769 if (used == 0) {
770 n = ".";
771 normal_entry = False;
772 } else if (used == 2) {
773 n = "..";
774 normal_entry = False;
775 } else {
776 n = vfs_readdirname(conn, p);
777 if (n == NULL)
778 break;
779 if ((strcmp(".",n) == 0) ||(strcmp("..",n) == 0))
780 continue;
781 normal_entry = True;
784 l = strlen(n)+1;
786 /* Return value of vfs_readdirname has already gone through
787 unix_to_dos() */
789 /* If it's a vetoed file, pretend it doesn't even exist */
790 if (normal_entry && use_veto && conn && IS_VETO_PATH(conn, n))
791 continue;
793 /* Honour _hide unreadable_ option */
794 if (normal_entry && conn && lp_hideunreadable(SNUM(conn))) {
795 char *entry;
796 int ret=0;
798 if (asprintf(&entry, "%s/%s/%s", conn->origpath, name, n) > 0) {
799 ret = user_can_read_file(conn, entry);
800 SAFE_FREE(entry);
802 if (!ret)
803 continue;
806 if (used + l > dirp->mallocsize) {
807 int s = MAX(used+l,used+2000);
808 char *r;
809 r = (char *)Realloc(dirp->data,s);
810 if (!r) {
811 DEBUG(0,("Out of memory in OpenDir\n"));
812 break;
814 dirp->data = r;
815 dirp->mallocsize = s;
816 dirp->current = dirp->data;
818 pstrcpy(dirp->data+used,n);
819 used += l;
820 dirp->numentries++;
823 conn->vfs_ops.closedir(conn,p);
824 return((void *)dirp);
828 /*******************************************************************
829 Close a directory.
830 ********************************************************************/
832 void CloseDir(void *p)
834 Dir *dirp = (Dir *)p;
835 if (!dirp) return;
836 SAFE_FREE(dirp->data);
837 SAFE_FREE(dirp);
840 /*******************************************************************
841 Read from a directory.
842 ********************************************************************/
844 char *ReadDirName(void *p)
846 char *ret;
847 Dir *dirp = (Dir *)p;
849 if (!dirp || !dirp->current || dirp->pos >= dirp->numentries) return(NULL);
851 ret = dirp->current;
852 dirp->current = skip_string(dirp->current,1);
853 dirp->pos++;
855 return(ret);
859 /*******************************************************************
860 Seek a dir.
861 ********************************************************************/
863 BOOL SeekDir(void *p,int pos)
865 Dir *dirp = (Dir *)p;
867 if (!dirp) return(False);
869 if (pos < dirp->pos) {
870 dirp->current = dirp->data;
871 dirp->pos = 0;
874 while (dirp->pos < pos && ReadDirName(p)) ;
876 return(dirp->pos == pos);
879 /*******************************************************************
880 Tell a dir position.
881 ********************************************************************/
883 int TellDir(void *p)
885 Dir *dirp = (Dir *)p;
887 if (!dirp) return(-1);
889 return(dirp->pos);
892 /*******************************************************************************
893 This section manages a global directory cache.
894 (It should probably be split into a separate module. crh)
895 ********************************************************************************/
897 typedef struct {
898 ubi_dlNode node;
899 char *path;
900 char *name;
901 char *dname;
902 int snum;
903 } dir_cache_entry;
905 static ubi_dlNewList( dir_cache );
907 /*****************************************************************************
908 Add an entry to the directory cache.
909 Input: path -
910 name -
911 dname -
912 snum -
913 Output: None.
914 *****************************************************************************/
916 void DirCacheAdd( const char *path, const char *name, const char *dname, int snum )
918 int pathlen;
919 int namelen;
920 dir_cache_entry *entry;
922 /* Allocate the structure & string space in one go so that it can be freed
923 * in one call to free().
925 pathlen = strlen( path ) +1; /* Bytes required to store path (with nul). */
926 namelen = strlen( name ) +1; /* Bytes required to store name (with nul). */
927 entry = (dir_cache_entry *)malloc( sizeof( dir_cache_entry )
928 + pathlen
929 + namelen
930 + strlen( dname ) +1 );
931 if( NULL == entry ) /* Not adding to the cache is not fatal, */
932 return; /* so just return as if nothing happened. */
934 /* Set pointers correctly and load values. */
935 entry->path = memcpy( (char *)&entry[1], path, strlen(path)+1 );
936 entry->name = memcpy( &(entry->path[pathlen]), name, strlen(name)+1 );
937 entry->dname = memcpy( &(entry->name[namelen]), dname, strlen(dname)+1 );
938 entry->snum = snum;
940 /* Add the new entry to the linked list. */
941 (void)ubi_dlAddHead( dir_cache, entry );
942 DEBUG( 4, ("Added dir cache entry %s %s -> %s\n", path, name, dname ) );
944 /* Free excess cache entries. */
945 while( DIRCACHESIZE < dir_cache->count )
946 safe_free( ubi_dlRemTail( dir_cache ) );
950 /*****************************************************************************
951 Search for an entry to the directory cache.
952 Input: path -
953 name -
954 snum -
955 Output: The dname string of the located entry, or NULL if the entry was
956 not found.
958 Notes: This uses a linear search, which is is okay because of
959 the small size of the cache. Use a splay tree or hash
960 for large caches.
961 *****************************************************************************/
963 char *DirCacheCheck( const char *path, const char *name, int snum )
965 dir_cache_entry *entry;
967 for( entry = (dir_cache_entry *)ubi_dlFirst( dir_cache );
968 NULL != entry;
969 entry = (dir_cache_entry *)ubi_dlNext( entry ) )
971 if( entry->snum == snum
972 && entry->name && 0 == strcmp( name, entry->name )
973 && entry->path && 0 == strcmp( path, entry->path ) )
975 DEBUG(4, ("Got dir cache hit on %s %s -> %s\n",path,name,entry->dname));
976 return( entry->dname );
980 return(NULL);
983 /*****************************************************************************
984 Remove all cache entries which have an snum that matches the input.
985 Input: snum -
986 Output: None.
987 *****************************************************************************/
989 void DirCacheFlush(int snum)
991 dir_cache_entry *entry;
992 ubi_dlNodePtr next;
994 for(entry = (dir_cache_entry *)ubi_dlFirst( dir_cache );
995 NULL != entry; ) {
996 next = ubi_dlNext( entry );
997 if( entry->snum == snum )
998 safe_free( ubi_dlRemThis( dir_cache, entry ) );
999 entry = (dir_cache_entry *)next;