XRootD
XrdClFS.cc
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // Copyright (c) 2011-2014 by European Organization for Nuclear Research (CERN)
3 // Author: Lukasz Janyst <ljanyst@cern.ch>
4 //------------------------------------------------------------------------------
5 // This file is part of the XRootD software suite.
6 //
7 // XRootD is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Lesser General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // XRootD is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with XRootD. If not, see <http://www.gnu.org/licenses/>.
19 //
20 // In applying this licence, CERN does not waive the privileges and immunities
21 // granted to it by virtue of its status as an Intergovernmental Organization
22 // or submit itself to any jurisdiction.
23 //------------------------------------------------------------------------------
24 
25 #include "XrdCl/XrdClFileSystem.hh"
27 #include "XrdCl/XrdClFSExecutor.hh"
28 #include "XrdCl/XrdClURL.hh"
29 #include "XrdCl/XrdClLog.hh"
30 #include "XrdCl/XrdClDefaultEnv.hh"
31 #include "XrdCl/XrdClConstants.hh"
32 #include "XrdCl/XrdClUtils.hh"
34 #include "XrdCl/XrdClFile.hh"
38 #include "XrdSys/XrdSysE2T.hh"
39 
40 #include <cstdlib>
41 #include <cstdio>
42 #include <iostream>
43 #include <iomanip>
44 #include <cmath>
45 
46 #ifdef HAVE_READLINE
47 #include <readline/readline.h>
48 #include <readline/history.h>
49 #endif
50 
51 using namespace XrdCl;
52 
53 //------------------------------------------------------------------------------
54 // Build a path
55 //------------------------------------------------------------------------------
56 XRootDStatus BuildPath( std::string &newPath, Env *env,
57  const std::string &path )
58 {
59  if( path.empty() )
61 
62  int noCwd = 0;
63  env->GetInt( "NoCWD", noCwd );
64 
65  if( path[0] == '/' || noCwd )
66  {
67  newPath = path;
68  return XRootDStatus();
69  }
70 
71  std::string cwd = "/";
72  env->GetString( "CWD", cwd );
73  newPath = cwd;
74  newPath += "/";
75  newPath += path;
76 
77  //----------------------------------------------------------------------------
78  // Collapse the dots
79  //----------------------------------------------------------------------------
80  std::list<std::string> pathComponents;
81  std::list<std::string>::iterator it;
82  XrdCl::Utils::splitString( pathComponents, newPath, "/" );
83  newPath = "/";
84  for( it = pathComponents.begin(); it != pathComponents.end(); )
85  {
86  if( *it == "." )
87  {
88  it = pathComponents.erase( it );
89  continue;
90  }
91 
92  if( *it == ".." )
93  {
94  if( it == pathComponents.begin() )
96  std::list<std::string>::iterator it1 = it;
97  --it1;
98  it = pathComponents.erase( it1 );
99  it = pathComponents.erase( it );
100  continue;
101  }
102  ++it;
103  }
104 
105  newPath = "/";
106  for( it = pathComponents.begin(); it != pathComponents.end(); ++it )
107  {
108  newPath += *it;
109  newPath += "/";
110  }
111  if( newPath.length() > 1 )
112  newPath.erase( newPath.length()-1, 1 );
113 
114  return XRootDStatus();
115 }
116 
117 //------------------------------------------------------------------------------
118 // Convert mode string to uint16_t
119 //------------------------------------------------------------------------------
120 XRootDStatus ConvertMode( Access::Mode &mode, const std::string &modeStr )
121 {
122  if( modeStr.length() != 9 )
124 
125  mode = Access::None;
126  for( int i = 0; i < 3; ++i )
127  {
128  if( modeStr[i] == 'r' )
129  mode |= Access::UR;
130  else if( modeStr[i] == 'w' )
131  mode |= Access::UW;
132  else if( modeStr[i] == 'x' )
133  mode |= Access::UX;
134  else if( modeStr[i] != '-' )
136  }
137  for( int i = 3; i < 6; ++i )
138  {
139  if( modeStr[i] == 'r' )
140  mode |= Access::GR;
141  else if( modeStr[i] == 'w' )
142  mode |= Access::GW;
143  else if( modeStr[i] == 'x' )
144  mode |= Access::GX;
145  else if( modeStr[i] != '-' )
147  }
148  for( int i = 6; i < 9; ++i )
149  {
150  if( modeStr[i] == 'r' )
151  mode |= Access::OR;
152  else if( modeStr[i] == 'w' )
153  mode |= Access::OW;
154  else if( modeStr[i] == 'x' )
155  mode |= Access::OX;
156  else if( modeStr[i] != '-' )
158  }
159  return XRootDStatus();
160 }
161 
162 //------------------------------------------------------------------------------
163 // Perform a cache operation
164 //------------------------------------------------------------------------------
166  Env *env,
167  const FSExecutor::CommandParams &args )
168 {
169  //----------------------------------------------------------------------------
170  // Check up the args
171  //----------------------------------------------------------------------------
172  Log *log = DefaultEnv::GetLog();
173  uint32_t argc = args.size();
174 
175  if( argc != 3 )
176  {
177  log->Error( AppMsg, "Wrong number of arguments." );
178  return XRootDStatus( stError, errInvalidArgs, 0,
179  "Wrong number of arguments." );
180  }
181 
182  if( args[1] != "evict" && args[1] != "fevict")
183  {
184  log->Error( AppMsg, "Invalid cache operation." );
185  return XRootDStatus( stError, errInvalidArgs, 0, "Invalid cache operation." );
186  }
187 
188  std::string fullPath;
189  if( !BuildPath( fullPath, env, args[2] ).IsOK() )
190  {
191  log->Error( AppMsg, "Invalid cache path." );
192  return XRootDStatus( stError, errInvalidArgs, 0, "Invalid cache path." );
193  }
194 
195  //----------------------------------------------------------------------------
196  // Create the command
197  //----------------------------------------------------------------------------
198  std::string cmd = args[1];
199  cmd.append(" ");
200  cmd.append(fullPath);
201 
202  //----------------------------------------------------------------------------
203  // Run the operation
204  //----------------------------------------------------------------------------
205  Buffer *response = 0;
206  XRootDStatus st = fs->SendCache( cmd, response );
207  if( !st.IsOK() )
208  {
209  log->Error( AppMsg, "Unable set cache %s: %s",
210  fullPath.c_str(),
211  st.ToStr().c_str() );
212  return st;
213  }
214 
215  if( response )
216  {
217  std::cout << response->ToString() << '\n';
218  }
219 
220  delete response;
221 
222  return XRootDStatus();
223 }
224 //------------------------------------------------------------------------------
225 // Change current working directory
226 //------------------------------------------------------------------------------
228  Env *env,
229  const FSExecutor::CommandParams &args )
230 {
231  //----------------------------------------------------------------------------
232  // Check up the args
233  //----------------------------------------------------------------------------
234  Log *log = DefaultEnv::GetLog();
235  if( args.size() != 2 )
236  {
237  log->Error( AppMsg, "Invalid arguments. Expected a path." );
239  }
240 
241  //----------------------------------------------------------------------------
242  // cd excludes NoCWD
243  //----------------------------------------------------------------------------
244  env->PutInt( "NoCWD", 0 );
245 
246  std::string newPath;
247  if( !BuildPath( newPath, env, args[1] ).IsOK() )
248  {
249  log->Error( AppMsg, "Invalid path." );
251  }
252 
253  //----------------------------------------------------------------------------
254  // Check if the path exist and is not a directory
255  //----------------------------------------------------------------------------
256  StatInfo *info;
257  XRootDStatus st = fs->Stat( newPath, info );
258  if( !st.IsOK() )
259  {
260  log->Error( AppMsg, "Unable to stat the path: %s", st.ToStr().c_str() );
261  return st;
262  }
263 
264  if( !info->TestFlags( StatInfo::IsDir ) )
265  {
266  log->Error( AppMsg, "%s is not a directory.", newPath.c_str() );
268  }
269 
270  env->PutString( "CWD", newPath );
271  delete info;
272  return XRootDStatus();
273 }
274 
275 //------------------------------------------------------------------------------
276 // Helper function to calculate number of digits in a number
277 //------------------------------------------------------------------------------
278 uint32_t nbDigits( uint64_t nb )
279 {
280  if( nb == 0 ) return 1;
281  return uint32_t( log10( double(nb) ) + 1);
282 }
283 
284 std::string getSizeStr(uint64_t size, bool human, uint64_t base) {
285  std::ostringstream oss;
286  if (!human) {
287  oss << size;
288  } else {
289  oss << XrdOucUtils::genHumanSize(size,base);
290  }
291  return oss.str();
292 }
293 
294 void PrintDirListStatInfo( StatInfo *info, bool hascks = false, uint32_t ownerwidth = 0, uint32_t groupwidth = 0, uint32_t sizewidth = 0, bool human = false, uint64_t base = 1000 )
295 {
296  if( info->ExtendedFormat() )
297  {
298  if( info->TestFlags( StatInfo::IsDir ) )
299  std::cout << "d";
300  else
301  std::cout << "-";
302  std::cout << info->GetModeAsOctString();
303 
304  std::cout << " " << std::setw( ownerwidth ) << info->GetOwner();
305  std::cout << " " << std::setw( groupwidth ) << info->GetGroup();
306  std::cout << " " << std::setw( sizewidth ) << getSizeStr(info->GetSize(),human, base);
307  if( hascks && info->HasChecksum() )
308  std::cout << " " << std::setw( sizewidth ) << info->GetChecksum();
309  std::cout << " " << info->GetModTimeAsString() << " ";
310  }
311  else
312  {
313  if( info->TestFlags( StatInfo::IsDir ) )
314  std::cout << "d";
315  else
316  std::cout << "-";
317 
318  if( info->TestFlags( StatInfo::IsReadable ) )
319  std::cout << "r";
320  else
321  std::cout << "-";
322 
323  if( info->TestFlags( StatInfo::IsWritable ) )
324  std::cout << "w";
325  else
326  std::cout << "-";
327 
328  if( info->TestFlags( StatInfo::XBitSet ) )
329  std::cout << "x";
330  else
331  std::cout << "-";
332 
333  std::cout << " " << info->GetModTimeAsString();
334 
335  uint64_t size = info->GetSize();
336  std::string displaySize = getSizeStr(size,human,base);
337  int width = displaySize.size() + 2;
338  if (width < 12) {
339  width = 12;
340  }
341  std::cout << std::setw( width ) << displaySize << " ";
342  }
343 }
344 
345 //------------------------------------------------------------------------------
346 // List a directory
347 //------------------------------------------------------------------------------
349  Env *env,
350  const FSExecutor::CommandParams &args )
351 {
352  //----------------------------------------------------------------------------
353  // Check up the args
354  //----------------------------------------------------------------------------
355  Log *log = DefaultEnv::GetLog();
356  uint32_t argc = args.size();
357  bool stats = false;
358  bool showUrls = false;
359  bool hascks = false;
360  bool human = false;
361  uint64_t base = 1024;
362  std::string path;
364 
365  if( argc > 6 )
366  {
367  log->Error( AppMsg, "Too many arguments." );
369  }
370 
371  for( uint32_t i = 1; i < args.size(); ++i )
372  {
373  if( args[i] == "-l" )
374  {
375  stats = true;
376  flags |= DirListFlags::Stat;
377  }
378  else if( args[i] == "-u" )
379  showUrls = true;
380  else if( args[i] == "-R" )
381  {
382  flags |= DirListFlags::Recursive;
383  }
384  else if( args[i] == "-D" )
385  {
386  // show duplicates
387  flags &= ~DirListFlags::Merge;
388  }
389  else if( args[i] == "-Z" )
390  {
391  // check if file is a ZIP archive if yes list content
392  flags |= DirListFlags::Zip;
393  }
394  else if( args[i] == "-C" )
395  {
396  // query checksum for each entry in the directory
397  hascks = true;
398  stats = true;
399  flags |= DirListFlags::Cksm;
400  }
401  else if ( args [i] == "-h" )
402  {
403  human = true;
404  }
405  else
406  path = args[i];
407  }
408 
409  if( showUrls )
410  // we don't merge the duplicate entries
411  // in case we print the full URL
412  flags &= ~DirListFlags::Merge;
413 
414  std::string newPath = "/";
415  if( path.empty() )
416  env->GetString( "CWD", newPath );
417  else
418  {
419  if( !BuildPath( newPath, env, path ).IsOK() )
420  {
421  log->Error( AppMsg, "Invalid arguments. Invalid path." );
423  }
424  }
425 
426  //----------------------------------------------------------------------------
427  // Stat the entry so we know if it is a file or a directory
428  //----------------------------------------------------------------------------
429  log->Debug( AppMsg, "Attempting to stat: %s", newPath.c_str() );
430 
431  StatInfo *info = 0;
432  XRootDStatus st = fs->Stat( newPath, info );
433  std::unique_ptr<StatInfo> ptr( info );
434  if( !st.IsOK() )
435  {
436  log->Error( AppMsg, "Unable to stat the path: %s", st.ToStr().c_str() );
437  return st;
438  }
439 
440  if( !info->TestFlags( StatInfo::IsDir ) &&
441  !( flags & DirListFlags::Zip ) )
442  {
443  if( stats )
444  PrintDirListStatInfo( info, false, 0, 0, 0, human, base );
445 
446  if( showUrls )
447  {
448  std::string url;
449  fs->GetProperty( "LastURL", url );
450  std::cout << url;
451  }
452  std::cout << newPath << std::endl;
453  return XRootDStatus();
454  }
455 
456 
457  //----------------------------------------------------------------------------
458  // Ask for the list
459  //----------------------------------------------------------------------------
460  log->Debug( AppMsg, "Attempting to list: %s", newPath.c_str() );
461 
462  DirectoryList *list;
463  st = fs->DirList( newPath, flags, list );
464  if( !st.IsOK() )
465  {
466  log->Error( AppMsg, "Unable to list the path: %s", st.ToStr().c_str() );
467  return st;
468  }
469 
470  if( st.code == suPartial )
471  {
472  std::cerr << "[!] Some of the requests failed. The result may be ";
473  std::cerr << "incomplete." << std::endl;
474  }
475 
476  uint32_t ownerwidth = 0, groupwidth = 0, sizewidth = 0, ckswidth = 0;
478  for( it = list->Begin(); it != list->End() && stats; ++it )
479  {
480  StatInfo *info = (*it)->GetStatInfo();
481 
482  std::string size = getSizeStr(info->GetSize(),human,base);
483  uint32_t sizeWidthComp;
484  if (human) {
485  sizeWidthComp = size.size();
486  } else {
487  sizeWidthComp = nbDigits( info->GetSize());
488  }
489 
490  if( ownerwidth < info->GetOwner().size() )
491  ownerwidth = info->GetOwner().size();
492  if( groupwidth < info->GetGroup().size() )
493  groupwidth = info->GetGroup().size();
494  if( sizewidth < sizeWidthComp )
495  sizewidth = sizeWidthComp;
496  if( ckswidth < info->GetChecksum().size() )
497  ckswidth = info->GetChecksum().size();
498  }
499 
500  //----------------------------------------------------------------------------
501  // Print the results
502  //----------------------------------------------------------------------------
503  for( it = list->Begin(); it != list->End(); ++it )
504  {
505  if( stats )
506  {
507  StatInfo *info = (*it)->GetStatInfo();
508  if( !info )
509  std::cout << "---- 0000-00-00 00:00:00 ? ";
510  else
511  PrintDirListStatInfo( info, hascks, ownerwidth, groupwidth, sizewidth, human, base );
512  }
513  if( showUrls )
514  std::cout << "root://" << (*it)->GetHostAddress() << "/";
515  std::cout << list->GetParentName() << (*it)->GetName() << std::endl;
516  }
517  delete list;
518  return XRootDStatus();
519 }
520 
521 //------------------------------------------------------------------------------
522 // Create a directory
523 //------------------------------------------------------------------------------
525  Env *env,
526  const FSExecutor::CommandParams &args )
527 {
528  //----------------------------------------------------------------------------
529  // Check up the args
530  //----------------------------------------------------------------------------
531  Log *log = DefaultEnv::GetLog();
532  uint32_t argc = args.size();
533 
534  if( argc < 2 || argc > 4 )
535  {
536  log->Error( AppMsg, "Too few arguments." );
538  }
539 
541  Access::Mode mode = Access::None;
542  std::string modeStr = "rwxr-x---";
543  std::string path = "";
544 
545  for( uint32_t i = 1; i < args.size(); ++i )
546  {
547  if( args[i] == "-p" )
548  flags |= MkDirFlags::MakePath;
549  else if( !args[i].compare( 0, 2, "-m" ) )
550  modeStr = args[i].substr( 2, 9 );
551  else
552  path = args[i];
553  }
554 
555  XRootDStatus st = ConvertMode( mode, modeStr );
556  if( !st.IsOK() )
557  {
558  log->Error( AppMsg, "Invalid mode string." );
559  return st;
560  }
561 
562  std::string newPath;
563  if( !BuildPath( newPath, env, path ).IsOK() )
564  {
565  log->Error( AppMsg, "Invalid path." );
567  }
568 
569  //----------------------------------------------------------------------------
570  // Run the query
571  //----------------------------------------------------------------------------
572  st = fs->MkDir( newPath, flags, mode );
573  if( !st.IsOK() )
574  {
575  log->Error( AppMsg, "Unable create directory %s: %s",
576  newPath.c_str(),
577  st.ToStr().c_str() );
578  return st;
579  }
580 
581  return XRootDStatus();
582 }
583 
584 //------------------------------------------------------------------------------
585 // Remove a directory
586 //------------------------------------------------------------------------------
588  Env *env,
589  const FSExecutor::CommandParams &args )
590 {
591  //----------------------------------------------------------------------------
592  // Check up the args
593  //----------------------------------------------------------------------------
594  Log *log = DefaultEnv::GetLog();
595  uint32_t argc = args.size();
596 
597  if( argc != 2 )
598  {
599  log->Error( AppMsg, "Wrong number of arguments." );
601  }
602 
603  std::string fullPath;
604  if( !BuildPath( fullPath, env, args[1] ).IsOK() )
605  {
606  log->Error( AppMsg, "Invalid path." );
608  }
609 
610  //----------------------------------------------------------------------------
611  // Run the query
612  //----------------------------------------------------------------------------
613  XRootDStatus st = query->RmDir( fullPath );
614  if( !st.IsOK() )
615  {
616  log->Error( AppMsg, "Unable remove directory %s: %s",
617  fullPath.c_str(),
618  st.ToStr().c_str() );
619  return st;
620  }
621 
622  return XRootDStatus();
623 }
624 
625 //------------------------------------------------------------------------------
626 // Move a file or directory
627 //------------------------------------------------------------------------------
629  Env *env,
630  const FSExecutor::CommandParams &args )
631 {
632  //----------------------------------------------------------------------------
633  // Check up the args
634  //----------------------------------------------------------------------------
635  Log *log = DefaultEnv::GetLog();
636  uint32_t argc = args.size();
637 
638  if( argc != 3 )
639  {
640  log->Error( AppMsg, "Wrong number of arguments." );
642  }
643 
644  std::string fullPath1;
645  if( !BuildPath( fullPath1, env, args[1] ).IsOK() )
646  {
647  log->Error( AppMsg, "Invalid source path." );
649  }
650 
651  std::string fullPath2;
652  if( !BuildPath( fullPath2, env, args[2] ).IsOK() )
653  {
654  log->Error( AppMsg, "Invalid destination path." );
656  }
657 
658  if( is_subdirectory(fullPath1, fullPath2) )
659  return XRootDStatus( stError, errInvalidArgs, 0,
660  "cannot move directory to a subdirectory of itself." );
661 
662  //----------------------------------------------------------------------------
663  // Run the query
664  //----------------------------------------------------------------------------
665  XRootDStatus st = fs->Mv( fullPath1, fullPath2 );
666  if( !st.IsOK() )
667  {
668  log->Error( AppMsg, "Unable move %s to %s: %s",
669  fullPath1.c_str(), fullPath2.c_str(),
670  st.ToStr().c_str() );
671  return st;
672  }
673 
674  return XRootDStatus();
675 }
676 
677 //------------------------------------------------------------------------------
678 // Remove a file
679 //------------------------------------------------------------------------------
681  Env *env,
682  const FSExecutor::CommandParams &args )
683 {
684  //----------------------------------------------------------------------------
685  // Check up the args
686  //----------------------------------------------------------------------------
687  Log *log = DefaultEnv::GetLog();
688  uint32_t argc = args.size();
689 
690  if( argc < 2 )
691  {
692  log->Error( AppMsg, "Wrong number of arguments." );
694  }
695 
696  struct print_t
697  {
698  void print( const std::string &msg )
699  {
700  std::unique_lock<std::mutex> lck( mtx );
701  std::cout << msg << '\n';
702  }
703  std::mutex mtx;
704  };
705  std::shared_ptr<print_t> print;
706  if( argc - 1 > 0 )
707  print = std::make_shared<print_t>();
708 
709  std::vector<Pipeline> rms;
710  rms.reserve( argc - 1 );
711  for( size_t i = 1; i < argc; ++i )
712  {
713  std::string fullPath;
714  if( !BuildPath( fullPath, env, args[i] ).IsOK() )
715  {
716  log->Error( AppMsg, "Invalid path: %s", fullPath.c_str() );
718  }
719  rms.emplace_back( Rm( fs, fullPath ) >>
720  [log, fullPath, print]( XRootDStatus &st )
721  {
722  if( !st.IsOK() )
723  {
724  log->Error( AppMsg, "Unable remove %s: %s",
725  fullPath.c_str(),
726  st.ToStr().c_str() );
727  }
728  if( print )
729  {
730  print->print( "rm " + fullPath + " : " + st.ToString() );
731  }
732  } );
733  }
734 
735  //----------------------------------------------------------------------------
736  // Run the query:
737  // Parallel() will take the vector of Pipeline by reference and empty the
738  // vector, so rms.size() will change after the call.
739  //----------------------------------------------------------------------------
740  const size_t rs = rms.size();
741  XRootDStatus st = WaitFor( Parallel( rms ).AtLeast( rs ) );
742  if( !st.IsOK() )
743  return st;
744 
745  return XRootDStatus();
746 }
747 
748 //------------------------------------------------------------------------------
749 // Truncate a file
750 //------------------------------------------------------------------------------
752  Env *env,
753  const FSExecutor::CommandParams &args )
754 {
755  //----------------------------------------------------------------------------
756  // Check up the args
757  //----------------------------------------------------------------------------
758  Log *log = DefaultEnv::GetLog();
759  uint32_t argc = args.size();
760 
761  if( argc != 3 )
762  {
763  log->Error( AppMsg, "Wrong number of arguments." );
765  }
766 
767  std::string fullPath;
768  if( !BuildPath( fullPath, env, args[1] ).IsOK() )
769  {
770  log->Error( AppMsg, "Invalid path." );
772  }
773 
774  char *result;
775  uint64_t size = ::strtoll( args[2].c_str(), &result, 0 );
776  if( *result != 0 )
777  {
778  log->Error( AppMsg, "Size parameter needs to be an integer" );
780  }
781 
782  //----------------------------------------------------------------------------
783  // Run the query
784  //----------------------------------------------------------------------------
785  XRootDStatus st = fs->Truncate( fullPath, size );
786  if( !st.IsOK() )
787  {
788  log->Error( AppMsg, "Unable truncate %s: %s",
789  fullPath.c_str(),
790  st.ToStr().c_str() );
791  return st;
792  }
793 
794  return XRootDStatus();
795 }
796 
797 //------------------------------------------------------------------------------
798 // Change the access rights to a file
799 //------------------------------------------------------------------------------
801  Env *env,
802  const FSExecutor::CommandParams &args )
803 {
804  //----------------------------------------------------------------------------
805  // Check up the args
806  //----------------------------------------------------------------------------
807  Log *log = DefaultEnv::GetLog();
808  uint32_t argc = args.size();
809 
810  if( argc != 3 )
811  {
812  log->Error( AppMsg, "Wrong number of arguments." );
814  }
815 
816  std::string fullPath;
817  if( !BuildPath( fullPath, env, args[1] ).IsOK() )
818  {
819  log->Error( AppMsg, "Invalid path." );
821  }
822 
823  Access::Mode mode = Access::None;
824  XRootDStatus st = ConvertMode( mode, args[2] );
825  if( !st.IsOK() )
826  {
827  log->Error( AppMsg, "Invalid mode string." );
828  return st;
829  }
830 
831  //----------------------------------------------------------------------------
832  // Run the query
833  //----------------------------------------------------------------------------
834  st = fs->ChMod( fullPath, mode );
835  if( !st.IsOK() )
836  {
837  log->Error( AppMsg, "Unable change mode of %s: %s",
838  fullPath.c_str(),
839  st.ToStr().c_str() );
840  return st;
841  }
842 
843  return XRootDStatus();
844 }
845 
846 //------------------------------------------------------------------------------
847 // Locate a path
848 //------------------------------------------------------------------------------
850  Env *env,
851  const FSExecutor::CommandParams &args )
852 {
853  //----------------------------------------------------------------------------
854  // Check up the args
855  //----------------------------------------------------------------------------
856  Log *log = DefaultEnv::GetLog();
857  uint32_t argc = args.size();
858 
859  if( argc > 4 )
860  {
861  log->Error( AppMsg, "Wrong number of arguments." );
863  }
864 
866  std::string path;
867  bool hasPath = false;
868  bool doDeepLocate = false;
869  for( uint32_t i = 1; i < argc; ++i )
870  {
871  if( args[i] == "-n" )
872  flags |= OpenFlags::NoWait;
873  else if( args[i] == "-r" )
874  flags |= OpenFlags::Refresh;
875  else if( args[i] == "-m" || args[i] == "-h" )
876  flags |= OpenFlags::PrefName;
877  else if( args[i] == "-i" )
878  flags |= OpenFlags::Force;
879  else if( args[i] == "-d" )
880  doDeepLocate = true;
881  else if( args[i] == "-p" )
882  {
883  Env *env = DefaultEnv::GetEnv();
884  env->PutInt( "PreserveLocateTried", 0 );
885  }
886  else if( !hasPath )
887  {
888  path = args[i];
889  hasPath = true;
890  }
891  else
892  {
893  log->Error( AppMsg, "Invalid argument: %s.", args[i].c_str() );
895  }
896  }
897 
898  std::string fullPath;
899  if( path[0] == '*' )
900  fullPath = path;
901  else
902  {
903  if( !BuildPath( fullPath, env, path ).IsOK() )
904  {
905  log->Error( AppMsg, "Invalid path." );
907  }
908  }
909 
910  //----------------------------------------------------------------------------
911  // Run the query
912  //----------------------------------------------------------------------------
913  LocationInfo *info = 0;
914  XRootDStatus st;
915  if( doDeepLocate )
916  st = fs->DeepLocate( fullPath, flags, info );
917  else
918  st = fs->Locate( fullPath, flags, info );
919 
920  if( !st.IsOK() )
921  {
922  log->Error( AppMsg, "Unable locate %s: %s",
923  fullPath.c_str(),
924  st.ToStr().c_str() );
925  return st;
926  }
927 
928  //----------------------------------------------------------------------------
929  // Print the result
930  //----------------------------------------------------------------------------
931  if( st.code == suPartial )
932  {
933  std::cerr << "[!] Some of the requests failed. The result may be ";
934  std::cerr << "incomplete." << std::endl;
935  }
936 
938  for( it = info->Begin(); it != info->End(); ++it )
939  {
940  std::cout << it->GetAddress() << " ";
941  switch( it->GetType() )
942  {
944  std::cout << "Manager ";
945  break;
947  std::cout << "ManagerPending ";
948  break;
950  std::cout << "Server ";
951  break;
953  std::cout << "ServerPending ";
954  break;
955  default:
956  std::cout << "Unknown ";
957  };
958 
959  switch( it->GetAccessType() )
960  {
961  case LocationInfo::Read:
962  std::cout << "Read";
963  break;
965  std::cout << "ReadWrite ";
966  break;
967  default:
968  std::cout << "Unknown ";
969  };
970  std::cout << std::endl;
971  }
972 
973  delete info;
974  return XRootDStatus();
975 }
976 
977 //------------------------------------------------------------------------------
978 // Process stat query
979 //------------------------------------------------------------------------------
980 XRootDStatus ProcessStatQuery( StatInfo &info, const std::string &query )
981 {
982  Log *log = DefaultEnv::GetLog();
983 
984  //----------------------------------------------------------------------------
985  // Process the query
986  //----------------------------------------------------------------------------
987  bool isOrQuery = false;
988  bool status = true;
989  if( query.find( '|' ) != std::string::npos )
990  {
991  isOrQuery = true;
992  status = false;
993  }
994  std::vector<std::string> queryFlags;
995  if( isOrQuery )
996  Utils::splitString( queryFlags, query, "|" );
997  else
998  Utils::splitString( queryFlags, query, "&" );
999 
1000  //----------------------------------------------------------------------------
1001  // Initialize flag translation map and check the input flags
1002  //----------------------------------------------------------------------------
1003  std::map<std::string, StatInfo::Flags> flagMap;
1004  flagMap["XBitSet"] = StatInfo::XBitSet;
1005  flagMap["IsDir"] = StatInfo::IsDir;
1006  flagMap["Other"] = StatInfo::Other;
1007  flagMap["Offline"] = StatInfo::Offline;
1008  flagMap["POSCPending"] = StatInfo::POSCPending;
1009  flagMap["IsReadable"] = StatInfo::IsReadable;
1010  flagMap["IsWritable"] = StatInfo::IsWritable;
1011  flagMap["BackUpExists"] = StatInfo::BackUpExists;
1012 
1013  std::vector<std::string>::iterator it;
1014  for( it = queryFlags.begin(); it != queryFlags.end(); ++it )
1015  if( flagMap.find( *it ) == flagMap.end() )
1016  {
1017  log->Error( AppMsg, "Flag '%s' is not recognized.", it->c_str() );
1018  return XRootDStatus( stError, errInvalidArgs );
1019  }
1020 
1021  //----------------------------------------------------------------------------
1022  // Process the query
1023  //----------------------------------------------------------------------------
1024  if( isOrQuery )
1025  {
1026  for( it = queryFlags.begin(); it != queryFlags.end(); ++it )
1027  if( info.TestFlags( flagMap[*it] ) )
1028  return XRootDStatus();
1029  }
1030  else
1031  {
1032  for( it = queryFlags.begin(); it != queryFlags.end(); ++it )
1033  if( !info.TestFlags( flagMap[*it] ) )
1035  }
1036 
1037  if( status )
1038  return XRootDStatus();
1040 }
1041 
1042 //------------------------------------------------------------------------------
1043 // Stat a path
1044 //------------------------------------------------------------------------------
1046  Env *env,
1047  const FSExecutor::CommandParams &args )
1048 {
1049  //----------------------------------------------------------------------------
1050  // Check up the args
1051  //----------------------------------------------------------------------------
1052  Log *log = DefaultEnv::GetLog();
1053  uint32_t argc = args.size();
1054 
1055  if( argc < 2 )
1056  {
1057  log->Error( AppMsg, "Wrong number of arguments." );
1058  return XRootDStatus( stError, errInvalidArgs );
1059  }
1060 
1061  std::vector<std::string> paths;
1062  std::string query;
1063 
1064  for( uint32_t i = 1; i < args.size(); ++i )
1065  {
1066  if( args[i] == "-q" )
1067  {
1068  if( i < args.size()-1 )
1069  {
1070  query = args[i+1];
1071  ++i;
1072  }
1073  else
1074  {
1075  log->Error( AppMsg, "Parameter '-q' requires an argument." );
1076  return XRootDStatus( stError, errInvalidArgs );
1077  }
1078  }
1079  else
1080  paths.emplace_back( args[i] );
1081  }
1082 
1083  std::vector<XrdCl::Pipeline> stats;
1084  std::vector<std::tuple<std::future<StatInfo>, std::string>> results;
1085  for( auto &path : paths )
1086  {
1087  std::string fullPath;
1088  if( !BuildPath( fullPath, env, path ).IsOK() )
1089  {
1090  log->Error( AppMsg, "Invalid path." );
1091  return XRootDStatus( stError, errInvalidArgs );
1092  }
1093  std::future<XrdCl::StatInfo> ftr;
1094  stats.emplace_back( XrdCl::Stat( fs, fullPath ) >> ftr );
1095  results.emplace_back( std::move( ftr ), std::move( fullPath ) );
1096  }
1097 
1098  //----------------------------------------------------------------------------
1099  // Run the query
1100  //----------------------------------------------------------------------------
1101  XrdCl::Async( XrdCl::Parallel( stats ) );
1102 
1103  //----------------------------------------------------------------------------
1104  // Print the result
1105  //----------------------------------------------------------------------------
1107  for( auto &tpl : results )
1108  {
1109  auto &ftr = std::get<0>( tpl );
1110  auto &fullPath = std::get<1>( tpl );
1111  std::cout << std::endl;
1112  try
1113  {
1114  XrdCl::StatInfo info( ftr.get() );
1115  std::string flags;
1116 
1117  if( info.TestFlags( StatInfo::XBitSet ) )
1118  flags += "XBitSet|";
1119  if( info.TestFlags( StatInfo::IsDir ) )
1120  flags += "IsDir|";
1121  if( info.TestFlags( StatInfo::Other ) )
1122  flags += "Other|";
1123  if( info.TestFlags( StatInfo::Offline ) )
1124  flags += "Offline|";
1125  if( info.TestFlags( StatInfo::POSCPending ) )
1126  flags += "POSCPending|";
1127  if( info.TestFlags( StatInfo::IsReadable ) )
1128  flags += "IsReadable|";
1129  if( info.TestFlags( StatInfo::IsWritable ) )
1130  flags += "IsWritable|";
1131  if( info.TestFlags( StatInfo::BackUpExists ) )
1132  flags += "BackUpExists|";
1133 
1134  if( !flags.empty() )
1135  flags.erase( flags.length()-1, 1 );
1136 
1137  std::cout << "Path: " << fullPath << std::endl;
1138  std::cout << "Id: " << info.GetId() << std::endl;
1139  std::cout << "Size: " << info.GetSize() << std::endl;
1140  std::cout << "MTime: " << info.GetModTimeAsString() << std::endl;
1141  // if extended stat information is available we can print also
1142  // change time and access time
1143  if( info.ExtendedFormat() )
1144  {
1145  std::cout << "CTime: " << info.GetChangeTimeAsString() << std::endl;
1146  std::cout << "ATime: " << info.GetAccessTimeAsString() << std::endl;
1147  }
1148  std::cout << "Flags: " << info.GetFlags() << " (" << flags << ")";
1149 
1150  // check if extended stat information is available
1151  if( info.ExtendedFormat() )
1152  {
1153  std::cout << "\nMode: " << info.GetModeAsString() << std::endl;
1154  std::cout << "Owner: " << info.GetOwner() << std::endl;
1155  std::cout << "Group: " << info.GetGroup();
1156  }
1157 
1158  std::cout << std::endl;
1159 
1160  if( query.length() != 0 )
1161  {
1162  XRootDStatus s = ProcessStatQuery( info, query );
1163  if( !s.IsOK() )
1164  st = s;
1165  std::cout << "Query: " << query << " " << std::endl;
1166  }
1167  }
1168  catch( XrdCl::PipelineException &ex )
1169  {
1170  st = ex.GetError();
1171  log->Error( AppMsg, "Unable stat %s: %s", fullPath.c_str(), st.ToStr().c_str() );
1172  }
1173  }
1174 
1175  return st;
1176 }
1177 
1178 //------------------------------------------------------------------------------
1179 // Stat a VFS
1180 //------------------------------------------------------------------------------
1182  Env *env,
1183  const FSExecutor::CommandParams &args )
1184 {
1185  //----------------------------------------------------------------------------
1186  // Check up the args
1187  //----------------------------------------------------------------------------
1188  Log *log = DefaultEnv::GetLog();
1189  uint32_t argc = args.size();
1190 
1191  if( argc != 2 )
1192  {
1193  log->Error( AppMsg, "Wrong number of arguments." );
1194  return XRootDStatus( stError, errInvalidArgs );
1195  }
1196 
1197  std::string fullPath;
1198  if( !BuildPath( fullPath, env, args[1] ).IsOK() )
1199  {
1200  log->Error( AppMsg, "Invalid path." );
1201  return XRootDStatus( stError, errInvalidArgs );
1202  }
1203 
1204  //----------------------------------------------------------------------------
1205  // Run the query
1206  //----------------------------------------------------------------------------
1207  StatInfoVFS *info = 0;
1208  XRootDStatus st = fs->StatVFS( fullPath, info );
1209 
1210  if( !st.IsOK() )
1211  {
1212  log->Error( AppMsg, "Unable stat VFS at %s: %s",
1213  fullPath.c_str(),
1214  st.ToStr().c_str() );
1215  return st;
1216  }
1217 
1218  //----------------------------------------------------------------------------
1219  // Print the result
1220  //----------------------------------------------------------------------------
1221  std::cout << "Path: ";
1222  std::cout << fullPath << std::endl;
1223  std::cout << "Nodes with RW space: ";
1224  std::cout << info->GetNodesRW() << std::endl;
1225  std::cout << "Size of largest RW space (MB): ";
1226  std::cout << info->GetFreeRW() << std::endl;
1227  std::cout << "Utilization of RW space (%): ";
1228  std::cout << (uint16_t)info->GetUtilizationRW() << std::endl;
1229  std::cout << "Nodes with staging space: ";
1230  std::cout << info->GetNodesStaging() << std::endl;
1231  std::cout << "Size of largest staging space (MB): ";
1232  std::cout << info->GetFreeStaging() << std::endl;
1233  std::cout << "Utilization of staging space (%): ";
1234  std::cout << (uint16_t)info->GetUtilizationStaging() << std::endl;
1235 
1236  delete info;
1237  return XRootDStatus();
1238 }
1239 
1240 //------------------------------------------------------------------------------
1241 // Query the server
1242 //------------------------------------------------------------------------------
1244  Env *env,
1245  const FSExecutor::CommandParams &args )
1246 {
1247  //----------------------------------------------------------------------------
1248  // Check up the args
1249  //----------------------------------------------------------------------------
1250  Log *log = DefaultEnv::GetLog();
1251  uint32_t argc = args.size();
1252 
1253  if( !( argc >= 3 ) )
1254  {
1255  log->Error( AppMsg, "Wrong number of arguments." );
1256  return XRootDStatus( stError, errInvalidArgs );
1257  }
1258 
1259  QueryCode::Code qCode;
1260  if( args[1] == "config" )
1261  qCode = QueryCode::Config;
1262  else if( args[1] == "checksumcancel" )
1263  qCode = QueryCode::ChecksumCancel;
1264  else if( args[1] == "checksum" )
1265  qCode = QueryCode::Checksum;
1266  else if( args[1] == "opaque" )
1267  qCode = QueryCode::Opaque;
1268  else if( args[1] == "opaquefile" )
1269  qCode = QueryCode::OpaqueFile;
1270  else if( args[1] == "prepare" )
1271  qCode = QueryCode::Prepare;
1272  else if( args[1] == "space" )
1273  qCode = QueryCode::Space;
1274  else if( args[1] == "stats" )
1275  qCode = QueryCode::Stats;
1276  else if( args[1] == "xattr" )
1277  qCode = QueryCode::XAttr;
1278  else
1279  {
1280  log->Error( AppMsg, "Invalid query code." );
1281  return XRootDStatus( stError, errInvalidArgs );
1282  }
1283 
1284  if( !( qCode & QueryCode::Prepare ) && argc != 3 )
1285  {
1286  log->Error( AppMsg, "Wrong number of arguments." );
1287  return XRootDStatus( stError, errInvalidArgs );
1288  }
1289 
1290  std::string strArg = args[2];
1291  if( qCode & QueryCode::Prepare )
1292  {
1293  // strArg is supposed to contain already the request ID
1294 
1295  for( size_t i = 3; i < args.size(); ++i )
1296  {
1297  std::string path = args[i];
1298  if( !BuildPath( path, env, path ).IsOK() )
1299  {
1300  log->Error( AppMsg, "Invalid path." );
1301  return XRootDStatus( stError, errInvalidArgs );
1302  }
1303  // we use new line character as delimiter
1304  strArg += '\n';
1305  strArg += path;
1306  }
1307  }
1308  else
1309  {
1310  std::string strArg = args[2];
1311  if( qCode == QueryCode::ChecksumCancel ||
1312  qCode == QueryCode::Checksum ||
1313  qCode == QueryCode::XAttr )
1314  {
1315  if( !BuildPath( strArg, env, args[2] ).IsOK() )
1316  {
1317  log->Error( AppMsg, "Invalid path." );
1318  return XRootDStatus( stError, errInvalidArgs );
1319  }
1320  }
1321  }
1322 
1323  //----------------------------------------------------------------------------
1324  // Run the query
1325  //----------------------------------------------------------------------------
1326  Buffer arg( strArg.size() );
1327  arg.FromString( strArg );
1328  Buffer *response = 0;
1329  XRootDStatus st = fs->Query( qCode, arg, response );
1330 
1331  if( !st.IsOK() )
1332  {
1333  log->Error( AppMsg, "Unable run query %s: %s",
1334  args[1].c_str(),
1335  st.ToStr().c_str() );
1336  return st;
1337  }
1338 
1339  //----------------------------------------------------------------------------
1340  // Print the result
1341  //----------------------------------------------------------------------------
1342  std::cout << response->ToString() << std::endl;
1343  delete response;
1344  return XRootDStatus();
1345 }
1346 
1347 //------------------------------------------------------------------------------
1348 // Query the server
1349 //------------------------------------------------------------------------------
1351  Env *env,
1352  const FSExecutor::CommandParams &args )
1353 {
1354  //----------------------------------------------------------------------------
1355  // Check up the args
1356  //----------------------------------------------------------------------------
1357  Log *log = DefaultEnv::GetLog();
1358  uint32_t argc = args.size();
1359 
1360  if( argc < 2 )
1361  {
1362  log->Error( AppMsg, "Wrong number of arguments." );
1363  return XRootDStatus( stError, errInvalidArgs );
1364  }
1365 
1367  std::vector<std::string> files;
1368  uint8_t priority = 0;
1369  std::string reqid;
1370 
1371  for( uint32_t i = 1; i < args.size(); ++i )
1372  {
1373  if( args[i] == "-p" )
1374  {
1375  if( i < args.size()-1 )
1376  {
1377  char *result;
1378  int32_t param = ::strtol( args[i+1].c_str(), &result, 0 );
1379  if( *result != 0 || param > 3 || param < 0 )
1380  {
1381  log->Error( AppMsg, "Size priotiry needs to be an integer between 0 "
1382  "and 3" );
1383  return XRootDStatus( stError, errInvalidArgs );
1384  }
1385  priority = (uint8_t)param;
1386  ++i;
1387  }
1388  else
1389  {
1390  log->Error( AppMsg, "Parameter '-p' requires an argument." );
1391  return XRootDStatus( stError, errInvalidArgs );
1392  }
1393  }
1394  else if( args[i] == "-c" )
1395  flags |= PrepareFlags::Colocate;
1396  else if( args[i] == "-f" )
1397  flags |= PrepareFlags::Fresh;
1398  else if( args[i] == "-s" )
1399  flags |= PrepareFlags::Stage;
1400  else if( args[i] == "-w" )
1401  flags |= PrepareFlags::WriteMode;
1402  else if( args[i] == "-e" )
1403  flags |= PrepareFlags::Evict;
1404  else if( args[i] == "-a" )
1405  {
1406  flags |= PrepareFlags::Cancel;
1407  if( i < args.size()-1 )
1408  {
1409  // by convention the request ID appears as the the first token
1410  // in the list of files
1411  files.push_back( args[i+1] );
1412  ++i;
1413  }
1414  else
1415  {
1416  log->Error( AppMsg, "Parameter '-a' requires an argument." );
1417  return XRootDStatus( stError, errInvalidArgs );
1418  }
1419  }
1420  else
1421  files.push_back( args[i] );
1422  }
1423 
1424  if( files.empty() )
1425  {
1426  log->Error( AppMsg, "Filename missing." );
1427  return XRootDStatus( stError, errInvalidArgs );
1428  }
1429 
1430  //----------------------------------------------------------------------------
1431  // Run the command
1432  //----------------------------------------------------------------------------
1433  Buffer *response = 0;
1434  XRootDStatus st = fs->Prepare( files, flags, priority, response );
1435  if( !st.IsOK() )
1436  {
1437  log->Error( AppMsg, "Prepare request failed: %s", st.ToStr().c_str() );
1438  return st;
1439  }
1440 
1441  if( ( flags & PrepareFlags::Stage ) && response )
1442  {
1443  std::cout << response->ToString() << '\n';
1444  }
1445 
1446  delete response;
1447  return XRootDStatus();
1448 }
1449 
1450 //------------------------------------------------------------------------------
1451 // Copy progress handler
1452 //------------------------------------------------------------------------------
1454 {
1455  public:
1456  //--------------------------------------------------------------------------
1457  // Constructor
1458  //--------------------------------------------------------------------------
1459  ProgressDisplay(): pBytesProcessed(0), pBytesTotal(0), pPrevious(0)
1460  {}
1461 
1462  //--------------------------------------------------------------------------
1463  // End job
1464  //--------------------------------------------------------------------------
1465  virtual void EndJob( uint32_t jobNum, const XrdCl::PropertyList *results )
1466  {
1467  JobProgress( jobNum, pBytesProcessed, pBytesTotal );
1468  std::cerr << std::endl;
1469  }
1470 
1471  //--------------------------------------------------------------------------
1472  // Job progress
1473  //--------------------------------------------------------------------------
1474  virtual void JobProgress( uint32_t jobNum,
1475  uint64_t bytesProcessed,
1476  uint64_t bytesTotal )
1477  {
1478  pBytesProcessed = bytesProcessed;
1479  pBytesTotal = bytesTotal;
1480 
1481  time_t now = time(0);
1482  if( (now - pPrevious < 1) && (bytesProcessed != bytesTotal) )
1483  return;
1484  pPrevious = now;
1485 
1486  std::cerr << "\r";
1487  std::cerr << "Progress: ";
1488  std::cerr << XrdCl::Utils::BytesToString(bytesProcessed) << "B ";
1489 
1490  if( bytesTotal )
1491  std::cerr << "(" << bytesProcessed*100/bytesTotal << "%)";
1492 
1493  std::cerr << std::flush;
1494  }
1495 
1496  private:
1497  uint64_t pBytesProcessed;
1498  uint64_t pBytesTotal;
1499  time_t pPrevious;
1500 };
1501 
1502 //------------------------------------------------------------------------------
1503 // Cat a file
1504 //------------------------------------------------------------------------------
1506  Env *env,
1507  const FSExecutor::CommandParams &args )
1508 {
1509  //----------------------------------------------------------------------------
1510  // Check up the args
1511  //----------------------------------------------------------------------------
1512  Log *log = DefaultEnv::GetLog();
1513  uint32_t argc = args.size();
1514 
1515  if( argc < 2 )
1516  {
1517  log->Error( AppMsg, "Wrong number of arguments." );
1518  return XRootDStatus( stError, errInvalidArgs );
1519  }
1520 
1521  std::string server;
1522  env->GetString( "ServerURL", server );
1523  if( server.empty() )
1524  {
1525  log->Error( AppMsg, "Invalid address: \"%s\".", server.c_str() );
1526  return XRootDStatus( stError, errInvalidAddr );
1527  }
1528 
1529  std::vector<std::string> remotes;
1530  std::string local;
1531 
1532  for( uint32_t i = 1; i < args.size(); ++i )
1533  {
1534  if( args[i] == "-o" )
1535  {
1536  if( i < args.size()-1 )
1537  {
1538  local = args[i+1];
1539  ++i;
1540  }
1541  else
1542  {
1543  log->Error( AppMsg, "Parameter '-o' requires an argument." );
1544  return XRootDStatus( stError, errInvalidArgs );
1545  }
1546  }
1547  else
1548  remotes.emplace_back( args[i] );
1549  }
1550 
1551  if( !local.empty() && remotes.size() > 1 )
1552  {
1553  log->Error( AppMsg, "If '-o' is used only can be used with only one remote file." );
1554  return XRootDStatus( stError, errInvalidArgs );
1555  }
1556 
1557  std::vector<URL> remoteUrls;
1558  remoteUrls.reserve( remotes.size() );
1559  for( auto &remote : remotes )
1560  {
1561  std::string remoteFile;
1562  if( !BuildPath( remoteFile, env, remote ).IsOK() )
1563  {
1564  log->Error( AppMsg, "Invalid path." );
1565  return XRootDStatus( stError, errInvalidArgs );
1566  }
1567 
1568  remoteUrls.emplace_back( server );
1569  remoteUrls.back().SetPath( remoteFile );
1570  }
1571 
1572  //----------------------------------------------------------------------------
1573  // Fetch the data
1574  //----------------------------------------------------------------------------
1575  CopyProgressHandler *handler = 0; ProgressDisplay d;
1576  CopyProcess process;
1577  std::vector<PropertyList> props( remoteUrls.size() ), results( remoteUrls.size() );
1578 
1579  for( size_t i = 0; i < remoteUrls.size(); ++i )
1580  {
1581  props[i].Set( "source", remoteUrls[i].GetURL() );
1582  if( !local.empty() )
1583  {
1584  props[i].Set( "target", std::string( "file://" ) + local );
1585  handler = &d;
1586  }
1587  else
1588  props[i].Set( "target", "stdio://-" );
1589 
1590  props[i].Set( "dynamicSource", true );
1591 
1592  XRootDStatus st = process.AddJob( props[i], &results[i] );
1593  if( !st.IsOK() )
1594  {
1595  log->Error( AppMsg, "Job adding failed: %s.", st.ToStr().c_str() );
1596  return st;
1597  }
1598  }
1599 
1600  XRootDStatus st = process.Prepare();
1601  if( !st.IsOK() )
1602  {
1603  log->Error( AppMsg, "Copy preparation failed: %s.", st.ToStr().c_str() );
1604  return st;
1605  }
1606 
1607  st = process.Run(handler);
1608  if( !st.IsOK() )
1609  {
1610  log->Error( AppMsg, "Cope process failed: %s.", st.ToStr().c_str() );
1611  return st;
1612  }
1613 
1614  return XRootDStatus();
1615 }
1616 
1617 //------------------------------------------------------------------------------
1618 // Tail a file
1619 //------------------------------------------------------------------------------
1621  Env *env,
1622  const FSExecutor::CommandParams &args )
1623 {
1624  //----------------------------------------------------------------------------
1625  // Check up the args
1626  //----------------------------------------------------------------------------
1627  Log *log = DefaultEnv::GetLog();
1628  uint32_t argc = args.size();
1629 
1630  if( argc < 2 || argc > 5 )
1631  {
1632  log->Error( AppMsg, "Wrong number of arguments." );
1633  return XRootDStatus( stError, errInvalidArgs );
1634  }
1635 
1636  std::string server;
1637  env->GetString( "ServerURL", server );
1638  if( server.empty() )
1639  {
1640  log->Error( AppMsg, "Invalid address: \"%s\".", server.c_str() );
1641  return XRootDStatus( stError, errInvalidAddr );
1642  }
1643 
1644  std::string remote;
1645  bool followMode = false;
1646  uint32_t offset = 512;
1647 
1648  for( uint32_t i = 1; i < args.size(); ++i )
1649  {
1650  if( args[i] == "-f" )
1651  followMode = true;
1652  else if( args[i] == "-c" )
1653  {
1654  if( i < args.size()-1 )
1655  {
1656  char *result;
1657  offset = ::strtol( args[i+1].c_str(), &result, 0 );
1658  if( *result != 0 )
1659  {
1660  log->Error( AppMsg, "Offset from the end needs to be a number: %s",
1661  args[i+1].c_str() );
1662  return XRootDStatus( stError, errInvalidArgs );
1663  }
1664  ++i;
1665  }
1666  else
1667  {
1668  log->Error( AppMsg, "Parameter '-n' requires an argument." );
1669  return XRootDStatus( stError, errInvalidArgs );
1670  }
1671  }
1672  else
1673  remote = args[i];
1674  }
1675 
1676  std::string remoteFile;
1677  if( !BuildPath( remoteFile, env, remote ).IsOK() )
1678  {
1679  log->Error( AppMsg, "Invalid path." );
1680  return XRootDStatus( stError, errInvalidArgs );
1681  }
1682 
1683  URL remoteUrl( server );
1684  remoteUrl.SetPath( remoteFile );
1685 
1686  //----------------------------------------------------------------------------
1687  // Fetch the data
1688  //----------------------------------------------------------------------------
1689  File file;
1690  XRootDStatus st = file.Open( remoteUrl.GetURL(), OpenFlags::Read );
1691  if( !st.IsOK() )
1692  {
1693  log->Error( AppMsg, "Unable to open file %s: %s",
1694  remoteUrl.GetObfuscatedURL().c_str(), st.ToStr().c_str() );
1695  return st;
1696  }
1697 
1698  StatInfo *info = 0;
1699  uint64_t size = 0;
1700  st = file.Stat( false, info );
1701  if (st.IsOK()) size = info->GetSize();
1702 
1703  if( size < offset )
1704  offset = 0;
1705  else
1706  offset = size - offset;
1707 
1708  uint32_t chunkSize = 1*1024*1024;
1709  char *buffer = new char[chunkSize];
1710  uint32_t bytesRead = 0;
1711  while(1)
1712  {
1713  st = file.Read( offset, chunkSize, buffer, bytesRead );
1714  if( !st.IsOK() )
1715  {
1716  log->Error( AppMsg, "Unable to read from %s: %s",
1717  remoteUrl.GetObfuscatedURL().c_str(), st.ToStr().c_str() );
1718  delete [] buffer;
1719  return st;
1720  }
1721 
1722  offset += bytesRead;
1723  int ret = write( 1, buffer, bytesRead );
1724  if( ret == -1 )
1725  {
1726  log->Error( AppMsg, "Unable to write to stdout: %s",
1727  XrdSysE2T(errno) );
1728  delete [] buffer;
1729  return st;
1730  }
1731 
1732  if( bytesRead < chunkSize )
1733  {
1734  if( !followMode )
1735  break;
1736  sleep(1);
1737  }
1738  }
1739  delete [] buffer;
1740 
1741  XRootDStatus stC = file.Close();
1742 
1743  return XRootDStatus();
1744 }
1745 
1746 //------------------------------------------------------------------------------
1747 // Print statistics concerning given space
1748 //------------------------------------------------------------------------------
1750  Env *env,
1751  const FSExecutor::CommandParams &args )
1752 {
1753  using namespace XrdCl;
1754 
1755  //----------------------------------------------------------------------------
1756  // Check up the args
1757  //----------------------------------------------------------------------------
1758  Log *log = DefaultEnv::GetLog();
1759  uint32_t argc = args.size();
1760 
1761  if( argc != 2 )
1762  {
1763  log->Error( AppMsg, "Wrong number of arguments." );
1764  return XRootDStatus( stError, errInvalidArgs );
1765  }
1766 
1768 
1769  XRootDStatus st = FileSystemUtils::GetSpaceInfo( i, fs, args[1] );
1770  if( !st.IsOK() )
1771  return st;
1772 
1773  if( st.code == suPartial )
1774  {
1775  std::cerr << "[!] Some of the requests failed. The result may be ";
1776  std::cerr << "incomplete." << std::endl;
1777  }
1778 
1779  std::cout << "Path: " << args[1] << std::endl;
1780  std::cout << "Total: " << i->GetTotal() << std::endl;
1781  std::cout << "Free: " << i->GetFree() << std::endl;
1782  std::cout << "Used: " << i->GetUsed() << std::endl;
1783  std::cout << "Largest free chunk: " << i->GetLargestFreeChunk() << std::endl;
1784 
1785  delete i;
1786  return XRootDStatus();
1787 }
1788 
1789 //------------------------------------------------------------------------------
1790 // Carry out xattr operation
1791 //------------------------------------------------------------------------------
1793  Env *env,
1794  const FSExecutor::CommandParams &args )
1795 {
1796  //----------------------------------------------------------------------------
1797  // Check up the args
1798  //----------------------------------------------------------------------------
1799  Log *log = DefaultEnv::GetLog();
1800  uint32_t argc = args.size();
1801 
1802  if( argc < 3 )
1803  {
1804  log->Error( AppMsg, "Wrong number of arguments." );
1805  return XRootDStatus( stError, errInvalidArgs );
1806  }
1807 
1808  kXR_char code = 0;
1809  if( args[2] == "set")
1810  code = kXR_fattrSet;
1811  else if( args[2] == "get" )
1812  code = kXR_fattrGet;
1813  else if( args[2] == "del" )
1814  code = kXR_fattrDel;
1815  else if( args[2] == "list" )
1816  code = kXR_fattrList;
1817  else
1818  {
1819  log->Error( AppMsg, "Invalid xattr code." );
1820  return XRootDStatus( stError, errInvalidArgs );
1821  }
1822 
1823  std::string path;
1824  if( !BuildPath( path, env, args[1] ).IsOK() )
1825  {
1826  log->Error( AppMsg, "Invalid path." );
1827  return XRootDStatus( stError, errInvalidArgs );
1828  }
1829 
1830  //----------------------------------------------------------------------------
1831  // Issue the xattr operation
1832  //----------------------------------------------------------------------------
1833  XRootDStatus status;
1834  switch( code )
1835  {
1836  case kXR_fattrSet:
1837  {
1838  if( argc != 4 )
1839  {
1840  log->Error( AppMsg, "Wrong number of arguments." );
1841  return XRootDStatus( stError, errInvalidArgs );
1842  }
1843 
1844  std::string key_value = args[3];
1845  size_t pos = key_value.find( '=' );
1846  std::string key = key_value.substr( 0, pos );
1847  std::string value = key_value.substr( pos + 1 );
1848  std::vector<xattr_t> attrs;
1849  attrs.push_back( std::make_tuple( key, value ) );
1850 
1851  std::vector<XAttrStatus> result;
1852  XRootDStatus status = fs->SetXAttr( path, attrs, result );
1853  XAttrStatus xst = status.IsOK() ? result.front() : XAttrStatus( key, status );
1854 
1855  if( !xst.status.IsOK() )
1856  status = xst.status;
1857 
1858  if( !status.IsOK() )
1859  log->Error( AppMsg, "Unable to xattr set %s %s: %s",
1860  key.c_str(), value.c_str(),
1861  status.ToStr().c_str() );
1862  return status;
1863  }
1864 
1865  case kXR_fattrGet:
1866  {
1867  if( argc != 4 )
1868  {
1869  log->Error( AppMsg, "Wrong number of arguments." );
1870  return XRootDStatus( stError, errInvalidArgs );
1871  }
1872 
1873  std::string key = args[3];
1874  std::vector<std::string> attrs;
1875  attrs.push_back( key );
1876 
1877  std::vector<XAttr> result;
1878  XRootDStatus status = fs->GetXAttr( path, attrs, result );
1879  XAttr xattr = status.IsOK() ? result.front() : XAttr( key, status );
1880 
1881  if( !xattr.status.IsOK() )
1882  status = xattr.status;
1883 
1884  if( !status.IsOK() )
1885  log->Error( AppMsg, "Unable to xattr get %s : %s",
1886  key.c_str(),
1887  status.ToStr().c_str() );
1888  else
1889  {
1890  std::cout << "# file: " << path << '\n';
1891  std::cout << xattr.name << "=\"" << xattr.value << "\"\n";
1892  }
1893 
1894  return status;
1895  }
1896 
1897  case kXR_fattrDel:
1898  {
1899  if( argc != 4 )
1900  {
1901  log->Error( AppMsg, "Wrong number of arguments." );
1902  return XRootDStatus( stError, errInvalidArgs );
1903  }
1904 
1905  std::string key = args[3];
1906  std::vector<std::string> attrs;
1907  attrs.push_back( key );
1908 
1909  std::vector<XAttrStatus> result ;
1910  XRootDStatus status = fs->DelXAttr( path, attrs, result );
1911  XAttrStatus xst = status.IsOK() ? result.front() : XAttrStatus( key, status );
1912 
1913  if( !xst.status.IsOK() )
1914  status = xst.status;
1915 
1916  if( !status.IsOK() )
1917  log->Error( AppMsg, "Unable to xattr del %s : %s",
1918  key.c_str(),
1919  status.ToStr().c_str() );
1920  return status;
1921  }
1922 
1923  case kXR_fattrList:
1924  {
1925  if( argc != 3 )
1926  {
1927  log->Error( AppMsg, "Wrong number of arguments." );
1928  return XRootDStatus( stError, errInvalidArgs );
1929  }
1930 
1931  std::vector<XAttr> result;
1932  XRootDStatus status = fs->ListXAttr( path, result );
1933 
1934  if( !status.IsOK() )
1935  log->Error( AppMsg, "Unable to xattr list : %s",
1936  status.ToStr().c_str() );
1937  else
1938  {
1939  std::cout << "# file: " << path << '\n';
1940  auto itr = result.begin();
1941  for( ; itr != result.end(); ++itr )
1942  std::cout << itr->name << "=\"" << itr->value << "\"\n";
1943  }
1944 
1945  return status;
1946  }
1947 
1948  default:
1949  return XRootDStatus( stError, errInvalidAddr );
1950  }
1951 }
1952 
1953 //------------------------------------------------------------------------------
1954 // Print help
1955 //------------------------------------------------------------------------------
1957  const FSExecutor::CommandParams & )
1958 {
1959  printf( "Usage:\n" );
1960  printf( " xrdfs [--no-cwd] host[:port] - interactive mode\n" );
1961  printf( " xrdfs host[:port] command args - batch mode\n\n" );
1962 
1963  printf( "Available options:\n\n" );
1964 
1965  printf( " --no-cwd no CWD is being preset\n\n" );
1966 
1967  printf( "Available commands:\n\n" );
1968 
1969  printf( " exit\n" );
1970  printf( " Exits from the program.\n\n" );
1971 
1972  printf( " help\n" );
1973  printf( " This help screen.\n\n" );
1974 
1975  printf( " cache {evict | fevict} <path>\n" );
1976  printf( " Evict a file from a cache if not in use; while fevict\n" );
1977  printf( " forcibly evicts the file causing any current uses of the\n" );
1978  printf( " file to get read failures on a subsequent read\n\n" );
1979 
1980  printf( " cd <path>\n" );
1981  printf( " Change the current working directory\n\n" );
1982 
1983  printf( " chmod <path> <user><group><other>\n" );
1984  printf( " Modify permissions. Permission string example:\n" );
1985  printf( " rwxr-x--x\n\n" );
1986 
1987  printf( " ls [-l] [-u] [-R] [-D] [-Z] [-C] [dirname]\n" );
1988  printf( " Get directory listing.\n" );
1989  printf( " -l stat every entry and print long listing\n" );
1990  printf( " -u print paths as URLs\n" );
1991  printf( " -R list subdirectories recursively\n" );
1992  printf( " -D show duplicate entries\n" );
1993  printf( " -Z if a ZIP archive list its content\n" );
1994  printf( " -C checksum every entry\n\n" );
1995 
1996  printf( " locate [-n] [-r] [-d] [-m] [-i] [-p] <path>\n" );
1997  printf( " Get the locations of the path.\n" );
1998  printf( " -r refresh, don't use cached locations\n" );
1999  printf( " -n make the server return the response immediately even\n" );
2000  printf( " though it may be incomplete\n" );
2001  printf( " -d do a recursive (deep) locate\n" );
2002  printf( " -m|-h prefer host names to IP addresses\n" );
2003  printf( " -i ignore network dependencies\n" );
2004  printf( " -p be passive: ignore tried/triedrc cgi opaque info\n\n" );
2005 
2006  printf( " mkdir [-p] [-m<user><group><other>] <dirname>\n" );
2007  printf( " Creates a directory/tree of directories.\n\n" );
2008 
2009  printf( " mv <path1> <path2>\n" );
2010  printf( " Move path1 to path2 locally on the same server.\n\n" );
2011 
2012  printf( " stat [-q query] <path>\n" );
2013  printf( " Get info about the file or directory.\n" );
2014  printf( " -q query optional flag query parameter that makes\n" );
2015  printf( " xrdfs return error code to the shell if the\n" );
2016  printf( " requested flag combination is not present;\n" );
2017  printf( " flags may be combined together using '|' or '&'\n" );
2018  printf( " Available flags:\n" );
2019  printf( " XBitSet, IsDir, Other, Offline, POSCPending,\n" );
2020  printf( " IsReadable, IsWritable\n\n" );
2021 
2022  printf( " statvfs <path>\n" );
2023  printf( " Get info about a virtual file system.\n\n" );
2024 
2025  printf( " query <code> <parameters>\n" );
2026  printf( " Obtain server information. Query codes:\n\n" );
2027 
2028  printf( " config <what> Server configuration; <what> is\n" );
2029  printf( " one of the following:\n" );
2030  printf( " bind_max - the maximum number of parallel streams\n" );
2031  printf( " chksum - the supported checksum\n" );
2032  printf( " cms - the status of the cmsd\n" );
2033  printf( " pio_max - maximum number of parallel I/O requests\n" );
2034  printf( " readv_ior_max - maximum size of a readv element\n" );
2035  printf( " readv_iov_max - maximum number of readv entries\n" );
2036  printf( " role - the role in a cluster\n" );
2037  printf( " sitename - the site name\n" );
2038  printf( " tpc - support for third party copies\n" );
2039  printf( " version - the version of the server\n" );
2040  printf( " wan_port - the port to use for wan copies\n" );
2041  printf( " wan_window - the wan_port window size\n" );
2042  printf( " window - the tcp window size\n" );
2043  printf( " checksumcancel <path> File checksum cancellation\n" );
2044  printf( " checksum <path> File checksum\n" );
2045  printf( " opaque <arg> Implementation dependent\n" );
2046  printf( " opaquefile <arg> Implementation dependent\n" );
2047  printf( " space <space> Logical space stats\n" );
2048  printf( " stats <what> Server stats; <what> is a list\n" );
2049  printf( " of letters indicating information\n");
2050  printf( " to be returned:\n" );
2051  printf( " a - all statistics\n" );
2052  printf( " p - protocol statistics\n" );
2053  printf( " b - buffer usage statistics\n" );
2054  printf( " s - scheduling statistics\n" );
2055  printf( " d - device polling statistics\n" );
2056  printf( " u - usage statistics\n" );
2057  printf( " i - server identification\n" );
2058  printf( " z - synchronized statistics\n" );
2059  printf( " l - connection statistics\n" );
2060  printf( " xattr <path> Extended attributes\n" );
2061  printf( " prepare <reqid> [filenames] Prepare request status\n\n" );
2062 
2063  printf( " rm <filename>\n" );
2064  printf( " Remove a file.\n\n" );
2065 
2066  printf( " rmdir <dirname>\n" );
2067  printf( " Remove a directory.\n\n" );
2068 
2069  printf( " truncate <filename> <length>\n" );
2070  printf( " Truncate a file.\n\n" );
2071 
2072  printf( " prepare [-c] [-f] [-s] [-w] [-e] [-p priority] [-a requestid] filenames\n" );
2073  printf( " Prepare one or more files for access.\n" );
2074  printf( " -c co-locate staged files if possible\n" );
2075  printf( " -f refresh file access time even if the location is known\n" );
2076  printf( " -s stage the files to disk if they are not online\n" );
2077  printf( " -w the files will be accessed for modification\n" );
2078  printf( " -p priority of the request, 0 (lowest) - 3 (highest)\n" );
2079  printf( " -a abort stage request\n" );
2080  printf( " -e evict the file from disk cache\n\n" );
2081 
2082  printf( " cat [-o local file] files\n" );
2083  printf( " Print contents of one or more files to stdout.\n" );
2084  printf( " -o print to the specified local file\n\n" );
2085 
2086  printf( " tail [-c bytes] [-f] file\n" );
2087  printf( " Output last part of files to stdout.\n" );
2088  printf( " -c num_bytes out last num_bytes\n" );
2089  printf( " -f output appended data as file grows\n\n" );
2090 
2091  printf( " spaceinfo path\n" );
2092  printf( " Get space statistics for given path.\n\n" );
2093 
2094  printf( " xattr <path> <code> <params> \n" );
2095  printf( " Operation on extended attributes. Codes:\n\n" );
2096  printf( " set <attr> Set extended attribute; <attr> is\n" );
2097  printf( " string of form name=value\n" );
2098  printf( " get <name> Get extended attribute\n" );
2099  printf( " del <name> Delete extended attribute\n" );
2100  printf( " list List extended attributes\n\n" );
2101 
2102  return XRootDStatus();
2103 }
2104 
2105 //------------------------------------------------------------------------------
2106 // Create the executor object
2107 //------------------------------------------------------------------------------
2109 {
2110  Env *env = new Env();
2111  env->PutString( "CWD", "/" );
2112  FSExecutor *executor = new FSExecutor( url, env );
2113  executor->AddCommand( "cache", DoCache );
2114  executor->AddCommand( "cd", DoCD );
2115  executor->AddCommand( "chmod", DoChMod );
2116  executor->AddCommand( "ls", DoLS );
2117  executor->AddCommand( "help", PrintHelp );
2118  executor->AddCommand( "stat", DoStat );
2119  executor->AddCommand( "statvfs", DoStatVFS );
2120  executor->AddCommand( "locate", DoLocate );
2121  executor->AddCommand( "mv", DoMv );
2122  executor->AddCommand( "mkdir", DoMkDir );
2123  executor->AddCommand( "rm", DoRm );
2124  executor->AddCommand( "rmdir", DoRmDir );
2125  executor->AddCommand( "query", DoQuery );
2126  executor->AddCommand( "truncate", DoTruncate );
2127  executor->AddCommand( "prepare", DoPrepare );
2128  executor->AddCommand( "cat", DoCat );
2129  executor->AddCommand( "tail", DoTail );
2130  executor->AddCommand( "spaceinfo", DoSpaceInfo );
2131  executor->AddCommand( "xattr", DoXAttr );
2132  return executor;
2133 }
2134 
2135 //------------------------------------------------------------------------------
2136 // Execute command
2137 //------------------------------------------------------------------------------
2138 int ExecuteCommand( FSExecutor *ex, int argc, char **argv )
2139 {
2140  // std::vector<std::string> args (argv, argv + argc);
2141  std::vector<std::string> args;
2142  args.reserve(argc);
2143  for (int i = 0; i < argc; ++i)
2144  {
2145  args.push_back(argv[i]);
2146  }
2147  XRootDStatus st = ex->Execute( args );
2148  if( !st.IsOK() )
2149  std::cerr << st.ToStr() << std::endl;
2150  return st.GetShellCode();
2151 }
2152 
2153 //------------------------------------------------------------------------------
2154 // Define some functions required to function when build without readline
2155 //------------------------------------------------------------------------------
2156 #ifndef HAVE_READLINE
2157 char *readline(const char *prompt)
2158 {
2159  std::cout << prompt << std::flush;
2160  std::string input;
2161  std::getline( std::cin, input );
2162 
2163  if( !std::cin.good() )
2164  return 0;
2165 
2166  char *linebuf = (char *)malloc( input.size()+1 );
2167  strncpy( linebuf, input.c_str(), input.size()+1 );
2168 
2169  return linebuf;
2170 }
2171 
2172 void add_history( const char * )
2173 {
2174 }
2175 
2176 void rl_bind_key( char, uint16_t )
2177 {
2178 }
2179 
2180 uint16_t rl_insert = 0;
2181 
2182 int read_history( const char * )
2183 {
2184  return 0;
2185 }
2186 
2187 int write_history( const char * )
2188 {
2189  return 0;
2190 }
2191 #endif
2192 
2193 //------------------------------------------------------------------------------
2194 // Build the command prompt
2195 //------------------------------------------------------------------------------
2196 std::string BuildPrompt( Env *env, const URL &url )
2197 {
2198  std::ostringstream prompt;
2199  std::string cwd = "/";
2200  env->GetString( "CWD", cwd );
2201  prompt << "[" << url.GetHostId() << "] " << cwd << " > ";
2202  return prompt.str();
2203 }
2204 
2205 //------------------------------------------------------------------------
2211 //------------------------------------------------------------------------
2212 bool getArguments (std::vector<std::string> & result, const std::string &input)
2213 {
2214  // the delimiter (space in the case of command line)
2215  static const char delimiter = ' ';
2216  // two types of quotes: single and double quotes
2217  const char singleQuote = '\'', doubleQuote = '\"';
2218  // if the current character of the command has been
2219  // quoted 'currentQuote' holds the type of quote,
2220  // otherwise it holds the null character
2221  char currentQuote = '\0';
2222 
2223  std::string tmp;
2224  for (std::string::const_iterator it = input.begin (); it != input.end (); ++it)
2225  {
2226  // if we encountered a quote character ...
2227  if (*it == singleQuote || *it == doubleQuote)
2228  {
2229  // if we are not within quoted text ...
2230  if (!currentQuote)
2231  {
2232  currentQuote = *it; // set the type of quote
2233  continue; // and continue, the quote character itself is not a part of the parameter
2234  }
2235  // otherwise if it is the closing quote character ...
2236  else if (currentQuote == *it)
2237  {
2238  currentQuote = '\0'; // reset the current quote type
2239  continue; // and continue, the quote character itself is not a part of the parameter
2240  }
2241  }
2242  // if we are within quoted text or the character is not a delimiter ...
2243  if (currentQuote || *it != delimiter)
2244  {
2245  // concatenate it
2246  tmp += *it;
2247  }
2248  else
2249  {
2250  // otherwise add a parameter and erase the tmp string
2251  if (!tmp.empty ())
2252  {
2253  result.push_back(tmp);
2254  tmp.erase ();
2255  }
2256  }
2257  }
2258  // if the there are some remainders of the command add them
2259  if (!tmp.empty())
2260  {
2261  result.push_back(tmp);
2262  }
2263  // return true if the quotation has been closed
2264  return currentQuote == '\0';
2265 }
2266 
2267 //------------------------------------------------------------------------------
2268 // Execute interactive
2269 //------------------------------------------------------------------------------
2270 int ExecuteInteractive( const URL &url, bool noCwd = false )
2271 {
2272  //----------------------------------------------------------------------------
2273  // Set up the environment
2274  //----------------------------------------------------------------------------
2275  std::string historyFile = getenv( "HOME" );
2276  historyFile += "/.xrdquery.history";
2277  rl_bind_key( '\t', rl_insert );
2278  read_history( historyFile.c_str() );
2279  FSExecutor *ex = CreateExecutor( url );
2280 
2281  if( noCwd )
2282  ex->GetEnv()->PutInt( "NoCWD", 1 );
2283 
2284  //----------------------------------------------------------------------------
2285  // Execute the commands
2286  //----------------------------------------------------------------------------
2287  std::string cmdline;
2288  while(1)
2289  {
2290  char *linebuf = 0;
2291  // print new prompt only if the previous line was complete
2292  // (a line is considered not to be complete if a quote has
2293  // been opened but it has not been closed)
2294  linebuf = readline( cmdline.empty() ? BuildPrompt( ex->GetEnv(), url ).c_str() : "> " );
2295  if( !linebuf || !strncmp( linebuf, "exit", 4 ) || !strncmp( linebuf, "quit", 4 ) )
2296  {
2297  std::cout << "Goodbye." << std::endl << std::endl;
2298  break;
2299  }
2300  if( !*linebuf)
2301  {
2302  free( linebuf );
2303  continue;
2304  }
2305  std::vector<std::string> args;
2306  cmdline += linebuf;
2307  free( linebuf );
2308  if (getArguments( args, cmdline ))
2309  {
2310  XRootDStatus st = ex->Execute( args );
2311  add_history( cmdline.c_str() );
2312  cmdline.erase();
2313  if( !st.IsOK() )
2314  std::cerr << st.ToStr() << std::endl;
2315  }
2316  }
2317 
2318  //----------------------------------------------------------------------------
2319  // Cleanup
2320  //----------------------------------------------------------------------------
2321  delete ex;
2322  write_history( historyFile.c_str() );
2323  return 0;
2324 }
2325 
2326 //------------------------------------------------------------------------------
2327 // Execute command
2328 //------------------------------------------------------------------------------
2329 int ExecuteCommand( const URL &url, int argc, char **argv )
2330 {
2331  //----------------------------------------------------------------------------
2332  // Build the command to be executed
2333  //----------------------------------------------------------------------------
2334  std::string commandline;
2335  for( int i = 0; i < argc; ++i )
2336  {
2337  commandline += argv[i];
2338  commandline += " ";
2339  }
2340 
2341  FSExecutor *ex = CreateExecutor( url );
2342  ex->GetEnv()->PutInt( "NoCWD", 1 );
2343  int st = ExecuteCommand( ex, argc, argv );
2344  delete ex;
2345  return st;
2346 }
2347 
2348 //------------------------------------------------------------------------------
2349 // Start the show
2350 //------------------------------------------------------------------------------
2351 int main( int argc, char **argv )
2352 {
2353  //----------------------------------------------------------------------------
2354  // Check the commandline parameters
2355  //----------------------------------------------------------------------------
2357  if( argc == 1 )
2358  {
2359  PrintHelp( 0, 0, params );
2360  return 1;
2361  }
2362 
2363  if( !strcmp( argv[1], "--help" ) ||
2364  !strcmp( argv[1], "-h" ) )
2365  {
2366  PrintHelp( 0, 0, params );
2367  return 0;
2368  }
2369 
2370  bool noCwd = false;
2371  int urlIndex = 1;
2372  if( !strcmp( argv[1], "--no-cwd") )
2373  {
2374  ++urlIndex;
2375  noCwd = true;
2376  }
2377 
2378  URL url( argv[urlIndex] );
2379  if( !url.IsValid() )
2380  {
2381  PrintHelp( 0, 0, params );
2382  return 1;
2383  }
2384 
2385  if( argc == urlIndex + 1 )
2386  return ExecuteInteractive( url, noCwd );
2387  int shift = urlIndex + 1;
2388  return ExecuteCommand( url, argc-shift, argv+shift );
2389 }
@ kXR_fattrDel
Definition: XProtocol.hh:300
@ kXR_fattrSet
Definition: XProtocol.hh:303
@ kXR_fattrList
Definition: XProtocol.hh:302
@ kXR_fattrGet
Definition: XProtocol.hh:301
unsigned char kXR_char
Definition: XPtypes.hh:65
void PrintDirListStatInfo(StatInfo *info, bool hascks=false, uint32_t ownerwidth=0, uint32_t groupwidth=0, uint32_t sizewidth=0, bool human=false, uint64_t base=1000)
Definition: XrdClFS.cc:294
uint32_t nbDigits(uint64_t nb)
Definition: XrdClFS.cc:278
XRootDStatus DoQuery(FileSystem *fs, Env *env, const FSExecutor::CommandParams &args)
Definition: XrdClFS.cc:1243
XRootDStatus DoLocate(FileSystem *fs, Env *env, const FSExecutor::CommandParams &args)
Definition: XrdClFS.cc:849
int write_history(const char *)
Definition: XrdClFS.cc:2187
XRootDStatus DoMkDir(FileSystem *fs, Env *env, const FSExecutor::CommandParams &args)
Definition: XrdClFS.cc:524
void add_history(const char *)
Definition: XrdClFS.cc:2172
XRootDStatus DoRmDir(FileSystem *query, Env *env, const FSExecutor::CommandParams &args)
Definition: XrdClFS.cc:587
XRootDStatus DoRm(FileSystem *fs, Env *env, const FSExecutor::CommandParams &args)
Definition: XrdClFS.cc:680
XRootDStatus DoTail(FileSystem *fs, Env *env, const FSExecutor::CommandParams &args)
Definition: XrdClFS.cc:1620
int ExecuteInteractive(const URL &url, bool noCwd=false)
Definition: XrdClFS.cc:2270
int main(int argc, char **argv)
Definition: XrdClFS.cc:2351
void rl_bind_key(char, uint16_t)
Definition: XrdClFS.cc:2176
XRootDStatus DoStat(FileSystem *fs, Env *env, const FSExecutor::CommandParams &args)
Definition: XrdClFS.cc:1045
XRootDStatus ProcessStatQuery(StatInfo &info, const std::string &query)
Definition: XrdClFS.cc:980
XRootDStatus DoCat(FileSystem *fs, Env *env, const FSExecutor::CommandParams &args)
Definition: XrdClFS.cc:1505
std::string BuildPrompt(Env *env, const URL &url)
Definition: XrdClFS.cc:2196
XRootDStatus DoMv(FileSystem *fs, Env *env, const FSExecutor::CommandParams &args)
Definition: XrdClFS.cc:628
XRootDStatus DoPrepare(FileSystem *fs, Env *env, const FSExecutor::CommandParams &args)
Definition: XrdClFS.cc:1350
int ExecuteCommand(FSExecutor *ex, int argc, char **argv)
Definition: XrdClFS.cc:2138
XRootDStatus DoChMod(FileSystem *fs, Env *env, const FSExecutor::CommandParams &args)
Definition: XrdClFS.cc:800
XRootDStatus DoLS(FileSystem *fs, Env *env, const FSExecutor::CommandParams &args)
Definition: XrdClFS.cc:348
bool getArguments(std::vector< std::string > &result, const std::string &input)
Definition: XrdClFS.cc:2212
XRootDStatus DoCD(FileSystem *fs, Env *env, const FSExecutor::CommandParams &args)
Definition: XrdClFS.cc:227
XRootDStatus DoXAttr(FileSystem *fs, Env *env, const FSExecutor::CommandParams &args)
Definition: XrdClFS.cc:1792
XRootDStatus DoStatVFS(FileSystem *fs, Env *env, const FSExecutor::CommandParams &args)
Definition: XrdClFS.cc:1181
XRootDStatus DoCache(FileSystem *fs, Env *env, const FSExecutor::CommandParams &args)
Definition: XrdClFS.cc:165
XRootDStatus DoSpaceInfo(FileSystem *fs, Env *env, const FSExecutor::CommandParams &args)
Definition: XrdClFS.cc:1749
XRootDStatus BuildPath(std::string &newPath, Env *env, const std::string &path)
Definition: XrdClFS.cc:56
FSExecutor * CreateExecutor(const URL &url)
Definition: XrdClFS.cc:2108
int read_history(const char *)
Definition: XrdClFS.cc:2182
char * readline(const char *prompt)
Definition: XrdClFS.cc:2157
XRootDStatus ConvertMode(Access::Mode &mode, const std::string &modeStr)
Definition: XrdClFS.cc:120
uint16_t rl_insert
Definition: XrdClFS.cc:2180
std::string getSizeStr(uint64_t size, bool human, uint64_t base)
Definition: XrdClFS.cc:284
XRootDStatus DoTruncate(FileSystem *fs, Env *env, const FSExecutor::CommandParams &args)
Definition: XrdClFS.cc:751
XRootDStatus PrintHelp(FileSystem *, Env *, const FSExecutor::CommandParams &)
Definition: XrdClFS.cc:1956
static bool is_subdirectory(const std::string_view dir, const std::string_view subdir)
#define write(a, b, c)
Definition: XrdPosix.hh:123
void getline(uchar *buff, int blen)
const char * XrdSysE2T(int errcode)
Definition: XrdSysE2T.cc:104
virtual void EndJob(uint32_t jobNum, const XrdCl::PropertyList *results)
Definition: XrdClFS.cc:1465
virtual void JobProgress(uint32_t jobNum, uint64_t bytesProcessed, uint64_t bytesTotal)
Definition: XrdClFS.cc:1474
Binary blob representation.
Definition: XrdClBuffer.hh:34
void FromString(const std::string str)
Fill the buffer from a string.
Definition: XrdClBuffer.hh:205
std::string ToString() const
Convert the buffer to a string.
Definition: XrdClBuffer.hh:215
Copy the data from one point to another.
XRootDStatus Run(CopyProgressHandler *handler)
Run the copy jobs.
XRootDStatus Prepare()
XRootDStatus AddJob(const PropertyList &properties, PropertyList *results)
Interface for copy progress notification.
static Log * GetLog()
Get default log.
static Env * GetEnv()
Get default client environment.
DirList::iterator Iterator
Directory listing iterator.
Iterator End()
Get the end iterator.
Iterator Begin()
Get the begin iterator.
const std::string & GetParentName() const
Get parent directory name.
bool PutInt(const std::string &key, int value)
Definition: XrdClEnv.cc:110
bool PutString(const std::string &key, const std::string &value)
Definition: XrdClEnv.cc:52
bool GetString(const std::string &key, std::string &value)
Definition: XrdClEnv.cc:31
bool GetInt(const std::string &key, int &value)
Definition: XrdClEnv.cc:89
Execute queries given as a commandline.
std::vector< std::string > CommandParams
Definition of command argument list.
Env * GetEnv()
Get the environment.
XRootDStatus Execute(const CommandParams &args)
bool AddCommand(const std::string &name, Command command)
Container for space information.
uint64_t GetUsed() const
Amount of used space in MB.
uint64_t GetLargestFreeChunk() const
Largest single chunk of free space.
uint64_t GetTotal() const
Amount of total space in MB.
uint64_t GetFree() const
Amount of free space in MB.
static XRootDStatus GetSpaceInfo(SpaceInfo *&result, FileSystem *fs, const std::string &path)
Recursively get space information for given path.
Send file/filesystem queries to an XRootD cluster.
XRootDStatus Mv(const std::string &source, const std::string &dest, ResponseHandler *handler, time_t timeout=0) XRD_WARN_UNUSED_RESULT
XRootDStatus DirList(const std::string &path, DirListFlags::Flags flags, ResponseHandler *handler, time_t timeout=0) XRD_WARN_UNUSED_RESULT
XRootDStatus Locate(const std::string &path, OpenFlags::Flags flags, ResponseHandler *handler, time_t timeout=0) XRD_WARN_UNUSED_RESULT
XRootDStatus SendCache(const std::string &info, ResponseHandler *handler, time_t timeout=0) XRD_WARN_UNUSED_RESULT
XRootDStatus Truncate(const std::string &path, uint64_t size, ResponseHandler *handler, time_t timeout=0) XRD_WARN_UNUSED_RESULT
XRootDStatus RmDir(const std::string &path, ResponseHandler *handler, time_t timeout=0) XRD_WARN_UNUSED_RESULT
XRootDStatus ListXAttr(const std::string &path, ResponseHandler *handler, time_t timeout=0)
XRootDStatus Stat(const std::string &path, ResponseHandler *handler, time_t timeout=0) XRD_WARN_UNUSED_RESULT
XRootDStatus StatVFS(const std::string &path, ResponseHandler *handler, time_t timeout=0) XRD_WARN_UNUSED_RESULT
XRootDStatus GetXAttr(const std::string &path, const std::vector< std::string > &attrs, ResponseHandler *handler, time_t timeout=0)
XRootDStatus ChMod(const std::string &path, Access::Mode mode, ResponseHandler *handler, time_t timeout=0) XRD_WARN_UNUSED_RESULT
XRootDStatus DelXAttr(const std::string &path, const std::vector< std::string > &attrs, ResponseHandler *handler, time_t timeout=0)
XRootDStatus Prepare(const std::vector< std::string > &fileList, PrepareFlags::Flags flags, uint8_t priority, ResponseHandler *handler, time_t timeout=0) XRD_WARN_UNUSED_RESULT
XRootDStatus MkDir(const std::string &path, MkDirFlags::Flags flags, Access::Mode mode, ResponseHandler *handler, time_t timeout=0) XRD_WARN_UNUSED_RESULT
XRootDStatus Query(QueryCode::Code queryCode, const Buffer &arg, ResponseHandler *handler, time_t timeout=0) XRD_WARN_UNUSED_RESULT
XRootDStatus SetXAttr(const std::string &path, const std::vector< xattr_t > &attrs, ResponseHandler *handler, time_t timeout=0)
bool GetProperty(const std::string &name, std::string &value) const
XRootDStatus DeepLocate(const std::string &path, OpenFlags::Flags flags, ResponseHandler *handler, time_t timeout=0) XRD_WARN_UNUSED_RESULT
A file.
Definition: XrdClFile.hh:52
XRootDStatus Open(const std::string &url, OpenFlags::Flags flags, Access::Mode mode, ResponseHandler *handler, time_t timeout=0) XRD_WARN_UNUSED_RESULT
Definition: XrdClFile.cc:125
XRootDStatus Read(uint64_t offset, uint32_t size, void *buffer, ResponseHandler *handler, time_t timeout=0) XRD_WARN_UNUSED_RESULT
Definition: XrdClFile.cc:265
XRootDStatus Close(ResponseHandler *handler, time_t timeout=0) XRD_WARN_UNUSED_RESULT
Definition: XrdClFile.cc:210
XRootDStatus Stat(bool force, ResponseHandler *handler, time_t timeout=0) XRD_WARN_UNUSED_RESULT
Definition: XrdClFile.cc:236
Path location info.
Iterator Begin()
Get the location begin iterator.
@ Read
read access is allowed
@ ReadWrite
write access is allowed
@ ServerPending
server node where the file is pending to be online
@ ManagerOnline
manager node where the file is online
@ ServerOnline
server node where the file is online
@ ManagerPending
manager node where the file is pending to be online
LocationList::iterator Iterator
Iterator over locations.
Iterator End()
Get the location end iterator.
Handle diagnostics.
Definition: XrdClLog.hh:101
void Error(uint64_t topic, const char *format,...)
Report an error.
Definition: XrdClLog.cc:231
void Debug(uint64_t topic, const char *format,...)
Print a debug message.
Definition: XrdClLog.cc:282
Pipeline exception, wrapps an XRootDStatus.
const XRootDStatus & GetError() const
A key-value pair map storing both keys and values as strings.
uint64_t GetFreeRW() const
Get size of the largest contiguous area of free r/w space (in MB)
uint64_t GetNodesStaging() const
Get number of nodes that can provide staging space.
uint8_t GetUtilizationStaging() const
Get percentage of the partition utilization represented by FreeStaging.
uint64_t GetFreeStaging() const
Get size of the largest contiguous area of free staging space (in MB)
uint8_t GetUtilizationRW() const
Get percentage of the partition utilization represented by FreeRW.
uint64_t GetNodesRW() const
Get number of nodes that can provide read/write space.
Object stat info.
std::string GetChangeTimeAsString() const
Get change time.
std::string GetModTimeAsString() const
Get modification time.
bool HasChecksum() const
Has checksum.
bool TestFlags(uint32_t flags) const
Test flags.
uint64_t GetSize() const
Get size (in bytes)
const std::string GetModeAsOctString() const
Get mode.
const std::string & GetOwner() const
Get owner.
@ IsReadable
Read access is allowed.
@ IsDir
This is a directory.
@ Other
Neither a file nor a directory.
@ BackUpExists
Back up copy exists.
@ XBitSet
Executable/searchable bit set.
@ Offline
File is not online (ie. on disk)
@ IsWritable
Write access is allowed.
uint32_t GetFlags() const
Get flags.
bool ExtendedFormat() const
Has extended stat information.
const std::string & GetModeAsString() const
Get mode.
const std::string & GetId() const
Get id.
const std::string & GetGroup() const
Get group.
std::string GetAccessTimeAsString() const
Get change time.
const std::string & GetChecksum() const
Get checksum.
URL representation.
Definition: XrdClURL.hh:31
std::string GetHostId() const
Get the host part of the URL (user:password@host:port)
Definition: XrdClURL.hh:99
std::string GetURL() const
Get the URL.
Definition: XrdClURL.hh:86
std::string GetObfuscatedURL() const
Get the URL with authz information obfuscated.
Definition: XrdClURL.cc:498
void SetPath(const std::string &path)
Set the path.
Definition: XrdClURL.hh:225
bool IsValid() const
Is the url valid.
Definition: XrdClURL.cc:452
static void splitString(Container &result, const std::string &input, const std::string &delimiter)
Split a string.
Definition: XrdClUtils.hh:56
static std::string BytesToString(uint64_t bytes)
Convert bytes to a human readable string.
Definition: XrdClUtils.cc:367
std::string ToStr() const
Convert to string.
static std::string genHumanSize(size_t size, uint64_t base)
Definition: XrdOucUtils.cc:461
const uint16_t suPartial
Definition: XrdClStatus.hh:41
const uint16_t errInvalidAddr
Definition: XrdClStatus.hh:71
RmImpl< false > Rm
const uint16_t stError
An error occurred that could potentially be retried.
Definition: XrdClStatus.hh:32
const uint64_t AppMsg
const uint16_t errResponseNegative
Query response was negative.
Definition: XrdClStatus.hh:109
StatImpl< false > Stat(Ctx< File > file, Arg< bool > force, time_t timeout=0)
std::future< XRootDStatus > Async(Pipeline pipeline, time_t timeout=0)
ParallelOperation< false > Parallel(Container &&container)
Factory function for creating parallel operation from a vector.
const uint16_t errInvalidArgs
Definition: XrdClStatus.hh:58
XRootDStatus WaitFor(Pipeline pipeline, time_t timeout=0)
Mode
Access mode.
@ OX
world executable/browsable
@ OW
world writeable
@ UR
owner readable
@ GR
group readable
@ UW
owner writable
@ GX
group executable/browsable
@ GW
group writable
@ UX
owner executable/browsable
@ OR
world readable
@ Stat
Stat each entry.
@ Merge
Merge duplicates.
@ Zip
List content of ZIP files.
@ Recursive
Do a recursive listing.
@ Cksm
Get checksum for every entry.
@ None
Nothing special.
@ MakePath
create the entire directory tree if it doesn't exist
Flags
Open flags, may be or'd when appropriate.
@ Read
Open only for reading.
@ Cancel
cancel staging request
@ Colocate
co-locate staged files, if possible
Code
XRootD query request codes.
@ OpaqueFile
Implementation dependent.
@ XAttr
Query file extended attributes.
@ Opaque
Implementation dependent.
@ Config
Query server configuration.
@ Stats
Query server stats.
@ ChecksumCancel
Query file checksum cancellation.
@ Checksum
Query file checksum.
@ Space
Query logical space stats.
@ Prepare
Query prepare status.
uint16_t code
Error type, or additional hints on what to do.
Definition: XrdClStatus.hh:147
bool IsOK() const
We're fine.
Definition: XrdClStatus.hh:124
int GetShellCode() const
Get the status code that may be returned to the shell.
Definition: XrdClStatus.hh:129
Extended attribute operation status.
Extended attributes with status.