ATLAS Offline Software
DiskListXRD.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 // (See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 
10 // Please feel free to contact me (krumnack@iastate.edu) for bug
11 // reports, feature suggestions, praise and complaints.
12 
13 
14 //
15 // includes
16 //
17 
19 
20 #include <sstream>
21 #include <vector>
22 #include <RootCoreUtils/Assert.h>
24 #include <RootCoreUtils/ThrowMsg.h>
26 
27 #include <iostream>
28 
29 //
30 // method implementations
31 //
32 
33 namespace SH
34 {
36  testInvariant () const
37  {
38  }
39 
40 
41 
43  DiskListXRD (const std::string& val_server, const std::string& val_directory,
44  bool val_laxParser)
45  : m_server (val_server), m_directory (val_directory),
46  m_laxParser (val_laxParser), m_isRead (false)
47  {
48  RCU_NEW_INVARIANT (this);
49  }
50 
51 
52 
54  getNext ()
55  {
56  RCU_CHANGE_INVARIANT (this);
57  using namespace msgScanDir;
58 
59  if (!m_isRead)
60  {
61  std::string command = "xrdfs " + m_server + " ls -l " + m_directory;
62  ANA_MSG_DEBUG ("trying XRD command: " << command);
64  ANA_MSG_DEBUG ("XRD command output:\n" << command);
65  m_context = "command: " + command + "\n" + m_list;
66  m_isRead = true;
67  }
68 
69  while (!m_list.empty())
70  {
71  std::string::size_type split1 = m_list.find ('\n');
72  if (split1 == std::string::npos)
73  split1 = m_list.size();
74 
75  const std::string line (m_list.substr (0, split1));
76  ANA_MSG_DEBUG ("next XRD list line: " << line);
77  m_list = m_list.substr (split1 + 1);
78 
79  std::string::size_type split2 = line.find ('/');
80  if (split2 != std::string::npos &&
81  (line[0] == '-' || line[0] == 'd'))
82  {
83  m_isDir = line[0] == 'd';
84  m_file = line.substr (split2);
85  ANA_MSG_DEBUG ("next XRD file found: " << m_file);
86  ANA_MSG_DEBUG ("XRD file isDir: " << m_isDir);
87  return true;
88  }
89 
90  if (!line.empty())
91  {
92  std::string message = "failed to parse line: \"" + line + "\"\n" + m_context;
93 
95  if (!m_laxParser)
96  throw std::runtime_error (message);
97  }
98  }
99  return false;
100  }
101 
102 
103 
104  std::string DiskListXRD ::
105  getPath () const
106  {
107  RCU_READ_INVARIANT (this);
108  return "root://" + m_server + "/" + m_file;
109  }
110 
111 
112 
114  doOpenDir () const
115  {
116  RCU_READ_INVARIANT (this);
117 
118  if (m_file.empty() || !m_isDir)
119  return 0;
120 
121  return new DiskListXRD (m_server, m_file, m_laxParser);
122  }
123 
124 
125 
126  std::string DiskListXRD ::
127  getDirname () const
128  {
129  RCU_READ_INVARIANT (this);
130  return m_directory;
131  }
132 }
SH::DiskListXRD::m_file
std::string m_file
the last file we read
Definition: DiskListXRD.h:112
checkFileSG.line
line
Definition: checkFileSG.py:75
SH::DiskListXRD::getNext
virtual bool getNext()
get the next list entry
Definition: DiskListXRD.cxx:54
MessageCheck.h
SH::DiskListXRD::getPath
virtual std::string getPath() const
the path for the current entry.
Definition: DiskListXRD.cxx:105
DiskListXRD.h
SH::DiskListXRD::m_list
std::string m_list
the result of the directory listing
Definition: DiskListXRD.h:108
SH::DiskListXRD::m_isDir
bool m_isDir
whether this is a directory
Definition: DiskListXRD.h:116
ShellExec.h
SH::DiskListXRD::DiskListXRD
DiskListXRD(const std::string &val_server, const std::string &val_dir, bool val_laxParsing=false)
make the listing for the given directory, but replacing the directory with prefix for the path
Definition: DiskListXRD.cxx:43
Assert.h
SH::DiskListXRD::m_directory
std::string m_directory
the directory we are reading
Definition: DiskListXRD.h:96
ReweightUtils.message
message
Definition: ReweightUtils.py:15
SH::DiskListXRD::testInvariant
void testInvariant() const
test the invariant of this object
Definition: DiskListXRD.cxx:36
SH::DiskListXRD::doOpenDir
virtual DiskList * doOpenDir() const
make a new list object for the sub-directory
Definition: DiskListXRD.cxx:114
SH::DiskList
an interface for listing directory contents, locally or on a file server
Definition: DiskList.h:32
ANA_MSG_WARNING
#define ANA_MSG_WARNING(xmsg)
Macro printing warning messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:292
SH::DiskListXRD::m_isRead
bool m_isRead
whether the directory has been read
Definition: DiskListXRD.h:120
SH::DiskListXRD::getDirname
virtual std::string getDirname() const
the base path for the directory listed
Definition: DiskListXRD.cxx:127
SH::DiskListXRD::m_server
std::string m_server
the server from which we are reading
Definition: DiskListXRD.h:92
ThrowMsg.h
SH::DiskListXRD::m_context
std::string m_context
the context (for error reporting)
Definition: DiskListXRD.h:104
RCU::Shell::exec_read
std::string exec_read(const std::string &cmd)
effects: execute the given command and return the output returns: the output of the command guarantee...
Definition: ShellExec.cxx:37
SH::DiskListXRD::m_laxParser
bool m_laxParser
whether we employ lax parsing
Definition: DiskListXRD.h:100
RCU_CHANGE_INVARIANT
#define RCU_CHANGE_INVARIANT(x)
Definition: Assert.h:231
SH
This module provides a lot of global definitions, forward declarations and includes that are used by ...
Definition: PrunDriver.h:15
RCU_READ_INVARIANT
#define RCU_READ_INVARIANT(x)
Definition: Assert.h:229
get_generator_info.command
string command
Definition: get_generator_info.py:38
ANA_MSG_DEBUG
#define ANA_MSG_DEBUG(xmsg)
Macro printing debug messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:288
RCU_NEW_INVARIANT
#define RCU_NEW_INVARIANT(x)
Definition: Assert.h:233