first pass at updating head branch to be to be the same as the SAMBA_2_0 branch
[kai/samba.git] / source3 / smbwrapper / smbw_dir.c
index 5a45c111753ca9709794dc3f41c2aab712a3e766..09294282736bcee81802d12c3eaa0e6b63c112e3 100644 (file)
 */
 
 #include "includes.h"
-#include "wrapper.h"
+#include "realcalls.h"
 
-extern pstring smb_cwd;
+extern pstring smbw_cwd;
+extern fstring smbw_prefix;
 
 static struct smbw_dir *smbw_dirs;
 
@@ -31,9 +32,6 @@ extern int DEBUGLEVEL;
 
 extern int smbw_busy;
 
-#define DIRP_SIZE (sizeof(fstring) + 12)
-
-
 /***************************************************** 
 map a fd to a smbw_dir structure
 *******************************************************/
@@ -80,7 +78,7 @@ static struct smbw_dir *cur_dir;
 /***************************************************** 
 add a entry to a directory listing
 *******************************************************/
-static void smbw_dir_add(struct file_info *finfo)
+static void smbw_dir_add(struct file_info *finfo, const char *mask)
 {
        DEBUG(5,("%s\n", finfo->name));
 
@@ -106,12 +104,14 @@ static void smbw_share_add(const char *share, uint32 type, const char *comment)
 {
        struct file_info finfo;
 
+       if (strcmp(share,"IPC$") == 0) return;
+
        ZERO_STRUCT(finfo);
 
        pstrcpy(finfo.name, share);
        finfo.mode = aRONLY | aDIR;     
 
-       smbw_dir_add(&finfo);
+       smbw_dir_add(&finfo, NULL);
 }
 
 
@@ -128,7 +128,7 @@ static void smbw_server_add(const char *name, uint32 type,
        pstrcpy(finfo.name, name);
        finfo.mode = aRONLY | aDIR;     
 
-       smbw_dir_add(&finfo);
+       smbw_dir_add(&finfo, NULL);
 }
 
 
@@ -150,7 +150,7 @@ static void smbw_printjob_add(struct print_job_info *job)
        finfo.mode = aRONLY;
        finfo.size = job->size;
 
-       smbw_dir_add(&finfo);
+       smbw_dir_add(&finfo, NULL);
 }
 
 
@@ -167,8 +167,6 @@ int smbw_dir_open(const char *fname)
        int fd;
        char *s, *p;
 
-       DEBUG(4,("%s\n", __FUNCTION__));
-
        if (!fname) {
                errno = EINVAL;
                return -1;
@@ -199,26 +197,36 @@ int smbw_dir_open(const char *fname)
        cur_dir = dir;
 
        slprintf(mask, sizeof(mask)-1, "%s\\*", path);
-       string_sub(mask,"\\\\","\\");
+       all_string_sub(mask,"\\\\","\\",0);
 
        if ((p=strstr(srv->server_name,"#1D"))) {
                DEBUG(4,("doing NetServerEnum\n"));
                *p = 0;
+               smbw_server_add(".",0,"");
+               smbw_server_add("..",0,"");
                cli_NetServerEnum(&srv->cli, srv->server_name, SV_TYPE_ALL,
                                  smbw_server_add);
                *p = '#';
        } else if (strcmp(srv->cli.dev,"IPC") == 0) {
                DEBUG(4,("doing NetShareEnum\n"));
+               smbw_share_add(".",0,"");
+               smbw_share_add("..",0,"");
                if (cli_RNetShareEnum(&srv->cli, smbw_share_add) < 0) {
                        errno = smbw_errno(&srv->cli);
                        goto failed;
                }
        } else if (strncmp(srv->cli.dev,"LPT",3) == 0) {
+               smbw_share_add(".",0,"");
+               smbw_share_add("..",0,"");
                if (cli_print_queue(&srv->cli, smbw_printjob_add) < 0) {
                        errno = smbw_errno(&srv->cli);
                        goto failed;
                }
        } else {
+               if (strcmp(path,"\\") == 0) {
+                       smbw_share_add(".",0,"");
+                       smbw_share_add("..",0,"");
+               }
                if (cli_list(&srv->cli, mask, aHIDDEN|aSYSTEM|aDIR, 
                             smbw_dir_add) < 0) {
                        errno = smbw_errno(&srv->cli);
@@ -267,8 +275,6 @@ int smbw_dir_fstat(int fd, struct stat *st)
 {
        struct smbw_dir *dir;
 
-       DEBUG(4,("%s\n", __FUNCTION__));
-
        dir = smbw_dir(fd);
        if (!dir) {
                errno = EBADF;
@@ -291,11 +297,8 @@ int smbw_dir_close(int fd)
 {
        struct smbw_dir *dir;
 
-       DEBUG(4,("%s\n", __FUNCTION__));
-
        dir = smbw_dir(fd);
        if (!dir) {
-               DEBUG(4,("%s(%d)\n", __FUNCTION__, __LINE__));
                errno = EBADF;
                return -1;
        }
@@ -318,8 +321,6 @@ int smbw_getdents(unsigned int fd, struct dirent *dirp, int count)
        struct smbw_dir *dir;
        int n=0;
 
-       DEBUG(4,("%s\n", __FUNCTION__));
-
        smbw_busy++;
 
        dir = smbw_dir(fd);
@@ -329,18 +330,20 @@ int smbw_getdents(unsigned int fd, struct dirent *dirp, int count)
                return -1;
        }
 
-       DEBUG(4,("sizeof(*dirp)=%d\n", sizeof(*dirp)));
-       
        while (count>=DIRP_SIZE && (dir->offset < dir->count)) {
+#if HAVE_DIRENT_D_OFF
                dirp->d_off = (dir->offset+1)*DIRP_SIZE;
+#endif
                dirp->d_reclen = DIRP_SIZE;
                fstrcpy(&dirp->d_name[0], dir->list[dir->offset].name);
                dirp->d_ino = smbw_inode(dir->list[dir->offset].name);
                dir->offset++;
                count -= dirp->d_reclen;
+#if HAVE_DIRENT_D_OFF
                if (dir->offset == dir->count) {
                        dirp->d_off = -1;
                }
+#endif
                dirp = (struct dirent *)(((char *)dirp) + DIRP_SIZE);
                n++;
        }
@@ -358,12 +361,15 @@ int smbw_chdir(const char *name)
        struct smbw_server *srv;
        fstring server, share;
        pstring path;
-       uint32 mode = aDIR;
+       uint16 mode = aDIR;
        char *cwd;
+       int len;
 
        smbw_init();
 
-       if (smbw_busy) return real_chdir(cwd);
+       len = strlen(smbw_prefix);
+
+       if (smbw_busy) return real_chdir(name);
 
        smbw_busy++;
 
@@ -372,21 +378,21 @@ int smbw_chdir(const char *name)
                goto failed;
        }
 
-       DEBUG(4,("%s (%s)\n", __FUNCTION__, name));
+       DEBUG(4,("smbw_chdir(%s)\n", name));
 
        /* work out what server they are after */
        cwd = smbw_parse_path(name, server, share, path);
 
-       if (strncmp(cwd,SMBW_PREFIX,strlen(SMBW_PREFIX))) {
+       /* a special case - accept cd to /smb */
+       if (strncmp(cwd, smbw_prefix, len-1) == 0 &&
+           cwd[len-1] == 0) {
+               goto success1;
+       }
+
+       if (strncmp(cwd,smbw_prefix,strlen(smbw_prefix))) {
                if (real_chdir(cwd) == 0) {
-                       DEBUG(4,("set SMBW_CWD to %s\n", cwd));
-                       pstrcpy(smb_cwd, cwd);
-                       if (setenv(SMBW_PWD_ENV, smb_cwd, 1)) {
-                               DEBUG(4,("setenv failed\n"));
-                       }
-                       goto success;
+                       goto success2;
                }
-               errno = ENOENT;
                goto failed;
        }
 
@@ -400,7 +406,7 @@ int smbw_chdir(const char *name)
        if (strncmp(srv->cli.dev,"IPC",3) &&
            strncmp(srv->cli.dev,"LPT",3) &&
            !smbw_getatr(srv, path, 
-                        &mode, NULL, NULL, NULL, NULL)) {
+                        &mode, NULL, NULL, NULL, NULL, NULL)) {
                errno = smbw_errno(&srv->cli);
                goto failed;
        }
@@ -410,16 +416,16 @@ int smbw_chdir(const char *name)
                goto failed;
        }
 
-       DEBUG(4,("set SMBW_CWD2 to %s\n", cwd));
-       pstrcpy(smb_cwd, cwd);
-       if (setenv(SMBW_PWD_ENV, smb_cwd, 1)) {
-               DEBUG(4,("setenv failed\n"));
-       }
-
+ success1:
        /* we don't want the old directory to be busy */
        real_chdir("/");
 
- success:
+ success2:
+
+       DEBUG(4,("set SMBW_CWD to %s\n", cwd));
+
+       pstrcpy(smbw_cwd, cwd);
+
        smbw_busy--;
        return 0;
 
@@ -437,9 +443,6 @@ off_t smbw_dir_lseek(int fd, off_t offset, int whence)
        struct smbw_dir *dir;
        off_t ret;
 
-       DEBUG(4,("%s offset=%d whence=%d\n", __FUNCTION__, 
-                (int)offset, whence));
-
        dir = smbw_dir(fd);
        if (!dir) {
                errno = EBADF;
@@ -476,8 +479,6 @@ int smbw_mkdir(const char *fname, mode_t mode)
        fstring server, share;
        pstring path;
 
-       DEBUG(4,("%s (%s)\n", __FUNCTION__, fname));
-
        if (!fname) {
                errno = EINVAL;
                return -1;
@@ -519,8 +520,6 @@ int smbw_rmdir(const char *fname)
        fstring server, share;
        pstring path;
 
-       DEBUG(4,("%s (%s)\n", __FUNCTION__, fname));
-
        if (!fname) {
                errno = EINVAL;
                return -1;
@@ -562,13 +561,13 @@ char *smbw_getcwd(char *buf, size_t size)
        smbw_init();
 
        if (smbw_busy) {
-               return real_getcwd(buf, size);
+               return (char *)real_getcwd(buf, size);
        }
 
        smbw_busy++;
 
        if (!buf) {
-               if (size <= 0) size = strlen(smb_cwd)+1;
+               if (size <= 0) size = strlen(smbw_cwd)+1;
                buf = (char *)malloc(size);
                if (!buf) {
                        errno = ENOMEM;
@@ -577,13 +576,13 @@ char *smbw_getcwd(char *buf, size_t size)
                }
        }
 
-       if (strlen(smb_cwd) > size-1) {
+       if (strlen(smbw_cwd) > size-1) {
                errno = ERANGE;
                smbw_busy--;
                return NULL;
        }
 
-       safe_strcpy(buf, smb_cwd, size);
+       safe_strcpy(buf, smbw_cwd, size);
 
        smbw_busy--;
        return buf;
@@ -595,21 +594,23 @@ a wrapper for fchdir()
 int smbw_fchdir(unsigned int fd)
 {
        struct smbw_dir *dir;
-
-       DEBUG(4,("%s\n", __FUNCTION__));
+       int ret;
 
        smbw_busy++;
 
        dir = smbw_dir(fd);
-       if (!dir) {
-               errno = EBADF;
+       if (dir) {
                smbw_busy--;
-               return -1;
+               return chdir(dir->path);
        }       
 
+       ret = real_fchdir(fd);
+       if (ret == 0) {
+               sys_getwd(smbw_cwd);            
+       }
+
        smbw_busy--;
-       
-       return chdir(dir->path);
+       return ret;
 }
 
 /***************************************************** 
@@ -639,11 +640,13 @@ read one entry from a directory
 struct dirent *smbw_readdir(DIR *dirp)
 {
        struct smbw_dir *d = (struct smbw_dir *)dirp;
-       static char buf[DIRP_SIZE];
-       struct dirent *de = (struct dirent *)buf;
+       static union {
+               char buf[DIRP_SIZE];
+               struct dirent de;
+       } dbuf;
 
-       if (smbw_getdents(d->fd, de, DIRP_SIZE) > 0) 
-               return de;
+       if (smbw_getdents(d->fd, &dbuf.de, DIRP_SIZE) > 0) 
+               return &dbuf.de;
 
        return NULL;
 }
@@ -674,3 +677,4 @@ off_t smbw_telldir(DIR *dirp)
        struct smbw_dir *d = (struct smbw_dir *)dirp;
        return smbw_dir_lseek(d->fd,0,SEEK_CUR);
 }
+