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)
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, const char *comment)
105 struct file_info finfo;
107 if (strcmp(share,"IPC$") == 0) return;
111 pstrcpy(finfo.name, share);
112 finfo.mode = aRONLY | aDIR;
114 smbw_dir_add(&finfo, NULL);
118 /*****************************************************
119 add a server to a directory listing
120 *******************************************************/
121 static void smbw_server_add(const char *name, uint32 type,
124 struct file_info finfo;
128 pstrcpy(finfo.name, name);
129 finfo.mode = aRONLY | aDIR;
131 smbw_dir_add(&finfo, NULL);
135 /*****************************************************
136 add a entry to a directory listing
137 *******************************************************/
138 static void smbw_printjob_add(struct print_job_info *job)
140 struct file_info finfo;
144 pstrcpy(finfo.name, job->name);
145 finfo.mode = aRONLY | aDIR;
146 finfo.mtime = job->t;
147 finfo.atime = job->t;
148 finfo.ctime = job->t;
149 finfo.uid = nametouid(job->user);
151 finfo.size = job->size;
153 smbw_dir_add(&finfo, NULL);
157 /*****************************************************
158 open a directory on the server
159 *******************************************************/
160 int smbw_dir_open(const char *fname)
162 fstring server, share;
164 struct smbw_server *srv=NULL;
165 struct smbw_dir *dir=NULL;
177 /* work out what server they are after */
178 s = smbw_parse_path(fname, server, share, path);
180 DEBUG(4,("dir_open share=%s\n", share));
182 /* get a connection to the server */
183 srv = smbw_server(server, share);
185 /* smbw_server sets errno */
189 dir = (struct smbw_dir *)malloc(sizeof(*dir));
199 slprintf(mask, sizeof(mask)-1, "%s\\*", path);
200 all_string_sub(mask,"\\\\","\\",0);
202 if ((p=strstr(srv->server_name,"#1D"))) {
203 DEBUG(4,("doing NetServerEnum\n"));
205 smbw_server_add(".",0,"");
206 smbw_server_add("..",0,"");
207 cli_NetServerEnum(&srv->cli, srv->server_name, SV_TYPE_ALL,
210 } else if (strcmp(srv->cli.dev,"IPC") == 0) {
211 DEBUG(4,("doing NetShareEnum\n"));
212 smbw_share_add(".",0,"");
213 smbw_share_add("..",0,"");
214 if (cli_RNetShareEnum(&srv->cli, smbw_share_add) < 0) {
215 errno = smbw_errno(&srv->cli);
218 } else if (strncmp(srv->cli.dev,"LPT",3) == 0) {
219 smbw_share_add(".",0,"");
220 smbw_share_add("..",0,"");
221 if (cli_print_queue(&srv->cli, smbw_printjob_add) < 0) {
222 errno = smbw_errno(&srv->cli);
226 if (strcmp(path,"\\") == 0) {
227 smbw_share_add(".",0,"");
228 smbw_share_add("..",0,"");
230 if (cli_list(&srv->cli, mask, aHIDDEN|aSYSTEM|aDIR,
232 errno = smbw_errno(&srv->cli);
239 fd = open(SMBW_DUMMY, O_WRONLY);
245 if (bitmap_query(smbw_file_bmap, fd)) {
246 DEBUG(0,("ERROR: fd used in smbw_dir_open\n"));
251 DLIST_ADD(smbw_dirs, dir);
253 bitmap_set(smbw_file_bmap, fd);
257 dir->path = strdup(s);
259 DEBUG(4,(" -> %d\n", dir->count));
271 /*****************************************************
272 a wrapper for fstat() on a directory
273 *******************************************************/
274 int smbw_dir_fstat(int fd, struct stat *st)
276 struct smbw_dir *dir;
286 smbw_setup_stat(st, "", dir->count*DIRP_SIZE, aDIR);
288 st->st_dev = dir->srv->dev;
293 /*****************************************************
294 close a directory handle
295 *******************************************************/
296 int smbw_dir_close(int fd)
298 struct smbw_dir *dir;
306 bitmap_clear(smbw_file_bmap, dir->fd);
309 DLIST_REMOVE(smbw_dirs, dir);
316 /*****************************************************
317 a wrapper for getdents()
318 *******************************************************/
319 int smbw_getdents(unsigned int fd, struct dirent *dirp, int count)
321 struct smbw_dir *dir;
333 while (count>=DIRP_SIZE && (dir->offset < dir->count)) {
334 #if HAVE_DIRENT_D_OFF
335 dirp->d_off = (dir->offset+1)*DIRP_SIZE;
337 dirp->d_reclen = DIRP_SIZE;
338 fstrcpy(&dirp->d_name[0], dir->list[dir->offset].name);
339 dirp->d_ino = smbw_inode(dir->list[dir->offset].name);
341 count -= dirp->d_reclen;
342 #if HAVE_DIRENT_D_OFF
343 if (dir->offset == dir->count) {
347 dirp = (struct dirent *)(((char *)dirp) + DIRP_SIZE);
356 /*****************************************************
357 a wrapper for chdir()
358 *******************************************************/
359 int smbw_chdir(const char *name)
361 struct smbw_server *srv;
362 fstring server, share;
370 len = strlen(smbw_prefix);
372 if (smbw_busy) return real_chdir(name);
381 DEBUG(4,("smbw_chdir(%s)\n", name));
383 /* work out what server they are after */
384 cwd = smbw_parse_path(name, server, share, path);
386 /* a special case - accept cd to /smb */
387 if (strncmp(cwd, smbw_prefix, len-1) == 0 &&
392 if (strncmp(cwd,smbw_prefix,strlen(smbw_prefix))) {
393 if (real_chdir(cwd) == 0) {
399 /* get a connection to the server */
400 srv = smbw_server(server, share);
402 /* smbw_server sets errno */
406 if (strncmp(srv->cli.dev,"IPC",3) &&
407 strncmp(srv->cli.dev,"LPT",3) &&
408 !smbw_getatr(srv, path,
409 &mode, NULL, NULL, NULL, NULL, NULL)) {
410 errno = smbw_errno(&srv->cli);
414 if (!(mode & aDIR)) {
420 /* we don't want the old directory to be busy */
425 DEBUG(4,("set SMBW_CWD to %s\n", cwd));
427 pstrcpy(smbw_cwd, cwd);
438 /*****************************************************
439 a wrapper for lseek() on directories
440 *******************************************************/
441 off_t smbw_dir_lseek(int fd, off_t offset, int whence)
443 struct smbw_dir *dir;
454 dir->offset = offset/DIRP_SIZE;
457 dir->offset += offset/DIRP_SIZE;
460 dir->offset = (dir->count * DIRP_SIZE) + offset;
461 dir->offset /= DIRP_SIZE;
465 ret = dir->offset * DIRP_SIZE;
467 DEBUG(4,(" -> %d\n", (int)ret));
473 /*****************************************************
474 a wrapper for mkdir()
475 *******************************************************/
476 int smbw_mkdir(const char *fname, mode_t mode)
478 struct smbw_server *srv;
479 fstring server, share;
491 /* work out what server they are after */
492 smbw_parse_path(fname, server, share, path);
494 /* get a connection to the server */
495 srv = smbw_server(server, share);
497 /* smbw_server sets errno */
501 if (!cli_mkdir(&srv->cli, path)) {
502 errno = smbw_errno(&srv->cli);
514 /*****************************************************
515 a wrapper for rmdir()
516 *******************************************************/
517 int smbw_rmdir(const char *fname)
519 struct smbw_server *srv;
520 fstring server, share;
532 /* work out what server they are after */
533 smbw_parse_path(fname, server, share, path);
535 /* get a connection to the server */
536 srv = smbw_server(server, share);
538 /* smbw_server sets errno */
542 if (!cli_rmdir(&srv->cli, path)) {
543 errno = smbw_errno(&srv->cli);
556 /*****************************************************
557 a wrapper for getcwd()
558 *******************************************************/
559 char *smbw_getcwd(char *buf, size_t size)
564 return (char *)real_getcwd(buf, size);
570 if (size <= 0) size = strlen(smbw_cwd)+1;
571 buf = (char *)malloc(size);
579 if (strlen(smbw_cwd) > size-1) {
585 safe_strcpy(buf, smbw_cwd, size);
591 /*****************************************************
592 a wrapper for fchdir()
593 *******************************************************/
594 int smbw_fchdir(unsigned int fd)
596 struct smbw_dir *dir;
604 return chdir(dir->path);
607 ret = real_fchdir(fd);
616 /*****************************************************
617 open a directory on the server
618 *******************************************************/
619 DIR *smbw_opendir(const char *fname)
625 fd = smbw_dir_open(fname);
634 return (DIR *)smbw_dir(fd);
637 /*****************************************************
638 read one entry from a directory
639 *******************************************************/
640 struct dirent *smbw_readdir(DIR *dirp)
642 struct smbw_dir *d = (struct smbw_dir *)dirp;
648 if (smbw_getdents(d->fd, &dbuf.de, DIRP_SIZE) > 0)
654 /*****************************************************
656 *******************************************************/
657 int smbw_closedir(DIR *dirp)
659 struct smbw_dir *d = (struct smbw_dir *)dirp;
660 return smbw_close(d->fd);
663 /*****************************************************
665 *******************************************************/
666 void smbw_seekdir(DIR *dirp, off_t offset)
668 struct smbw_dir *d = (struct smbw_dir *)dirp;
669 smbw_dir_lseek(d->fd,offset, SEEK_SET);
672 /*****************************************************
673 current loc in a directory
674 *******************************************************/
675 off_t smbw_telldir(DIR *dirp)
677 struct smbw_dir *d = (struct smbw_dir *)dirp;
678 return smbw_dir_lseek(d->fd,0,SEEK_CUR);