+/**
+ * Given a unixpath that is too long (larger than UNIX_PATH_MAX),
+ * shorten it to an acceptable length while keeping it unique
+ * and making sure it remains a valid filename (if possible).
+ *
+ * @param unixpath long path, will be freed (or same pointer returned
+ * with moved 0-termination).
+ * @return shortened unixpath, NULL on error
+ */
+char *
+GNUNET_NETWORK_shorten_unixpath (char *unixpath)
+{
+ struct sockaddr_un dummy;
+ size_t slen;
+ char *end;
+ struct GNUNET_CRYPTO_ShortHashCode sh;
+ struct GNUNET_CRYPTO_ShortHashAsciiEncoded ae;
+ size_t upm;
+
+ upm = sizeof (dummy.sun_path);
+ slen = strlen (unixpath);
+ if (slen < upm)
+ return unixpath; /* no shortening required */
+ GNUNET_CRYPTO_short_hash (unixpath, slen, &sh);
+ while (sizeof (struct GNUNET_CRYPTO_ShortHashAsciiEncoded) +
+ strlen (unixpath) >= upm)
+ {
+ if (NULL == (end = strrchr (unixpath, '/')))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ _("Unable to shorten unix path `%s' while keeping name unique\n"),
+ unixpath);
+ GNUNET_free (unixpath);
+ return NULL;
+ }
+ *end = '\0';
+ }
+ GNUNET_CRYPTO_short_hash_to_enc (&sh, &ae);
+ strcat (unixpath, (char*) ae.short_encoding);
+ return unixpath;
+}
+
+