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.
25 extern pstring smb_cwd;
27 static struct smbw_dir *smbw_dirs;
29 extern struct bitmap *smbw_file_bmap;
30 extern int DEBUGLEVEL;
34 #define DIRP_SIZE (sizeof(fstring) + 12)
37 /*****************************************************
38 map a fd to a smbw_dir structure
39 *******************************************************/
40 struct smbw_dir *smbw_dir(int fd)
44 for (dir=smbw_dirs;dir;dir=dir->next) {
45 if (dir->fd == fd) return dir;
50 /*****************************************************
51 check if a DIR* is one of ours
52 *******************************************************/
53 int smbw_dirp(DIR *dirp)
55 struct smbw_dir *d = (struct smbw_dir *)dirp;
58 for (dir=smbw_dirs;dir;dir=dir->next) {
59 if (dir == d) return 1;
64 /*****************************************************
65 free a smbw_dir structure and all entries
66 *******************************************************/
67 static void free_dir(struct smbw_dir *dir)
72 if (dir->path) free(dir->path);
78 static struct smbw_dir *cur_dir;
80 /*****************************************************
81 add a entry to a directory listing
82 *******************************************************/
83 static void smbw_dir_add(struct file_info *finfo)
85 DEBUG(5,("%s\n", finfo->name));
87 if (cur_dir->malloced == cur_dir->count) {
88 cur_dir->list = (struct file_info *)Realloc(cur_dir->list,
89 sizeof(cur_dir->list[0])*
90 (cur_dir->count+100));
95 cur_dir->malloced += 100;
98 cur_dir->list[cur_dir->count] = *finfo;
102 /*****************************************************
103 add a entry to a directory listing
104 *******************************************************/
105 static void smbw_share_add(const char *share, uint32 type, const char *comment)
107 struct file_info finfo;
111 pstrcpy(finfo.name, share);
112 finfo.mode = aRONLY | aDIR;
114 smbw_dir_add(&finfo);
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);
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);
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;
170 DEBUG(4,("%s\n", __FUNCTION__));
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 string_sub(mask,"\\\\","\\");
204 if ((p=strstr(srv->server_name,"#1D"))) {
205 DEBUG(4,("doing NetServerEnum\n"));
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 if (cli_RNetShareEnum(&srv->cli, smbw_share_add) < 0) {
213 errno = smbw_errno(&srv->cli);
216 } else if (strncmp(srv->cli.dev,"LPT",3) == 0) {
217 if (cli_print_queue(&srv->cli, smbw_printjob_add) < 0) {
218 errno = smbw_errno(&srv->cli);
222 if (cli_list(&srv->cli, mask, aHIDDEN|aSYSTEM|aDIR,
224 errno = smbw_errno(&srv->cli);
231 fd = open(SMBW_DUMMY, O_WRONLY);
237 if (bitmap_query(smbw_file_bmap, fd)) {
238 DEBUG(0,("ERROR: fd used in smbw_dir_open\n"));
243 DLIST_ADD(smbw_dirs, dir);
245 bitmap_set(smbw_file_bmap, fd);
249 dir->path = strdup(s);
251 DEBUG(4,(" -> %d\n", dir->count));
263 /*****************************************************
264 a wrapper for fstat() on a directory
265 *******************************************************/
266 int smbw_dir_fstat(int fd, struct stat *st)
268 struct smbw_dir *dir;
270 DEBUG(4,("%s\n", __FUNCTION__));
280 smbw_setup_stat(st, "", dir->count*DIRP_SIZE, aDIR);
282 st->st_dev = dir->srv->dev;
287 /*****************************************************
288 close a directory handle
289 *******************************************************/
290 int smbw_dir_close(int fd)
292 struct smbw_dir *dir;
294 DEBUG(4,("%s\n", __FUNCTION__));
298 DEBUG(4,("%s(%d)\n", __FUNCTION__, __LINE__));
303 bitmap_clear(smbw_file_bmap, dir->fd);
306 DLIST_REMOVE(smbw_dirs, dir);
313 /*****************************************************
314 a wrapper for getdents()
315 *******************************************************/
316 int smbw_getdents(unsigned int fd, struct dirent *dirp, int count)
318 struct smbw_dir *dir;
321 DEBUG(4,("%s\n", __FUNCTION__));
332 DEBUG(4,("sizeof(*dirp)=%d\n", sizeof(*dirp)));
334 while (count>=DIRP_SIZE && (dir->offset < dir->count)) {
335 dirp->d_off = (dir->offset+1)*DIRP_SIZE;
336 dirp->d_reclen = DIRP_SIZE;
337 fstrcpy(&dirp->d_name[0], dir->list[dir->offset].name);
338 dirp->d_ino = smbw_inode(dir->list[dir->offset].name);
340 count -= dirp->d_reclen;
341 if (dir->offset == dir->count) {
344 dirp = (struct dirent *)(((char *)dirp) + DIRP_SIZE);
353 /*****************************************************
354 a wrapper for chdir()
355 *******************************************************/
356 int smbw_chdir(const char *name)
358 struct smbw_server *srv;
359 fstring server, share;
366 if (smbw_busy) return real_chdir(cwd);
375 DEBUG(4,("%s (%s)\n", __FUNCTION__, name));
377 /* work out what server they are after */
378 cwd = smbw_parse_path(name, server, share, path);
380 if (strncmp(cwd,SMBW_PREFIX,strlen(SMBW_PREFIX))) {
381 if (real_chdir(cwd) == 0) {
382 DEBUG(4,("set SMBW_CWD to %s\n", cwd));
383 pstrcpy(smb_cwd, cwd);
384 if (setenv(SMBW_PWD_ENV, smb_cwd, 1)) {
385 DEBUG(4,("setenv failed\n"));
393 /* get a connection to the server */
394 srv = smbw_server(server, share);
396 /* smbw_server sets errno */
400 if (strncmp(srv->cli.dev,"IPC",3) &&
401 strncmp(srv->cli.dev,"LPT",3) &&
402 !smbw_getatr(srv, path,
403 &mode, NULL, NULL, NULL, NULL)) {
404 errno = smbw_errno(&srv->cli);
408 if (!(mode & aDIR)) {
413 DEBUG(4,("set SMBW_CWD2 to %s\n", cwd));
414 pstrcpy(smb_cwd, cwd);
415 if (setenv(SMBW_PWD_ENV, smb_cwd, 1)) {
416 DEBUG(4,("setenv failed\n"));
419 /* we don't want the old directory to be busy */
432 /*****************************************************
433 a wrapper for lseek() on directories
434 *******************************************************/
435 off_t smbw_dir_lseek(int fd, off_t offset, int whence)
437 struct smbw_dir *dir;
440 DEBUG(4,("%s offset=%d whence=%d\n", __FUNCTION__,
441 (int)offset, whence));
451 dir->offset = offset/DIRP_SIZE;
454 dir->offset += offset/DIRP_SIZE;
457 dir->offset = (dir->count * DIRP_SIZE) + offset;
458 dir->offset /= DIRP_SIZE;
462 ret = dir->offset * DIRP_SIZE;
464 DEBUG(4,(" -> %d\n", (int)ret));
470 /*****************************************************
471 a wrapper for mkdir()
472 *******************************************************/
473 int smbw_mkdir(const char *fname, mode_t mode)
475 struct smbw_server *srv;
476 fstring server, share;
479 DEBUG(4,("%s (%s)\n", __FUNCTION__, fname));
490 /* work out what server they are after */
491 smbw_parse_path(fname, server, share, path);
493 /* get a connection to the server */
494 srv = smbw_server(server, share);
496 /* smbw_server sets errno */
500 if (!cli_mkdir(&srv->cli, path)) {
501 errno = smbw_errno(&srv->cli);
513 /*****************************************************
514 a wrapper for rmdir()
515 *******************************************************/
516 int smbw_rmdir(const char *fname)
518 struct smbw_server *srv;
519 fstring server, share;
522 DEBUG(4,("%s (%s)\n", __FUNCTION__, fname));
533 /* work out what server they are after */
534 smbw_parse_path(fname, server, share, path);
536 /* get a connection to the server */
537 srv = smbw_server(server, share);
539 /* smbw_server sets errno */
543 if (!cli_rmdir(&srv->cli, path)) {
544 errno = smbw_errno(&srv->cli);
557 /*****************************************************
558 a wrapper for getcwd()
559 *******************************************************/
560 char *smbw_getcwd(char *buf, size_t size)
565 return real_getcwd(buf, size);
571 if (size <= 0) size = strlen(smb_cwd)+1;
572 buf = (char *)malloc(size);
580 if (strlen(smb_cwd) > size-1) {
586 safe_strcpy(buf, smb_cwd, size);
592 /*****************************************************
593 a wrapper for fchdir()
594 *******************************************************/
595 int smbw_fchdir(unsigned int fd)
597 struct smbw_dir *dir;
599 DEBUG(4,("%s\n", __FUNCTION__));
612 return chdir(dir->path);
615 /*****************************************************
616 open a directory on the server
617 *******************************************************/
618 DIR *smbw_opendir(const char *fname)
624 fd = smbw_dir_open(fname);
633 return (DIR *)smbw_dir(fd);
636 /*****************************************************
637 read one entry from a directory
638 *******************************************************/
639 struct dirent *smbw_readdir(DIR *dirp)
641 struct smbw_dir *d = (struct smbw_dir *)dirp;
642 static char buf[DIRP_SIZE];
643 struct dirent *de = (struct dirent *)buf;
645 if (smbw_getdents(d->fd, de, DIRP_SIZE) > 0)
651 /*****************************************************
653 *******************************************************/
654 int smbw_closedir(DIR *dirp)
656 struct smbw_dir *d = (struct smbw_dir *)dirp;
657 return smbw_close(d->fd);
660 /*****************************************************
662 *******************************************************/
663 void smbw_seekdir(DIR *dirp, off_t offset)
665 struct smbw_dir *d = (struct smbw_dir *)dirp;
666 smbw_dir_lseek(d->fd,offset, SEEK_SET);
669 /*****************************************************
670 current loc in a directory
671 *******************************************************/
672 off_t smbw_telldir(DIR *dirp)
674 struct smbw_dir *d = (struct smbw_dir *)dirp;
675 return smbw_dir_lseek(d->fd,0,SEEK_CUR);