/
XrdClFS.cc
1737 lines (1536 loc) · 56.4 KB
/
XrdClFS.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
//------------------------------------------------------------------------------
// Copyright (c) 2011-2014 by European Organization for Nuclear Research (CERN)
// Author: Lukasz Janyst <ljanyst@cern.ch>
//------------------------------------------------------------------------------
// This file is part of the XRootD software suite.
//
// XRootD is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// XRootD is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with XRootD. If not, see <http://www.gnu.org/licenses/>.
//
// In applying this licence, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
//------------------------------------------------------------------------------
#include "XrdCl/XrdClFileSystem.hh"
#include "XrdCl/XrdClFileSystemUtils.hh"
#include "XrdCl/XrdClFSExecutor.hh"
#include "XrdCl/XrdClURL.hh"
#include "XrdCl/XrdClLog.hh"
#include "XrdCl/XrdClDefaultEnv.hh"
#include "XrdCl/XrdClConstants.hh"
#include "XrdCl/XrdClUtils.hh"
#include "XrdCl/XrdClCopyProcess.hh"
#include "XrdCl/XrdClFile.hh"
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <iomanip>
#ifdef HAVE_READLINE
#include <readline/readline.h>
#include <readline/history.h>
#endif
using namespace XrdCl;
//------------------------------------------------------------------------------
// Build a path
//------------------------------------------------------------------------------
XRootDStatus BuildPath( std::string &newPath, Env *env,
const std::string &path )
{
if( path.empty() )
return XRootDStatus( stError, errInvalidArgs );
if( path[0] == '/' )
{
newPath = path;
return XRootDStatus();
}
std::string cwd = "/";
env->GetString( "CWD", cwd );
newPath = cwd;
newPath += "/";
newPath += path;
//----------------------------------------------------------------------------
// Collapse the dots
//----------------------------------------------------------------------------
std::list<std::string> pathComponents;
std::list<std::string>::iterator it;
XrdCl::Utils::splitString( pathComponents, newPath, "/" );
newPath = "/";
for( it = pathComponents.begin(); it != pathComponents.end(); )
{
if( *it == "." )
{
it = pathComponents.erase( it );
continue;
}
if( *it == ".." )
{
if( it == pathComponents.begin() )
return XRootDStatus( stError, errInvalidArgs );
std::list<std::string>::iterator it1 = it;
--it1;
it = pathComponents.erase( it1 );
it = pathComponents.erase( it );
continue;
}
++it;
}
newPath = "/";
for( it = pathComponents.begin(); it != pathComponents.end(); ++it )
{
newPath += *it;
newPath += "/";
}
if( newPath.length() > 1 )
newPath.erase( newPath.length()-1, 1 );
return XRootDStatus();
}
//------------------------------------------------------------------------------
// Convert mode string to uint16_t
//------------------------------------------------------------------------------
XRootDStatus ConvertMode( Access::Mode &mode, const std::string &modeStr )
{
if( modeStr.length() != 9 )
return XRootDStatus( stError, errInvalidArgs );
mode = Access::None;
for( int i = 0; i < 3; ++i )
{
if( modeStr[i] == 'r' )
mode |= Access::UR;
else if( modeStr[i] == 'w' )
mode |= Access::UW;
else if( modeStr[i] == 'x' )
mode |= Access::UX;
else if( modeStr[i] != '-' )
return XRootDStatus( stError, errInvalidArgs );
}
for( int i = 3; i < 6; ++i )
{
if( modeStr[i] == 'r' )
mode |= Access::GR;
else if( modeStr[i] == 'w' )
mode |= Access::GW;
else if( modeStr[i] == 'x' )
mode |= Access::GX;
else if( modeStr[i] != '-' )
return XRootDStatus( stError, errInvalidArgs );
}
for( int i = 6; i < 9; ++i )
{
if( modeStr[i] == 'r' )
mode |= Access::OR;
else if( modeStr[i] == 'w' )
mode |= Access::OW;
else if( modeStr[i] == 'x' )
mode |= Access::OX;
else if( modeStr[i] != '-' )
return XRootDStatus( stError, errInvalidArgs );
}
return XRootDStatus();
}
//------------------------------------------------------------------------------
// Change current working directory
//------------------------------------------------------------------------------
XRootDStatus DoCD( FileSystem *fs,
Env *env,
const FSExecutor::CommandParams &args )
{
//----------------------------------------------------------------------------
// Check up the args
//----------------------------------------------------------------------------
Log *log = DefaultEnv::GetLog();
if( args.size() != 2 )
{
log->Error( AppMsg, "Invalid arguments. Expected a path." );
return XRootDStatus( stError, errInvalidArgs );
}
std::string newPath;
if( !BuildPath( newPath, env, args[1] ).IsOK() )
{
log->Error( AppMsg, "Invalid path." );
return XRootDStatus( stError, errInvalidArgs );
}
//----------------------------------------------------------------------------
// Check if the path exist and is not a directory
//----------------------------------------------------------------------------
StatInfo *info;
XRootDStatus st = fs->Stat( newPath, info );
if( !st.IsOK() )
{
log->Error( AppMsg, "Unable to stat the path: %s", st.ToStr().c_str() );
return st;
}
if( !info->TestFlags( StatInfo::IsDir ) )
{
log->Error( AppMsg, "%s is not a directory.", newPath.c_str() );
return XRootDStatus( stError, errInvalidArgs );
}
env->PutString( "CWD", newPath );
delete info;
return XRootDStatus();
}
//------------------------------------------------------------------------------
// List a directory
//------------------------------------------------------------------------------
XRootDStatus DoLS( FileSystem *fs,
Env *env,
const FSExecutor::CommandParams &args )
{
//----------------------------------------------------------------------------
// Check up the args
//----------------------------------------------------------------------------
Log *log = DefaultEnv::GetLog();
uint32_t argc = args.size();
bool stats = false;
bool showUrls = false;
std::string path;
DirListFlags::Flags flags = DirListFlags::Locate;
if( argc > 5 )
{
log->Error( AppMsg, "Too many arguments." );
return XRootDStatus( stError, errInvalidArgs );
}
for( uint32_t i = 1; i < args.size(); ++i )
{
if( args[i] == "-l" )
{
stats = true;
flags |= DirListFlags::Stat;
}
else if( args[i] == "-u" )
showUrls = true;
else
path = args[i];
}
std::string newPath = "/";
if( path.empty() )
env->GetString( "CWD", newPath );
else
{
if( !BuildPath( newPath, env, path ).IsOK() )
{
log->Error( AppMsg, "Invalid arguments. Invalid path." );
return XRootDStatus( stError, errInvalidArgs );
}
}
log->Debug( AppMsg, "Attempting to list: %s", newPath.c_str() );
//----------------------------------------------------------------------------
// Ask for the list
//----------------------------------------------------------------------------
DirectoryList *list;
XRootDStatus st = fs->DirList( newPath, flags, list );
if( !st.IsOK() )
{
log->Error( AppMsg, "Unable to list the path: %s", st.ToStr().c_str() );
return st;
}
if( st.code == suPartial )
{
std::cerr << "[!] Some of the requests failed. The result may be ";
std::cerr << "incomplete." << std::endl;
}
//----------------------------------------------------------------------------
// Print the results
//----------------------------------------------------------------------------
DirectoryList::Iterator it;
for( it = list->Begin(); it != list->End(); ++it )
{
if( stats )
{
StatInfo *info = (*it)->GetStatInfo();
if( !info )
{
std::cout << "---- 0000-00-00 00:00:00 ? ";
}
else
{
if( info->TestFlags( StatInfo::IsDir ) )
std::cout << "d";
else
std::cout << "-";
if( info->TestFlags( StatInfo::IsReadable ) )
std::cout << "r";
else
std::cout << "-";
if( info->TestFlags( StatInfo::IsWritable ) )
std::cout << "w";
else
std::cout << "-";
if( info->TestFlags( StatInfo::XBitSet ) )
std::cout << "x";
else
std::cout << "-";
std::cout << " " << info->GetModTimeAsString();
std::cout << std::setw(12) << info->GetSize() << " ";
}
}
if( showUrls )
std::cout << "root://" << (*it)->GetHostAddress() << "/";
std::cout << list->GetParentName() << (*it)->GetName() << std::endl;
}
delete list;
return XRootDStatus();
}
//------------------------------------------------------------------------------
// Create a directory
//------------------------------------------------------------------------------
XRootDStatus DoMkDir( FileSystem *fs,
Env *env,
const FSExecutor::CommandParams &args )
{
//----------------------------------------------------------------------------
// Check up the args
//----------------------------------------------------------------------------
Log *log = DefaultEnv::GetLog();
uint32_t argc = args.size();
if( argc < 2 || argc > 4 )
{
log->Error( AppMsg, "Too few arguments." );
return XRootDStatus( stError, errInvalidArgs );
}
MkDirFlags::Flags flags = MkDirFlags::None;
Access::Mode mode = Access::None;
std::string modeStr = "rwxr-x---";
std::string path = "";
for( uint32_t i = 1; i < args.size(); ++i )
{
if( args[i] == "-p" )
flags |= MkDirFlags::MakePath;
else if( !args[i].compare( 0, 2, "-m" ) )
modeStr = args[i].substr( 2, 9 );
else
path = args[i];
}
XRootDStatus st = ConvertMode( mode, modeStr );
if( !st.IsOK() )
{
log->Error( AppMsg, "Invalid mode string." );
return st;
}
std::string newPath;
if( !BuildPath( newPath, env, path ).IsOK() )
{
log->Error( AppMsg, "Invalid path." );
return XRootDStatus( stError, errInvalidArgs );
}
//----------------------------------------------------------------------------
// Run the query
//----------------------------------------------------------------------------
st = fs->MkDir( newPath, flags, mode );
if( !st.IsOK() )
{
log->Error( AppMsg, "Unable create directory %s: %s",
newPath.c_str(),
st.ToStr().c_str() );
return st;
}
return XRootDStatus();
}
//------------------------------------------------------------------------------
// Remove a directory
//------------------------------------------------------------------------------
XRootDStatus DoRmDir( FileSystem *query,
Env *env,
const FSExecutor::CommandParams &args )
{
//----------------------------------------------------------------------------
// Check up the args
//----------------------------------------------------------------------------
Log *log = DefaultEnv::GetLog();
uint32_t argc = args.size();
if( argc != 2 )
{
log->Error( AppMsg, "Wrong number of arguments." );
return XRootDStatus( stError, errInvalidArgs );
}
std::string fullPath;
if( !BuildPath( fullPath, env, args[1] ).IsOK() )
{
log->Error( AppMsg, "Invalid path." );
return XRootDStatus( stError, errInvalidArgs );
}
//----------------------------------------------------------------------------
// Run the query
//----------------------------------------------------------------------------
XRootDStatus st = query->RmDir( fullPath );
if( !st.IsOK() )
{
log->Error( AppMsg, "Unable remove directory %s: %s",
fullPath.c_str(),
st.ToStr().c_str() );
return st;
}
return XRootDStatus();
}
//------------------------------------------------------------------------------
// Move a file or directory
//------------------------------------------------------------------------------
XRootDStatus DoMv( FileSystem *fs,
Env *env,
const FSExecutor::CommandParams &args )
{
//----------------------------------------------------------------------------
// Check up the args
//----------------------------------------------------------------------------
Log *log = DefaultEnv::GetLog();
uint32_t argc = args.size();
if( argc != 3 )
{
log->Error( AppMsg, "Wrong number of arguments." );
return XRootDStatus( stError, errInvalidArgs );
}
std::string fullPath1;
if( !BuildPath( fullPath1, env, args[1] ).IsOK() )
{
log->Error( AppMsg, "Invalid source path." );
return XRootDStatus( stError, errInvalidArgs );
}
std::string fullPath2;
if( !BuildPath( fullPath2, env, args[2] ).IsOK() )
{
log->Error( AppMsg, "Invalid destination path." );
return XRootDStatus( stError, errInvalidArgs );
}
//----------------------------------------------------------------------------
// Run the query
//----------------------------------------------------------------------------
XRootDStatus st = fs->Mv( fullPath1, fullPath2 );
if( !st.IsOK() )
{
log->Error( AppMsg, "Unable move %s to %s: %s",
fullPath1.c_str(), fullPath2.c_str(),
st.ToStr().c_str() );
return st;
}
return XRootDStatus();
}
//------------------------------------------------------------------------------
// Remove a file
//------------------------------------------------------------------------------
XRootDStatus DoRm( FileSystem *fs,
Env *env,
const FSExecutor::CommandParams &args )
{
//----------------------------------------------------------------------------
// Check up the args
//----------------------------------------------------------------------------
Log *log = DefaultEnv::GetLog();
uint32_t argc = args.size();
if( argc != 2 )
{
log->Error( AppMsg, "Wrong number of arguments." );
return XRootDStatus( stError, errInvalidArgs );
}
std::string fullPath;
if( !BuildPath( fullPath, env, args[1] ).IsOK() )
{
log->Error( AppMsg, "Invalid path." );
return XRootDStatus( stError, errInvalidArgs );
}
//----------------------------------------------------------------------------
// Run the query
//----------------------------------------------------------------------------
XRootDStatus st = fs->Rm( fullPath );
if( !st.IsOK() )
{
log->Error( AppMsg, "Unable remove %s: %s",
fullPath.c_str(),
st.ToStr().c_str() );
return st;
}
return XRootDStatus();
}
//------------------------------------------------------------------------------
// Truncate a file
//------------------------------------------------------------------------------
XRootDStatus DoTruncate( FileSystem *fs,
Env *env,
const FSExecutor::CommandParams &args )
{
//----------------------------------------------------------------------------
// Check up the args
//----------------------------------------------------------------------------
Log *log = DefaultEnv::GetLog();
uint32_t argc = args.size();
if( argc != 3 )
{
log->Error( AppMsg, "Wrong number of arguments." );
return XRootDStatus( stError, errInvalidArgs );
}
std::string fullPath;
if( !BuildPath( fullPath, env, args[1] ).IsOK() )
{
log->Error( AppMsg, "Invalid path." );
return XRootDStatus( stError, errInvalidArgs );
}
char *result;
uint64_t size = ::strtoll( args[2].c_str(), &result, 0 );
if( *result != 0 )
{
log->Error( AppMsg, "Size parameter needs to be an integer" );
return XRootDStatus( stError, errInvalidArgs );
}
//----------------------------------------------------------------------------
// Run the query
//----------------------------------------------------------------------------
XRootDStatus st = fs->Truncate( fullPath, size );
if( !st.IsOK() )
{
log->Error( AppMsg, "Unable truncate %s: %s",
fullPath.c_str(),
st.ToStr().c_str() );
return st;
}
return XRootDStatus();
}
//------------------------------------------------------------------------------
// Change the access rights to a file
//------------------------------------------------------------------------------
XRootDStatus DoChMod( FileSystem *fs,
Env *env,
const FSExecutor::CommandParams &args )
{
//----------------------------------------------------------------------------
// Check up the args
//----------------------------------------------------------------------------
Log *log = DefaultEnv::GetLog();
uint32_t argc = args.size();
if( argc != 3 )
{
log->Error( AppMsg, "Wrong number of arguments." );
return XRootDStatus( stError, errInvalidArgs );
}
std::string fullPath;
if( !BuildPath( fullPath, env, args[1] ).IsOK() )
{
log->Error( AppMsg, "Invalid path." );
return XRootDStatus( stError, errInvalidArgs );
}
Access::Mode mode = Access::None;
XRootDStatus st = ConvertMode( mode, args[2] );
if( !st.IsOK() )
{
log->Error( AppMsg, "Invalid mode string." );
return st;
}
//----------------------------------------------------------------------------
// Run the query
//----------------------------------------------------------------------------
st = fs->ChMod( fullPath, mode );
if( !st.IsOK() )
{
log->Error( AppMsg, "Unable change mode of %s: %s",
fullPath.c_str(),
st.ToStr().c_str() );
return st;
}
return XRootDStatus();
}
//------------------------------------------------------------------------------
// Locate a path
//------------------------------------------------------------------------------
XRootDStatus DoLocate( FileSystem *fs,
Env *env,
const FSExecutor::CommandParams &args )
{
//----------------------------------------------------------------------------
// Check up the args
//----------------------------------------------------------------------------
Log *log = DefaultEnv::GetLog();
uint32_t argc = args.size();
if( argc > 4 )
{
log->Error( AppMsg, "Wrong number of arguments." );
return XRootDStatus( stError, errInvalidArgs );
}
OpenFlags::Flags flags = OpenFlags::None;
std::string path;
bool hasPath = false;
bool doDeepLocate = false;
for( uint32_t i = 1; i < argc; ++i )
{
if( args[i] == "-n" )
flags |= OpenFlags::NoWait;
else if( args[i] == "-r" )
flags |= OpenFlags::Refresh;
else if( args[i] == "-m" || args[i] == "-h" )
flags |= OpenFlags::PrefName;
else if( args[i] == "-i" )
flags |= OpenFlags::Force;
else if( args[i] == "-d" )
doDeepLocate = true;
else if( !hasPath )
{
path = args[i];
hasPath = true;
}
else
{
log->Error( AppMsg, "Invalid argument: %s.", args[i].c_str() );
return XRootDStatus( stError, errInvalidArgs );
}
}
std::string fullPath;
if( path[0] == '*' )
fullPath = path;
else
{
if( !BuildPath( fullPath, env, path ).IsOK() )
{
log->Error( AppMsg, "Invalid path." );
return XRootDStatus( stError, errInvalidArgs );
}
}
//----------------------------------------------------------------------------
// Run the query
//----------------------------------------------------------------------------
LocationInfo *info = 0;
XRootDStatus st;
if( doDeepLocate )
st = fs->DeepLocate( fullPath, flags, info );
else
st = fs->Locate( fullPath, flags, info );
if( !st.IsOK() )
{
log->Error( AppMsg, "Unable locate %s: %s",
fullPath.c_str(),
st.ToStr().c_str() );
return st;
}
//----------------------------------------------------------------------------
// Print the result
//----------------------------------------------------------------------------
if( st.code == suPartial )
{
std::cerr << "[!] Some of the requests failed. The result may be ";
std::cerr << "incomplete." << std::endl;
}
LocationInfo::Iterator it;
for( it = info->Begin(); it != info->End(); ++it )
{
std::cout << it->GetAddress() << " ";
switch( it->GetType() )
{
case LocationInfo::ManagerOnline:
std::cout << "Manager ";
break;
case LocationInfo::ManagerPending:
std::cout << "ManagerPending ";
break;
case LocationInfo::ServerOnline:
std::cout << "Server ";
break;
case LocationInfo::ServerPending:
std::cout << "ServerPending ";
break;
default:
std::cout << "Unknown ";
};
switch( it->GetAccessType() )
{
case LocationInfo::Read:
std::cout << "Read";
break;
case LocationInfo::ReadWrite:
std::cout << "ReadWrite ";
break;
default:
std::cout << "Unknown ";
};
std::cout << std::endl;
}
delete info;
return XRootDStatus();
}
//------------------------------------------------------------------------------
// Process stat query
//------------------------------------------------------------------------------
XRootDStatus ProcessStatQuery( StatInfo *info, const std::string &query )
{
Log *log = DefaultEnv::GetLog();
//----------------------------------------------------------------------------
// Process the query
//----------------------------------------------------------------------------
bool isOrQuery = false;
bool status = true;
if( query.find( '|' ) != std::string::npos )
{
isOrQuery = true;
status = false;
}
std::vector<std::string> queryFlags;
if( isOrQuery )
Utils::splitString( queryFlags, query, "|" );
else
Utils::splitString( queryFlags, query, "&" );
//----------------------------------------------------------------------------
// Initialize flag translation map and check the input flags
//----------------------------------------------------------------------------
std::map<std::string, StatInfo::Flags> flagMap;
flagMap["XBitSet"] = StatInfo::XBitSet;
flagMap["IsDir"] = StatInfo::IsDir;
flagMap["Other"] = StatInfo::Other;
flagMap["Offline"] = StatInfo::Offline;
flagMap["POSCPending"] = StatInfo::POSCPending;
flagMap["IsReadable"] = StatInfo::IsReadable;
flagMap["IsWritable"] = StatInfo::IsWritable;
flagMap["BackUpExists"] = StatInfo::BackUpExists;
std::vector<std::string>::iterator it;
for( it = queryFlags.begin(); it != queryFlags.end(); ++it )
if( flagMap.find( *it ) == flagMap.end() )
{
log->Error( AppMsg, "Flag '%s' is not recognized.", it->c_str() );
return XRootDStatus( stError, errInvalidArgs );
}
//----------------------------------------------------------------------------
// Process the query
//----------------------------------------------------------------------------
if( isOrQuery )
{
for( it = queryFlags.begin(); it != queryFlags.end(); ++it )
if( info->TestFlags( flagMap[*it] ) )
return XRootDStatus();
}
else
{
for( it = queryFlags.begin(); it != queryFlags.end(); ++it )
if( !info->TestFlags( flagMap[*it] ) )
return XRootDStatus( stError, errResponseNegative );
}
if( status )
return XRootDStatus();
return XRootDStatus( stError, errResponseNegative );
}
//------------------------------------------------------------------------------
// Stat a path
//------------------------------------------------------------------------------
XRootDStatus DoStat( FileSystem *fs,
Env *env,
const FSExecutor::CommandParams &args )
{
//----------------------------------------------------------------------------
// Check up the args
//----------------------------------------------------------------------------
Log *log = DefaultEnv::GetLog();
uint32_t argc = args.size();
if( argc != 2 && argc != 4 )
{
log->Error( AppMsg, "Wrong number of arguments." );
return XRootDStatus( stError, errInvalidArgs );
}
std::string path;
std::string query;
for( uint32_t i = 1; i < args.size(); ++i )
{
if( args[i] == "-q" )
{
if( i < args.size()-1 )
{
query = args[i+1];
++i;
}
else
{
log->Error( AppMsg, "Parameter '-q' requires an argument." );
return XRootDStatus( stError, errInvalidArgs );
}
}
else
path = args[i];
}
std::string fullPath;
if( !BuildPath( fullPath, env, path ).IsOK() )
{
log->Error( AppMsg, "Invalid path." );
return XRootDStatus( stError, errInvalidArgs );
}
//----------------------------------------------------------------------------
// Run the query
//----------------------------------------------------------------------------
StatInfo *info = 0;
XRootDStatus st = fs->Stat( fullPath, info );
if( !st.IsOK() )
{
log->Error( AppMsg, "Unable stat %s: %s",
fullPath.c_str(),
st.ToStr().c_str() );
return st;
}
//----------------------------------------------------------------------------
// Print the result
//----------------------------------------------------------------------------
std::string flags;
if( info->TestFlags( StatInfo::XBitSet ) )
flags += "XBitSet|";
if( info->TestFlags( StatInfo::IsDir ) )
flags += "IsDir|";
if( info->TestFlags( StatInfo::Other ) )
flags += "Other|";
if( info->TestFlags( StatInfo::Offline ) )
flags += "Offline|";
if( info->TestFlags( StatInfo::POSCPending ) )
flags += "POSCPending|";
if( info->TestFlags( StatInfo::IsReadable ) )
flags += "IsReadable|";
if( info->TestFlags( StatInfo::IsWritable ) )
flags += "IsWritable|";
if( info->TestFlags( StatInfo::BackUpExists ) )
flags += "BackUpExists|";
if( !flags.empty() )
flags.erase( flags.length()-1, 1 );
std::cout << "Path: " << fullPath << std::endl;
std::cout << "Id: " << info->GetId() << std::endl;
std::cout << "Size: " << info->GetSize() << std::endl;
std::cout << "Flags: " << info->GetFlags() << " (" << flags << ")";
std::cout << std::endl;
if( query.length() != 0 )
{
st = ProcessStatQuery( info, query );
std::cout << "Query: " << query << " " << std::endl;
}
delete info;
return st;
}
//------------------------------------------------------------------------------
// Stat a VFS
//------------------------------------------------------------------------------
XRootDStatus DoStatVFS( FileSystem *fs,
Env *env,
const FSExecutor::CommandParams &args )
{
//----------------------------------------------------------------------------
// Check up the args
//----------------------------------------------------------------------------
Log *log = DefaultEnv::GetLog();
uint32_t argc = args.size();
if( argc != 2 )
{
log->Error( AppMsg, "Wrong number of arguments." );
return XRootDStatus( stError, errInvalidArgs );
}
std::string fullPath;
if( !BuildPath( fullPath, env, args[1] ).IsOK() )
{
log->Error( AppMsg, "Invalid path." );
return XRootDStatus( stError, errInvalidArgs );
}
//----------------------------------------------------------------------------
// Run the query
//----------------------------------------------------------------------------
StatInfoVFS *info = 0;
XRootDStatus st = fs->StatVFS( fullPath, info );
if( !st.IsOK() )
{
log->Error( AppMsg, "Unable stat VFS at %s: %s",
fullPath.c_str(),
st.ToStr().c_str() );
return st;
}
//----------------------------------------------------------------------------
// Print the result
//----------------------------------------------------------------------------
std::cout << "Path: ";
std::cout << fullPath << std::endl;
std::cout << "Nodes with RW space: ";
std::cout << info->GetNodesRW() << std::endl;
std::cout << "Size of RW space (MB): ";
std::cout << info->GetFreeRW() << std::endl;
std::cout << "Utilization of RW space (%): ";
std::cout << (uint16_t)info->GetUtilizationRW() << std::endl;
std::cout << "Nodes with staging space: ";
std::cout << info->GetNodesStaging() << std::endl;
std::cout << "Size of staging space (MB): ";
std::cout << info->GetFreeStaging() << std::endl;
std::cout << "Utilization of staging space (%): ";
std::cout << (uint16_t)info->GetUtilizationStaging() << std::endl;
delete info;
return XRootDStatus();
}
//------------------------------------------------------------------------------
// Query the server
//------------------------------------------------------------------------------
XRootDStatus DoQuery( FileSystem *fs,
Env *env,
const FSExecutor::CommandParams &args )
{
//----------------------------------------------------------------------------
// Check up the args
//----------------------------------------------------------------------------
Log *log = DefaultEnv::GetLog();
uint32_t argc = args.size();
if( argc != 3 )
{
log->Error( AppMsg, "Wrong number of arguments." );
return XRootDStatus( stError, errInvalidArgs );
}
QueryCode::Code qCode;
if( args[1] == "config" )
qCode = QueryCode::Config;
else if( args[1] == "checksumcancel" )
qCode = QueryCode::ChecksumCancel;
else if( args[1] == "checksum" )
qCode = QueryCode::Checksum;
else if( args[1] == "opaque" )
qCode = QueryCode::Opaque;
else if( args[1] == "opaqueFile" )
qCode = QueryCode::OpaqueFile;
else if( args[1] == "prepare" )
qCode = QueryCode::Prepare;
else if( args[1] == "space" )
qCode = QueryCode::Space;
else if( args[1] == "stats" )
qCode = QueryCode::Stats;
else if( args[1] == "xattr" )
qCode = QueryCode::XAttr;
else
{