added lseek() to smbwrapper
[Samba/gbeck.git] / source / smbwrapper / smbw.c
blob5392cf1abc8206a770c9f883310d72a881ec2afd
1 /*
2 Unix SMB/Netbios implementation.
3 Version 2.0
4 SMB wrapper 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.
22 #include "includes.h"
23 #include "smbw.h"
24 #include "wrapper.h"
26 static pstring smb_cwd;
28 struct smbw_server {
29 struct smbw_server *next, *prev;
30 struct cli_state cli;
31 char *server_name;
32 char *share_name;
35 struct smbw_file {
36 struct smbw_file *next, *prev;
37 int cli_fd, fd;
38 char *fname;
39 off_t offset;
40 struct smbw_server *srv;
43 struct smbw_dir {
44 struct smbw_dir *next, *prev;
45 int fd;
46 int offset, count, malloced;
47 struct smbw_server *srv;
48 struct file_info *list;
51 static struct smbw_file *smbw_files;
52 static struct smbw_dir *smbw_dirs;
53 static struct smbw_server *smbw_srvs;
55 static struct bitmap *file_bmap;
56 static pstring local_machine;
57 extern int DEBUGLEVEL;
59 static int smbw_busy;
61 /*****************************************************
62 initialise structures
63 *******************************************************/
64 void smbw_init(void)
66 extern BOOL in_client;
67 static int initialised;
68 static pstring servicesf = CONFIGFILE;
69 extern FILE *dbf;
70 char *p;
72 if (initialised) return;
73 initialised = 1;
75 smbw_busy++;
77 DEBUGLEVEL = 0;
78 setup_logging("smbw",True);
80 dbf = stderr;
82 if ((p=getenv("SMBW_LOGFILE"))) {
83 dbf = fopen(p, "a");
86 file_bmap = bitmap_allocate(SMBW_MAX_OPEN);
87 if (!file_bmap) {
88 exit(1);
91 charset_initialise();
93 in_client = True;
95 if (!lp_load(servicesf,True,False,False)) {
96 exit(1);
99 get_myname(local_machine,NULL);
101 if ((p=getenv("SMBW_DEBUG"))) {
102 DEBUGLEVEL = atoi(p);
105 if ((p=getenv(SMBW_PWD_ENV))) {
106 pstrcpy(smb_cwd, p);
107 DEBUG(4,("Initial cwd from smb_cwd is %s\n", smb_cwd));
108 } else {
109 sys_getwd(smb_cwd);
110 DEBUG(4,("Initial cwd from getwd is %s\n", smb_cwd));
112 smbw_busy--;
115 /*****************************************************
116 determine if a file descriptor is a smb one
117 *******************************************************/
118 BOOL smbw_fd(int fd)
120 if (smbw_busy) return False;
121 return (fd >= SMBW_FD_OFFSET);
125 /*****************************************************
126 remove redundent stuff from a filename
127 *******************************************************/
128 void clean_fname(char *name)
130 char *p, *p2;
131 int l;
132 int modified = 1;
134 if (!name) return;
136 while (modified) {
137 modified = 0;
139 DEBUG(5,("cleaning %s\n", name));
141 if ((p=strstr(name,"/./"))) {
142 modified = 1;
143 while (*p) {
144 p[0] = p[2];
145 p++;
149 if ((p=strstr(name,"//"))) {
150 modified = 1;
151 while (*p) {
152 p[0] = p[1];
153 p++;
157 if (strcmp(name,"/../")==0) {
158 modified = 1;
159 name[1] = 0;
162 if ((p=strstr(name,"/../"))) {
163 modified = 1;
164 for (p2=(p>name?p-1:p);p2>name;p2--) {
165 if (p2[0] == '/') break;
167 while (*p2) {
168 p2[0] = p2[3];
169 p2++;
173 if (strcmp(name,"/..")==0) {
174 modified = 1;
175 name[1] = 0;
178 l = strlen(name);
179 p = l>=3?(name+l-3):name;
180 if (strcmp(p,"/..")==0) {
181 modified = 1;
182 for (p2=p-1;p2>name;p2--) {
183 if (p2[0] == '/') break;
185 if (p2==name) {
186 p[0] = '/';
187 p[1] = 0;
188 } else {
189 p2[0] = 0;
193 l = strlen(name);
194 p = l>=2?(name+l-2):name;
195 if (strcmp(p,"/.")==0) {
196 if (p == name) {
197 p[1] = 0;
198 } else {
199 p[0] = 0;
203 if (strncmp(p=name,"./",2) == 0) {
204 modified = 1;
205 do {
206 p[0] = p[2];
207 } while (*p++);
210 l = strlen(p=name);
211 if (l > 1 && p[l-1] == '/') {
212 modified = 1;
213 p[l-1] = 0;
219 /*****************************************************
220 parse a smb path into its components.
221 *******************************************************/
222 char *smbw_parse_path(const char *fname, char *server, char *share, char *path)
224 static pstring s;
225 char *p, *p2;
226 int len;
228 *server = *share = *path = 0;
230 if (fname[0] == '/') {
231 pstrcpy(s, fname);
232 } else {
233 slprintf(s,sizeof(s)-1, "%s/%s", smb_cwd, fname);
235 clean_fname(s);
237 DEBUG(5,("cleaned %s (fname=%s cwd=%s)\n",
238 s, fname, smb_cwd));
240 if (strncmp(s,SMBW_PREFIX,strlen(SMBW_PREFIX))) return s;
242 p = s + strlen(SMBW_PREFIX);
243 p2 = strchr(p,'/');
245 if (p2) {
246 len = (int)(p2-p);
247 } else {
248 len = strlen(p);
251 len = MIN(len,sizeof(fstring)-1);
253 strncpy(server, p, len);
254 server[len] = 0;
256 p = p2;
257 if (!p) {
258 fstrcpy(share,"IPC$");
259 pstrcpy(path,"");
260 goto ok;
263 p++;
264 p2 = strchr(p,'/');
266 if (p2) {
267 len = (int)(p2-p);
268 } else {
269 len = strlen(p);
272 len = MIN(len,sizeof(fstring)-1);
274 strncpy(share, p, len);
275 share[len] = 0;
277 p = p2;
278 if (!p) {
279 pstrcpy(path,"\\");
280 goto ok;
283 pstrcpy(path,p);
285 string_sub(path, "/", "\\");
288 DEBUG(5,("parsed path name=%s cwd=%s [%s] [%s] [%s]\n",
289 fname, smb_cwd,
290 server, share, path));
292 return s;
295 /*****************************************************
296 determine if a path name (possibly relative) is in the
297 smb name space
298 *******************************************************/
299 BOOL smbw_path(const char *path)
301 fstring server, share;
302 pstring s;
303 char *cwd;
304 int l=strlen(SMBW_PREFIX)-1;
306 if (path[0] == '/' && strncmp(path,SMBW_PREFIX,l)) {
307 return False;
310 if (smbw_busy) return False;
312 smbw_init();
314 DEBUG(3,("smbw_path(%s)\n", path));
316 cwd = smbw_parse_path(path, server, share, s);
318 if (strncmp(cwd,SMBW_PREFIX,l) == 0 &&
319 (cwd[l] == '/' || cwd[l] == 0)) {
320 return True;
323 return False;
326 /*****************************************************
327 return a unix errno from a SMB error pair
328 *******************************************************/
329 int smbw_errno(struct cli_state *c)
331 uint8 eclass;
332 uint32 ecode;
333 int ret;
335 ret = cli_error(c, &eclass, &ecode);
337 if (ret) {
338 DEBUG(3,("smbw_error %d %d (0x%x)\n",
339 (int)eclass, (int)ecode, (int)ecode));
341 return ret;
344 /*****************************************************
345 return a connection to a server (existing or new)
346 *******************************************************/
347 struct smbw_server *smbw_server(char *server, char *share)
349 struct smbw_server *srv=NULL;
350 static struct cli_state c;
351 char *username;
352 char *password;
353 char *workgroup;
354 struct nmb_name called, calling;
356 username = getenv("SMBW_USER");
357 if (!username) username = getenv("USER");
358 if (!username) username = "guest";
360 workgroup = getenv("SMBW_WORKGROUP");
361 if (!workgroup) workgroup = lp_workgroup();
363 password = getenv("SMBW_PASSWORD");
364 if (!password) password = "";
366 /* try to use an existing connection */
367 for (srv=smbw_srvs;srv;srv=srv->next) {
368 if (strcmp(server,srv->server_name)==0 &&
369 strcmp(share,srv->share_name)==0) return srv;
372 if (server[0] == 0) {
373 errno = EPERM;
374 return NULL;
377 /* have to open a new connection */
378 if (!cli_initialise(&c) || !cli_connect(&c, server, NULL)) {
379 errno = ENOENT;
380 return NULL;
383 make_nmb_name(&calling, local_machine, 0x0, "");
384 make_nmb_name(&called , server, 0x20, "");
386 if (!cli_session_request(&c, &calling, &called)) {
387 cli_shutdown(&c);
388 errno = ENOENT;
389 return NULL;
392 if (!cli_negprot(&c)) {
393 cli_shutdown(&c);
394 errno = ENOENT;
395 return NULL;
398 if (!cli_session_setup(&c, username,
399 password, strlen(password),
400 password, strlen(password),
401 workgroup)) {
402 cli_shutdown(&c);
403 errno = EPERM;
404 return NULL;
407 if (!cli_send_tconX(&c, share,
408 strstr(share,"IPC$")?"IPC":"A:",
409 password, strlen(password)+1)) {
410 errno = smbw_errno(&c);
411 cli_shutdown(&c);
412 return NULL;
415 srv = (struct smbw_server *)malloc(sizeof(*srv));
416 if (!srv) {
417 errno = ENOMEM;
418 goto failed;
421 ZERO_STRUCTP(srv);
423 srv->cli = c;
425 srv->server_name = strdup(server);
426 if (!srv->server_name) {
427 errno = ENOMEM;
428 goto failed;
431 srv->share_name = strdup(share);
432 if (!srv->share_name) {
433 errno = ENOMEM;
434 goto failed;
437 /* some programs play with file descriptors fairly intimately. We
438 try to get out of the way by duping to a high fd number */
439 if (fcntl(SMBW_CLI_FD + srv->cli.fd, F_GETFD) && errno == EBADF) {
440 if (dup2(srv->cli.fd,SMBW_CLI_FD+srv->cli.fd) ==
441 srv->cli.fd+SMBW_CLI_FD) {
442 close(srv->cli.fd);
443 srv->cli.fd += SMBW_CLI_FD;
447 DLIST_ADD(smbw_srvs, srv);
449 return srv;
451 failed:
452 cli_shutdown(&c);
453 if (!srv) return NULL;
455 if (srv->server_name) free(srv->server_name);
456 if (srv->share_name) free(srv->share_name);
457 free(srv);
458 return NULL;
462 /*****************************************************
463 map a fd to a smbw_file structure
464 *******************************************************/
465 struct smbw_file *smbw_file(int fd)
467 struct smbw_file *file;
469 for (file=smbw_files;file;file=file->next) {
470 if (file->fd == fd) return file;
472 return NULL;
475 /*****************************************************
476 map a fd to a smbw_dir structure
477 *******************************************************/
478 struct smbw_dir *smbw_dir(int fd)
480 struct smbw_dir *dir;
482 for (dir=smbw_dirs;dir;dir=dir->next) {
483 if (dir->fd == fd) return dir;
485 return NULL;
488 /*****************************************************
489 setup basic info in a stat structure
490 *******************************************************/
491 void smbw_setup_stat(struct stat *st, char *fname, size_t size, int mode)
493 ZERO_STRUCTP(st);
495 if (IS_DOS_DIR(mode)) {
496 st->st_mode = SMBW_DIR_MODE;
497 } else {
498 st->st_mode = SMBW_FILE_MODE;
501 st->st_size = size;
502 st->st_blksize = 512;
503 st->st_blocks = (size+511)/512;
504 st->st_uid = getuid();
505 st->st_gid = getgid();
509 /*****************************************************
510 try to do a QPATHINFO and if that fails then do a getatr
511 this is needed because win95 sometimes refuses the qpathinfo
512 *******************************************************/
513 static BOOL smbw_getatr(struct smbw_server *srv, char *path,
514 uint32 *mode, size_t *size,
515 time_t *c_time, time_t *a_time, time_t *m_time)
517 if (cli_qpathinfo(&srv->cli, path, c_time, a_time, m_time,
518 size, mode)) return True;
520 /* if this is NT then don't bother with the getatr */
521 if (srv->cli.capabilities & CAP_NT_SMBS) return False;
523 if (cli_getatr(&srv->cli, path, mode, size, m_time)) {
524 a_time = c_time = m_time;
525 return True;
527 return False;
530 /*****************************************************
531 free a smbw_dir structure and all entries
532 *******************************************************/
533 static void free_dir(struct smbw_dir *dir)
535 if (dir->list) {
536 free(dir->list);
538 ZERO_STRUCTP(dir);
539 free(dir);
543 static struct smbw_dir *cur_dir;
545 /*****************************************************
546 add a entry to a directory listing
547 *******************************************************/
548 void smbw_dir_add(struct file_info *finfo)
550 DEBUG(5,("%s\n", finfo->name));
552 if (cur_dir->malloced == cur_dir->count) {
553 cur_dir->list = (struct file_info *)Realloc(cur_dir->list,
554 sizeof(cur_dir->list[0])*
555 (cur_dir->count+100));
556 if (!cur_dir->list) {
557 /* oops */
558 return;
560 cur_dir->malloced += 100;
563 cur_dir->list[cur_dir->count] = *finfo;
564 cur_dir->count++;
567 /*****************************************************
568 add a entry to a directory listing
569 *******************************************************/
570 void smbw_share_add(const char *share, uint32 type, const char *comment)
572 struct file_info finfo;
574 ZERO_STRUCT(finfo);
576 pstrcpy(finfo.name, share);
577 finfo.mode = aRONLY | aDIR;
579 smbw_dir_add(&finfo);
583 /*****************************************************
584 open a directory on the server
585 *******************************************************/
586 int smbw_dir_open(const char *fname, int flags)
588 fstring server, share;
589 pstring path;
590 struct smbw_server *srv=NULL;
591 struct smbw_dir *dir=NULL;
592 pstring mask;
593 int fd;
595 DEBUG(4,("%s\n", __FUNCTION__));
597 if (!fname) {
598 errno = EINVAL;
599 return -1;
602 smbw_init();
604 /* work out what server they are after */
605 smbw_parse_path(fname, server, share, path);
607 DEBUG(4,("dir_open share=%s\n", share));
609 /* get a connection to the server */
610 srv = smbw_server(server, share);
611 if (!srv) {
612 /* smbw_server sets errno */
613 goto failed;
616 dir = (struct smbw_dir *)malloc(sizeof(*dir));
617 if (!dir) {
618 errno = ENOMEM;
619 goto failed;
622 ZERO_STRUCTP(dir);
624 cur_dir = dir;
626 slprintf(mask, sizeof(mask)-1, "%s\\*", path);
627 string_sub(mask,"\\\\","\\");
629 if (strcmp(share,"IPC$") == 0) {
630 DEBUG(4,("doing NetShareEnum\n"));
631 if (cli_RNetShareEnum(&srv->cli, smbw_share_add) <= 0) {
632 errno = smbw_errno(&srv->cli);
633 goto failed;
635 } else {
636 if (cli_list(&srv->cli, mask, aHIDDEN|aSYSTEM|aDIR,
637 smbw_dir_add) <= 0) {
638 errno = smbw_errno(&srv->cli);
639 goto failed;
643 cur_dir = NULL;
645 fd = bitmap_find(file_bmap, 0);
646 if (fd == -1) {
647 errno = EMFILE;
648 goto failed;
651 DLIST_ADD(smbw_dirs, dir);
653 bitmap_set(file_bmap, fd);
655 dir->fd = fd + SMBW_FD_OFFSET;
657 return dir->fd;
659 failed:
660 if (dir) {
661 free_dir(dir);
664 return -1;
668 /*****************************************************
669 a wrapper for open()
670 *******************************************************/
671 int smbw_open(const char *fname, int flags, mode_t mode)
673 fstring server, share;
674 pstring path;
675 struct smbw_server *srv=NULL;
676 int eno, fd = -1;
677 struct smbw_file *file=NULL;
679 DEBUG(4,("%s\n", __FUNCTION__));
681 smbw_init();
683 if (!fname) {
684 errno = EINVAL;
685 return -1;
688 smbw_busy++;
690 /* work out what server they are after */
691 smbw_parse_path(fname, server, share, path);
693 /* get a connection to the server */
694 srv = smbw_server(server, share);
695 if (!srv) {
696 /* smbw_server sets errno */
697 goto failed;
700 if (path[strlen(path)-1] == '\\') {
701 fd = -1;
702 } else {
703 fd = cli_open(&srv->cli, path, flags, DENY_NONE);
705 if (fd == -1) {
706 /* it might be a directory. Maybe we should use chkpath? */
707 fd = smbw_dir_open(fname, flags);
708 smbw_busy--;
709 return fd;
711 if (fd == -1) {
712 errno = eno;
713 goto failed;
716 file = (struct smbw_file *)malloc(sizeof(*file));
717 if (!file) {
718 errno = ENOMEM;
719 goto failed;
722 ZERO_STRUCTP(file);
724 file->cli_fd = fd;
725 file->fname = strdup(path);
726 if (!file->fname) {
727 errno = ENOMEM;
728 goto failed;
730 file->srv = srv;
731 file->fd = bitmap_find(file_bmap, 0);
733 if (file->fd == -1) {
734 errno = EMFILE;
735 goto failed;
738 bitmap_set(file_bmap, file->fd);
740 file->fd += SMBW_FD_OFFSET;
742 DLIST_ADD(smbw_files, file);
744 DEBUG(4,("opened %s\n", fname));
746 smbw_busy--;
747 return file->fd;
749 failed:
750 if (fd != -1) {
751 cli_close(&srv->cli, fd);
753 if (file) {
754 if (file->fname) {
755 free(file->fname);
757 free(file);
759 smbw_busy--;
760 return -1;
764 /*****************************************************
765 a wrapper for fstat() on a directory
766 *******************************************************/
767 int smbw_dir_fstat(int fd, struct stat *st)
769 struct smbw_dir *dir;
771 DEBUG(4,("%s\n", __FUNCTION__));
773 dir = smbw_dir(fd);
774 if (!dir) {
775 errno = EBADF;
776 return -1;
779 ZERO_STRUCTP(st);
781 smbw_setup_stat(st, "", dir->count*sizeof(struct dirent), aDIR);
783 return 0;
786 /*****************************************************
787 a wrapper for fstat()
788 *******************************************************/
789 int smbw_fstat(int fd, struct stat *st)
791 struct smbw_file *file;
792 time_t c_time, a_time, m_time;
793 uint32 size;
794 int mode;
796 DEBUG(4,("%s\n", __FUNCTION__));
798 smbw_busy++;
800 file = smbw_file(fd);
801 if (!file) {
802 int ret = smbw_dir_fstat(fd, st);
803 smbw_busy--;
804 return ret;
807 if (!cli_qfileinfo(&file->srv->cli, file->cli_fd,
808 &mode, &size, &c_time, &a_time, &m_time) &&
809 !cli_getattrE(&file->srv->cli, file->cli_fd,
810 &mode, &size, &c_time, &a_time, &m_time)) {
811 errno = EINVAL;
812 smbw_busy--;
813 return -1;
816 smbw_setup_stat(st, file->fname, size, mode);
818 st->st_atime = a_time;
819 st->st_ctime = c_time;
820 st->st_mtime = m_time;
822 DEBUG(4,("%s - OK\n", __FUNCTION__));
824 smbw_busy--;
825 return 0;
829 /*****************************************************
830 a wrapper for stat()
831 *******************************************************/
832 int smbw_stat(const char *fname, struct stat *st)
834 struct smbw_server *srv;
835 fstring server, share;
836 pstring path;
837 time_t m_time=0, a_time=0, c_time=0;
838 size_t size=0;
839 uint32 mode=0;
841 DEBUG(4,("%s (%s)\n", __FUNCTION__, fname));
843 if (!fname) {
844 errno = EINVAL;
845 return -1;
848 smbw_init();
850 smbw_busy++;
852 /* work out what server they are after */
853 smbw_parse_path(fname, server, share, path);
855 /* get a connection to the server */
856 srv = smbw_server(server, share);
857 if (!srv) {
858 /* smbw_server sets errno */
859 goto failed;
862 if (strcmp(share,"IPC$") == 0) {
863 mode = aDIR | aRONLY;
864 } else {
865 if (!smbw_getatr(srv, path,
866 &mode, &size, &c_time, &a_time, &m_time)) {
867 errno = smbw_errno(&srv->cli);
868 goto failed;
872 smbw_setup_stat(st, path, size, mode);
874 st->st_atime = time(NULL);
875 st->st_ctime = m_time;
876 st->st_mtime = m_time;
878 smbw_busy--;
879 return 0;
881 failed:
882 smbw_busy--;
883 return -1;
886 /*****************************************************
887 a wrapper for read()
888 *******************************************************/
889 ssize_t smbw_read(int fd, void *buf, size_t count)
891 struct smbw_file *file;
892 int ret;
894 DEBUG(4,("%s\n", __FUNCTION__));
896 smbw_busy++;
898 file = smbw_file(fd);
899 if (!file) {
900 errno = EBADF;
901 smbw_busy--;
902 return -1;
905 ret = cli_read(&file->srv->cli, file->cli_fd, buf, file->offset, count);
907 if (ret == -1) {
908 errno = smbw_errno(&file->srv->cli);
909 smbw_busy--;
910 return -1;
913 file->offset += ret;
915 smbw_busy--;
916 return ret;
919 /*****************************************************
920 a wrapper for write()
921 *******************************************************/
922 ssize_t smbw_write(int fd, void *buf, size_t count)
924 struct smbw_file *file;
925 int ret;
927 DEBUG(4,("%s\n", __FUNCTION__));
929 smbw_busy++;
931 file = smbw_file(fd);
932 if (!file) {
933 DEBUG(3,("bad fd in read\n"));
934 errno = EBADF;
935 smbw_busy--;
936 return -1;
939 ret = cli_write(&file->srv->cli, file->cli_fd, buf, file->offset, count);
941 if (ret == -1) {
942 errno = smbw_errno(&file->srv->cli);
943 smbw_busy--;
944 return -1;
947 file->offset += ret;
949 smbw_busy--;
950 return ret;
953 /*****************************************************
954 close a directory handle
955 *******************************************************/
956 int smbw_dir_close(int fd)
958 struct smbw_dir *dir;
960 DEBUG(4,("%s\n", __FUNCTION__));
962 dir = smbw_dir(fd);
963 if (!dir) {
964 DEBUG(4,("%s(%d)\n", __FUNCTION__, __LINE__));
965 errno = EBADF;
966 return -1;
969 bitmap_clear(file_bmap, dir->fd - SMBW_FD_OFFSET);
971 DLIST_REMOVE(smbw_dirs, dir);
973 free_dir(dir);
975 return 0;
978 /*****************************************************
979 a wrapper for close()
980 *******************************************************/
981 int smbw_close(int fd)
983 struct smbw_file *file;
985 DEBUG(4,("%s\n", __FUNCTION__));
987 smbw_busy++;
989 file = smbw_file(fd);
990 if (!file) {
991 int ret = smbw_dir_close(fd);
992 smbw_busy--;
993 return ret;
996 if (!cli_close(&file->srv->cli, file->cli_fd)) {
997 errno = smbw_errno(&file->srv->cli);
998 smbw_busy--;
999 return -1;
1003 bitmap_clear(file_bmap, file->fd - SMBW_FD_OFFSET);
1005 DLIST_REMOVE(smbw_files, file);
1007 free(file->fname);
1008 ZERO_STRUCTP(file);
1009 free(file);
1011 smbw_busy--;
1013 return 0;
1017 /*****************************************************
1018 a wrapper for fcntl()
1019 *******************************************************/
1020 int smbw_fcntl(int fd, int cmd, long arg)
1022 DEBUG(4,("%s\n", __FUNCTION__));
1023 return 0;
1027 /*****************************************************
1028 a wrapper for getdents()
1029 *******************************************************/
1030 int smbw_getdents(unsigned int fd, struct dirent *dirp, int count)
1032 struct smbw_dir *dir;
1033 int n=0;
1035 DEBUG(4,("%s\n", __FUNCTION__));
1037 smbw_busy++;
1039 dir = smbw_dir(fd);
1040 if (!dir) {
1041 errno = EBADF;
1042 smbw_busy--;
1043 return -1;
1046 while (count>=sizeof(*dirp) && (dir->offset < dir->count)) {
1047 dirp->d_ino = dir->offset + 0x10000;
1048 dirp->d_off = (dir->offset+1)*sizeof(*dirp);
1049 dirp->d_reclen = sizeof(*dirp);
1050 /* what's going on with the -1 here? maybe d_type
1051 isn't really there? */
1052 safe_strcpy(&dirp->d_name[-1], dir->list[dir->offset].name,
1053 sizeof(dirp->d_name)-1);
1054 dir->offset++;
1055 count -= dirp->d_reclen;
1056 dirp++;
1057 n++;
1060 smbw_busy--;
1061 return n*sizeof(*dirp);
1065 /*****************************************************
1066 a wrapper for access()
1067 *******************************************************/
1068 int smbw_access(const char *name, int mode)
1070 struct stat st;
1071 /* how do we map this properly ?? */
1072 return smbw_stat(name, &st) == 0;
1075 /*****************************************************
1076 a wrapper for realink() - needed for correct errno setting
1077 *******************************************************/
1078 int smbw_readlink(const char *path, char *buf, size_t bufsize)
1080 struct stat st;
1081 int ret;
1083 ret = smbw_stat(path, &st);
1084 if (ret != 0) {
1085 DEBUG(4,("readlink(%s) failed\n", path));
1086 return -1;
1089 /* it exists - say it isn't a link */
1090 DEBUG(4,("readlink(%s) not a link\n", path));
1092 errno = EINVAL;
1093 return -1;
1097 /*****************************************************
1098 a wrapper for chdir()
1099 *******************************************************/
1100 int smbw_chdir(const char *name)
1102 struct smbw_server *srv;
1103 fstring server, share;
1104 pstring path;
1105 uint32 mode = aDIR;
1106 char *cwd;
1108 smbw_init();
1110 if (smbw_busy) return real_chdir(cwd);
1112 smbw_busy++;
1114 if (!name) {
1115 errno = EINVAL;
1116 goto failed;
1119 DEBUG(4,("%s (%s)\n", __FUNCTION__, name));
1121 /* work out what server they are after */
1122 cwd = smbw_parse_path(name, server, share, path);
1124 if (strncmp(cwd,SMBW_PREFIX,strlen(SMBW_PREFIX))) {
1125 if (real_chdir(cwd) == 0) {
1126 DEBUG(4,("set SMBW_CWD to %s\n", cwd));
1127 pstrcpy(smb_cwd, cwd);
1128 if (setenv(SMBW_PWD_ENV, smb_cwd, 1)) {
1129 DEBUG(4,("setenv failed\n"));
1131 goto success;
1133 errno = ENOENT;
1134 goto failed;
1137 /* get a connection to the server */
1138 srv = smbw_server(server, share);
1139 if (!srv) {
1140 /* smbw_server sets errno */
1141 goto failed;
1144 if (strcmp(share,"IPC$") &&
1145 !smbw_getatr(srv, path,
1146 &mode, NULL, NULL, NULL, NULL)) {
1147 errno = smbw_errno(&srv->cli);
1148 goto failed;
1151 if (!(mode & aDIR)) {
1152 errno = ENOTDIR;
1153 goto failed;
1156 DEBUG(4,("set SMBW_CWD2 to %s\n", cwd));
1157 pstrcpy(smb_cwd, cwd);
1158 if (setenv(SMBW_PWD_ENV, smb_cwd, 1)) {
1159 DEBUG(4,("setenv failed\n"));
1162 /* we don't want the old directory to be busy */
1163 real_chdir("/");
1165 success:
1166 smbw_busy--;
1167 return 0;
1169 failed:
1170 smbw_busy--;
1171 return -1;
1175 /*****************************************************
1176 a wrapper for unlink()
1177 *******************************************************/
1178 int smbw_unlink(const char *fname)
1180 struct smbw_server *srv;
1181 fstring server, share;
1182 pstring path;
1184 DEBUG(4,("%s (%s)\n", __FUNCTION__, fname));
1186 if (!fname) {
1187 errno = EINVAL;
1188 return -1;
1191 smbw_init();
1193 smbw_busy++;
1195 /* work out what server they are after */
1196 smbw_parse_path(fname, server, share, path);
1198 /* get a connection to the server */
1199 srv = smbw_server(server, share);
1200 if (!srv) {
1201 /* smbw_server sets errno */
1202 goto failed;
1205 if (!cli_unlink(&srv->cli, path)) {
1206 errno = smbw_errno(&srv->cli);
1207 goto failed;
1210 smbw_busy--;
1211 return 0;
1213 failed:
1214 smbw_busy--;
1215 return -1;
1219 /*****************************************************
1220 a wrapper for rename()
1221 *******************************************************/
1222 int smbw_rename(const char *oldname, const char *newname)
1224 struct smbw_server *srv;
1225 fstring server1, share1;
1226 pstring path1;
1227 fstring server2, share2;
1228 pstring path2;
1230 DEBUG(4,("%s (%s, %s)\n", __FUNCTION__, oldname, newname));
1232 if (!oldname || !newname) {
1233 errno = EINVAL;
1234 return -1;
1237 smbw_init();
1239 smbw_busy++;
1241 /* work out what server they are after */
1242 smbw_parse_path(oldname, server1, share1, path1);
1243 smbw_parse_path(newname, server2, share2, path2);
1245 if (strcmp(server1, server2) || strcmp(share1, share2)) {
1246 /* can't cross filesystems */
1247 errno = EXDEV;
1248 return -1;
1251 /* get a connection to the server */
1252 srv = smbw_server(server1, share1);
1253 if (!srv) {
1254 /* smbw_server sets errno */
1255 goto failed;
1258 if (!cli_rename(&srv->cli, path1, path2)) {
1259 errno = smbw_errno(&srv->cli);
1260 goto failed;
1263 smbw_busy--;
1264 return 0;
1266 failed:
1267 smbw_busy--;
1268 return -1;
1272 /*****************************************************
1273 a wrapper for utime()
1274 *******************************************************/
1275 int smbw_utime(const char *fname, struct utimbuf *buf)
1277 struct smbw_server *srv;
1278 fstring server, share;
1279 pstring path;
1280 uint32 mode;
1282 DEBUG(4,("%s (%s)\n", __FUNCTION__, fname));
1284 if (!fname) {
1285 errno = EINVAL;
1286 return -1;
1289 smbw_init();
1291 smbw_busy++;
1293 /* work out what server they are after */
1294 smbw_parse_path(fname, server, share, path);
1296 /* get a connection to the server */
1297 srv = smbw_server(server, share);
1298 if (!srv) {
1299 /* smbw_server sets errno */
1300 goto failed;
1303 if (!cli_getatr(&srv->cli, path, &mode, NULL, NULL)) {
1304 errno = smbw_errno(&srv->cli);
1305 goto failed;
1308 if (!cli_setatr(&srv->cli, path, mode, buf->modtime)) {
1309 errno = smbw_errno(&srv->cli);
1310 goto failed;
1313 smbw_busy--;
1314 return 0;
1316 failed:
1317 smbw_busy--;
1318 return -1;
1321 /*****************************************************
1322 a wrapper for chown()
1323 *******************************************************/
1324 int smbw_chown(const char *fname, uid_t owner, gid_t group)
1326 struct smbw_server *srv;
1327 fstring server, share;
1328 pstring path;
1329 uint32 mode;
1331 DEBUG(4,("%s (%s)\n", __FUNCTION__, fname));
1333 if (!fname) {
1334 errno = EINVAL;
1335 return -1;
1338 smbw_init();
1340 smbw_busy++;
1342 /* work out what server they are after */
1343 smbw_parse_path(fname, server, share, path);
1345 /* get a connection to the server */
1346 srv = smbw_server(server, share);
1347 if (!srv) {
1348 /* smbw_server sets errno */
1349 goto failed;
1352 if (!cli_getatr(&srv->cli, path, &mode, NULL, NULL)) {
1353 errno = smbw_errno(&srv->cli);
1354 goto failed;
1357 /* assume success */
1359 smbw_busy--;
1360 return 0;
1362 failed:
1363 smbw_busy--;
1364 return -1;
1367 /*****************************************************
1368 a wrapper for chmod()
1369 *******************************************************/
1370 int smbw_chmod(const char *fname, mode_t newmode)
1372 struct smbw_server *srv;
1373 fstring server, share;
1374 pstring path;
1375 uint32 mode;
1377 DEBUG(4,("%s (%s)\n", __FUNCTION__, fname));
1379 if (!fname) {
1380 errno = EINVAL;
1381 return -1;
1384 smbw_init();
1386 smbw_busy++;
1388 /* work out what server they are after */
1389 smbw_parse_path(fname, server, share, path);
1391 /* get a connection to the server */
1392 srv = smbw_server(server, share);
1393 if (!srv) {
1394 /* smbw_server sets errno */
1395 goto failed;
1398 if (!cli_getatr(&srv->cli, path, &mode, NULL, NULL)) {
1399 errno = smbw_errno(&srv->cli);
1400 goto failed;
1403 /* assume success for the moment - need to add attribute mapping */
1405 smbw_busy--;
1406 return 0;
1408 failed:
1409 smbw_busy--;
1410 return -1;
1413 /*****************************************************
1414 a wrapper for lseek()
1415 *******************************************************/
1416 ssize_t smbw_lseek(int fd, off_t offset, int whence)
1418 struct smbw_file *file;
1419 uint32 size;
1421 DEBUG(4,("%s\n", __FUNCTION__));
1423 smbw_busy++;
1425 file = smbw_file(fd);
1426 if (!file) {
1427 errno = EBADF;
1428 smbw_busy--;
1429 return -1;
1432 switch (whence) {
1433 case SEEK_SET:
1434 file->offset = offset;
1435 break;
1436 case SEEK_CUR:
1437 file->offset += offset;
1438 break;
1439 case SEEK_END:
1440 if (!cli_qfileinfo(&file->srv->cli, file->cli_fd,
1441 NULL, &size, NULL, NULL, NULL) &&
1442 !cli_getattrE(&file->srv->cli, file->cli_fd,
1443 NULL, &size, NULL, NULL, NULL)) {
1444 errno = EINVAL;
1445 smbw_busy--;
1446 return -1;
1448 file->offset = size + offset;
1449 break;
1452 smbw_busy--;
1453 return file->offset;