GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/notify/cmd_pub.cc
Date: 2024-04-21 02:33:16
Exec Total Coverage
Lines: 0 51 0.0%
Branches: 0 44 0.0%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 */
4
5 #include "cmd_pub.h"
6
7 #include <fcntl.h>
8
9 #include <string>
10 #include <vector>
11
12 #include "manifest.h"
13 #include "network/download.h"
14 #include "notify/messages.h"
15 #include "notify/publisher_http.h"
16 #include "util/pointer.h"
17 #include "util/posix.h"
18 #include "util/string.h"
19
20 namespace {
21
22 const LogFacilities& kLogInfo = DefaultLogging::info;
23 const LogFacilities& kLogError = DefaultLogging::error;
24
25 const int kMaxPoolHandles = 1;
26 const unsigned kDownloadTimeout = 60; // 1 minute
27 const unsigned kDownloadRetries = 1; // 2 attempts in total
28
29 } // namespace
30
31 namespace notify {
32
33 int DoPublish(const std::string& server_url, const std::string& repository_url,
34 bool verbose) {
35 const std::string repo_url = MakeCanonicalPath(repository_url);
36
37 if (verbose) {
38 LogCvmfs(kLogCvmfs, kLogInfo, "Parameters: ");
39 LogCvmfs(kLogCvmfs, kLogInfo, " CVMFS repository URL: %s",
40 repo_url.c_str());
41 LogCvmfs(kLogCvmfs, kLogInfo, " Notification server URL: %s",
42 server_url.c_str());
43 }
44
45 // Download repository manifest
46 std::string manifest_contents;
47 const std::string manifest_url = repo_url + "/.cvmfspublished";
48 if (IsHttpUrl(repo_url)) {
49 perf::Statistics stats;
50 UniquePtr<download::DownloadManager> download_manager(
51 new download::DownloadManager(kMaxPoolHandles,
52 perf::StatisticsTemplate("download", &stats)));
53 assert(download_manager.IsValid());
54
55 download_manager->SetTimeout(kDownloadTimeout, kDownloadTimeout);
56 download_manager->SetRetryParameters(kDownloadRetries, 500, 2000);
57
58 cvmfs::MemSink manifest_memsink;
59 download::JobInfo download_manifest(&manifest_url, false, false, NULL,
60 &manifest_memsink);
61 download::Failures retval = download_manager->Fetch(&download_manifest);
62 if (retval != download::kFailOk) {
63 LogCvmfs(kLogCvmfs, kLogError, "Failed to download manifest (%d - %s)",
64 retval, download::Code2Ascii(retval));
65 return 6;
66 }
67 manifest_contents = std::string(
68 reinterpret_cast<char*>(manifest_memsink.data()),
69 manifest_memsink.pos());
70 } else {
71 int fd = open(manifest_url.c_str(), O_RDONLY);
72 if (fd == -1) {
73 LogCvmfs(kLogCvmfs, kLogError, "Could not open manifest file");
74 return 7;
75 }
76 if (!SafeReadToString(fd, &manifest_contents)) {
77 LogCvmfs(kLogCvmfs, kLogError, "Could not read manifest file");
78 close(fd);
79 return 8;
80 }
81 close(fd);
82 }
83
84 UniquePtr<manifest::Manifest> manifest(manifest::Manifest::LoadMem(
85 reinterpret_cast<const unsigned char*>(manifest_contents.data()),
86 manifest_contents.size()));
87
88 if (verbose) {
89 LogCvmfs(kLogCvmfs, kLogInfo, "Current repository manifest:\n%s",
90 manifest->ExportString().c_str());
91 }
92
93 const std::string repository_name = manifest->repository_name();
94
95 // Publish message
96 UniquePtr<notify::Publisher> publisher(new notify::PublisherHTTP(server_url));
97
98 std::string msg_text;
99 notify::msg::Activity msg;
100 msg.version_ = 1;
101 msg.timestamp_ = StringifyTime(std::time(NULL), true);
102 msg.repository_ = repository_name;
103 msg.manifest_ = manifest_contents;
104 msg.ToJSONString(&msg_text);
105
106 if (!publisher->Publish(msg_text, repository_name)) {
107 LogCvmfs(kLogCvmfs, kLogError, "Could not publish notification");
108 return 9;
109 }
110
111 assert(publisher->Finalize());
112
113 return 0;
114 }
115
116 } // namespace notify
117