ATLAS Offline Software
Loading...
Searching...
No Matches
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
6
7//
8// includes
9//
10
12
13#include <sstream>
17
18//
19// method implementations
20//
21
22namespace SH
23{
24 void DiskListXRD ::
25 testInvariant () const
26 {
27 }
28
29
30
31 DiskListXRD ::
32 DiskListXRD (const std::string& val_server, const std::string& val_directory,
33 bool val_laxParser)
34 : m_server (val_server), m_directory (val_directory),
35 m_laxParser (val_laxParser), m_isRead (false)
36 {
37 RCU_NEW_INVARIANT (this);
38 }
39
40
41
42 bool DiskListXRD ::
43 getNext ()
44 {
46 using namespace msgScanDir;
47
48 if (!m_isRead)
49 {
50 std::string command = "xrdfs " + RCU::Shell::quote (m_server) + " ls -l " + RCU::Shell::quote (m_directory);
51 ANA_MSG_DEBUG ("trying XRD command: " << command);
52 m_list = RCU::Shell::exec_read (command);
53 ANA_MSG_DEBUG ("XRD command output:\n" << m_list);
54 m_context = "command: " + command + "\n" + m_list;
55 m_isRead = true;
56 m_pos = 0;
57 }
58
59 // rationale: we advance a running index through m_list rather than
60 // repeatedly copying its tail, so that listing N entries costs
61 // O(N) rather than O(N^2). a final line without a trailing
62 // newline is still processed.
63 while (m_pos < m_list.size())
64 {
65 std::string::size_type split1 = m_list.find ('\n', m_pos);
66 std::string line;
67 if (split1 == std::string::npos)
68 {
69 line = m_list.substr (m_pos);
70 m_pos = m_list.size();
71 } else
72 {
73 line = m_list.substr (m_pos, split1 - m_pos);
74 m_pos = split1 + 1;
75 }
76 ANA_MSG_DEBUG ("next XRD list line: " << line);
77
78 std::string::size_type split2 = line.find ('/');
79 if (split2 != std::string::npos &&
80 (line[0] == '-' || line[0] == 'd'))
81 {
82 m_isDir = line[0] == 'd';
83 m_file = line.substr (split2);
84 ANA_MSG_DEBUG ("next XRD file found: " << m_file);
85 ANA_MSG_DEBUG ("XRD file isDir: " << m_isDir);
86 return true;
87 }
88
89 if (!line.empty())
90 {
91 std::string message = "failed to parse line: \"" + line + "\"\n" + m_context;
92
93 ANA_MSG_WARNING (message);
94 if (!m_laxParser)
95 throw std::runtime_error (message);
96 }
97 }
98 return false;
99 }
100
101
102
103 std::string DiskListXRD ::
104 getPath () const
105 {
106 RCU_READ_INVARIANT (this);
107 return "root://" + m_server + "/" + m_file;
108 }
109
110
111
112 DiskList *DiskListXRD ::
113 doOpenDir () const
114 {
115 RCU_READ_INVARIANT (this);
116
117 if (m_file.empty() || !m_isDir)
118 return nullptr;
119
120 return new DiskListXRD (m_server, m_file, m_laxParser);
121 }
122
123
124
125 std::string DiskListXRD ::
126 getDirname () const
127 {
128 RCU_READ_INVARIANT (this);
129 return m_directory;
130 }
131}
#define RCU_CHANGE_INVARIANT(x)
Definition Assert.h:219
#define RCU_NEW_INVARIANT(x)
Definition Assert.h:221
#define RCU_READ_INVARIANT(x)
Definition Assert.h:217
#define ANA_MSG_DEBUG(xmsg,...)
Macro printing debug messages.
#define ANA_MSG_WARNING(xmsg,...)
Macro printing warning messages.
std::string m_server
the server from which we are reading
Definition DiskListXRD.h:84
std::string m_context
the context (for error reporting)
Definition DiskListXRD.h:96
bool m_laxParser
whether we employ lax parsing
Definition DiskListXRD.h:92
std::string m_directory
the directory we are reading
Definition DiskListXRD.h:88
std::string m_list
the result of the directory listing
std::string::size_type m_pos
the current parse position within m_list
bool m_isRead
whether the directory has been read
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
bool m_isDir
whether this is a directory
std::string m_file
the last file we read
DiskList()
standard constructor
Definition DiskList.cxx:38
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:35
std::string quote(const std::string &name)
effects: quote the given name to protect it from the shell returns: the quoted name guarantee: strong...
Definition ShellExec.cxx:65
This module provides a lot of global definitions, forward declarations and includes that are used by ...
Definition DiskList.cxx:21