forked from EUDAT-B2STAGE/B2STAGE-GridFTP
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathglobus_gridftp_server_iRODS.cpp
3097 lines (2656 loc) · 117 KB
/
globus_gridftp_server_iRODS.cpp
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
/*
* Copyright (c) 2013 CINECA (www.hpc.cineca.it)
*
* Copyright (c) 1999-2006 University of Chicago
*
* 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.
*
*
* Globus DSI to manage data on iRODS.
*
* Author: Roberto Mucci - SCAI - CINECA
* Email: [email protected]
*
*/
//#pragma GCC diagnostic ignored "-Wregister"`
extern "C" {
#include "globus_gridftp_server.h"
#include "globus_range_list.h"
}
//#pragma GCC diagnostic pop
#ifdef IRODS_HEADER_HPP
#include <irods/rodsClient.hpp>
#else
#include <irods/rodsClient.h>
#endif
#include <irods/irods_query.hpp>
#include <irods/irods_string_tokenize.hpp>
#include <irods/irods_virtual_path.hpp>
#include <irods_hasher_factory.hpp>
#include <irods/irods_at_scope_exit.hpp>
#include <irods/get_file_descriptor_info.h>
#include <irods/replica_close.h>
#include <irods/thread_pool.hpp>
#include <irods/filesystem.hpp>
#include <irods/base64.hpp>
#include <irods/touch.h>
// boost includes
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <string>
#include "pid_manager.h"
#include <cstring>
#include <cstdio>
#include <ctime>
#include <unistd.h>
#include <dlfcn.h>
#include <pthread.h>
#include <iomanip>
#include <condition_variable>
#include <map>
// local includes
#include "circular_buffer.hpp"
#define MAX_DATA_SIZE 1024
/* Path to the file mapping iRODS path and resources*/
#define IRODS_RESOURCE_MAP "irodsResourceMap"
#define IRODS_USER_MAP "irodsUerap"
#define IRODS_LIST_UPDATE_INTERVAL_SECONDS 10
#define IRODS_LIST_UPDATE_INTERVAL_COUNT 1000
#define IRODS_CHECKSUM_DEFAULT_UPDATE_INTERVAL_SECONDS 5
#ifndef DEFAULT_HOMEDIR_PATTERN
/* Default homeDir pattern, referencing up to two strings with %s.
* If used, first gets substituted with the zone name, second with the user name.
*/
#define DEFAULT_HOMEDIR_PATTERN "/%s/home/%s"
#endif
/* name of environment variable to check for the homeDirPattern */
#define HOMEDIR_PATTERN "homeDirPattern"
/* if present, connect as the admin account stored in rodsEnv and not as the user */
#define IRODS_CONNECT_AS_ADMIN "irodsConnectAsAdmin"
/* If present, use the handle server to resolve PID */
#define PID_HANDLE_SERVER "pidHandleServer"
/* if present, set the number or read/write threads for file transfers */
#define NUMBER_OF_IRODS_READ_WRITE_THREADS "numberOfIrodsReadWriteThreads"
/* if present, set the number or read/write threads for file transfers */
#define IRODS_PARALLEL_FILE_SIZE_THRESHOLD_BYTES "irodsParallelFileSizeThresholdBytes"
const static unsigned int DEFAULT_NUMBER_OF_IRODS_READ_WRITE_THREADS = 3;
const static unsigned int MAXIMUM_NUMBER_OF_IRODS_READ_WRITE_THREADS = 10;
const static std::string CHECKSUM_AVU_NAMESPACE{"GLOBUS"};
// map to translate globus_gfs_command_type_t values into strings for error messages
const static std::map<int, std::string> command_map = {
{ 1, "GLOBUS_GFS_CMD_MKD" },
{ 2, "GLOBUS_GFS_CMD_RMD" },
{ 3, "GLOBUS_GFS_CMD_DELE" },
{ 4, "GLOBUS_GFS_CMD_SITE_AUTHZ_ASSERT" },
{ 5, "GLOBUS_GFS_CMD_SITE_RDEL" },
{ 6, "GLOBUS_GFS_CMD_RNTO" },
{ 7, "GLOBUS_GFS_CMD_RNFR" },
{ 8, "GLOBUS_GFS_CMD_CKSM" },
{ 9, "GLOBUS_GFS_CMD_SITE_CHMOD" },
{ 10, "GLOBUS_GFS_CMD_SITE_DSI" },
{ 11, "GLOBUS_GFS_CMD_SITE_SETNETSTACK" },
{ 12, "GLOBUS_GFS_CMD_SITE_SETDISKSTACK" },
{ 13, "GLOBUS_GFS_CMD_SITE_CLIENTINFO" },
{ 14, "GLOBUS_GFS_CMD_DCSC" },
{ 15, "GLOBUS_GFS_CMD_SITE_CHGRP" },
{ 16, "GLOBUS_GFS_CMD_SITE_UTIME" },
{ 17, "GLOBUS_GFS_CMD_SITE_SYMLINKFROM" },
{ 18, "GLOBUS_GFS_CMD_SITE_SYMLINK" },
{ 19, "GLOBUS_GFS_CMD_HTTP_PUT" },
{ 21, "GLOBUS_GFS_CMD_HTTP_GET" },
{ 22, "GLOBUS_GFS_CMD_HTTP_CONFIG" },
{ 23, "GLOBUS_GFS_CMD_TRNC" },
{ 24, "GLOBUS_GFS_CMD_SITE_TASKID" },
{ 3072, "GLOBUS_GFS_CMD_SITE_RESTRICT" },
{ 3073, "GLOBUS_GFS_CMD_SITE_CHROOT" },
{ 3074, "GLOBUS_GFS_CMD_SITE_SHARING" },
{ 3075, "GLOBUS_GFS_CMD_UPAS" },
{ 3076, "GLOBUS_GFS_CMD_UPRT" },
{ 3077, "GLOBUS_GFS_CMD_STORATTR" },
{ 3078, "GLOBUS_GFS_CMD_WHOAMI" },
{ 4096, "GLOBUS_GFS_MIN_CUSTOM_CMD" }
};
const std::string get_command_string(const int i) {
auto iter = command_map.find(i);
if (iter != command_map.end()) {
return iter->second;
}
return std::to_string(i);
}
// struct to save buffer to be written to iRODS
typedef struct read_write_buffer
{
globus_byte_t * buffer;
globus_size_t nbytes;
globus_off_t offset;
} read_write_buffer_t;
// create reading and writing circular buffers (10 buffer entries and 30 second timeout)
irods::experimental::circular_buffer<read_write_buffer_t> irods_write_circular_buffer{10, 30};
static int iRODS_l_dev_wrapper = 10;
/* structure and global variable for holding pointer to the (last) selected resource mapping */
struct iRODS_Resource
{
char * path;
char * resource;
};
struct iRODS_Resource iRODS_Resource_struct = {nullptr,NULL};
typedef struct cksum_thread_args
{
bool *done_flag;
globus_gfs_operation_t *op;
pthread_mutex_t *mutex;
pthread_cond_t *cond;
int *update_interval;
size_t *bytes_processed;
} cksum_thread_args_t;
GlobusDebugDefine(GLOBUS_GRIDFTP_SERVER_IRODS);
static
globus_version_t local_version =
{
0, /* major version number */
1, /* minor version number */
1369393102,
0 /* branch ID */
};
int convert_base64_to_hex_string(const std::string& base64_str, const int& bit_count, std::string& out_str) {
unsigned char out[bit_count / 8];
unsigned long out_len = bit_count / 8;
int ret = irods::base64_decode(reinterpret_cast<const unsigned char*>(base64_str.c_str()), base64_str.size(), out, &out_len);
if (ret < 0) {
return ret;
} else {
std::stringstream ss;
for (unsigned long offset = 0; offset < out_len; offset += 1) {
unsigned char *current_byte = reinterpret_cast<unsigned char*>(out + offset);
int int_value = *current_byte;
ss << std::setfill('0') << std::setw(2) << std::hex << int_value;
}
out_str = ss.str();
}
return 0;
}
// removes all trailing slashes and replaces consecutive slashes with a single slash
int
iRODS_l_reduce_path(
char * path)
{
char * ptr;
int len;
int end;
len = strlen(path);
while(len > 1 && path[len-1] == '/')
{
len--;
path[len] = '\0';
}
end = len-2;
while(end >= 0)
{
ptr = &path[end];
if(strncmp(ptr, "//", 2) == 0)
{
memmove(ptr, &ptr[1], len - end);
len--;
}
end--;
}
return 0;
}
/*
* the data structure representing the FTP session
*/
struct globus_l_gfs_iRODS_handle_t
{
rcComm_t * conn;
int stor_sys_type;
int fd;
globus_mutex_t mutex;
globus_gfs_operation_t op;
globus_bool_t done;
globus_bool_t read_eof;
int outstanding;
int optimal_count;
globus_size_t block_size;
globus_result_t cached_res;
globus_off_t blk_length;
globus_off_t blk_offset;
globus_fifo_t rh_q;
char * hostname;
int port;
char * zone;
char * defResource;
char * user;
char * domain;
char * irods_dn;
char * original_stat_path;
char * resolved_stat_path;
// added for redesign (issue 33)
char * adminUser;
char * adminZone;
char * replica_token;
unsigned int number_of_irods_read_write_threads;
uint64_t irods_parallel_file_size_threshold_bytes;
bool first_write_done;
// added to get file modification time from client
time_t utime;
};
std::condition_variable outstanding_cntr_cv;
std::mutex outstanding_cntr_mutex;
std::condition_variable first_write_cv;
std::mutex first_write_mutex;
void print_irods_handle(globus_l_gfs_iRODS_handle_t& handle, char * file, int line)
{
globus_gfs_log_message(GLOBUS_GFS_LOG_INFO, "%s:%d conn=%p \n", file, line, handle.conn);
}
typedef struct globus_l_iRODS_read_ahead_s
{
globus_l_gfs_iRODS_handle_t * iRODS_handle;
globus_off_t offset;
globus_size_t length;
globus_byte_t * buffer;
} globus_l_iRODS_read_ahead_t;
static
int
iRODS_l_filename_hash(
char * string)
{
int rc;
unsigned long h = 0;
unsigned long g;
char * key;
if(string == nullptr)
{
return 0;
}
key = (char *) string;
while(*key)
{
h = (h << 4) + *key++;
if((g = (h & 0xF0UL)))
{
h ^= g >> 24;
h ^= g;
}
}
rc = h % 2147483647;
return rc;
}
char *str_replace(char *orig, char *rep, char *with) {
char *result; // the return string
char *ins; // the next insert point
char *tmp; // varies
int len_rep; // length of rep
int len_with; // length of with
int len_front; // distance between rep and end of last rep
int count; // number of replacements
if (!orig)
{
return nullptr;
}
if (!rep)
{
rep = const_cast<char*>("");
}
len_rep = strlen(rep);
if (!with)
{
with = const_cast<char*>("");
}
len_with = strlen(with);
ins = orig;
for ((count = 0); (tmp = strstr(ins, rep)); ++count)
{
ins = tmp + len_rep;
}
tmp = result = static_cast<char*>(malloc(strlen(orig) + (len_with - len_rep) * count + 1));
if (!result)
{
return nullptr;
}
while (count--) {
ins = strstr(orig, rep);
len_front = ins - orig;
tmp = strncpy(tmp, orig, len_front) + len_front;
tmp = strcpy(tmp, with) + len_with;
orig += len_front + len_rep; // move to next "end of rep"
}
strcpy(tmp, orig);
return result;
}
static
void
iRODS_disconnect(
rcComm_t * conn,
const std::string& call_context_for_logging = "")
{
if (conn) {
rcDisconnect(conn);
globus_gfs_log_message(GLOBUS_GFS_LOG_INFO, "iRODS: %s disconnected from iRODS.\n", call_context_for_logging.c_str());
}
}
static
char *
iRODS_getUserName(
char * DN)
{
char *DN_Read = nullptr;
char *iRODS_user_name = nullptr;
char *search = const_cast<char*>(";");
FILE *file = fopen (getenv(IRODS_USER_MAP), "r" );
if ( file != nullptr )
{
char line [ 256 ]; /* or other suitable maximum line size */
while ( fgets ( line, sizeof line, file ) != nullptr ) /* read a line */
{
// Token will point to the part before the ;.
DN_Read = strtok(line, search);
if ( strcmp(DN, DN_Read) == 0)
{
iRODS_user_name = strtok(nullptr, search);
unsigned int len = strlen(iRODS_user_name);
if (iRODS_user_name[len - 1] == '\n')
{
iRODS_user_name[len - 1] = '\0'; //Remove EOF
}
globus_gfs_log_message(GLOBUS_GFS_LOG_INFO, "iRODS: User found in irodsUserMap.conf: DN = %s, iRODS user = %s.\n",
DN, iRODS_user_name);
break;
}
}
fclose ( file );
}
// the username is a string on the stack, return a copy (if it's not nullptr)
return iRODS_user_name == nullptr ? NULL : strdup(iRODS_user_name);
}
static
void
iRODS_getResource(
char * destinationPath)
{
char *path_Read = nullptr;
char *iRODS_res = nullptr;
char *search = const_cast<char*>(";");
FILE *file = fopen (getenv(IRODS_RESOURCE_MAP), "r" );
if ( file != nullptr )
{
char line [ 256 ]; /* or other suitable maximum line size */
while ( fgets ( line, sizeof line, file ) != nullptr ) /* read a line */
{
// Token will point to the part before the ;.
path_Read = strtok(line, search);
if (strncmp(path_Read, destinationPath, strlen(path_Read)) == 0)
{
//found the resource
iRODS_res = strtok(nullptr, search);
unsigned int len = strlen(iRODS_res);
if (iRODS_res[len - 1] == '\n')
{
iRODS_res[len - 1] = '\0'; //Remove EOF
}
globus_gfs_log_message(GLOBUS_GFS_LOG_INFO, "iRODS: found iRODS resource %s for destinationPath %s.\n",
iRODS_res, destinationPath);
/* store the mapping in the global pointers in iRODS_Resource_struct - duplicating the string value.
* Free any previously stored (duplicated) string pointer first!
*/
if (iRODS_Resource_struct.resource != nullptr)
{
free(iRODS_Resource_struct.resource);
iRODS_Resource_struct.resource = nullptr;
};
iRODS_Resource_struct.resource = strdup(iRODS_res);
if (iRODS_Resource_struct.path != nullptr)
{
free(iRODS_Resource_struct.path);
iRODS_Resource_struct.path = nullptr;
}
iRODS_Resource_struct.path = strdup(path_Read);
break;
}
}
fclose ( file );
}
else
{
globus_gfs_log_message(GLOBUS_GFS_LOG_INFO, "iRODS: irodsResourceMap file not found: %s.\n", getenv(IRODS_RESOURCE_MAP));
}
}
static
int
iRODS_l_stat1(
rcComm_t * conn,
globus_gfs_stat_t * stat_out,
char * start_dir)
{
int status;
char * tmp_s;
char * rsrcName;
char * fname;
collHandle_t collHandle;
memset(&collHandle, 0, sizeof(collHandle));
int queryFlags;
queryFlags = DATA_QUERY_FIRST_FG | VERY_LONG_METADATA_FG | NO_TRIM_REPL_FG;
status = rclOpenCollection (conn, start_dir, queryFlags, &collHandle);
if (status >= 0)
{
globus_gfs_log_message(GLOBUS_GFS_LOG_INFO, "iRODS: found collection %s.\n", start_dir);
rsrcName = (char*) start_dir;
memset(stat_out, '\0', sizeof(globus_gfs_stat_t));
fname = rsrcName ? rsrcName : const_cast<char*>("(null)");
tmp_s = strrchr(fname, '/');
if(tmp_s != nullptr) fname = tmp_s + 1;
stat_out->ino = iRODS_l_filename_hash(rsrcName);
stat_out->name = strdup(fname);
stat_out->nlink = 0;
stat_out->uid = getuid();
stat_out->gid = getgid();
stat_out->size = 0;
stat_out->dev = iRODS_l_dev_wrapper++;
stat_out->mode =
S_IFDIR | S_IRUSR | S_IWUSR | S_IXUSR |
S_IROTH | S_IXOTH | S_IRGRP | S_IXGRP;
}
else
{
dataObjInp_t dataObjInp;
rodsObjStat_t *rodsObjStatOut = nullptr;
memset (&dataObjInp, 0, sizeof (dataObjInp));
rstrcpy (dataObjInp.objPath, start_dir, MAX_NAME_LEN);
status = rcObjStat (conn, &dataObjInp, &rodsObjStatOut);
if (status >= 0)
{
globus_gfs_log_message(GLOBUS_GFS_LOG_INFO, "iRODS: found data object %s.\n", start_dir);
memset(stat_out, '\0', sizeof(globus_gfs_stat_t));
stat_out->symlink_target = nullptr;
stat_out->name = strdup(start_dir);
stat_out->nlink = 0;
stat_out->uid = getuid();
stat_out->gid = getgid();
stat_out->size = rodsObjStatOut->objSize;
time_t realTime = atol(rodsObjStatOut->modifyTime);
stat_out->ctime = realTime;
stat_out->mtime = realTime;
stat_out->atime = realTime;
stat_out->dev = iRODS_l_dev_wrapper++;
stat_out->ino = iRODS_l_filename_hash(start_dir);
stat_out->mode = S_IFREG | S_IRUSR | S_IWUSR |
S_IXUSR | S_IROTH | S_IXOTH | S_IRGRP | S_IXGRP;
}
freeRodsObjStat (rodsObjStatOut);
}
return status;
}
static
int
iRODS_l_stat_dir(
globus_gfs_operation_t op,
rcComm_t* conn,
globus_gfs_stat_t ** out_stat,
int * out_count,
char * start_dir,
char * username)
{
int status;
char * tmp_s;
globus_gfs_stat_t * stat_array = nullptr;
int stat_count = 0;
int stat_ndx = 0;
collHandle_t collHandle;
collEnt_t collEnt;
int queryFlags;
int internal_idx;
char * stat_last_data_obj_name = nullptr;
// will hold a copy of the pointer to last file, not a copy of the string
queryFlags = DATA_QUERY_FIRST_FG | VERY_LONG_METADATA_FG | NO_TRIM_REPL_FG;
status = rclOpenCollection (conn, start_dir, queryFlags, &collHandle);
if (status < 0) {
globus_gfs_log_message(GLOBUS_GFS_LOG_INFO,"iRODS: rclOpenCollection of %s error. status = %d", start_dir, status);
return status;
}
time_t last_update_time = time(0);
//We should always be including "." and ".."
//Run this block twice, add "." on iteration 0, ".." on iteration 1
//We skip this for the root directory, as it already provides "."
//internally - and we do not need .. there.
if (strcmp("/", start_dir) !=0 ) {
for (internal_idx = 0; internal_idx<=1; internal_idx++) {
stat_count++;
stat_array = (globus_gfs_stat_t *) globus_realloc(stat_array, stat_count * sizeof(globus_gfs_stat_t));
memset(&stat_array[stat_ndx], '\0', sizeof(globus_gfs_stat_t));
if ( internal_idx == 0 ) {
stat_array[stat_ndx].ino = iRODS_l_filename_hash(start_dir);
stat_array[stat_ndx].name = globus_libc_strdup(".");
} else {
char * parent_dir = strdup(start_dir);
char * last_slash = strrchr(parent_dir,'/');
if (last_slash != nullptr) *last_slash='\0';
stat_array[stat_ndx].ino = iRODS_l_filename_hash(parent_dir);
stat_array[stat_ndx].name = globus_libc_strdup("..");
free(parent_dir);
parent_dir = nullptr;
};
stat_array[stat_ndx].nlink = 0;
stat_array[stat_ndx].uid = getuid();
stat_array[stat_ndx].gid = getgid();
stat_array[stat_ndx].size = 0;
stat_array[stat_ndx].dev = iRODS_l_dev_wrapper++;
stat_array[stat_ndx].mode = S_IFDIR | S_IRUSR | S_IWUSR |
S_IXUSR | S_IROTH | S_IXOTH | S_IRGRP | S_IXGRP;
stat_ndx++;
}
}
while ((status = rclReadCollection (conn, &collHandle, &collEnt)) >= 0)
{
// skip duplicate listings of data objects (additional replicas)
if ( (collEnt.objType == DATA_OBJ_T) &&
(stat_last_data_obj_name != nullptr) &&
(strcmp(stat_last_data_obj_name, collEnt.dataName) == 0) ) continue;
stat_count++;
stat_array = (globus_gfs_stat_t *) globus_realloc(stat_array, stat_count * sizeof(globus_gfs_stat_t));
if (collEnt.objType == DATA_OBJ_T)
{
memset(&stat_array[stat_ndx], '\0', sizeof(globus_gfs_stat_t));
stat_array[stat_ndx].symlink_target = nullptr;
stat_array[stat_ndx].name = globus_libc_strdup(collEnt.dataName);
stat_last_data_obj_name = stat_array[stat_ndx].name;
stat_array[stat_ndx].nlink = 0;
stat_array[stat_ndx].uid = getuid();
//I could get unix uid from iRODS owner, but iRODS owner can not exist as unix user
//so now the file owner is always the user who started the gridftp process
//stat_array[stat_ndx].uid = getpwnam(ownerName)->pw_uid;
stat_array[stat_ndx].gid = getgid();
stat_array[stat_ndx].size = collEnt.dataSize;
time_t realTime = atol(collEnt.modifyTime);
stat_array[stat_ndx].ctime = realTime;
stat_array[stat_ndx].mtime = realTime;
stat_array[stat_ndx].atime = realTime;
stat_array[stat_ndx].dev = iRODS_l_dev_wrapper++;
stat_array[stat_ndx].ino = iRODS_l_filename_hash(collEnt.dataName);
stat_array[stat_ndx].mode = S_IFREG | S_IRUSR | S_IWUSR |
S_IXUSR | S_IROTH | S_IXOTH | S_IRGRP | S_IXGRP;
}
else
{
char * fname;
fname = collEnt.collName ? collEnt.collName : const_cast<char*>("(null)");
tmp_s = strrchr(fname, '/');
if(tmp_s != nullptr) fname = tmp_s + 1;
if(strlen(fname) == 0)
{
//in iRODS empty dir collection is root dir
fname = const_cast<char*>(".");
}
memset(&stat_array[stat_ndx], '\0', sizeof(globus_gfs_stat_t));
stat_array[stat_ndx].ino = iRODS_l_filename_hash(collEnt.collName);
stat_array[stat_ndx].name = strdup(fname);
stat_array[stat_ndx].nlink = 0;
stat_array[stat_ndx].uid = getuid();
stat_array[stat_ndx].gid = getgid();
stat_array[stat_ndx].size = 0;
time_t realTime = atol(collEnt.modifyTime);
stat_array[stat_ndx].ctime = realTime;
stat_array[stat_ndx].mtime = realTime;
stat_array[stat_ndx].dev = iRODS_l_dev_wrapper++;
stat_array[stat_ndx].mode = S_IFDIR | S_IRUSR | S_IWUSR |
S_IXUSR | S_IROTH | S_IXOTH | S_IRGRP | S_IXGRP;
}
stat_ndx++;
// go ahead and send a partial listing if either time or count has expired
time_t now = time(0);
time_t diff = now - last_update_time;
if (diff >= IRODS_LIST_UPDATE_INTERVAL_SECONDS || stat_count >= IRODS_LIST_UPDATE_INTERVAL_COUNT) {
// send partial stat
globus_gfs_log_message(GLOBUS_GFS_LOG_INFO,"iRODS: calling globus_gridftp_server_finished_stat_partial\n");
globus_gridftp_server_finished_stat_partial(op, GLOBUS_SUCCESS, stat_array, stat_count);
// free the names and array
for(int i = 0; i < stat_count; i++)
{
globus_free(stat_array[i].name);
}
globus_free(stat_array);
stat_array = nullptr;
stat_count = 0;
stat_ndx = 0;
last_update_time = now;
}
}
rclCloseCollection (&collHandle);
*out_stat = stat_array;
*out_count = stat_count;
if (status < 0 && status != -808000) {
return (status);
} else {
return (0);
}
}
static
void
globus_l_gfs_iRODS_read_from_net(
globus_l_gfs_iRODS_handle_t * iRODS_handle);
static
void
globus_l_gfs_get_next_read_block(
globus_off_t& offset,
globus_size_t& read_length,
globus_l_gfs_iRODS_handle_t* iRODS_handle);
static
void
globus_l_gfs_net_write_cb(
globus_gfs_operation_t op,
globus_result_t result,
globus_byte_t * buffer,
globus_size_t nbytes,
void * user_arg);
static
void
seek_and_read(
globus_l_gfs_iRODS_handle_t *iRODS_handle,
std::vector<read_write_buffer_t>& irods_read_buffer_vector,
int thr_id,
rcComm_t *conn,
int irods_fd);
/*
* utility function to make errors
*/
static
globus_result_t
globus_l_gfs_iRODS_make_error(
const char * msg,
int status)
{
char *errorSubName;
const char *errorName;
char * err_str;
globus_result_t result;
GlobusGFSName(globus_l_gfs_iRODS_make_error);
errorName = rodsErrorName(status, &errorSubName);
err_str = globus_common_create_string("iRODS: Error: %s. %s: %s, status: %d.\n", msg, errorName, errorSubName, status);
result = GlobusGFSErrorGeneric(err_str);
free(err_str);
err_str = nullptr;
return result;
}
static
globus_bool_t
iRODS_connect_and_login(
globus_l_gfs_iRODS_handle_t * iRODS_handle,
globus_result_t& result,
rcComm_t*& conn,
const std::string& call_context_for_logging = "")
{
result = GLOBUS_SUCCESS;
int status;
rErrMsg_t errMsg;
{
globus_mutex_lock(&iRODS_handle->mutex);
irods::at_scope_exit unlock_mutex{[&iRODS_handle] { globus_mutex_unlock(&iRODS_handle->mutex); }};
if (getenv(IRODS_CONNECT_AS_ADMIN)!=nullptr) {
globus_gfs_log_message(GLOBUS_GFS_LOG_INFO, "iRODS: %s calling _rcConnect(%s,%i,%s,%s, %s, %s)\n",
call_context_for_logging.c_str(), iRODS_handle->hostname, iRODS_handle->port, iRODS_handle->adminUser, iRODS_handle->adminZone,
iRODS_handle->user, iRODS_handle->zone);
conn = _rcConnect(iRODS_handle->hostname, iRODS_handle->port, iRODS_handle->adminUser, iRODS_handle->adminZone,iRODS_handle->user,
iRODS_handle->zone, &errMsg, 0, 0);
} else {
globus_gfs_log_message(GLOBUS_GFS_LOG_INFO, "iRODS: %s calling rcConnect(%s,%i,%s,%s)\n", iRODS_handle->hostname,
call_context_for_logging.c_str(), iRODS_handle->port, iRODS_handle->user, iRODS_handle->zone);
conn = rcConnect(iRODS_handle->hostname, iRODS_handle->port, iRODS_handle->user, iRODS_handle->zone, 0, &errMsg);
}
if (conn == nullptr) {
char *err_str = globus_common_create_string("rcConnect failed:: %s Host: '%s', Port: '%i', UserName '%s', Zone '%s'\n",
errMsg.msg, iRODS_handle->hostname, iRODS_handle->port, iRODS_handle->user, iRODS_handle->zone);
result = GlobusGFSErrorGeneric(err_str);
return false;
}
}
globus_gfs_log_message(GLOBUS_GFS_LOG_INFO, "iRODS: %s connected now logging in.\n", call_context_for_logging.c_str());
status = clientLogin(conn, nullptr, NULL);
if (status != 0) {
globus_gfs_log_message(GLOBUS_GFS_LOG_INFO, "iRODS: %s logging in failed with error %d.\n", call_context_for_logging.c_str(), status);
result = globus_l_gfs_iRODS_make_error("\'clientLogin\' failed.", status);
return false;
}
globus_gfs_log_message(GLOBUS_GFS_LOG_INFO, "iRODS: %s connected.\n", call_context_for_logging.c_str());
return true;
} // end iRODS_connect_and_login
/*************************************************************************
* start
* -----
* This function is called when a new session is initialized, ie a user
* connectes to the server. This hook gives the dsi an oppertunity to
* set internal state that will be threaded through to all other
* function calls associated with this session. And an oppertunity to
* reject the user.
*
* finished_info.info.session.session_arg should be set to an DSI
* defined data structure. This pointer will be passed as the void *
* user_arg parameter to all other interface functions.
*
* NOTE: at nice wrapper function should exist that hides the details
* of the finished_info structure, but it currently does not.
* The DSI developer should jsut follow this template for now
************************************************************************/
extern "C"
void
globus_l_gfs_iRODS_start(
globus_gfs_operation_t op,
globus_gfs_session_info_t * session_info)
{
GlobusGFSName(globus_l_gfs_iRODS_start);
globus_gfs_log_message(GLOBUS_GFS_LOG_INFO, "iRODS: %s called\n", __FUNCTION__);
load_client_api_plugins();
globus_l_gfs_iRODS_handle_t * iRODS_handle;
globus_result_t result = GLOBUS_SUCCESS;
globus_gfs_finished_info_t finished_info;
rodsEnv myRodsEnv;
char *user_name;
char *homeDirPattern;
int status;
memset(&finished_info, '\0', sizeof(globus_gfs_finished_info_t));
finished_info.type = GLOBUS_GFS_OP_SESSION_START;
finished_info.result = GLOBUS_SUCCESS;
finished_info.info.session.username = session_info->username;
iRODS_handle = (globus_l_gfs_iRODS_handle_t *)
globus_malloc(sizeof(globus_l_gfs_iRODS_handle_t));
irods::at_scope_exit cleanup_and_finalize{[op, &result, &finished_info] {
if (result != GLOBUS_SUCCESS)
{
globus_gridftp_server_operation_finished(op, result, &finished_info);
}
}};
if(iRODS_handle == nullptr)
{
result = GlobusGFSErrorGeneric("iRODS: start: malloc failed");
return;
}
finished_info.info.session.session_arg = iRODS_handle;
globus_mutex_init(&iRODS_handle->mutex, nullptr);
globus_fifo_init(&iRODS_handle->rh_q);
status = getRodsEnv(&myRodsEnv);
if (status >= 0) {
// myRodsEnv is a structure on the stack, we must make explicit string copies
iRODS_handle->hostname = strdup(myRodsEnv.rodsHost);
iRODS_handle->port = myRodsEnv.rodsPort;
iRODS_handle->zone = strdup(myRodsEnv.rodsZone);
iRODS_handle->adminUser = strdup(myRodsEnv.rodsUserName);
iRODS_handle->adminZone = strdup(myRodsEnv.rodsZone);
// copy also the default resource if it is set
if (strlen(myRodsEnv.rodsDefResource) > 0 ) {
iRODS_handle->defResource = strdup(myRodsEnv.rodsDefResource);
} else {
iRODS_handle->defResource = nullptr;
}
iRODS_handle->user = iRODS_getUserName(session_info->subject); //iRODS usernmae
user_name = strdup(session_info->username); //Globus user name
if (iRODS_handle->user == nullptr)
{
iRODS_handle->user = strdup(session_info->username);
}
iRODS_handle->original_stat_path = nullptr;
iRODS_handle->resolved_stat_path = nullptr;
iRODS_handle->first_write_done = false;
//Get zone from username if it contains "#"
char delims[] = "#";
char *token = nullptr;
// strtok modifies the input string, so we instead pass it a copy
char *username_to_parse = strdup(iRODS_handle->user);
token = strtok( username_to_parse, delims );
if (token != nullptr ) {
// Second token is the zone
char *token2 = strtok( nullptr, delims );
if ( token2 != nullptr ) {
if (iRODS_handle->zone != nullptr)
{
free(iRODS_handle->zone);
iRODS_handle->zone = nullptr;
}
iRODS_handle->zone = strdup(token2);
globus_gfs_log_message(GLOBUS_GFS_LOG_INFO, "iRODS: found zone '%s' in user name '%s'\n",
iRODS_handle->zone, iRODS_handle->user);
if (iRODS_handle->user != nullptr)
{
free(iRODS_handle->user);
iRODS_handle->zone = nullptr;
}
iRODS_handle->user = strdup(token);
}
}
free(username_to_parse);
username_to_parse = nullptr;
globus_gfs_log_message(GLOBUS_GFS_LOG_INFO, "iRODS: iRODS_handle->hostname = [%s] iRODS_handle->port = [%i] "
"myRodsEnv.rodsUserName = [%s] myRodsEnv.rodsZone = [%s] iRODS_handle->user = [%s] iRODS_handle->zone = [%s]\n",
iRODS_handle->hostname, iRODS_handle->port, iRODS_handle->adminUser, iRODS_handle->adminZone, iRODS_handle->user,
iRODS_handle->zone);
if (!iRODS_connect_and_login(iRODS_handle, result, iRODS_handle->conn, "main thread"))
{
globus_gfs_log_message(GLOBUS_GFS_LOG_INFO, "iRODS: main thread: failed to connect. exiting...\n");
return;
}
homeDirPattern = getenv(HOMEDIR_PATTERN);
if (homeDirPattern == nullptr) { homeDirPattern = const_cast<char*>(DEFAULT_HOMEDIR_PATTERN); }
finished_info.info.session.home_dir = globus_common_create_string(homeDirPattern, iRODS_handle->zone, iRODS_handle->user);
free(user_name);
user_name = nullptr;
// default to 3 threads for read/write
iRODS_handle->number_of_irods_read_write_threads = DEFAULT_NUMBER_OF_IRODS_READ_WRITE_THREADS;
char * number_of_irods_read_write_threads_str = getenv(NUMBER_OF_IRODS_READ_WRITE_THREADS);
if (number_of_irods_read_write_threads_str)
{
try
{
iRODS_handle->number_of_irods_read_write_threads = boost::lexical_cast<int>(number_of_irods_read_write_threads_str);
// set a hard limit in the range [1,10]
if (iRODS_handle->number_of_irods_read_write_threads > MAXIMUM_NUMBER_OF_IRODS_READ_WRITE_THREADS)
{
iRODS_handle->number_of_irods_read_write_threads = MAXIMUM_NUMBER_OF_IRODS_READ_WRITE_THREADS;
}