Elements  6.2
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 "ElementsKernel/Unused.h"
20 #include <string>
21 
24 
25 namespace Elements {
26 inline namespace Services {
27 namespace DataSync {
28 
30  : m_connection(connection), m_fileMap(dependency.fileMap()) {}
31 
33  for (const auto& item : m_fileMap) {
34  const auto& localFile = item.first;
35  const auto& distantFile = item.second;
36  if (fileShouldBeWritten(localFile)) {
37  downloadOneFile(distantFile, localFile);
38  }
39  }
40 }
41 
43  if (not fileAlreadyExists(localFile)) {
44  return true;
45  }
47 }
48 
50  return boost::filesystem::is_regular_file(localFile);
51 }
52 
53 void DataSynchronizer::downloadOneFile(path distantFile, path localFile) const {
54  std::string command = createDownloadCommand(distantFile, localFile);
55  createLocalDirOf(localFile);
56  const auto outErr = runCommandAndCaptureOutErr(command);
57  if (not hasBeenDownloaded(distantFile, localFile)) {
58  throw DownloadFailed(distantFile, localFile);
59  }
60 }
61 
62 bool DataSynchronizer::hasBeenDownloaded(ELEMENTS_UNUSED path distantFile, path localFile) const {
63  if (not boost::filesystem::is_regular_file(localFile)) {
64  return false;
65  }
66  return boost::filesystem::file_size(localFile) > 0;
67 }
68 
69 } // namespace DataSync
70 } // namespace Services
71 } // namespace Elements
std::string
STL class.
Elements::Services::DataSync::DataSynchronizer::DataSynchronizer
DataSynchronizer(const ConnectionConfiguration &connection, const DependencyConfiguration &dependency)
Definition: DataSynchronizer.cpp:29
Elements::Services::DataSync::DownloadFailed
An exception raised when downloading fails.
Definition: DataSynchronizer.h:46
Elements::Services::DataSync::DataSynchronizer::downloadOneFile
void downloadOneFile(path distantFile, path localFile) const
Definition: DataSynchronizer.cpp:53
Elements::Services::DataSync::DataSynchronizer::fileAlreadyExists
bool fileAlreadyExists(path localFile) const
Definition: DataSynchronizer.cpp:49
Elements::Services::DataSync::runCommandAndCaptureOutErr
ELEMENTS_API std::pair< std::string, std::string > runCommandAndCaptureOutErr(std::string command)
Definition: DataSyncUtils.cpp:49
Elements::Services::DataSync::DependencyConfiguration
The dependency configurations holds, for each test file to be retrieved:
Definition: DependencyConfiguration.h:45
Elements::Services::DataSync::createLocalDirOf
ELEMENTS_API void createLocalDirOf(path localFile)
Definition: DataSyncUtils.cpp:68
Elements::Services::DataSync::ConnectionConfiguration::overwritingAllowed
bool overwritingAllowed() const
Check whether existing local files can be overwritten.
Definition: ConnectionConfiguration.cpp:36
Elements::Services::DataSync::DataSynchronizer::hasBeenDownloaded
bool hasBeenDownloaded(path distantFile, path localFile) const
Definition: DataSynchronizer.cpp:62
Elements::Services::DataSync::DataSynchronizer::downloadAllFiles
void downloadAllFiles() const
Definition: DataSynchronizer.cpp:32
DataSyncUtils.h
Elements::Services::DataSync::DataSynchronizer::fileShouldBeWritten
bool fileShouldBeWritten(path localFile) const
Definition: DataSynchronizer.cpp:42
Elements::Services::DataSync::DataSynchronizer::m_fileMap
std::map< path, path > m_fileMap
Definition: DataSynchronizer.h:83
DataSynchronizer.h
Unused.h
Macro to silence unused variables warnings from the compiler.
ELEMENTS_UNUSED
#define ELEMENTS_UNUSED
Definition: Unused.h:39
Elements::Services::DataSync::DataSynchronizer::createDownloadCommand
virtual std::string createDownloadCommand(path distantFile, path localFile) const =0
Elements::Services::DataSync::ConnectionConfiguration
The connection configuration mainly holds:
Definition: ConnectionConfiguration.h:75
Elements
Definition: callBackExample.h:35
Elements::Services::DataSync::path
Path::Item path
importing the path item from ElementsKernel
Definition: DataSyncUtils.h:41
Elements::Services::DataSync::DataSynchronizer::m_connection
ConnectionConfiguration m_connection
Definition: DataSynchronizer.h:82