2 Unix SMB/Netbios implementation.
4 SMB wrapper directory functions
5 Copyright (C) Andrew Tridgell 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.
23 #include "realcalls.h"
25 extern pstring smbw_cwd;
26 extern fstring smbw_prefix;
28 static struct smbw_dir *smbw_dirs;
30 extern struct bitmap *smbw_file_bmap;
31 extern int DEBUGLEVEL;
35 /*****************************************************
36 map a fd to a smbw_dir structure
37 *******************************************************/
38 struct smbw_dir *smbw_dir(int fd)
42 for (dir=smbw_dirs;dir;dir=dir->next) {
43 if (dir->fd == fd) return dir;
48 /*****************************************************
49 check if a DIR* is one of ours
50 *******************************************************/
51 int smbw_dirp(DIR *dirp)
53 struct smbw_dir *d = (struct smbw_dir *)dirp;
56 for (dir=smbw_dirs;dir;dir=dir->next) {
57 if (dir == d) return 1;
62 /*****************************************************
63 free a smbw_dir structure and all entries
64 *******************************************************/
65 static void free_dir(struct smbw_dir *dir)
70 if (dir->path) free(dir->path);
76 static struct smbw_dir *cur_dir;
78 /*****************************************************
79 add a entry to a directory listing
80 *******************************************************/
81 static void smbw_dir_add(struct file_info *finfo, const char *mask, void * NULL)
83 DEBUG(5,("%s\n", finfo->name));
85 if (cur_dir->malloced == cur_dir->count) {
86 cur_dir->list = (struct file_info *)Realloc(cur_dir->list,
87 sizeof(cur_dir->list[0])*
88 (cur_dir->count+100));
93 cur_dir->malloced += 100;
96 cur_dir->list[cur_dir->count] = *finfo;
100 /*****************************************************
101 add a entry to a directory listing
102 *******************************************************/
103 static void smbw_share_add(const char *share, uint32 type,
104 const char *comment, void *state)
106 struct file_info finfo;
108 if (strcmp(share,"IPC$") == 0) return;
112 pstrcpy(finfo.name, share);
113 finfo.mode = aRONLY | aDIR;
115 smbw_dir_add(&finfo, NULL);
119 /*****************************************************
120 add a server to a directory listing
121 *******************************************************/
122 static void smbw_server_add(const char *name, uint32 type,
123 const char *comment, void *state)
125 struct file_info finfo;
129 pstrcpy(finfo.name, name);
130 finfo.mode = aRONLY | aDIR;
132 smbw_dir_add(&finfo, NULL);
136 /*****************************************************
137 add a entry to a directory listing
138 *******************************************************/
139 static void smbw_printjob_add(struct print_job_info *job)
141 struct file_info finfo;
145 pstrcpy(finfo.name, job->name);
146 finfo.mode = aRONLY | aDIR;
147 finfo.mtime = job->t;
148 finfo.atime = job->t;
149 finfo.ctime = job->t;
150 finfo.uid = nametouid(job->user);
152 finfo.size = job->size;
154 smbw_dir_add(&finfo, NULL);
158 /*****************************************************
159 open a directory on the server
160 *******************************************************/
161 int smbw_dir_open(const char *fname)
163 fstring server, share;
165 struct smbw_server *srv=NULL;
166 struct smbw_dir *dir=NULL;
178 /* work out what server they are after */
179 s = smbw_parse_path(fname, server, share, path);
181 DEBUG(4,("dir_open share=%s\n", share));
183 /* get a connection to the server */
184 srv = smbw_server(server, share);
186 /* smbw_server sets errno */
190 dir = (struct smbw_dir *)malloc(sizeof(*dir));
200 slprintf(mask, sizeof(mask)-1, "%s\\*", path);
201 all_string_sub(mask,"\\\\","\\",0);
203 if ((p=strstr(srv->server_name,"#01"))) {
205 smbw_server_add(".",0,"", NULL);
206 smbw_server_add("..",0,"", NULL);
207 cli_NetServerEnum(&srv->cli, srv->server_name, SV_TYPE_DOMAIN_ENUM,
208 smbw_server_add, NULL);
210 } else if ((p=strstr(srv->server_name,"#1D"))) {
211 DEBUG(4,("doing NetServerEnum\n"));
213 smbw_server_add(".",0,"", NULL);
214 smbw_server_add("..",0,"", NULL);
215 cli_NetServerEnum(&srv->cli, srv->server_name, SV_TYPE_ALL,
216 smbw_server_add, NULL);
218 } else if (strcmp(srv->cli.dev,"IPC") == 0) {
219 DEBUG(4,("doing NetShareEnum\n"));
220 smbw_share_add(".",0,"", NULL);
221 smbw_share_add("..",0,"", NULL);
222 if (cli_RNetShareEnum(&srv->cli, smbw_share_add, NULL) < 0) {
223 errno = smbw_errno(&srv->cli);
226 } else if (strncmp(srv->cli.dev,"LPT",3) == 0) {
227 smbw_share_add(".",0,"", NULL);
228 smbw_share_add("..",0,"", NULL);
229 if (cli_print_queue(&srv->cli, smbw_printjob_add) < 0) {
230 errno = smbw_errno(&srv->cli);
235 if (strcmp(path,"\\") == 0) {
236 smbw_share_add(".",0,"");
237 smbw_share_add("..",0,"");
240 if (cli_list(&srv->cli, mask, aHIDDEN|aSYSTEM|aDIR,
241 smbw_dir_add, NULL) < 0) {
242 errno = smbw_errno(&srv->cli);
249 fd = open(SMBW_DUMMY, O_WRONLY);
255 if (bitmap_query(smbw_file_bmap, fd)) {
256 DEBUG(0,("ERROR: fd used in smbw_dir_open\n"));
261 DLIST_ADD(smbw_dirs, dir);
263 bitmap_set(smbw_file_bmap, fd);
267 dir->path = strdup(s);
269 DEBUG(4,(" -> %d\n", dir->count));
281 /*****************************************************
282 a wrapper for fstat() on a directory
283 *******************************************************/
284 int smbw_dir_fstat(int fd, struct stat *st)
286 struct smbw_dir *dir;
296 smbw_setup_stat(st, "", dir->count*DIRP_SIZE, aDIR);
298 st->st_dev = dir->srv->dev;
303 /*****************************************************
304 close a directory handle
305 *******************************************************/
306 int smbw_dir_close(int fd)
308 struct smbw_dir *dir;
316 bitmap_clear(smbw_file_bmap, dir->fd);
319 DLIST_REMOVE(smbw_dirs, dir);
326 /*****************************************************
327 a wrapper for getdents()
328 *******************************************************/
329 int smbw_getdents(unsigned int fd, struct dirent *dirp, int count)
331 struct smbw_dir *dir;
343 while (count>=DIRP_SIZE && (dir->offset < dir->count)) {
344 #if HAVE_DIRENT_D_OFF
345 dirp->d_off = (dir->offset+1)*DIRP_SIZE;
347 dirp->d_reclen = DIRP_SIZE;
348 fstrcpy(&dirp->d_name[0], dir->list[dir->offset].name);
349 dirp->d_ino = smbw_inode(dir->list[dir->offset].name);
351 count -= dirp->d_reclen;
352 #if HAVE_DIRENT_D_OFF
353 if (dir->offset == dir->count) {
357 dirp = (struct dirent *)(((char *)dirp) + DIRP_SIZE);
366 /*****************************************************
367 a wrapper for chdir()
368 *******************************************************/
369 int smbw_chdir(const char *name)
371 struct smbw_server *srv;
372 fstring server, share;
380 len = strlen(smbw_prefix);
382 if (smbw_busy) return real_chdir(name);
391 DEBUG(4,("smbw_chdir(%s)\n", name));
393 /* work out what server they are after */
394 cwd = smbw_parse_path(name, server, share, path);
396 /* a special case - accept cd to /smb */
397 if (strncmp(cwd, smbw_prefix, len-1) == 0 &&
402 if (strncmp(cwd,smbw_prefix,strlen(smbw_prefix))) {
403 if (real_chdir(cwd) == 0) {
409 /* get a connection to the server */
410 srv = smbw_server(server, share);
412 /* smbw_server sets errno */
416 if (strncmp(srv->cli.dev,"IPC",3) &&
417 strncmp(srv->cli.dev,"LPT",3) &&
418 !smbw_getatr(srv, path,
419 &mode, NULL, NULL, NULL, NULL, NULL)) {
420 errno = smbw_errno(&srv->cli);
424 if (!(mode & aDIR)) {
430 /* we don't want the old directory to be busy */
435 DEBUG(4,("set SMBW_CWD to %s\n", cwd));
437 pstrcpy(smbw_cwd, cwd);
448 /*****************************************************
449 a wrapper for lseek() on directories
450 *******************************************************/
451 off_t smbw_dir_lseek(int fd, off_t offset, int whence)
453 struct smbw_dir *dir;
464 dir->offset = offset/DIRP_SIZE;
467 dir->offset += offset/DIRP_SIZE;
470 dir->offset = (dir->count * DIRP_SIZE) + offset;
471 dir->offset /= DIRP_SIZE;
475 ret = dir->offset * DIRP_SIZE;
477 DEBUG(4,(" -> %d\n", (int)ret));
483 /*****************************************************
484 a wrapper for mkdir()
485 *******************************************************/
486 int smbw_mkdir(const char *fname, mode_t mode)
488 struct smbw_server *srv;
489 fstring server, share;
501 /* work out what server they are after */
502 smbw_parse_path(fname, server, share, path);
504 /* get a connection to the server */
505 srv = smbw_server(server, share);
507 /* smbw_server sets errno */
511 if (!cli_mkdir(&srv->cli, path)) {
512 errno = smbw_errno(&srv->cli);
524 /*****************************************************
525 a wrapper for rmdir()
526 *******************************************************/
527 int smbw_rmdir(const char *fname)
529 struct smbw_server *srv;
530 fstring server, share;
542 /* work out what server they are after */
543 smbw_parse_path(fname, server, share, path);
545 /* get a connection to the server */
546 srv = smbw_server(server, share);
548 /* smbw_server sets errno */
552 if (!cli_rmdir(&srv->cli, path)) {
553 errno = smbw_errno(&srv->cli);
566 /*****************************************************
567 a wrapper for getcwd()
568 *******************************************************/
569 char *smbw_getcwd(char *buf, size_t size)
574 return (char *)real_getcwd(buf, size);
580 if (size <= 0) size = strlen(smbw_cwd)+1;
581 buf = (char *)malloc(size);
589 if (strlen(smbw_cwd) > size-1) {
595 safe_strcpy(buf, smbw_cwd, size);
601 /*****************************************************
602 a wrapper for fchdir()
603 *******************************************************/
604 int smbw_fchdir(unsigned int fd)
606 struct smbw_dir *dir;
614 return chdir(dir->path);
617 ret = real_fchdir(fd);
626 /*****************************************************
627 open a directory on the server
628 *******************************************************/
629 DIR *smbw_opendir(const char *fname)
635 fd = smbw_dir_open(fname);
644 return (DIR *)smbw_dir(fd);
647 /*****************************************************
648 read one entry from a directory
649 *******************************************************/
650 struct dirent *smbw_readdir(DIR *dirp)
652 struct smbw_dir *d = (struct smbw_dir *)dirp;
658 if (smbw_getdents(d->fd, &dbuf.de, DIRP_SIZE) > 0)
664 /*****************************************************
666 *******************************************************/
667 int smbw_closedir(DIR *dirp)
669 struct smbw_dir *d = (struct smbw_dir *)dirp;
670 return smbw_close(d->fd);
673 /*****************************************************
675 *******************************************************/
676 void smbw_seekdir(DIR *dirp, off_t offset)
678 struct smbw_dir *d = (struct smbw_dir *)dirp;
679 smbw_dir_lseek(d->fd,offset, SEEK_SET);
682 /*****************************************************
683 current loc in a directory
684 *******************************************************/
685 off_t smbw_telldir(DIR *dirp)
687 struct smbw_dir *d = (struct smbw_dir *)dirp;
688 return smbw_dir_lseek(d->fd,0,SEEK_CUR);