60ed166
From a814fa3f3a2067a17a6698d2fcb6230774d0f981 Mon Sep 17 00:00:00 2001
60ed166
From: Jan Synacek <jsynacek@redhat.com>
60ed166
Date: Fri, 2 Oct 2015 11:00:19 +0200
60ed166
Subject: [PATCH 06/12] import: hash URL in paths if they are too long
60ed166
60ed166
https://bugzilla.redhat.com/show_bug.cgi?id=1266775
60ed166
(cherry picked from commit 9818005de6cc6f525be8aa62a2b8593a7c72e798)
60ed166
60ed166
Resolves: #1266775
60ed166
---
60ed166
 src/import/pull-common.c | 44 ++++++++++++++++++++++++++++++++++++++------
60ed166
 1 file changed, 38 insertions(+), 6 deletions(-)
60ed166
60ed166
diff --git a/src/import/pull-common.c b/src/import/pull-common.c
60ed166
index 652277e..84611cb 100644
60ed166
--- a/src/import/pull-common.c
60ed166
+++ b/src/import/pull-common.c
60ed166
@@ -31,8 +31,10 @@
60ed166
 #include "pull-common.h"
60ed166
 #include "process-util.h"
60ed166
 #include "signal-util.h"
60ed166
+#include "siphash24.h"
60ed166
 
60ed166
 #define FILENAME_ESCAPE "/.#\"\'"
60ed166
+#define HASH_URL_THRESHOLD_LENGTH (_POSIX_PATH_MAX - 16)
60ed166
 
60ed166
 int pull_find_old_etags(const char *url, const char *image_root, int dt, const char *prefix, const char *suffix, char ***etags) {
60ed166
         _cleanup_free_ char *escaped_url = NULL;
60ed166
@@ -142,8 +144,21 @@ int pull_make_local_copy(const char *final, const char *image_root, const char *
60ed166
         return 0;
60ed166
 }
60ed166
 
60ed166
+static int hash_url(const char *url, char **ret) {
60ed166
+        uint64_t h;
60ed166
+        static const sd_id128_t k = SD_ID128_ARRAY(df,89,16,87,01,cc,42,30,98,ab,4a,19,a6,a5,63,4f);
60ed166
+
60ed166
+        assert(url);
60ed166
+
60ed166
+        siphash24((uint8_t *) &h, url, strlen(url), k.bytes);
60ed166
+        if (asprintf(ret, "%"PRIx64, h) < 0)
60ed166
+                return -ENOMEM;
60ed166
+
60ed166
+        return 0;
60ed166
+}
60ed166
+
60ed166
 int pull_make_path(const char *url, const char *etag, const char *image_root, const char *prefix, const char *suffix, char **ret) {
60ed166
-        _cleanup_free_ char *escaped_url = NULL;
60ed166
+        _cleanup_free_ char *escaped_url = NULL, *escaped_etag = NULL;
60ed166
         char *path;
60ed166
 
60ed166
         assert(url);
60ed166
@@ -157,18 +172,35 @@ int pull_make_path(const char *url, const char *etag, const char *image_root, co
60ed166
                 return -ENOMEM;
60ed166
 
60ed166
         if (etag) {
60ed166
-                _cleanup_free_ char *escaped_etag = NULL;
60ed166
-
60ed166
                 escaped_etag = xescape(etag, FILENAME_ESCAPE);
60ed166
                 if (!escaped_etag)
60ed166
                         return -ENOMEM;
60ed166
+        }
60ed166
 
60ed166
-                path = strjoin(image_root, "/", strempty(prefix), escaped_url, ".", escaped_etag, strempty(suffix), NULL);
60ed166
-        } else
60ed166
-                path = strjoin(image_root, "/", strempty(prefix), escaped_url, strempty(suffix), NULL);
60ed166
+        path = strjoin(image_root, "/", strempty(prefix), escaped_url, escaped_etag ? "." : "",
60ed166
+                       strempty(escaped_etag), strempty(suffix), NULL);
60ed166
         if (!path)
60ed166
                 return -ENOMEM;
60ed166
 
60ed166
+        /* URLs might make the path longer than the maximum allowed length for a file name.
60ed166
+         * When that happens, a URL hash is used instead. Paths returned by this function
60ed166
+         * can be later used with tempfn_random() which adds 16 bytes to the resulting name. */
60ed166
+        if (strlen(path) >= HASH_URL_THRESHOLD_LENGTH) {
60ed166
+                _cleanup_free_ char *hash = NULL;
60ed166
+                int r;
60ed166
+
60ed166
+                free(path);
60ed166
+
60ed166
+                r = hash_url(url, &hash);
60ed166
+                if (r < 0)
60ed166
+                        return r;
60ed166
+
60ed166
+                path = strjoin(image_root, "/", strempty(prefix), hash, escaped_etag ? "." : "",
60ed166
+                               strempty(escaped_etag), strempty(suffix), NULL);
60ed166
+                if (!path)
60ed166
+                        return -ENOMEM;
60ed166
+        }
60ed166
+
60ed166
         *ret = path;
60ed166
         return 0;
60ed166
 }
60ed166
-- 
60ed166
2.5.0
60ed166