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 /*****************************************************
35 map a fd to a smbw_dir structure
36 *******************************************************/
37 struct smbw_dir *smbw_dir(int fd)
41 for (dir=smbw_dirs;dir;dir=dir->next) {
42 if (dir->fd == fd) return dir;
47 /*****************************************************
48 check if a DIR* is one of ours
49 *******************************************************/
50 int smbw_dirp(DIR *dirp)
52 struct smbw_dir *d = (struct smbw_dir *)dirp;
55 for (dir=smbw_dirs;dir;dir=dir->next) {
56 if (dir == d) return 1;
61 /*****************************************************
62 free a smbw_dir structure and all entries
63 *******************************************************/
64 static void free_dir(struct smbw_dir *dir)
69 if (dir->path) free(dir->path);
75 static struct smbw_dir *cur_dir;
77 /*****************************************************
78 add a entry to a directory listing
79 *******************************************************/
80 static void smbw_dir_add(struct file_info *finfo)
82 DEBUG(5,("%s\n", finfo->name));
84 if (cur_dir->malloced == cur_dir->count) {
85 cur_dir->list = (struct file_info *)Realloc(cur_dir->list,
86 sizeof(cur_dir->list[0])*
87 (cur_dir->count+100));
92 cur_dir->malloced += 100;
95 cur_dir->list[cur_dir->count] = *finfo;
99 /*****************************************************
100 add a entry to a directory listing
101 *******************************************************/
102 static void smbw_share_add(const char *share, uint32 type, const char *comment)
104 struct file_info finfo;
108 pstrcpy(finfo.name, share);
109 finfo.mode = aRONLY | aDIR;
111 smbw_dir_add(&finfo);
115 /*****************************************************
116 add a server to a directory listing
117 *******************************************************/
118 static void smbw_server_add(const char *name, uint32 type,
121 struct file_info finfo;
125 pstrcpy(finfo.name, name);
126 finfo.mode = aRONLY | aDIR;
128 smbw_dir_add(&finfo);
132 /*****************************************************
133 add a entry to a directory listing
134 *******************************************************/
135 static void smbw_printjob_add(struct print_job_info *job)
137 struct file_info finfo;
141 pstrcpy(finfo.name, job->name);
142 finfo.mode = aRONLY | aDIR;
143 finfo.mtime = job->t;
144 finfo.atime = job->t;
145 finfo.ctime = job->t;
146 finfo.uid = nametouid(job->user);
148 finfo.size = job->size;
150 smbw_dir_add(&finfo);
154 /*****************************************************
155 open a directory on the server
156 *******************************************************/
157 int smbw_dir_open(const char *fname)
159 fstring server, share;
161 struct smbw_server *srv=NULL;
162 struct smbw_dir *dir=NULL;
167 DEBUG(4,("%s\n", __FUNCTION__));
176 /* work out what server they are after */
177 s = smbw_parse_path(fname, server, share, path);
179 DEBUG(4,("dir_open share=%s\n", share));
181 /* get a connection to the server */
182 srv = smbw_server(server, share);
184 /* smbw_server sets errno */
188 dir = (struct smbw_dir *)malloc(sizeof(*dir));
198 slprintf(mask, sizeof(mask)-1, "%s\\*", path);
199 string_sub(mask,"\\\\","\\");
201 if ((p=strstr(srv->server_name,"#1D"))) {
202 DEBUG(4,("doing NetServerEnum\n"));
204 cli_NetServerEnum(&srv->cli, srv->server_name, SV_TYPE_ALL,
207 } else if (strcmp(srv->cli.dev,"IPC") == 0) {
208 DEBUG(4,("doing NetShareEnum\n"));
209 if (cli_RNetShareEnum(&srv->cli, smbw_share_add) < 0) {
210 errno = smbw_errno(&srv->cli);
213 } else if (strncmp(srv->cli.dev,"LPT",3) == 0) {
214 if (cli_print_queue(&srv->cli, smbw_printjob_add) < 0) {
215 errno = smbw_errno(&srv->cli);
219 if (cli_list(&srv->cli, mask, aHIDDEN|aSYSTEM|aDIR,
221 errno = smbw_errno(&srv->cli);
228 fd = open(SMBW_DUMMY, O_WRONLY);
234 if (bitmap_query(smbw_file_bmap, fd)) {
235 DEBUG(0,("ERROR: fd used in smbw_dir_open\n"));
240 DLIST_ADD(smbw_dirs, dir);
242 bitmap_set(smbw_file_bmap, fd);
246 dir->path = strdup(s);
248 DEBUG(4,(" -> %d\n", dir->count));
260 /*****************************************************
261 a wrapper for fstat() on a directory
262 *******************************************************/
263 int smbw_dir_fstat(int fd, struct stat *st)
265 struct smbw_dir *dir;
267 DEBUG(4,("%s\n", __FUNCTION__));
277 smbw_setup_stat(st, "", dir->count*sizeof(struct dirent), aDIR);
279 st->st_dev = dir->srv->dev;
284 /*****************************************************
285 close a directory handle
286 *******************************************************/
287 int smbw_dir_close(int fd)
289 struct smbw_dir *dir;
291 DEBUG(4,("%s\n", __FUNCTION__));
295 DEBUG(4,("%s(%d)\n", __FUNCTION__, __LINE__));
300 bitmap_clear(smbw_file_bmap, dir->fd);
303 DLIST_REMOVE(smbw_dirs, dir);
311 /*****************************************************
312 a wrapper for getdents()
313 *******************************************************/
314 int smbw_getdents(unsigned int fd, struct dirent *dirp, int count)
316 struct smbw_dir *dir;
319 DEBUG(4,("%s\n", __FUNCTION__));
330 while (count>=sizeof(*dirp) && (dir->offset < dir->count)) {
331 dirp->d_off = (dir->offset+1)*sizeof(*dirp);
332 dirp->d_reclen = sizeof(*dirp);
333 safe_strcpy(&dirp->d_name[0], dir->list[dir->offset].name,
334 sizeof(dirp->d_name)-1);
335 dirp->d_ino = smbw_inode(dir->list[dir->offset].name);
337 count -= dirp->d_reclen;
338 if (dir->offset == dir->count) {
346 return n*sizeof(*dirp);
350 /*****************************************************
351 a wrapper for chdir()
352 *******************************************************/
353 int smbw_chdir(const char *name)
355 struct smbw_server *srv;
356 fstring server, share;
363 if (smbw_busy) return real_chdir(cwd);
372 DEBUG(4,("%s (%s)\n", __FUNCTION__, name));
374 /* work out what server they are after */
375 cwd = smbw_parse_path(name, server, share, path);
377 if (strncmp(cwd,SMBW_PREFIX,strlen(SMBW_PREFIX))) {
378 if (real_chdir(cwd) == 0) {
379 DEBUG(4,("set SMBW_CWD to %s\n", cwd));
380 pstrcpy(smb_cwd, cwd);
381 if (setenv(SMBW_PWD_ENV, smb_cwd, 1)) {
382 DEBUG(4,("setenv failed\n"));
390 /* get a connection to the server */
391 srv = smbw_server(server, share);
393 /* smbw_server sets errno */
397 if (strncmp(srv->cli.dev,"IPC",3) &&
398 strncmp(srv->cli.dev,"LPT",3) &&
399 !smbw_getatr(srv, path,
400 &mode, NULL, NULL, NULL, NULL)) {
401 errno = smbw_errno(&srv->cli);
405 if (!(mode & aDIR)) {
410 DEBUG(4,("set SMBW_CWD2 to %s\n", cwd));
411 pstrcpy(smb_cwd, cwd);
412 if (setenv(SMBW_PWD_ENV, smb_cwd, 1)) {
413 DEBUG(4,("setenv failed\n"));
416 /* we don't want the old directory to be busy */
429 /*****************************************************
430 a wrapper for lseek() on directories
431 *******************************************************/
432 off_t smbw_dir_lseek(int fd, off_t offset, int whence)
434 struct smbw_dir *dir;
437 DEBUG(4,("%s offset=%d whence=%d\n", __FUNCTION__,
438 (int)offset, whence));
448 dir->offset = offset/sizeof(struct dirent);
451 dir->offset += offset/sizeof(struct dirent);
454 dir->offset = (dir->count * sizeof(struct dirent)) + offset;
455 dir->offset /= sizeof(struct dirent);
459 ret = dir->offset * sizeof(struct dirent);
461 DEBUG(4,(" -> %d\n", (int)ret));
467 /*****************************************************
468 a wrapper for mkdir()
469 *******************************************************/
470 int smbw_mkdir(const char *fname, mode_t mode)
472 struct smbw_server *srv;
473 fstring server, share;
476 DEBUG(4,("%s (%s)\n", __FUNCTION__, fname));
487 /* work out what server they are after */
488 smbw_parse_path(fname, server, share, path);
490 /* get a connection to the server */
491 srv = smbw_server(server, share);
493 /* smbw_server sets errno */
497 if (!cli_mkdir(&srv->cli, path)) {
498 errno = smbw_errno(&srv->cli);
510 /*****************************************************
511 a wrapper for rmdir()
512 *******************************************************/
513 int smbw_rmdir(const char *fname)
515 struct smbw_server *srv;
516 fstring server, share;
519 DEBUG(4,("%s (%s)\n", __FUNCTION__, fname));
530 /* work out what server they are after */
531 smbw_parse_path(fname, server, share, path);
533 /* get a connection to the server */
534 srv = smbw_server(server, share);
536 /* smbw_server sets errno */
540 if (!cli_rmdir(&srv->cli, path)) {
541 errno = smbw_errno(&srv->cli);
554 /*****************************************************
555 a wrapper for getcwd()
556 *******************************************************/
557 char *smbw_getcwd(char *buf, size_t size)
562 return __getcwd(buf, size);
568 if (size <= 0) size = strlen(smb_cwd)+1;
569 buf = (char *)malloc(size);
577 if (strlen(smb_cwd) > size-1) {
583 safe_strcpy(buf, smb_cwd, size);
589 /*****************************************************
590 a wrapper for fchdir()
591 *******************************************************/
592 int smbw_fchdir(unsigned int fd)
594 struct smbw_dir *dir;
596 DEBUG(4,("%s\n", __FUNCTION__));
609 return chdir(dir->path);
612 /*****************************************************
613 open a directory on the server
614 *******************************************************/
615 DIR *smbw_opendir(const char *fname)
621 fd = smbw_dir_open(fname);
630 return (DIR *)smbw_dir(fd);
633 /*****************************************************
634 read one entry from a directory
635 *******************************************************/
636 struct dirent *smbw_readdir(DIR *dirp)
638 struct smbw_dir *d = (struct smbw_dir *)dirp;
639 static struct dirent de;
641 if (smbw_getdents(d->fd, &de, sizeof(struct dirent)) > 0)
647 /*****************************************************
649 *******************************************************/
650 int smbw_closedir(DIR *dirp)
652 struct smbw_dir *d = (struct smbw_dir *)dirp;
653 return smbw_close(d->fd);
656 /*****************************************************
658 *******************************************************/
659 void smbw_seekdir(DIR *dirp, off_t offset)
661 struct smbw_dir *d = (struct smbw_dir *)dirp;
662 smbw_dir_lseek(d->fd,offset, SEEK_SET);
665 /*****************************************************
666 current loc in a directory
667 *******************************************************/
668 off_t smbw_telldir(DIR *dirp)
670 struct smbw_dir *d = (struct smbw_dir *)dirp;
671 return smbw_dir_lseek(d->fd,0,SEEK_CUR);