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

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

Samba Client 2.1: removed obsolete code

  • Property svn:eol-style set to native
File size: 53.1 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
892 debuglocal(9,"NdpQueryPathInfo in [%p] <%s>\n", pConn, szPath);
893
894 // is wildcard is specified, we suppose parent dir exist, so exit immediately
895 if (ph->fsphStrChr(szPath, '*') || ph->fsphStrChr(szPath, '?'))
896 {
897 return ERROR_FILE_NOT_FOUND;
898 }
899
900
901 do {
902 /* First check if there is information in the directory cache. */
903 unsigned long ulAge = 0;
904 if (dircache_find_path(pRes->pdc, szPath, &finfo, &ulAge))
905 {
906 if (ulAge <= 15) /* @todo configurable. */
907 {
908 rc = NO_ERROR;
909 finfo.easize = -1;
910 getfindinfoL(pConn, plist, &finfo, 0, NULL);
911 break;
912 }
913 }
914
915 rc = pathparser(pRes, pConn, szPath, path);
916 debuglocal(9,"NdpQueryPathInfo pathparser for <%s> rc=%d\n", path, rc);
917 switch (rc)
918 {
919 case NO_ERROR :
920 case ERROR_FILE_NOT_FOUND:
921 case ERROR_PATH_NOT_FOUND:
922 case ERROR_ACCESS_DENIED:
923 case ERROR_INVALID_PARAMETER:
924 {
925 break;
926 }
927 default :
928 {
929 rc = ERROR_PATH_NOT_FOUND;
930 }
931 }
932 if (rc)
933 {
934 break;
935 }
936 strncpy(finfo.fname, path, sizeof(finfo.fname) - 1);
937 debuglocal(9,"NdpQueryPathInfo smbwrp_getattr for <%s>\n", path);
938 rc = smbwrp_getattr( &pRes->srv, pConn->cli, &finfo);
939 if (rc)
940 {
941 // remote server not available for first time?
942 if (rc == ERROR_REM_NOT_LIST)
943 {
944 // free current cli resources
945 smbwrp_disconnect( pRes, pConn->cli);
946 // reconnect
947 smbwrp_connect( pRes, &pConn->cli);
948 // try file list again
949 rc = smbwrp_getattr( &pRes->srv, pConn->cli, &finfo);
950 debuglocal(9,"NdpQueryPathInfo remote connection lost, rc = %d\n", rc);
951 }
952 switch (rc)
953 {
954 case NO_ERROR :
955 case ERROR_FILE_NOT_FOUND:
956 case ERROR_PATH_NOT_FOUND:
957 case ERROR_ACCESS_DENIED:
958 case ERROR_INVALID_PARAMETER:
959 case ERROR_REM_NOT_LIST:
960 break;
961 default :
962 {
963 rc = ERROR_PATH_NOT_FOUND;
964 }
965 }
966 }
967
968 if (rc == NO_ERROR) {
969 finfo.easize = -1;
970 getfindinfoL(pConn, plist, &finfo, 0, NULL);
971 }
972 else if (rc == ERROR_FILE_NOT_FOUND)
973 {
974 // now try the upper path
975 char * p = ph->fsphStrRChr(finfo.fname, '\\');
976 if (p && p > finfo.fname)
977 {
978 *p = 0;
979 rc = smbwrp_getattr( &pRes->srv, pConn->cli, &finfo);
980 debuglocal(9,"NdpQueryPathInfo upper path in <%s>, rc = %d\n", finfo.fname, rc);
981 if (rc == NO_ERROR)
982 {
983 rc = (finfo.attr & FILE_DIRECTORY) !=0 ?
984 ERROR_PATH_NOT_FOUND :
985 ERROR_INVALID_PARAMETER;
986 }
987 else if (rc != ERROR_REM_NOT_LIST)
988 {
989 rc = ERROR_PATH_NOT_FOUND;
990 }
991 }
992 }
993 } while (0);
994 debuglocal(9,"NdpQueryPathInfo <%s> (%s) %d\n", szPath, path, rc);
995
996 return rc;
997}
998
999// -------------------------------------------------------------
1000
1001int APIENTRY NdpFindStart (HCONNECTION conn, void *plist, NDFILEINFOL *pfiparent, char *szPath, ULONG ulAttribute)
1002{
1003 Connection *pConn = (Connection *)conn;
1004 Resource *pRes = pConn->pRes;
1005 int rc = NO_ERROR, count = 0;
1006 unsigned long action;
1007 char *mask = "*";
1008 char dir[CCHMAXPATH+1] = {0};
1009 char path[CCHMAXPATH+1] = {0};
1010 smbwrp_fileinfo * data;
1011 NDPATHELEMENT *pel = ph->fsphNameElem(0);
1012 filelist_state state;
1013 char * p;
1014
1015 debug_printf("NdpFindStart in [%p]\n", pConn);
1016
1017 strncpy(dir, szPath, sizeof(dir) - 1);
1018 if (pel)
1019 {
1020 mask = pel->name;
1021 dir[strlen(szPath) - pel->length] = 0;
1022 }
1023 action = strlen(dir) - 1;
1024 if (dir[action] == '\\')
1025 {
1026 dir[action] = 0;
1027 }
1028 rc = pathparser(pRes, pConn, dir, path);
1029 if (rc)
1030 {
1031 return rc;
1032 }
1033 action = strlen(path) - 1;
1034 if (path[action] != '\\')
1035 {
1036 strncat(path, "\\", sizeof(path) - 1);
1037 }
1038 strcpy(dir, path);
1039 strncat(path, mask, sizeof(path) - 1);
1040
1041 // this structure will be used by libsmb callbacks, so we store here all we need
1042 // to fill netdrive structures
1043 state.pConn = pConn;
1044 state.plist = plist;
1045 state.ulAttribute = ulAttribute;
1046 strcpy( state.dir, dir);
1047 strcpy( state.dir_mask, mask);
1048 strcpy( state.mask, path);
1049 state.fullpath = szPath;
1050 /* This plugin always reads a complete directory listing and filters results
1051 * using actual mask (state.dir_mask) in getfindinfoL.
1052 * May be this was a workaround for some server bug.
1053 * If this will be changed, then directory listing cache must be changed too,
1054 * and must remember the mask, which was used to obtain a listing.
1055 * Now the directory cache saves complete directory listings and then uses them to find
1056 * information about single files.
1057 * However, with a directory cache, it is probably faster to get a full listing and
1058 * then use it to obtain info about separate files than to perform a network
1059 * list query operation using actual wild cards for each file. Some application,
1060 * for example OpenOffice, do this.
1061 */
1062 p = getlastslash(state.mask);
1063 if (p)
1064 {
1065 *(p + 1) = '*';
1066 *(p + 2) = 0;
1067 }
1068 else
1069 {
1070 strcpy(state.mask, "\\*");
1071 }
1072 debuglocal(9,"NdpFindStart: dir [%s], dir_mask [%s], mask [%s], szPath [%s]\n",
1073 state.dir, state.dir_mask, state.mask, state.fullpath);
1074 rc = smbwrp_filelist( &pRes->srv, pConn->cli, &state);
1075 // we need to handle reconnection also here, because NdpQueryPathInfo
1076 // could be called with '*' and exit then immediately (without calling libsmb)
1077 if (rc == ERROR_REM_NOT_LIST)
1078 {
1079 // free current cli resources
1080 smbwrp_disconnect( pRes, pConn->cli);
1081 // reconnect
1082 smbwrp_connect( pRes, &pConn->cli);
1083 // try file list again next loop
1084 rc = smbwrp_filelist( &pRes->srv, pConn->cli, &state);
1085 debuglocal(9,"NdpFindStart remote connection lost, rc = %d\n", rc);
1086 }
1087
1088 debuglocal(9,"NdpFindStart <%s> (%s) cnt %d %d\n", szPath, path, count, rc);
1089
1090 return rc;
1091}
1092
1093int APIENTRY NdpDeletePathInfo (HRESOURCE resource, NDFILEINFOL *pfi)
1094{
1095// debuglocal(9,"NdpDeletePathInfo %d\n", 0);
1096 return NO_ERROR;
1097}
1098
1099int APIENTRY NdpRefresh (HCONNECTION conn, char *path, int tree)
1100{
1101 debuglocal(9,"NdpRefresh <%s> %d\n", path, 0);
1102 return NO_ERROR;
1103}
1104
1105int APIENTRY NdpDiscardResourceData (HRESOURCE resource, NDDATABUF *pdatabuf)
1106{
1107 // The plugin do not have to deallocate anything
1108 // because resource data did not contain any pointers
1109 // to plugins data.
1110 // Data stored by fsphSetResourceData will be
1111 // deallocated by NetDrive.
1112
1113 debuglocal(9,"NdpDicardresourceData %d\n", 0);
1114 return NO_ERROR;
1115}
1116
1117int APIENTRY NdpSetPathInfo (HCONNECTION conn, NDFILEINFOL *pfi, char *szPathName)
1118{
1119 Connection *pConn = (Connection *)conn;
1120 Resource *pRes = pConn->pRes;
1121 int rc = 0;
1122 unsigned long action;
1123 char path[CCHMAXPATH+1] = {0};
1124 smbwrp_fileinfo finfo;
1125
1126 debug_printf("NdpSetPathInfo in [%p]\n", pConn);
1127
1128 // delete the dir cache
1129 dircache_invalidate(szPathName, pRes->pdc, 1);
1130
1131 do {
1132 rc = pathparser(pRes, pConn, szPathName, path);
1133 if (rc)
1134 {
1135 break;
1136 }
1137
1138 memset(&finfo, 0, sizeof(finfo));
1139
1140 strncpy(finfo.fname, path, sizeof(finfo.fname) - 1);
1141 fsphDosDateToUnixTime(pfi->stat.fdateLastWrite, pfi->stat.ftimeLastWrite, &(finfo.mtime));
1142 finfo.attr = pfi->stat.attrFile & 0x37;
1143 rc = smbwrp_setattr(pConn->cli, &finfo);
1144 } while (0);
1145 debuglocal(9,"NdpSetPathInfo <%s> (%s) %d\n", szPathName, path, rc);
1146
1147 return rc;
1148}
1149
1150int buildFEALIST(FEALIST *pFEASrc, GEALIST *pGEAList, FEALIST *pFEAList)
1151{
1152 int rc = 0;
1153 FEA * pfea;
1154 FEA * pfeadest;
1155 unsigned long size, done = sizeof(pFEAList->cbList), dsize, ddone = sizeof(pFEAList->cbList);
1156
1157 size = pFEASrc->cbList;
1158 pfea = pFEASrc->list;
1159 pfeadest = pFEAList->list;
1160 dsize = pFEAList->cbList;
1161//debuglocal(9,"buildFEALIST in destsize %d srcsize %d pGEAList=%08x pGEAList->cbList=%d\n", dsize, ddone, size, pGEAList, pGEAList ? pGEAList->cbList : 0);
1162 while (done < size)
1163 {
1164 char * name = (char *)(pfea + 1);
1165 int insert = 1;
1166 if (pGEAList && pGEAList->cbList > sizeof(pGEAList->cbList))
1167 {
1168 GEA * pgea = pGEAList->list;
1169 unsigned long size = pGEAList->cbList - sizeof(pGEAList->cbList), done = 0;
1170 insert = 0;
1171 while (done < size)
1172 {
1173//debuglocal(9,"comp <%s> <%s>\n", name, pgea->szName);
1174 if (!ph->fsphStrNCmp(name, pgea->szName, pgea->cbName))
1175 {
1176 insert = 1;
1177 break;
1178 }
1179 done += pgea->cbName + 2;
1180 pgea = (GEA *)((char *)pgea + pgea->cbName + 2);
1181 }
1182 }
1183 if (insert)
1184 {
1185 ddone += sizeof(FEA) + pfea->cbName + 1 + pfea->cbValue;
1186 if (ddone <= dsize)
1187 {
1188 pfeadest->cbName = pfea->cbName;
1189 pfeadest->cbValue = pfea->cbValue;
1190 pfeadest->fEA = 0;
1191 strcpy((char *)(pfeadest + 1), name);
1192 memcpy((char *)(pfeadest + 1) + pfea->cbName + 1, (char *)(pfea + 1) + pfea->cbName + 1, pfea->cbValue);
1193 pfeadest = (FEA *)((char *)pFEAList + ddone);
1194 }
1195 }
1196 done += sizeof(FEA) + pfea->cbName + 1 + pfea->cbValue;
1197//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);
1198 pfea = (FEA *)((char *)pFEASrc + done);
1199 }
1200 pFEAList->cbList = ddone;
1201 if (ddone > dsize && dsize > sizeof(pFEAList->cbList))
1202 {
1203 rc = ERROR_BUFFER_OVERFLOW;
1204 }
1205 debuglocal(9,"buildFEALIST rc=%d destsize=%d destdone=%d srcsize=%d pGEAList=%08x\n", rc, dsize, ddone, size, pGEAList);
1206 return rc;
1207}
1208
1209int APIENTRY NdpEAQuery (HCONNECTION conn, GEALIST *pGEAList, NDFILEINFOL *pfi, FEALIST *pFEAList)
1210{
1211 Connection *pConn = (Connection *)conn;
1212 Resource *pRes = pConn->pRes;
1213 int rc = 0;
1214 unsigned long action;
1215 char * path = NULL;
1216 FEALIST * pFEASrc;
1217 NDDATABUF fdata = {0};
1218 smbwrp_fileinfo *finfo;
1219 char pBuffer[64*1024];
1220
1221 if (!pfi || !pfi->pszName || !pFEAList)
1222 {
1223 return ERROR_EAS_NOT_SUPPORTED;
1224 }
1225 if (!pRes->easupport)
1226 {
1227 return ERROR_EAS_NOT_SUPPORTED;
1228 }
1229
1230 rc = ph->fsphGetFileInfoData(pfi, &fdata, 0);
1231 if (rc || !fdata.ulSize || !fdata.pData)
1232 {
1233 debuglocal(9,"NdpEAQuery: ph->fsphGetFileInfoData = %d/%d %08x\n", rc, fdata.ulSize, fdata.pData);
1234 return ERROR_EAS_NOT_SUPPORTED;
1235 }
1236 finfo = (smbwrp_fileinfo *)fdata.pData;
1237 path = finfo->fname;
1238
1239 debuglocal(9,"NdpEAQuery in [%p] <%s> %08x %d\n", pConn, path, pGEAList, pGEAList ? pGEAList->cbList : 0);
1240
1241 do {
1242 rc = smbwrp_listea( pConn->cli, path, pBuffer, sizeof( pBuffer));
1243 pFEASrc = (FEALIST*) pBuffer;
1244 if (rc)
1245 {
1246 //rc = pConn->rc ? pConn->rc : (resp.rc ? resp.rc : ERROR_INVALID_PARAMETER);
1247 switch (rc)
1248 {
1249 case ERROR_FILE_NOT_FOUND :
1250 case ERROR_PATH_NOT_FOUND :
1251 {
1252 pFEAList->cbList = sizeof(pFEAList->cbList);
1253 rc = NO_ERROR;
1254 } break;
1255 case ERROR_BUFFER_OVERFLOW :
1256 {
1257 pFEAList->cbList = pFEASrc->cbList;
1258 } break;
1259 default :
1260 {
1261 rc = ERROR_EAS_NOT_SUPPORTED;
1262 }
1263 }
1264 }
1265 else
1266 {
1267 rc = buildFEALIST((FEALIST *)pFEASrc, pGEAList, pFEAList);
1268 }
1269 } while (0);
1270 debuglocal(9,"NdpEAQuery <%s> %d %d %d\n", pfi->pszName, rc, pFEASrc->cbList, pFEAList->cbList);
1271
1272 return rc;
1273}
1274
1275int APIENTRY NdpEASet (HCONNECTION conn, FEALIST *pFEAList, NDFILEINFOL *pfi)
1276{
1277 Connection *pConn = (Connection *)conn;
1278 Resource *pRes = pConn->pRes;
1279 int rc = 0;
1280 char * path;
1281 unsigned long action;
1282 NDDATABUF fdata = {0};
1283 smbwrp_fileinfo *finfo;
1284
1285 debuglocal(9,"NdpEASet in [%p]\n", pConn);
1286
1287 if (!pfi || !pfi->pszName || !pFEAList || pFEAList->cbList <= sizeof(long))
1288 {
1289 return ERROR_EAS_NOT_SUPPORTED;
1290 }
1291 if (!pRes->easupport)
1292 {
1293 return ERROR_EAS_NOT_SUPPORTED;
1294 }
1295
1296 rc = ph->fsphGetFileInfoData(pfi, &fdata, 0);
1297 if (rc || !fdata.ulSize || !fdata.pData)
1298 {
1299 debuglocal(9,"NdpEASet: ph->fsphGetFileInfoData = %d/%d/%08x\n", rc, fdata.ulSize, fdata.pData);
1300 return ERROR_EAS_NOT_SUPPORTED;
1301 }
1302 finfo = (smbwrp_fileinfo *)fdata.pData;
1303 path = finfo->fname;
1304
1305 do {
1306 // got FEA there
1307 FEA * pfea;
1308 unsigned long done = sizeof(long);
1309 pfea = pFEAList->list;
1310 while (done < pFEAList->cbList)
1311 {
1312 rc = smbwrp_setea(pConn->cli, path, (char*)(pfea + 1), pfea->cbValue ? (char *)(pfea + 1) + pfea->cbName + 1: NULL, pfea->cbValue);
1313 if (rc)
1314 {
1315 break;
1316 }
1317 pfea = (FEA *)((char *)(pfea + 1) + pfea->cbName + 1 + pfea->cbValue);
1318 done += sizeof(FEA) + pfea->cbName + 1 + pfea->cbValue;
1319 }
1320 } while (0);
1321 debuglocal(9,"NdpEASet %d\n", rc);
1322
1323 return rc;
1324}
1325
1326int APIENTRY NdpEASize (HCONNECTION conn, NDFILEINFOL *pfi, ULONG *pulEASize)
1327{
1328 Connection *pConn = (Connection *)conn;
1329 Resource *pRes = pConn->pRes;
1330 int rc = 0;
1331 unsigned long action;
1332 char * path = NULL;
1333 FEALIST * pfealist;
1334 NDDATABUF fdata = {0};
1335 smbwrp_fileinfo *finfo;
1336 char pBuffer[64*1024];
1337 int easize;
1338
1339 if (!pfi || !pulEASize)
1340 {
1341 return ERROR_EAS_NOT_SUPPORTED;
1342 }
1343 if (!pRes->easupport)
1344 {
1345 return ERROR_EAS_NOT_SUPPORTED;
1346 }
1347
1348 rc = ph->fsphGetFileInfoData(pfi, &fdata, 0);
1349 if (rc || !fdata.ulSize || !fdata.pData)
1350 {
1351 debuglocal(9,"NdpEASize: ph->fsphGetFileInfoData = %d/%d/%08x\n", rc, fdata.ulSize, fdata.pData);
1352 return ERROR_EAS_NOT_SUPPORTED;
1353 }
1354 finfo = (smbwrp_fileinfo *)fdata.pData;
1355 easize = finfo->easize;
1356 finfo->easize = -1;
1357 path = finfo->fname;
1358 if (easize >= 0)
1359 {
1360 *pulEASize = easize;
1361 debuglocal(9,"NdpEASize <%s> cached %d\n", path, easize);
1362 return NO_ERROR;
1363 }
1364
1365 debuglocal(9,"NdpEASize in [%p] <%s> \n", pConn, path);
1366
1367 do {
1368 rc = smbwrp_listea(pConn->cli, path, pBuffer, sizeof( pBuffer));
1369 pfealist = (FEALIST*)pBuffer;
1370 if (rc)
1371 {
1372 //rc = pConn->rc ? pConn->rc : (resp.rc ? resp.rc : ERROR_INVALID_PARAMETER);
1373 switch (rc)
1374 {
1375 case ERROR_FILE_NOT_FOUND :
1376 case ERROR_PATH_NOT_FOUND :
1377 {
1378 pfealist->cbList = sizeof(pfealist->cbList);
1379 } /* Fall through */
1380 case ERROR_BUFFER_OVERFLOW :
1381 {
1382 rc = NO_ERROR;
1383 } break;
1384 default :
1385 {
1386 rc = ERROR_EAS_NOT_SUPPORTED;
1387 }
1388 }
1389 }
1390 *pulEASize = pfealist->cbList;
1391 } while (0);
1392 debuglocal(9,"NdpEASize <%s> %d %d\n", pfi->pszName, *pulEASize, rc);
1393
1394 return rc;
1395}
1396
1397int APIENTRY NdpSetCurrentDir (HCONNECTION conn, NDFILEINFOL *pfi, char *szPath)
1398{
1399 Connection *pConn = (Connection *)conn;
1400 Resource *pRes = pConn->pRes;
1401 int rc = 0;
1402 unsigned long action;
1403 char path[CCHMAXPATH+1] = {0};
1404
1405 debuglocal(9,"NdpSetCurrentDir in [%p]\n", pConn);
1406
1407 do {
1408 rc = pathparser(pRes, pConn, szPath, path);
1409 if (rc)
1410 {
1411 break;
1412 }
1413
1414 rc = smbwrp_chdir(&pRes->srv, pConn->cli, path);
1415 } while (0);
1416 debuglocal(9,"NdpSetCurrentDir <%s> (%s) %d\n", szPath, path, rc);
1417
1418 return rc;
1419}
1420
1421int APIENTRY NdpCopy (HCONNECTION conn, NDFILEINFOL *pfiDst, char *szDst, NDFILEINFOL *pfiSrc, char *szSrc, ULONG ulOption)
1422{
1423 debuglocal(9,"NdpCopy <%s> -> <%s> %d\n", szSrc, szDst, ERROR_CANNOT_COPY);
1424 return ERROR_CANNOT_COPY;
1425}
1426
1427int APIENTRY NdpCopy2 (HCONNECTION conn, HRESOURCE resDst, NDFILEINFOL *pfiDst, char *szDst, NDFILEINFOL *pfiSrc, char *szSrc, ULONG ulOption)
1428{
1429 debuglocal(9,"NdpCopy2 <%s> -> <%s> %d\n", szSrc, szDst, ERROR_CANNOT_COPY);
1430 return ERROR_CANNOT_COPY;
1431}
1432
1433int APIENTRY NdpForceDelete (HCONNECTION conn, NDFILEINFOL *pfi, char *szFile)
1434{
1435 Connection *pConn = (Connection *)conn;
1436 Resource *pRes = pConn->pRes;
1437 int rc = 0;
1438 unsigned long action;
1439 char path[CCHMAXPATH+1] = {0};
1440
1441 debuglocal(9,"NdpForceDelete in [%p]\n", pConn);
1442
1443 dircache_invalidate(szFile, pRes->pdc, 1);
1444
1445 do {
1446 rc = pathparser(pRes, pConn, szFile, path);
1447 if (rc)
1448 {
1449 break;
1450 }
1451
1452 rc = smbwrp_unlink(pConn->cli, path);
1453 } while (0);
1454 debuglocal(9,"NdpForceDelete <%s> (%s) %d\n", szFile, path, rc);
1455
1456 return rc;
1457}
1458
1459int APIENTRY NdpCreateDir (HCONNECTION conn, NDFILEINFOL *pfiparent, char *szDirName, FEALIST *pFEAList)
1460{
1461 Connection *pConn = (Connection *)conn;
1462 Resource *pRes = pConn->pRes;
1463 int rc = 0;
1464 unsigned long action;
1465 char path[CCHMAXPATH+1] = {0};
1466
1467 debuglocal(9,"NdpCreateDir in [%p]\n", pConn);
1468
1469 dircache_invalidate(szDirName, pRes->pdc, 1);
1470
1471 do {
1472 rc = pathparser(pRes, pConn, szDirName, path);
1473 if (rc)
1474 {
1475 break;
1476 }
1477
1478 rc = smbwrp_mkdir(pConn->cli, path);
1479 } while (0);
1480 debuglocal(9,"NdpCreateDir <%s> (%s) %d\n", szDirName, path, rc);
1481
1482 return rc;
1483}
1484
1485int APIENTRY NdpDeleteDir (HCONNECTION conn, NDFILEINFOL *pfi, char *szDir)
1486{
1487 Connection *pConn = (Connection *)conn;
1488 Resource *pRes = pConn->pRes;
1489 int rc = 0;
1490 unsigned long action;
1491 char path[CCHMAXPATH+1] = {0};
1492
1493 debuglocal(9,"NdpDeleteDir in [%p]\n", pConn);
1494
1495 dircache_invalidate(szDir, pRes->pdc, 1);
1496
1497 do {
1498 rc = pathparser(pRes, pConn, szDir, path);
1499 if (rc)
1500 {
1501 break;
1502 }
1503
1504 rc = smbwrp_rmdir(pConn->cli, path);
1505 } while (0);
1506 debuglocal(9,"NdpDeleteDir <%s> (%s) %d\n", szDir, path, rc);
1507
1508 return rc;
1509}
1510
1511int APIENTRY NdpMove (HCONNECTION conn, NDFILEINFOL *pfiDst, char *szDst, NDFILEINFOL *pfiSrc, char *szSrc)
1512{
1513 Connection *pConn = (Connection *)conn;
1514 Resource *pRes = pConn->pRes;
1515 int rc = 0;
1516 unsigned long action;
1517 char src[CCHMAXPATH+1] = {0};
1518 int l1, l2;
1519 char * p = szDst;
1520
1521 debuglocal(9,"NdpMove in [%p] from <%s> to <%s>\n", pConn, szSrc, szDst);
1522
1523 dircache_invalidate(szSrc, pRes->pdc, 1);
1524 dircache_invalidate(szDst, pRes->pdc, 1);
1525
1526 do
1527 {
1528 rc = pathparser(pRes, pConn, szSrc, src);
1529 if (rc)
1530 {
1531 break;
1532 }
1533 l1 = strlen(szSrc);
1534 l2 = strlen(src);
1535 if (l1 > l2)
1536 {
1537 if (ph->fsphStrNICmp(szSrc, szDst, l1 - l2))
1538 {
1539 // the file moved accross different shares or servers or workgroups
1540 rc = ERROR_WRITE_PROTECT;
1541 break;
1542 }
1543 p = szDst + l1 - l2 + 1;
1544 }
1545 //pConn->mem[CCHMAXPATH + 1] = '\\';
1546 rc = smbwrp_rename(pConn->cli, src, p);
1547 } while (0);
1548 debuglocal(9,"NdpMove <%s> -> <%s> (%s) %d\n", szSrc, szDst, src, rc);
1549
1550 return rc;
1551}
1552
1553int APIENTRY NdpMove2 (HCONNECTION conn, HRESOURCE resDst, NDFILEINFOL *pfiDst, char *szDst, NDFILEINFOL *pfiSrc, char *szSrc)
1554{
1555 debuglocal(9,"NdpMove2 <%s> -> <%s> %d\n", szSrc, szDst, ERROR_WRITE_PROTECT);
1556 return ERROR_WRITE_PROTECT;
1557}
1558
1559
1560int APIENTRY NdpChangeCase (HCONNECTION conn, char *szDst, NDFILEINFOL *pfiSrc, char *szSrc, char *szNewName, ULONG ulNameLen)
1561{
1562 return NdpMove (conn, pfiSrc, szDst, pfiSrc, szSrc);
1563}
1564
1565int APIENTRY NdpRename (HCONNECTION conn, char *szDst, NDFILEINFOL *pfiSrc, char *szSrc, char *szNewName, ULONG ulNameLen)
1566{
1567 return NdpMove (conn, pfiSrc, szDst, pfiSrc, szSrc);
1568}
1569
1570int smbopen(Connection *pConn, char *szFileName, int flags, ULONG ulOpenMode, ULONG ulAttribute, FEALIST *pFEAList)
1571{
1572 Resource *pRes = pConn->pRes;
1573 unsigned long action;
1574 int rc = 0;
1575 char path[CCHMAXPATH+1] = {0};
1576
1577 debuglocal(9,"smbopen in [%p] %d\n", pConn, pConn->file.fd);
1578
1579 if (flags & O_CREAT)
1580 {
1581 dircache_invalidate(szFileName, pRes->pdc, 1);
1582 }
1583 do {
1584 if (pConn->file.fd > 0)
1585 {
1586 rc = ERROR_TOO_MANY_OPEN_FILES;
1587 break;
1588 }
1589
1590 rc = pathparser(pRes, pConn, szFileName, path);
1591 if (rc)
1592 {
1593 break;
1594 }
1595
1596 strncpy(pConn->file.fullname, szFileName, sizeof(pConn->file.fullname) - 1);
1597 strncpy(pConn->file.fname, path, sizeof(pConn->file.fname) - 1);
1598 flags |= O_BINARY;
1599 switch (ulOpenMode & 3)
1600 {
1601 case OPEN_ACCESS_READONLY : flags |= O_RDONLY; break;
1602 case OPEN_ACCESS_WRITEONLY : flags |= O_WRONLY; break;
1603 case OPEN_ACCESS_READWRITE : flags |= O_RDWR; break;
1604 default : flags |= O_RDWR;
1605 }
1606 pConn->file.openmode = flags;
1607 pConn->file.openattr = ulAttribute & 0x37;
1608 pConn->file.denymode = (ulOpenMode & 0x70) >> 4;
1609 rc = smbwrp_open(pConn->cli, &pConn->file);
1610 } while (0);
1611 debuglocal(9,"smbopen <%s> (%s) %08x %08x %08x %d. file = %d\n", szFileName, path, flags, ulOpenMode, ulAttribute, rc, pConn->file.fd);
1612 if (!rc && pFEAList)
1613 {
1614 int rc1 = NdpFileEASet((HCONNECTION)pConn, (NDFILEHANDLE)0, pFEAList);
1615 debuglocal(9,"smbopen NdpFileEASet %d. pFEAList->cbList %d\n", rc1, pFEAList->cbList);
1616 }
1617
1618 return rc;
1619}
1620
1621int APIENTRY NdpOpenReplace (HCONNECTION conn, NDFILEINFOL *pfi, NDFILEHANDLE *phandle, char *szFileName, ULONG ulSize, ULONG ulOpenMode, ULONG ulAttribute, FEALIST *pFEAList)
1622{
1623 return smbopen((Connection *)conn, szFileName, O_TRUNC, ulOpenMode, ulAttribute, pFEAList);
1624}
1625
1626int APIENTRY NdpOpenReplaceL(HCONNECTION conn, NDFILEINFO *pfi, NDFILEHANDLE *phandle, char *szFileName, LONGLONG llSize, ULONG ulOpenMode, ULONG ulAttribute, FEALIST *pFEAList)
1627{
1628 return smbopen((Connection *)conn, szFileName, O_TRUNC, ulOpenMode, ulAttribute, pFEAList);
1629}
1630
1631int APIENTRY NdpOpenCreate (HCONNECTION conn, NDFILEINFOL *pfiparent, NDFILEHANDLE *phandle, char *szFileName, ULONG ulSize, ULONG ulOpenMode, ULONG ulAttribute, FEALIST *pFEAList)
1632{
1633// return smbopen((Connection *)conn, szFileName, O_CREAT, ulOpenMode, ulAttribute);
1634 return smbopen((Connection *)conn, szFileName, O_CREAT | O_EXCL, ulOpenMode, ulAttribute, pFEAList);
1635}
1636
1637int APIENTRY NdpOpenCreateL(HCONNECTION conn, NDFILEINFO *pfiparent, NDFILEHANDLE *phandle, char *szFileName, LONGLONG llSize, ULONG ulOpenMode, ULONG ulAttribute, FEALIST *pFEAList)
1638{
1639 return smbopen((Connection *)conn, szFileName, O_CREAT | O_EXCL, ulOpenMode, ulAttribute, pFEAList);
1640}
1641
1642int APIENTRY NdpOpenExisting (HCONNECTION conn, NDFILEINFOL *pfi, NDFILEHANDLE *phandle, char *szFileName, ULONG ulOpenMode, USHORT *pfNeedEA)
1643{
1644 if (pfNeedEA) *pfNeedEA = 0; // wtf is this ?
1645 return smbopen((Connection *)conn, szFileName, 0, ulOpenMode, 0, NULL);
1646}
1647
1648int APIENTRY NdpSetFileAttribute (HCONNECTION conn, NDFILEINFOL *pfi, char *szFileName, USHORT usAttr)
1649{
1650 Connection *pConn = (Connection *)conn;
1651 Resource *pRes = pConn->pRes;
1652 int rc = 0;
1653 unsigned long action;
1654
1655 smbwrp_fileinfo finfo;
1656 char path[CCHMAXPATH+1] = {0};
1657
1658 debuglocal(9,"NdpSetFileAttribute in [%p]\n", pConn);
1659 do {
1660 rc = pathparser(pRes, pConn, szFileName, path);
1661 if (rc)
1662 {
1663 break;
1664 }
1665
1666 memset(&finfo, 0, sizeof(finfo));
1667 strncpy(finfo.fname, path, sizeof(finfo.fname) - 1);
1668 finfo.attr = usAttr & 0x37;
1669 rc = smbwrp_setattr(pConn->cli, &finfo);
1670 } while (0);
1671 debuglocal(9,"NdpSetFileAttribute <%s> (%s) %04x %d\n", szFileName, path, usAttr, rc);
1672
1673 return rc;
1674}
1675
1676int APIENTRY NdpFlush (HRESOURCE resource)
1677{
1678 debuglocal(9,"NdpFlush %d\n", ERROR_NOT_SUPPORTED);
1679 return ERROR_NOT_SUPPORTED;
1680}
1681
1682// Called when a new thread will call the plugin. Allows to do per thread init, like disable signals.
1683#define ND_PL_INIT_THREAD 0xFFFF0000
1684
1685int APIENTRY NdpIOCTL (int type, HRESOURCE resource, char *path, int function, void *in, ULONG insize, PULONG poutlen)
1686{
1687 if (function == ND_PL_INIT_THREAD)
1688 {
1689 smbwrp_initthread();
1690 debuglocal(9, "NdpIOCTL init thread\n");
1691 return NO_ERROR;
1692 }
1693
1694 debuglocal(9,"NdpIOCTL <%s> %d\n", path, function);
1695
1696 if (in && insize > 4096)
1697 {
1698 char out[4096];
1699 sprintf (out, "SAMBA IOCTL function = %d, parms [%s] insize = %d, *poutlen = %d", function, in, insize, *poutlen);
1700 *poutlen = strlen(out);
1701 strcpy (in, out);
1702 return NO_ERROR;
1703 }
1704
1705 return ERROR_NOT_SUPPORTED;
1706}
1707
1708int APIENTRY NdpFileQueryInfo (HCONNECTION conn, NDFILEHANDLE handle, void *plist)
1709{
1710 Connection *pConn = (Connection *)conn;
1711 Resource *pRes = pConn->pRes;
1712 int rc = 0;
1713 unsigned long action;
1714 smbwrp_fileinfo finfo;
1715
1716 debug_printf("NdpFileQueryInfo in [%p]\n", pConn);
1717 do {
1718 if (pConn->file.fd < 0 || !*pConn->file.fname)
1719 {
1720 rc = ERROR_INVALID_HANDLE;
1721 break;
1722 }
1723 strncpy(finfo.fname, pConn->file.fname, sizeof(finfo.fname) - 1);
1724 rc = smbwrp_fgetattr(pConn->cli, &pConn->file, &finfo);
1725 if (!rc)
1726 {
1727 finfo.easize = -1;
1728 getfindinfoL(pConn, plist, &finfo, 0, NULL);
1729 }
1730 } while (0);
1731 debuglocal(9,"NdpFileQueryInfo <%s> %d\n", pConn->file.fd < 0 ? "!null!" : pConn->file.fname, rc);
1732
1733 return rc;
1734}
1735
1736int APIENTRY NdpFileEAQuery (HCONNECTION conn, NDFILEHANDLE handle, GEALIST *pGEAList, FEALIST *pFEAList)
1737{
1738 Connection *pConn = (Connection *)conn;
1739 Resource *pRes = pConn->pRes;
1740 int rc = 0;
1741 unsigned long action;
1742 char pBuffer[64*1024];
1743 FEALIST * pFEASrc;
1744
1745 if (!pFEAList)
1746 {
1747 return ERROR_EAS_NOT_SUPPORTED;
1748 }
1749 if (!pRes->easupport)
1750 {
1751 return ERROR_EAS_NOT_SUPPORTED;
1752 }
1753
1754 debuglocal(9,"NdpFileEAQuery in [%p] <%s>/%d pGEAList=%08x\n", pConn, pConn->file.fname, pConn->file.fd, pGEAList);
1755 do {
1756 if (pConn->file.fd < 0)
1757 {
1758 rc = ERROR_INVALID_HANDLE;
1759 break;
1760 }
1761 rc = smbwrp_flistea(pConn->cli, &pConn->file, pBuffer, sizeof( pBuffer));
1762 pFEASrc = (FEALIST *) pBuffer;
1763 if (rc)
1764 {
1765 //rc = pConn->rc ? pConn->rc : (resp.rc ? resp.rc : ERROR_INVALID_PARAMETER);
1766 switch (rc)
1767 {
1768 case ERROR_FILE_NOT_FOUND :
1769 case ERROR_PATH_NOT_FOUND :
1770 {
1771 pFEAList->cbList = sizeof(pFEAList->cbList);
1772 rc = NO_ERROR;
1773 } break;
1774 case ERROR_BUFFER_OVERFLOW :
1775 {
1776 pFEAList->cbList = pFEASrc->cbList;
1777 } break;
1778 default :
1779 {
1780 rc = ERROR_EAS_NOT_SUPPORTED;
1781 }
1782 }
1783 }
1784 else
1785 {
1786 rc = buildFEALIST(pFEASrc, pGEAList, pFEAList);
1787 }
1788 } while (0);
1789 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);
1790
1791 return rc;
1792}
1793
1794int APIENTRY NdpFileEASet (HCONNECTION conn, NDFILEHANDLE handle, FEALIST *pFEAList)
1795{
1796 Connection *pConn = (Connection *)conn;
1797 Resource *pRes = pConn->pRes;
1798 int rc = 0;
1799 unsigned long action;
1800
1801 debuglocal(9,"NdpFileEASet in [%p]\n", pConn);
1802
1803 if (!pFEAList || pFEAList->cbList <= sizeof(long))
1804 {
1805 return ERROR_EAS_NOT_SUPPORTED;
1806 }
1807 if (!pRes->easupport)
1808 {
1809 return ERROR_EAS_NOT_SUPPORTED;
1810 }
1811
1812 do {
1813 // got FEA there
1814 FEA * pfea;
1815 unsigned long done = sizeof(long);
1816 if (pConn->file.fd < 0)
1817 {
1818 rc = ERROR_INVALID_HANDLE;
1819 break;
1820 }
1821
1822 pfea = pFEAList->list;
1823 while (done < pFEAList->cbList)
1824 {
1825 rc = smbwrp_fsetea(pConn->cli, &pConn->file, (char *)(pfea + 1), pfea->cbValue ? (char *)(pfea + 1) + pfea->cbName + 1: NULL, pfea->cbValue);
1826 if (rc)
1827 {
1828 break;
1829 }
1830 pfea = (FEA *)((char *)(pfea + 1) + pfea->cbName + 1 + pfea->cbValue);
1831 done += sizeof(FEA) + pfea->cbName + 1 + pfea->cbValue;
1832 }
1833
1834 } while (0);
1835 debuglocal(9,"NdpFileEASet %d\n", rc);
1836
1837 return rc;
1838}
1839
1840int APIENTRY NdpFileEASize (HCONNECTION conn, NDFILEHANDLE handle, ULONG *pulEASize)
1841{
1842 Connection *pConn = (Connection *)conn;
1843 Resource *pRes = pConn->pRes;
1844 int rc = 0;
1845 unsigned long action;
1846 char path[CCHMAXPATH+1] = {0};
1847 FEALIST * pFEAList;
1848 char pBuffer[64*1024];
1849
1850 if (!pulEASize)
1851 {
1852 return ERROR_EAS_NOT_SUPPORTED;
1853 }
1854 if (!pRes->easupport)
1855 {
1856 return ERROR_EAS_NOT_SUPPORTED;
1857 }
1858
1859 debuglocal(9,"NdpFileEASize in [%p] <%s>/%d \n", pConn, pConn->file.fname, pConn->file.fd);
1860 do {
1861 if (pConn->file.fd < 0)
1862 {
1863 rc = ERROR_INVALID_HANDLE;
1864 break;
1865 }
1866 rc = smbwrp_flistea(pConn->cli, &pConn->file, pBuffer, sizeof(pBuffer));
1867 pFEAList = (FEALIST*) pBuffer;
1868 if (rc)
1869 {
1870 //rc = pConn->rc ? pConn->rc : (resp.rc ? resp.rc : ERROR_INVALID_PARAMETER);
1871 switch (rc)
1872 {
1873 case ERROR_FILE_NOT_FOUND :
1874 case ERROR_PATH_NOT_FOUND :
1875 {
1876 pFEAList->cbList = sizeof(pFEAList->cbList);
1877 } /* Fall through */
1878 case ERROR_BUFFER_OVERFLOW :
1879 {
1880 rc = NO_ERROR;
1881 } break;
1882 default :
1883 {
1884 rc = ERROR_EAS_NOT_SUPPORTED;
1885 }
1886 }
1887 }
1888 *pulEASize = pFEAList->cbList;
1889 } while (0);
1890 debuglocal(9,"NdpFileEASize %d %d\n", *pulEASize, rc);
1891
1892 return rc;
1893}
1894
1895int APIENTRY NdpFileSetInfo (HCONNECTION conn, NDFILEHANDLE handle, NDFILEINFOL *pfi)
1896{
1897 Connection *pConn = (Connection *)conn;
1898 Resource *pRes = pConn->pRes;
1899 int rc = 0;
1900 unsigned long action, attrFile;
1901
1902 debug_printf("NdpFileSetInfo in [%p]\n", pConn);
1903
1904 // delete the dir cache
1905 dircache_invalidate(pConn->file.fullname, pRes->pdc, 1);
1906
1907 do {
1908 if (pConn->file.fd < 0 || !*pConn->file.fname)
1909 {
1910 rc = ERROR_INVALID_HANDLE;
1911 break;
1912 }
1913 attrFile = pfi->stat.attrFile;
1914 // deferred setinfo - on closing the file
1915 pConn->file.openattr = attrFile;
1916 fsphDosDateToUnixTime(pfi->stat.fdateLastWrite, pfi->stat.ftimeLastWrite, &(pConn->file.mtime));
1917 debug_printf("NdpFileSetInfo mtime %d\n", pConn->file.mtime);
1918 } while (0);
1919 debuglocal(9,"NdpFileSetInfo <%s> %08x %d\n", pConn->file.fd < 0 ? "!null!" : pConn->file.fname, attrFile, rc);
1920
1921 return NO_ERROR;
1922}
1923
1924int APIENTRY NdpFileSetFilePtrL(HCONNECTION conn, NDFILEHANDLE handle, LONGLONG llOffset, ULONG ulMethod, LONGLONG *pllActual)
1925{
1926 Connection *pConn = (Connection *)conn;
1927 Resource *pRes = pConn->pRes;
1928 int rc = 0;
1929 unsigned long action;
1930
1931 debuglocal(9,"NdpFileSetFilePtrL in [%p]\n", pConn);
1932
1933 do {
1934 if (pConn->file.fd < 0)
1935 {
1936 rc = ERROR_INVALID_HANDLE;
1937 break;
1938 }
1939
1940 rc = smbwrp_lseek(pConn->cli, &pConn->file, ulMethod, llOffset);
1941 if (!rc)
1942 *pllActual = pConn->file.offset;
1943
1944 } while (0);
1945 debuglocal(9,"NdpFileSetFilePtrL <%s> %lld %lu %lld %d\n", pConn->file.fd < 0 ? "!null!" : pConn->file.fname, llOffset, ulMethod, *pllActual, rc);
1946
1947 return rc;
1948}
1949
1950int APIENTRY NdpFileSetFilePtr (HCONNECTION conn, NDFILEHANDLE handle, LONG lOffset, ULONG ulMethod, ULONG *pulActual)
1951{
1952 LONGLONG llActual;
1953 int rc = NdpFileSetFilePtrL(conn, handle, lOffset, ulMethod, &llActual);
1954 *pulActual = llActual & 0xFFFFFFFF;
1955 debuglocal(9,"NdpFileSetFilePtr %ld %lu %ld %d\n", lOffset, ulMethod, *pulActual, rc);
1956 return rc;
1957}
1958
1959int APIENTRY NdpFileClose (HCONNECTION conn, NDFILEHANDLE handle)
1960{
1961 Connection *pConn = (Connection *)conn;
1962 Resource *pRes = pConn->pRes;
1963 int rc = 0;
1964 unsigned long action;
1965
1966 debuglocal(9,"NdpFileClose in [%p] %d <%s>\n", pConn, pConn->file.fd, pConn->file.fd < 0 ? "!null!" : pConn->file.fname);
1967
1968 do {
1969 if (pConn->file.fd < 0)
1970 {
1971 rc = ERROR_INVALID_HANDLE;
1972 break;
1973 }
1974
1975 rc = smbwrp_close(pConn->cli, &pConn->file);
1976
1977 } while (0);
1978 debuglocal(9,"NdpFileClose %d %d\n", pConn->file.fd, rc);
1979
1980 pConn->file.fd = -1;
1981 return rc;
1982}
1983
1984int APIENTRY NdpFileCommit (HCONNECTION conn, NDFILEHANDLE handle)
1985{
1986 debuglocal(9,"NdpFileCommit %d\n", NO_ERROR);
1987 return NO_ERROR;
1988}
1989
1990
1991int APIENTRY NdpFileNewSize (HCONNECTION conn, NDFILEHANDLE handle, ULONG ulLen)
1992{
1993 int rc = NdpFileNewSizeL(conn, handle, ulLen);
1994 debuglocal(9,"NdpFileNewSize %ld %d\n", ulLen, rc);
1995 return rc;
1996}
1997
1998int APIENTRY NdpFileNewSizeL(HCONNECTION conn, NDFILEHANDLE handle, LONGLONG llLen)
1999{
2000 Connection *pConn = (Connection *)conn;
2001 Resource *pRes = pConn->pRes;
2002 int rc = 0;
2003 unsigned long action;
2004
2005 debuglocal(9,"NdpFileNewSizeL in [%p]\n", pConn);
2006
2007 do {
2008 if (pConn->file.fd < 0)
2009 {
2010 rc = ERROR_INVALID_HANDLE;
2011 break;
2012 }
2013
2014 rc = smbwrp_setfilesize(pConn->cli, &pConn->file, llLen);
2015
2016 } while (0);
2017 debuglocal(9,"NdpFileNewSizeL <%s> %lld %d\n", pConn->file.fd < 0 ? "!null!" : pConn->file.fname, llLen, rc);
2018
2019 return rc;
2020}
2021
2022#define NDPSMB_READ_MAX_SIZE (65536 - 4096)
2023
2024int APIENTRY NdpFileRead (HCONNECTION conn, NDFILEHANDLE handle, void *pBuffer, ULONG ulRead, ULONG *pulActual)
2025{
2026 Connection *pConn = (Connection *)conn;
2027 Resource *pRes = pConn->pRes;
2028 int rc = 0;
2029 unsigned long done = 0;
2030 unsigned long onedone;
2031 unsigned long action;
2032 ULONG ulReadCompleted = 0;
2033
2034 debuglocal(9,"NdpFileRead in [%p]\n", pConn);
2035
2036 do {
2037 if (pConn->file.fd < 0)
2038 {
2039 rc = ERROR_INVALID_HANDLE;
2040 break;
2041 }
2042 while (ulReadCompleted < ulRead)
2043 {
2044 ULONG ulActual;
2045 ULONG ulToRead = ulRead - ulReadCompleted;
2046 debuglocal(9,"NdpFileRead completed %d, to read %d\n", ulReadCompleted, ulToRead);
2047 if (ulToRead > NDPSMB_READ_MAX_SIZE)
2048 {
2049 ulToRead = NDPSMB_READ_MAX_SIZE;
2050 }
2051 rc = smbwrp_read(pConn->cli, &pConn->file, (char *)pBuffer + ulReadCompleted, ulToRead, &ulActual);
2052 if (ulActual == 0 || rc != NO_ERROR)
2053 {
2054 break;
2055 }
2056 ulReadCompleted += ulActual;
2057 }
2058 //*pulActual = ulRead;
2059 //DosSleep(0);
2060
2061 } while (0);
2062
2063 if (ulReadCompleted > 0)
2064 {
2065 rc = NO_ERROR; /* Still were able to read some data. */
2066 }
2067
2068 if (rc == NO_ERROR)
2069 {
2070 *pulActual = ulReadCompleted;
2071 }
2072
2073 debuglocal(9,"NdpFileRead <%s> %lu %lu %d\n", pConn->file.fd < 0 ? "!null!" : pConn->file.fname, ulRead, *pulActual, rc);
2074
2075 return rc;
2076}
2077
2078int APIENTRY NdpFileWrite (HCONNECTION conn, NDFILEHANDLE handle, void *pBuffer, ULONG ulWrite, ULONG *pulActual)
2079{
2080 Connection *pConn = (Connection *)conn;
2081 Resource *pRes = pConn->pRes;
2082 int rc = 0;
2083 unsigned long done = 0;
2084 unsigned long onedone;
2085 unsigned long action;
2086
2087 debuglocal(9,"NdpFileWrite in [%p]\n", pConn);
2088
2089 /* delete the dir cache
2090 this was moved from NdpFileClose() becasue if there are a lot files in the tree all are reread
2091 the problem when moved to here is, that last accessed time is not refreshed
2092 if this is needed, a new function needs to be done to update only one file in the cache */
2093 dircache_invalidate(pConn->file.fullname, pRes->pdc, 1);
2094
2095 do {
2096 if (pConn->file.fd < 0)
2097 {
2098 rc = ERROR_INVALID_HANDLE;
2099 break;
2100 }
2101 rc = smbwrp_write(pConn->cli, &pConn->file, pBuffer, ulWrite, pulActual);
2102
2103 } while (0);
2104 debuglocal(9,"NdpFileWrite <%s> %lu %lu %d\n", pConn->file.fd < 0 ? "!null!" : pConn->file.fname, ulWrite, *pulActual, rc);
2105
2106 return rc;
2107}
2108
Note: See TracBrowser for help on using the repository browser.