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,
84 DEBUG(5,("%s\n", finfo->name));
86 if (cur_dir->malloced == cur_dir->count) {
87 cur_dir->list = (struct file_info *)Realloc(cur_dir->list,
88 sizeof(cur_dir->list[0])*
89 (cur_dir->count+100));
94 cur_dir->malloced += 100;
97 cur_dir->list[cur_dir->count] = *finfo;
101 /*****************************************************
102 add a entry to a directory listing
103 *******************************************************/
104 static void smbw_share_add(const char *share, uint32 type,
105 const char *comment, void *state)
107 struct file_info finfo;
109 if (strcmp(share,"IPC$") == 0) return;
113 pstrcpy(finfo.name, share);
114 finfo.mode = aRONLY | aDIR;
116 smbw_dir_add(&finfo, NULL, NULL);
120 /*****************************************************
121 add a server to a directory listing
122 *******************************************************/
123 static void smbw_server_add(const char *name, uint32 type,
124 const char *comment, void *state)
126 struct file_info finfo;
130 pstrcpy(finfo.name, name);
131 finfo.mode = aRONLY | aDIR;
133 smbw_dir_add(&finfo, NULL, NULL);
137 /*****************************************************
138 add a entry to a directory listing
139 *******************************************************/
140 static void smbw_printjob_add(struct print_job_info *job)
142 struct file_info finfo;
146 pstrcpy(finfo.name, job->name);
147 finfo.mode = aRONLY | aDIR;
148 finfo.mtime = job->t;
149 finfo.atime = job->t;
150 finfo.ctime = job->t;
151 finfo.uid = nametouid(job->user);
153 finfo.size = job->size;
155 smbw_dir_add(&finfo, NULL, NULL);
159 /*****************************************************
160 open a directory on the server
161 *******************************************************/
162 int smbw_dir_open(const char *fname)
164 fstring server, share;
166 struct smbw_server *srv=NULL;
167 struct smbw_dir *dir=NULL;
179 /* work out what server they are after */
180 s = smbw_parse_path(fname, server, share, path);
182 DEBUG(4,("dir_open share=%s\n", share));
184 /* get a connection to the server */
185 srv = smbw_server(server, share);
187 /* smbw_server sets errno */
191 dir = (struct smbw_dir *)malloc(sizeof(*dir));
201 slprintf(mask, sizeof(mask)-1, "%s\\*", path);
202 all_string_sub(mask,"\\\\","\\",0);
204 if ((p=strstr(srv->server_name,"#01"))) {
206 smbw_server_add(".",0,"", NULL);
207 smbw_server_add("..",0,"", NULL);
208 cli_NetServerEnum(&srv->cli, srv->server_name, SV_TYPE_DOMAIN_ENUM,
209 smbw_server_add, NULL);
211 } else if ((p=strstr(srv->server_name,"#1D"))) {
212 DEBUG(4,("doing NetServerEnum\n"));
214 smbw_server_add(".",0,"", NULL);
215 smbw_server_add("..",0,"", NULL);
216 cli_NetServerEnum(&srv->cli, srv->server_name, SV_TYPE_ALL,
217 smbw_server_add, NULL);
219 } else if (strcmp(srv->cli.dev,"IPC") == 0) {
220 DEBUG(4,("doing NetShareEnum\n"));
221 smbw_share_add(".",0,"", NULL);
222 smbw_share_add("..",0,"", NULL);
223 if (cli_RNetShareEnum(&srv->cli, smbw_share_add, NULL) < 0) {
224 errno = smbw_errno(&srv->cli);
227 } else if (strncmp(srv->cli.dev,"LPT",3) == 0) {
228 smbw_share_add(".",0,"", NULL);
229 smbw_share_add("..",0,"", NULL);
230 if (cli_print_queue(&srv->cli, smbw_printjob_add) < 0) {
231 errno = smbw_errno(&srv->cli);
236 if (strcmp(path,"\\") == 0) {
237 smbw_share_add(".",0,"");
238 smbw_share_add("..",0,"");
241 if (cli_list(&srv->cli, mask, aHIDDEN|aSYSTEM|aDIR,
242 smbw_dir_add, NULL) < 0) {
243 errno = smbw_errno(&srv->cli);
250 fd = open(SMBW_DUMMY, O_WRONLY);
256 if (bitmap_query(smbw_file_bmap, fd)) {
257 DEBUG(0,("ERROR: fd used in smbw_dir_open\n"));
262 DLIST_ADD(smbw_dirs, dir);
264 bitmap_set(smbw_file_bmap, fd);
268 dir->path = strdup(s);
270 DEBUG(4,(" -> %d\n", dir->count));
282 /*****************************************************
283 a wrapper for fstat() on a directory
284 *******************************************************/
285 int smbw_dir_fstat(int fd, struct stat *st)
287 struct smbw_dir *dir;
297 smbw_setup_stat(st, "", dir->count*DIRP_SIZE, aDIR);
299 st->st_dev = dir->srv->dev;
304 /*****************************************************
305 close a directory handle
306 *******************************************************/
307 int smbw_dir_close(int fd)
309 struct smbw_dir *dir;
317 bitmap_clear(smbw_file_bmap, dir->fd);
320 DLIST_REMOVE(smbw_dirs, dir);
327 /*****************************************************
328 a wrapper for getdents()
329 *******************************************************/
330 int smbw_getdents(unsigned int fd, struct dirent *dirp, int count)
332 struct smbw_dir *dir;
344 while (count>=DIRP_SIZE && (dir->offset < dir->count)) {
345 #if HAVE_DIRENT_D_OFF
346 dirp->d_off = (dir->offset+1)*DIRP_SIZE;
348 dirp->d_reclen = DIRP_SIZE;
349 fstrcpy(&dirp->d_name[0], dir->list[dir->offset].name);
350 dirp->d_ino = smbw_inode(dir->list[dir->offset].name);
352 count -= dirp->d_reclen;
353 #if HAVE_DIRENT_D_OFF
354 if (dir->offset == dir->count) {
358 dirp = (struct dirent *)(((char *)dirp) + DIRP_SIZE);
367 /*****************************************************
368 a wrapper for chdir()
369 *******************************************************/
370 int smbw_chdir(const char *name)
372 struct smbw_server *srv;
373 fstring server, share;
381 len = strlen(smbw_prefix);
383 if (smbw_busy) return real_chdir(name);
392 DEBUG(4,("smbw_chdir(%s)\n", name));
394 /* work out what server they are after */
395 cwd = smbw_parse_path(name, server, share, path);
397 /* a special case - accept cd to /smb */
398 if (strncmp(cwd, smbw_prefix, len-1) == 0 &&
403 if (strncmp(cwd,smbw_prefix,strlen(smbw_prefix))) {
404 if (real_chdir(cwd) == 0) {
410 /* get a connection to the server */
411 srv = smbw_server(server, share);
413 /* smbw_server sets errno */
417 if (strncmp(srv->cli.dev,"IPC",3) &&
418 strncmp(srv->cli.dev,"LPT",3) &&
419 !smbw_getatr(srv, path,
420 &mode, NULL, NULL, NULL, NULL, NULL)) {
421 errno = smbw_errno(&srv->cli);
425 if (!(mode & aDIR)) {
431 /* we don't want the old directory to be busy */
436 DEBUG(4,("set SMBW_CWD to %s\n", cwd));
438 pstrcpy(smbw_cwd, cwd);
449 /*****************************************************
450 a wrapper for lseek() on directories
451 *******************************************************/
452 off_t smbw_dir_lseek(int fd, off_t offset, int whence)
454 struct smbw_dir *dir;
465 dir->offset = offset/DIRP_SIZE;
468 dir->offset += offset/DIRP_SIZE;
471 dir->offset = (dir->count * DIRP_SIZE) + offset;
472 dir->offset /= DIRP_SIZE;
476 ret = dir->offset * DIRP_SIZE;
478 DEBUG(4,(" -> %d\n", (int)ret));
484 /*****************************************************
485 a wrapper for mkdir()
486 *******************************************************/
487 int smbw_mkdir(const char *fname, mode_t mode)
489 struct smbw_server *srv;
490 fstring server, share;
502 /* work out what server they are after */
503 smbw_parse_path(fname, server, share, path);
505 /* get a connection to the server */
506 srv = smbw_server(server, share);
508 /* smbw_server sets errno */
512 if (!cli_mkdir(&srv->cli, path)) {
513 errno = smbw_errno(&srv->cli);
525 /*****************************************************
526 a wrapper for rmdir()
527 *******************************************************/
528 int smbw_rmdir(const char *fname)
530 struct smbw_server *srv;
531 fstring server, share;
543 /* work out what server they are after */
544 smbw_parse_path(fname, server, share, path);
546 /* get a connection to the server */
547 srv = smbw_server(server, share);
549 /* smbw_server sets errno */
553 if (!cli_rmdir(&srv->cli, path)) {
554 errno = smbw_errno(&srv->cli);
567 /*****************************************************
568 a wrapper for getcwd()
569 *******************************************************/
570 char *smbw_getcwd(char *buf, size_t size)
575 return (char *)real_getcwd(buf, size);
581 if (size <= 0) size = strlen(smbw_cwd)+1;
582 buf = (char *)malloc(size);
590 if (strlen(smbw_cwd) > size-1) {
596 safe_strcpy(buf, smbw_cwd, size);
602 /*****************************************************
603 a wrapper for fchdir()
604 *******************************************************/
605 int smbw_fchdir(unsigned int fd)
607 struct smbw_dir *dir;
615 return chdir(dir->path);
618 ret = real_fchdir(fd);
627 /*****************************************************
628 open a directory on the server
629 *******************************************************/
630 DIR *smbw_opendir(const char *fname)
636 fd = smbw_dir_open(fname);
645 return (DIR *)smbw_dir(fd);
648 /*****************************************************
649 read one entry from a directory
650 *******************************************************/
651 struct dirent *smbw_readdir(DIR *dirp)
653 struct smbw_dir *d = (struct smbw_dir *)dirp;
659 if (smbw_getdents(d->fd, &dbuf.de, DIRP_SIZE) > 0)
665 /*****************************************************
667 *******************************************************/
668 int smbw_closedir(DIR *dirp)
670 struct smbw_dir *d = (struct smbw_dir *)dirp;
671 return smbw_close(d->fd);
674 /*****************************************************
676 *******************************************************/
677 void smbw_seekdir(DIR *dirp, off_t offset)
679 struct smbw_dir *d = (struct smbw_dir *)dirp;
680 smbw_dir_lseek(d->fd,offset, SEEK_SET);
683 /*****************************************************
684 current loc in a directory
685 *******************************************************/
686 off_t smbw_telldir(DIR *dirp)
688 struct smbw_dir *d = (struct smbw_dir *)dirp;
689 return smbw_dir_lseek(d->fd,0,SEEK_CUR);