#include "includes.h"
extern int DEBUGLEVEL;
-extern connection_struct Connections[];
/*
This module implements directory related functions for Samba.
-uint32 dircounter = 0;
+static uint32 dircounter = 0;
#define NUMDIRPTRS 256
-static struct dptr_struct
-{
- int pid;
- int cnum;
- uint32 lastused;
- void *ptr;
- BOOL valid;
- BOOL finished;
- BOOL expect_close;
- char *wcard; /* Field only used for lanman2 trans2_findfirst/next searches */
- uint16 attr; /* Field only used for lanman2 trans2_findfirst/next searches */
- char *path;
+static struct dptr_struct {
+ int pid;
+ connection_struct *conn;
+ uint32 lastused;
+ void *ptr;
+ BOOL valid;
+ BOOL finished;
+ BOOL expect_close;
+ char *wcard; /* Field only used for trans2_ searches */
+ uint16 attr; /* Field only used for trans2_ searches */
+ char *path;
}
dirptrs[NUMDIRPTRS];
****************************************************************************/
static void *dptr_get(int key,uint32 lastused)
{
- struct dptr_struct *dp = &dirptrs[key];
-
- if (dp->valid) {
- if (lastused) dp->lastused = lastused;
- if (!dp->ptr) {
- if (dptrs_open >= MAXDIR)
- dptr_idleoldest();
- DEBUG(4,("Reopening dptr key %d\n",key));
- if ((dp->ptr = OpenDir(dp->cnum, dp->path, True)))
- dptrs_open++;
- }
- return(dp->ptr);
- }
- return(NULL);
+ struct dptr_struct *dp = &dirptrs[key];
+
+ if (dp->valid) {
+ if (lastused) dp->lastused = lastused;
+ if (!dp->ptr) {
+ if (dptrs_open >= MAX_OPEN_DIRECTORIES)
+ dptr_idleoldest();
+ DEBUG(4,("Reopening dptr key %d\n",key));
+ if ((dp->ptr = OpenDir(dp->conn, dp->path, True)))
+ dptrs_open++;
+ }
+ return(dp->ptr);
+ }
+ return(NULL);
}
/****************************************************************************
/****************************************************************************
close all dptrs for a cnum
****************************************************************************/
-void dptr_closecnum(int cnum)
+void dptr_closecnum(connection_struct *conn)
{
int i;
for (i=0;i<NUMDIRPTRS;i++)
- if (dirptrs[i].valid && dirptrs[i].cnum == cnum)
+ if (dirptrs[i].valid && dirptrs[i].conn == conn)
dptr_close(i);
}
/****************************************************************************
idle all dptrs for a cnum
****************************************************************************/
-void dptr_idlecnum(int cnum)
+void dptr_idlecnum(connection_struct *conn)
{
int i;
for (i=0;i<NUMDIRPTRS;i++)
- if (dirptrs[i].valid && dirptrs[i].cnum == cnum && dirptrs[i].ptr)
+ if (dirptrs[i].valid && dirptrs[i].conn == conn && dirptrs[i].ptr)
dptr_idle(i);
}
/****************************************************************************
start a directory listing
****************************************************************************/
-static BOOL start_dir(int cnum,char *directory)
+static BOOL start_dir(connection_struct *conn,char *directory)
{
- DEBUG(5,("start_dir cnum=%d dir=%s\n",cnum,directory));
+ DEBUG(5,("start_dir dir=%s\n",directory));
- if (!check_name(directory,cnum))
- return(False);
+ if (!check_name(directory,conn))
+ return(False);
- if (! *directory)
- directory = ".";
-
- Connections[cnum].dirptr = OpenDir(cnum, directory, True);
- if (Connections[cnum].dirptr) {
- dptrs_open++;
- string_set(&Connections[cnum].dirpath,directory);
- return(True);
- }
+ if (! *directory)
+ directory = ".";
+
+ conn->dirptr = OpenDir(conn, directory, True);
+ if (conn->dirptr) {
+ dptrs_open++;
+ string_set(&conn->dirpath,directory);
+ return(True);
+ }
- return(False);
+ return(False);
}
/****************************************************************************
create a new dir ptr
****************************************************************************/
-int dptr_create(int cnum,char *path, BOOL expect_close,int pid)
+int dptr_create(connection_struct *conn,char *path, BOOL expect_close,int pid)
{
int i;
uint32 old;
int oldi;
- if (!start_dir(cnum,path))
+ if (!start_dir(conn,path))
return(-2); /* Code to say use a unix error return code. */
- if (dptrs_open >= MAXDIR)
+ if (dptrs_open >= MAX_OPEN_DIRECTORIES)
dptr_idleoldest();
for (i=0;i<NUMDIRPTRS;i++)
if (dirptrs[i].valid)
dptr_close(i);
- dirptrs[i].ptr = Connections[cnum].dirptr;
+ dirptrs[i].ptr = conn->dirptr;
string_set(&dirptrs[i].path,path);
dirptrs[i].lastused = dircounter++;
dirptrs[i].finished = False;
- dirptrs[i].cnum = cnum;
+ dirptrs[i].conn = conn;
dirptrs[i].pid = pid;
dirptrs[i].expect_close = expect_close;
dirptrs[i].wcard = NULL; /* Only used in lanman2 searches */
return(False);
}
offset = TellDir(p);
- DEBUG(6,("fill on key %d dirptr 0x%x now at %d\n",key,p,offset));
+ DEBUG(6,("fill on key %ld dirptr 0x%lx now at %d\n",key,
+ (long)p,(int)offset));
buf[0] = key;
SIVAL(buf,1,offset | DPTR_MASK);
return(True);
/****************************************************************************
check a filetype for being valid
****************************************************************************/
-BOOL dir_check_ftype(int cnum,int mode,struct stat *st,int dirtype)
+BOOL dir_check_ftype(connection_struct *conn,int mode,SMB_STRUCT_STAT *st,int dirtype)
{
if (((mode & ~dirtype) & (aHIDDEN | aSYSTEM | aDIR)) != 0)
return False;
/****************************************************************************
get a directory entry
****************************************************************************/
-BOOL get_dir_entry(int cnum,char *mask,int dirtype,char *fname,int *size,int *mode,time_t *date,BOOL check_descend)
+BOOL get_dir_entry(connection_struct *conn,char *mask,int dirtype,char *fname,
+ SMB_OFF_T *size,int *mode,time_t *date,BOOL check_descend)
{
char *dname;
BOOL found = False;
- struct stat sbuf;
+ SMB_STRUCT_STAT sbuf;
pstring path;
pstring pathreal;
BOOL isrootdir;
pstring filename;
- BOOL matched;
BOOL needslash;
*path = *pathreal = *filename = 0;
- isrootdir = (strequal(Connections[cnum].dirpath,"./") ||
- strequal(Connections[cnum].dirpath,".") ||
- strequal(Connections[cnum].dirpath,"/"));
+ isrootdir = (strequal(conn->dirpath,"./") ||
+ strequal(conn->dirpath,".") ||
+ strequal(conn->dirpath,"/"));
needslash =
- ( Connections[cnum].dirpath[strlen(Connections[cnum].dirpath) -1] != '/');
+ ( conn->dirpath[strlen(conn->dirpath) -1] != '/');
- if (!Connections[cnum].dirptr)
+ if (!conn->dirptr)
return(False);
while (!found)
{
- dname = ReadDirName(Connections[cnum].dirptr);
+ dname = ReadDirName(conn->dirptr);
- DEBUG(6,("readdir on dirptr 0x%x now at offset %d\n",
- Connections[cnum].dirptr,TellDir(Connections[cnum].dirptr)));
+ DEBUG(6,("readdir on dirptr 0x%lx now at offset %d\n",
+ (long)conn->dirptr,TellDir(conn->dirptr)));
if (dname == NULL)
return(False);
- matched = False;
-
pstrcpy(filename,dname);
if ((strcmp(filename,mask) == 0) ||
- (name_map_mangle(filename,True,SNUM(cnum)) &&
+ (name_map_mangle(filename,True,SNUM(conn)) &&
mask_match(filename,mask,False,False)))
{
if (isrootdir && (strequal(filename,"..") || strequal(filename,".")))
pstrcpy(fname,filename);
*path = 0;
- pstrcpy(path,Connections[cnum].dirpath);
+ pstrcpy(path,conn->dirpath);
if(needslash)
pstrcat(path,"/");
pstrcpy(pathreal,path);
pstrcat(path,fname);
pstrcat(pathreal,dname);
- if (sys_stat(pathreal,&sbuf) != 0)
+ if (dos_stat(pathreal,&sbuf) != 0)
{
DEBUG(5,("Couldn't stat 1 [%s]\n",path));
continue;
!strequal(fname,".") && !strequal(fname,".."))
continue;
- *mode = dos_mode(cnum,pathreal,&sbuf);
+ *mode = dos_mode(conn,pathreal,&sbuf);
- if (!dir_check_ftype(cnum,*mode,&sbuf,dirtype)) {
+ if (!dir_check_ftype(conn,*mode,&sbuf,dirtype)) {
DEBUG(5,("[%s] attribs didn't match %x\n",filename,dirtype));
continue;
}
/*******************************************************************
open a directory
********************************************************************/
-void *OpenDir(int cnum, char *name, BOOL use_veto)
+void *OpenDir(connection_struct *conn, char *name, BOOL use_veto)
{
Dir *dirp;
char *n;
- void *p = sys_opendir(name);
+ void *p = dos_opendir(name);
int used=0;
if (!p) return(NULL);
int l = strlen(n)+1;
/* If it's a vetoed file, pretend it doesn't even exist */
- if (use_veto && IS_VETO_PATH(cnum, n)) continue;
+ if (use_veto && conn && IS_VETO_PATH(conn, n)) continue;
if (used + l > dirp->mallocsize) {
int s = MAX(used+l,used+2000);
int snum;
} dir_cache_entry;
-static ubi_dlList dir_cache[1] = { { NULL, NULL, 0 } };
+static ubi_dlNewList( dir_cache );
void DirCacheAdd( char *path, char *name, char *dname, int snum )
/* ------------------------------------------------------------------------ **
return(NULL);
} /* DirCacheCheck */
-void DirCacheFlush( int snum )
+void DirCacheFlush(int snum)
/* ------------------------------------------------------------------------ **
* Remove all cache entries which have an snum that matches the input.
*
*
* ------------------------------------------------------------------------ **
*/
- {
- dir_cache_entry *entry;
- ubi_dlNodePtr next;
-
- for( entry = (dir_cache_entry *)ubi_dlFirst( dir_cache ); NULL != entry; )
- {
- next = ubi_dlNext( entry );
- if( entry->snum == snum )
- free( ubi_dlRemThis( dir_cache, entry ) );
- entry = (dir_cache_entry *)next;
- }
- } /* DirCacheFlush */
+{
+ dir_cache_entry *entry;
+ ubi_dlNodePtr next;
+
+ for(entry = (dir_cache_entry *)ubi_dlFirst( dir_cache );
+ NULL != entry; ) {
+ next = ubi_dlNext( entry );
+ if( entry->snum == snum )
+ free( ubi_dlRemThis( dir_cache, entry ) );
+ entry = (dir_cache_entry *)next;
+ }
+} /* DirCacheFlush */
/* -------------------------------------------------------------------------- **
* End of the section that manages the global directory cache.
*/
-#ifdef REPLACE_GETWD
-/* This is getcwd.c from bash. It is needed in Interactive UNIX. To
- * add support for another OS you need to determine which of the
- * conditional compilation macros you need to define. All the options
- * are defined for Interactive UNIX.
- */
-#ifdef ISC
-#define HAVE_UNISTD_H
-#define USGr3
-#define USG
-#endif
-
-#if defined (HAVE_UNISTD_H)
-# include <unistd.h>
-#endif
-
-#if defined (__STDC__)
-# define CONST const
-# define PTR void *
-#else /* !__STDC__ */
-# define CONST
-# define PTR char *
-#endif /* !__STDC__ */
-
-#if !defined (PATH_MAX)
-# if defined (MAXPATHLEN)
-# define PATH_MAX MAXPATHLEN
-# else /* !MAXPATHLEN */
-# define PATH_MAX 1024
-# endif /* !MAXPATHLEN */
-#endif /* !PATH_MAX */
-
-#if defined (_POSIX_VERSION) || defined (USGr3) || defined (HAVE_DIRENT_H)
-# if !defined (HAVE_DIRENT)
-# define HAVE_DIRENT
-# endif /* !HAVE_DIRENT */
-#endif /* _POSIX_VERSION || USGr3 || HAVE_DIRENT_H */
-
-#if defined (HAVE_DIRENT)
-# define D_NAMLEN(d) (strlen ((d)->d_name))
-#else
-# define D_NAMLEN(d) ((d)->d_namlen)
-#endif /* ! (_POSIX_VERSION || USGr3) */
-
-#if defined (USG) || defined (USGr3)
-# define d_fileno d_ino
-#endif
-
-#if !defined (alloca)
-extern char *alloca ();
-#endif /* alloca */
-
-/* Get the pathname of the current working directory,
- and put it in SIZE bytes of BUF. Returns NULL if the
- directory couldn't be determined or SIZE was too small.
- If successful, returns BUF. In GNU, if BUF is NULL,
- an array is allocated with `malloc'; the array is SIZE
- bytes long, unless SIZE <= 0, in which case it is as
- big as necessary. */
-#if defined (__STDC__)
-char *
-getcwd (char *buf, size_t size)
-#else /* !__STDC__ */
-char *
-getcwd (buf, size)
- char *buf;
- int size;
-#endif /* !__STDC__ */
-{
- static CONST char dots[]
- = "../../../../../../../../../../../../../../../../../../../../../../../\
-../../../../../../../../../../../../../../../../../../../../../../../../../../\
-../../../../../../../../../../../../../../../../../../../../../../../../../..";
- CONST char *dotp, *dotlist;
- size_t dotsize;
- dev_t rootdev, thisdev;
- ino_t rootino, thisino;
- char path[PATH_MAX + 1];
- register char *pathp;
- char *pathbuf;
- size_t pathsize;
- struct stat st;
-
- if (buf != NULL && size == 0)
- {
- errno = EINVAL;
- return ((char *)NULL);
- }
-
- pathsize = sizeof (path);
- pathp = &path[pathsize];
- *--pathp = '\0';
- pathbuf = path;
-
- if (stat (".", &st) < 0)
- return ((char *)NULL);
- thisdev = st.st_dev;
- thisino = st.st_ino;
-
- if (stat ("/", &st) < 0)
- return ((char *)NULL);
- rootdev = st.st_dev;
- rootino = st.st_ino;
-
- dotsize = sizeof (dots) - 1;
- dotp = &dots[sizeof (dots)];
- dotlist = dots;
- while (!(thisdev == rootdev && thisino == rootino))
- {
- register DIR *dirstream;
- register struct dirent *d;
- dev_t dotdev;
- ino_t dotino;
- char mount_point;
- int namlen;
-
- /* Look at the parent directory. */
- if (dotp == dotlist)
- {
- /* My, what a deep directory tree you have, Grandma. */
- char *new;
- if (dotlist == dots)
- {
- new = malloc (dotsize * 2 + 1);
- if (new == NULL)
- goto lose;
- memcpy (new, dots, dotsize);
- }
- else
- {
- new = realloc ((PTR) dotlist, dotsize * 2 + 1);
- if (new == NULL)
- goto lose;
- }
- memcpy (&new[dotsize], new, dotsize);
- dotp = &new[dotsize];
- dotsize *= 2;
- new[dotsize] = '\0';
- dotlist = new;
- }
-
- dotp -= 3;
-
- /* Figure out if this directory is a mount point. */
- if (stat (dotp, &st) < 0)
- goto lose;
- dotdev = st.st_dev;
- dotino = st.st_ino;
- mount_point = dotdev != thisdev;
-
- /* Search for the last directory. */
- dirstream = opendir(dotp);
- if (dirstream == NULL)
- goto lose;
- while ((d = (struct dirent *)readdir(dirstream)) != NULL)
- {
- if (d->d_name[0] == '.' &&
- (d->d_name[1] == '\0' ||
- (d->d_name[1] == '.' && d->d_name[2] == '\0')))
- continue;
- if (mount_point || d->d_fileno == thisino)
- {
- char *name;
-
- namlen = D_NAMLEN(d);
- name = (char *)
- alloca (dotlist + dotsize - dotp + 1 + namlen + 1);
- memcpy (name, dotp, dotlist + dotsize - dotp);
- name[dotlist + dotsize - dotp] = '/';
- memcpy (&name[dotlist + dotsize - dotp + 1],
- d->d_name, namlen + 1);
- if (lstat (name, &st) < 0)
- {
- int save = errno;
- closedir(dirstream);
- errno = save;
- goto lose;
- }
- if (st.st_dev == thisdev && st.st_ino == thisino)
- break;
- }
- }
- if (d == NULL)
- {
- int save = errno;
- closedir(dirstream);
- errno = save;
- goto lose;
- }
- else
- {
- size_t space;
-
- while ((space = pathp - pathbuf) <= namlen)
- {
- char *new;
-
- if (pathbuf == path)
- {
- new = malloc (pathsize * 2);
- if (!new)
- goto lose;
- }
- else
- {
- new = realloc ((PTR) pathbuf, (pathsize * 2));
- if (!new)
- goto lose;
- pathp = new + space;
- }
- (void) memcpy (new + pathsize + space, pathp, pathsize - space);
- pathp = new + pathsize + space;
- pathbuf = new;
- pathsize *= 2;
- }
-
- pathp -= namlen;
- (void) memcpy (pathp, d->d_name, namlen);
- *--pathp = '/';
- closedir(dirstream);
- }
-
- thisdev = dotdev;
- thisino = dotino;
- }
-
- if (pathp == &path[sizeof(path) - 1])
- *--pathp = '/';
-
- if (dotlist != dots)
- free ((PTR) dotlist);
-
- {
- size_t len = pathbuf + pathsize - pathp;
- if (buf == NULL)
- {
- if (len < (size_t) size)
- len = size;
- buf = (char *) malloc (len);
- if (buf == NULL)
- goto lose2;
- }
- else if ((size_t) size < len)
- {
- errno = ERANGE;
- goto lose2;
- }
- (void) memcpy((PTR) buf, (PTR) pathp, len);
- }
-
- if (pathbuf != path)
- free (pathbuf);
-
- return (buf);
-
- lose:
- if ((dotlist != dots) && dotlist)
- {
- int e = errno;
- free ((PTR) dotlist);
- errno = e;
- }
-
- lose2:
- if ((pathbuf != path) && pathbuf)
- {
- int e = errno;
- free ((PTR) pathbuf);
- errno = e;
- }
- return ((char *)NULL);
-}
-#endif