CoEPP RC
 

Xrootd client classes

This page includes some simple examples of how to use Xrd C++ libraries to manipulate files on an xrootd server.

In order to use Xrootd client C++ classes you need to do the following to setup your environment

setupATLAS
localSetupROOT
localSetupXRootD

Reading a file

Here's an example program to read a file through the xrootd server in Sydney

#include <iostream>
#include <XrdClient/XrdClient.hh>
#include <fstream>
#include <vector>
#include <string>
#include <sys/time.h>
#include <math.h>

#include "XrdClient/XrdClientEnv.hh"
#include "XrdSys/XrdSysHeaders.hh"

kXR_unt16 open_mode = (kXR_ur);
kXR_unt16 open_opts = (1);

//To compile
//g++ mytest.cc -I/usr/include/xrootd -lXrdClient -o mytest

int main(void)
{

  char mybuffer[100];

  XrdClient *cli = new XrdClient("root://xrdsydsr.syd.coepp.org.au//coepp/local/antonio/eggs");
  cli->Open(open_mode,open_opts);
  cli->Read(mybuffer,0,30);
  std::cout << mybuffer ;

  return 1;
}

To compile and run

> g++ mytest.cc -I/usr/include/xrootd -lXrdClient -o mytest
> ./mytest
hello world

Writing a file

Here's an example of how to write a file

#include <iostream>
#include <XrdClient/XrdClient.hh>
#include <fstream>
#include <vector>
#include <string>
#include <sys/time.h>
#include <math.h>

#include "XrdClient/XrdClientEnv.hh"
#include "XrdSys/XrdSysHeaders.hh"

kXR_unt16 open_mode = (kXR_or);
kXR_unt16 open_opts = (3); //new

//To compile
//g++ mywrite.cc -I/usr/include/xrootd -lXrdClient -o mywrite

int main(void)
{

  char mybuffer[100]={"hello world!"};
  char mybuffer2[100];

  XrdClient *cli = new XrdClient("root://xrdsydsr.syd.coepp.org.au//coepp/local/antonio/rilke");
  cli->Open(open_mode,open_opts);
  cli->Write(mybuffer, 0, 30);
  cli->Read(mybuffer2,0,30);
  std::cout << mybuffer2 << endl;

  return 1;
}

To compile and run

>./xrdtestcpp>g++ mywrite.cc -I/usr/include/xrootd -lXrdClient -o mywrite
>./xrdtestcpp>./mywrite
hello world!

Pay close attention to the attributes passed in as an argument to the file opening i.e. open_mode and open_opts.

kXR_unt16 open_mode = (kXR_or);
kXR_unt16 open_opts = (3); //new
  cli->Open(open_mode,open_opts);

OpenFlags.NEW ensures that a new file is opened only if it does not already exist. AccessMode.OR makes the file world readable. This is important if the file exists in an area which can be accessed via an alternative method to xrootd. In the example above, the file is written in the disk server that is also accessible from cxin01/02 machines as /data/antonio. This example writes a file named “cath” at /coepp/atlas/cloud/antonio/cath in the CoEPP xrootd namespace, which is equivalent to /data/antonio/cath in cxin01. However the file although written in user antonio's directory “antonio” will not be readable unless the AccessMode is set to world readable.

Information on the possible open flags

  1. 0 = OpenFlags.DELETE: Open a new file, deleting any existing file
  2. 1 = OpenFlags.FORCE: Ignore file usage rules
  3. 2 = OpenFlags.NEW: Open the file only if it does not already exist
  4. 3 = OpenFlags.READ: Open only for reading
  5. 4 = OpenFlags.UPDATE: Open for reading and writing
  6. 5 = OpenFlags.REFRESH: Refresh the cached information on file location. Voids NoWait.
  7. 6 = OpenFlags.MAKEPATH: Create directory path if it doesn’t already exist
  8. 7 = OpenFlags.APPEND: Open only for appending
  9. 8 = OpenFlags.REPLICA: The file is being opened for replica creation
  10. 9 = OpenFlags.POSC: Enable Persist On Successful Close processing
  11. 10 = OpenFlags.NOWAIT:
  12. 11 = OpenFlags.SEQIO: File will be read or written sequentially
tier3/faq/accessing_xrootd_storage_using_cpp_libraries.txt · Last modified: 2014/03/31 14:22 by antonio
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki