ATLAS Offline Software
Loading...
Searching...
No Matches
FCcmd.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
8
11
12#include <exception>
13#include <memory>
14
15using namespace pool;
16using std::cerr, std::cout, std::endl;
17using std::string;
18
20 cout <<
21 "Usage: " << endl <<
22 "FCcmd <action> <item> [-p PFName -l LFName -n newPFName -g GUID -u catalogName -h]" << endl <<
23 " supported actions are:" <<endl <<
24 " register : <item> = lfn|pfn (registering a PFN requires a GUID)" <<endl <<
25 " list : <item> = guid|lfn|pfn" <<endl <<
26 " delete : deletes an entry using LFN or PFN" <<endl <<
27 " rename : rename PFName to a new PFName" <<endl <<
28 " example: FCcmd list pfn -l myLogicalFileName" << endl;
29}
30
31
32class Options {
33 public:
34 Options(int argc, char* argv[]);
35
36 inline const string& getProgName() const;
37 inline const string& getAction() const;
38 inline const string& getItem() const;
39 string getOptByName( char opt ) const;
40 inline bool exists( char opt ) const;
41
42 private:
43 std::map<char, string> m_argMap;
44 string m_action;
45 string m_item;
46 string m_progName;
47
48 // No copying or assignment is supported
49 Options(const Options&) = delete;
50 Options& operator=(const Options&) = delete;
51};
52
53// Parse command line and keep information about arguments
54Options::Options(int argc, char* argv[]) {
55 if( argc > 0 ) {
56 m_progName = string( argv[0] );
57 }
58 if( argc > 1 ) {
59 m_action = string( argv[1] );
60 }
61 if( argc > 2 ) {
62 m_item = string( argv[2] );
63 }
64 // scan from the beginning (in case there is a lone -h)
65 int pos = 1;
66 while( argc > pos ) {
67 char opt {};
68 string val;
69 string arg = string( argv[pos++] );
70 if( arg.length() == 2 && arg[0] == '-' ) {
71 opt = arg[1];
72 } else continue;
73 if( argc > pos ) {
74 arg = string( argv[pos] );
75 if( arg.length() != 2 or arg[0] != '-' ) {
76 val = std::move(arg);
77 pos++;
78 }
79 }
80 m_argMap[opt] = std::move(val);
81 }
82}
83
84inline string Options::getOptByName( char opt ) const {
85 auto it = m_argMap.find( opt );
86 return (it != m_argMap.end())? it->second : "" ;
87}
88
89inline const string& Options::getProgName() const { return m_progName; }
90inline const string& Options::getAction() const { return m_action; }
91inline const string& Options::getItem() const { return m_item; }
92inline bool Options::exists( char opt ) const { return m_argMap.find(opt) != m_argMap.end(); }
93
94
95
96int main(int argc, char** argv)
97{
99
100 Options options(argc, argv);
101 // possible options:
102 // const char* opts[] = {"n","l","p","g","u","h",0};
103 if( options.exists('h') ) {
104 printUsage();
105 return 0;
106 }
107 if( argc < 3 ){
108 cerr << "ERROR! insufficient number of arguments" << endl;
109 printUsage();
110 return 1;
111 }
112 string catName;
113 // In case none of the options below work, the default FC name is specified in IFileCatalog::addCatalog()
114 if( options.exists('u') ){
115 catName=options.getOptByName('u');
116 }else{
117 catName=getEnvStr("POOL_CATALOG");
118 }
119 string lfn = options.getOptByName('l');
120 string pfn = options.getOptByName('p');
121 string newpfn = options.getOptByName('n');
122 string guid = options.getOptByName('g');
123 string action = options.getAction();
124 string item = options.getItem();
125
126 try{
127 std::unique_ptr<IFileCatalog> mycatalog(new IFileCatalog);
128 mycatalog->setWriteCatalog(catName);
129 mycatalog->start();
130
131 if( action == "delete" ) {
132 if( !lfn.empty() ){
133 mycatalog->deleteFID( mycatalog->lookupLFN( lfn ) );
134 }else if( !pfn.empty() ) {
135 mycatalog->deleteFID( mycatalog->lookupPFN( pfn ) );
136 }else {
137 cerr << "ERROR! Use PFN or LFN to specify the entry for deletion: " << endl;
138 printUsage();
139 return 2;
140 }
141
142 } else if( action == "list" ) {
143 if( item == "lfn" ) {
145 if( !pfn.empty() ) {
146 fids.push_back( mycatalog->lookupPFN( pfn ) );
147 }else{
148 mycatalog->getFIDs( fids );
149 }
150 for( const auto& fid: fids ) {
152 mycatalog->getLFNs( fid, files );
153 for( const auto& file: files ) {
154 cout << file.first << " , " << file.second << endl;
155 }
156 }
157 }
158 else if( item == "pfn" ) {
160 if( !lfn.empty() ) {
161 fids.push_back( mycatalog->lookupLFN( lfn ) );
162 } else if( !guid.empty() ) {
163 fids.emplace_back( std::move(guid) );
164 } else {
165 // go through all FIDs in the catalog
166 mycatalog->getFIDs( fids );
167 }
168 for( const auto& fid: fids ) {
170 mycatalog->getPFNs( fid, files );
171 for( const auto& file: files ) {
172 string pf = file.first;
173 string filetype = (file.second.empty()? string("NULL") : file.second);
174 cout<<pf<<" "<<filetype<<endl;
175 }
176 }
177 }
178 else if( item == "guid" ) {
180 if( !pfn.empty() ){
181 fids.push_back( mycatalog->lookupPFN( pfn ) );
182 } else if( !lfn.empty() ){
183 fids.push_back( mycatalog->lookupLFN( lfn ) );
184 } else {
185 mycatalog->getFIDs( fids );
186 }
187 for( const auto& fid: fids ) {
188 cout << fid << endl;
189 }
190 }
191 else {
192 cerr<< "ERROR! Unsupported catalog item type for listing: "<< item << endl;
193 printUsage();
194 return 2;
195 }
196
197 } else if( action == "register" ) {
198 if( item == "lfn" ) {
199 if( pfn.empty() || lfn.empty() ){
200 cerr<<"ERROR! You must specify PFName using -p and LFName using -l" << endl;
201 return 3;
202 }
203 mycatalog->registerLFN( mycatalog->lookupPFN(pfn), lfn );
204 }
205 else if( item == "pfn" ) {
206 if( pfn.empty() or guid.empty() ) {
207 cerr<<"ERROR! You must specify PFName using -p and GUID using -g options" << endl;
208 return 3;
209 }
210 mycatalog->registerPFN(pfn, "ROOT_All", guid);
211 }
212 else {
213 cerr << "ERROR! Unsupported catalog item type for registration: "<< item << endl;
214 printUsage();
215 return 3;
216 }
217
218 } else if( action == "rename" ) {
219 if( pfn.empty() || newpfn.empty() ){
220 cerr<<"ERROR! You must specify the current PFName using -p and the new PFName using -n option" << endl;
221 return 4;
222 }
223 mycatalog->renamePFN(pfn, newpfn);
224 }
225 else {
226 cerr << "ERROR! Unsupported action: " << action << endl;
227 printUsage();
228 return 10;
229 }
230
231 mycatalog->commit();
232
233 }catch (const std::runtime_error& er){
234 cerr<<er.what()<<endl;
235 return 1;
236 }catch (const std::exception& er){
237 cerr<<er.what()<<endl;
238 return 1;
239 }
240}
void printUsage()
Definition FCcmd.cxx:19
const string & getAction() const
Definition FCcmd.cxx:90
string getOptByName(char opt) const
Definition FCcmd.cxx:84
Options(const Options &)=delete
bool exists(char opt) const
Definition FCcmd.cxx:92
Options & operator=(const Options &)=delete
std::map< char, string > m_argMap
Definition FCcmd.cxx:43
string m_action
Definition FCcmd.cxx:44
string m_progName
Definition FCcmd.cxx:46
Options(int argc, char *argv[])
Definition FCcmd.cxx:54
string m_item
Definition FCcmd.cxx:45
const string & getItem() const
Definition FCcmd.cxx:91
const string & getProgName() const
Definition FCcmd.cxx:89
Gaudi::IFileCatalog::Strings Strings
Gaudi::IFileCatalog::Files Files
static bool initGaudi()
initialize the Gaudi framework for standalone executables
std::vector< std::string > files
file names and file pointers
Definition hcg.cxx:52
int main()
Definition hello.cxx:18
Framework include files.
Definition libname.h:15
std::string getEnvStr(const std::string &key)
Read an environment variable into string (returns empty string if not set).
TFile * file