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 open a directory on the server
117 *******************************************************/
118 int smbw_dir_open(const char *fname)
120 fstring server, share;
122 struct smbw_server *srv=NULL;
123 struct smbw_dir *dir=NULL;
128 DEBUG(4,("%s\n", __FUNCTION__));
137 /* work out what server they are after */
138 s = smbw_parse_path(fname, server, share, path);
140 DEBUG(4,("dir_open share=%s\n", share));
142 /* get a connection to the server */
143 srv = smbw_server(server, share);
145 /* smbw_server sets errno */
149 dir = (struct smbw_dir *)malloc(sizeof(*dir));
159 slprintf(mask, sizeof(mask)-1, "%s\\*", path);
160 string_sub(mask,"\\\\","\\");
162 if (strcmp(share,"IPC$") == 0) {
163 DEBUG(4,("doing NetShareEnum\n"));
164 if (cli_RNetShareEnum(&srv->cli, smbw_share_add) <= 0) {
165 errno = smbw_errno(&srv->cli);
169 if (cli_list(&srv->cli, mask, aHIDDEN|aSYSTEM|aDIR,
170 smbw_dir_add) <= 0) {
171 errno = smbw_errno(&srv->cli);
178 fd = bitmap_find(smbw_file_bmap, 0);
184 DLIST_ADD(smbw_dirs, dir);
186 bitmap_set(smbw_file_bmap, fd);
188 dir->fd = fd + SMBW_FD_OFFSET;
190 dir->path = strdup(s);
192 DEBUG(4,(" -> %d\n", dir->count));
204 /*****************************************************
205 a wrapper for fstat() on a directory
206 *******************************************************/
207 int smbw_dir_fstat(int fd, struct stat *st)
209 struct smbw_dir *dir;
211 DEBUG(4,("%s\n", __FUNCTION__));
221 smbw_setup_stat(st, "", dir->count*sizeof(struct dirent), aDIR);
223 st->st_dev = dir->srv->dev;
228 /*****************************************************
229 close a directory handle
230 *******************************************************/
231 int smbw_dir_close(int fd)
233 struct smbw_dir *dir;
235 DEBUG(4,("%s\n", __FUNCTION__));
239 DEBUG(4,("%s(%d)\n", __FUNCTION__, __LINE__));
244 bitmap_clear(smbw_file_bmap, dir->fd - SMBW_FD_OFFSET);
246 DLIST_REMOVE(smbw_dirs, dir);
254 /*****************************************************
255 a wrapper for getdents()
256 *******************************************************/
257 int smbw_getdents(unsigned int fd, struct dirent *dirp, int count)
259 struct smbw_dir *dir;
262 DEBUG(4,("%s\n", __FUNCTION__));
273 while (count>=sizeof(*dirp) && (dir->offset < dir->count)) {
274 dirp->d_off = (dir->offset+1)*sizeof(*dirp);
275 dirp->d_reclen = sizeof(*dirp);
276 safe_strcpy(&dirp->d_name[0], dir->list[dir->offset].name,
277 sizeof(dirp->d_name)-1);
278 dirp->d_ino = smbw_inode(dir->list[dir->offset].name);
280 count -= dirp->d_reclen;
281 if (dir->offset == dir->count) {
289 return n*sizeof(*dirp);
293 /*****************************************************
294 a wrapper for chdir()
295 *******************************************************/
296 int smbw_chdir(const char *name)
298 struct smbw_server *srv;
299 fstring server, share;
306 if (smbw_busy) return real_chdir(cwd);
315 DEBUG(4,("%s (%s)\n", __FUNCTION__, name));
317 /* work out what server they are after */
318 cwd = smbw_parse_path(name, server, share, path);
320 if (strncmp(cwd,SMBW_PREFIX,strlen(SMBW_PREFIX))) {
321 if (real_chdir(cwd) == 0) {
322 DEBUG(4,("set SMBW_CWD to %s\n", cwd));
323 pstrcpy(smb_cwd, cwd);
324 if (setenv(SMBW_PWD_ENV, smb_cwd, 1)) {
325 DEBUG(4,("setenv failed\n"));
333 /* get a connection to the server */
334 srv = smbw_server(server, share);
336 /* smbw_server sets errno */
340 if (strcmp(share,"IPC$") &&
341 !smbw_getatr(srv, path,
342 &mode, NULL, NULL, NULL, NULL)) {
343 errno = smbw_errno(&srv->cli);
347 if (!(mode & aDIR)) {
352 DEBUG(4,("set SMBW_CWD2 to %s\n", cwd));
353 pstrcpy(smb_cwd, cwd);
354 if (setenv(SMBW_PWD_ENV, smb_cwd, 1)) {
355 DEBUG(4,("setenv failed\n"));
358 /* we don't want the old directory to be busy */
371 /*****************************************************
372 a wrapper for lseek() on directories
373 *******************************************************/
374 off_t smbw_dir_lseek(int fd, off_t offset, int whence)
376 struct smbw_dir *dir;
379 DEBUG(4,("%s offset=%d whence=%d\n", __FUNCTION__,
380 (int)offset, whence));
390 dir->offset = offset/sizeof(struct dirent);
393 dir->offset += offset/sizeof(struct dirent);
396 dir->offset = (dir->count * sizeof(struct dirent)) + offset;
397 dir->offset /= sizeof(struct dirent);
401 ret = dir->offset * sizeof(struct dirent);
403 DEBUG(4,(" -> %d\n", (int)ret));
409 /*****************************************************
410 a wrapper for mkdir()
411 *******************************************************/
412 int smbw_mkdir(const char *fname, mode_t mode)
414 struct smbw_server *srv;
415 fstring server, share;
418 DEBUG(4,("%s (%s)\n", __FUNCTION__, fname));
429 /* work out what server they are after */
430 smbw_parse_path(fname, server, share, path);
432 /* get a connection to the server */
433 srv = smbw_server(server, share);
435 /* smbw_server sets errno */
439 if (!cli_mkdir(&srv->cli, path)) {
440 errno = smbw_errno(&srv->cli);
452 /*****************************************************
453 a wrapper for rmdir()
454 *******************************************************/
455 int smbw_rmdir(const char *fname)
457 struct smbw_server *srv;
458 fstring server, share;
461 DEBUG(4,("%s (%s)\n", __FUNCTION__, fname));
472 /* work out what server they are after */
473 smbw_parse_path(fname, server, share, path);
475 /* get a connection to the server */
476 srv = smbw_server(server, share);
478 /* smbw_server sets errno */
482 if (!cli_rmdir(&srv->cli, path)) {
483 errno = smbw_errno(&srv->cli);
496 /*****************************************************
497 a wrapper for getcwd()
498 *******************************************************/
499 char *smbw_getcwd(char *buf, size_t size)
504 return __getcwd(buf, size);
510 if (size <= 0) size = strlen(smb_cwd)+1;
511 buf = (char *)malloc(size);
519 if (strlen(smb_cwd) > size-1) {
525 safe_strcpy(buf, smb_cwd, size);
531 /*****************************************************
532 a wrapper for fchdir()
533 *******************************************************/
534 int smbw_fchdir(unsigned int fd)
536 struct smbw_dir *dir;
538 DEBUG(4,("%s\n", __FUNCTION__));
551 return chdir(dir->path);
554 /*****************************************************
555 open a directory on the server
556 *******************************************************/
557 DIR *smbw_opendir(const char *fname)
563 fd = smbw_dir_open(fname);
572 return (DIR *)smbw_dir(fd);
575 /*****************************************************
576 read one entry from a directory
577 *******************************************************/
578 struct dirent *smbw_readdir(DIR *dirp)
580 struct smbw_dir *d = (struct smbw_dir *)dirp;
581 static struct dirent de;
583 if (smbw_getdents(d->fd, &de, sizeof(struct dirent)) > 0)
589 /*****************************************************
591 *******************************************************/
592 int smbw_closedir(DIR *dirp)
594 struct smbw_dir *d = (struct smbw_dir *)dirp;
595 return smbw_close(d->fd);
598 /*****************************************************
600 *******************************************************/
601 void smbw_seekdir(DIR *dirp, off_t offset)
603 struct smbw_dir *d = (struct smbw_dir *)dirp;
604 smbw_dir_lseek(d->fd,offset, SEEK_SET);
607 /*****************************************************
608 current loc in a directory
609 *******************************************************/
610 off_t smbw_telldir(DIR *dirp)
612 struct smbw_dir *d = (struct smbw_dir *)dirp;
613 return smbw_dir_lseek(d->fd,0,SEEK_CUR);