git@vger.kernel.org list mirror (unofficial, one of many)
 help / color / mirror / code / Atom feed
* [PATCH] TIG: Implement mkstemps() work-around for platforms lacking it
@ 2013-07-09 15:33 Drew Northup
  2013-07-18  4:45 ` Drew Northup
  0 siblings, 1 reply; 2+ messages in thread
From: Drew Northup @ 2013-07-09 15:33 UTC (permalink / raw)
  To: git
  Cc: David Aguilar, Junio C Hamano, Johannes Sixt, Jonas Fonseca,
	Drew Northup

The function mkstemps() isn't available in all libc implementations. In
glibc it first became available in 2.11, so platforms such as RHEL 5 &
Slackware 13 lack it. This is likely true of many non-LINUX platforms
as well.

This fixes breakage that was introduced with a0fdac29 "Create temporary
file with name as suffix."

Signed-off-by: Drew Northup <n1xim.email@gmail.com>
---

This work-around is taken from Git and was inspired by code in libiberty.
It is presumed that this isn't a problem due to compatible license terms.

A (virtually identical) version of this available in
https://github.com/n1xim/tig/tree/mkstemps_wkarnd (differences only in
the commit message).

 configure.ac |  4 ++++
 io.c         | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 io.h         | 14 +++++++++++
 3 files changed, 95 insertions(+)

diff --git a/configure.ac b/configure.ac
index 8dd2508..40e1f85 100644
--- a/configure.ac
+++ b/configure.ac
@@ -21,6 +21,10 @@ AC_SUBST(CURSES_LIB)
 
 AM_ICONV
 
+dnl  Not all platforms have mkstemps
+AC_CHECK_FUNC([mkstemps], [AC_DEFINE([HAVE_MKSTEMPS], [1],
+	      [Define if mkstemps is available.])])
+
 AC_PROG_CC
 
 AC_CHECK_PROGS(ASCIIDOC, [asciidoc], [false])
diff --git a/io.c b/io.c
index 3ff1d1c..f1b6fbc 100644
--- a/io.c
+++ b/io.c
@@ -237,6 +237,83 @@ encoding_convert(struct encoding *encoding, char *line)
 }
 
 /*
+ * Compatibility: no mkstemps()
+ */
+
+/* Adapted from libiberty's mkstemp.c via Git's wrapper.c. */
+
+#undef TMP_MAX
+#define TMP_MAX 16384
+
+int tig_mkstemps_mode(char *pattern, int suffix_len, int mode)
+{
+	static const char letters[] =
+		"abcdefghijklmnopqrstuvwxyz"
+		"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+		"0123456789";
+	static const int num_letters = 62;
+	uint64_t value;
+	struct timeval tv;
+	char *template;
+	size_t len;
+	int fd, count;
+
+	len = strlen(pattern);
+
+	if (len < 6 + suffix_len) {
+		errno = EINVAL;
+		return -1;
+	}
+
+	if (strncmp(&pattern[len - 6 - suffix_len], "XXXXXX", 6)) {
+		errno = EINVAL;
+		return -1;
+	}
+
+	/*
+	 * Replace pattern's XXXXXX characters with randomness.
+	 * Try TMP_MAX different filenames.
+	 */
+	gettimeofday(&tv, NULL);
+	value = ((size_t)(tv.tv_usec << 16)) ^ tv.tv_sec ^ getpid();
+	template = &pattern[len - 6 - suffix_len];
+	for (count = 0; count < TMP_MAX; ++count) {
+		uint64_t v = value;
+		/* Fill in the random bits. */
+		template[0] = letters[v % num_letters]; v /= num_letters;
+		template[1] = letters[v % num_letters]; v /= num_letters;
+		template[2] = letters[v % num_letters]; v /= num_letters;
+		template[3] = letters[v % num_letters]; v /= num_letters;
+		template[4] = letters[v % num_letters]; v /= num_letters;
+		template[5] = letters[v % num_letters]; v /= num_letters;
+
+		fd = open(pattern, O_CREAT | O_EXCL | O_RDWR, mode);
+		if (fd > 0)
+			return fd;
+		/*
+		 * Fatal error (EPERM, ENOSPC etc).
+		 * It doesn't make sense to loop.
+		 */
+		if (errno != EEXIST)
+			break;
+		/*
+		 * This is a random value.  It is only necessary that
+		 * the next TMP_MAX values generated by adding 7777 to
+		 * VALUE are different with (module 2^32).
+		 */
+		value += 7777;
+	}
+	/* We return the null string if we can't find a unique file name.  */
+	pattern[0] = '\0';
+	return -1;
+}
+
+int tigmkstemps(char *pattern, int suffix_len)
+{
+	return tig_mkstemps_mode(pattern, suffix_len, 0600);
+}
+
+/*
  * Executing external commands.
  */
 
diff --git a/io.h b/io.h
index 646989d..8f43216 100644
--- a/io.h
+++ b/io.h
@@ -16,6 +16,9 @@
 
 #include "tig.h"
 
+/* Needed for mkstemps workaround */
+#include <stdint.h>
+
 /*
  * Argument array helpers.
  */
@@ -41,6 +44,17 @@ struct encoding *encoding_open(const char *fromcode);
 char *encoding_convert(struct encoding *encoding, char *line);
 
 /*
+ * Compatibility: no mkstemps()
+ */
+
+#ifndef HAVE_MKSTEMPS
+#define mkstemps tigmkstemps
+#endif
+
+int tigmkstemps(char *, int);
+int tig_mkstemps_mode(char *pattern, int suffix_len, int mode);
+
+/*
  * Executing external commands.
  */
 
-- 
1.8.0

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] TIG: Implement mkstemps() work-around for platforms lacking it
  2013-07-09 15:33 [PATCH] TIG: Implement mkstemps() work-around for platforms lacking it Drew Northup
@ 2013-07-18  4:45 ` Drew Northup
  0 siblings, 0 replies; 2+ messages in thread
From: Drew Northup @ 2013-07-18  4:45 UTC (permalink / raw)
  To: Drew Northup
  Cc: git, David Aguilar, Junio C Hamano, Johannes Sixt, Jonas Fonseca

Giving this one last kick to make absolutely sure that nobody disagrees
with allowing this code to be included into tig, which does not limit
to a specific version of the GPL (version 2 in the case of git, any
version equal to or newer than 2 in the case of tig), pursuant to
paragraph 9 of said license.

On 07/09/2013 11:33 AM, Drew Northup wrote:
> The function mkstemps() isn't available in all libc implementations. In
> glibc it first became available in 2.11, so platforms such as RHEL 5&
> Slackware 13 lack it. This is likely true of many non-LINUX platforms
> as well.
>
> This fixes breakage that was introduced with a0fdac29 "Create temporary
> file with name as suffix."
>
> Signed-off-by: Drew Northup<n1xim.email@gmail.com>
> ---
>
> This work-around is taken from Git and was inspired by code in libiberty.
> It is presumed that this isn't a problem due to compatible license terms.
>
> A (virtually identical) version of this available in
> https://github.com/n1xim/tig/tree/mkstemps_wkarnd (differences only in
> the commit message).
>
>   configure.ac |  4 ++++
>   io.c         | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>   io.h         | 14 +++++++++++
>   3 files changed, 95 insertions(+)
>
> diff --git a/configure.ac b/configure.ac
> index 8dd2508..40e1f85 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -21,6 +21,10 @@ AC_SUBST(CURSES_LIB)
>
>   AM_ICONV
>
> +dnl  Not all platforms have mkstemps
> +AC_CHECK_FUNC([mkstemps], [AC_DEFINE([HAVE_MKSTEMPS], [1],
> +	      [Define if mkstemps is available.])])
> +
>   AC_PROG_CC
>
>   AC_CHECK_PROGS(ASCIIDOC, [asciidoc], [false])
> diff --git a/io.c b/io.c
> index 3ff1d1c..f1b6fbc 100644
> --- a/io.c
> +++ b/io.c
> @@ -237,6 +237,83 @@ encoding_convert(struct encoding *encoding, char *line)
>   }
>
>   /*
> + * Compatibility: no mkstemps()
> + */
> +
> +/* Adapted from libiberty's mkstemp.c via Git's wrapper.c. */
> +
> +#undef TMP_MAX
> +#define TMP_MAX 16384
> +
> +int tig_mkstemps_mode(char *pattern, int suffix_len, int mode)
> +{
> +	static const char letters[] =
> +		"abcdefghijklmnopqrstuvwxyz"
> +		"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
> +		"0123456789";
> +	static const int num_letters = 62;
> +	uint64_t value;
> +	struct timeval tv;
> +	char *template;
> +	size_t len;
> +	int fd, count;
> +
> +	len = strlen(pattern);
> +
> +	if (len<  6 + suffix_len) {
> +		errno = EINVAL;
> +		return -1;
> +	}
> +
> +	if (strncmp(&pattern[len - 6 - suffix_len], "XXXXXX", 6)) {
> +		errno = EINVAL;
> +		return -1;
> +	}
> +
> +	/*
> +	 * Replace pattern's XXXXXX characters with randomness.
> +	 * Try TMP_MAX different filenames.
> +	 */
> +	gettimeofday(&tv, NULL);
> +	value = ((size_t)(tv.tv_usec<<  16)) ^ tv.tv_sec ^ getpid();
> +	template =&pattern[len - 6 - suffix_len];
> +	for (count = 0; count<  TMP_MAX; ++count) {
> +		uint64_t v = value;
> +		/* Fill in the random bits. */
> +		template[0] = letters[v % num_letters]; v /= num_letters;
> +		template[1] = letters[v % num_letters]; v /= num_letters;
> +		template[2] = letters[v % num_letters]; v /= num_letters;
> +		template[3] = letters[v % num_letters]; v /= num_letters;
> +		template[4] = letters[v % num_letters]; v /= num_letters;
> +		template[5] = letters[v % num_letters]; v /= num_letters;
> +
> +		fd = open(pattern, O_CREAT | O_EXCL | O_RDWR, mode);
> +		if (fd>  0)
> +			return fd;
> +		/*
> +		 * Fatal error (EPERM, ENOSPC etc).
> +		 * It doesn't make sense to loop.
> +		 */
> +		if (errno != EEXIST)
> +			break;
> +		/*
> +		 * This is a random value.  It is only necessary that
> +		 * the next TMP_MAX values generated by adding 7777 to
> +		 * VALUE are different with (module 2^32).
> +		 */
> +		value += 7777;
> +	}
> +	/* We return the null string if we can't find a unique file name.  */
> +	pattern[0] = '\0';
> +	return -1;
> +}
> +
> +int tigmkstemps(char *pattern, int suffix_len)
> +{
> +	return tig_mkstemps_mode(pattern, suffix_len, 0600);
> +}
> +
> +/*
>    * Executing external commands.
>    */
>
> diff --git a/io.h b/io.h
> index 646989d..8f43216 100644
> --- a/io.h
> +++ b/io.h
> @@ -16,6 +16,9 @@
>
>   #include "tig.h"
>
> +/* Needed for mkstemps workaround */
> +#include<stdint.h>
> +
>   /*
>    * Argument array helpers.
>    */
> @@ -41,6 +44,17 @@ struct encoding *encoding_open(const char *fromcode);
>   char *encoding_convert(struct encoding *encoding, char *line);
>
>   /*
> + * Compatibility: no mkstemps()
> + */
> +
> +#ifndef HAVE_MKSTEMPS
> +#define mkstemps tigmkstemps
> +#endif
> +
> +int tigmkstemps(char *, int);
> +int tig_mkstemps_mode(char *pattern, int suffix_len, int mode);
> +
> +/*
>    * Executing external commands.
>    */
>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2013-07-18  4:52 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-07-09 15:33 [PATCH] TIG: Implement mkstemps() work-around for platforms lacking it Drew Northup
2013-07-18  4:45 ` Drew Northup

git@vger.kernel.org list mirror (unofficial, one of many)

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://public-inbox.org/git
	git clone --mirror http://ou63pmih66umazou.onion/git
	git clone --mirror http://czquwvybam4bgbro.onion/git
	git clone --mirror http://hjrcffqmbrq6wope.onion/git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 git git/ https://public-inbox.org/git \
		git@vger.kernel.org
	public-inbox-index git

Example config snippet for mirrors.
Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.version-control.git
	nntp://7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd.onion/inbox.comp.version-control.git
	nntp://ie5yzdi7fg72h7s4sdcztq5evakq23rdt33mfyfcddc5u3ndnw24ogqd.onion/inbox.comp.version-control.git
	nntp://4uok3hntl7oi7b4uf4rtfwefqeexfzil2w6kgk2jn5z2f764irre7byd.onion/inbox.comp.version-control.git
	nntp://news.gmane.io/gmane.comp.version-control.git
 note: .onion URLs require Tor: https://www.torproject.org/

code repositories for project(s) associated with this inbox:

	https://80x24.org/mirrors/git.git

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git