-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathsmartmirror.patch
115 lines (111 loc) · 3.72 KB
/
smartmirror.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
diff --git a/apt-pkg/acquire-worker.cc b/apt-pkg/acquire-worker.cc
index f901847..309b752 100644
--- a/apt-pkg/acquire-worker.cc
+++ b/apt-pkg/acquire-worker.cc
@@ -38,6 +38,7 @@
#include <apti18n.h>
/*}}}*/
+#include "smartmirrors.h"
using namespace std;
@@ -604,6 +605,7 @@ bool pkgAcquire::Worker::QueueItem(pkgAcquire::Queue::QItem *Item)
if (OutFd == -1)
return false;
+ Item->URI = SmartMirrors::GuestURI(Item->URI);
string Message = "600 URI Acquire\n";
Message.reserve(300);
Message += "URI: " + Item->URI;
diff --git a/apt-pkg/smartmirrors.cc b/apt-pkg/smartmirrors.cc
new file mode 100644
index 0000000..90e3a88
--- /dev/null
+++ b/apt-pkg/smartmirrors.cc
@@ -0,0 +1,75 @@
+// -*- mode: cpp; mode: fold -*-
+// Description /*{{{*/
+/* ######################################################################
+
+ Smart Mirrors - Get Archive File by Smart Mirrors Server.
+
+ ##################################################################### */
+/*}}}*/
+// Include Files /*{{{*/
+
+
+#include <apt-pkg/acquire-item.h>
+#include <apt-pkg/configuration.h>
+#include <apt-pkg/aptconfiguration.h>
+
+#include <tr1/memory>
+#include "smartmirrors.h"
+
+using namespace std;
+
+namespace SmartMirrors {
+ static string
+ normalizeURI(const string& uri)
+ {
+ if (uri.length() >=1 && uri.at(uri.length()-1) == '/') {
+ return uri.substr(0, uri.length()-1);
+ }
+ return uri;
+ }
+
+ const std::string
+ GuestURI(const std::string& uri)
+ {
+ if(uri.substr(0, strlen("http")) != "http") {
+ return uri;
+ }
+ static bool debug = _config->FindB("Acquire::SmartMirrors::Debug");
+ static bool enabled = _config->FindB("Acquire::SmartMirrors::Enable");
+ if (!enabled) {
+ if (debug) {
+ std::clog << "SmartMirrors is disabled." << std::endl;
+ }
+ return uri;
+ }
+ static string official = normalizeURI(_config->Find("Acquire::SmartMirrors::MainSource"));
+ static string mirror = normalizeURI(_config->Find("Acquire::SmartMirrors::MirrorSource"));
+
+ static string detector = _config->Find("Acquire::SmartMirrors::GuestURI");
+ static bool exists = FileExists(detector);
+ if (!exists) {
+ return uri;
+ }
+
+ string cmd = detector + " " + uri + " " + official + " " + mirror;
+ std::tr1::shared_ptr<FILE> out(popen(cmd.c_str(), "r"), pclose);
+ if (!out) {
+ return uri;
+ }
+
+ char buffer[1024] = {0};
+ std::string result = "";
+ while (!feof(out.get())) {
+ if (fgets(buffer, 1024, out.get()) != NULL)
+ result += buffer;
+ }
+ if(result.substr(0, strlen("http")) != "http") {
+ return uri;
+ }
+
+ if (debug && result != uri) {
+ printf("Using '%s' instead of '%s'\n", result.c_str(), uri.c_str());
+ }
+ return result;
+ }
+}
diff --git a/apt-pkg/smartmirrors.h b/apt-pkg/smartmirrors.h
new file mode 100644
index 0000000..e2fe5fd
--- /dev/null
+++ b/apt-pkg/smartmirrors.h
@@ -0,0 +1,8 @@
+#include <config.h>
+
+#include <apt-pkg/cacheiterators.h>
+#include <apt-pkg/indexfile.h>
+
+namespace SmartMirrors {
+ const std::string GuestURI(const std::string& uri);
+};