source: branches/client-2.1/src/ndpsmb.c@ 817

Last change on this file since 817 was 817, checked in by Silvan Scherrer, 12 years ago

Samba Client 2.1: timestamp work and some backport from 2.2 client

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