Initial revamp of the libsmbclient interface.
[samba.git] / source / libsmb / libsmb_file.c
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
10    
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.
15    
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.
20    
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/>.
23 */
24
25 #include "includes.h"
26 #include "libsmbclient.h"
27 #include "libsmb_internal.h"
28
29
30 /*
31  * Routine to open() a file ...
32  */
33
34 SMBCFILE *
35 SMBC_open_ctx(SMBCCTX *context,
36               const char *fname,
37               int flags,
38               mode_t mode)
39 {
40         char *server = NULL, *share = NULL, *user = NULL, *password = NULL, *workgroup = NULL;
41         char *path = NULL;
42         char *targetpath = NULL;
43         struct cli_state *targetcli = NULL;
44         SMBCSRV *srv   = NULL;
45         SMBCFILE *file = NULL;
46         int fd;
47         TALLOC_CTX *frame = talloc_stackframe();
48
49         if (!context || !context->initialized) {
50
51                 errno = EINVAL;  /* Best I can think of ... */
52                 TALLOC_FREE(frame);
53                 return NULL;
54
55         }
56
57         if (!fname) {
58
59                 errno = EINVAL;
60                 TALLOC_FREE(frame);
61                 return NULL;
62
63         }
64
65         if (SMBC_parse_path(frame,
66                                 context,
67                                 fname,
68                                 &workgroup,
69                                 &server,
70                                 &share,
71                                 &path,
72                                 &user,
73                                 &password,
74                                 NULL)) {
75                 errno = EINVAL;
76                 TALLOC_FREE(frame);
77                 return NULL;
78         }
79
80         if (!user || user[0] == (char)0) {
81                 user = talloc_strdup(frame, context->user);
82                 if (!user) {
83                         errno = ENOMEM;
84                         TALLOC_FREE(frame);
85                         return NULL;
86                 }
87         }
88
89         srv = SMBC_server(frame, context, True,
90                           server, share, &workgroup, &user, &password);
91
92         if (!srv) {
93                 if (errno == EPERM) errno = EACCES;
94                 TALLOC_FREE(frame);
95                 return NULL;  /* SMBC_server sets errno */
96         }
97
98         /* Hmmm, the test for a directory is suspect here ... FIXME */
99
100         if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
101                 fd = -1;
102         } else {
103                 file = SMB_MALLOC_P(SMBCFILE);
104
105                 if (!file) {
106                         errno = ENOMEM;
107                         TALLOC_FREE(frame);
108                         return NULL;
109                 }
110
111                 ZERO_STRUCTP(file);
112
113                 /*d_printf(">>>open: resolving %s\n", path);*/
114                 if (!cli_resolve_path(frame, "", srv->cli, path, &targetcli, &targetpath)) {
115                         d_printf("Could not resolve %s\n", path);
116                         SAFE_FREE(file);
117                         TALLOC_FREE(frame);
118                         return NULL;
119                 }
120                 /*d_printf(">>>open: resolved %s as %s\n", path, targetpath);*/
121
122                 if ((fd = cli_open(targetcli, targetpath, flags,
123                                    context->share_mode)) < 0) {
124
125                         /* Handle the error ... */
126
127                         SAFE_FREE(file);
128                         errno = SMBC_errno(context, targetcli);
129                         TALLOC_FREE(frame);
130                         return NULL;
131
132                 }
133
134                 /* Fill in file struct */
135
136                 file->cli_fd  = fd;
137                 file->fname   = SMB_STRDUP(fname);
138                 file->srv     = srv;
139                 file->offset  = 0;
140                 file->file    = True;
141
142                 DLIST_ADD(context->files, file);
143
144                 /*
145                  * If the file was opened in O_APPEND mode, all write
146                  * operations should be appended to the file.  To do that,
147                  * though, using this protocol, would require a getattrE()
148                  * call for each and every write, to determine where the end
149                  * of the file is. (There does not appear to be an append flag
150                  * in the protocol.)  Rather than add all of that overhead of
151                  * retrieving the current end-of-file offset prior to each
152                  * write operation, we'll assume that most append operations
153                  * will continuously write, so we'll just set the offset to
154                  * the end of the file now and hope that's adequate.
155                  *
156                  * Note to self: If this proves inadequate, and O_APPEND
157                  * should, in some cases, be forced for each write, add a
158                  * field in the context options structure, for
159                  * "strict_append_mode" which would select between the current
160                  * behavior (if FALSE) or issuing a getattrE() prior to each
161                  * write and forcing the write to the end of the file (if
162                  * TRUE).  Adding that capability will likely require adding
163                  * an "append" flag into the _SMBCFILE structure to track
164                  * whether a file was opened in O_APPEND mode.  -- djl
165                  */
166                 if (flags & O_APPEND) {
167                         if (SMBC_lseek_ctx(context, file, 0, SEEK_END) < 0) {
168                                 (void) SMBC_close_ctx(context, file);
169                                 errno = ENXIO;
170                                 TALLOC_FREE(frame);
171                                 return NULL;
172                         }
173                 }
174
175                 TALLOC_FREE(frame);
176                 return file;
177
178         }
179
180         /* Check if opendir needed ... */
181
182         if (fd == -1) {
183                 int eno = 0;
184
185                 eno = SMBC_errno(context, srv->cli);
186                 file = (context->posix_emu.opendir_fn)(context, fname);
187                 if (!file) errno = eno;
188                 TALLOC_FREE(frame);
189                 return file;
190
191         }
192
193         errno = EINVAL; /* FIXME, correct errno ? */
194         TALLOC_FREE(frame);
195         return NULL;
196
197 }
198
199 /*
200  * Routine to create a file 
201  */
202
203 static int creat_bits = O_WRONLY | O_CREAT | O_TRUNC; /* FIXME: Do we need this */
204
205 SMBCFILE *
206 SMBC_creat_ctx(SMBCCTX *context,
207                const char *path,
208                mode_t mode)
209 {
210
211         if (!context || !context->initialized) {
212
213                 errno = EINVAL;
214                 return NULL;
215
216         }
217
218         return SMBC_open_ctx(context, path, creat_bits, mode);
219 }
220
221 /*
222  * Routine to read() a file ...
223  */
224
225 ssize_t
226 SMBC_read_ctx(SMBCCTX *context,
227               SMBCFILE *file,
228               void *buf,
229               size_t count)
230 {
231         int ret;
232         char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
233         char *path = NULL;
234         char *targetpath = NULL;
235         struct cli_state *targetcli = NULL;
236         TALLOC_CTX *frame = talloc_stackframe();
237
238         /*
239          * offset:
240          *
241          * Compiler bug (possibly) -- gcc (GCC) 3.3.5 (Debian 1:3.3.5-2) --
242          * appears to pass file->offset (which is type off_t) differently than
243          * a local variable of type off_t.  Using local variable "offset" in
244          * the call to cli_read() instead of file->offset fixes a problem
245          * retrieving data at an offset greater than 4GB.
246          */
247         off_t offset;
248
249         if (!context || !context->initialized) {
250
251                 errno = EINVAL;
252                 TALLOC_FREE(frame);
253                 return -1;
254
255         }
256
257         DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
258
259         if (!file || !SMBC_dlist_contains(context->files, file)) {
260                 errno = EBADF;
261                 TALLOC_FREE(frame);
262                 return -1;
263
264         }
265
266         offset = file->offset;
267
268         /* Check that the buffer exists ... */
269
270         if (buf == NULL) {
271                 errno = EINVAL;
272                 TALLOC_FREE(frame);
273                 return -1;
274
275         }
276
277         /*d_printf(">>>read: parsing %s\n", file->fname);*/
278         if (SMBC_parse_path(frame,
279                                 context,
280                                 file->fname,
281                                 NULL,
282                                 &server,
283                                 &share,
284                                 &path,
285                                 &user,
286                                 &password,
287                                 NULL)) {
288                 errno = EINVAL;
289                 TALLOC_FREE(frame);
290                 return -1;
291         }
292
293         /*d_printf(">>>read: resolving %s\n", path);*/
294         if (!cli_resolve_path(frame, "", file->srv->cli, path,
295                               &targetcli, &targetpath)) {
296                 d_printf("Could not resolve %s\n", path);
297                 TALLOC_FREE(frame);
298                 return -1;
299         }
300         /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
301
302         ret = cli_read(targetcli, file->cli_fd, (char *)buf, offset, count);
303
304         if (ret < 0) {
305
306                 errno = SMBC_errno(context, targetcli);
307                 TALLOC_FREE(frame);
308                 return -1;
309
310         }
311
312         file->offset += ret;
313
314         DEBUG(4, ("  --> %d\n", ret));
315
316         TALLOC_FREE(frame);
317         return ret;  /* Success, ret bytes of data ... */
318
319 }
320
321 /*
322  * Routine to write() a file ...
323  */
324
325 ssize_t
326 SMBC_write_ctx(SMBCCTX *context,
327                SMBCFILE *file,
328                void *buf,
329                size_t count)
330 {
331         int ret;
332         off_t offset;
333         char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
334         char *path = NULL;
335         char *targetpath = NULL;
336         struct cli_state *targetcli = NULL;
337         TALLOC_CTX *frame = talloc_stackframe();
338
339         /* First check all pointers before dereferencing them */
340
341         if (!context || !context->initialized) {
342
343                 errno = EINVAL;
344                 TALLOC_FREE(frame);
345                 return -1;
346
347         }
348
349         if (!file || !SMBC_dlist_contains(context->files, file)) {
350                 errno = EBADF;
351                 TALLOC_FREE(frame);
352                 return -1;
353         }
354
355         /* Check that the buffer exists ... */
356
357         if (buf == NULL) {
358                 errno = EINVAL;
359                 TALLOC_FREE(frame);
360                 return -1;
361
362         }
363
364         offset = file->offset; /* See "offset" comment in SMBC_read_ctx() */
365
366         /*d_printf(">>>write: parsing %s\n", file->fname);*/
367         if (SMBC_parse_path(frame,
368                                 context,
369                                 file->fname,
370                                 NULL,
371                                 &server,
372                                 &share,
373                                 &path,
374                                 &user,
375                                 &password,
376                                 NULL)) {
377                 errno = EINVAL;
378                 TALLOC_FREE(frame);
379                 return -1;
380         }
381
382         /*d_printf(">>>write: resolving %s\n", path);*/
383         if (!cli_resolve_path(frame, "", file->srv->cli, path,
384                               &targetcli, &targetpath)) {
385                 d_printf("Could not resolve %s\n", path);
386                 TALLOC_FREE(frame);
387                 return -1;
388         }
389         /*d_printf(">>>write: resolved path as %s\n", targetpath);*/
390
391         ret = cli_write(targetcli, file->cli_fd, 0, (char *)buf, offset, count);
392
393         if (ret <= 0) {
394                 errno = SMBC_errno(context, targetcli);
395                 TALLOC_FREE(frame);
396                 return -1;
397
398         }
399
400         file->offset += ret;
401
402         TALLOC_FREE(frame);
403         return ret;  /* Success, 0 bytes of data ... */
404 }
405
406 /*
407  * Routine to close() a file ...
408  */
409
410 int
411 SMBC_close_ctx(SMBCCTX *context,
412                SMBCFILE *file)
413 {
414         SMBCSRV *srv;
415         char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
416         char *path = NULL;
417         char *targetpath = NULL;
418         struct cli_state *targetcli = NULL;
419         TALLOC_CTX *frame = talloc_stackframe();
420
421         if (!context || !context->initialized) {
422
423                 errno = EINVAL;
424                 TALLOC_FREE(frame);
425                 return -1;
426         }
427
428         if (!file || !SMBC_dlist_contains(context->files, file)) {
429                 errno = EBADF;
430                 TALLOC_FREE(frame);
431                 return -1;
432         }
433
434         /* IS a dir ... */
435         if (!file->file) {
436                 TALLOC_FREE(frame);
437                 return (context->posix_emu.closedir_fn)(context, file);
438         }
439
440         /*d_printf(">>>close: parsing %s\n", file->fname);*/
441         if (SMBC_parse_path(frame,
442                                 context,
443                                 file->fname,
444                                 NULL,
445                                 &server,
446                                 &share,
447                                 &path,
448                                 &user,
449                                 &password,
450                                 NULL)) {
451                 errno = EINVAL;
452                 TALLOC_FREE(frame);
453                 return -1;
454         }
455
456         /*d_printf(">>>close: resolving %s\n", path);*/
457         if (!cli_resolve_path(frame, "", file->srv->cli, path,
458                               &targetcli, &targetpath)) {
459                 d_printf("Could not resolve %s\n", path);
460                 TALLOC_FREE(frame);
461                 return -1;
462         }
463         /*d_printf(">>>close: resolved path as %s\n", targetpath);*/
464
465         if (!cli_close(targetcli, file->cli_fd)) {
466
467                 DEBUG(3, ("cli_close failed on %s. purging server.\n", 
468                           file->fname));
469                 /* Deallocate slot and remove the server 
470                  * from the server cache if unused */
471                 errno = SMBC_errno(context, targetcli);
472                 srv = file->srv;
473                 DLIST_REMOVE(context->files, file);
474                 SAFE_FREE(file->fname);
475                 SAFE_FREE(file);
476                 (context->server.remove_unused_server_fn)(context, srv);
477                 TALLOC_FREE(frame);
478                 return -1;
479
480         }
481
482         DLIST_REMOVE(context->files, file);
483         SAFE_FREE(file->fname);
484         SAFE_FREE(file);
485         TALLOC_FREE(frame);
486
487         return 0;
488 }
489
490 /*
491  * Get info from an SMB server on a file. Use a qpathinfo call first
492  * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
493  */
494 bool
495 SMBC_getatr(SMBCCTX * context,
496             SMBCSRV *srv,
497             char *path,
498             uint16 *mode,
499             SMB_OFF_T *size,
500             struct timespec *create_time_ts,
501             struct timespec *access_time_ts,
502             struct timespec *write_time_ts,
503             struct timespec *change_time_ts,
504             SMB_INO_T *ino)
505 {
506         char *fixedpath = NULL;
507         char *targetpath = NULL;
508         struct cli_state *targetcli = NULL;
509         time_t write_time;
510         TALLOC_CTX *frame = talloc_stackframe();
511
512         if (!context || !context->initialized) {
513
514                 errno = EINVAL;
515                 TALLOC_FREE(frame);
516                 return -1;
517         }
518
519         /* path fixup for . and .. */
520         if (strequal(path, ".") || strequal(path, "..")) {
521                 fixedpath = talloc_strdup(frame, "\\");
522                 if (!fixedpath) {
523                         errno = ENOMEM;
524                         TALLOC_FREE(frame);
525                         return -1;
526                 }
527         } else {
528                 fixedpath = talloc_strdup(frame, path);
529                 if (!fixedpath) {
530                         errno = ENOMEM;
531                         TALLOC_FREE(frame);
532                         return -1;
533                 }
534                 trim_string(fixedpath, NULL, "\\..");
535                 trim_string(fixedpath, NULL, "\\.");
536         }
537         DEBUG(4,("SMBC_getatr: sending qpathinfo\n"));
538
539         if (!cli_resolve_path(frame, "", srv->cli, fixedpath,
540                                 &targetcli, &targetpath)) {
541                 d_printf("Couldn't resolve %s\n", path);
542                 TALLOC_FREE(frame);
543                 return False;
544         }
545
546         if (!srv->no_pathinfo2 &&
547             cli_qpathinfo2(targetcli, targetpath,
548                            create_time_ts,
549                            access_time_ts,
550                            write_time_ts,
551                            change_time_ts,
552                            size, mode, ino)) {
553                 TALLOC_FREE(frame);
554                 return True;
555         }
556
557         /* if this is NT then don't bother with the getatr */
558         if (targetcli->capabilities & CAP_NT_SMBS) {
559                 errno = EPERM;
560                 TALLOC_FREE(frame);
561                 return False;
562         }
563
564         if (cli_getatr(targetcli, targetpath, mode, size, &write_time)) {
565
566                 struct timespec w_time_ts;
567
568                 w_time_ts = convert_time_t_to_timespec(write_time);
569
570                 if (write_time_ts != NULL) {
571                         *write_time_ts = w_time_ts;
572                 }
573
574                 if (create_time_ts != NULL) {
575                         *create_time_ts = w_time_ts;
576                 }
577
578                 if (access_time_ts != NULL) {
579                         *access_time_ts = w_time_ts;
580                 }
581
582                 if (change_time_ts != NULL) {
583                         *change_time_ts = w_time_ts;
584                 }
585
586                 srv->no_pathinfo2 = True;
587                 TALLOC_FREE(frame);
588                 return True;
589         }
590
591         errno = EPERM;
592         TALLOC_FREE(frame);
593         return False;
594
595 }
596
597 /*
598  * Set file info on an SMB server.  Use setpathinfo call first.  If that
599  * fails, use setattrE..
600  *
601  * Access and modification time parameters are always used and must be
602  * provided.  Create time, if zero, will be determined from the actual create
603  * time of the file.  If non-zero, the create time will be set as well.
604  *
605  * "mode" (attributes) parameter may be set to -1 if it is not to be set.
606  */
607 bool
608 SMBC_setatr(SMBCCTX * context, SMBCSRV *srv, char *path, 
609             time_t create_time,
610             time_t access_time,
611             time_t write_time,
612             time_t change_time,
613             uint16 mode)
614 {
615         int fd;
616         int ret;
617         TALLOC_CTX *frame = talloc_stackframe();
618
619         /*
620          * First, try setpathinfo (if qpathinfo succeeded), for it is the
621          * modern function for "new code" to be using, and it works given a
622          * filename rather than requiring that the file be opened to have its
623          * attributes manipulated.
624          */
625         if (srv->no_pathinfo ||
626             ! cli_setpathinfo(srv->cli, path,
627                               create_time,
628                               access_time,
629                               write_time,
630                               change_time,
631                               mode)) {
632
633                 /*
634                  * setpathinfo is not supported; go to plan B. 
635                  *
636                  * cli_setatr() does not work on win98, and it also doesn't
637                  * support setting the access time (only the modification
638                  * time), so in all cases, we open the specified file and use
639                  * cli_setattrE() which should work on all OS versions, and
640                  * supports both times.
641                  */
642
643                 /* Don't try {q,set}pathinfo() again, with this server */
644                 srv->no_pathinfo = True;
645
646                 /* Open the file */
647                 if ((fd = cli_open(srv->cli, path, O_RDWR, DENY_NONE)) < 0) {
648
649                         errno = SMBC_errno(context, srv->cli);
650                         TALLOC_FREE(frame);
651                         return -1;
652                 }
653
654                 /* Set the new attributes */
655                 ret = cli_setattrE(srv->cli, fd,
656                                    change_time,
657                                    access_time,
658                                    write_time);
659
660                 /* Close the file */
661                 cli_close(srv->cli, fd);
662
663                 /*
664                  * Unfortunately, setattrE() doesn't have a provision for
665                  * setting the access mode (attributes).  We'll have to try
666                  * cli_setatr() for that, and with only this parameter, it
667                  * seems to work on win98.
668                  */
669                 if (ret && mode != (uint16) -1) {
670                         ret = cli_setatr(srv->cli, path, mode, 0);
671                 }
672
673                 if (! ret) {
674                         errno = SMBC_errno(context, srv->cli);
675                         TALLOC_FREE(frame);
676                         return False;
677                 }
678         }
679
680         TALLOC_FREE(frame);
681         return True;
682 }
683
684 /*
685  * A routine to lseek() a file
686  */
687
688 off_t
689 SMBC_lseek_ctx(SMBCCTX *context,
690                SMBCFILE *file,
691                off_t offset,
692                int whence)
693 {
694         SMB_OFF_T size;
695         char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
696         char *path = NULL;
697         char *targetpath = NULL;
698         struct cli_state *targetcli = NULL;
699         TALLOC_CTX *frame = talloc_stackframe();
700
701         if (!context || !context->initialized) {
702
703                 errno = EINVAL;
704                 TALLOC_FREE(frame);
705                 return -1;
706         }
707
708         if (!file || !SMBC_dlist_contains(context->files, file)) {
709
710                 errno = EBADF;
711                 TALLOC_FREE(frame);
712                 return -1;
713
714         }
715
716         if (!file->file) {
717
718                 errno = EINVAL;
719                 TALLOC_FREE(frame);
720                 return -1;      /* Can't lseek a dir ... */
721
722         }
723
724         switch (whence) {
725         case SEEK_SET:
726                 file->offset = offset;
727                 break;
728
729         case SEEK_CUR:
730                 file->offset += offset;
731                 break;
732
733         case SEEK_END:
734                 /*d_printf(">>>lseek: parsing %s\n", file->fname);*/
735                 if (SMBC_parse_path(frame,
736                                         context,
737                                         file->fname,
738                                         NULL,
739                                         &server,
740                                         &share,
741                                         &path,
742                                         &user,
743                                         &password,
744                                         NULL)) {
745                         errno = EINVAL;
746                         TALLOC_FREE(frame);
747                         return -1;
748                 }
749
750                 /*d_printf(">>>lseek: resolving %s\n", path);*/
751                 if (!cli_resolve_path(frame, "", file->srv->cli, path,
752                                       &targetcli, &targetpath)) {
753                         d_printf("Could not resolve %s\n", path);
754                         TALLOC_FREE(frame);
755                         return -1;
756                 }
757                 /*d_printf(">>>lseek: resolved path as %s\n", targetpath);*/
758
759                 if (!cli_qfileinfo(targetcli, file->cli_fd, NULL,
760                                    &size, NULL, NULL, NULL, NULL, NULL))
761                 {
762                     SMB_OFF_T b_size = size;
763                         if (!cli_getattrE(targetcli, file->cli_fd,
764                                           NULL, &b_size, NULL, NULL, NULL))
765                     {
766                         errno = EINVAL;
767                         TALLOC_FREE(frame);
768                         return -1;
769                     } else
770                         size = b_size;
771                 }
772                 file->offset = size + offset;
773                 break;
774
775         default:
776                 errno = EINVAL;
777                 break;
778
779         }
780
781         TALLOC_FREE(frame);
782         return file->offset;
783
784 }
785
786
787 /*
788  * Routine to truncate a file given by its file descriptor, to a specified size
789  */
790
791 int
792 SMBC_ftruncate_ctx(SMBCCTX *context,
793                    SMBCFILE *file,
794                    off_t length)
795 {
796         SMB_OFF_T size = length;
797         char *server = NULL;
798         char *share = NULL;
799         char *user = NULL;
800         char *password = NULL;
801         char *path = NULL;
802         char *targetpath = NULL;
803         struct cli_state *targetcli = NULL;
804         TALLOC_CTX *frame = talloc_stackframe();
805
806         if (!context || !context->initialized) {
807
808                 errno = EINVAL;
809                 TALLOC_FREE(frame);
810                 return -1;
811         }
812
813         if (!file || !SMBC_dlist_contains(context->files, file)) {
814                 errno = EBADF;
815                 TALLOC_FREE(frame);
816                 return -1;
817         }
818
819         if (!file->file) {
820                 errno = EINVAL;
821                 TALLOC_FREE(frame);
822                 return -1;
823         }
824
825         /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
826         if (SMBC_parse_path(frame,
827                             context,
828                             file->fname,
829                             NULL,
830                             &server,
831                             &share,
832                             &path,
833                             &user,
834                             &password,
835                             NULL)) {
836                 errno = EINVAL;
837                 TALLOC_FREE(frame);
838                 return -1;
839         }
840
841         /*d_printf(">>>fstat: resolving %s\n", path);*/
842         if (!cli_resolve_path(frame, "", file->srv->cli, path,
843                               &targetcli, &targetpath)) {
844                 d_printf("Could not resolve %s\n", path);
845                 TALLOC_FREE(frame);
846                 return -1;
847         }
848         /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
849
850         if (!cli_ftruncate(targetcli, file->cli_fd, size)) {
851                 errno = EINVAL;
852                 TALLOC_FREE(frame);
853                 return -1;
854         }
855
856         TALLOC_FREE(frame);
857         return 0;
858
859 }