Menu

[r157]: / jpfm_native / jpfmMount.cpp  Maximize  Restore  History

Download this file

1143 lines (1031 with data), 39.5 kB

   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
/*
* Copyright 2009-2010 Shashank Tulsyan
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* File: jpfmMount.cpp
* Author: Shashank Tulsyan
*/
#include <string.h>
#include "jpfm.h"
#include "JPfmConcurrent.h"
#include "Opmethodidinitializer.h"
//#include "include/traces.h"
//const int fileErrorNotImplemented = ERROR_CALL_NOT_IMPLEMENTED;
#ifndef WINAPI
#define WINAPI
#endif
#ifdef _WIN32
////////////////WIN//////////////////
const int fileErrorNotImplemented = ERROR_CALL_NOT_IMPLEMENTED;
DWORD WINAPI mountingThread(LPVOID lpParam);
DWORD WINAPI mountingThreadForConcurrentFS(LPVOID lpParam);
#else
//#ifdef __APPLE__
////////////////MAC//////////////////
//#else
////////////////LINUX//////////////////
const int fileErrorNotImplemented = ENOSYS;
void * mountingThread(void* lpParam);
void * mountingThreadForConcurrentFS(void* lpParam);
//#endif
#endif
#define USE_FAST_PIPE
PfmApi * pfmapi;
JavaVM * jvm;
int jvmInitialized;
int initiatedByNative;
HANDLE FORMATTER_READ;
HANDLE FORMATTER_WRITE;
JNIEnv *mainThread;
const char * THREAD_NAME = "JPfm Native Thread";
//jmethodID FileDescriptor_constructor;
//jmethodID FormatterEvent_constructor;
#ifdef _WIN32
#define USE_FAST_PIPE
#endif
inline void attachAndMessageC(
jint attachResult,
JPfmFormatterDispatch * formatterDispatch,
char * threadName,
int jpfmMessageType,
char * message,
char * filename,
int lineNumber,
INT64 systemError){
attachResult = 0 ;
JNIEnv *env;
if(formatterDispatch->noListenerAttached)return;
attachResult = jvm->AttachCurrentThreadAsDaemon(
(void **)&env,
formatterDispatch->createAttachArgs(threadName));
if(attachResult >= 0){
formatterDispatch->messageFormatterListener(env,
jpfmMessageType,
message,
filename,
lineNumber,
systemError,
formatterDispatch->jpfmMount
);
jvm->DetachCurrentThread();
}else{
printf("attach to java failed in %s at linenumber %i ",__FILE__,__LINE__);
}
}
inline void attachAndMessage(
jint attachResult,
JPfmReadOnlyFormatterDispatch * formatterDispatch,
char * threadName,
int jpfmMessageType,
char * message,
char * filename,
int lineNumber,
INT64 systemError){
attachResult = 0 ;
JNIEnv *env;
if(formatterDispatch->noListenerAttached)return;
attachResult = jvm->AttachCurrentThreadAsDaemon(
(void **)&env,
formatterDispatch->createAttachArgs((char *)threadName));
if(attachResult >= 0){
formatterDispatch->messageFormatterListener(env,
jpfmMessageType,
message,
filename,
lineNumber,
systemError,
formatterDispatch->jpfmMount
);
jvm->DetachCurrentThread();
}else{
printf("attach to java failed in %s at linenumber %i ",__FILE__,__LINE__);
}
}
JNIEXPORT jint JNICALL
JNI_OnLoad(JavaVM *JVM, void *reserved) {
if (jvm != JVM) {
jvm = JVM;
jvmInitialized = true;
initiatedByNative = false;
mainThread = 0;
}
return JNI_VERSION_IN_USE;
}
JNIEXPORT void JNICALL
JNI_OnUnload(JavaVM *JVM, void *reserved) {
if (!IsBadReadPointer(pfmapi, sizeof (PfmApi))) {
if (pfmapi)pfmapi->Release();
pfmapi = 0;
}jvmInitialized = false;
}
JNIEXPORT jlong JNICALL Java_jpfm_JPfmMount_getPismoVersion
(JNIEnv *env, jclass clazz){
return (jlong)(pfmapi);
}
JNIEXPORT jlong JNICALL Java_jpfm_JPfmMount_getMyProcessId
(JNIEnv *env, jclass clazz){
#ifdef _WIN32
return GetCurrentProcessId();
#else
return getpid();
#endif
}
JNIEXPORT jboolean JNICALL
Java_jpfm_JPfmMount_isAlive(JNIEnv *env, jobject self, jlong formatterHandle) {
JPfmReadOnlyFormatterDispatch * formatter = getFormatter(formatterHandle);
if (!isFormatterPointerValid(formatter))return JNI_FALSE;
return (jboolean) formatter->isAlive;
}
JNIEXPORT jstring JNICALL
Java_jpfm_JPfmMount_convertErrorCodeToString(JNIEnv *env, jclass self, jlong errorCode){
jstring jmessage;
#ifdef _WIN32
// DWORD WINAPI FormatMessage(
// __in DWORD dwFlags,
// __in_opt LPCVOID lpSource,
// __in DWORD dwMessageId,
// __in DWORD dwLanguageId,
// __out LPTSTR lpBuffer,
// __in DWORD nSize,
// __in_opt va_list *Arguments
//);
int size = 100+1;
wchar_t * mesbuf = new wchar_t[size];
int messSz = 0;
messSz = FormatMessageW(
FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
(DWORD)errorCode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
mesbuf,
size,
NULL
);
if(messSz>0){
jmessage = toJavaString(env,mesbuf,messSz);
}else{
jmessage = env->NewStringUTF((const char *)" Could not convert system error to string ");
}delete mesbuf;
return jmessage;
#else //works for linux and mac
jmessage = env->NewStringUTF((const char *)strerror((int)errorCode));
return jmessage;
#endif
}
JNIEXPORT jint JNICALL
Java_jpfm_JPfmMount_unMountAndRelease(JNIEnv *env, jobject self, jlong formatterHandle) {
JPfmReadOnlyFormatterDispatch * formatterDispatch = getFormatter(formatterHandle);
if (!isFormatterPointerValid(formatterDispatch))return -1*(__LINE__);
int error = 0;
if(formatterDispatch->isAlive){
formatterDispatch->isAlive = false; // when unmount is done externally,
// this value is still true, can be used to check if unmount was done externally
error = formatterDispatch->pfmmount->Unmount(pfmUnmountFlagForce);
}
return error;
}
JNIEXPORT void JNICALL
Java_jpfm_JPfmMount_destroyAll(JNIEnv *env, jclass clazz) {
if (!IsBadReadPointer(pfmapi, sizeof (PfmApi))) {
return;
}
PFM_INT64 startChangeInstance = 0;
PFM_INT64 nextChangeInstance = startChangeInstance;
PfmIterator* pfmiterator;
PfmMount* pfmMount;
int mountId = 0;
if (
!pfmapi->MountIterate(startChangeInstance, &nextChangeInstance, &pfmiterator)
) {
for (
mountId = pfmiterator->Next(&nextChangeInstance);
mountId != 0;
mountId = pfmiterator->Next(&nextChangeInstance)
) {
if (
!pfmapi->MountOpenId(mountId, &pfmMount)
) {
pfmMount->Unmount(0);
pfmMount->Release();
pfmMount = 0;
}
}
pfmiterator->Release();
} else {
}
//pfmapi->Release();
return;
}
JNIEXPORT jint JNICALL
Java_jpfm_JPfmMount_createJPfmApi(JNIEnv *env, jclass clazz) {
/*JNIEnv *env;
JavaVMAttachArgs * args = new JavaVMAttachArgs();
args->group = NULL;
args->version = JNI_VERSION_IN_USE;
args->name = "JPfm methodId intializer (native)";
jint attachResult = jvm->AttachCurrentThreadAsDaemon((void **) & env, args);
if(attachResult < 0){
return JNI_ERR;
}
printf("going to initialize \n");
initializeMethodId(env);
jvm->DetachCurrentThread();*/
if(!jvmInitialized){
env->GetJavaVM(&jvm);
jvmInitialized = true;
//printf("Unexpected behaviour : JNI_ON_LOAD_WAS_NOT_CALLED!\n");
}
int error = !(initializeMethodId(env));
/*FileDescriptor_constructor = env->GetMethodID(FileDescriptor_class, "<init>", "()V");
if(FileDescriptor_constructor==NULL)error = true;
FormatterEvent_constructor = env->GetMethodID(FormatterEvent_class, "<init>", "(ILjava/lang/String;)V");
if(FormatterEvent_constructor==NULL)error = true;*/
if(error){
printf("Some classes of jpfm are missing or incorrectly defined\n");
return 1;
}
error = !(initializeOpMethodId(env));
if(error){
printf("Some classes of concurrent filesystem in jpfm are missing or incorrectly defined\n");
return 1;
}
error = PfmApiFactory(&pfmapi);
if(error)printf("Error initializing pfmapi, error=%i\n",error);
if(error)return 2; //pismo has not been installed
return 0;
}
JNIEXPORT jlong JNICALL
Java_jpfm_JPfmMount_mountNewReadOnly(
JNIEnv *env,
jobject jpfmMount,
jobject jpfmReadOnlyFileSystem,
jstring mountLocation,
jobject rootDirectory,
jobject formatterListener,
jobject threadGroup,
jstring formatterName,
jint jpfmSuppliedMountFlag,
jint visibleProcessId) {
//all null checks have been done by the java side code
JPfmReadOnlyFormatterDispatch * formatter =
new JPfmReadOnlyFormatterDispatch(
env->NewGlobalRef(jpfmReadOnlyFileSystem),
env->NewGlobalRef(rootDirectory),
env->NewGlobalRef(formatterListener),
env->NewGlobalRef(threadGroup)
);
if( env->IsSameObject(formatter->formatterListener,NULL) == JNI_FALSE )
formatter->noListenerAttached = false;
else formatter->noListenerAttached = true;
formatter->mountFileName = makeALocalCopyOfString(env, mountLocation);
formatter->jpfmSuppliedMountFlag = jpfmSuppliedMountFlag;
formatter->formattername = makeALocalUTFCopyOfString(env, formatterName);
formatter->jpfmMount = env->NewGlobalRef(jpfmMount);
//correct processid here itself, because it from here that it will be passed
//everywhere
if(visibleProcessId==-2) { // impylies process id of myself
#ifdef _WIN32
formatter->visibleProcessId = GetCurrentProcessId();
#else
formatter->visibleProcessId = getpid();
//docs says getpid shall never be unsuccessfull and no value exists to
//indicate a failure.
//Assuming the same for GetCurrentProcessId
#endif
} else {
formatter->visibleProcessId = visibleProcessId;
}
if(initiatedByNative){
mountingThread((void*)formatter);
return 0;//formatter is never passed to java side
} else {
#ifdef _WIN32
////////////////WIN//////////////////
DWORD dwThreadId;
formatter->hThread = CreateThread(
NULL, // default security attributes
0, // use default stack size
(LPTHREAD_START_ROUTINE) mountingThread, // thread function
formatter, // argument to thread function
0, // use default creation flags
&dwThreadId);
#else
#ifdef __APPLE__
//////////////////MAC/////////////////////////
pthread_attr_t attr;
int returnVal;
returnVal = pthread_attr_init(&attr);
//assert(!returnVal);
returnVal = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
//assert(!returnVal);
#endif
// works on both mac and linux
int thread_ret;
pthread_t mountingThread_pthread_t;
thread_ret = formatter->hThread = pthread_create(
&mountingThread_pthread_t,
#ifdef __APPLE__
&attr
#else
NULL
#endif
,mountingThread,
(void*)formatter);
#endif
}
//DWORD waitResult = WaitForSingleObject(formatter->hThread,10000);
/*DWORD waitResult = WaitForSingleObject(formatter->hThread,INFINITE);
if(waitResult==WAIT_FAILED){
printf("volume mounting thread failed wait failed\n");
}
if(waitResult==WAIT_TIMEOUT){
printf("volume mounting thread failed wait timeout\n");
}*/
return (jlong) formatter;
}
JNIEXPORT jlong JNICALL
Java_jpfm_JPfmMount_mountNewConcurrent(
JNIEnv *env,
jobject jpfmMount,
jobject jpfmFileSystem,
jstring mountLocation,
jchar driveLetter,
jobject formatterListener,
jobject threadGroup,
jstring formatterName,
jint jpfmSuppliedMountFlag,
jint volumeFlags,
jint visibleProcessId) {
//all null checks have been done by the java side code
JPfmFormatterDispatch * formatter =
new JPfmFormatterDispatch(
env->NewGlobalRef(jpfmFileSystem),
env->NewGlobalRef(formatterListener),
env->NewGlobalRef(threadGroup)
);
if( env->IsSameObject(formatter->formatterListener,NULL) == JNI_FALSE )
formatter->noListenerAttached = false;
else formatter->noListenerAttached = true;
formatter->mountFileName = makeALocalCopyOfString(env, mountLocation);
formatter->driveLetter = (wchar_t)driveLetter;
formatter->jpfmSuppliedMountFlag = jpfmSuppliedMountFlag;
formatter->volumeFlags = volumeFlags;
//correct processid here itself, because it from here that it will be passed
//everywhere
if(visibleProcessId==-2) { // impylies process id of myself
#ifdef _WIN32
formatter->visibleProcessId = GetCurrentProcessId();
#else
formatter->visibleProcessId = getpid();
//docs says getpid shall never be unsuccessfull and no value exists to
//indicate a failure.
//Assuming the same for GetCurrentProcessId
#endif
} else {
formatter->visibleProcessId = visibleProcessId;
}
formatter->formattername = makeALocalUTFCopyOfString(env, formatterName);
formatter->jpfmMount = env->NewGlobalRef(jpfmMount);
if(initiatedByNative){
mountingThreadForConcurrentFS((void*)formatter);
return 0;//formatter is never passed to java side
}else{
#ifdef _WIN32
////////////////WIN//////////////////
DWORD dwThreadId;
formatter->hThread = CreateThread(
NULL, // default security attributes
0, // use default stack size
(LPTHREAD_START_ROUTINE) mountingThreadForConcurrentFS, // thread function
formatter, // argument to thread function
0, // use default creation flags
&dwThreadId);
#else
//#ifdef __APPLE__
////////////////MAC//////////////////
//#else
////////////////LINUX//////////////////
//#endif
#endif
}
//DWORD waitResult = WaitForSingleObject(formatter->hThread,10000);
/*DWORD waitResult = WaitForSingleObject(formatter->hThread,INFINITE);
if(waitResult==WAIT_FAILED){
printf("volume mounting thread failed wait failed\n");
}
if(waitResult==WAIT_TIMEOUT){
printf("volume mounting thread failed wait timeout\n");
}*/
return (jlong) formatter;
}
#ifdef _WIN32
DWORD WINAPI mountingThread(LPVOID lpParam) {
#else
//#ifdef __APPLE__
////////////////MAC//////////////////
//#else
////////////////LINUX//////////////////
void * mountingThread(void* lpParam) {
//#endif
#endif
JPfmReadOnlyFormatterDispatch * formatterDispatch = (JPfmReadOnlyFormatterDispatch *) lpParam;
PfmMountCreateParams * mountCreateParams = new PfmMountCreateParams();
PfmMarshallerServeParams * marshallerServeParams = new PfmMarshallerServeParams();
PfmFastPipeCreateParams * fastPipeCreateParams = new PfmFastPipeCreateParams();
//fast pipe handles
PFM_HANDLE clientHandle = PFM_INVALIDHANDLE;
PFM_HANDLE serverHandle = PFM_INVALIDHANDLE;
//fast pipe
//to remove
PFM_HANDLE toFormatterRead = PFM_INVALIDHANDLE;
PFM_HANDLE toFormatterWrite = PFM_INVALIDHANDLE;
PFM_HANDLE fromFormatterRead = PFM_INVALIDHANDLE;
PFM_HANDLE fromFormatterWrite = PFM_INVALIDHANDLE;
//normal pipe handle
JNIEnv *env;
int error = 0;
jint attachResult = 0;
char * pipeErrorMess ;
// For communication between driver and formatter, use
// fastpipe if available. Fastpipe direct maps buffers
// into formatter address space, providing increased
// maximum throughput for high bandwidth file systems.
// Fastpipe also saves the marshaller from needing to
// create an extra "write" thread.
#ifdef USE_FAST_PIPE
pipeErrorMess = "Could not create fast pipe";
#ifdef __APPLE__
error = fileErrorNotImplemented;
#else
memset(fastPipeCreateParams, 0, sizeof (PfmFastPipeCreateParams)); //t_memzero(&pcp);
fastPipeCreateParams->paramsSize = sizeof (PfmFastPipeCreateParams);
fastPipeCreateParams->fastPipeFlags = pfmFastPipeFlagFastMapping|
pfmFastPipeFlagAsyncClient|pfmFastPipeFlagAsyncServer;
error = pfmapi->FastPipeCreate(
fastPipeCreateParams,
&clientHandle, //driverEndHandle
&serverHandle //formatterEndHandle
);
#endif
pipeErrorMess = "Could not create fast pipe, trying to create and use normal pipe";
#else
pipeErrorMess = "Using normal pipe because USE_FAST_PIPE was not defined";
error = fileErrorNotImplemented; // testing normal pipe procedures
#endif
if(error)
attachAndMessage(
attachResult,
formatterDispatch,
(char *)THREAD_NAME,
JPFM_FORMATTER_EVENT_MESSAGE,
pipeErrorMess,
__FILE__,
__LINE__,
error);
if (error == fileErrorNotImplemented) {
// If no fastpipe then use a domain socket.
error = EFileCreateSocket(&clientHandle, &serverHandle);
pipeErrorMess = "Could not even create normal pipe";
}
if(error){
attachAndMessage(
attachResult,
formatterDispatch,
(char *)THREAD_NAME,
JPFM_FORMATTER_EVENT_ERROR,
pipeErrorMess,
__FILE__,
__LINE__,
error);
if (!IsBadWritePointer(formatterDispatch, sizeof (JPfmReadOnlyFormatterDispatch)))
delete formatterDispatch;
#ifdef _WIN32
return __LINE__;
#else
pthread_exit(NULL);
return NULL;
#endif
}
memset(mountCreateParams, 0, sizeof (PfmMountCreateParams));
/*mountCreateParams->mountFlags |= (
//pfmMountFlagUncOnly|
pfmMountFlagSystemVisible| pfmMountFlagWorldRead | pfmMountFlagReadOnly); */
mountCreateParams->mountFlags = 0;
mountCreateParams->mountFlags |= formatterDispatch->jpfmSuppliedMountFlag;
mountCreateParams->mountFileName = formatterDispatch->mountFileName;
mountCreateParams->visibleProcessId = formatterDispatch->visibleProcessId; ;// pfmVisibleProcessIdAll;
#ifdef USE_FAST_PIPE
mountCreateParams->toFormatterWrite = clientHandle;
mountCreateParams->fromFormatterRead = clientHandle;
#else
mountCreateParams->toFormatterWrite = toFormatterWrite;
mountCreateParams->fromFormatterRead = fromFormatterRead;
#endif
mountCreateParams->toAuthWrite = PFM_INVALIDHANDLE;
mountCreateParams->fromAuthRead = PFM_INVALIDHANDLE;
mountCreateParams->paramsSize = sizeof (PfmMountCreateParams);
error = pfmapi->MountCreate(mountCreateParams, &formatterDispatch->pfmmount);
if(error){
attachAndMessage(
attachResult,
formatterDispatch,
(char *)THREAD_NAME,
JPFM_FORMATTER_EVENT_MOUNT_CREATE_FAILED,
"Mounting was unsuccessful.",
__FILE__,
__LINE__,
error);
delete mountCreateParams;
delete fastPipeCreateParams;
if (!IsBadWritePointer(formatterDispatch, sizeof (JPfmReadOnlyFormatterDispatch)));
delete formatterDispatch;
#ifdef _WIN32
return error;
#else
pthread_exit(NULL);
return NULL;
#endif
}else{
attachResult = jvm->AttachCurrentThreadAsDaemon((void **)&env,formatterDispatch->createAttachArgs((char *)THREAD_NAME));
if(attachResult >=0){
env->CallVoidMethod(formatterDispatch->jpfmMount,JPfmMount_setMountId,(jlong)formatterDispatch->pfmmount->GetMountId());
jvm->DetachCurrentThread();
}
}
#ifdef USE_FAST_PIPE
FileClose(clientHandle);//this is a little modified than what we have for ramfs
clientHandle = PFM_INVALIDHANDLE;
#else
/* Close driver end pipe handles now. Driver has duplicated what
it needs. If these handles are not closed then pipes will not
break if driver disconnects, leaving us stuck in the
marshaller.
*/
FileClose(toFormatterWrite);
FileClose(fromFormatterRead);
#endif
memset(marshallerServeParams, 0, sizeof (PfmMarshallerServeParams));
marshallerServeParams->dispatch = formatterDispatch;
marshallerServeParams->paramsSize = sizeof (PfmMarshallerServeParams);
marshallerServeParams->formatterName = (const char *) formatterDispatch->formattername;
#ifdef USE_FAST_PIPE
marshallerServeParams->toFormatterRead = serverHandle;
marshallerServeParams->fromFormatterWrite = serverHandle;
#else
marshallerServeParams->toFormatterRead = toFormatterRead;
marshallerServeParams->fromFormatterWrite = fromFormatterWrite;
#endif
marshallerServeParams->volumeFlags = pfmVolumeFlagReadOnly;//files can be deleted
error = PfmMarshallerFactory(&formatterDispatch->marshaller);
if (error) {
attachAndMessage(
attachResult,
formatterDispatch,
(char *)THREAD_NAME,
JPFM_FORMATTER_EVENT_ERROR,
"Marshaller could not be created",
__FILE__,
__LINE__,
error);
delete mountCreateParams;
delete fastPipeCreateParams;
delete marshallerServeParams;
if (!IsBadWritePointer(formatterDispatch, sizeof (JPfmReadOnlyFormatterDispatch)));
delete formatterDispatch;
jvm->DetachCurrentThread();
#ifdef _WIN32
return __LINE__;
#else
pthread_exit(NULL);
return NULL;
#endif
}
//formatterDispatch->marshaller->SetStatus(GetStdHandle(STD_ERROR_HANDLE));
formatterDispatch->marshaller->SetTrace(formatterDispatch->mountFileName/*L"JPfm"*/);
//JNIEnv *env;
//char *nm = /*_strdup(*/"Formatter ServeDispatch thread";/*);*/
//printf("attaching %s\n",nm);
//jvm->AttachCurrentThread((void **)&env,formatter->createAttachArgs(nm));
//attaching thread is useless as the first method that
//is invoked after servedispatch is formatterDispatch's open method
//After finishing it 's work it kills the thread
//Checks can be added, but, even so, it cannot be said for sure
//if this thread will be used again. In such a case
//checks will have to be added everywhere, which might be a good
//thing because it seems all reside in the same thread.
//The classic method followed is going in a loop to read the driver requests
//so maybe this same thread is used. Yet again I wonder how
//this type of formatter is multithreaded
if(!formatterDispatch->noListenerAttached){
#ifdef __APPLE__
printf("Just going to dispatch formatter\n" );
#else
attachResult = jvm->AttachCurrentThreadAsDaemon((void **)&env,formatterDispatch->createAttachArgs((char *)THREAD_NAME));
if(attachResult >=0){
formatterDispatch->messageFormatterListener(env,
JPFM_FORMATTER_EVENT_MESSAGE,
"Just going to dispatch formatter.",0
);
jvm->DetachCurrentThread();
}
#endif
}
error = formatterDispatch->marshaller->ServeDispatch(marshallerServeParams);
if(!formatterDispatch->noListenerAttached){
if(error){
attachAndMessage(
attachResult,
formatterDispatch,
(char *)THREAD_NAME,
JPFM_FORMATTER_EVENT_ERROR,
"ServeDispatch returned with error",
__FILE__,
__LINE__,
error);
}
attachResult = jvm->AttachCurrentThreadAsDaemon((void **)&env,formatterDispatch->createAttachArgs((char *)THREAD_NAME));
if(attachResult >=0){
if(formatterDispatch->isAlive==true){
// if unmount was intiated by us, we set isAlive false,
formatterDispatch->messageFormatterListener(env,
JPFM_FORMATTER_EVENT_DETACHED,
"Some external tool might have initiated an unmount operation or and error might have occurred.",0);
}else /*we did it*/{
formatterDispatch->messageFormatterListener(env,
JPFM_FORMATTER_EVENT_DETACHED,
"Unmount intiated internally was successful",0);
}jvm->DetachCurrentThread();//this will detach the
}
}
// NULL);
//printf("served\n");
//separated by | oring
#ifdef USE_FAST_PIPE
if (serverHandle != PFM_INVALIDHANDLE){
FileClose(serverHandle);//CloseHandle(serverHandle);
}
#else
if(toFormatterRead != PFM_INVALIDHANDLE)
FileClose(toFormatterRead);
if(toFormatterRead != PFM_INVALIDHANDLE)
FileClose(fromFormatterWrite);
#endif
if(formatterDispatch->pfmmount){
//formatterDispatch->pfmmount->Release();
formatterDispatch->pfmmount = 0;
}
delete mountCreateParams;
delete marshallerServeParams;
delete fastPipeCreateParams;
if (!IsBadWritePointer(formatterDispatch, sizeof (JPfmReadOnlyFormatterDispatch)));
delete formatterDispatch;
#ifdef _WIN32
////////////////WIN//////////////////
return error;
#else
//#ifdef __APPLE__
////////////////MAC//////////////////
//#else
////////////////LINUX//////////////////
pthread_exit(NULL);
//#endif
#endif
}
#ifdef _WIN32
////////////////WIN//////////////////
DWORD WINAPI mountingThreadForConcurrentFS(LPVOID lpParam) {
#else
//#ifdef __APPLE__
////////////////MAC//////////////////
//#else
////////////////LINUX//////////////////
void * mountingThreadForConcurrentFS(void* lpParam) {
//#endif
#endif
JPfmFormatterDispatch * formatterDispatch = (JPfmFormatterDispatch *) lpParam;
PfmMountCreateParams * mountCreateParams = new PfmMountCreateParams();
PfmMarshallerServeParams * marshallerServeParams = new PfmMarshallerServeParams();
PfmFastPipeCreateParams * fastPipeCreateParams = new PfmFastPipeCreateParams();
PFM_HANDLE clientHandle = PFM_INVALIDHANDLE;
PFM_HANDLE serverHandle = PFM_INVALIDHANDLE;
//to remove
PFM_HANDLE toFormatterRead = PFM_INVALIDHANDLE;
PFM_HANDLE toFormatterWrite = PFM_INVALIDHANDLE;
PFM_HANDLE fromFormatterRead = PFM_INVALIDHANDLE;
PFM_HANDLE fromFormatterWrite = PFM_INVALIDHANDLE;
//normal pipe handle
JNIEnv *env;
int error = 0;
jint attachResult;
char * pipeErrorMess ;
// For communication between driver and formatter, use
// fastpipe if available. Fastpipe direct maps buffers
// into formatter address space, providing increased
// maximum throughput for high bandwidth file systems.
// Fastpipe also saves the marshaller from needing to
// create an extra "write" thread.
#ifdef USE_FAST_PIPE
pipeErrorMess = "Could not create fast pipe";
memset(fastPipeCreateParams, 0, sizeof (PfmFastPipeCreateParams)); //t_memzero(&pcp);
fastPipeCreateParams->paramsSize = sizeof (PfmFastPipeCreateParams);
fastPipeCreateParams->fastPipeFlags = pfmFastPipeFlagFastMapping|
pfmFastPipeFlagAsyncClient|pfmFastPipeFlagAsyncServer;
error = pfmapi->FastPipeCreate(
fastPipeCreateParams,
&clientHandle, //driverEndHandle
&serverHandle //formatterEndHandle
);
#else // use normal pipes
pipeErrorMess = "Using normal pipe as USE_FAST_PIPE is undefined";
error = fileErrorNotImplemented;
#endif
if (error == fileErrorNotImplemented) {
// If no fastpipe then use a domain socket.
error = EFileCreateSocket(&clientHandle, &serverHandle);
pipeErrorMess = "Could not even use EFileCreateSoket";
}
if(error){
attachAndMessageC(
attachResult,
formatterDispatch,
(char *)THREAD_NAME,
JPFM_FORMATTER_EVENT_ERROR,
pipeErrorMess,
__FILE__,
__LINE__,
error);
if (!IsBadWritePointer(formatterDispatch, sizeof (JPfmReadOnlyFormatterDispatch)));
delete formatterDispatch;
#ifdef _WIN32
return __LINE__;
#else
pthread_exit(NULL);
return NULL;
#endif
}
memset(mountCreateParams, 0, sizeof (PfmMountCreateParams));
mountCreateParams->mountFlags |= (
//pfmMountFlagSystemVisible|
pfmMountFlagWorldRead /*| pfmMountFlagReadOnly*/);
mountCreateParams->mountFlags |= formatterDispatch->jpfmSuppliedMountFlag;
mountCreateParams->mountFileName = formatterDispatch->mountFileName;
mountCreateParams->driveLetter = formatterDispatch->driveLetter;
mountCreateParams->visibleProcessId = formatterDispatch->visibleProcessId;//checks for this already done
#ifdef USE_FAST_PIPE
mountCreateParams->toFormatterWrite = clientHandle;
mountCreateParams->fromFormatterRead = clientHandle;
#else
mountCreateParams->toFormatterWrite = toFormatterWrite;
mountCreateParams->fromFormatterRead = fromFormatterRead;
#endif
mountCreateParams->paramsSize = sizeof (PfmMountCreateParams);
error = pfmapi->MountCreate(mountCreateParams, &formatterDispatch->pfmmount);
jvm->AttachCurrentThreadAsDaemon((void **)&env,formatterDispatch->createAttachArgs((char *)THREAD_NAME));
if(error){
attachAndMessageC(
attachResult,
formatterDispatch,
(char *)THREAD_NAME,
JPFM_FORMATTER_EVENT_MOUNT_CREATE_FAILED,
"Mounting was unsuccessful.",
__FILE__,
__LINE__,
error
);
delete mountCreateParams;
delete fastPipeCreateParams;
if (!IsBadWritePointer(formatterDispatch, sizeof (JPfmReadOnlyFormatterDispatch)));
delete formatterDispatch;
#ifdef _WIN32
return __LINE__;
#else
pthread_exit(NULL);
return NULL;
#endif
}else{
attachResult = jvm->AttachCurrentThreadAsDaemon((void**)&env,formatterDispatch->createAttachArgs((char *)THREAD_NAME));
if(attachResult >= 0){
env->CallVoidMethod(formatterDispatch->jpfmMount,JPfmMount_setMountId,(jlong)formatterDispatch->pfmmount->GetMountId());
if(env->ExceptionCheck()){
env->ExceptionDescribe();
env->ExceptionClear();
}
jvm->DetachCurrentThread();
}
}
#ifdef USE_FAST_PIPE
FileClose(clientHandle);//this is a little modified than what we have for ramfs
clientHandle = PFM_INVALIDHANDLE;
#else
/* Close driver end pipe handles now. Driver has duplicated what
it needs. If these handles are not closed then pipes will not
break if driver disconnects, leaving us stuck in the
marshaller.
*/
FileClose(toFormatterWrite);
FileClose(fromFormatterRead);
#endif
memset(marshallerServeParams, 0, sizeof (PfmMarshallerServeParams));
marshallerServeParams->dispatch = formatterDispatch;
marshallerServeParams->paramsSize = sizeof (PfmMarshallerServeParams);
marshallerServeParams->formatterName = (const char *) formatterDispatch->formattername;
#ifdef USE_FAST_PIPE
marshallerServeParams->toFormatterRead = serverHandle;
marshallerServeParams->fromFormatterWrite = serverHandle;
#else
marshallerServeParams->toFormatterRead = toFormatterRead;
marshallerServeParams->fromFormatterWrite = fromFormatterWrite;
#endif
marshallerServeParams->volumeFlags = formatterDispatch->volumeFlags; //pfmVolumeFlagReadOnly;//files can be deleted even though pfmVolumeFlagReadOnly maybe this setting is purely cosmetic
error = PfmMarshallerFactory(&formatterDispatch->marshaller);
if (error) {
attachAndMessageC(
attachResult,
formatterDispatch,
(char *)THREAD_NAME,
JPFM_FORMATTER_EVENT_MOUNT_CREATE_FAILED,
"Could not create fast pipe. Giving up.",
__FILE__,
__LINE__,
error);
if (!IsBadWritePointer(formatterDispatch, sizeof (JPfmReadOnlyFormatterDispatch)));
delete formatterDispatch;
delete mountCreateParams;
delete fastPipeCreateParams;
delete marshallerServeParams;
#ifdef _WIN32
return __LINE__;
#else
pthread_exit(NULL);
return NULL;
#endif
}
//formatterDispatch->marshaller->SetStatus(GetStdHandle(STD_ERROR_HANDLE));
formatterDispatch->marshaller->SetTrace(formatterDispatch->mountFileName/*L"JPfm"*/);
//JNIEnv *env;
//char *nm = /*_strdup(*/"Formatter ServeDispatch thread";/*);*/
//printf("attaching %s\n",nm);
//jvm->AttachCurrentThread((void **)&env,formatter->createAttachArgs(nm));
//attaching thread is useless as the first method that
//is invoked after servedispatch is formatterDispatch's open method
//After finishing it 's work it kills the thread
//Checks can be added, but, even so, it cannot be said for sure
//if this thread will be used again. In such a case
//checks will have to be added everywhere, which might be a good
//thing because it seems all reside in the same thread.
//The classic method followed is going in a loop to read the driver requests
//so maybe this same thread is used. Yet again I wonder how
//this type of formatter is multithreaded
if(!formatterDispatch->noListenerAttached){
jvm->AttachCurrentThreadAsDaemon((void **)&env,formatterDispatch->createAttachArgs((char *)THREAD_NAME));
formatterDispatch->messageFormatterListener(env,
JPFM_FORMATTER_EVENT_MESSAGE,
"Just going to dispatch formatter.",0
);
jvm->DetachCurrentThread();
}
error = formatterDispatch->marshaller->ServeDispatch(marshallerServeParams);
if(error){
attachAndMessageC(
attachResult,
formatterDispatch,
(char *)THREAD_NAME,
JPFM_FORMATTER_EVENT_ERROR,
"ServeDispatch ended with error",
__FILE__,
__LINE__,
error);
}
if(!formatterDispatch->noListenerAttached){
jvm->AttachCurrentThreadAsDaemon((void **)&env,formatterDispatch->createAttachArgs((char *)THREAD_NAME));
if(formatterDispatch->isAlive==JNI_TRUE){
// if unmount was intiated by us, we set isAlive false,
formatterDispatch->messageFormatterListener(env,
JPFM_FORMATTER_EVENT_DETACHED,
"Some external tool might have initiated an unmount operation or and error might have occurred.",0);
}else /*we did it*/{
formatterDispatch->messageFormatterListener(env,
JPFM_FORMATTER_EVENT_DETACHED,
"Unmount intiated internally was successful",0);
}
jvm->DetachCurrentThread();
}
// NULL);
//printf("served\n");
//separated by | oring
#ifdef USE_FAST_PIPE
if (serverHandle != PFM_INVALIDHANDLE){
FileClose(serverHandle);//CloseHandle(serverHandle);
}
#else
if(toFormatterRead != PFM_INVALIDHANDLE)
FileClose(toFormatterRead);
if(toFormatterRead != PFM_INVALIDHANDLE)
FileClose(fromFormatterWrite);
#endif
if(formatterDispatch->pfmmount){
//formatterDispatch->pfmmount->Release();
formatterDispatch->pfmmount = 0;
}
delete mountCreateParams;
delete marshallerServeParams;
delete fastPipeCreateParams;
if (!IsBadWritePointer(formatterDispatch, sizeof (JPfmReadOnlyFormatterDispatch)));
delete formatterDispatch;
#ifdef _WIN32
#else
//#ifdef __APPLE__
////////////////MAC//////////////////
//#else
////////////////LINUX//////////////////
pthread_exit(NULL);
//#endif
#endif
#ifdef _WIN32
return error;
#else
pthread_exit(NULL);
#endif
}
//not required on windows and fast pipe is always implemented there
#ifdef _WIN32
static long file_last_pipe_instance = 0;
INT64 PFM_CCALL file_get_system_time(void){
union{
INT64 file;
FILETIME system;
} time;
GetSystemTimeAsFileTime(&(time.system));
return time.file;
}
int/*error*/ EFileCreateSocket(HANDLE* out_handle_1,HANDLE* out_handle_2) {
#else
int/*error*/ EFileCreateSocket(int* handle1,int* handle2){
#endif
#ifdef _WIN32
int/*bool*/ bidir = true;
int error = 0;
HANDLE handle1;
HANDLE handle2 = INVALID_HANDLE_VALUE;
enum { max_pipe_name_chars = 100 };
wchar_t pipe_name[max_pipe_name_chars];
// CreatePipe creates handles that do not support overlapped IO and
// are unidirectional, so use named pipe instead.
wsprintfW(pipe_name,L"\\\\.\\Pipe\\AnonymousPipe.%i.%p.%i.%I64i",
GetCurrentProcessId(),&file_last_pipe_instance,
InterlockedIncrement(&file_last_pipe_instance),file_get_system_time());
handle1 = CreateNamedPipeW(pipe_name,(bidir?(PIPE_ACCESS_DUPLEX|
FILE_FLAG_OVERLAPPED):PIPE_ACCESS_INBOUND)|
FILE_FLAG_FIRST_PIPE_INSTANCE,PIPE_TYPE_BYTE|PIPE_READMODE_BYTE|
PIPE_WAIT,1/*pipeCount*/,0/*outBufSize*/,0/*inBufSize*/,
30*1000/*timeout*/,0/*security*/);
if(!handle1)
{
handle1 = INVALID_HANDLE_VALUE;
}
if(handle1 == INVALID_HANDLE_VALUE)
{
error = GetLastError();
}
if(!error)
{
handle2 = CreateFileW(pipe_name,(bidir?GENERIC_READ:0)|GENERIC_WRITE,
0/*shareMode*/,0/*security*/,OPEN_EXISTING,
bidir?FILE_FLAG_OVERLAPPED:0,0/*template*/);
if(handle2 == INVALID_HANDLE_VALUE)
{
error = GetLastError();
/*VERIFY(*/CloseHandle(handle1)/*)*/;
handle1 = INVALID_HANDLE_VALUE;
}
}
*out_handle_1 = handle1;
*out_handle_2 = handle2;
return error;
return -1; // fast pipe always available on windows :)
#else
int error = 0;
int handles[2];
if(socketpair(AF_UNIX,SOCK_STREAM,0,handles) != 0){
error = errno;
}/*else {
// Convert socket to uni-directional.
shutdown(handles[0],SHUT_WR);
shutdown(handles[1],SHUT_RD);
}*/
*handle1 = handles[0];
*handle2 = handles[1];
return error;
#endif
}