Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
dCache
cta
Commits
a2c2ef0b
Commit
a2c2ef0b
authored
Aug 26, 2016
by
Victor Kotlyar
Browse files
Removed cmdline dependencies on cta::common
parent
602cfeff
Changes
5
Hide whitespace changes
Inline
Side-by-side
cmdline/CMakeLists.txt
View file @
a2c2ef0b
...
...
@@ -21,8 +21,8 @@ find_package (cryptopp REQUIRED)
find_package
(
xrootd REQUIRED
)
include_directories
(
${
XROOTD_INCLUDE_DIR
}
${
XROOTD_PRIVATE_INCLUDE_DIR
}
${
CMAKE_SOURCE_DIR
}
)
add_executable
(
cta CTACmdMain.cpp
)
target_link_libraries
(
cta
${
XROOTD_XRDCL_LIB
}
c
tacommon cryptopp ctautils
)
add_executable
(
cta CTACmdMain.cpp
Configuration.cpp
)
target_link_libraries
(
cta
${
XROOTD_XRDCL_LIB
}
c
ryptopp
)
install
(
TARGETS cta DESTINATION usr/bin
)
INSTALL
(
FILES cta-cli.conf DESTINATION
${
CMAKE_INSTALL_SYSCONFDIR
}
/cta
)
...
...
cmdline/CTACmdMain.cpp
View file @
a2c2ef0b
...
...
@@ -16,6 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include
"cmdline/Configuration.hpp"
#include
"common/Configuration.hpp"
#include
"common/dataStructures/FrontendReturnCode.hpp"
...
...
@@ -82,8 +83,9 @@ std::string encode(const std::string msg) {
* @return the command string
*/
std
::
string
formatCommandPath
(
const
int
argc
,
const
char
**
argv
)
{
cta
::
common
::
Configuration
ctaConf
(
"/etc/cta/cta-cli.conf"
);
std
::
string
cmdPath
=
"root://"
+
ctaConf
.
getConfEntString
(
"Frontend"
,
"HostAndPort"
,
nullptr
)
+
"//"
;
cta
::
cmdline
::
Configuration
cliConf
(
"/etc/cta/cta-cli.conf"
);
std
::
string
cmdPath
=
"root://"
+
cliConf
.
getFrontendHostAndPort
()
+
"//"
;
for
(
int
i
=
0
;
i
<
argc
;
i
++
)
{
if
(
i
)
cmdPath
+=
"&"
;
cmdPath
+=
encode
(
std
::
string
(
argv
[
i
]));
...
...
cmdline/Configuration.cpp
0 → 100644
View file @
a2c2ef0b
/*
* The CERN Tape Archive (CTA) project
* Copyright (C) 2015 CERN
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include
"Configuration.hpp"
#include
<fstream>
#include
<stdexcept>
#include
<exception>
namespace
cta
{
namespace
cmdline
{
//------------------------------------------------------------------------------
// s_fileFormat
//------------------------------------------------------------------------------
const
char
*
Configuration
::
s_fileFormat
=
"<FQDN>:<port>"
;
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
Configuration
::
Configuration
(
const
std
::
string
&
filename
)
:
filename
(
filename
)
{
frontendHostAndPort
=
parseFile
(
filename
);
}
//------------------------------------------------------------------------------
// parseFile
//------------------------------------------------------------------------------
std
::
string
Configuration
::
parseFile
(
const
std
::
string
&
filename
)
{
try
{
std
::
ifstream
file
(
filename
);
if
(
!
file
)
{
throw
std
::
runtime_error
(
"Failed to open "
);
}
return
parseStream
(
file
);
}
catch
(
std
::
exception
&
ex
)
{
throw
std
::
runtime_error
(
std
::
string
(
"Failed to parse configuration file "
+
filename
+
": "
+
ex
.
what
()));
}
}
//------------------------------------------------------------------------------
// parseFile
//------------------------------------------------------------------------------
std
::
string
Configuration
::
getFrontendHostAndPort
()
throw
()
{
return
frontendHostAndPort
;
}
//------------------------------------------------------------------------------
// parseStream
//------------------------------------------------------------------------------
std
::
string
Configuration
::
parseStream
(
std
::
istream
&
inputStream
)
{
const
std
::
list
<
std
::
string
>
lines
=
readNonEmptyLines
(
inputStream
);
if
(
1
!=
lines
.
size
())
{
throw
std
::
runtime_error
(
"There should only be one and only one line "
"containing a frontend hostname and port"
);
}
const
std
::
string
frontendHostAndPort
=
lines
.
front
();
std
::
vector
<
std
::
string
>
configurationLine
;
splitString
(
frontendHostAndPort
,
' '
,
configurationLine
);
if
(
1
!=
configurationLine
.
size
())
{
throw
std
::
runtime_error
(
std
::
string
(
"There should not be any space in the "
"host configuration: Correct"
" format is "
)
+
s_fileFormat
);
}
std
::
vector
<
std
::
string
>
components
;
splitString
(
frontendHostAndPort
,
':'
,
components
);
if
(
2
!=
components
.
size
())
{
throw
std
::
runtime_error
(
std
::
string
(
"Port not found in the configuration:"
" Correct format is "
)
+
s_fileFormat
);
}
const
std
::
string
&
hostname
=
components
[
0
];
const
std
::
string
&
port
=
components
[
1
];
if
(
!
onlyContainsNumerals
(
port
))
{
throw
std
::
runtime_error
(
std
::
string
(
"Port must only contain numerals: "
"value="
)
+
port
);
}
if
(
4
>
hostname
.
length
())
{
throw
std
::
runtime_error
(
std
::
string
(
"Hostname too short: "
"value="
)
+
hostname
);
}
return
frontendHostAndPort
;
}
//-----------------------------------------------------------------------------
// splitString
//-----------------------------------------------------------------------------
void
Configuration
::
splitString
(
const
std
::
string
&
str
,
const
char
separator
,
std
::
vector
<
std
::
string
>
&
result
)
{
if
(
str
.
empty
())
{
return
;
}
std
::
string
::
size_type
beginIndex
=
0
;
std
::
string
::
size_type
endIndex
=
str
.
find
(
separator
);
while
(
endIndex
!=
std
::
string
::
npos
)
{
result
.
push_back
(
str
.
substr
(
beginIndex
,
endIndex
-
beginIndex
));
beginIndex
=
++
endIndex
;
endIndex
=
str
.
find
(
separator
,
endIndex
);
}
// If no separator could not be found then simply append the whole input
// string to the result
if
(
endIndex
==
std
::
string
::
npos
)
{
result
.
push_back
(
str
.
substr
(
beginIndex
,
str
.
length
()));
}
}
//------------------------------------------------------------------------------
// onlyContainsNumerals
//------------------------------------------------------------------------------
bool
Configuration
::
onlyContainsNumerals
(
const
std
::
string
&
str
)
throw
()
{
for
(
std
::
string
::
const_iterator
itor
=
str
.
begin
();
itor
!=
str
.
end
();
itor
++
)
{
if
(
*
itor
<
'0'
||
*
itor
>
'9'
)
{
return
false
;
}
}
return
true
;
}
//-----------------------------------------------------------------------------
// trimString
//-----------------------------------------------------------------------------
std
::
string
Configuration
::
trimString
(
const
std
::
string
&
s
)
throw
()
{
const
std
::
string
&
spaces
=
"
\t\n\v\f\r
"
;
// Find first non white character
size_t
beginpos
=
s
.
find_first_not_of
(
spaces
);
std
::
string
::
const_iterator
it1
;
if
(
std
::
string
::
npos
!=
beginpos
)
{
it1
=
beginpos
+
s
.
begin
();
}
else
{
it1
=
s
.
begin
();
}
// Find last non white chararacter
std
::
string
::
const_iterator
it2
;
size_t
endpos
=
s
.
find_last_not_of
(
spaces
);
if
(
std
::
string
::
npos
!=
endpos
)
{
it2
=
endpos
+
1
+
s
.
begin
();
}
else
{
it2
=
s
.
end
();
}
return
std
::
string
(
it1
,
it2
);
}
//------------------------------------------------------------------------------
// readNonEmptyLines
//------------------------------------------------------------------------------
std
::
list
<
std
::
string
>
Configuration
::
readNonEmptyLines
(
std
::
istream
&
inputStream
)
{
std
::
list
<
std
::
string
>
lines
;
std
::
string
line
;
while
(
std
::
getline
(
inputStream
,
line
))
{
// Remove the newline character if there is one
{
const
std
::
string
::
size_type
newlinePos
=
line
.
find
(
"
\n
"
);
if
(
newlinePos
!=
std
::
string
::
npos
)
{
line
=
line
.
substr
(
0
,
newlinePos
);
}
}
// If there is a comment, then remove it from the line
{
const
std
::
string
::
size_type
startOfComment
=
line
.
find
(
"#"
);
if
(
startOfComment
!=
std
::
string
::
npos
)
{
line
=
line
.
substr
(
0
,
startOfComment
);
}
}
// Left and right trim the line of whitespace
line
=
Configuration
::
trimString
(
std
::
string
(
line
));
// If the line is not empty
if
(
!
line
.
empty
())
{
lines
.
push_back
(
line
);
}
}
return
lines
;
}
}
// namesapce cmdline
}
// namespace cta
cmdline/Configuration.hpp
0 → 100644
View file @
a2c2ef0b
/*
* The CERN Tape Archive (CTA) project
* Copyright (C) 2015 CERN
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include
<istream>
#include
<list>
#include
<string>
#include
<vector>
namespace
cta
{
namespace
cmdline
{
/**
* A set of configuration details.
*/
class
Configuration
{
public:
/**
* Constructor.
*
* @param filename The configuration file name.
*/
Configuration
(
const
std
::
string
&
filename
);
private:
/**
* The frontend hostname and port to connect.
*/
std
::
string
frontendHostAndPort
;
/**
* The configuration file name.
*/
const
std
::
string
filename
;
/**
* Reads and parses the configuration information from the specified file.
*
* The input stream must contain one and only one configuration string.
*
* The format of the configuration string is:
*
* <host>.cern.ch:<port>
*
* The file can contain multiple empty lines.
*
* The file can contain multiple comment lines where a comment
* line starts with optional whitespace and a hash character '#'.
*
* @param filename The name of the file containing the configuration
* information.
* @return The configuration information.
*/
std
::
string
parseFile
(
const
std
::
string
&
filename
);
/**
* Reads and parses the configuration information from the specified file.
*
* The input stream must contain one and only one configuration string.
*
* The format of the configuration string is:
*
* <host>.cern.ch:<port>
*
* The file can contain multiple empty lines.
*
* The file can contain multiple comment lines where a comment
* line starts with optional whitespace and a hash character '#'.
*
* @param filename The name of the file containing the configuration
* information.
* @return The configuration information.
*/
std
::
string
parseStream
(
std
::
istream
&
inputStream
);
/**
* Reads the entire contents of the specified stream and returns a list of the
* non-empty lines.
*
* A line is considered not empty if it contains characters that are not white
* space and are not part of a comment.
*
* @param is The input stream.
* @return A list of the non-empty lines.
*/
std
::
list
<
std
::
string
>
readNonEmptyLines
(
std
::
istream
&
inputStream
);
/**
* Returns the result of trimming both left and right white-space from the
* specified string.
*
* @param s The string to be trimmed.
* @return The result of trimming the string.
*/
std
::
string
trimString
(
const
std
::
string
&
s
)
throw
();
/**
* Splits the specified string into a vector of strings using the specified
* separator.
*
* Please note that the string to be split is NOT modified.
*
* @param str The string to be split.
* @param separator The separator to be used to split the specified string.
* @param result The vector when the result of spliting the string will be
* stored.
*/
void
splitString
(
const
std
::
string
&
str
,
const
char
separator
,
std
::
vector
<
std
::
string
>
&
result
);
/**
* Returns true if the specified string only contains numerals else false.
*
* @return True if the specified string only contains numerals else false.
*/
static
bool
onlyContainsNumerals
(
const
std
::
string
&
str
)
throw
();
/**
* Human readable description of the format of the database
* login/configuration file.
*/
static
const
char
*
s_fileFormat
;
public:
/**
* Returns the value of the configuration parameters.
*
* @return The frontend host and port from the configuration.
*/
std
::
string
getFrontendHostAndPort
()
throw
();
};
// class Configuration
}
// namespace cmdline
}
// namespace cta
cmdline/cta-cli.conf
View file @
a2c2ef0b
Frontend
HostAndPort
<
host
>.
cern
.
ch
:
10955
\ No newline at end of file
# The CTA frontend address in the form <FQDN>:<TCPPort>
<
host
>.
cern
.
ch
:
10955
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment