More warning fixes for Solaris.
[Samba.git] / source / libsmb / libsmb_dir.c
blob89782ce2a1dcb7d17114c63eef1ce8c91fa40ca7
1 /*
2 Unix SMB/Netbios implementation.
3 SMB client library implementation
4 Copyright (C) Andrew Tridgell 1998
5 Copyright (C) Richard Sharpe 2000, 2002
6 Copyright (C) John Terpstra 2000
7 Copyright (C) Tom Jansen (Ninja ISD) 2002
8 Copyright (C) Derrell Lipman 2003-2008
9 Copyright (C) Jeremy Allison 2007, 2008
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "includes.h"
26 #include "libsmbclient.h"
27 #include "libsmb_internal.h"
31 * Routine to open a directory
32 * We accept the URL syntax explained in SMBC_parse_path(), above.
35 static void
36 remove_dir(SMBCFILE *dir)
38 struct smbc_dir_list *d,*f;
40 d = dir->dir_list;
41 while (d) {
43 f = d; d = d->next;
45 SAFE_FREE(f->dirent);
46 SAFE_FREE(f);
50 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
54 static int
55 add_dirent(SMBCFILE *dir,
56 const char *name,
57 const char *comment,
58 uint32 type)
60 struct smbc_dirent *dirent;
61 int size;
62 int name_length = (name == NULL ? 0 : strlen(name));
63 int comment_len = (comment == NULL ? 0 : strlen(comment));
66 * Allocate space for the dirent, which must be increased by the
67 * size of the name and the comment and 1 each for the null terminator.
70 size = sizeof(struct smbc_dirent) + name_length + comment_len + 2;
72 dirent = (struct smbc_dirent *)SMB_MALLOC(size);
74 if (!dirent) {
76 dir->dir_error = ENOMEM;
77 return -1;
81 ZERO_STRUCTP(dirent);
83 if (dir->dir_list == NULL) {
85 dir->dir_list = SMB_MALLOC_P(struct smbc_dir_list);
86 if (!dir->dir_list) {
88 SAFE_FREE(dirent);
89 dir->dir_error = ENOMEM;
90 return -1;
93 ZERO_STRUCTP(dir->dir_list);
95 dir->dir_end = dir->dir_next = dir->dir_list;
97 else {
99 dir->dir_end->next = SMB_MALLOC_P(struct smbc_dir_list);
101 if (!dir->dir_end->next) {
103 SAFE_FREE(dirent);
104 dir->dir_error = ENOMEM;
105 return -1;
108 ZERO_STRUCTP(dir->dir_end->next);
110 dir->dir_end = dir->dir_end->next;
113 dir->dir_end->next = NULL;
114 dir->dir_end->dirent = dirent;
116 dirent->smbc_type = type;
117 dirent->namelen = name_length;
118 dirent->commentlen = comment_len;
119 dirent->dirlen = size;
122 * dirent->namelen + 1 includes the null (no null termination needed)
123 * Ditto for dirent->commentlen.
124 * The space for the two null bytes was allocated.
126 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
127 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
128 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
130 return 0;
134 static void
135 list_unique_wg_fn(const char *name,
136 uint32 type,
137 const char *comment,
138 void *state)
140 SMBCFILE *dir = (SMBCFILE *)state;
141 struct smbc_dir_list *dir_list;
142 struct smbc_dirent *dirent;
143 int dirent_type;
144 int do_remove = 0;
146 dirent_type = dir->dir_type;
148 if (add_dirent(dir, name, comment, dirent_type) < 0) {
150 /* An error occurred, what do we do? */
151 /* FIXME: Add some code here */
154 /* Point to the one just added */
155 dirent = dir->dir_end->dirent;
157 /* See if this was a duplicate */
158 for (dir_list = dir->dir_list;
159 dir_list != dir->dir_end;
160 dir_list = dir_list->next) {
161 if (! do_remove &&
162 strcmp(dir_list->dirent->name, dirent->name) == 0) {
163 /* Duplicate. End end of list need to be removed. */
164 do_remove = 1;
167 if (do_remove && dir_list->next == dir->dir_end) {
168 /* Found the end of the list. Remove it. */
169 dir->dir_end = dir_list;
170 free(dir_list->next);
171 free(dirent);
172 dir_list->next = NULL;
173 break;
178 static void
179 list_fn(const char *name,
180 uint32 type,
181 const char *comment,
182 void *state)
184 SMBCFILE *dir = (SMBCFILE *)state;
185 int dirent_type;
188 * We need to process the type a little ...
190 * Disk share = 0x00000000
191 * Print share = 0x00000001
192 * Comms share = 0x00000002 (obsolete?)
193 * IPC$ share = 0x00000003
195 * administrative shares:
196 * ADMIN$, IPC$, C$, D$, E$ ... are type |= 0x80000000
199 if (dir->dir_type == SMBC_FILE_SHARE) {
200 switch (type) {
201 case 0 | 0x80000000:
202 case 0:
203 dirent_type = SMBC_FILE_SHARE;
204 break;
206 case 1:
207 dirent_type = SMBC_PRINTER_SHARE;
208 break;
210 case 2:
211 dirent_type = SMBC_COMMS_SHARE;
212 break;
214 case 3 | 0x80000000:
215 case 3:
216 dirent_type = SMBC_IPC_SHARE;
217 break;
219 default:
220 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
221 break;
224 else {
225 dirent_type = dir->dir_type;
228 if (add_dirent(dir, name, comment, dirent_type) < 0) {
230 /* An error occurred, what do we do? */
231 /* FIXME: Add some code here */
236 static void
237 dir_list_fn(const char *mnt,
238 file_info *finfo,
239 const char *mask,
240 void *state)
243 if (add_dirent((SMBCFILE *)state, finfo->name, "",
244 (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
246 /* Handle an error ... */
248 /* FIXME: Add some code ... */
254 static int
255 net_share_enum_rpc(struct cli_state *cli,
256 void (*fn)(const char *name,
257 uint32 type,
258 const char *comment,
259 void *state),
260 void *state)
262 int i;
263 WERROR result;
264 uint32 preferred_len = 0xffffffff;
265 uint32 type;
266 struct srvsvc_NetShareInfoCtr info_ctr;
267 struct srvsvc_NetShareCtr1 ctr1;
268 fstring name = "";
269 fstring comment = "";
270 struct rpc_pipe_client *pipe_hnd;
271 NTSTATUS nt_status;
272 uint32_t resume_handle = 0;
273 uint32_t total_entries = 0;
275 /* Open the server service pipe */
276 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_srvsvc.syntax_id,
277 &pipe_hnd);
278 if (!NT_STATUS_IS_OK(nt_status)) {
279 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
280 return -1;
283 ZERO_STRUCT(info_ctr);
284 ZERO_STRUCT(ctr1);
286 info_ctr.level = 1;
287 info_ctr.ctr.ctr1 = &ctr1;
289 /* Issue the NetShareEnum RPC call and retrieve the response */
290 nt_status = rpccli_srvsvc_NetShareEnumAll(pipe_hnd, talloc_tos(),
291 pipe_hnd->desthost,
292 &info_ctr,
293 preferred_len,
294 &total_entries,
295 &resume_handle,
296 &result);
298 /* Was it successful? */
299 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result) ||
300 total_entries == 0) {
301 /* Nope. Go clean up. */
302 goto done;
305 /* For each returned entry... */
306 for (i = 0; i < total_entries; i++) {
308 /* pull out the share name */
309 fstrcpy(name, info_ctr.ctr.ctr1->array[i].name);
311 /* pull out the share's comment */
312 fstrcpy(comment, info_ctr.ctr.ctr1->array[i].comment);
314 /* Get the type value */
315 type = info_ctr.ctr.ctr1->array[i].type;
317 /* Add this share to the list */
318 (*fn)(name, type, comment, state);
321 done:
322 /* Close the server service pipe */
323 TALLOC_FREE(pipe_hnd);
325 /* Tell 'em if it worked */
326 return W_ERROR_IS_OK(result) ? 0 : -1;
331 * Verify that the options specified in a URL are valid
334 SMBC_check_options(char *server,
335 char *share,
336 char *path,
337 char *options)
339 DEBUG(4, ("SMBC_check_options(): server='%s' share='%s' "
340 "path='%s' options='%s'\n",
341 server, share, path, options));
343 /* No options at all is always ok */
344 if (! *options) return 0;
346 /* Currently, we don't support any options. */
347 return -1;
351 SMBCFILE *
352 SMBC_opendir_ctx(SMBCCTX *context,
353 const char *fname)
355 int saved_errno;
356 char *server = NULL;
357 char *share = NULL;
358 char *user = NULL;
359 char *password = NULL;
360 char *options = NULL;
361 char *workgroup = NULL;
362 char *path = NULL;
363 uint16 mode;
364 char *p = NULL;
365 SMBCSRV *srv = NULL;
366 SMBCFILE *dir = NULL;
367 struct sockaddr_storage rem_ss;
368 TALLOC_CTX *frame = talloc_stackframe();
370 if (!context || !context->internal->initialized) {
371 DEBUG(4, ("no valid context\n"));
372 errno = EINVAL + 8192;
373 TALLOC_FREE(frame);
374 return NULL;
378 if (!fname) {
379 DEBUG(4, ("no valid fname\n"));
380 errno = EINVAL + 8193;
381 TALLOC_FREE(frame);
382 return NULL;
385 if (SMBC_parse_path(frame,
386 context,
387 fname,
388 &workgroup,
389 &server,
390 &share,
391 &path,
392 &user,
393 &password,
394 &options)) {
395 DEBUG(4, ("no valid path\n"));
396 errno = EINVAL + 8194;
397 TALLOC_FREE(frame);
398 return NULL;
401 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
402 "path='%s' options='%s'\n",
403 fname, server, share, path, options));
405 /* Ensure the options are valid */
406 if (SMBC_check_options(server, share, path, options)) {
407 DEBUG(4, ("unacceptable options (%s)\n", options));
408 errno = EINVAL + 8195;
409 TALLOC_FREE(frame);
410 return NULL;
413 if (!user || user[0] == (char)0) {
414 user = talloc_strdup(frame, smbc_getUser(context));
415 if (!user) {
416 errno = ENOMEM;
417 TALLOC_FREE(frame);
418 return NULL;
422 dir = SMB_MALLOC_P(SMBCFILE);
424 if (!dir) {
425 errno = ENOMEM;
426 TALLOC_FREE(frame);
427 return NULL;
430 ZERO_STRUCTP(dir);
432 dir->cli_fd = 0;
433 dir->fname = SMB_STRDUP(fname);
434 dir->srv = NULL;
435 dir->offset = 0;
436 dir->file = False;
437 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
439 if (server[0] == (char)0) {
441 int i;
442 int count;
443 int max_lmb_count;
444 struct ip_service *ip_list;
445 struct ip_service server_addr;
446 struct user_auth_info u_info;
448 if (share[0] != (char)0 || path[0] != (char)0) {
450 errno = EINVAL + 8196;
451 if (dir) {
452 SAFE_FREE(dir->fname);
453 SAFE_FREE(dir);
455 TALLOC_FREE(frame);
456 return NULL;
459 /* Determine how many local master browsers to query */
460 max_lmb_count = (smbc_getOptionBrowseMaxLmbCount(context) == 0
461 ? INT_MAX
462 : smbc_getOptionBrowseMaxLmbCount(context));
464 memset(&u_info, '\0', sizeof(u_info));
465 u_info.username = talloc_strdup(frame,user);
466 u_info.password = talloc_strdup(frame,password);
467 if (!u_info.username || !u_info.password) {
468 if (dir) {
469 SAFE_FREE(dir->fname);
470 SAFE_FREE(dir);
472 TALLOC_FREE(frame);
473 return NULL;
477 * We have server and share and path empty but options
478 * requesting that we scan all master browsers for their list
479 * of workgroups/domains. This implies that we must first try
480 * broadcast queries to find all master browsers, and if that
481 * doesn't work, then try our other methods which return only
482 * a single master browser.
485 ip_list = NULL;
486 if (!NT_STATUS_IS_OK(name_resolve_bcast(MSBROWSE, 1, &ip_list,
487 &count)))
490 SAFE_FREE(ip_list);
492 if (!find_master_ip(workgroup, &server_addr.ss)) {
494 if (dir) {
495 SAFE_FREE(dir->fname);
496 SAFE_FREE(dir);
498 errno = ENOENT;
499 TALLOC_FREE(frame);
500 return NULL;
503 ip_list = (struct ip_service *)memdup(
504 &server_addr, sizeof(server_addr));
505 if (ip_list == NULL) {
506 errno = ENOMEM;
507 TALLOC_FREE(frame);
508 return NULL;
510 count = 1;
513 for (i = 0; i < count && i < max_lmb_count; i++) {
514 char addr[INET6_ADDRSTRLEN];
515 char *wg_ptr = NULL;
516 struct cli_state *cli = NULL;
518 print_sockaddr(addr, sizeof(addr), &ip_list[i].ss);
519 DEBUG(99, ("Found master browser %d of %d: %s\n",
520 i+1, MAX(count, max_lmb_count),
521 addr));
523 cli = get_ipc_connect_master_ip(talloc_tos(),
524 &ip_list[i],
525 &u_info,
526 &wg_ptr);
527 /* cli == NULL is the master browser refused to talk or
528 could not be found */
529 if (!cli) {
530 continue;
533 workgroup = talloc_strdup(frame, wg_ptr);
534 server = talloc_strdup(frame, cli->desthost);
536 cli_shutdown(cli);
538 if (!workgroup || !server) {
539 errno = ENOMEM;
540 TALLOC_FREE(frame);
541 return NULL;
544 DEBUG(4, ("using workgroup %s %s\n",
545 workgroup, server));
548 * For each returned master browser IP address, get a
549 * connection to IPC$ on the server if we do not
550 * already have one, and determine the
551 * workgroups/domains that it knows about.
554 srv = SMBC_server(frame, context, True, server, "IPC$",
555 &workgroup, &user, &password);
556 if (!srv) {
557 continue;
560 dir->srv = srv;
561 dir->dir_type = SMBC_WORKGROUP;
563 /* Now, list the stuff ... */
565 if (!cli_NetServerEnum(srv->cli,
566 workgroup,
567 SV_TYPE_DOMAIN_ENUM,
568 list_unique_wg_fn,
569 (void *)dir)) {
570 continue;
574 SAFE_FREE(ip_list);
575 } else {
577 * Server not an empty string ... Check the rest and see what
578 * gives
580 if (*share == '\0') {
581 if (*path != '\0') {
583 /* Should not have empty share with path */
584 errno = EINVAL + 8197;
585 if (dir) {
586 SAFE_FREE(dir->fname);
587 SAFE_FREE(dir);
589 TALLOC_FREE(frame);
590 return NULL;
595 * We don't know if <server> is really a server name
596 * or is a workgroup/domain name. If we already have
597 * a server structure for it, we'll use it.
598 * Otherwise, check to see if <server><1D>,
599 * <server><1B>, or <server><20> translates. We check
600 * to see if <server> is an IP address first.
604 * See if we have an existing server. Do not
605 * establish a connection if one does not already
606 * exist.
608 srv = SMBC_server(frame, context, False,
609 server, "IPC$",
610 &workgroup, &user, &password);
613 * If no existing server and not an IP addr, look for
614 * LMB or DMB
616 if (!srv &&
617 !is_ipaddress(server) &&
618 (resolve_name(server, &rem_ss, 0x1d) || /* LMB */
619 resolve_name(server, &rem_ss, 0x1b) )) { /* DMB */
621 fstring buserver;
623 dir->dir_type = SMBC_SERVER;
626 * Get the backup list ...
628 if (!name_status_find(server, 0, 0,
629 &rem_ss, buserver)) {
631 DEBUG(0,("Could not get name of "
632 "local/domain master browser "
633 "for server %s\n", server));
634 if (dir) {
635 SAFE_FREE(dir->fname);
636 SAFE_FREE(dir);
638 errno = EPERM;
639 TALLOC_FREE(frame);
640 return NULL;
645 * Get a connection to IPC$ on the server if
646 * we do not already have one
648 srv = SMBC_server(frame, context, True,
649 buserver, "IPC$",
650 &workgroup,
651 &user, &password);
652 if (!srv) {
653 DEBUG(0, ("got no contact to IPC$\n"));
654 if (dir) {
655 SAFE_FREE(dir->fname);
656 SAFE_FREE(dir);
658 TALLOC_FREE(frame);
659 return NULL;
663 dir->srv = srv;
665 /* Now, list the servers ... */
666 if (!cli_NetServerEnum(srv->cli, server,
667 0x0000FFFE, list_fn,
668 (void *)dir)) {
670 if (dir) {
671 SAFE_FREE(dir->fname);
672 SAFE_FREE(dir);
674 TALLOC_FREE(frame);
675 return NULL;
677 } else if (srv ||
678 (resolve_name(server, &rem_ss, 0x20))) {
681 * If we hadn't found the server, get one now
683 if (!srv) {
684 srv = SMBC_server(frame, context, True,
685 server, "IPC$",
686 &workgroup,
687 &user, &password);
690 if (!srv) {
691 if (dir) {
692 SAFE_FREE(dir->fname);
693 SAFE_FREE(dir);
695 TALLOC_FREE(frame);
696 return NULL;
700 dir->dir_type = SMBC_FILE_SHARE;
701 dir->srv = srv;
703 /* List the shares ... */
705 if (net_share_enum_rpc(
706 srv->cli,
707 list_fn,
708 (void *) dir) < 0 &&
709 cli_RNetShareEnum(
710 srv->cli,
711 list_fn,
712 (void *)dir) < 0) {
714 errno = cli_errno(srv->cli);
715 if (dir) {
716 SAFE_FREE(dir->fname);
717 SAFE_FREE(dir);
719 TALLOC_FREE(frame);
720 return NULL;
723 } else {
724 /* Neither the workgroup nor server exists */
725 errno = ECONNREFUSED;
726 if (dir) {
727 SAFE_FREE(dir->fname);
728 SAFE_FREE(dir);
730 TALLOC_FREE(frame);
731 return NULL;
735 else {
737 * The server and share are specified ... work from
738 * there ...
740 char *targetpath;
741 struct cli_state *targetcli;
743 /* We connect to the server and list the directory */
744 dir->dir_type = SMBC_FILE_SHARE;
746 srv = SMBC_server(frame, context, True, server, share,
747 &workgroup, &user, &password);
749 if (!srv) {
750 if (dir) {
751 SAFE_FREE(dir->fname);
752 SAFE_FREE(dir);
754 TALLOC_FREE(frame);
755 return NULL;
758 dir->srv = srv;
760 /* Now, list the files ... */
762 p = path + strlen(path);
763 path = talloc_asprintf_append(path, "\\*");
764 if (!path) {
765 if (dir) {
766 SAFE_FREE(dir->fname);
767 SAFE_FREE(dir);
769 TALLOC_FREE(frame);
770 return NULL;
773 if (!cli_resolve_path(frame, "", srv->cli, path,
774 &targetcli, &targetpath)) {
775 d_printf("Could not resolve %s\n", path);
776 if (dir) {
777 SAFE_FREE(dir->fname);
778 SAFE_FREE(dir);
780 TALLOC_FREE(frame);
781 return NULL;
784 if (cli_list(targetcli, targetpath,
785 aDIR | aSYSTEM | aHIDDEN,
786 dir_list_fn, (void *)dir) < 0) {
788 if (dir) {
789 SAFE_FREE(dir->fname);
790 SAFE_FREE(dir);
792 saved_errno = SMBC_errno(context, targetcli);
794 if (saved_errno == EINVAL) {
796 * See if they asked to opendir
797 * something other than a directory.
798 * If so, the converted error value we
799 * got would have been EINVAL rather
800 * than ENOTDIR.
802 *p = '\0'; /* restore original path */
804 if (SMBC_getatr(context, srv, path,
805 &mode, NULL,
806 NULL, NULL, NULL, NULL,
807 NULL) &&
808 ! IS_DOS_DIR(mode)) {
810 /* It is. Correct the error value */
811 saved_errno = ENOTDIR;
816 * If there was an error and the server is no
817 * good any more...
819 if (cli_is_error(targetcli) &&
820 smbc_getFunctionCheckServer(context)(context, srv)) {
822 /* ... then remove it. */
823 if (smbc_getFunctionRemoveUnusedServer(context)(context,
824 srv)) {
826 * We could not remove the
827 * server completely, remove
828 * it from the cache so we
829 * will not get it again. It
830 * will be removed when the
831 * last file/dir is closed.
833 smbc_getFunctionRemoveCachedServer(context)(context, srv);
837 errno = saved_errno;
838 TALLOC_FREE(frame);
839 return NULL;
845 DLIST_ADD(context->internal->files, dir);
846 TALLOC_FREE(frame);
847 return dir;
852 * Routine to close a directory
856 SMBC_closedir_ctx(SMBCCTX *context,
857 SMBCFILE *dir)
859 TALLOC_CTX *frame = talloc_stackframe();
861 if (!context || !context->internal->initialized) {
862 errno = EINVAL;
863 TALLOC_FREE(frame);
864 return -1;
867 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
868 errno = EBADF;
869 TALLOC_FREE(frame);
870 return -1;
873 remove_dir(dir); /* Clean it up */
875 DLIST_REMOVE(context->internal->files, dir);
877 if (dir) {
879 SAFE_FREE(dir->fname);
880 SAFE_FREE(dir); /* Free the space too */
883 TALLOC_FREE(frame);
884 return 0;
888 static void
889 smbc_readdir_internal(SMBCCTX * context,
890 struct smbc_dirent *dest,
891 struct smbc_dirent *src,
892 int max_namebuf_len)
894 if (smbc_getOptionUrlEncodeReaddirEntries(context)) {
896 /* url-encode the name. get back remaining buffer space */
897 max_namebuf_len =
898 smbc_urlencode(dest->name, src->name, max_namebuf_len);
900 /* We now know the name length */
901 dest->namelen = strlen(dest->name);
903 /* Save the pointer to the beginning of the comment */
904 dest->comment = dest->name + dest->namelen + 1;
906 /* Copy the comment */
907 strncpy(dest->comment, src->comment, max_namebuf_len - 1);
908 dest->comment[max_namebuf_len - 1] = '\0';
910 /* Save other fields */
911 dest->smbc_type = src->smbc_type;
912 dest->commentlen = strlen(dest->comment);
913 dest->dirlen = ((dest->comment + dest->commentlen + 1) -
914 (char *) dest);
915 } else {
917 /* No encoding. Just copy the entry as is. */
918 memcpy(dest, src, src->dirlen);
919 dest->comment = (char *)(&dest->name + src->namelen + 1);
925 * Routine to get a directory entry
928 struct smbc_dirent *
929 SMBC_readdir_ctx(SMBCCTX *context,
930 SMBCFILE *dir)
932 int maxlen;
933 struct smbc_dirent *dirp, *dirent;
934 TALLOC_CTX *frame = talloc_stackframe();
936 /* Check that all is ok first ... */
938 if (!context || !context->internal->initialized) {
940 errno = EINVAL;
941 DEBUG(0, ("Invalid context in SMBC_readdir_ctx()\n"));
942 TALLOC_FREE(frame);
943 return NULL;
947 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
949 errno = EBADF;
950 DEBUG(0, ("Invalid dir in SMBC_readdir_ctx()\n"));
951 TALLOC_FREE(frame);
952 return NULL;
956 if (dir->file != False) { /* FIXME, should be dir, perhaps */
958 errno = ENOTDIR;
959 DEBUG(0, ("Found file vs directory in SMBC_readdir_ctx()\n"));
960 TALLOC_FREE(frame);
961 return NULL;
965 if (!dir->dir_next) {
966 TALLOC_FREE(frame);
967 return NULL;
970 dirent = dir->dir_next->dirent;
971 if (!dirent) {
973 errno = ENOENT;
974 TALLOC_FREE(frame);
975 return NULL;
979 dirp = &context->internal->dirent;
980 maxlen = sizeof(context->internal->_dirent_name);
982 smbc_readdir_internal(context, dirp, dirent, maxlen);
984 dir->dir_next = dir->dir_next->next;
986 TALLOC_FREE(frame);
987 return dirp;
991 * Routine to get directory entries
995 SMBC_getdents_ctx(SMBCCTX *context,
996 SMBCFILE *dir,
997 struct smbc_dirent *dirp,
998 int count)
1000 int rem = count;
1001 int reqd;
1002 int maxlen;
1003 char *ndir = (char *)dirp;
1004 struct smbc_dir_list *dirlist;
1005 TALLOC_CTX *frame = talloc_stackframe();
1007 /* Check that all is ok first ... */
1009 if (!context || !context->internal->initialized) {
1011 errno = EINVAL;
1012 TALLOC_FREE(frame);
1013 return -1;
1017 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
1019 errno = EBADF;
1020 TALLOC_FREE(frame);
1021 return -1;
1025 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1027 errno = ENOTDIR;
1028 TALLOC_FREE(frame);
1029 return -1;
1034 * Now, retrieve the number of entries that will fit in what was passed
1035 * We have to figure out if the info is in the list, or we need to
1036 * send a request to the server to get the info.
1039 while ((dirlist = dir->dir_next)) {
1040 struct smbc_dirent *dirent;
1042 if (!dirlist->dirent) {
1044 errno = ENOENT; /* Bad error */
1045 TALLOC_FREE(frame);
1046 return -1;
1050 /* Do urlencoding of next entry, if so selected */
1051 dirent = &context->internal->dirent;
1052 maxlen = sizeof(context->internal->_dirent_name);
1053 smbc_readdir_internal(context, dirent,
1054 dirlist->dirent, maxlen);
1056 reqd = dirent->dirlen;
1058 if (rem < reqd) {
1060 if (rem < count) { /* We managed to copy something */
1062 errno = 0;
1063 TALLOC_FREE(frame);
1064 return count - rem;
1067 else { /* Nothing copied ... */
1069 errno = EINVAL; /* Not enough space ... */
1070 TALLOC_FREE(frame);
1071 return -1;
1077 memcpy(ndir, dirent, reqd); /* Copy the data in ... */
1079 ((struct smbc_dirent *)ndir)->comment =
1080 (char *)(&((struct smbc_dirent *)ndir)->name +
1081 dirent->namelen +
1084 ndir += reqd;
1086 rem -= reqd;
1088 dir->dir_next = dirlist = dirlist -> next;
1091 TALLOC_FREE(frame);
1093 if (rem == count)
1094 return 0;
1095 else
1096 return count - rem;
1101 * Routine to create a directory ...
1105 SMBC_mkdir_ctx(SMBCCTX *context,
1106 const char *fname,
1107 mode_t mode)
1109 SMBCSRV *srv = NULL;
1110 char *server = NULL;
1111 char *share = NULL;
1112 char *user = NULL;
1113 char *password = NULL;
1114 char *workgroup = NULL;
1115 char *path = NULL;
1116 char *targetpath = NULL;
1117 struct cli_state *targetcli = NULL;
1118 TALLOC_CTX *frame = talloc_stackframe();
1120 if (!context || !context->internal->initialized) {
1121 errno = EINVAL;
1122 TALLOC_FREE(frame);
1123 return -1;
1126 if (!fname) {
1127 errno = EINVAL;
1128 TALLOC_FREE(frame);
1129 return -1;
1132 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
1134 if (SMBC_parse_path(frame,
1135 context,
1136 fname,
1137 &workgroup,
1138 &server,
1139 &share,
1140 &path,
1141 &user,
1142 &password,
1143 NULL)) {
1144 errno = EINVAL;
1145 TALLOC_FREE(frame);
1146 return -1;
1149 if (!user || user[0] == (char)0) {
1150 user = talloc_strdup(frame, smbc_getUser(context));
1151 if (!user) {
1152 errno = ENOMEM;
1153 TALLOC_FREE(frame);
1154 return -1;
1158 srv = SMBC_server(frame, context, True,
1159 server, share, &workgroup, &user, &password);
1161 if (!srv) {
1163 TALLOC_FREE(frame);
1164 return -1; /* errno set by SMBC_server */
1168 /*d_printf(">>>mkdir: resolving %s\n", path);*/
1169 if (!cli_resolve_path(frame, "", srv->cli, path,
1170 &targetcli, &targetpath)) {
1171 d_printf("Could not resolve %s\n", path);
1172 TALLOC_FREE(frame);
1173 return -1;
1175 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
1177 if (!cli_mkdir(targetcli, targetpath)) {
1179 errno = SMBC_errno(context, targetcli);
1180 TALLOC_FREE(frame);
1181 return -1;
1185 TALLOC_FREE(frame);
1186 return 0;
1191 * Our list function simply checks to see if a directory is not empty
1194 static int smbc_rmdir_dirempty = True;
1196 static void
1197 rmdir_list_fn(const char *mnt,
1198 file_info *finfo,
1199 const char *mask,
1200 void *state)
1202 if (strncmp(finfo->name, ".", 1) != 0 &&
1203 strncmp(finfo->name, "..", 2) != 0) {
1204 smbc_rmdir_dirempty = False;
1209 * Routine to remove a directory
1213 SMBC_rmdir_ctx(SMBCCTX *context,
1214 const char *fname)
1216 SMBCSRV *srv = NULL;
1217 char *server = NULL;
1218 char *share = NULL;
1219 char *user = NULL;
1220 char *password = NULL;
1221 char *workgroup = NULL;
1222 char *path = NULL;
1223 char *targetpath = NULL;
1224 struct cli_state *targetcli = NULL;
1225 TALLOC_CTX *frame = talloc_stackframe();
1227 if (!context || !context->internal->initialized) {
1228 errno = EINVAL;
1229 TALLOC_FREE(frame);
1230 return -1;
1233 if (!fname) {
1234 errno = EINVAL;
1235 TALLOC_FREE(frame);
1236 return -1;
1239 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
1241 if (SMBC_parse_path(frame,
1242 context,
1243 fname,
1244 &workgroup,
1245 &server,
1246 &share,
1247 &path,
1248 &user,
1249 &password,
1250 NULL)) {
1251 errno = EINVAL;
1252 TALLOC_FREE(frame);
1253 return -1;
1256 if (!user || user[0] == (char)0) {
1257 user = talloc_strdup(frame, smbc_getUser(context));
1258 if (!user) {
1259 errno = ENOMEM;
1260 TALLOC_FREE(frame);
1261 return -1;
1265 srv = SMBC_server(frame, context, True,
1266 server, share, &workgroup, &user, &password);
1268 if (!srv) {
1270 TALLOC_FREE(frame);
1271 return -1; /* errno set by SMBC_server */
1275 /*d_printf(">>>rmdir: resolving %s\n", path);*/
1276 if (!cli_resolve_path(frame, "", srv->cli, path,
1277 &targetcli, &targetpath)) {
1278 d_printf("Could not resolve %s\n", path);
1279 TALLOC_FREE(frame);
1280 return -1;
1282 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
1285 if (!cli_rmdir(targetcli, targetpath)) {
1287 errno = SMBC_errno(context, targetcli);
1289 if (errno == EACCES) { /* Check if the dir empty or not */
1291 /* Local storage to avoid buffer overflows */
1292 char *lpath;
1294 smbc_rmdir_dirempty = True; /* Make this so ... */
1296 lpath = talloc_asprintf(frame, "%s\\*",
1297 targetpath);
1298 if (!lpath) {
1299 errno = ENOMEM;
1300 TALLOC_FREE(frame);
1301 return -1;
1304 if (cli_list(targetcli, lpath,
1305 aDIR | aSYSTEM | aHIDDEN,
1306 rmdir_list_fn, NULL) < 0) {
1308 /* Fix errno to ignore latest error ... */
1309 DEBUG(5, ("smbc_rmdir: "
1310 "cli_list returned an error: %d\n",
1311 SMBC_errno(context, targetcli)));
1312 errno = EACCES;
1316 if (smbc_rmdir_dirempty)
1317 errno = EACCES;
1318 else
1319 errno = ENOTEMPTY;
1323 TALLOC_FREE(frame);
1324 return -1;
1328 TALLOC_FREE(frame);
1329 return 0;
1334 * Routine to return the current directory position
1337 off_t
1338 SMBC_telldir_ctx(SMBCCTX *context,
1339 SMBCFILE *dir)
1341 TALLOC_CTX *frame = talloc_stackframe();
1343 if (!context || !context->internal->initialized) {
1345 errno = EINVAL;
1346 TALLOC_FREE(frame);
1347 return -1;
1351 if (!dir || !SMBC_dlist_contains(context->internal->files, dir)) {
1353 errno = EBADF;
1354 TALLOC_FREE(frame);
1355 return -1;
1359 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1361 errno = ENOTDIR;
1362 TALLOC_FREE(frame);
1363 return -1;
1367 /* See if we're already at the end. */
1368 if (dir->dir_next == NULL) {
1369 /* We are. */
1370 TALLOC_FREE(frame);
1371 return -1;
1375 * We return the pointer here as the offset
1377 TALLOC_FREE(frame);
1378 return (off_t)(long)dir->dir_next->dirent;
1382 * A routine to run down the list and see if the entry is OK
1385 static struct smbc_dir_list *
1386 check_dir_ent(struct smbc_dir_list *list,
1387 struct smbc_dirent *dirent)
1390 /* Run down the list looking for what we want */
1392 if (dirent) {
1394 struct smbc_dir_list *tmp = list;
1396 while (tmp) {
1398 if (tmp->dirent == dirent)
1399 return tmp;
1401 tmp = tmp->next;
1407 return NULL; /* Not found, or an error */
1413 * Routine to seek on a directory
1417 SMBC_lseekdir_ctx(SMBCCTX *context,
1418 SMBCFILE *dir,
1419 off_t offset)
1421 long int l_offset = offset; /* Handle problems of size */
1422 struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
1423 struct smbc_dir_list *list_ent = (struct smbc_dir_list *)NULL;
1424 TALLOC_CTX *frame = talloc_stackframe();
1426 if (!context || !context->internal->initialized) {
1428 errno = EINVAL;
1429 TALLOC_FREE(frame);
1430 return -1;
1434 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1436 errno = ENOTDIR;
1437 TALLOC_FREE(frame);
1438 return -1;
1442 /* Now, check what we were passed and see if it is OK ... */
1444 if (dirent == NULL) { /* Seek to the begining of the list */
1446 dir->dir_next = dir->dir_list;
1447 TALLOC_FREE(frame);
1448 return 0;
1452 if (offset == -1) { /* Seek to the end of the list */
1453 dir->dir_next = NULL;
1454 TALLOC_FREE(frame);
1455 return 0;
1458 /* Now, run down the list and make sure that the entry is OK */
1459 /* This may need to be changed if we change the format of the list */
1461 if ((list_ent = check_dir_ent(dir->dir_list, dirent)) == NULL) {
1462 errno = EINVAL; /* Bad entry */
1463 TALLOC_FREE(frame);
1464 return -1;
1467 dir->dir_next = list_ent;
1469 TALLOC_FREE(frame);
1470 return 0;
1474 * Routine to fstat a dir
1478 SMBC_fstatdir_ctx(SMBCCTX *context,
1479 SMBCFILE *dir,
1480 struct stat *st)
1483 if (!context || !context->internal->initialized) {
1485 errno = EINVAL;
1486 return -1;
1489 /* No code yet ... */
1490 return 0;
1494 SMBC_chmod_ctx(SMBCCTX *context,
1495 const char *fname,
1496 mode_t newmode)
1498 SMBCSRV *srv = NULL;
1499 char *server = NULL;
1500 char *share = NULL;
1501 char *user = NULL;
1502 char *password = NULL;
1503 char *workgroup = NULL;
1504 char *targetpath = NULL;
1505 struct cli_state *targetcli = NULL;
1506 char *path = NULL;
1507 uint16 mode;
1508 TALLOC_CTX *frame = talloc_stackframe();
1510 if (!context || !context->internal->initialized) {
1512 errno = EINVAL; /* Best I can think of ... */
1513 TALLOC_FREE(frame);
1514 return -1;
1517 if (!fname) {
1518 errno = EINVAL;
1519 TALLOC_FREE(frame);
1520 return -1;
1523 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname, (unsigned int)newmode));
1525 if (SMBC_parse_path(frame,
1526 context,
1527 fname,
1528 &workgroup,
1529 &server,
1530 &share,
1531 &path,
1532 &user,
1533 &password,
1534 NULL)) {
1535 errno = EINVAL;
1536 TALLOC_FREE(frame);
1537 return -1;
1540 if (!user || user[0] == (char)0) {
1541 user = talloc_strdup(frame, smbc_getUser(context));
1542 if (!user) {
1543 errno = ENOMEM;
1544 TALLOC_FREE(frame);
1545 return -1;
1549 srv = SMBC_server(frame, context, True,
1550 server, share, &workgroup, &user, &password);
1552 if (!srv) {
1553 TALLOC_FREE(frame);
1554 return -1; /* errno set by SMBC_server */
1557 /*d_printf(">>>unlink: resolving %s\n", path);*/
1558 if (!cli_resolve_path(frame, "", srv->cli, path,
1559 &targetcli, &targetpath)) {
1560 d_printf("Could not resolve %s\n", path);
1561 TALLOC_FREE(frame);
1562 return -1;
1565 mode = 0;
1567 if (!(newmode & (S_IWUSR | S_IWGRP | S_IWOTH))) mode |= aRONLY;
1568 if ((newmode & S_IXUSR) && lp_map_archive(-1)) mode |= aARCH;
1569 if ((newmode & S_IXGRP) && lp_map_system(-1)) mode |= aSYSTEM;
1570 if ((newmode & S_IXOTH) && lp_map_hidden(-1)) mode |= aHIDDEN;
1572 if (!cli_setatr(targetcli, targetpath, mode, 0)) {
1573 errno = SMBC_errno(context, targetcli);
1574 TALLOC_FREE(frame);
1575 return -1;
1578 TALLOC_FREE(frame);
1579 return 0;
1583 SMBC_utimes_ctx(SMBCCTX *context,
1584 const char *fname,
1585 struct timeval *tbuf)
1587 SMBCSRV *srv = NULL;
1588 char *server = NULL;
1589 char *share = NULL;
1590 char *user = NULL;
1591 char *password = NULL;
1592 char *workgroup = NULL;
1593 char *path = NULL;
1594 time_t access_time;
1595 time_t write_time;
1596 TALLOC_CTX *frame = talloc_stackframe();
1598 if (!context || !context->internal->initialized) {
1600 errno = EINVAL; /* Best I can think of ... */
1601 TALLOC_FREE(frame);
1602 return -1;
1605 if (!fname) {
1606 errno = EINVAL;
1607 TALLOC_FREE(frame);
1608 return -1;
1611 if (tbuf == NULL) {
1612 access_time = write_time = time(NULL);
1613 } else {
1614 access_time = tbuf[0].tv_sec;
1615 write_time = tbuf[1].tv_sec;
1618 if (DEBUGLVL(4)) {
1619 char *p;
1620 char atimebuf[32];
1621 char mtimebuf[32];
1623 strncpy(atimebuf, ctime(&access_time), sizeof(atimebuf) - 1);
1624 atimebuf[sizeof(atimebuf) - 1] = '\0';
1625 if ((p = strchr(atimebuf, '\n')) != NULL) {
1626 *p = '\0';
1629 strncpy(mtimebuf, ctime(&write_time), sizeof(mtimebuf) - 1);
1630 mtimebuf[sizeof(mtimebuf) - 1] = '\0';
1631 if ((p = strchr(mtimebuf, '\n')) != NULL) {
1632 *p = '\0';
1635 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
1636 fname, atimebuf, mtimebuf);
1639 if (SMBC_parse_path(frame,
1640 context,
1641 fname,
1642 &workgroup,
1643 &server,
1644 &share,
1645 &path,
1646 &user,
1647 &password,
1648 NULL)) {
1649 errno = EINVAL;
1650 TALLOC_FREE(frame);
1651 return -1;
1654 if (!user || user[0] == (char)0) {
1655 user = talloc_strdup(frame, smbc_getUser(context));
1656 if (!user) {
1657 errno = ENOMEM;
1658 TALLOC_FREE(frame);
1659 return -1;
1663 srv = SMBC_server(frame, context, True,
1664 server, share, &workgroup, &user, &password);
1666 if (!srv) {
1667 TALLOC_FREE(frame);
1668 return -1; /* errno set by SMBC_server */
1671 if (!SMBC_setatr(context, srv, path,
1672 0, access_time, write_time, 0, 0)) {
1673 TALLOC_FREE(frame);
1674 return -1; /* errno set by SMBC_setatr */
1677 TALLOC_FREE(frame);
1678 return 0;
1682 * Routine to unlink() a file
1686 SMBC_unlink_ctx(SMBCCTX *context,
1687 const char *fname)
1689 char *server = NULL;
1690 char *share = NULL;
1691 char *user = NULL;
1692 char *password = NULL;
1693 char *workgroup = NULL;
1694 char *path = NULL;
1695 char *targetpath = NULL;
1696 struct cli_state *targetcli = NULL;
1697 SMBCSRV *srv = NULL;
1698 TALLOC_CTX *frame = talloc_stackframe();
1700 if (!context || !context->internal->initialized) {
1702 errno = EINVAL; /* Best I can think of ... */
1703 TALLOC_FREE(frame);
1704 return -1;
1708 if (!fname) {
1709 errno = EINVAL;
1710 TALLOC_FREE(frame);
1711 return -1;
1715 if (SMBC_parse_path(frame,
1716 context,
1717 fname,
1718 &workgroup,
1719 &server,
1720 &share,
1721 &path,
1722 &user,
1723 &password,
1724 NULL)) {
1725 errno = EINVAL;
1726 TALLOC_FREE(frame);
1727 return -1;
1730 if (!user || user[0] == (char)0) {
1731 user = talloc_strdup(frame, smbc_getUser(context));
1732 if (!user) {
1733 errno = ENOMEM;
1734 TALLOC_FREE(frame);
1735 return -1;
1739 srv = SMBC_server(frame, context, True,
1740 server, share, &workgroup, &user, &password);
1742 if (!srv) {
1743 TALLOC_FREE(frame);
1744 return -1; /* SMBC_server sets errno */
1748 /*d_printf(">>>unlink: resolving %s\n", path);*/
1749 if (!cli_resolve_path(frame, "", srv->cli, path,
1750 &targetcli, &targetpath)) {
1751 d_printf("Could not resolve %s\n", path);
1752 TALLOC_FREE(frame);
1753 return -1;
1755 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1757 if (!cli_unlink(targetcli, targetpath)) {
1759 errno = SMBC_errno(context, targetcli);
1761 if (errno == EACCES) { /* Check if the file is a directory */
1763 int saverr = errno;
1764 SMB_OFF_T size = 0;
1765 uint16 mode = 0;
1766 struct timespec write_time_ts;
1767 struct timespec access_time_ts;
1768 struct timespec change_time_ts;
1769 SMB_INO_T ino = 0;
1771 if (!SMBC_getatr(context, srv, path, &mode, &size,
1772 NULL,
1773 &access_time_ts,
1774 &write_time_ts,
1775 &change_time_ts,
1776 &ino)) {
1778 /* Hmmm, bad error ... What? */
1780 errno = SMBC_errno(context, targetcli);
1781 TALLOC_FREE(frame);
1782 return -1;
1785 else {
1787 if (IS_DOS_DIR(mode))
1788 errno = EISDIR;
1789 else
1790 errno = saverr; /* Restore this */
1795 TALLOC_FREE(frame);
1796 return -1;
1800 TALLOC_FREE(frame);
1801 return 0; /* Success ... */
1806 * Routine to rename() a file
1810 SMBC_rename_ctx(SMBCCTX *ocontext,
1811 const char *oname,
1812 SMBCCTX *ncontext,
1813 const char *nname)
1815 char *server1 = NULL;
1816 char *share1 = NULL;
1817 char *server2 = NULL;
1818 char *share2 = NULL;
1819 char *user1 = NULL;
1820 char *user2 = NULL;
1821 char *password1 = NULL;
1822 char *password2 = NULL;
1823 char *workgroup = NULL;
1824 char *path1 = NULL;
1825 char *path2 = NULL;
1826 char *targetpath1 = NULL;
1827 char *targetpath2 = NULL;
1828 struct cli_state *targetcli1 = NULL;
1829 struct cli_state *targetcli2 = NULL;
1830 SMBCSRV *srv = NULL;
1831 TALLOC_CTX *frame = talloc_stackframe();
1833 if (!ocontext || !ncontext ||
1834 !ocontext->internal->initialized ||
1835 !ncontext->internal->initialized) {
1837 errno = EINVAL; /* Best I can think of ... */
1838 TALLOC_FREE(frame);
1839 return -1;
1842 if (!oname || !nname) {
1843 errno = EINVAL;
1844 TALLOC_FREE(frame);
1845 return -1;
1848 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
1850 if (SMBC_parse_path(frame,
1851 ocontext,
1852 oname,
1853 &workgroup,
1854 &server1,
1855 &share1,
1856 &path1,
1857 &user1,
1858 &password1,
1859 NULL)) {
1860 errno = EINVAL;
1861 TALLOC_FREE(frame);
1862 return -1;
1865 if (!user1 || user1[0] == (char)0) {
1866 user1 = talloc_strdup(frame, smbc_getUser(ocontext));
1867 if (!user1) {
1868 errno = ENOMEM;
1869 TALLOC_FREE(frame);
1870 return -1;
1874 if (SMBC_parse_path(frame,
1875 ncontext,
1876 nname,
1877 NULL,
1878 &server2,
1879 &share2,
1880 &path2,
1881 &user2,
1882 &password2,
1883 NULL)) {
1884 errno = EINVAL;
1885 TALLOC_FREE(frame);
1886 return -1;
1889 if (!user2 || user2[0] == (char)0) {
1890 user2 = talloc_strdup(frame, smbc_getUser(ncontext));
1891 if (!user2) {
1892 errno = ENOMEM;
1893 TALLOC_FREE(frame);
1894 return -1;
1898 if (strcmp(server1, server2) || strcmp(share1, share2) ||
1899 strcmp(user1, user2)) {
1900 /* Can't rename across file systems, or users?? */
1901 errno = EXDEV;
1902 TALLOC_FREE(frame);
1903 return -1;
1906 srv = SMBC_server(frame, ocontext, True,
1907 server1, share1, &workgroup, &user1, &password1);
1908 if (!srv) {
1909 TALLOC_FREE(frame);
1910 return -1;
1914 /* set the credentials to make DFS work */
1915 smbc_set_credentials_with_fallback(ocontext,
1916 workgroup,
1917 user1,
1918 password1);
1920 /*d_printf(">>>rename: resolving %s\n", path1);*/
1921 if (!cli_resolve_path(frame, "", srv->cli, path1,
1922 &targetcli1, &targetpath1)) {
1923 d_printf("Could not resolve %s\n", path1);
1924 TALLOC_FREE(frame);
1925 return -1;
1928 /* set the credentials to make DFS work */
1929 smbc_set_credentials_with_fallback(ncontext,
1930 workgroup,
1931 user2,
1932 password2);
1934 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
1935 /*d_printf(">>>rename: resolving %s\n", path2);*/
1936 if (!cli_resolve_path(frame, "", srv->cli, path2,
1937 &targetcli2, &targetpath2)) {
1938 d_printf("Could not resolve %s\n", path2);
1939 TALLOC_FREE(frame);
1940 return -1;
1942 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
1944 if (strcmp(targetcli1->desthost, targetcli2->desthost) ||
1945 strcmp(targetcli1->share, targetcli2->share))
1947 /* can't rename across file systems */
1948 errno = EXDEV;
1949 TALLOC_FREE(frame);
1950 return -1;
1953 if (!cli_rename(targetcli1, targetpath1, targetpath2)) {
1954 int eno = SMBC_errno(ocontext, targetcli1);
1956 if (eno != EEXIST ||
1957 !cli_unlink(targetcli1, targetpath2) ||
1958 !cli_rename(targetcli1, targetpath1, targetpath2)) {
1960 errno = eno;
1961 TALLOC_FREE(frame);
1962 return -1;
1967 TALLOC_FREE(frame);
1968 return 0; /* Success */