]> git.ipfire.org Git - pakfire.git/commitdiff
libpakfire: Automatically determine the repository priority
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 22 Jan 2018 16:21:29 +0000 (17:21 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 22 Jan 2018 16:21:29 +0000 (17:21 +0100)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/include/pakfire/util.h
src/libpakfire/repo.c
src/libpakfire/util.c
src/pakfire/repository/remote.py

index b9318d59a3a60df03ae2c77e8f67ad459e24d550..df32e5bd18cffad8235194659b664e557510aba5 100644 (file)
@@ -36,6 +36,7 @@ void* pakfire_realloc(void* ptr, size_t size);
 void* pakfire_free(void* mem);
 
 char* pakfire_strdup(const char* s);
+int pakfire_string_startswith(const char* s, const char* prefix);
 
 char* pakfire_format_size(double size);
 char* pakfire_format_date(time_t t);
index be797754fe7ff6a745bb145049ceee2bfdbc0d82..3498c78f627204783bfa574492afdf41bd9a6a83 100644 (file)
@@ -206,8 +206,35 @@ PAKFIRE_EXPORT void pakfire_repo_set_enabled(PakfireRepo repo, int enabled) {
        pakfire_pool_has_changed(repo->pakfire);
 }
 
+// Returns a default priority based on the repository configuration
+static int pakfire_repo_auto_priority(PakfireRepo repo) {
+       // The @system repository has a priority of zero
+       if (pakfire_repo_is_installed_repo(repo) == 0)
+               return 0;
+
+       if (repo->appdata->baseurl) {
+               // HTTPS
+               if (pakfire_string_startswith(repo->appdata->baseurl, "https://"))
+                       return 75;
+
+               // HTTP
+               if (pakfire_string_startswith(repo->appdata->baseurl, "http://"))
+                       return 75;
+
+               // Local path
+               if (pakfire_string_startswith(repo->appdata->baseurl, "dir://"))
+                       return 50;
+       }
+
+       // Default to 100
+       return 100;
+}
+
 PAKFIRE_EXPORT int pakfire_repo_get_priority(PakfireRepo repo) {
-       return repo->repo->priority;
+       if (repo->repo->priority > 0)
+               return repo->repo->priority;
+
+       return pakfire_repo_auto_priority(repo);
 }
 
 PAKFIRE_EXPORT void pakfire_repo_set_priority(PakfireRepo repo, int priority) {
index 6b7c5218d7cd4fc032442ed68ff8afe832078a9d..df1a7475369d85c3e92d4839a2793cc8df511d23 100644 (file)
@@ -95,6 +95,10 @@ char* pakfire_strdup(const char* s) {
        return r;
 }
 
+int pakfire_string_startswith(const char* s, const char* prefix) {
+       return strncmp(s, prefix, strlen(prefix));
+}
+
 char* pakfire_format_size(double size) {
        char string[STRING_SIZE];
        const char* units[] = {" ", "k", "M", "G", "T", NULL};
index 9b52f6a281e2c21857be2506732f8bd3a1e6c8ed..416884af6077e05ed5b6ceafde796e00702b9f5a 100644 (file)
@@ -58,34 +58,6 @@ class RepositoryRemote(base.RepositoryFactory):
 
                return keyfile
 
-       @property
-       def priority(self):
-               priority = self.settings.get("priority", None)
-               if not priority is None:
-                       # Try to concert the given input to an integer
-                       # and return the value if possible.
-                       try:
-                               priority = int(priority)
-                               return priority
-
-                       except ValueError:
-                               pass
-
-               # The default priority is 100.
-               priority = 100
-
-               url2priority = {
-                       "file://" : 50,
-                       "http://" : 75,
-               }
-
-               for url, prio in list(url2priority.items()):
-                       if self.baseurl.startswith(url):
-                               priority = prio
-                               break
-
-               return priority
-
        def make_downloader(self):
                """
                        Creates a downloader that can be used to download