Elements 6.3.1
A C++ base framework for the Euclid Software.
Loading...
Searching...
No Matches
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 "ElementsServices/DataSync/DataSynchronizer.h" // for DataSynchronizer, DownloadFailed
20
21#include <map> // for map, operator!=, _Rb_tree_const_iterator
22#include <string> // for string
23#include <utility> // for pair
24
25#include <boost/filesystem/operations.hpp> // for is_regular_file, file_size
26
27#include "ElementsKernel/Unused.h" // for ELEMENTS_UNUSED
28
29#include "ElementsServices/DataSync/ConnectionConfiguration.h" // for ConnectionConfiguration
30#include "ElementsServices/DataSync/DataSyncUtils.h" // for path, createLocalDirOf, runCommandAndCaptureOutErr
31#include "ElementsServices/DataSync/DependencyConfiguration.h" // for DependencyConfiguration
32
33namespace Elements {
34inline namespace Services {
35namespace DataSync {
36
38 : m_connection(connection), m_fileMap(dependency.fileMap()) {}
39
41 for (const auto& item : m_fileMap) {
42 const auto& localFile = item.first;
43 const auto& distantFile = item.second;
44 if (fileShouldBeWritten(localFile)) {
45 downloadOneFile(distantFile, localFile);
46 }
47 }
48}
49
51 if (not fileAlreadyExists(localFile)) {
52 return true;
53 }
55}
56
58 return boost::filesystem::is_regular_file(localFile);
59}
60
61void DataSynchronizer::downloadOneFile(path distantFile, path localFile) const {
62 std::string command = createDownloadCommand(distantFile, localFile);
63 createLocalDirOf(localFile);
64 const auto outErr = runCommandAndCaptureOutErr(command);
65 if (not hasBeenDownloaded(distantFile, localFile)) {
66 throw DownloadFailed(distantFile, localFile);
67 }
68}
69
71 if (not boost::filesystem::is_regular_file(localFile)) {
72 return false;
73 }
74 return boost::filesystem::file_size(localFile) > 0;
75}
76
77} // namespace DataSync
78} // namespace Services
79} // namespace Elements
Macro to silence unused variables warnings from the compiler.
bool overwritingAllowed() const
Check whether existing local files can be overwritten.
void downloadOneFile(path distantFile, path localFile) const
virtual std::string createDownloadCommand(path distantFile, path localFile) const =0
DataSynchronizer(const ConnectionConfiguration &connection, const DependencyConfiguration &dependency)
bool hasBeenDownloaded(path distantFile, path localFile) const
The dependency configurations holds, for each test file to be retrieved:
An exception raised when downloading fails.
#define ELEMENTS_UNUSED
Definition Unused.h:39
ELEMENTS_API std::pair< std::string, std::string > runCommandAndCaptureOutErr(std::string command)
Path::Item path
importing the path item from ElementsKernel
ELEMENTS_API void createLocalDirOf(path localFile)