source: trunk/client/src/ndpsmb.c@ 521

Last change on this file since 521 was 521, checked in by Silvan Scherrer, 15 years ago

Samba Client 2.1: another bunch of updates

  • Property svn:eol-style set to native
File size: 52.8 KB
Line 
1/*
2 Netdrive Samba client plugin
3 plugin API
4 Copyright (C) netlabs.org 2003-2008
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19*/
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <stdarg.h>
24#include <string.h>
25#include <time.h>
26
27#define NDPL_LARGEFILES
28#define INCL_LONGLONG
29#include <ndextpl2.h>
30#include "smbwrp.h"
31#include "util.h"
32
33#if 0
34
35#ifndef DEBUG_PRINTF
36#define debug_printf( ...)
37#endif
38
39#define log debug_printf
40#endif
41
42#define debug_printf(...) debuglocal(9, __VA_ARGS__)
43
44// -------------------------------------------------------------
45
46/* time conversion functions: SMB protocol sends timestamps in GMT time,
47* os2 api uses localtime,
48* emx/klibc uses timezone and daylight saving to convert GMT timestamps,
49* so only the timezone must be counted in conversion.
50*/
51void fsphUnixTimeToDosDate( time_t time, FDATE* fdate, FTIME *ftime)
52{
53 struct tm* gmt = localtime( &time);
54#if 0 // as localtime() already does dst we don't need to add something
55 if (gmt->tm_isdst>0) {
56 debug_printf( "daylight saving in effect %d, timezone %d\n",gmt->tm_isdst, timezone);
57 time -= 3600;
58 gmt = localtime( &time);
59 }
60#endif
61 fdate->day = gmt->tm_mday;
62 fdate->month = gmt->tm_mon+1;
63 fdate->year = gmt->tm_year + 1900 - 1980;
64 ftime->twosecs = gmt->tm_sec/2;
65 ftime->minutes = gmt->tm_min;
66 ftime->hours = gmt->tm_hour;
67}
68
69void fsphDosDateToUnixTime( FDATE fdate, FTIME ftime, ULONG* time)
70{
71 struct tm gmtime = { 0 };
72
73 debug_printf( "fsphDosDateToUnixTime time %02d:%02d\n", ftime.hours, ftime.minutes);
74 gmtime.tm_mday = fdate.day;
75 gmtime.tm_mon = fdate.month-1;
76 gmtime.tm_year = fdate.year + 1980 - 1900;
77 gmtime.tm_sec = ftime.twosecs*2;
78 gmtime.tm_min = ftime.minutes;
79 gmtime.tm_hour = ftime.hours;
80 gmtime.tm_isdst = -1; // force libc to check dst saving
81
82 *time = mktime( &gmtime);
83 debug_printf( "fsphDosDateToUnixTime time1 %d %s", *time, ctime( (time_t*)time));
84#if 0 // as mktime() already does dst we don't need to add something
85 struct tm* gmt;
86 gmt = localtime( (time_t*) time);
87 if (gmt->tm_isdst>0) {
88 debug_printf( "fsphDosDateToUnixTime daylight saving in effect %d, timezone %d\n",gmt->tm_isdst, timezone);
89 *time += 3600;
90 }
91#endif
92 debug_printf( "fsphDosDateToUnixTime time2 %d %s", *time, ctime( (time_t*)time));
93}
94
95// -------------------------------------------------------------
96
97/* uppercased type of resource */
98const char *NdpTypes[] =
99{
100 "SMBFS",
101 NULL
102}
103;
104
105/* Properties of supported resource types */
106
107/* Properties of resource */
108static const NDPROPERTYINFO smbProperties[] =
109{
110 {ND_PROP_STRING, 0, "WORKGROUP", ""},
111 {ND_PROP_STRING, 0, "SERVER", ""},
112 {ND_PROP_STRING, 0, "SHARE", ""},
113 {ND_PROP_STRING, 0, "USER", "guest"},
114 {ND_PROP_STRING, 0, "PASSWORD", ""},
115 {ND_PROP_STRING, 0, "SPASSWORD", ""},
116 {ND_PROP_STRING, 0, "MASTER", "WORKGROUP"},
117 {ND_PROP_ULONG, 0, "MASTERTYPE", "1"},
118 {ND_PROP_ULONG, 0, "CTO", "10"},
119 {ND_PROP_ULONG, 0, "CLD", "32"},
120 {ND_PROP_ULONG, 0, "EASUPPORT", "1"},
121 {ND_PROP_STRING, 0, NULL, NULL}
122};
123
124
125/* Exported array of properties */
126const NDPROPERTYINFO *NdpPropertiesInfo[] =
127{
128 smbProperties
129};
130
131
132PLUGINHELPERTABLE2L *ph;
133static int ifL;
134
135int APIENTRY NdpPluginLoad (PLUGINHELPERTABLE2L *pPHT)
136{
137 int rc;
138 HPIPE pipe;
139 unsigned long action;
140 ph = pPHT;
141 ifL = 0;
142/*
143 if (ph->cb < sizeof (PLUGINHELPERTABLE2))
144 {
145 return ERROR_INVALID_FUNCTION;
146 }
147*/
148 if (ph->cb >= sizeof (PLUGINHELPERTABLE2L))
149 {
150 ifL = 1;
151 }
152 debuglocal(9,"Working with %s bit fileio NDFS\n", ifL ? "64" : "32");
153 return NO_ERROR;
154}
155
156
157int APIENTRY NdpPluginFree (void)
158{
159 return NO_ERROR;
160}
161
162
163void getfindinfo(Connection * pConn, FILEFINDBUF3 * stat, smbwrp_fileinfo * finfo)
164{
165 char * name = ph->fsphStrRChr(finfo->fname, '\\');
166 if (name)
167 {
168 name++;
169 }
170 else
171 {
172 name = finfo->fname;
173 }
174 if (!*name)
175 {
176 name = pConn->pRes->srv.share_name;
177 }
178 strncpy(stat->achName, name, CCHMAXPATHCOMP - 1);
179 stat->cbFile = finfo->size;
180 stat->cbFileAlloc = stat->cbFile;
181 stat->oNextEntryOffset = 0ul;
182 stat->cchName = strlen(stat->achName);
183 stat->attrFile = (finfo->attr & 0x37);
184
185 fsphUnixTimeToDosDate(finfo->mtime, &stat->fdateLastWrite, &stat->ftimeLastWrite);
186 fsphUnixTimeToDosDate(finfo->ctime, &stat->fdateCreation, &stat->ftimeCreation);
187 fsphUnixTimeToDosDate(finfo->atime, &stat->fdateLastAccess, &stat->ftimeLastAccess);
188}
189
190int getfindinfoL(Connection * pConn, void * plist, smbwrp_fileinfo * finfo, ULONG ulAttribute, char * mask)
191{
192 FILESTATUS3L stat = {0};
193 char * name = ph->fsphStrRChr(finfo->fname, '\\');
194 if (name)
195 {
196 name++;
197 }
198 else
199 {
200 name = finfo->fname;
201 }
202 if (!*name)
203 {
204 name = pConn->pRes->srv.share_name;
205 }
206 if (mask && (!ph->fsphAttrMatch(ulAttribute, finfo->attr & 0x37) || !ph->fsphWildMatch(mask, name, ND_IGNORE_CASE)))
207 {
208 return 0;
209 }
210
211 stat.cbFile = finfo->size;
212 stat.cbFileAlloc = stat.cbFile;
213 stat.attrFile = (finfo->attr & 0x37);
214
215 fsphUnixTimeToDosDate(finfo->mtime, &stat.fdateLastWrite, &stat.ftimeLastWrite);
216 fsphUnixTimeToDosDate(finfo->ctime, &stat.fdateCreation, &stat.ftimeCreation);
217 fsphUnixTimeToDosDate(finfo->atime, &stat.fdateLastAccess, &stat.ftimeLastAccess);
218 debug_printf( "fname %s\n", finfo->fname);
219 debug_printf( "mtime %d %s", finfo->mtime, ctime( (time_t*)&finfo->mtime));
220 debug_printf( "ftimeLastAccess %02d:%02d:%02d\n", stat.ftimeLastWrite.hours, stat.ftimeLastWrite.minutes, stat.ftimeLastWrite.twosecs*2);
221
222 ph->fsphAddFile32L(plist, &stat, name, strlen(name), finfo, sizeof(*finfo), 0);
223 return 1;
224}
225
226static unsigned char fromhex (char c)
227{
228 if ('0' <= c && c <= '9')
229 {
230 return c - '0';
231 }
232
233 if ('A' <= c && c <= 'F')
234 {
235 return c - 'A' + 0xA;
236 }
237
238 if ('a' <= c && c <= 'f')
239 {
240 return c - 'a' + 0xA;
241 }
242
243 return 0;
244}
245
246static char tohex (unsigned char b)
247{
248 b &= 0xF;
249
250 if (b <= 9)
251 {
252 return b + '0';
253 }
254
255 return 'A' + (b - 0xA);
256}
257
258static void decryptPassword (const char *pszCrypt, char *pszPlain)
259{
260 /* A simple "decryption", character from the hex value. */
261 const char *s = pszCrypt;
262 char *d = pszPlain;
263
264 while (*s)
265 {
266 *d++ = (char)((fromhex (*s++) << 4) + fromhex (*s++));
267 }
268
269 *d++ = 0;
270}
271
272static void encryptPassword (const char *pszPlain, char *pszCrypt)
273{
274 /* A simple "encryption" encode each character as hex value. */
275 const char *s = pszPlain;
276 char *d = pszCrypt;
277
278 while (*s)
279 {
280 *d++ = tohex ((*s) >> 4);
281 *d++ = tohex (*s);
282 s++;
283 }
284
285 *d++ = 0;
286}
287
288/* accept parameters in form
289 * [filename][;name=filename]
290 */
291int initResource (Resource *pRes, NDPROPERTYHANDLE properties)
292{
293 int rc = NO_ERROR;
294 unsigned long t;
295 const CHAR * q = NULL;
296 int defaultPassword = 1;
297
298 pRes->rootlevel = 0;
299 pRes->easupport = 1;
300#ifdef HAVE_KRB5_H
301 pRes->krb5support = 1;
302#else
303 pRes->krb5support = 0;
304#endif
305 pRes->pdc = NULL;
306
307 t = 0, q = NULL;
308 rc = ph->fsphQueryStringProperty (properties, "WORKGROUP", &q, &t);
309 if (!rc && t && *q)
310 {
311 strncpy(pRes->srv.workgroup, q, sizeof(pRes->srv.workgroup) - 1);
312 pRes->rootlevel = 1;
313 }
314
315 t = 0, q = NULL;
316 rc = ph->fsphQueryStringProperty (properties, "SERVER", &q, &t);
317 if (!rc && t && *q)
318 {
319 strncpy(pRes->srv.server_name, q, sizeof(pRes->srv.server_name) - 1);
320 pRes->rootlevel = 2;
321 }
322
323 t = 0, q = NULL;
324 rc = ph->fsphQueryStringProperty (properties, "SHARE", &q, &t);
325 if (!rc && t && *q)
326 {
327 strncpy(pRes->srv.share_name, q, sizeof(pRes->srv.share_name) - 1);
328 pRes->rootlevel = 3;
329 }
330
331 t = 0, q = NULL;
332 rc = ph->fsphQueryStringProperty (properties, "USER", &q, &t);
333 if (!rc && t && *q)
334 {
335 strncpy(pRes->srv.username, q, sizeof(pRes->srv.username) - 1);
336 }
337
338 t = 0, q = NULL;
339 rc = ph->fsphQueryStringProperty (properties, "PASSWORD", &q, &t);
340 if (!rc && t && *q)
341 {
342 strncpy(pRes->srv.password, q, sizeof(pRes->srv.password) - 1);
343 defaultPassword = 0;
344 }
345
346 t = 0, q = NULL;
347 rc = ph->fsphQueryStringProperty (properties, "SPASSWORD", &q, &t);
348 if ( rc == NO_ERROR
349 && *q != '\0'
350 && defaultPassword)
351 {
352 char p[1024];
353 p[0] = 0;
354
355 decryptPassword (q, p);
356
357 if (*p)
358 {
359 strncpy(pRes->srv.password, p, sizeof(pRes->srv.password) - 1);
360
361 /* clear the plain password */
362 ph->fsphSetProperty (properties, "PASSWORD", "");
363 }
364 }
365 else
366 {
367 char c[1024];
368 encryptPassword (pRes->srv.password, c);
369
370 ph->fsphSetProperty (properties, "SPASSWORD", c);
371
372 // clear the plain password
373 ph->fsphSetProperty (properties, "PASSWORD", "");
374 }
375
376 t = 0, q = NULL;
377 rc = ph->fsphQueryStringProperty (properties, "MASTER", &q, &t);
378 if (!rc && t && *q)
379 {
380 strncpy(pRes->srv.master, q, sizeof(pRes->srv.master) - 1);
381 }
382
383 t = 0;
384 rc = ph->fsphQueryUlongProperty (properties, "MASTERTYPE", &t);
385 if (!rc)
386 {
387 if (t > 1)
388 {
389 rc = ERROR_INVALID_PARAMETER;
390 }
391 else
392 {
393 pRes->srv.ifmastergroup = t;
394 }
395 }
396
397 t = 0;
398 rc = ph->fsphQueryUlongProperty (properties, "EASUPPORT", &t);
399 if (!rc)
400 {
401 if (t > 1)
402 {
403 rc = ERROR_INVALID_PARAMETER;
404 }
405 else
406 {
407 pRes->easupport = t;
408 }
409 }
410
411 t = 0;
412 rc = ph->fsphQueryUlongProperty (properties, "CTO", &t);
413 if (!rc)
414 {
415 if (t > 600)
416 {
417 rc = ERROR_INVALID_PARAMETER;
418 }
419 else
420 {
421 pRes->cachetimeout = t;
422 }
423 }
424
425 t = 0;
426 rc = ph->fsphQueryUlongProperty (properties, "CLD", &t);
427 if (!rc)
428 {
429 if (t > 96)
430 {
431 rc = ERROR_INVALID_PARAMETER;
432 }
433 else
434 {
435 pRes->cachedepth = t;
436 }
437 }
438
439 /*
440 * Create a directory cache with expiration time and cache listings
441 * the above values come from the gui. default: timeout 10; listings: 32
442 */
443 dircache_create(&pRes->pdc, pRes->cachetimeout, pRes->cachedepth);
444
445 return rc;
446}
447
448int iftestpath(char * path)
449{
450 char * p = path;
451 if (!path)
452 {
453 return 0;
454 }
455 while ((p = ph->fsphStrChr(p, 'A')) != NULL)
456 {
457 if (ph->fsphStrNCmp(p, "A.+,;=[].B", 10) == 0)
458 {
459 return 1;
460 }
461 p++;
462 }
463 return 0;
464}
465
466int pathparser(Resource *pRes, Connection * pConn, char * path, char * result)
467{
468 int rootlevel;
469 int rc = NO_ERROR;
470 if (!pRes || !path || !result)
471 {
472 return ERROR_INVALID_PARAMETER;
473 }
474 // handle special case when someone wants to test support of LFN or smth similar
475 if (iftestpath(path))
476 {
477 strcpy(result, "\\A.+,;=[].B");
478 return NO_ERROR;
479 }
480
481 rootlevel = pRes->rootlevel;
482 if (*path == '\\') path++;
483
484 if (rootlevel < 3)
485 {
486 char * p;
487 // flag: 1 parameters changed, reconnection required, 0 do nothing
488 int newlevel = 0;
489 // use a temporary resource to test disconnection/reconnection
490 Resource tmpRes;
491 // copy exising data
492 memcpy( &tmpRes, pRes, sizeof( tmpRes));
493 // pointer to new connection fields
494 smbwrp_server * tmp = &tmpRes.srv;
495 if (rootlevel == 0)
496 {
497 p = ph->fsphStrChr(path, '\\');
498 if (!p)
499 {
500 p = path + strlen(path);
501 }
502 if (strlen(tmp->workgroup) != p - path
503 || (p == path || ph->fsphStrNICmp(path, tmp->workgroup, p - path)))
504 {
505 strncpy(tmp->workgroup, path, p - path);
506 tmp->workgroup[p - path] = 0;
507 newlevel = 1;
508 }
509 path = *p == '\\' ? p + 1 : p;
510 rootlevel = 1;
511 }
512 if (rootlevel == 1) // root path starts from server name
513 {
514 p = ph->fsphStrChr(path, '\\');
515 if (!p)
516 {
517 p = path + strlen(path);
518 }
519 if (strlen(tmp->server_name) != p - path
520 || (p == path || ph->fsphStrNICmp(path, tmp->server_name, p - path)))
521 {
522 strncpy(tmp->server_name, path, p - path);
523 tmp->server_name[p - path] = 0;
524 newlevel = 1;
525 }
526 path = *p == '\\' ? p + 1 : p;
527 rootlevel = 2;
528 }
529 if (rootlevel == 2) // root path starts from share name
530 {
531 p = ph->fsphStrChr(path, '\\');
532 if (!p)
533 {
534 p = path + strlen(path);
535 }
536 if (strlen(tmp->share_name) != (p - path)
537 || (p == path || ph->fsphStrNICmp(path, tmp->share_name, p - path)))
538 {
539 strncpy(tmp->share_name, path, p - path);
540 tmp->share_name[p - path] = 0;
541 newlevel = 1;
542 }
543 path = *p == '\\' ? p + 1 : p;
544 }
545 if (newlevel)
546 {
547 // reconnect to server here, first test new connection
548 cli_state* tmp_cli = NULL;
549 rc = smbwrp_connect( &tmpRes, &tmp_cli);
550 if (!rc)
551 {
552 // new connection is ok, disconnect old one
553 cli_state* cli = pConn->cli;
554 smbwrp_disconnect( pRes, cli);
555 // save tmp data structure
556 memcpy( pRes, &tmpRes, sizeof( tmpRes));
557 // save new connection handle
558 pConn->cli = tmp_cli;
559 }
560 }
561 }
562
563 strcpy(result, "\\");
564 strncat(result, path, CCHMAXPATH);
565
566 return rc;
567}
568
569
570// -------------------------------------------------------------
571
572/* check if the requested resource is available */
573static int checkMountResource( Resource* pRes)
574{
575 int rc;
576 unsigned long action;
577 cli_state* cli = NULL;
578
579 debug_printf("checkMountResource in tid#%d\n", _gettid());
580 rc = smbwrp_connect( pRes, &cli);
581/* changed to real error codes SCS
582 if (rc)
583 rc = (rc == 7 ? ERROR_BAD_DEV_TYPE : ERROR_ACCESS_DENIED); */
584 switch (rc) {
585 case 0:
586 rc = NO_ERROR;
587 break;
588 case 1:
589 case 10:
590 case 11:
591 rc = ERROR_BAD_NET_NAME;
592 break;
593 case 2:
594 rc = ERROR_INIT_ROUTINE_FAILED;
595 break;
596 case 3:
597 rc = ERROR_BAD_NET_RESP;
598 break;
599 case 4:
600 rc = ERROR_NETWORK_BUSY;
601 break;
602 case 6:
603 rc = ERROR_NETWORK_ACCESS_DENIED;
604 break;
605 case 7:
606 rc = ERROR_BAD_NETPATH;
607 break;
608 default:
609 rc = ERROR_UNEXP_NET_ERR;
610 break;
611 } /* endswitch */
612
613 smbwrp_disconnect( pRes, cli);
614
615 return rc;
616}
617
618int APIENTRY NdpMountResource (HRESOURCE *presource, int type, NDPROPERTYHANDLE properties)
619{
620 int rc = NO_ERROR;
621 unsigned long objany = OBJ_ANY;
622 Resource *pRes = NULL;
623
624 debuglocal(9,"NdpMountResource in\n");
625
626 // init code
627 smbwrp_init();
628
629 /* since samba plugin support only 1 type of resources we do not need */
630 /* to check what the found type really is */
631 pRes = malloc( sizeof(Resource));
632 if (pRes == NULL)
633 {
634 rc = ERROR_NOT_ENOUGH_MEMORY;
635 }
636 else
637 {
638 memset(pRes, 0, sizeof(Resource));
639 //pRes->objany = objany;
640 // parse init string
641 rc = initResource (pRes, properties);
642 // try to connect to resource (check type) only if thread!=1, so ndctl startup
643 // is not slowed down by network connections.
644 // ndctl does mounting on main thread (#1)
645 // nd/ndpm do not use main thread
646 if (!rc && _gettid()!=1)
647 rc = checkMountResource( pRes);
648 if (!rc)
649 {
650 // store resource data
651 *presource = (HRESOURCE)pRes;
652 }
653 else
654 {
655 free(pRes);
656 }
657 }
658 debuglocal(9,"NdpMountResource rc=%d\n", rc);
659 return rc;
660}
661
662// -------------------------------------------------------------
663
664int APIENTRY NdpFreeResource (HRESOURCE resource)
665{
666 Resource *pRes = (Resource *)resource;
667 dircache_delete(pRes->pdc);
668 memset(&pRes->srv, 0, sizeof(pRes->srv));
669 free(pRes);
670 debuglocal(9,"NdpFreeResource %d\n", NO_ERROR);
671 return NO_ERROR;
672}
673
674// -------------------------------------------------------------
675
676int APIENTRY NdpRsrcCompare (HRESOURCE resource, HRESOURCE resource2)
677{
678 Resource *pRes = (Resource *)resource;
679 Resource *pRes2 = (Resource *)resource2;
680 int rc = ND_RSRC_DIFFERENT;
681
682 debuglocal(9,"NdpRsrcCompare in\n");
683 if (ph->fsphStrICmp(pRes->srv.server_name, pRes2->srv.server_name) == 0
684 && ph->fsphStrICmp(pRes->srv.share_name, pRes2->srv.share_name) == 0
685 && ph->fsphStrICmp(pRes->srv.username, pRes2->srv.username) == 0
686 && ph->fsphStrICmp(pRes->srv.workgroup, pRes2->srv.workgroup) == 0)
687 {
688 // resources are equal
689 rc = ND_RSRC_EQUAL;
690 }
691
692 debuglocal(9,"NdpRsrcCompare %d\n", rc);
693
694 return rc;
695}
696
697int APIENTRY NdpRsrcUpdate (HRESOURCE resource, HRESOURCE resource2)
698{
699 // do nothing
700 debuglocal(9,"NdpRsrcUpdate %d\n", NO_ERROR);
701 return NO_ERROR;
702}
703
704int APIENTRY NdpRsrcQueryInfo (HRESOURCE resource, ULONG *pulFlags, void *pdata, ULONG insize, ULONG *poutlen)
705{
706 Resource *pRes = (Resource *)resource;
707 int rc = NO_ERROR;
708 char s[4096];
709
710 debuglocal(9,"NdpRsrcQueryInfo in\n");
711
712 switch (pRes->rootlevel)
713 {
714 case 0:
715 {
716 ph->fsph_snprintf(s, sizeof(s) - 1, "SMBFS%s \\\\@%s", ifL ? "64" : "32", pRes->srv.username);
717 } break;
718 case 1:
719 {
720 ph->fsph_snprintf(s, sizeof(s) - 1, "SMBFS%s %s: \\\\@%s", ifL ? "64" : "32", pRes->srv.workgroup, pRes->srv.username);
721 } break;
722 case 2:
723 {
724 ph->fsph_snprintf(s, sizeof(s) - 1, "SMBFS%s \\\\%s%s%s@%s", ifL ? "64" : "32", *pRes->srv.workgroup ? pRes->srv.workgroup : "", *pRes->srv.workgroup ? ":" : "", pRes->srv.server_name, pRes->srv.username);
725 } break;
726 default:
727 {
728 ph->fsph_snprintf(s, sizeof(s) - 1, "SMBFS%s \\\\%s%s%s\\%s@%s", ifL ? "64" : "32", *pRes->srv.workgroup ? pRes->srv.workgroup : "", *pRes->srv.workgroup ? ":" : "", pRes->srv.server_name, pRes->srv.share_name, pRes->srv.username);
729 } break;
730 }
731 *poutlen = strlen(s) + 1;
732 if (*poutlen > insize)
733 {
734 rc = ERROR_BUFFER_OVERFLOW;
735 }
736 else
737 {
738 memcpy(pdata, s, *poutlen);
739 }
740
741 debuglocal(9,"NdpRsrcQueryInfo %d\n", rc);
742
743 return rc;
744}
745
746int APIENTRY NdpRsrcQueryFSAttach (HRESOURCE resource, void *pdata, ULONG insize, ULONG *poutlen)
747{
748 ULONG ulDummy = 0;
749 /* just return the resource info string */
750 return NdpRsrcQueryInfo (resource, &ulDummy, pdata, insize, poutlen);
751}
752
753int APIENTRY NdpRsrcQueryFSAllocate (HRESOURCE resource, NDFSALLOCATE *pfsa)
754{
755 Resource *pRes = (Resource *)resource;
756 int rc = NO_ERROR, rc1;
757 unsigned long action = 0;
758 cli_state* cli = NULL;
759 FSALLOCATE fsa;
760
761 debuglocal(9,"NdpRsrcQueryFSAllocate %08x\n", pfsa);
762
763 if (!pfsa)
764 {
765 return NO_ERROR;
766 }
767
768 debug_printf("checkMountResource in tid#%d\n", _gettid());
769 rc = smbwrp_connect( pRes, &cli);
770 if (rc)
771 {
772 debuglocal(9,"NdpCreateConnection failed rc=%d\n", rc);
773 pfsa->cSectorUnit = 1;
774 pfsa->cUnit = 123456;
775 pfsa->cUnitAvail = 123456;
776 pfsa->cbSector = 2048;
777 rc = (rc == 7 ? ERROR_BAD_DEV_TYPE : ERROR_ACCESS_DENIED);
778 return rc;
779 }
780
781 rc = smbwrp_dskattr( cli, &fsa);
782 if (rc)
783 {
784 pfsa->cSectorUnit = 1;
785 pfsa->cUnit = 123456;
786 pfsa->cUnitAvail = 123456;
787 pfsa->cbSector = 2048;
788 //rc = rc ? rc : (resp.rc ? resp.rc : ERROR_INVALID_PARAMETER);
789 }
790 else
791 {
792 pfsa->cSectorUnit = fsa.cSectorUnit;
793 pfsa->cUnit = fsa.cUnit;
794 pfsa->cUnitAvail = fsa.cUnitAvail;
795 pfsa->cbSector = fsa.cbSector;
796 }
797
798 smbwrp_disconnect( pRes, cli);
799
800 debuglocal(9,"NdpRsrcQueryFSAllocate %d/%d (cUnit = %d/cUnitAvail = %d/cbSector = %d)\n", rc, rc1, pfsa->cUnit, pfsa->cUnitAvail, pfsa->cbSector);
801 return rc;
802}
803
804// -------------------------------------------------------------
805
806int APIENTRY NdpCreateConnection (HRESOURCE resource, HCONNECTION *pconn)
807{
808 int rc = 0;
809 Resource * pRes = (Resource *)resource;
810 unsigned long action;
811 Connection *pConn = NULL;
812
813 debuglocal(9,"NdpCreateConnection in\n");
814
815 pConn = malloc( sizeof(Connection));
816 if (pConn == NULL)
817 {
818 rc = ERROR_NOT_ENOUGH_MEMORY;
819 }
820 if (rc)
821 {
822 debuglocal(9,"NdpCreateConnection ERROR_NOT_ENOUGH_MEMORY %d\n", rc);
823 return rc;
824 }
825 memset(pConn, 0, sizeof(Connection));
826 pConn->pRes = pRes;
827 pConn->file.fd = -1;
828
829 debuglocal(9,"NdpCreateConnection send CONNECT\n");
830 rc = smbwrp_connect( pRes, &pConn->cli);
831 if (rc)
832 {
833 free(pConn);
834 pConn = NULL;
835 rc = (rc == 7 ? ERROR_BAD_DEV_TYPE : ERROR_INVALID_PARAMETER);
836 }
837
838 *pconn = (HCONNECTION)pConn;
839 debuglocal(9,"NdpCreateConnection [%p] %d\n", pConn, rc);
840 return rc;
841}
842
843// -------------------------------------------------------------
844
845int APIENTRY NdpFreeConnection (HCONNECTION conn)
846{
847 Connection *pConn = (Connection *)conn;
848 Resource *pRes = pConn->pRes;
849 int rc;
850
851 debuglocal(9,"NdpFreeConnection in [%p]\n", pConn);
852 if (pConn->file.fd >= 0)
853 {
854 rc = smbwrp_close( pConn->cli, &pConn->file);
855 pConn->file.fd = -1;
856 }
857
858 smbwrp_disconnect( pRes, pConn->cli);
859
860 free(pConn);
861 debuglocal(9,"NdpFreeConnection %d\n", NO_ERROR);
862 return NO_ERROR;
863}
864
865// -------------------------------------------------------------
866
867/*
868 * NdpQueryPathInfo is the most important function :) netdrive always calls
869 * the function before every operation to find out the path status: does it exist, is it a file, does a
870 * parent directory exist, etc.
871 * Plugin must return one of the following error codes:
872 * NO_ERROR - path exists and the path information have been successfully retrieved.
873 * ERROR_FILE_NOT_FOUND - all but the last component of the path exist and the
874 * path without the last component is a directory. dir1_ok\dir2_ok\does_not_exist.
875 * the wildcard can not exist, so the plugin returns FILE_NOT_FOUND, if the parent
876 * directory exist.
877 * ERROR_PATH_NOT_FOUND - any of not last path components does not exist, or all
878 * but the last component exist and is a file: \dir_ok\dir2_ok\file_ok\non_existing.
879 * ERROR_REM_NOT_LIST - resource is temporarily unavailable for some reasons.
880 * Any other error codes means an internal plugin error, not related to the status
881 * of the path queried.
882 */
883int APIENTRY NdpQueryPathInfo (HCONNECTION conn, void *plist, char *szPath)
884{
885 Connection *pConn = (Connection *)conn;
886 Resource *pRes = pConn->pRes;
887 smbwrp_fileinfo finfo;
888 int rc = 0;
889 unsigned long action;
890 char path[CCHMAXPATH+1] = {0};
891 int retry = 0;
892
893 debuglocal(9,"NdpQueryPathInfo in [%p] <%s>, retry = %d\n", pConn, szPath, retry);
894
895 // is wildcard is specified, we suppose parent dir exist, so exit immediately
896 if (ph->fsphStrChr(szPath, '*') || ph->fsphStrChr(szPath, '?'))
897 {
898 return ERROR_FILE_NOT_FOUND;
899 }
900
901
902 do {
903 /* First check if there is information in the directory cache. */
904 unsigned long ulAge = 0;
905 if (dircache_find_path(pRes->pdc, szPath, &finfo, &ulAge))
906 {
907 if (ulAge <= 15) /* @todo configurable. */
908 {
909 rc = NO_ERROR;
910 finfo.easize = -1;
911 getfindinfoL(pConn, plist, &finfo, 0, NULL);
912 break;
913 }
914 }
915
916 rc = pathparser(pRes, pConn, szPath, path);
917 debuglocal(9,"NdpQueryPathInfo pathparser for <%s> rc=%d\n", path, rc);
918 switch (rc)
919 {
920 case NO_ERROR :
921 case ERROR_FILE_NOT_FOUND:
922 case ERROR_PATH_NOT_FOUND:
923 case ERROR_ACCESS_DENIED:
924 case ERROR_INVALID_PARAMETER:
925 {
926 break;
927 }
928 default :
929 {
930 rc = ERROR_PATH_NOT_FOUND;
931 }
932 }
933 if (rc)
934 {
935 break;
936 }
937 strncpy(finfo.fname, path, sizeof(finfo.fname) - 1);
938 debuglocal(9,"NdpQueryPathInfo smbwrp_getattr for <%s>\n", path);
939 rc = smbwrp_getattr( &pRes->srv, pConn->cli, &finfo);
940 if (rc)
941 {
942 // remote server not available for first time?
943 if (rc == ERROR_REM_NOT_LIST && retry == 0)
944 {
945 // free current cli resources
946 smbwrp_disconnect( pRes, pConn->cli);
947 // reconnect
948 smbwrp_connect( pRes, &pConn->cli);
949 // try file list again
950 rc = smbwrp_getattr( &pRes->srv, pConn->cli, &finfo);
951 debuglocal(9,"NdpQueryPathInfo remote connection lost, retry rc = %d\n", rc);
952 }
953 switch (rc)
954 {
955 case NO_ERROR :
956 case ERROR_FILE_NOT_FOUND:
957 case ERROR_PATH_NOT_FOUND:
958 case ERROR_ACCESS_DENIED:
959 case ERROR_INVALID_PARAMETER:
960 case ERROR_REM_NOT_LIST:
961 break;
962 default :
963 {
964 rc = ERROR_PATH_NOT_FOUND;
965 }
966 }
967 }
968 else
969 {
970 finfo.easize = -1;
971 getfindinfoL(pConn, plist, &finfo, 0, NULL);
972 }
973 if (rc == ERROR_FILE_NOT_FOUND)
974 {
975 // now try the upper path
976 char * p = ph->fsphStrChr(finfo.fname, '\\');
977 if (p && p > finfo.fname)
978 {
979 *p = 0;
980 rc = smbwrp_getattr( &pRes->srv, pConn->cli, &finfo);
981 if (rc)
982 {
983 debuglocal(9,"NdpQueryPathInfo upper path in <%s>, retry = %d\n", finfo.fname, retry);
984 rc = rc ? ERROR_PATH_NOT_FOUND : ERROR_INVALID_PARAMETER;
985 }
986 }
987 }
988 } while (0);
989 debuglocal(9,"NdpQueryPathInfo <%s> (%s) %d\n", szPath, path, rc);
990
991 return rc;
992}
993
994// -------------------------------------------------------------
995
996int APIENTRY NdpFindStart (HCONNECTION conn, void *plist, NDFILEINFOL *pfiparent, char *szPath, ULONG ulAttribute)
997{
998 Connection *pConn = (Connection *)conn;
999 Resource *pRes = pConn->pRes;
1000 int rc = NO_ERROR, count = 0;
1001 unsigned long action;
1002 char *mask = "*";
1003 char dir[CCHMAXPATH+1] = {0};
1004 char path[CCHMAXPATH+1] = {0};
1005 smbwrp_fileinfo * data;
1006 NDPATHELEMENT *pel = ph->fsphNameElem(0);
1007 filelist_state state;
1008 char * p;
1009
1010 debug_printf("NdpFindStart in [%p]\n", pConn);
1011
1012 strncpy(dir, szPath, sizeof(dir) - 1);
1013 if (pel)
1014 {
1015 mask = pel->name;
1016 dir[strlen(szPath) - pel->length] = 0;
1017 }
1018 action = strlen(dir) - 1;
1019 if (dir[action] == '\\')
1020 {
1021 dir[action] = 0;
1022 }
1023 rc = pathparser(pRes, pConn, dir, path);
1024 if (rc)
1025 {
1026 return rc;
1027 }
1028 action = strlen(path) - 1;
1029 if (path[action] != '\\')
1030 {
1031 strncat(path, "\\", sizeof(path) - 1);
1032 }
1033 strcpy(dir, path);
1034 strncat(path, mask, sizeof(path) - 1);
1035
1036 // this structure will be used by libsmb callbacks, so we store here all we need
1037 // to fill netdrive structures
1038 state.pConn = pConn;
1039 state.plist = plist;
1040 state.ulAttribute = ulAttribute;
1041 strcpy( state.dir, dir);
1042 strcpy( state.dir_mask, mask);
1043 strcpy( state.mask, path);
1044 state.fullpath = szPath;
1045 /* This plugin always reads a complete directory listing and filters results
1046 * using actual mask (state.dir_mask) in getfindinfoL.
1047 * May be this was a workaround for some server bug.
1048 * If this will be changed, then directory listing cache must be changed too,
1049 * and must remember the mask, which was used to obtain a listing.
1050 * Now the directory cache saves complete directory listings and then uses them to find
1051 * information about single files.
1052 * However, with a directory cache, it is probably faster to get a full listing and
1053 * then use it to obtain info about separate files than to perform a network
1054 * list query operation using actual wild cards for each file. Some application,
1055 * for example OpenOffice, do this.
1056 */
1057 p = getlastslash(state.mask);
1058 if (p)
1059 {
1060 *(p + 1) = '*';
1061 *(p + 2) = 0;
1062 }
1063 else
1064 {
1065 strcpy(state.mask, "\\*");
1066 }
1067 debuglocal(9,"NdpFindStart: dir [%s], dir_mask [%s], mask [%s], szPath [%s]\n",
1068 state.dir, state.dir_mask, state.mask, state.fullpath);
1069 rc = smbwrp_filelist( &pRes->srv, pConn->cli, &state);
1070 // we need to handle reconnection also here, because NdpQueryPathInfo
1071 // could be called with '*' and exit then immediately (without calling libsmb)
1072 if (rc == ERROR_REM_NOT_LIST)
1073 {
1074 // free current cli resources
1075 smbwrp_disconnect( pRes, pConn->cli);
1076 // reconnect
1077 smbwrp_connect( pRes, &pConn->cli);
1078 // try file list again next loop
1079 rc = smbwrp_filelist( &pRes->srv, pConn->cli, &state);
1080 debuglocal(9,"NdpFindStart remote connection lost, retry rc = %d\n", rc);
1081 }
1082
1083 debuglocal(9,"NdpFindStart <%s> (%s) cnt %d %d\n", szPath, path, count, rc);
1084
1085 return rc;
1086}
1087
1088int APIENTRY NdpDeletePathInfo (HRESOURCE resource, NDFILEINFOL *pfi)
1089{
1090// debuglocal(9,"NdpDeletePathInfo %d\n", 0);
1091 return NO_ERROR;
1092}
1093
1094int APIENTRY NdpRefresh (HCONNECTION conn, char *path, int tree)
1095{
1096 debuglocal(9,"NdpRefresh <%s> %d\n", path, 0);
1097 return NO_ERROR;
1098}
1099
1100int APIENTRY NdpDiscardResourceData (HRESOURCE resource, NDDATABUF *pdatabuf)
1101{
1102 // The plugin do not have to deallocate anything
1103 // because resource data did not contain any pointers
1104 // to plugins data.
1105 // Data stored by fsphSetResourceData will be
1106 // deallocated by NetDrive.
1107
1108 debuglocal(9,"NdpDicardresourceData %d\n", 0);
1109 return NO_ERROR;
1110}
1111
1112int APIENTRY NdpSetPathInfo (HCONNECTION conn, NDFILEINFOL *pfi, char *szPathName)
1113{
1114 Connection *pConn = (Connection *)conn;
1115 Resource *pRes = pConn->pRes;
1116 int rc = 0;
1117 unsigned long action;
1118 char path[CCHMAXPATH+1] = {0};
1119 smbwrp_fileinfo finfo;
1120
1121 debug_printf("NdpSetPathInfo in [%p]\n", pConn);
1122
1123 // delete the dir cache
1124 dircache_invalidate(szPathName, pRes->pdc, 1);
1125
1126 do {
1127 rc = pathparser(pRes, pConn, szPathName, path);
1128 if (rc)
1129 {
1130 break;
1131 }
1132
1133 memset(&finfo, 0, sizeof(finfo));
1134
1135 strncpy(finfo.fname, path, sizeof(finfo.fname) - 1);
1136 fsphDosDateToUnixTime(pfi->stat.fdateLastWrite, pfi->stat.ftimeLastWrite, &(finfo.mtime));
1137 finfo.attr = pfi->stat.attrFile & 0x37;
1138 rc = smbwrp_setattr(pConn->cli, &finfo);
1139 } while (0);
1140 debuglocal(9,"NdpSetPathInfo <%s> (%s) %d\n", szPathName, path, rc);
1141
1142 return rc;
1143}
1144
1145int buildFEALIST(FEALIST *pFEASrc, GEALIST *pGEAList, FEALIST *pFEAList)
1146{
1147 int rc = 0;
1148 FEA * pfea;
1149 FEA * pfeadest;
1150 unsigned long size, done = sizeof(pFEAList->cbList), dsize, ddone = sizeof(pFEAList->cbList);
1151
1152 size = pFEASrc->cbList;
1153 pfea = pFEASrc->list;
1154 pfeadest = pFEAList->list;
1155 dsize = pFEAList->cbList;
1156//debuglocal(9,"buildFEALIST in destsize %d srcsize %d pGEAList=%08x pGEAList->cbList=%d\n", dsize, ddone, size, pGEAList, pGEAList ? pGEAList->cbList : 0);
1157 while (done < size)
1158 {
1159 char * name = (char *)(pfea + 1);
1160 int insert = 1;
1161 if (pGEAList && pGEAList->cbList > sizeof(pGEAList->cbList))
1162 {
1163 GEA * pgea = pGEAList->list;
1164 unsigned long size = pGEAList->cbList - sizeof(pGEAList->cbList), done = 0;
1165 insert = 0;
1166 while (done < size)
1167 {
1168//debuglocal(9,"comp <%s> <%s>\n", name, pgea->szName);
1169 if (!ph->fsphStrNCmp(name, pgea->szName, pgea->cbName))
1170 {
1171 insert = 1;
1172 break;
1173 }
1174 done += pgea->cbName + 2;
1175 pgea = (GEA *)((char *)pgea + pgea->cbName + 2);
1176 }
1177 }
1178 if (insert)
1179 {
1180 ddone += sizeof(FEA) + pfea->cbName + 1 + pfea->cbValue;
1181 if (ddone <= dsize)
1182 {
1183 pfeadest->cbName = pfea->cbName;
1184 pfeadest->cbValue = pfea->cbValue;
1185 pfeadest->fEA = 0;
1186 strcpy((char *)(pfeadest + 1), name);
1187 memcpy((char *)(pfeadest + 1) + pfea->cbName + 1, (char *)(pfea + 1) + pfea->cbName + 1, pfea->cbValue);
1188 pfeadest = (FEA *)((char *)pFEAList + ddone);
1189 }
1190 }
1191 done += sizeof(FEA) + pfea->cbName + 1 + pfea->cbValue;
1192//debuglocal(9,"buuildfea <%s> insert=%d pfea->cbName=%d pfea->cbValue=%d srcdone=%d destdone=%d pfeadest=%08x pfea=%08x\n", name, insert, pfea->cbName, pfea->cbValue, done, ddone, pfeadest, pfea);
1193 pfea = (FEA *)((char *)pFEASrc + done);
1194 }
1195 pFEAList->cbList = ddone;
1196 if (ddone > dsize && dsize > sizeof(pFEAList->cbList))
1197 {
1198 rc = ERROR_BUFFER_OVERFLOW;
1199 }
1200 debuglocal(9,"buildFEALIST rc=%d destsize=%d destdone=%d srcsize=%d pGEAList=%08x\n", rc, dsize, ddone, size, pGEAList);
1201 return rc;
1202}
1203
1204int APIENTRY NdpEAQuery (HCONNECTION conn, GEALIST *pGEAList, NDFILEINFOL *pfi, FEALIST *pFEAList)
1205{
1206 Connection *pConn = (Connection *)conn;
1207 Resource *pRes = pConn->pRes;
1208 int rc = 0;
1209 unsigned long action;
1210 char * path = NULL;
1211 FEALIST * pFEASrc;
1212 NDDATABUF fdata = {0};
1213 smbwrp_fileinfo *finfo;
1214 char pBuffer[64*1024];
1215
1216 if (!pfi || !pfi->pszName || !pFEAList)
1217 {
1218 return ERROR_EAS_NOT_SUPPORTED;
1219 }
1220 if (!pRes->easupport)
1221 {
1222 return ERROR_EAS_NOT_SUPPORTED;
1223 }
1224
1225 rc = ph->fsphGetFileInfoData(pfi, &fdata, 0);
1226 if (rc || !fdata.ulSize || !fdata.pData)
1227 {
1228 debuglocal(9,"NdpEAQuery: ph->fsphGetFileInfoData = %d/%d %08x\n", rc, fdata.ulSize, fdata.pData);
1229 return ERROR_EAS_NOT_SUPPORTED;
1230 }
1231 finfo = (smbwrp_fileinfo *)fdata.pData;
1232 path = finfo->fname;
1233
1234 debuglocal(9,"NdpEAQuery in [%p] <%s> %08x %d\n", pConn, path, pGEAList, pGEAList ? pGEAList->cbList : 0);
1235
1236 do {
1237 rc = smbwrp_listea( pConn->cli, path, pBuffer, sizeof( pBuffer));
1238 pFEASrc = (FEALIST*) pBuffer;
1239 if (rc)
1240 {
1241 //rc = pConn->rc ? pConn->rc : (resp.rc ? resp.rc : ERROR_INVALID_PARAMETER);
1242 switch (rc)
1243 {
1244 case ERROR_FILE_NOT_FOUND :
1245 case ERROR_PATH_NOT_FOUND :
1246 {
1247 pFEAList->cbList = sizeof(pFEAList->cbList);
1248 rc = NO_ERROR;
1249 } break;
1250 case ERROR_BUFFER_OVERFLOW :
1251 {
1252 pFEAList->cbList = pFEASrc->cbList;
1253 } break;
1254 default :
1255 {
1256 rc = ERROR_EAS_NOT_SUPPORTED;
1257 }
1258 }
1259 }
1260 else
1261 {
1262 rc = buildFEALIST((FEALIST *)pFEASrc, pGEAList, pFEAList);
1263 }
1264 } while (0);
1265 debuglocal(9,"NdpEAQuery <%s> %d %d %d\n", pfi->pszName, rc, pFEASrc->cbList, pFEAList->cbList);
1266
1267 return rc;
1268}
1269
1270int APIENTRY NdpEASet (HCONNECTION conn, FEALIST *pFEAList, NDFILEINFOL *pfi)
1271{
1272 Connection *pConn = (Connection *)conn;
1273 Resource *pRes = pConn->pRes;
1274 int rc = 0;
1275 char * path;
1276 unsigned long action;
1277 NDDATABUF fdata = {0};
1278 smbwrp_fileinfo *finfo;
1279
1280 debuglocal(9,"NdpEASet in [%p]\n", pConn);
1281
1282 if (!pfi || !pfi->pszName || !pFEAList || pFEAList->cbList <= sizeof(long))
1283 {
1284 return ERROR_EAS_NOT_SUPPORTED;
1285 }
1286 if (!pRes->easupport)
1287 {
1288 return ERROR_EAS_NOT_SUPPORTED;
1289 }
1290
1291 rc = ph->fsphGetFileInfoData(pfi, &fdata, 0);
1292 if (rc || !fdata.ulSize || !fdata.pData)
1293 {
1294 debuglocal(9,"NdpEASet: ph->fsphGetFileInfoData = %d/%d/%08x\n", rc, fdata.ulSize, fdata.pData);
1295 return ERROR_EAS_NOT_SUPPORTED;
1296 }
1297 finfo = (smbwrp_fileinfo *)fdata.pData;
1298 path = finfo->fname;
1299
1300 do {
1301 // got FEA there
1302 FEA * pfea;
1303 unsigned long done = sizeof(long);
1304 pfea = pFEAList->list;
1305 while (done < pFEAList->cbList)
1306 {
1307 rc = smbwrp_setea(pConn->cli, path, (char*)(pfea + 1), pfea->cbValue ? (char *)(pfea + 1) + pfea->cbName + 1: NULL, pfea->cbValue);
1308 if (rc)
1309 {
1310 break;
1311 }
1312 pfea = (FEA *)((char *)(pfea + 1) + pfea->cbName + 1 + pfea->cbValue);
1313 done += sizeof(FEA) + pfea->cbName + 1 + pfea->cbValue;
1314 }
1315 } while (0);
1316 debuglocal(9,"NdpEASet %d\n", rc);
1317
1318 return rc;
1319}
1320
1321int APIENTRY NdpEASize (HCONNECTION conn, NDFILEINFOL *pfi, ULONG *pulEASize)
1322{
1323 Connection *pConn = (Connection *)conn;
1324 Resource *pRes = pConn->pRes;
1325 int rc = 0;
1326 unsigned long action;
1327 char * path = NULL;
1328 FEALIST * pfealist;
1329 NDDATABUF fdata = {0};
1330 smbwrp_fileinfo *finfo;
1331 char pBuffer[64*1024];
1332 int easize;
1333
1334 if (!pfi || !pulEASize)
1335 {
1336 return ERROR_EAS_NOT_SUPPORTED;
1337 }
1338 if (!pRes->easupport)
1339 {
1340 return ERROR_EAS_NOT_SUPPORTED;
1341 }
1342
1343 rc = ph->fsphGetFileInfoData(pfi, &fdata, 0);
1344 if (rc || !fdata.ulSize || !fdata.pData)
1345 {
1346 debuglocal(9,"NdpEASize: ph->fsphGetFileInfoData = %d/%d/%08x\n", rc, fdata.ulSize, fdata.pData);
1347 return ERROR_EAS_NOT_SUPPORTED;
1348 }
1349 finfo = (smbwrp_fileinfo *)fdata.pData;
1350 easize = finfo->easize;
1351 finfo->easize = -1;
1352 path = finfo->fname;
1353 if (easize >= 0)
1354 {
1355 *pulEASize = easize;
1356 debuglocal(9,"NdpEASize <%s> cached %d\n", path, easize);
1357 return NO_ERROR;
1358 }
1359
1360 debuglocal(9,"NdpEASize in [%p] <%s> \n", pConn, path);
1361
1362 do {
1363 rc = smbwrp_listea(pConn->cli, path, pBuffer, sizeof( pBuffer));
1364 pfealist = (FEALIST*)pBuffer;
1365 if (rc)
1366 {
1367 //rc = pConn->rc ? pConn->rc : (resp.rc ? resp.rc : ERROR_INVALID_PARAMETER);
1368 switch (rc)
1369 {
1370 case ERROR_FILE_NOT_FOUND :
1371 case ERROR_PATH_NOT_FOUND :
1372 {
1373 pfealist->cbList = sizeof(pfealist->cbList);
1374 } /* Fall through */
1375 case ERROR_BUFFER_OVERFLOW :
1376 {
1377 rc = NO_ERROR;
1378 } break;
1379 default :
1380 {
1381 rc = ERROR_EAS_NOT_SUPPORTED;
1382 }
1383 }
1384 }
1385 *pulEASize = pfealist->cbList;
1386 } while (0);
1387 debuglocal(9,"NdpEASize <%s> %d %d\n", pfi->pszName, *pulEASize, rc);
1388
1389 return rc;
1390}
1391
1392int APIENTRY NdpSetCurrentDir (HCONNECTION conn, NDFILEINFOL *pfi, char *szPath)
1393{
1394 Connection *pConn = (Connection *)conn;
1395 Resource *pRes = pConn->pRes;
1396 int rc = 0;
1397 unsigned long action;
1398 char path[CCHMAXPATH+1] = {0};
1399
1400 debuglocal(9,"NdpSetCurrentDir in [%p]\n", pConn);
1401
1402 do {
1403 rc = pathparser(pRes, pConn, szPath, path);
1404 if (rc)
1405 {
1406 break;
1407 }
1408
1409 rc = smbwrp_chdir(&pRes->srv, pConn->cli, path);
1410 } while (0);
1411 debuglocal(9,"NdpSetCurrentDir <%s> (%s) %d\n", szPath, path, rc);
1412
1413 return rc;
1414}
1415
1416int APIENTRY NdpCopy (HCONNECTION conn, NDFILEINFOL *pfiDst, char *szDst, NDFILEINFOL *pfiSrc, char *szSrc, ULONG ulOption)
1417{
1418 debuglocal(9,"NdpCopy <%s> -> <%s> %d\n", szSrc, szDst, ERROR_CANNOT_COPY);
1419 return ERROR_CANNOT_COPY;
1420}
1421
1422int APIENTRY NdpCopy2 (HCONNECTION conn, HRESOURCE resDst, NDFILEINFOL *pfiDst, char *szDst, NDFILEINFOL *pfiSrc, char *szSrc, ULONG ulOption)
1423{
1424 debuglocal(9,"NdpCopy2 <%s> -> <%s> %d\n", szSrc, szDst, ERROR_CANNOT_COPY);
1425 return ERROR_CANNOT_COPY;
1426}
1427
1428int APIENTRY NdpForceDelete (HCONNECTION conn, NDFILEINFOL *pfi, char *szFile)
1429{
1430 Connection *pConn = (Connection *)conn;
1431 Resource *pRes = pConn->pRes;
1432 int rc = 0;
1433 unsigned long action;
1434 char path[CCHMAXPATH+1] = {0};
1435
1436 debuglocal(9,"NdpForceDelete in [%p]\n", pConn);
1437
1438 dircache_invalidate(szFile, pRes->pdc, 1);
1439
1440 do {
1441 rc = pathparser(pRes, pConn, szFile, path);
1442 if (rc)
1443 {
1444 break;
1445 }
1446
1447 rc = smbwrp_unlink(pConn->cli, path);
1448 } while (0);
1449 debuglocal(9,"NdpForceDelete <%s> (%s) %d\n", szFile, path, rc);
1450
1451 return rc;
1452}
1453
1454int APIENTRY NdpCreateDir (HCONNECTION conn, NDFILEINFOL *pfiparent, char *szDirName, FEALIST *pFEAList)
1455{
1456 Connection *pConn = (Connection *)conn;
1457 Resource *pRes = pConn->pRes;
1458 int rc = 0;
1459 unsigned long action;
1460 char path[CCHMAXPATH+1] = {0};
1461
1462 debuglocal(9,"NdpCreateDir in [%p]\n", pConn);
1463
1464 dircache_invalidate(szDirName, pRes->pdc, 1);
1465
1466 do {
1467 rc = pathparser(pRes, pConn, szDirName, path);
1468 if (rc)
1469 {
1470 break;
1471 }
1472
1473 rc = smbwrp_mkdir(pConn->cli, path);
1474 } while (0);
1475 debuglocal(9,"NdpCreateDir <%s> (%s) %d\n", szDirName, path, rc);
1476
1477 return rc;
1478}
1479
1480int APIENTRY NdpDeleteDir (HCONNECTION conn, NDFILEINFOL *pfi, char *szDir)
1481{
1482 Connection *pConn = (Connection *)conn;
1483 Resource *pRes = pConn->pRes;
1484 int rc = 0;
1485 unsigned long action;
1486 char path[CCHMAXPATH+1] = {0};
1487
1488 debuglocal(9,"NdpDeleteDir in [%p]\n", pConn);
1489
1490 dircache_invalidate(szDir, pRes->pdc, 1);
1491
1492 do {
1493 rc = pathparser(pRes, pConn, szDir, path);
1494 if (rc)
1495 {
1496 break;
1497 }
1498
1499 rc = smbwrp_rmdir(pConn->cli, path);
1500 } while (0);
1501 debuglocal(9,"NdpDeleteDir <%s> (%s) %d\n", szDir, path, rc);
1502
1503 return rc;
1504}
1505
1506int APIENTRY NdpMove (HCONNECTION conn, NDFILEINFOL *pfiDst, char *szDst, NDFILEINFOL *pfiSrc, char *szSrc)
1507{
1508 Connection *pConn = (Connection *)conn;
1509 Resource *pRes = pConn->pRes;
1510 int rc = 0;
1511 unsigned long action;
1512 char src[CCHMAXPATH+1] = {0};
1513 int l1, l2;
1514 char * p = szDst;
1515
1516 debuglocal(9,"NdpMove in [%p] from <%s> to <%s>\n", pConn, szSrc, szDst);
1517
1518 dircache_invalidate(szSrc, pRes->pdc, 1);
1519 dircache_invalidate(szDst, pRes->pdc, 1);
1520
1521 do
1522 {
1523 rc = pathparser(pRes, pConn, szSrc, src);
1524 if (rc)
1525 {
1526 break;
1527 }
1528 l1 = strlen(szSrc);
1529 l2 = strlen(src);
1530 if (l1 > l2)
1531 {
1532 if (ph->fsphStrNICmp(szSrc, szDst, l1 - l2))
1533 {
1534 // the file moved accross different shares or servers or workgroups
1535 rc = ERROR_WRITE_PROTECT;
1536 break;
1537 }
1538 p = szDst + l1 - l2 + 1;
1539 }
1540 //pConn->mem[CCHMAXPATH + 1] = '\\';
1541 rc = smbwrp_rename(pConn->cli, src, p);
1542 } while (0);
1543 debuglocal(9,"NdpMove <%s> -> <%s> (%s) %d\n", szSrc, szDst, src, rc);
1544
1545 return rc;
1546}
1547
1548int APIENTRY NdpMove2 (HCONNECTION conn, HRESOURCE resDst, NDFILEINFOL *pfiDst, char *szDst, NDFILEINFOL *pfiSrc, char *szSrc)
1549{
1550 debuglocal(9,"NdpMove2 <%s> -> <%s> %d\n", szSrc, szDst, ERROR_WRITE_PROTECT);
1551 return ERROR_WRITE_PROTECT;
1552}
1553
1554
1555int APIENTRY NdpChangeCase (HCONNECTION conn, char *szDst, NDFILEINFOL *pfiSrc, char *szSrc, char *szNewName, ULONG ulNameLen)
1556{
1557 return NdpMove (conn, pfiSrc, szDst, pfiSrc, szSrc);
1558}
1559
1560int APIENTRY NdpRename (HCONNECTION conn, char *szDst, NDFILEINFOL *pfiSrc, char *szSrc, char *szNewName, ULONG ulNameLen)
1561{
1562 return NdpMove (conn, pfiSrc, szDst, pfiSrc, szSrc);
1563}
1564
1565int smbopen(Connection *pConn, char *szFileName, int flags, ULONG ulOpenMode, ULONG ulAttribute, FEALIST *pFEAList)
1566{
1567 Resource *pRes = pConn->pRes;
1568 unsigned long action;
1569 int rc = 0;
1570 char path[CCHMAXPATH+1] = {0};
1571
1572 debuglocal(9,"smbopen in [%p] %d\n", pConn, pConn->file.fd);
1573
1574 if (flags & O_CREAT)
1575 {
1576 dircache_invalidate(szFileName, pRes->pdc, 1);
1577 }
1578 do {
1579 if (pConn->file.fd > 0)
1580 {
1581 rc = ERROR_TOO_MANY_OPEN_FILES;
1582 break;
1583 }
1584
1585 rc = pathparser(pRes, pConn, szFileName, path);
1586 if (rc)
1587 {
1588 break;
1589 }
1590
1591 strncpy(pConn->file.fullname, szFileName, sizeof(pConn->file.fullname) - 1);
1592 strncpy(pConn->file.fname, path, sizeof(pConn->file.fname) - 1);
1593 flags |= O_BINARY;
1594 switch (ulOpenMode & 3)
1595 {
1596 case OPEN_ACCESS_READONLY : flags |= O_RDONLY; break;
1597 case OPEN_ACCESS_WRITEONLY : flags |= O_WRONLY; break;
1598 case OPEN_ACCESS_READWRITE : flags |= O_RDWR; break;
1599 default : flags |= O_RDWR;
1600 }
1601 pConn->file.openmode = flags;
1602 pConn->file.openattr = ulAttribute & 0x37;
1603 pConn->file.denymode = (ulOpenMode & 0x70) >> 4;
1604 rc = smbwrp_open(pConn->cli, &pConn->file);
1605 } while (0);
1606 debuglocal(9,"smbopen <%s> (%s) %08x %08x %08x %d. file = %d\n", szFileName, path, flags, ulOpenMode, ulAttribute, rc, pConn->file.fd);
1607 if (!rc && pFEAList)
1608 {
1609 int rc1 = NdpFileEASet((HCONNECTION)pConn, (NDFILEHANDLE)0, pFEAList);
1610 debuglocal(9,"smbopen NdpFileEASet %d. pFEAList->cbList %d\n", rc1, pFEAList->cbList);
1611 }
1612
1613 return rc;
1614}
1615
1616int APIENTRY NdpOpenReplace (HCONNECTION conn, NDFILEINFOL *pfi, NDFILEHANDLE *phandle, char *szFileName, ULONG ulSize, ULONG ulOpenMode, ULONG ulAttribute, FEALIST *pFEAList)
1617{
1618 return smbopen((Connection *)conn, szFileName, O_TRUNC, ulOpenMode, ulAttribute, pFEAList);
1619}
1620
1621int APIENTRY NdpOpenReplaceL(HCONNECTION conn, NDFILEINFO *pfi, NDFILEHANDLE *phandle, char *szFileName, LONGLONG llSize, ULONG ulOpenMode, ULONG ulAttribute, FEALIST *pFEAList)
1622{
1623 return smbopen((Connection *)conn, szFileName, O_TRUNC, ulOpenMode, ulAttribute, pFEAList);
1624}
1625
1626int APIENTRY NdpOpenCreate (HCONNECTION conn, NDFILEINFOL *pfiparent, NDFILEHANDLE *phandle, char *szFileName, ULONG ulSize, ULONG ulOpenMode, ULONG ulAttribute, FEALIST *pFEAList)
1627{
1628// return smbopen((Connection *)conn, szFileName, O_CREAT, ulOpenMode, ulAttribute);
1629 return smbopen((Connection *)conn, szFileName, O_CREAT | O_EXCL, ulOpenMode, ulAttribute, pFEAList);
1630}
1631
1632int APIENTRY NdpOpenCreateL(HCONNECTION conn, NDFILEINFO *pfiparent, NDFILEHANDLE *phandle, char *szFileName, LONGLONG llSize, ULONG ulOpenMode, ULONG ulAttribute, FEALIST *pFEAList)
1633{
1634 return smbopen((Connection *)conn, szFileName, O_CREAT | O_EXCL, ulOpenMode, ulAttribute, pFEAList);
1635}
1636
1637int APIENTRY NdpOpenExisting (HCONNECTION conn, NDFILEINFOL *pfi, NDFILEHANDLE *phandle, char *szFileName, ULONG ulOpenMode, USHORT *pfNeedEA)
1638{
1639 if (pfNeedEA) *pfNeedEA = 0; // wtf is this ?
1640 return smbopen((Connection *)conn, szFileName, 0, ulOpenMode, 0, NULL);
1641}
1642
1643int APIENTRY NdpSetFileAttribute (HCONNECTION conn, NDFILEINFOL *pfi, char *szFileName, USHORT usAttr)
1644{
1645 Connection *pConn = (Connection *)conn;
1646 Resource *pRes = pConn->pRes;
1647 int rc = 0;
1648 unsigned long action;
1649
1650 smbwrp_fileinfo finfo;
1651 char path[CCHMAXPATH+1] = {0};
1652
1653 debuglocal(9,"NdpSetFileAttribute in [%p]\n", pConn);
1654 do {
1655 rc = pathparser(pRes, pConn, szFileName, path);
1656 if (rc)
1657 {
1658 break;
1659 }
1660
1661 memset(&finfo, 0, sizeof(finfo));
1662 strncpy(finfo.fname, path, sizeof(finfo.fname) - 1);
1663 finfo.attr = usAttr & 0x37;
1664 rc = smbwrp_setattr(pConn->cli, &finfo);
1665 } while (0);
1666 debuglocal(9,"NdpSetFileAttribute <%s> (%s) %04x %d\n", szFileName, path, usAttr, rc);
1667
1668 return rc;
1669}
1670
1671int APIENTRY NdpFlush (HRESOURCE resource)
1672{
1673 debuglocal(9,"NdpFlush %d\n", ERROR_NOT_SUPPORTED);
1674 return ERROR_NOT_SUPPORTED;
1675}
1676
1677// Called when a new thread will call the plugin. Allows to do per thread init, like disable signals.
1678#define ND_PL_INIT_THREAD 0xFFFF0000
1679
1680int APIENTRY NdpIOCTL (int type, HRESOURCE resource, char *path, int function, void *in, ULONG insize, PULONG poutlen)
1681{
1682 if (function == ND_PL_INIT_THREAD)
1683 {
1684 smbwrp_initthread();
1685 debuglocal(9, "NdpIOCTL init thread\n");
1686 return NO_ERROR;
1687 }
1688
1689 debuglocal(9,"NdpIOCTL <%s> %d\n", path, function);
1690
1691 if (in && insize > 4096)
1692 {
1693 char out[4096];
1694 sprintf (out, "SAMBA IOCTL function = %d, parms [%s] insize = %d, *poutlen = %d", function, in, insize, *poutlen);
1695 *poutlen = strlen(out);
1696 strcpy (in, out);
1697 return NO_ERROR;
1698 }
1699
1700 return ERROR_NOT_SUPPORTED;
1701}
1702
1703int APIENTRY NdpFileQueryInfo (HCONNECTION conn, NDFILEHANDLE handle, void *plist)
1704{
1705 Connection *pConn = (Connection *)conn;
1706 Resource *pRes = pConn->pRes;
1707 int rc = 0;
1708 unsigned long action;
1709 smbwrp_fileinfo finfo;
1710
1711 debug_printf("NdpFileQueryInfo in [%p]\n", pConn);
1712 do {
1713 if (pConn->file.fd < 0 || !*pConn->file.fname)
1714 {
1715 rc = ERROR_INVALID_HANDLE;
1716 break;
1717 }
1718 strncpy(finfo.fname, pConn->file.fname, sizeof(finfo.fname) - 1);
1719 rc = smbwrp_fgetattr(pConn->cli, &pConn->file, &finfo);
1720 if (!rc)
1721 {
1722 finfo.easize = -1;
1723 getfindinfoL(pConn, plist, &finfo, 0, NULL);
1724 }
1725 } while (0);
1726 debuglocal(9,"NdpFileQueryInfo <%s> %d\n", pConn->file.fd < 0 ? "!null!" : pConn->file.fname, rc);
1727
1728 return rc;
1729}
1730
1731int APIENTRY NdpFileEAQuery (HCONNECTION conn, NDFILEHANDLE handle, GEALIST *pGEAList, FEALIST *pFEAList)
1732{
1733 Connection *pConn = (Connection *)conn;
1734 Resource *pRes = pConn->pRes;
1735 int rc = 0;
1736 unsigned long action;
1737 char pBuffer[64*1024];
1738 FEALIST * pFEASrc;
1739
1740 if (!pFEAList)
1741 {
1742 return ERROR_EAS_NOT_SUPPORTED;
1743 }
1744 if (!pRes->easupport)
1745 {
1746 return ERROR_EAS_NOT_SUPPORTED;
1747 }
1748
1749 debuglocal(9,"NdpFileEAQuery in [%p] <%s>/%d pGEAList=%08x\n", pConn, pConn->file.fname, pConn->file.fd, pGEAList);
1750 do {
1751 if (pConn->file.fd < 0)
1752 {
1753 rc = ERROR_INVALID_HANDLE;
1754 break;
1755 }
1756 rc = smbwrp_flistea(pConn->cli, &pConn->file, pBuffer, sizeof( pBuffer));
1757 pFEASrc = (FEALIST *) pBuffer;
1758 if (rc)
1759 {
1760 //rc = pConn->rc ? pConn->rc : (resp.rc ? resp.rc : ERROR_INVALID_PARAMETER);
1761 switch (rc)
1762 {
1763 case ERROR_FILE_NOT_FOUND :
1764 case ERROR_PATH_NOT_FOUND :
1765 {
1766 pFEAList->cbList = sizeof(pFEAList->cbList);
1767 rc = NO_ERROR;
1768 } break;
1769 case ERROR_BUFFER_OVERFLOW :
1770 {
1771 pFEAList->cbList = pFEASrc->cbList;
1772 } break;
1773 default :
1774 {
1775 rc = ERROR_EAS_NOT_SUPPORTED;
1776 }
1777 }
1778 }
1779 else
1780 {
1781 rc = buildFEALIST(pFEASrc, pGEAList, pFEAList);
1782 }
1783 } while (0);
1784 debuglocal(9,"NdpFileEAQuery out <%s>/%d pFEASrc->cbList=%d pFEAList->cbList=%d rc=%d\n", pConn->file.fname, pConn->file.fd, pFEASrc->cbList, pFEAList->cbList, rc);
1785
1786 return rc;
1787}
1788
1789int APIENTRY NdpFileEASet (HCONNECTION conn, NDFILEHANDLE handle, FEALIST *pFEAList)
1790{
1791 Connection *pConn = (Connection *)conn;
1792 Resource *pRes = pConn->pRes;
1793 int rc = 0;
1794 unsigned long action;
1795
1796 debuglocal(9,"NdpFileEASet in [%p]\n", pConn);
1797
1798 if (!pFEAList || pFEAList->cbList <= sizeof(long))
1799 {
1800 return ERROR_EAS_NOT_SUPPORTED;
1801 }
1802 if (!pRes->easupport)
1803 {
1804 return ERROR_EAS_NOT_SUPPORTED;
1805 }
1806
1807 do {
1808 // got FEA there
1809 FEA * pfea;
1810 unsigned long done = sizeof(long);
1811 if (pConn->file.fd < 0)
1812 {
1813 rc = ERROR_INVALID_HANDLE;
1814 break;
1815 }
1816
1817 pfea = pFEAList->list;
1818 while (done < pFEAList->cbList)
1819 {
1820 rc = smbwrp_fsetea(pConn->cli, &pConn->file, (char *)(pfea + 1), pfea->cbValue ? (char *)(pfea + 1) + pfea->cbName + 1: NULL, pfea->cbValue);
1821 if (rc)
1822 {
1823 break;
1824 }
1825 pfea = (FEA *)((char *)(pfea + 1) + pfea->cbName + 1 + pfea->cbValue);
1826 done += sizeof(FEA) + pfea->cbName + 1 + pfea->cbValue;
1827 }
1828
1829 } while (0);
1830 debuglocal(9,"NdpFileEASet %d\n", rc);
1831
1832 return rc;
1833}
1834
1835int APIENTRY NdpFileEASize (HCONNECTION conn, NDFILEHANDLE handle, ULONG *pulEASize)
1836{
1837 Connection *pConn = (Connection *)conn;
1838 Resource *pRes = pConn->pRes;
1839 int rc = 0;
1840 unsigned long action;
1841 char path[CCHMAXPATH+1] = {0};
1842 FEALIST * pFEAList;
1843 char pBuffer[64*1024];
1844
1845 if (!pulEASize)
1846 {
1847 return ERROR_EAS_NOT_SUPPORTED;
1848 }
1849 if (!pRes->easupport)
1850 {
1851 return ERROR_EAS_NOT_SUPPORTED;
1852 }
1853
1854 debuglocal(9,"NdpFileEASize in [%p] <%s>/%d \n", pConn, pConn->file.fname, pConn->file.fd);
1855 do {
1856 if (pConn->file.fd < 0)
1857 {
1858 rc = ERROR_INVALID_HANDLE;
1859 break;
1860 }
1861 rc = smbwrp_flistea(pConn->cli, &pConn->file, pBuffer, sizeof(pBuffer));
1862 pFEAList = (FEALIST*) pBuffer;
1863 if (rc)
1864 {
1865 //rc = pConn->rc ? pConn->rc : (resp.rc ? resp.rc : ERROR_INVALID_PARAMETER);
1866 switch (rc)
1867 {
1868 case ERROR_FILE_NOT_FOUND :
1869 case ERROR_PATH_NOT_FOUND :
1870 {
1871 pFEAList->cbList = sizeof(pFEAList->cbList);
1872 } /* Fall through */
1873 case ERROR_BUFFER_OVERFLOW :
1874 {
1875 rc = NO_ERROR;
1876 } break;
1877 default :
1878 {
1879 rc = ERROR_EAS_NOT_SUPPORTED;
1880 }
1881 }
1882 }
1883 *pulEASize = pFEAList->cbList;
1884 } while (0);
1885 debuglocal(9,"NdpFileEASize %d %d\n", *pulEASize, rc);
1886
1887 return rc;
1888}
1889
1890int APIENTRY NdpFileSetInfo (HCONNECTION conn, NDFILEHANDLE handle, NDFILEINFOL *pfi)
1891{
1892 Connection *pConn = (Connection *)conn;
1893 Resource *pRes = pConn->pRes;
1894 int rc = 0;
1895 unsigned long action, attrFile;
1896
1897 debug_printf("NdpFileSetInfo in [%p]\n", pConn);
1898
1899 // delete the dir cache
1900 dircache_invalidate(pConn->file.fullname, pRes->pdc, 1);
1901
1902 do {
1903 if (pConn->file.fd < 0 || !*pConn->file.fname)
1904 {
1905 rc = ERROR_INVALID_HANDLE;
1906 break;
1907 }
1908 attrFile = pfi->stat.attrFile;
1909 // deferred setinfo - on closing the file
1910 pConn->file.openattr = attrFile;
1911 fsphDosDateToUnixTime(pfi->stat.fdateLastWrite, pfi->stat.ftimeLastWrite, &(pConn->file.mtime));
1912 debug_printf("NdpFileSetInfo mtime %d\n", pConn->file.mtime);
1913 } while (0);
1914 debuglocal(9,"NdpFileSetInfo <%s> %08x %d\n", pConn->file.fd < 0 ? "!null!" : pConn->file.fname, attrFile, rc);
1915
1916 return NO_ERROR;
1917}
1918
1919int APIENTRY NdpFileSetFilePtrL(HCONNECTION conn, NDFILEHANDLE handle, LONGLONG llOffset, ULONG ulMethod, LONGLONG *pllActual)
1920{
1921 Connection *pConn = (Connection *)conn;
1922 Resource *pRes = pConn->pRes;
1923 int rc = 0;
1924 unsigned long action;
1925
1926 debuglocal(9,"NdpFileSetFilePtrL in [%p]\n", pConn);
1927
1928 do {
1929 if (pConn->file.fd < 0)
1930 {
1931 rc = ERROR_INVALID_HANDLE;
1932 break;
1933 }
1934
1935 rc = smbwrp_lseek(pConn->cli, &pConn->file, ulMethod, llOffset);
1936 if (!rc)
1937 *pllActual = pConn->file.offset;
1938
1939 } while (0);
1940 debuglocal(9,"NdpFileSetFilePtrL <%s> %lld %lu %lld %d\n", pConn->file.fd < 0 ? "!null!" : pConn->file.fname, llOffset, ulMethod, *pllActual, rc);
1941
1942 return rc;
1943}
1944
1945int APIENTRY NdpFileSetFilePtr (HCONNECTION conn, NDFILEHANDLE handle, LONG lOffset, ULONG ulMethod, ULONG *pulActual)
1946{
1947 LONGLONG llActual;
1948 int rc = NdpFileSetFilePtrL(conn, handle, lOffset, ulMethod, &llActual);
1949 *pulActual = llActual & 0xFFFFFFFF;
1950 debuglocal(9,"NdpFileSetFilePtr %ld %lu %ld %d\n", lOffset, ulMethod, *pulActual, rc);
1951 return rc;
1952}
1953
1954int APIENTRY NdpFileClose (HCONNECTION conn, NDFILEHANDLE handle)
1955{
1956 Connection *pConn = (Connection *)conn;
1957 Resource *pRes = pConn->pRes;
1958 int rc = 0;
1959 unsigned long action;
1960
1961 debuglocal(9,"NdpFileClose in [%p] %d <%s>\n", pConn, pConn->file.fd, pConn->file.fd < 0 ? "!null!" : pConn->file.fname);
1962
1963 do {
1964 if (pConn->file.fd < 0)
1965 {
1966 rc = ERROR_INVALID_HANDLE;
1967 break;
1968 }
1969
1970 rc = smbwrp_close(pConn->cli, &pConn->file);
1971
1972 } while (0);
1973 debuglocal(9,"NdpFileClose %d %d\n", pConn->file.fd, rc);
1974
1975 pConn->file.fd = -1;
1976 return rc;
1977}
1978
1979int APIENTRY NdpFileCommit (HCONNECTION conn, NDFILEHANDLE handle)
1980{
1981 debuglocal(9,"NdpFileCommit %d\n", NO_ERROR);
1982 return NO_ERROR;
1983}
1984
1985
1986int APIENTRY NdpFileNewSize (HCONNECTION conn, NDFILEHANDLE handle, ULONG ulLen)
1987{
1988 int rc = NdpFileNewSizeL(conn, handle, ulLen);
1989 debuglocal(9,"NdpFileNewSize %ld %d\n", ulLen, rc);
1990 return rc;
1991}
1992
1993int APIENTRY NdpFileNewSizeL(HCONNECTION conn, NDFILEHANDLE handle, LONGLONG llLen)
1994{
1995 Connection *pConn = (Connection *)conn;
1996 Resource *pRes = pConn->pRes;
1997 int rc = 0;
1998 unsigned long action;
1999
2000 debuglocal(9,"NdpFileNewSizeL in [%p]\n", pConn);
2001
2002 do {
2003 if (pConn->file.fd < 0)
2004 {
2005 rc = ERROR_INVALID_HANDLE;
2006 break;
2007 }
2008
2009 rc = smbwrp_setfilesize(pConn->cli, &pConn->file, llLen);
2010
2011 } while (0);
2012 debuglocal(9,"NdpFileNewSizeL <%s> %lld %d\n", pConn->file.fd < 0 ? "!null!" : pConn->file.fname, llLen, rc);
2013
2014 return rc;
2015}
2016
2017#define NDPSMB_READ_MAX_SIZE (65536 - 4096)
2018
2019int APIENTRY NdpFileRead (HCONNECTION conn, NDFILEHANDLE handle, void *pBuffer, ULONG ulRead, ULONG *pulActual)
2020{
2021 Connection *pConn = (Connection *)conn;
2022 Resource *pRes = pConn->pRes;
2023 int rc = 0;
2024 unsigned long done = 0;
2025 unsigned long onedone;
2026 unsigned long action;
2027 ULONG ulReadCompleted = 0;
2028
2029 debuglocal(9,"NdpFileRead in [%p]\n", pConn);
2030
2031 do {
2032 if (pConn->file.fd < 0)
2033 {
2034 rc = ERROR_INVALID_HANDLE;
2035 break;
2036 }
2037 while (ulReadCompleted < ulRead)
2038 {
2039 ULONG ulActual;
2040 ULONG ulToRead = ulRead - ulReadCompleted;
2041 debuglocal(9,"NdpFileRead completed %d, to read %d\n", ulReadCompleted, ulToRead);
2042 if (ulToRead > NDPSMB_READ_MAX_SIZE)
2043 {
2044 ulToRead = NDPSMB_READ_MAX_SIZE;
2045 }
2046 rc = smbwrp_read(pConn->cli, &pConn->file, (char *)pBuffer + ulReadCompleted, ulToRead, &ulActual);
2047 if (ulActual == 0 || rc != NO_ERROR)
2048 {
2049 break;
2050 }
2051 ulReadCompleted += ulActual;
2052 }
2053 //*pulActual = ulRead;
2054 //DosSleep(0);
2055
2056 } while (0);
2057
2058 if (ulReadCompleted > 0)
2059 {
2060 rc = NO_ERROR; /* Still were able to read some data. */
2061 }
2062
2063 if (rc == NO_ERROR)
2064 {
2065 *pulActual = ulReadCompleted;
2066 }
2067
2068 debuglocal(9,"NdpFileRead <%s> %lu %lu %d\n", pConn->file.fd < 0 ? "!null!" : pConn->file.fname, ulRead, *pulActual, rc);
2069
2070 return rc;
2071}
2072
2073int APIENTRY NdpFileWrite (HCONNECTION conn, NDFILEHANDLE handle, void *pBuffer, ULONG ulWrite, ULONG *pulActual)
2074{
2075 Connection *pConn = (Connection *)conn;
2076 Resource *pRes = pConn->pRes;
2077 int rc = 0;
2078 unsigned long done = 0;
2079 unsigned long onedone;
2080 unsigned long action;
2081
2082 debuglocal(9,"NdpFileWrite in [%p]\n", pConn);
2083
2084 /* delete the dir cache
2085 this was moved from NdpFileClose() becasue if there are a lot files in the tree all are reread
2086 the problem when moved to here is, that last accessed time is not refreshed
2087 if this is needed, a new function needs to be done to update only one file in the cache */
2088 dircache_invalidate(pConn->file.fullname, pRes->pdc, 1);
2089
2090 do {
2091 if (pConn->file.fd < 0)
2092 {
2093 rc = ERROR_INVALID_HANDLE;
2094 break;
2095 }
2096 rc = smbwrp_write(pConn->cli, &pConn->file, pBuffer, ulWrite, pulActual);
2097
2098 } while (0);
2099 debuglocal(9,"NdpFileWrite <%s> %lu %lu %d\n", pConn->file.fd < 0 ? "!null!" : pConn->file.fname, ulWrite, *pulActual, rc);
2100
2101 return rc;
2102}
2103
Note: See TracBrowser for help on using the repository browser.