Commit b83c2612 authored by Daniele Kruse's avatar Daniele Kruse
Browse files

Using the xroot dir interface for a directory listing command

parent 6ac49b79
......@@ -9,3 +9,6 @@ target_link_libraries (CTA_cmd ${XROOTD_XRDCL_LIB})
add_executable (CTA_copycmd CTACopyCmdMain.cpp CTACopyCmd.cpp)
target_link_libraries (CTA_copycmd ${XROOTD_XRDCL_LIB})
add_executable (CTA_dircmd CTADirCmdMain.cpp CTADirCmd.cpp)
target_link_libraries (CTA_dircmd ${XROOTD_XRDCL_LIB})
#include "CTADirCmd.hpp"
#include "XrdCl/XrdClFileSystem.hh"
#include "XrdCl/XrdClCopyProcess.hh"
#include "XrdOuc/XrdOucString.hh"
#include <getopt.h>
#include <iostream>
#include <string.h>
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
CTADirCmd::CTADirCmd() throw(): m_programName("CTA_dircmd") {
}
//------------------------------------------------------------------------------
// usage
//------------------------------------------------------------------------------
void CTADirCmd::usage(std::ostream &os) const throw() {
os <<
"Usage:\n"
"\t" << m_programName << " <directory_name>\n";
}
//------------------------------------------------------------------------------
// main
//------------------------------------------------------------------------------
int CTADirCmd::main(const int argc, char **argv) throw() {
// if(argc < 2) {
// usage(std::cerr);
// return 1;
// }
int rc = 1;
// Execute the command
try {
rc = executeCommand(argc, argv);
} catch(std::exception &ex) {
std::cerr << std::endl << "Failed to execute the archive command:\n\n" << ex.what() << std::endl;
return 1;
}
return rc;
}
//------------------------------------------------------------------------------
// executeCommand
//------------------------------------------------------------------------------
int CTADirCmd::executeCommand(const int argc, char **argv) {
XrdCl::FileSystem fs(XrdCl::URL("localhost"));
XrdCl::DirectoryList *response;
XrdCl::XRootDStatus status = fs.DirList("/", XrdCl::DirListFlags::None, response);
if(status.IsOK()) {
for(XrdCl::DirectoryList::ConstIterator it=response->Begin(); it!=response->End(); it++) {
std::cout << (*it)->GetName() << std::endl;
}
}
else {
std::cout << "Query error" << std::endl;
}
return 0;
}
#pragma once
#include <exception>
#include <istream>
#include <ostream>
#include <string>
/**
* Class implementing the business logic of the archive command-line tool.
*/
class CTADirCmd {
public:
/**
* Constructor.
*/
CTADirCmd() throw();
/**
* The entry function of the command.
*
* @param argc The number of command-line arguments.
* @param argv The command-line arguments.
*/
int main(const int argc, char **argv) throw();
protected:
/**
* The name of the program.
*/
const std::string m_programName;
/**
* Writes the command-line usage message of to the specified output stream.
*
* @param os Output stream to be written to.
*/
void usage(std::ostream &os) const throw();
/**
* Sends the archive request and waits for the reply
*
* @param argc The number of command-line arguments.
* @param argv The command-line arguments.
* @return the return code
*/
int executeCommand(const int argc, char **argv) ;
}; // class CTADirCmd
#include "CTADirCmd.hpp"
//------------------------------------------------------------------------------
// main
//------------------------------------------------------------------------------
int main(int argc, char **argv) {
CTADirCmd command;
return command.main(argc, argv);
}
......@@ -4,5 +4,5 @@ list (APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
find_package (xrootd REQUIRED)
include_directories (${XROOTD_INCLUDE_DIR} ${XROOTD_PRIVATE_INCLUDE_DIR} ${CMAKE_SOURCE_DIR})
add_library (XrdProFst MODULE XrdProFilesystem.cpp XrdProFile.cpp ParsedRequest.cpp)
add_library (XrdProFst MODULE XrdProFilesystem.cpp XrdProFile.cpp XrdProDir.cpp ParsedRequest.cpp)
target_link_libraries (XrdProFst ctaclient)
\ No newline at end of file
#include "XrdProDir.hpp"
#include <pwd.h>
//------------------------------------------------------------------------------
// checkClient
//------------------------------------------------------------------------------
int XrdProDir::checkClient(const XrdSecEntity *client, cta::SecurityIdentity &requester) {
if(!client || !client->host || strncmp(client->host, "localhost", 9))
{
error.setErrInfo(ENOTSUP, "[ERROR] Operation only possible from localhost.");
return SFS_ERROR;
}
struct passwd pwd;
struct passwd *result;
char *buf;
long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
if (bufsize == -1)
{
bufsize = 16384;
}
buf = (char *)malloc((size_t)bufsize);
if(buf == NULL)
{
free(buf);
error.setErrInfo(ENOTSUP, "[ERROR] malloc of the buffer failed");
return SFS_ERROR;
}
int rc = getpwnam_r(client->name, &pwd, buf, bufsize, &result);
if(result == NULL)
{
if (rc == 0)
{
std::string response = "[ERROR] User ";
response += client->name;
response += " not found";
free(buf);
error.setErrInfo(ENOTSUP, response.c_str());
return SFS_ERROR;
}
else
{
free(buf);
error.setErrInfo(ENOTSUP, "[ERROR] getpwnam_r failed");
return SFS_ERROR;
}
}
std::cout << "Dir request received from client. Username: " << client->name << " uid: " << pwd.pw_uid << " gid: " << pwd.pw_gid << std::endl;
requester.host = client->host;
requester.user.uid = pwd.pw_uid;
requester.user.gid = pwd.pw_gid;
free(buf);
return SFS_OK;
}
//------------------------------------------------------------------------------
// open
//------------------------------------------------------------------------------
int XrdProDir::open(const char *path, const XrdSecEntity *client, const char *opaque) {
cta::SecurityIdentity requester;
int checkResult = checkClient(client, requester);
if(SFS_OK!=checkResult) {
return checkResult;
}
m_itor = m_clientAPI->getDirectoryContents(requester, path);
return SFS_OK;
}
//------------------------------------------------------------------------------
// nextEntry
//------------------------------------------------------------------------------
const char * XrdProDir::nextEntry() {
if(m_itor.hasMore()) {
return m_itor.next().getName().c_str();
}
else {
return NULL;
}
}
//------------------------------------------------------------------------------
// close
//------------------------------------------------------------------------------
int XrdProDir::close() {
return SFS_OK;
}
//------------------------------------------------------------------------------
// FName
//------------------------------------------------------------------------------
const char * XrdProDir::FName() {
return NULL;
}
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
XrdProDir::XrdProDir(cta::MiddleTierAdmin *clientAPI, const char *user, int MonID): error(user, MonID) {
m_clientAPI = clientAPI;
}
//------------------------------------------------------------------------------
// destructor
//------------------------------------------------------------------------------
XrdProDir::~XrdProDir() {
}
\ No newline at end of file
#pragma once
#include <iostream>
#include "XrdSec/XrdSecEntity.hh"
#include "XrdSfs/XrdSfsInterface.hh"
#include "libs/client/MockMiddleTierAdmin.hpp"
class XrdProDir : public XrdSfsDirectory
{
public:
XrdOucErrInfo error;
virtual int open(const char *path, const XrdSecEntity *client = 0, const char *opaque = 0);
virtual const char *nextEntry();
virtual int close();
virtual const char *FName();
XrdProDir(cta::MiddleTierAdmin *clientAPI, const char *user=0, int MonID=0);
virtual ~XrdProDir();
protected:
/**
* Iterator holding contents of the directory
*/
cta::DirectoryIterator m_itor;
/**
* Pointer to the client API object
*/
cta::MiddleTierAdmin *m_clientAPI;
/**
* Checks whether client has correct permissions and fills the UserIdentity structure
*
* @param req parsed request
* @param requester The structure to be filled
* @return SFS_OK in case check is passed, SFS_ERROR otherwise
*/
int checkClient(const XrdSecEntity *client, cta::SecurityIdentity &requester);
}; // class XrdProDir
\ No newline at end of file
#include "XrdProFilesystem.hpp"
#include "XrdProFile.hpp"
#include "XrdProDir.hpp"
#include "XrdOuc/XrdOucString.hh"
#include "XrdSec/XrdSecEntity.hh"
......@@ -1022,6 +1023,14 @@ XrdSfsFile * XrdProFilesystem::newFile(char *user, int MonID)
return new XrdProFile(user, MonID);
}
//------------------------------------------------------------------------------
// newDir
//------------------------------------------------------------------------------
XrdSfsDirectory * XrdProFilesystem::newDir(char *user, int MonID)
{
return new XrdProDir(m_clientAPI, user, MonID);
}
//------------------------------------------------------------------------------
// fsctl
//------------------------------------------------------------------------------
......@@ -1139,15 +1148,6 @@ int XrdProFilesystem::truncate(const char *path, XrdSfsFileOffset fsize, XrdOucE
return SFS_ERROR;
}
//------------------------------------------------------------------------------
// newDir
//------------------------------------------------------------------------------
XrdSfsDirectory * XrdProFilesystem::newDir(char *user, int MonID)
{
(void)user; (void)MonID;
return NULL;
}
//------------------------------------------------------------------------------
// chksum
//------------------------------------------------------------------------------
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment