forked from infinit/elle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filesystem_dokanx.cc
1040 lines (993 loc) · 36.8 KB
/
filesystem_dokanx.cc
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
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define _FILE_OFFSET_BITS 64
#include <locale>
#include <thread>
#include <fcntl.h>
#include <sys/stat.h>
#undef stat
#include <elle/reactor/filesystem.hh>
#include <elle/reactor/scheduler.hh>
#include <elle/reactor/Barrier.hh>
#include <elle/windows/string_conversion.hh>
#include <elle/os/environ.hh>
#include <elle/finally.hh>
#include <ntstatus.h>
using NTSTATUS = DWORD;
#include <dokan/dokan.h>
#include <strsafe.h>
#undef wcscpy
ELLE_LOG_COMPONENT("elle.reactor.filesystem.dokanx");
namespace elle
{
namespace reactor
{
namespace filesystem
{
namespace
{
Scheduler* le_scheduler;
// Set to true if callbacks are called in the reactor thread.
bool dokan_sync = false;
NTSTATUS to_ntstatus(int erc)
{
switch(erc)
{
case ENOENT:
return STATUS_OBJECT_NAME_NOT_FOUND;
case EPERM:
case EACCES:
return STATUS_ACCESS_DENIED;
case ENOSYS:
return STATUS_NOT_IMPLEMENTED;
case EEXIST:
return STATUS_OBJECT_NAME_EXISTS;
case EISDIR:
return STATUS_FILE_IS_A_DIRECTORY;
case ENOTDIR:
return STATUS_NOT_A_DIRECTORY;
case ENODATA:
return STATUS_OBJECT_NAME_NOT_FOUND;
case EINVAL:
return STATUS_INVALID_PARAMETER;
case ENOSPC:
return STATUS_ALLOTTED_SPACE_EXCEEDED;
default:
return STATUS_NOT_IMPLEMENTED;
}
}
std::wstring from_utf8(std::string const& source)
{
using facet_type = std::codecvt<wchar_t,char,std::mbstate_t>;
std::locale mylocale;
const facet_type& myfacet = std::use_facet<facet_type>(mylocale);
// prepare objects to be filled by codecvt::in :
wchar_t pwstr[source.length()+1]; // the destination buffer (might be too short)
std::mbstate_t mystate = std::mbstate_t(); // the shift state object
const char* pc; // from_next
wchar_t* pwc; // to_next
// translate characters:
facet_type::result myresult = myfacet.in (mystate,
source.data(), source.data()+source.length(), pc,
pwstr, pwstr+source.length()+1, pwc);
if (myresult != facet_type::ok)
throw Error(0, "transcoding failed");
return std::wstring(pwstr);
}
std::string to_utf8(LPCWSTR pszTextUTF16)
{
//
// Special case of NULL or empty input string
//
if ( (pszTextUTF16 == NULL) || (*pszTextUTF16 == 0) )
{
// Return empty string
return "";
}
//
// Consider WCHAR’s count corresponding to total input string length,
// including end-of-string (L) character.
//
size_t cchUTF16Max = 10000 - 1;
size_t cchUTF16;
if (FAILED(::StringCchLengthW( pszTextUTF16, cchUTF16Max, &cchUTF16)))
ELLE_DEBUG("StringCchLengthW failed");
// Consider also terminating
++cchUTF16;
//
// WC_ERR_INVALID_CHARS flag is set to fail if invalid input character
// is encountered.
// This flag is supported on Windows Vista and later.
// Don’t use it on Windows XP and previous.
//
#if (WINVER >= 0x0600)
DWORD dwConversionFlags = WC_ERR_INVALID_CHARS;
#else
DWORD dwConversionFlags = 0;
#endif
//
// Get size of destination UTF-8 buffer, in CHAR’s (= bytes)
//
int cbUTF8 = ::WideCharToMultiByte(
CP_UTF8, // convert to UTF-8
dwConversionFlags, // specify conversion behavior
pszTextUTF16, // source UTF-16 string
static_cast<int>( cchUTF16 ), // total source string length, in WCHAR’s,
// including end-of-string
NULL, // unused – no conversion required in this step
0, // request buffer size
NULL, NULL // unused
);
//
// Allocate destination buffer for UTF-8 string
//
int cchUTF8 = cbUTF8; // sizeof(CHAR) = 1 byte
std::string strUTF8(cchUTF8, '\0');
char * pszUTF8 = (char*)strUTF8.data();
//
// Do the conversion from UTF-16 to UTF-8
//
if (::WideCharToMultiByte(
CP_UTF8, // convert to UTF-8
dwConversionFlags, // specify conversion behavior
pszTextUTF16, // source UTF-16 string
static_cast<int>( cchUTF16 ), // total source string length, in WCHAR’s,
// including end-of-string
pszUTF8, // destination buffer
cbUTF8, // destination buffer size, in bytes
NULL, NULL // unused
))
ELLE_LOG("WideCharToMultiByte failed: %s", GetLastError())
// Return resulting UTF-8 string
strUTF8 = strUTF8.substr(0, strUTF8.size()-1);
return strUTF8;
}
__attribute__((__stdcall__))
NTSTATUS CreateFile(
LPCWSTR fileName,
PDOKAN_IO_SECURITY_CONTEXT sctx,
DWORD desiredAccess,
DWORD fileAttributes,
DWORD shareMode,
DWORD creationDisposition,
DWORD createOptions,
PDOKAN_FILE_INFO context)
{
ELLE_TRACE("createfile");
context->Context = 0;
auto work = [&]() -> NTSTATUS {
try
{
FileSystem* fs = (FileSystem*)context->DokanOptions->GlobalContext;
std::string path = to_utf8(fileName);
std::shared_ptr<Path> p;
try
{
p = fs->path(path);
}
catch (Error const& e)
{
if (e.error_code() == ENOENT)
return STATUS_OBJECT_NAME_NOT_FOUND;
else if (e.error_code() == EPERM)
return STATUS_ACCESS_DENIED;
else
{
ELLE_TRACE("Unhandled specific code %s", e.error_code());
return STATUS_OBJECT_NAME_NOT_FOUND;
}
}
mode_t mode = 0;
if ((desiredAccess & GENERIC_READ) && (desiredAccess & GENERIC_WRITE))
mode = O_RDWR;
else if (desiredAccess & GENERIC_READ)
mode = O_RDONLY;
else
mode = O_WRONLY;
if (creationDisposition == FILE_SUPERSEDE
|| creationDisposition == FILE_OVERWRITE_IF)
mode = O_CREAT | O_TRUNC;
if (creationDisposition == FILE_CREATE)
mode = O_CREAT | O_EXCL;
if (creationDisposition == FILE_OPEN_IF)
mode = O_CREAT;
if (creationDisposition == FILE_OPEN)
mode = 0;
if (creationDisposition == FILE_OVERWRITE)
mode = O_TRUNC;
ELLE_TRACE("createFile %s, cd=%s, trunc=%s, creat=%s",
path, creationDisposition,
!!(mode & O_TRUNC), !!(mode & O_CREAT));
int access = 0644;
bool exists = false;
struct stat st;
memset(&st, 0, sizeof(struct stat));
try
{
p->stat(&st);
exists = true;
}
catch (Error const& e)
{
ELLE_DEBUG("stat failed: %s", e);
}
ELLE_DEBUG("stat, mode=%s", st.st_mode);
if (exists && !(st.st_mode & 0777))
return STATUS_ACCESS_DENIED;
if (!exists && !(mode & O_CREAT))
{
ELLE_DEBUG("entry does not exists");
context->Context = 0;
return 0xC0000034; // STATUS_OBJECT_NAME_NOT_FOUND
//return 0xC000000F; // STATUS_NO_SUCH_FILE
}
if (exists && (mode & O_EXCL))
{
ELLE_DEBUG("entry already exists");
context->Context = 0;
return 0xC0000035; //STATUS_OBJECT_NAME_COLLISION
// or is it return 0x40000000; //STATUS_OBJECT_NAME_EXISTS
}
if (exists && S_ISDIR(st.st_mode))
{
ELLE_DEBUG("directory, nothing to do");
context->IsDirectory = TRUE;
context->Context = 0;
return 0;
}
if (exists && (mode & O_CREAT))
{
SetLastError(ERROR_ALREADY_EXISTS);
}
if (createOptions & FILE_DIRECTORY_FILE)
{
try
{
struct stat st;
p->stat(&st);
}
catch (Error const& e)
{
p->mkdir(0755);
}
context->IsDirectory = TRUE;
context->Context = 0;
}
else
{
auto handle = p->create(mode, access);
context->Context = (decltype(context->Context))handle.release();
}
}
catch (Error const& e)
{
ELLE_TRACE("Filesystem error creating: %s", e);
return to_ntstatus(e.error_code());
}
return 0;
};
if (dokan_sync)
return work();
else
return le_scheduler->mt_run<NTSTATUS>("createfile", work);
}
__attribute__((__stdcall__))
void Cleanup(LPCWSTR fileName, PDOKAN_FILE_INFO context)
{
ELLE_TRACE("cleanup");
auto work = [&] {
try
{
FileSystem* fs = (FileSystem*)context->DokanOptions->GlobalContext;
std::string path = to_utf8(fileName);
ELLE_TRACE("cleanup %s, doc=%s", path, !!context->DeleteOnClose);
if (context->DeleteOnClose)
{
auto p = fs->path(path);
if (context->IsDirectory)
p->rmdir();
else
p->unlink();
}
Handle* handle = (Handle*)context->Context;
if (handle)
{
ELLE_DEBUG("Closing handle %s", (void*)handle);
handle->close();
ELLE_DEBUG("Deleting handle %s", (void*)handle);
delete handle;
context->Context = 0;
ELLE_DEBUG("Done");
}
}
catch (Error const& e)
{
ELLE_WARN("Filesystem error cleaning up: %s", e);
}
};
if (dokan_sync)
work();
else
le_scheduler->mt_run<void>("cleanup", work);
}
__attribute__((__stdcall__))
void CloseFile(LPCWSTR fileName, PDOKAN_FILE_INFO context)
{
ELLE_TRACE("closefile");
auto work = [&] {
std::string path = to_utf8(fileName);
Handle* handle = (Handle*)context->Context;
ELLE_DEBUG("closefile %s, h=%x", path, handle);
if (handle)
{
handle->close();
delete handle;
context->Context = 0;
}};
if (dokan_sync)
work();
else
le_scheduler->mt_run<void>("closefile", work);
}
__attribute__((__stdcall__))
NTSTATUS ReadFile(LPCWSTR fileName,
void* Buffer,
DWORD NumberOfBytesToRead,
LPDWORD NumberOfBytesRead,
LONGLONG Offset,
PDOKAN_FILE_INFO context)
{
auto work = [&]()->NTSTATUS {
try
{
bool temp_handle = false;
//FileSystem* fs = (FileSystem*)context->DokanOptions->GlobalContext;
//std::string path = to_utf8(fileName);
//Path& p = fs->path(path);
std::string path = to_utf8(fileName);
ELLE_TRACE("readfile %s : %s bytes from %s",
fileName, NumberOfBytesToRead, Offset);
if (Offset < 0)
Offset = 0;
Handle* handle = (Handle*)context->Context;
if (!handle)
{
ELLE_WARN("readfile on %s, no handle", path);
// Possibly a dokan bug, but this happens when previewing images
// under the windows explorer. Traces show a cleanup just before
// the read
bool prev_sync = dokan_sync;
dokan_sync = true;
elle::SafeFinally reset([&] { dokan_sync = prev_sync;});
CreateFile(fileName, NULL, GENERIC_READ, 0, 0, FILE_OPEN, 0, context);
handle = (Handle*)context->Context;
if (!handle)
{
ELLE_WARN("CreateFile workaround failed");
return 0xC0000185; // STATUS_IO_DEVICE_ERROR
}
temp_handle = true;
}
DWORD res = 0;
if (NumberOfBytesToRead > 512 * 1024)
{
// split it
unsigned char* buf = (unsigned char*)Buffer;
while (true)
{
auto to_read = std::min(NumberOfBytesToRead, (DWORD)(512 * 1024));
int r = handle->read(elle::WeakBuffer(buf + res, to_read),
to_read, Offset + res);
res += r;
NumberOfBytesToRead -= r;
if ((unsigned)r < to_read || !NumberOfBytesToRead)
break;
}
}
else
res = handle->read(elle::WeakBuffer(Buffer, NumberOfBytesToRead),
NumberOfBytesToRead, Offset);
if (NumberOfBytesRead)
*NumberOfBytesRead = res;
if (temp_handle)
{
handle->close();
delete handle;
context->Context = 0;
}
return 0;
}
catch (Error const& e)
{
ELLE_TRACE("Filesystem error reading: %s", e);
return to_ntstatus(e.error_code());
}};
if (dokan_sync)
return work();
else
return le_scheduler->mt_run<NTSTATUS>("readfile", work);
}
__attribute__((__stdcall__))
NTSTATUS WriteFile(LPCWSTR fileName,
const void* Buffer,
DWORD NumberOfBytesToRead,
LPDWORD NumberOfBytesRead,
LONGLONG Offset,
PDOKAN_FILE_INFO context)
{
auto work = [&]() -> NTSTATUS{
try
{
FileSystem* fs = (FileSystem*)context->DokanOptions->GlobalContext;
std::string path = to_utf8(fileName);
ELLE_TRACE("writefile %s : %s bytes from %s",
fileName, NumberOfBytesToRead, Offset);
if (Offset == -1)
{
auto p = fs->path(path);
struct stat st;
p->stat(&st);
Offset = st.st_size;
}
//Path& p = fs->path(path);
Handle* handle = (Handle*)context->Context;
int res = handle->write(elle::WeakBuffer((void*)Buffer, NumberOfBytesToRead),
NumberOfBytesToRead, Offset);
if (NumberOfBytesRead)
*NumberOfBytesRead = res;
return 0;
}
catch (Error const& e)
{
ELLE_TRACE("Filesystem error writing %s", e);
return to_ntstatus(e.error_code());
}};
if (dokan_sync)
return work();
else
return le_scheduler->mt_run<NTSTATUS>("writefile", work);
}
FILETIME to_filetime(time_t t)
{ // time_t time as the number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).
// filetime: Contains a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC)
// seriously? FIXME: compute the offset once and for all
uint64_t fnow = (uint64_t)t * 10000000ULL;
fnow += 116444736000000000ULL;
return FILETIME{fnow & 0xFFFFFFFF, fnow >> 32};
}
uint64_t from_filetime(FILETIME const& ft)
{
uint64_t fnow = ft.dwLowDateTime + ((uint64_t)ft.dwHighDateTime << 32);
fnow -= 116444736000000000ULL;
fnow /= 10000000ULL;
return fnow;
}
__attribute__((__stdcall__))
NTSTATUS GetFileInformation(LPCWSTR fileName,
LPBY_HANDLE_FILE_INFORMATION buffer,
PDOKAN_FILE_INFO context)
{
auto work = [&]() -> NTSTATUS {
try
{
FileSystem* fs = (FileSystem*)context->DokanOptions->GlobalContext;
std::string path = to_utf8(fileName);
auto p = fs->path(path);
struct stat st;
p->stat(&st);
if (st.st_mode & 0040000)
buffer->dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
else
buffer->dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
ELLE_TRACE("stat(%s): size %s, mtime %s", sizeof(st), st.st_size, st.st_mtime);
buffer->ftCreationTime = to_filetime(st.st_ctime);
buffer->ftLastWriteTime = to_filetime(st.st_mtime);
buffer->ftLastAccessTime = to_filetime(st.st_atime);
buffer->nFileSizeHigh = 0; // FIXME BROKEN st.st_size >> 32;
buffer->nFileSizeLow = st.st_size & 0xFFFFFFFF;
buffer->nNumberOfLinks = st.st_nlink;
return 0;
}
catch (Error const& e)
{
ELLE_TRACE("Filesystem error GetFileInformation: %s", e);
return to_ntstatus(e.error_code());
}
return 0;
};
if (dokan_sync)
return work();
else
return le_scheduler->mt_run<NTSTATUS>("getinfo", work);
}
__attribute__((__stdcall__))
NTSTATUS FindFiles(LPCWSTR fileName, PFillFindData cb,
PDOKAN_FILE_INFO context)
{
std::vector<WIN32_FIND_DATAW> res;
auto work = [&] () -> NTSTATUS
{
try
{
FileSystem* fs = (FileSystem*)context->DokanOptions->GlobalContext;
std::string parent_path = to_utf8(fileName);
auto p = fs->path(parent_path);
std::vector<std::string> hits;
p->list_directory([&](std::string const&path, struct stat* st) {
hits.push_back(path);
});
ELLE_DEBUG("got %s hits on %s", hits.size(), parent_path);
for (auto const& path: hits)
{
if (path == "." || path == "..")
continue; // we handle those ourselve
ELLE_DEBUG("scanning entry %s/%s (%s)", parent_path, path, sizeof(struct stat));
int g1 = 0xfefefefe;
struct stat lst;
int g2 = 0xefefefef;
// Always restat, list_directory stat info is bogus
fs->path((bfs::path(parent_path) / path).string())->stat(&lst);
ELLE_ASSERT_EQ(g1, (int)0xfefefefe);
ELLE_ASSERT_EQ(g2, (int)0xefefefef);
WIN32_FIND_DATAW fd;
memset(&fd, 0, sizeof(fd));
if (lst.st_mode & 0040000)
fd.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
else
fd.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
fd.ftCreationTime = to_filetime(lst.st_ctime);
fd.ftLastWriteTime = to_filetime(lst.st_mtime);
fd.ftLastAccessTime = to_filetime(lst.st_atime);
// FIXME BROKEN fd.nFileSizeHigh = lst.st_size >> 32;
fd.nFileSizeLow = lst.st_size & 0xFFFFFFFF;
std::string src = bfs::path(path).filename().string();
ELLE_TRACE("converting %s", src);
::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
src.data(), src.size(), fd.cFileName, MAX_PATH);
res.push_back(fd);
}
// . and ..
struct stat st;
p->stat(&st);
WIN32_FIND_DATAW fd;
memset(&fd, 0, sizeof(fd));
fd.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
fd.ftCreationTime = to_filetime(st.st_ctime);
fd.ftLastWriteTime = to_filetime(st.st_mtime);
fd.ftLastAccessTime = to_filetime(st.st_atime);
memcpy(fd.cFileName, ".", 1);
res.push_back(fd);
memcpy(fd.cFileName, ".\0.\0", 4);
res.push_back(fd);
}
catch (Error const& e)
{
ELLE_TRACE("Filesystem error FindFileing: %s", e);
return to_ntstatus(e.error_code());
}
return 0;
};
NTSTATUS st;
if (dokan_sync)
st = work();
else
st = le_scheduler->mt_run<NTSTATUS>("findfile", work);
if (st)
return st;
for (auto& r: res)
cb(&r, context);
return 0;
}
__attribute__((__stdcall__))
NTSTATUS DeleteFile(LPCWSTR fileName, PDOKAN_FILE_INFO context)
{ // see doc, action happens elsewhere
return 0;
}
__attribute__((__stdcall__))
NTSTATUS DeleteDirectory(LPCWSTR fileName, PDOKAN_FILE_INFO context)
{ // see doc, action happens elsewhere
ELLE_TRACE("DeleteDirectory");
auto work = [&]() -> NTSTATUS {
try
{
FileSystem* fs = (FileSystem*)context->DokanOptions->GlobalContext;
std::string path = to_utf8(fileName);
auto p = fs->path(path);
int count = 0;
p->list_directory([&](std::string const&, struct stat*)
{
++count;
});
if (count)
return 0xC0000101; // STATUS_DIRECTORY_NOT_EMPTY;
else
return 0;
}
catch (Error const& e)
{
ELLE_TRACE("Filesystem error deleting %s", e);
return to_ntstatus(e.error_code());
}
};
if (dokan_sync)
return work();
else
return le_scheduler->mt_run<NTSTATUS>("deletedirectory", work);
}
__attribute__((__stdcall__))
NTSTATUS FlushFileBuffers(LPCWSTR fileName, PDOKAN_FILE_INFO context)
{
return 0;
}
__attribute__((__stdcall__))
NTSTATUS SetFileAttributes(LPCWSTR fileName, DWORD attributes,
PDOKAN_FILE_INFO context)
{
return 0;
}
__attribute__((__stdcall__))
NTSTATUS SetFileTime(LPCWSTR fileName,
CONST FILETIME* creationTime,
CONST FILETIME* lastAccessTime,
CONST FILETIME* lastWriteTime,
PDOKAN_FILE_INFO context
)
{
ELLE_TRACE("SetFileTime");
auto work = [&]() -> NTSTATUS {
try
{
FileSystem* fs = (FileSystem*)context->DokanOptions->GlobalContext;
std::string path = to_utf8(fileName);
auto p = fs->path(path);
struct timespec tv[2] = {{0,0},{0,0}};
if (lastAccessTime)
tv[0].tv_sec = from_filetime(*lastAccessTime);
if (lastWriteTime)
tv[1].tv_sec = from_filetime(*lastWriteTime);
p->utimens(tv);
}
catch (Error const& e)
{
ELLE_TRACE("Filesystem error setfiletiming %s", e);
return to_ntstatus(e.error_code());
}
return 0;
};
if (dokan_sync)
return work();
else
return le_scheduler->mt_run<NTSTATUS>("setfiletime", work);
}
__attribute__((__stdcall__))
NTSTATUS MoveFile(LPCWSTR fileName, LPCWSTR wtarget,
BOOL replaceExisting,
PDOKAN_FILE_INFO context)
{
auto work = [&]()->NTSTATUS {
try
{
FileSystem* fs = (FileSystem*)context->DokanOptions->GlobalContext;
std::string path = to_utf8(fileName);
auto p = fs->path(path);
std::string target = to_utf8(wtarget);
if (!replaceExisting)
{
try
{
struct stat st;
fs->path(target)->stat(&st);
return STATUS_ACCESS_DENIED;
}
catch(...)
{}
}
p->rename(target);
}
catch (Error const& e)
{
ELLE_TRACE("Filesystem error moving : %s", e);
return to_ntstatus(e.error_code());
}
return 0;
};
if (dokan_sync)
return work();
else
return le_scheduler->mt_run<NTSTATUS>("movefile", work);
}
__attribute__((__stdcall__))
NTSTATUS SetEndOfFile(LPCWSTR fileName,
LONGLONG length,
PDOKAN_FILE_INFO context)
{
auto work = [&]()->NTSTATUS {
try
{
FileSystem* fs = (FileSystem*)context->DokanOptions->GlobalContext;
std::string path = to_utf8(fileName);
auto p = fs->path(path);
p->truncate(length);
}
catch (Error const& e)
{
ELLE_TRACE("Filesystem error truncating: %s", e);
return to_ntstatus(e.error_code());
}
return 0;
};
if (dokan_sync)
return work();
else
return le_scheduler->mt_run<NTSTATUS>("truncate", work);
}
__attribute__((__stdcall__))
NTSTATUS SetAllocationSize(LPCWSTR fileName,
LONGLONG length,
PDOKAN_FILE_INFO context)
{
auto work = [&]()->NTSTATUS {
try
{
FileSystem* fs = (FileSystem*)context->DokanOptions->GlobalContext;
std::string path = to_utf8(fileName);
auto p = fs->path(path);
struct stat st;
p->stat(&st);
if (st.st_size > length)
p->truncate(length);
}
catch (Error const& e)
{
ELLE_TRACE("Filesystem error setting alloc size: %s", e);
return to_ntstatus(e.error_code());
}
return 0;
};
if (dokan_sync)
return work();
else
return le_scheduler->mt_run<NTSTATUS>("allocsize", work);
}
__attribute__((__stdcall__))
NTSTATUS LockFile( LPCWSTR, // FileName
LONGLONG, // ByteOffset
LONGLONG, // Length
PDOKAN_FILE_INFO)
{
return STATUS_ACCESS_DENIED;
}
__attribute__((__stdcall__))
NTSTATUS UnlockFile( LPCWSTR, // FileName
LONGLONG, // ByteOffset
LONGLONG, // Length
PDOKAN_FILE_INFO)
{
return STATUS_ACCESS_DENIED;
}
__attribute__((__stdcall__))
NTSTATUS GetDiskFreeSpace(PULONGLONG freeBytes,
PULONGLONG totalBytes,
PULONGLONG totalFreeBytes,
PDOKAN_FILE_INFO context)
{
auto work = [&]() -> NTSTATUS {
try
{
FileSystem* fs = (FileSystem*)context->DokanOptions->GlobalContext;
std::string path = "/";
auto p = fs->path(path);
struct statvfs s;
p->statfs(&s);
if (totalBytes)
*totalBytes = (ULONGLONG)s.f_frsize * s.f_blocks;
if (freeBytes)
*freeBytes = (ULONGLONG)s.f_frsize * s.f_bfree;
if (totalFreeBytes)
*totalFreeBytes = *freeBytes; //FIXME what is this?
}
catch (Error const& e)
{
ELLE_TRACE("Filesystem error getting free space: %s", e);
return to_ntstatus(e.error_code());
}
return 0;
};
if (dokan_sync)
return work();
else
return le_scheduler->mt_run<NTSTATUS>("freespace", work);
}
__attribute__((__stdcall__))
NTSTATUS GetVolumeInformation(LPWSTR VolumeNameBuffer,
DWORD VolumeNameSize,
LPDWORD VolumeSerialNumber,
LPDWORD MaximumComponentLength,
LPDWORD FileSystemFlags,
LPWSTR FileSystemNameBuffer,
DWORD FileSystemNameSize,
PDOKAN_FILE_INFO context)
{
FileSystem* fs = (FileSystem*)context->DokanOptions->GlobalContext;
if (VolumeNameBuffer)
{
wcscpy(VolumeNameBuffer, L"infinit");
for (auto o: fs->mount_options())
{
if (o.find("volname=") == 0)
{
auto volname = o.substr(strlen("volname="));
auto volnamew = from_utf8(volname);
wcscpy(VolumeNameBuffer, volnamew.data());
}
}
}
if (VolumeSerialNumber)
*VolumeSerialNumber = 0x00000001;
if (FileSystemNameBuffer)
wcscpy(FileSystemNameBuffer, L"dokan");
if (MaximumComponentLength)
*MaximumComponentLength = 256;
if (FileSystemFlags)
*FileSystemFlags = FILE_CASE_SENSITIVE_SEARCH |
FILE_CASE_PRESERVED_NAMES |
FILE_SUPPORTS_HARD_LINKS |
FILE_UNICODE_ON_DISK;
return 0;
}
__attribute__((__stdcall__))
NTSTATUS Unmount(PDOKAN_FILE_INFO context)
{
ELLE_LOG("Filesystem umount notification received.");
return STATUS_SUCCESS;
}
}
class FileSystemImpl
{
public:
std::unique_ptr<std::thread> _thread;
std::unique_ptr<reactor::Thread> _rThread;
reactor::Barrier _barrier;
DOKAN_OPERATIONS* dokanOperations;
DOKAN_OPTIONS* dokanOptions;
};
namespace
{
void dokan_run(void* arg)
{
auto fs = (FileSystem*)arg;
int status = DokanMain(fs->impl()->dokanOptions, fs->impl()->dokanOperations);
fs->impl()->_barrier.open();
switch(status)
{
case DOKAN_SUCCESS:
fprintf(stderr, "Success");
break;
case DOKAN_ERROR:
fprintf(stderr, "Error");
break;
case DOKAN_DRIVE_LETTER_ERROR:
fprintf(stderr, "Bad Drive letter");
break;
case DOKAN_DRIVER_INSTALL_ERROR:
fprintf(stderr, "Can't install driver");
break;
case DOKAN_START_ERROR:
fprintf(stderr, "Driver something wrong");
break;
case DOKAN_MOUNT_ERROR:
fprintf(stderr, "Can't assign a drive letter");
break;
case DOKAN_MOUNT_POINT_ERROR:
fprintf(stderr, "Mount point error");
break;
default:
fprintf(stderr, "Unknown error: %d", status);
break;
}
}
}
FileSystem::FileSystem(std::unique_ptr<Operations> op, bool full_tree)
: _impl(new FileSystemImpl())
, _operations(std::move(op))
, _full_tree(full_tree)
{
this->_operations->filesystem(this);
}
void FileSystem::unmount()
{
// Without this flush, we get a strange trailing ",'" on stdout when
// closing stdin of `infinit-volume --run --script` which breaks the
// JSON output (tests/functional/passport-restrictions notably). This
// might be wine only.
std::cout.flush();
std::wstring w = from_utf8(this->_where);
DokanUnmount(w[0]);
}
void
FileSystem::kill()
{
// FIXME
ELLE_ERR("terminate not yet implemented for Dokan");
}
void FileSystem::mount(bfs::path const& where,
std::vector<std::string> const& options)
{
this->_mount_options = options;
le_scheduler = &scheduler();
DOKAN_OPTIONS* dokanOptions = new DOKAN_OPTIONS;
memset(dokanOptions, 0, sizeof(DOKAN_OPTIONS));
dokanOptions->Version = DOKAN_VERSION;
dokanOptions->ThreadCount = 1; // use default
dokanOptions->Timeout = 300000; // irp timeout in ms
auto& logger = elle::log::logger();
if (logger.component_is_active("reactor.filesystem.dokanx",
elle::log::Logger::Level::debug))
{
dokanOptions->Options |= DOKAN_OPTION_DEBUG;
dokanOptions->Options |= DOKAN_OPTION_STDERR;
}
std::string mode = elle::os::getenv("DOKAN_MOUNT_MODE", "");
if (mode.empty())
dokanOptions->Options |= DOKAN_OPTION_REMOVABLE;
else
{
if (mode.find("removable")!=mode.npos)
dokanOptions->Options |= DOKAN_OPTION_REMOVABLE;
if (mode.find("network")!=mode.npos)
dokanOptions->Options |= DOKAN_OPTION_NETWORK;
if (mode.find("manager")!=mode.npos)
dokanOptions->Options |= DOKAN_OPTION_MOUNT_MANAGER;
if (mode.find("alt")!=mode.npos)
dokanOptions->Options |= DOKAN_OPTION_ALT_STREAM;
if (mode.find("session")!=mode.npos)
dokanOptions->Options |= DOKAN_OPTION_CURRENT_SESSION;
}
for (auto o: options)
if (o == "ro")
dokanOptions->Options |= DOKAN_OPTION_WRITE_PROTECT;
dokanOptions->GlobalContext = (decltype(dokanOptions->GlobalContext))this;
std::wstring* mountPoint = new std::wstring(from_utf8(where.string()));
dokanOptions->MountPoint = mountPoint->c_str();
DOKAN_OPERATIONS* dokanOperations = new DOKAN_OPERATIONS;
memset(dokanOperations , 0, sizeof(DOKAN_OPERATIONS));
dokanOperations->ZwCreateFile = CreateFile;
dokanOperations->Cleanup = Cleanup;
dokanOperations->CloseFile = CloseFile;