Skip to content
Snippets Groups Projects
Commit 0ff70130 authored by Daniele Kruse's avatar Daniele Kruse
Browse files

WIP writing the xrdcp cmdline interface

parent 528ad67d
No related branches found
No related tags found
No related merge requests found
......@@ -8,7 +8,7 @@ add_executable (CTA_cmd CTACmdMain.cpp CTACmd.cpp)
target_link_libraries (CTA_cmd ${XROOTD_XRDCL_LIB})
add_executable (CTA_copycmd CTACopyCmdMain.cpp CTACopyCmd.cpp)
target_link_libraries (CTA_copycmd ${XROOTD_XRDCL_LIB})
target_link_libraries (CTA_copycmd ${XROOTD_XRDCL_LIB} ctacommon)
add_executable (CTA_dircmd CTADirCmdMain.cpp CTADirCmd.cpp)
target_link_libraries (CTA_dircmd ${XROOTD_XRDCL_LIB})
......@@ -16,46 +16,31 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "CTACopyCmd.hpp"
#include "cmdline/CTACopyCmd.hpp"
#include "common/exception/Exception.hpp"
#include "XrdCl/XrdClFileSystem.hh"
#include "XrdCl/XrdClCopyProcess.hh"
#include "XrdOuc/XrdOucString.hh"
#include <getopt.h>
#include <iostream>
#include <string.h>
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
CTACopyCmd::CTACopyCmd() throw(): m_programName("CTA_copycmd") {
}
//------------------------------------------------------------------------------
// usage
//------------------------------------------------------------------------------
void CTACopyCmd::usage(std::ostream &os) const throw() {
os <<
"Usage:\n"
"\t" << m_programName << " ls <directory_name>\n";
CTACopyCmd::CTACopyCmd() throw() {
}
//------------------------------------------------------------------------------
// main
//------------------------------------------------------------------------------
int CTACopyCmd::main(const int argc, char **argv) throw() {
// if(argc < 2) {
// usage(std::cerr);
// return 1;
// }
int CTACopyCmd::main(const int argc, const char **argv) const throw() {
int rc = 1;
// Execute the command
try {
rc = executeCommand(argc, argv);
rc = sendCommand(argc, argv);
} catch(std::exception &ex) {
std::cerr << std::endl << "Failed to execute the archive command:\n\n" << ex.what() << std::endl;
std::cerr << std::endl << "Failed to execute the command. Reason: " << ex.what() << std::endl;
return 1;
}
......@@ -63,45 +48,62 @@ int CTACopyCmd::main(const int argc, char **argv) throw() {
}
//------------------------------------------------------------------------------
// executeCommand
// sendCommand
//------------------------------------------------------------------------------
int CTACopyCmd::executeCommand(const int argc, char **argv) {
int CTACopyCmd::sendCommand(const int argc, const char **argv) const {
XrdCl::PropertyList properties;
properties.Set("source", "root://localhost//himama");
properties.Set("target", "/afs/cern.ch/user/d/dkruse/lola.txt");
properties.Set("force", true);
properties.Set("source", formatCommandPath(argc, argv));
properties.Set("target", "-"); //destination is stdout
XrdCl::PropertyList results;
XrdCl::CopyProcess copyProcess;
XrdCl::XRootDStatus status = copyProcess.AddJob(properties, &results);
if(status.IsOK())
if(!status.IsOK())
{
std::cout << "Job added" << std::endl;
}
else
{
std::cout << "Job adding error" << std::endl;
throw cta::exception::Exception("Job adding error");
}
status = copyProcess.Prepare();
if(status.IsOK())
if(!status.IsOK())
{
std::cout << "Job prepared" << std::endl;
}
else
{
std::cout << "Job preparing error" << std::endl;
throw cta::exception::Exception("Job preparing error");
}
XrdCl::CopyProgressHandler copyProgressHandler;
status = copyProcess.Run(&copyProgressHandler);
if(status.IsOK())
{
std::cout << "Copy OK" << std::endl;
}
else
if(!status.IsOK())
{
std::cout << "Copy error" << std::endl;
throw cta::exception::Exception("Copy process run error");
}
return 0;
}
//------------------------------------------------------------------------------
// formatCommandPath
//------------------------------------------------------------------------------
std::string CTACopyCmd::formatCommandPath(const int argc, const char **argv) const {
std::string cmdPath = "root://localhost//";
std::string arg = argv[0];
replaceAll(arg, "&", "_#_and_#_");
cmdPath += arg;
for(int i=1; i<argc; i++) {
std::string arg = argv[i];
replaceAll(arg, "&", "_#_and_#_");
cmdPath += "&";
cmdPath += arg;
}
return cmdPath;
}
//------------------------------------------------------------------------------
// replaceAll
//------------------------------------------------------------------------------
void CTACopyCmd::replaceAll(std::string& str, const std::string& from, const std::string& to) const {
if(from.empty() || str.empty())
return;
size_t start_pos = 0;
while((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length();
}
}
\ No newline at end of file
......@@ -18,9 +18,6 @@
#pragma once
#include <exception>
#include <istream>
#include <ostream>
#include <string>
/**
......@@ -39,29 +36,35 @@ public:
* @param argc The number of command-line arguments.
* @param argv The command-line arguments.
*/
int main(const int argc, char **argv) throw();
int main(const int argc, const char **argv) const throw();
protected:
/**
* The name of the program.
*/
const std::string m_programName;
/**
* Writes the command-line usage message of to the specified output stream.
* Sends the command and waits for the reply
*
* @param os Output stream to be written to.
* @param argc The number of command-line arguments.
* @param argv The command-line arguments.
* @return the return code
*/
void usage(std::ostream &os) const throw();
int sendCommand(const int argc, const char **argv) const;
/**
* Sends the archive request and waits for the reply
* Formats the command path string
*
* @param argc The number of command-line arguments.
* @param argv The command-line arguments.
* @return the return code
* @return the command string
*/
std::string formatCommandPath(const int argc, const char **argv) const;
/**
* Replaces all occurrences in a string "str" of a substring "from" with the string "to"
*
* @param str The original string
* @param from The substring to replace
* @param to The replacement string
*/
int executeCommand(const int argc, char **argv) ;
void replaceAll(std::string& str, const std::string& from, const std::string& to) const;
}; // class CTACopyCmd
......@@ -21,7 +21,7 @@
//------------------------------------------------------------------------------
// main
//------------------------------------------------------------------------------
int main(int argc, char **argv) {
int main(const int argc, const char **argv) {
CTACopyCmd command;
......
cmake_minimum_required (VERSION 2.6)
set (CTA_REMOTE_STORAGE_SRC_FILES
MockRemoteStorage.cpp
EosRemoteStorage.cpp
RemoteStorage.cpp)
......
......@@ -6,4 +6,4 @@ include_directories (${CMAKE_SOURCE_DIR})
include_directories (${XROOTD_INCLUDE_DIR} ${XROOTD_PRIVATE_INCLUDE_DIR})
add_library (XrdProFst MODULE XrdProFilesystem.cpp XrdProFile.cpp XrdProDir.cpp ParsedRequest.cpp)
target_link_libraries (XrdProFst ctascheduler ctacommon ctanameserver)
target_link_libraries (XrdProFst ctascheduler ctacommon ctanameserver ctaremotestorage)
......@@ -18,13 +18,30 @@
#include "XrdProFile.hpp"
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
//------------------------------------------------------------------------------
// open
//------------------------------------------------------------------------------
int XrdProFile::open(const char *fileName, XrdSfsFileOpenMode openMode, mode_t createMode, const XrdSecEntity *client, const char *opaque) {
m_data = "Hello this is my data";
std::vector<std::string> tokens;
std::stringstream ss(fileName);
std::string item;
while (std::getline(ss, item, '&')) {
replaceAll(item, "_#_and_#_", "&");
tokens.push_back(item);
}
m_data = "Hello this is original command received:";
for(auto i=tokens.begin(); i!=tokens.end(); i++) {
m_data += " ";
m_data += *i;
}
m_data += "\n";
return SFS_OK;
}
......@@ -55,8 +72,9 @@ const char* XrdProFile::FName() {
// getMmap
//------------------------------------------------------------------------------
int XrdProFile::getMmap(void **Addr, off_t &Size) {
*Addr = const_cast<char *>(m_data.c_str()); Size = m_data.length();
return SFS_ERROR;
*Addr = const_cast<char *>(m_data.c_str());
Size = m_data.length();
return SFS_OK; //change to "return SFS_ERROR;" in case the read function below is wanted
}
//------------------------------------------------------------------------------
......@@ -71,13 +89,15 @@ XrdSfsXferSize XrdProFile::read(XrdSfsFileOffset offset, XrdSfsXferSize size) {
// read
//------------------------------------------------------------------------------
XrdSfsXferSize XrdProFile::read(XrdSfsFileOffset offset, char *buffer, XrdSfsXferSize size) {
if((unsigned long)offset<m_data.length()) {
strncpy(buffer, m_data.c_str()+offset, size);
return m_data.length()-offset;
}
else {
return 0;
}
// if((unsigned long)offset<m_data.length()) {
// strncpy(buffer, m_data.c_str()+offset, size);
// return m_data.length()-offset;
// }
// else {
// return 0;
// }
error.setErrInfo(ENOTSUP, "Not supported.");
return 0;
}
//------------------------------------------------------------------------------
......@@ -154,4 +174,17 @@ XrdProFile::XrdProFile(const char *user, int MonID): error(user, MonID) {
// Destructor
//------------------------------------------------------------------------------
XrdProFile::~XrdProFile() {
}
//------------------------------------------------------------------------------
// replaceAll
//------------------------------------------------------------------------------
void XrdProFile::replaceAll(std::string& str, const std::string& from, const std::string& to) const {
if(from.empty() || str.empty())
return;
size_t start_pos = 0;
while((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length();
}
}
\ No newline at end of file
......@@ -43,5 +43,18 @@ public:
XrdProFile(const char *user=0, int MonID=0);
~XrdProFile();
protected:
/**
* This is the string holding the result of the command
*/
std::string m_data;
/**
* Replaces all occurrences in a string "str" of a substring "from" with the string "to"
*
* @param str The original string
* @param from The substring to replace
* @param to The replacement string
*/
void replaceAll(std::string& str, const std::string& from, const std::string& to) const;
};
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment