-
Notifications
You must be signed in to change notification settings - Fork 0
/
ninefs.c
916 lines (833 loc) · 20.7 KB
/
ninefs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
/*
* ninefs.c
* Dokan-based 9p filesystem for windows.
*
* TODO:
* - error reporting when dokan fails will make it easier for users
* with dokan incorrectly installed
* - better 8.3 support? eliminate?
* - make good win32 error codes
* - investigate user security. right now all files appear to be
* owned by the user mounting the filesystem. Is this a dokan limitation?
* - ssl support
* - support attach name as an argument
*/
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include "npfs.h"
#include "npclient.h"
#include "npauth.h"
#include "dokan.h"
#define ARRSZ(a) (sizeof(a) / sizeof((a)[0]))
static Npuser *user = NULL;
static Npcfsys *fs = NULL;
static int debug = 0;
static int transPath = 1;
static int optind = 1;
static int optpos = 0;
static char *optarg = NULL;
static int
getopt(int argc, char **argv, const char *opts)
{
char *p, ch;
if(optind >= argc || !argv[optind])
return -1;
if(optpos && !argv[optind][optpos]) {
optind ++;
optpos = 0;
}
if(optind >= argc || !argv[optind])
return -1;
if(optpos == 0 && argv[optind][optpos++] != '-')
return -1;
ch = argv[optind][optpos++];
p = strchr(opts, ch);
if(!p)
return '?';
if(p[1] != ':')
return ch;
optarg = argv[optind++] + optpos;
optpos = 0;
if(*optarg)
return ch;
if(optind >= argc || !argv[optind])
return '?';
optarg = argv[optind++];
return ch;
}
static int
notyet(char *msg) {
fprintf(stderr, "notyet: %s\n", msg);
fflush(stderr);
return -(int)ERROR_CALL_NOT_IMPLEMENTED;
}
// Return a dynamically allocated utf8 string.
static char *
utf8(LPCWSTR ws)
{
char *s;
int l, e;
if(!ws)
ws = L"";
l = WideCharToMultiByte(CP_UTF8, 0, ws, -1, NULL, 0, NULL, NULL);
e = GetLastError();
if((!transPath && e == ERROR_NO_UNICODE_TRANSLATION)
|| l == 0) {
if(debug)
fprintf(stderr, "utf8 bad conversion: %d\n", e);
return NULL;
}
s = malloc(l);
if(!s) {
if(debug)
fprintf(stderr, "utf8 malloc failed\n");
return NULL;
}
WideCharToMultiByte(CP_UTF8, 0, ws, -1, s, l, NULL, NULL);
return s;
}
// Return a dynamically allocated path in utf8 format with p9 conversions.
static char*
p9path(LPCWSTR ws)
{
char *fn = utf8(ws);
int i;
if(fn) {
for(i = 0; fn[i]; i++) {
switch(fn[i]) {
case '\\':
fn[i] = '/';
break;
case ' ':
if(transPath)
fn[i] = '?';
break;
}
}
}
return fn;
}
// Return a dynamically allocated wide string.
static LPWSTR
wstr(char *s)
{
LPWSTR ws;
int l, e;
if(!s)
s = "";
l = MultiByteToWideChar(CP_UTF8, 0, s, -1, NULL, 0);
e = GetLastError();
if((!transPath && e == ERROR_NO_UNICODE_TRANSLATION)
|| l == 0) {
if(debug)
fprintf(stderr, "wstr bad conversion: %d\n", e);
return NULL;
}
ws = malloc(l * sizeof *ws);
if(!s) {
if(debug)
fprintf(stderr, "wstr malloc failed\n");
return NULL;
}
MultiByteToWideChar(CP_UTF8, 0, s, -1, ws, l);
return ws;
}
// Return a dynamically allocated path in wstr format with win conversions.
static LPWSTR
winpath(char *s)
{
LPWSTR fn = wstr(s);
int i;
if(fn) {
for(i = 0; fn[i]; i++) {
switch(fn[i]) {
case L'/':
fn[i] = L'\\';
break;
case L'?':
if(transPath)
fn[i] = L' ';
break;
}
}
}
return fn;
}
static void
maybeOpen(LPCWSTR fname, int omode, int *opened, Npcfid **fidp)
{
char *fn;
*opened = 0;
if(*fidp)
return;
fn = p9path(fname);
*fidp = npc_open(fs, fn, omode);
if(*fidp)
*opened = 1;
free(fn);
}
static void
maybeClose(int *opened, Npcfid **fidp)
{
if(*opened)
npc_close(*fidp);
}
static u32
fromFT(const FILETIME *f)
{
LONGLONG dt = f->dwLowDateTime | ((LONGLONG)f->dwHighDateTime << 32);
return (u32)((dt - 116444736000000000) / 10000000);
}
static FILETIME
toFT(u32 ut)
{
FILETIME f;
LONGLONG dt;
dt = Int32x32To64(ut, 10000000) + 116444736000000000;
f.dwLowDateTime = (DWORD)dt;
f.dwHighDateTime = (DWORD)(dt >> 32);
return f;
}
static FILETIME zt = { 0, 0 };
static void
toFileInfo(Npwstat *st, LPBY_HANDLE_FILE_INFORMATION fi)
{
fi->dwFileAttributes = 0;
if(st->qid.type & Qtdir)
fi->dwFileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
else
fi->dwFileAttributes |= FILE_ATTRIBUTE_NORMAL;
fi->ftCreationTime = zt;
fi->ftLastAccessTime = toFT(st->atime);
fi->ftLastWriteTime = toFT(st->mtime);
fi->dwVolumeSerialNumber = st->dev;
fi->nFileSizeHigh = (DWORD)(st->length >> 32);
fi->nFileSizeLow = (DWORD)st->length;
fi->nNumberOfLinks = 1;
fi->nFileIndexHigh = (DWORD)(st->qid.path >> 32);
fi->nFileIndexLow = (DWORD)st->qid.path;
}
static int
toFindData(Npwstat *st, WIN32_FIND_DATA *fd)
{
LPWSTR fn = winpath(st->name);
int i, j;
if(!fn)
return -(int)ERROR_FILE_NOT_FOUND;
fd->dwFileAttributes = 0;
if(st->qid.type & Qtdir)
fd->dwFileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
else
fd->dwFileAttributes |= FILE_ATTRIBUTE_NORMAL;
fd->ftCreationTime = zt;
fd->ftLastAccessTime = toFT(st->atime);
fd->ftLastWriteTime = toFT(st->mtime);
fd->nFileSizeHigh = (DWORD)(st->length >> 32);
fd->nFileSizeLow = (DWORD)st->length;
fd->dwReserved0 = 0;
fd->dwReserved1 = 0;
wcsncpy(fd->cFileName, fn, ARRSZ(fd->cFileName) - 1);
free(fn);
// XXX this is a really bad hack:
for(j = 0, i = 0 ; j < 13 && fd->cFileName[i]; i++) {
if(j == 8)
fd->cAlternateFileName[j++] = '.';
if(st->name[i] != '.')
fd->cAlternateFileName[j++] = fd->cFileName[i];
}
fd->cAlternateFileName[j] = 0;
return 0;
}
static int
cvtError(void)
{
char *err;
int num;
np_rerror(&err, &num);
switch(num) {
case ENOENT: return -(int)ERROR_FILE_NOT_FOUND;
default: return -(int)ERROR_INVALID_PARAMETER; // XXX bogus
}
}
static int
_CreateFile(
LPCWSTR FileName,
DWORD AccessMode,
DWORD ShareMode,
DWORD CreationDisposition,
DWORD FlagsAndAttributes,
PDOKAN_FILE_INFO DokanFileInfo)
{
Npcfid *fid = NULL;
char *fn;
int omode, rd, wr;
if(debug)
fprintf(stderr, "createfile '%ws' create %d access %x flags %x\n", FileName, CreationDisposition, AccessMode, FlagsAndAttributes);
rd = (AccessMode & (GENERIC_READ | FILE_READ_DATA)) != 0;
wr = (AccessMode & (GENERIC_WRITE | FILE_WRITE_DATA)) != 0;
if(rd && wr)
omode = Ordwr;
else if(wr)
omode = Owrite;
else
omode = Oread;
if(CreationDisposition == TRUNCATE_EXISTING)
omode |= Otrunc;
fn = p9path(FileName);
if(!fn)
return -(int)ERROR_NOT_ENOUGH_MEMORY;
fid = npc_open(fs, fn, omode);
if(!fid && (CreationDisposition == CREATE_ALWAYS ||
CreationDisposition == CREATE_NEW ||
CreationDisposition == OPEN_ALWAYS)) {
fid = npc_create(fs, fn, 0666, omode);
}
free(fn);
if(!fid) {
if(debug)
fprintf(stderr, "open %ws failed\n", FileName);
return cvtError();
}
DokanFileInfo->Context = (ULONG64)fid;
return 0;
}
static int
_CreateDirectory(
LPCWSTR FileName,
PDOKAN_FILE_INFO DokanFileInfo)
{
Npcfid *fid;
char *fn;
int perm;
if(debug)
fprintf(stderr, "create directory '%ws'\n", FileName);
fn = p9path(FileName);
perm = Dmdir | 0777; // XXX figure out perm
fid = npc_create(fs, fn, perm, Oread);
if(fid)
npc_close(fid);
free(fn);
if(!fid) {
if(debug)
fprintf(stderr, "create directory %ws failed\n", FileName);
return cvtError();
}
return 0;
}
static int
_OpenDirectory(
LPCWSTR FileName,
PDOKAN_FILE_INFO DokanFileInfo)
{
Npcfid *fid = NULL;
char *fn;
int e;
if(debug)
fprintf(stderr, "open directory '%ws'\n", FileName);
fn = p9path(FileName);
if(!fn)
return -(int)ERROR_NOT_ENOUGH_MEMORY;
e = 0;
fid = npc_open(fs, fn, Oread);
if(fid && !(fid->qid.type & Qtdir)) {
npc_close(fid);
fid = NULL;
e = -(int)ERROR_DIRECTORY; // XXX?
} else if(!fid) {
e = cvtError();
}
free(fn);
if(e) {
if(debug)
fprintf(stderr, "diropen %ws failed\n", FileName);
return e;
}
DokanFileInfo->Context = (ULONG64)fid;
return 0;
}
static int
_CloseFile(
LPCWSTR FileName,
PDOKAN_FILE_INFO DokanFileInfo)
{
Npcfid *fid = (Npcfid *)DokanFileInfo->Context;
if(fid) {
DokanFileInfo->Context = 0;
npc_close(fid);
}
return 0;
}
static int
_Cleanup(
LPCWSTR FileName,
PDOKAN_FILE_INFO DokanFileInfo)
{
// XXX handle delete-on-close?
return _CloseFile(FileName, DokanFileInfo);
}
static int
_ReadFile(
LPCWSTR FileName,
LPVOID Buffer,
DWORD BufferLength,
LPDWORD ReadLength,
LONGLONG Offset,
PDOKAN_FILE_INFO DokanFileInfo)
{
Npcfid *fid = (Npcfid *)DokanFileInfo->Context;
int e, r, opened;
if(debug)
fprintf(stderr, "readfile\n");
maybeOpen(FileName, Oread, &opened, &fid);
if(!fid)
return cvtError();
e = 0;
r = npc_read(fid, Buffer, BufferLength, Offset);
if(r < 0)
e = cvtError();
maybeClose(&opened, &fid);
if(e) {
if(debug)
fprintf(stderr, "readfile error\n");
return e;
}
*ReadLength = r;
return 0;
}
static int
_WriteFile(
LPCWSTR FileName,
LPCVOID Buffer,
DWORD NumberOfBytesToWrite,
LPDWORD NumberOfBytesWritten,
LONGLONG Offset,
PDOKAN_FILE_INFO DokanFileInfo)
{
Npcfid *fid = (Npcfid *)DokanFileInfo->Context;
int e, r, opened;
if(debug)
fprintf(stderr, "writefile\n");
maybeOpen(FileName, Owrite, &opened, &fid);
if(!fid)
return cvtError();
e = 0;
r = npc_write(fid, (u8*)Buffer, NumberOfBytesToWrite, Offset);
if(r < 0)
e = cvtError();
maybeClose(&opened, &fid);
if(e) {
if(debug)
fprintf(stderr, "writefile error\n");
return e;
}
*NumberOfBytesWritten = r;
return 0;
}
static int
_FlushFileBuffers(
LPCWSTR FileName,
PDOKAN_FILE_INFO DokanFileInfo)
{
Npwstat st;
char *fn;
int e, r;
if(debug)
fprintf(stderr, "flushfilebuffers '%ws'\n", FileName);
fn = p9path(FileName);
npc_emptystat(&st);
e = 0;
r = npc_wstat(fs, fn, &st);
if(r < 0)
e = cvtError();
free(fn);
if(e) {
if(debug)
fprintf(stderr, "flushfilebuffers error\n");
return e;
}
return 0;
}
static int
_GetFileInformation(
LPCWSTR FileName,
LPBY_HANDLE_FILE_INFORMATION fi,
PDOKAN_FILE_INFO DokanFileInfo)
{
Npwstat *st = NULL;
char *fn;
int e;
if(debug)
fprintf(stderr, "getfileinfo '%ws'\n", FileName);
fn = p9path(FileName);
if(!fn)
return -(int)ERROR_NOT_ENOUGH_MEMORY;
e = 0;
st = npc_stat(fs, fn);
if(st) {
toFileInfo(st, fi);
free(st);
} else {
e = cvtError();
}
free(fn);
if(e) {
if(debug)
fprintf(stderr, "getfileinfo error\n");
return e;
}
return 0;
}
static int
_FindFiles(
LPCWSTR FileName,
PFillFindData FillFindData, // function pointer
PDOKAN_FILE_INFO DokanFileInfo)
{
Npwstat *st;
WIN32_FIND_DATA findData;
Npcfid *fid = NULL;
char *fn;
int cnt, i, e;
if(debug)
fprintf(stderr, "findfiles '%ws'\n", FileName);
fn = p9path(FileName);
if(!fn)
return -(int)ERROR_NOT_ENOUGH_MEMORY;
e = 0;
fid = npc_open(fs, fn, Oread);
if(!fid)
e = cvtError();
while(fid) {
cnt = npc_dirread(fid, &st);
if(cnt == 0)
break;
if(cnt < 0) {
e = cvtError();
break;
}
for(i = 0; i < cnt; i++) {
if(!st[i].name[0])
continue;
e = toFindData(&st[i], &findData);
if(e) {
if(debug)
fprintf(stderr, "findfiles error converting '%s'... eliding.\n", st[i].name);
continue;
}
FillFindData(&findData, DokanFileInfo);
}
free(st);
}
if(fid)
npc_close(fid);
free(fn);
if(e) {
if(debug)
fprintf(stderr, "findfiles failed\n");
return e;
}
return 0;
}
static int
_DeleteFile(
LPCWSTR FileName,
PDOKAN_FILE_INFO DokanFileInfo)
{
char *fn;
int r;
if(debug)
fprintf(stderr, "deletefile %ws\n", FileName);
fn = p9path(FileName);
if(!fn)
return -(int)ERROR_NOT_ENOUGH_MEMORY;
r = npc_remove(fs, fn);
free(fn);
if(r < 0) {
if(debug)
fprintf(stderr, "deletefile %ws failed\n", FileName);
return cvtError();
}
return 0;
}
static int
_DeleteDirectory(
LPCWSTR FileName,
PDOKAN_FILE_INFO DokanFileInfo)
{
return _DeleteFile(FileName, DokanFileInfo);
}
static int
_MoveFile(
LPCWSTR FileName, // existing file name
LPCWSTR NewFileName,
BOOL ReplaceIfExisting,
PDOKAN_FILE_INFO DokanFileInfo)
{
Npwstat st;
char *fn, *fn2, *p, *newname;
int dirlen, r, e;
e = 0;
if(debug)
fprintf(stderr, "move %ws to %ws\n", FileName, NewFileName);
fn = p9path(FileName);
fn2 = p9path(NewFileName);
if(!fn || !fn2) {
e = -(int)ERROR_NOT_ENOUGH_MEMORY;
goto err;
}
p = strrchr(fn, '/');
if(p) {
dirlen = p - fn;
newname = fn2 + dirlen + 1;
} else {
dirlen = 0;
newname = fn2;
}
// same directory?
if(strncmp(fn, fn2, dirlen) != 0 || strchr(newname, '/') != NULL) {
// XXX better error? cant move files between directories...
e = -(int)ERROR_NOT_SAME_DEVICE;
goto err;
}
npc_emptystat(&st);
st.name = newname;
r = npc_wstat(fs, fn, &st);
if(r < 0) {
e = cvtError();
goto err;
}
/* fall through */
err:
if(fn)
free(fn);
if(fn2)
free(fn2);
if(e) {
if(debug)
fprintf(stderr, "move failed\n");
return e;
}
return 0;
}
static int
_LockFile(
LPCWSTR FileName,
LONGLONG ByteOffset,
LONGLONG Length,
PDOKAN_FILE_INFO DokanFileInfo)
{
// always fail; we dont do locking.
return -(int)ERROR_NOT_SUPPORTED;
}
static int
_SetEndOfFile(
LPCWSTR FileName,
LONGLONG ByteOffset,
PDOKAN_FILE_INFO DokanFileInfo)
{
Npwstat st;
char *fn;
int r;
fn = p9path(FileName);
if(!fn)
return -(int)ERROR_NOT_ENOUGH_MEMORY;
npc_emptystat(&st);
st.length = ByteOffset;
r = npc_wstat(fs, fn, &st);
free(fn);
if(r < 0)
return cvtError();
return 0;
}
static int
_SetAllocationSize(
LPCWSTR FileName,
LONGLONG AllocSize,
PDOKAN_FILE_INFO DokanFileInfo)
{
// XXX what exactly is this?
return notyet("SetAllocationSize");
}
static int
_SetFileAttributes(
LPCWSTR FileName,
DWORD FileAttributes,
PDOKAN_FILE_INFO DokanFileInfo)
{
if(debug)
fprintf(stderr, "setfileattributes '%ws' %x\n", FileName, FileAttributes);
FileAttributes &= ~FILE_ATTRIBUTE_NORMAL;
if(FileAttributes) {
if(debug)
fprintf(stderr, "setfileattributes error (unsupported bits)\n");
return -(int)ERROR_NOT_SUPPORTED;
}
return 0;
}
static int
_SetFileTime(
LPCWSTR FileName,
CONST FILETIME* CreationTime,
CONST FILETIME* LastAccessTime,
CONST FILETIME* LastWriteTime,
PDOKAN_FILE_INFO DokanFileInfo)
{
Npwstat st;
char *fn;
int r;
if(!LastAccessTime && !LastWriteTime)
return 0;
fn = p9path(FileName);
if(!fn)
return -(int)ERROR_NOT_ENOUGH_MEMORY;
npc_emptystat(&st);
if(LastAccessTime)
st.atime = fromFT(LastAccessTime);
if(LastWriteTime)
st.mtime = fromFT(LastWriteTime);
r = npc_wstat(fs, fn, &st);
free(fn);
if(r < 0)
return cvtError();
return 0;
}
static int
_UnlockFile(
LPCWSTR FileName,
LONGLONG ByteOffset,
LONGLONG Length,
PDOKAN_FILE_INFO DokanFileInfo)
{
// always fail; we dont do locking.
return -(int)ERROR_NOT_SUPPORTED;
}
static int
_Unmount(
PDOKAN_FILE_INFO DokanFileInfo)
{
if(debug)
fprintf(stderr, "unmount\n");
npc_umount(fs);
fs = NULL;
return 0;
}
static void
usage(char *prog)
{
fprintf(stderr, "usage: %s [-cdDtU] [-a authserv] [-p passwd] [-u user] addr driveletter\n", prog);
fprintf(stderr, "\taddr and authserv must be of the form tcp!hostname!port\n");
fprintf(stderr, "\t-c\tchatty npfs messages\n");
fprintf(stderr, "\t-d\tninefs debug messages\n");
fprintf(stderr, "\t-D\tDokan debug mesages\n");
fprintf(stderr, "\t-t\tdo not perform path character translations\n");
fprintf(stderr, "\t-U\tdisable 9p2000.u support\n");
_exit(1);
}
int __cdecl
main(int argc, char **argv)
{
extern int npc_chatty;
DOKAN_OPERATIONS ops;
DOKAN_OPTIONS opt;
WSADATA wsData;
char *serv, *authserv, *passwd, *uname, *prog;
int x, ch, dotu;
char letter;
WSAStartup(MAKEWORD(2,2), &wsData);
memset(&opt, 0, sizeof opt);
uname = "nobody";
prog = argv[0];
dotu = 1;
authserv = NULL;
passwd = NULL;
while((ch = getopt(argc, argv, "a:cdDp:tu:U")) != -1) {
switch(ch) {
case 'a':
authserv = optarg;
break;
case 'c':
npc_chatty = 1;
break;
case 'd':
debug = 1;
break;
case 'D':
opt.Options |= DOKAN_OPTION_DEBUG | DOKAN_OPTION_STDERR;
break;
case 'p':
passwd = optarg;
break;
case 't':
transPath = 0;
break;
case 'u':
uname = optarg;
break;
case 'U':
dotu = 0;
break;
default:
usage(prog);
}
}
argc -= optind;
argv += optind;
if(argc != 2 || !argv[1][0])
usage(prog);
serv = argv[0];
letter = argv[1][0];
user = np_default_users->uname2user(np_default_users, uname);
if(passwd) {
struct npcauth auth;
if(!authserv)
authserv = serv;
memset(&auth, 0, sizeof auth);
makeKey(passwd, auth.key);
auth.srv = npc_netaddr(authserv, 567);
fs = npc_netmount(npc_netaddr(serv, 564), dotu, user, 564, authp9any, &auth);
} else {
fs = npc_netmount(npc_netaddr(serv, 564), dotu, user, 564, NULL, NULL);
}
if(!fs) {
char *emsg;
int eno;
np_rerror(&emsg, &eno);
fprintf(stderr, "failed to mount %s: (%d) %s\n", serv, eno, emsg);
return 1;
}
opt.ThreadCount = 0;
opt.DriveLetter = letter;
//opt.Options |= DOKAN_OPTION_KEEP_ALIVE;
memset(&ops, 0, sizeof ops);
ops.CreateFile = _CreateFile;
ops.OpenDirectory = _OpenDirectory;
ops.CreateDirectory = _CreateDirectory;
ops.Cleanup = _Cleanup;
ops.CloseFile = _CloseFile;
ops.ReadFile = _ReadFile;
ops.WriteFile = _WriteFile;
ops.FlushFileBuffers = _FlushFileBuffers;
ops.GetFileInformation = _GetFileInformation;
ops.FindFiles = _FindFiles;
ops.FindFilesWithPattern = NULL;
ops.SetFileAttributes = _SetFileAttributes;
ops.SetFileTime = _SetFileTime;
ops.DeleteFile = _DeleteFile;
ops.DeleteDirectory = _DeleteDirectory;
ops.MoveFile = _MoveFile;
ops.SetEndOfFile = _SetEndOfFile;
ops.SetAllocationSize = _SetAllocationSize;
ops.LockFile = _LockFile;
ops.UnlockFile = _UnlockFile;
ops.GetDiskFreeSpace = NULL;
ops.GetVolumeInformation = NULL;
ops.Unmount = _Unmount;
x = DokanMain(&opt, &ops);
if(x)
fprintf(stderr, "error: %x\n", x);
return 0;
}