Elements  5.10
A C++ base framework for the Euclid Software.
DataSynchronizer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2020 Euclid Science Ground Segment
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License as published by the Free
6  * Software Foundation; either version 3.0 of the License, or (at your option)
7  * any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include <string>
20 #include "ElementsKernel/Unused.h"
21 
24 
25 namespace ElementsServices {
26 namespace DataSync {
27 
29  const ConnectionConfiguration& connection,
30  const DependencyConfiguration& dependency) :
31  m_connection(connection), m_fileMap(dependency.fileMap()) {
32 }
33 
35  for (const auto& item : m_fileMap) {
36  const auto& localFile = item.first;
37  const auto& distantFile = item.second;
38  if (fileShouldBeWritten(localFile)) {
39  downloadOneFile(distantFile, localFile);
40  }
41  }
42 }
43 
45  if (not fileAlreadyExists(localFile)) {
46  return true;
47  }
49 }
50 
52  return boost::filesystem::is_regular_file(localFile);
53 }
54 
56  path distantFile,
57  path localFile) const {
58  std::string command = createDownloadCommand(distantFile, localFile);
59  createLocalDirOf(localFile);
60  const auto outErr = runCommandAndCaptureOutErr(command);
61  if (not hasBeenDownloaded(distantFile, localFile)) {
62  throw DownloadFailed(distantFile, localFile);
63  }
64 }
65 
67  ELEMENTS_UNUSED path distantFile,
68  path localFile) const {
69  if (not boost::filesystem::is_regular_file(localFile)) {
70  return false;
71  }
72  return boost::filesystem::file_size(localFile) > 0;
73 }
74 
75 } // namespace DataSync
76 } // namespace ElementsServices
ElementsServices::DataSync::ConnectionConfiguration::overwritingAllowed
bool overwritingAllowed() const
Check whether existing local files can be overwritten.
Definition: ConnectionConfiguration.cpp:35
std::string
STL class.
ElementsServices::DataSync::DataSynchronizer::fileShouldBeWritten
bool fileShouldBeWritten(path localFile) const
Definition: DataSynchronizer.cpp:44
ElementsServices::DataSync::DownloadFailed
An exception raised when downloading fails.
Definition: DataSynchronizer.h:45
ElementsServices::DataSync::DataSynchronizer::m_fileMap
std::map< path, path > m_fileMap
Definition: DataSynchronizer.h:98
ElementsServices::DataSync::path
boost::filesystem::path path
Definition: DataSyncUtils.h:38
ElementsServices::DataSync::DataSynchronizer::downloadOneFile
void downloadOneFile(path distantFile, path localFile) const
Definition: DataSynchronizer.cpp:55
ElementsServices::DataSync::DataSynchronizer::downloadAllFiles
void downloadAllFiles() const
Definition: DataSynchronizer.cpp:34
ElementsServices::DataSync::DataSynchronizer::hasBeenDownloaded
bool hasBeenDownloaded(path distantFile, path localFile) const
Definition: DataSynchronizer.cpp:66
ElementsServices::DataSync::DataSynchronizer::DataSynchronizer
DataSynchronizer(const ConnectionConfiguration &connection, const DependencyConfiguration &dependency)
Definition: DataSynchronizer.cpp:28
ElementsServices::DataSync::DataSynchronizer::createDownloadCommand
virtual std::string createDownloadCommand(path distantFile, path localFile) const =0
ElementsServices::DataSync::DependencyConfiguration
The dependency configurations holds, for each test file to be retrieved:
Definition: DependencyConfiguration.h:44
ElementsServices
Definition: ConnectionConfiguration.h:33
ElementsServices::DataSync::DataSynchronizer::fileAlreadyExists
bool fileAlreadyExists(path localFile) const
Definition: DataSynchronizer.cpp:51
ElementsServices::DataSync::createLocalDirOf
ELEMENTS_API void createLocalDirOf(path localFile)
Definition: DataSyncUtils.cpp:64
ElementsServices::DataSync::runCommandAndCaptureOutErr
ELEMENTS_API std::pair< std::string, std::string > runCommandAndCaptureOutErr(std::string command)
Definition: DataSyncUtils.cpp:45
DataSyncUtils.h
DataSynchronizer.h
ElementsServices::DataSync::DataSynchronizer::m_connection
ConnectionConfiguration m_connection
Definition: DataSynchronizer.h:97
ElementsServices::DataSync::ConnectionConfiguration
The connection configuration mainly holds:
Definition: ConnectionConfiguration.h:75
Unused.h
Macro to silence unused variables warnings from the compiler.
ELEMENTS_UNUSED
#define ELEMENTS_UNUSED
Definition: Unused.h:39