Skip to contentSkip to conversation

compat: add a mkstemps() compatibility function

PATCH Superseded David Aguilar 3 participants 7 messages Jun 15, 2016 Kbuild

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 compat: add a mkstemps() compatibility function

mkstemps() is a BSD extension so provide an implementation for cross-platform use.

---

This includes Jakub's autoconf fixups.

 Makefile          |   19 +++++++++++++
 compat/mkstemps.c |   78 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 config.mak.in     |    1 +
 configure.ac      |    6 ++++
 git-compat-util.h |    5 +++
 5 files changed, 109 insertions(+), 0 deletions(-)
 create mode 100644 compat/mkstemps.c
Makefile +19 -0
index eaae45d..a70b5f0 100644
@@ -52,6 +52,8 @@ all::
 #
 # Define NO_MKDTEMP if you don't have mkdtemp in the C library.
 #
+# Define NO_MKSTEMPS if you don't have mkstemps in the C library.
+#
 # Define NO_SYS_SELECT_H if you don't have sys/select.h.
 #
 # Define NO_SYMLINK_HEAD if you never want .git/HEAD to be a symbolic link.
@@ -636,10 +638,12 @@ EXTLIBS =
 
 ifeq ($(uname_S),Linux)
 	NO_STRLCPY = YesPlease
+	NO_MKSTEMPS = YesPlease
 	THREADED_DELTA_SEARCH = YesPlease
 endif
 ifeq ($(uname_S),GNU/kFreeBSD)
 	NO_STRLCPY = YesPlease
+	NO_MKSTEMPS = YesPlease
 	THREADED_DELTA_SEARCH = YesPlease
 endif
 ifeq ($(uname_S),UnixWare)
@@ -651,6 +655,7 @@ ifeq ($(uname_S),UnixWare)
 	SHELL_PATH = /usr/local/bin/bash
 	NO_IPV6 = YesPlease
 	NO_HSTRERROR = YesPlease
+	NO_MKSTEMPS = YesPlease
 	BASIC_CFLAGS += -Kthread
 	BASIC_CFLAGS += -I/usr/local/include
 	BASIC_LDFLAGS += -L/usr/local/lib
@@ -674,6 +679,7 @@ ifeq ($(uname_S),SCO_SV)
 	SHELL_PATH = /usr/bin/bash
 	NO_IPV6 = YesPlease
 	NO_HSTRERROR = YesPlease
+	NO_MKSTEMPS = YesPlease
 	BASIC_CFLAGS += -I/usr/local/include
 	BASIC_LDFLAGS += -L/usr/local/lib
 	NO_STRCASESTR = YesPlease
@@ -702,6 +708,7 @@ ifeq ($(uname_S),SunOS)
 	NO_MEMMEM = YesPlease
 	NO_HSTRERROR = YesPlease
 	NO_MKDTEMP = YesPlease
+	NO_MKSTEMPS = YesPlease
 	OLD_ICONV = UnfortunatelyYes
 	ifeq ($(uname_R),5.8)
 		NO_UNSETENV = YesPlease
@@ -724,6 +731,7 @@ ifeq ($(uname_O),Cygwin)
 	NO_D_INO_IN_DIRENT = YesPlease
 	NO_STRCASESTR = YesPlease
 	NO_MEMMEM = YesPlease
+	NO_MKSTEMPS = YesPlease
 	NO_SYMLINK_HEAD = YesPlease
 	NEEDS_LIBICONV = YesPlease
 	NO_FAST_WORKING_DIRECTORY = UnfortunatelyYes
@@ -767,11 +775,13 @@ ifeq ($(uname_S),NetBSD)
 	BASIC_LDFLAGS += -L/usr/pkg/lib $(CC_LD_DYNPATH)/usr/pkg/lib
 	THREADED_DELTA_SEARCH = YesPlease
 	USE_ST_TIMESPEC = YesPlease
+	NO_MKSTEMPS = YesPlease
 endif
 ifeq ($(uname_S),AIX)
 	NO_STRCASESTR=YesPlease
 	NO_MEMMEM = YesPlease
 	NO_MKDTEMP = YesPlease
+	NO_MKSTEMPS = YesPlease
 	NO_STRLCPY = YesPlease
 	NO_NSEC = YesPlease
 	FREAD_READS_DIRECTORIES = UnfortunatelyYes
@@ -787,12 +797,14 @@ endif
 ifeq ($(uname_S),GNU)
 	# GNU/Hurd
 	NO_STRLCPY=YesPlease
+	NO_MKSTEMPS = YesPlease
 endif
 ifeq ($(uname_S),IRIX64)
 	NO_IPV6=YesPlease
 	NO_SETENV=YesPlease
 	NO_STRCASESTR=YesPlease
 	NO_MEMMEM = YesPlease
+	NO_MKSTEMPS = YesPlease
 	NO_STRLCPY = YesPlease
 	NO_SOCKADDR_STORAGE=YesPlease
 	SHELL_PATH=/usr/gnu/bin/bash
@@ -805,6 +817,7 @@ ifeq ($(uname_S),HP-UX)
 	NO_SETENV=YesPlease
 	NO_STRCASESTR=YesPlease
 	NO_MEMMEM = YesPlease
+	NO_MKSTEMPS = YesPlease
 	NO_STRLCPY = YesPlease
 	NO_MKDTEMP = YesPlease
 	NO_UNSETENV = YesPlease
@@ -834,6 +847,7 @@ ifneq (,$(findstring MINGW,$(uname_S)))
 	NO_C99_FORMAT = YesPlease
 	NO_STRTOUMAX = YesPlease
 	NO_MKDTEMP = YesPlease
+	NO_MKSTEMPS = YesPlease
 	SNPRINTF_RETURNS_BOGUS = YesPlease
 	NO_SVN_TESTS = YesPlease
 	NO_PERL_MAKEMAKER = YesPlease
@@ -853,6 +867,7 @@ ifneq (,$(findstring MINGW,$(uname_S)))
 endif
 ifneq (,$(findstring arm,$(uname_M)))
 	ARM_SHA1 = YesPlease
+	NO_MKSTEMPS = YesPlease
 endif
 
 -include config.mak.autogen
@@ -1011,6 +1026,10 @@ ifdef NO_MKDTEMP
 	COMPAT_CFLAGS += -DNO_MKDTEMP
 	COMPAT_OBJS += compat/mkdtemp.o
 endif
+ifdef NO_MKSTEMPS
+	COMPAT_CFLAGS += -DNO_MKSTEMPS
+	COMPAT_OBJS += compat/mkstemps.o
+endif
 ifdef NO_UNSETENV
 	COMPAT_CFLAGS += -DNO_UNSETENV
 	COMPAT_OBJS += compat/unsetenv.o
compat/mkstemps.c new file +78 -0

78 lines not on this page · raw message

config.mak.in +1 -0
index 7cce0c1..b6619af 100644
@@ -46,6 +46,7 @@ NO_STRTOUMAX=@NO_STRTOUMAX@
 NO_SETENV=@NO_SETENV@
 NO_UNSETENV=@NO_UNSETENV@
 NO_MKDTEMP=@NO_MKDTEMP@
+NO_MKSTEMPS=@NO_MKSTEMPS@
 NO_ICONV=@NO_ICONV@
 OLD_ICONV=@OLD_ICONV@
 NO_DEFLATE_BOUND=@NO_DEFLATE_BOUND@
configure.ac +6 -0
index 4e728bc..95dccd4 100644
@@ -676,6 +676,12 @@ GIT_CHECK_FUNC(mkdtemp,
 [NO_MKDTEMP=],
 [NO_MKDTEMP=YesPlease])
 AC_SUBST(NO_MKDTEMP)
+# Define NO_MKSTEMPS if you don't have mkstemps in the C library.
+GIT_CHECK_FUNC(mkstemps,
+[NO_MKSTEMPS=],
+[NO_MKSTEMPS=YesPlease])
+AC_SUBST(NO_MKSTEMPS)
+#
 #
 # Define NO_MMAP if you want to avoid mmap.
 #
git-compat-util.h +5 -0
index c7cf2d5..f7217ad 100644
@@ -232,6 +232,11 @@ extern int gitsetenv(const char *, const char *, int);
 extern char *gitmkdtemp(char *);
 #endif
 
+#ifdef NO_MKSTEMPS
+#define mkstemps gitmkstemps
+extern int gitmkstemps(char *, int);
+#endif
+
 #ifdef NO_UNSETENV
 #define unsetenv gitunsetenv
 extern void gitunsetenv(const char *);
Signature · 1 line
1.6.3.1.30.g55524
#1 lore raw

PATCH v2 2/2 diff: generate prettier filenames when using GIT_EXTERNAL_DIFF

Naturally, prep_temp_blob() did not care about filenames. As a result, scripts that use GIT_EXTERNAL_DIFF ended up with filenames such as ".diff_XXXXXX".

This specializes the GIT_EXTERNAL_DIFF code to generate user-friendly filenames when creating temporary files.

Diffing "name.ext" now generates "XXXXXX_name.ext".

---

This includes Hannes' suggestion to avoid basename() for Windows portability.

 cache.h                  |    2 ++
 diff.c                   |   45 +++++++++++++++++++++++++++++++++++++--------
 path.c                   |   19 +++++++++++++++++++
 t/t4020-diff-external.sh |    9 +++++++++
 4 files changed, 67 insertions(+), 8 deletions(-)
cache.h +2 -0
index b8503ad..871c984 100644
@@ -614,6 +614,8 @@ extern int is_empty_blob_sha1(const unsigned char *sha1);
 
 int git_mkstemp(char *path, size_t n, const char *template);
 
+int git_mkstemps(char *path, size_t n, const char *template, int suffix_len);
+
 /*
  * NOTE NOTE NOTE!!
  *
diff.c +37 -8

86 lines not on this page · raw message

path.c +19 -0
index 8a0a674..090b490 100644
@@ -140,6 +140,25 @@ int git_mkstemp(char *path, size_t len, const char *template)
 }
 
 
+
+/* git_mkstemps() - create tmp file with suffix honoring TMPDIR variable. */
+int git_mkstemps(char *path, size_t len, const char *template, int suffix_len)
+{
+	const char *tmp;
+	size_t n;
+
+	tmp = getenv("TMPDIR");
+	if (!tmp)
+		tmp = "/tmp";
+	n = snprintf(path, len, "%s/%s", tmp, template);
+	if (len <= n) {
+		errno = ENAMETOOLONG;
+		return -1;
+	}
+	return mkstemps(path, suffix_len);
+}
+
+
 int validate_headref(const char *path)
 {
 	struct stat st;
t/t4020-diff-external.sh +9 -0
index 0720001..4ea42e0 100755
@@ -136,6 +136,15 @@ test_expect_success 'GIT_EXTERNAL_DIFF with more than one changed files' '
 	GIT_EXTERNAL_DIFF=echo git diff
 '
 
+test_expect_success 'GIT_EXTERNAL_DIFF generates pretty paths' '
+	touch file.ext &&
+	git add file.ext &&
+	echo with extension > file.ext &&
+	GIT_EXTERNAL_DIFF=echo git diff file.ext | grep ......_file\.ext &&
+	git update-index --force-remove file.ext &&
+	rm file.ext
+'
+
 echo "#!$SHELL_PATH" >fake-diff.sh
 cat >> fake-diff.sh <<\EOF
 cat $2 >> crlfed.txt
Signature · 1 line
1.6.3.1.30.g55524
#2 lore raw

Re: [PATCH v2 2/2] diff: generate prettier filenames when using GIT_EXTERNAL_DIFF

Quoted 1 line · David Aguilar

+int git_mkstemps(char *path, size_t n, const char *template, int suffix_len);

FWIW, I find this name not very descriptive. From the name, I would expect it to do the exact same thing as mkstemps, but be our own personal implementation. But it is actually a wrapper that behaves somewhat differently. So I wonder if "mkstemps_tmpdir" or something would be a better name.

diff.c static void prep_temp_blob(const char *path, struct diff_tempfile *temp, · line 1970

Quoted 2 lines
diff.c +2 -0
+	if (pretty_filename) {
+		/* Generate "XXXXXX_filename" */

Is there a reason _not_ to always just use the pretty filename? It looks like you turn it on for external diff, but off for textconv. I don't think there is a reason not to use it for textconv.

Is there some other code path that changes it that I'm missing?

diff.c static void run_external_diff(const char *pgm, · line 2103

Quoted 3 lines
diff.c +3 -0
+		int pretty_filename = 1;
+		temp_one = prepare_temp_file(name, one, pretty_filename);
+		temp_two = prepare_temp_file(othername, two, pretty_filename);

I think this reads much better as just:

  temp_one = prepare_temp_file(name, one, 1);
  temp_two = prepare_temp_file(othername, two, 1);

Then it eliminates one bit of state for the reader to keep track of; I don't have to wonder if pretty_filename might ever change. If I care about what the '1' means, I can go look at the definition of prepare_temp_file (or if you really want to make it more self-documenting, make it a "flags" field and set the USE_PRETTY_FILENAME flag).

However, I suspect that all callers should use pretty filenames, and then this bit would just go away.

diff.c static char *run_textconv(const char *pgm, struct diff_filespec *spec, · line 3607

Quoted 4 lines
diff.c +2 -1
+	int pretty_filename = 0;
 
-	temp = prepare_temp_file(spec->path, spec);
+	temp = prepare_temp_file(spec->path, spec, pretty_filename);

Ditto here.

-Peff

#3 lore raw

Re: [PATCH v2 2/2] diff: generate prettier filenames when using GIT_EXTERNAL_DIFF

Quoted 9 lines · Jeff King
Quoted 1 line · David Aguilar

+int git_mkstemps(char *path, size_t n, const char *template, int suffix_len);

FWIW, I find this name not very descriptive. From the name, I would expect it to do the exact same thing as mkstemps, but be our own personal implementation. But it is actually a wrapper that behaves somewhat differently. So I wonder if "mkstemps_tmpdir" or something would be a better name.

It does exactly what git_mkstemp() does, plus the extra suffix_len parameter. If we rename this function we have to rename both.

Quoted 3 lines

Is there a reason _not_ to always just use the pretty filename? It looks like you turn it on for external diff, but off for textconv. I don't think there is a reason not to use it for textconv.

I was not aware of the other code paths and only wanted to affect the one that I knew about. I agree that making that the default behavior would be great, meaning we could drop the pretty_filename flag altogether.

If you and others agree that the user-friendly names are a good thing to have by default then I can rework patch 2/2.

Quoted 2 lines

However, I suspect that all callers should use pretty filenames, and then this bit would just go away.

I fully agree.

Signature · 1 line
		David
#4 lore raw

Re: [PATCH v2 2/2] diff: generate prettier filenames when using GIT_EXTERNAL_DIFF

Quoted 9 lines · David Aguilar
Quoted 5 lines

FWIW, I find this name not very descriptive. From the name, I would expect it to do the exact same thing as mkstemps, but be our own personal implementation. But it is actually a wrapper that behaves somewhat differently. So I wonder if "mkstemps_tmpdir" or something would be a better name.

It does exactly what git_mkstemp() does, plus the extra suffix_len parameter. If we rename this function we have to rename both.

Ah. Well, I think that is a crappy name, too, then, but it is not your fault. ;) So if there is precedent, I guess somebody else thought it was a good idea, and you should leave your patch as-is.

Quoted 7 lines

I was not aware of the other code paths and only wanted to affect the one that I knew about. I agree that making that the default behavior would be great, meaning we could drop the pretty_filename flag altogether.

If you and others agree that the user-friendly names are a good thing to have by default then I can rework patch 2/2.

Switching to user-friendly temp filenames for textconv was on my todo list, so I definitely think it is a good idea.

-Peff

Tested-by on v2 1/2
#5 lore raw
In reply to David Aguilar
Quoted 10 lines · David Aguilar

+++ b/compat/mkstemps.c

(quoted diff) +8 -0
@@ -0,0 +1,78 @@
+#include <string.h>
+#include <errno.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <unistd.h>
+#include <sys/time.h>
+#include <sys/types.h>

...

Quoted 3 lines
(quoted diff) +3 -0
+#ifndef O_BINARY
+#define O_BINARY 0
+#endif

...

Review

Quoted 1 line
(quoted diff) +1 -0
+		fd = open(pattern, O_BINARY|O_CREAT|O_EXCL|O_RDWR, 0600);

You should not include "random" system headers, nor has mkstemps any business deciding whether files are opened in binary mode. We are not using O_BINARY anywhere else (except in compat/mingw.c). With the patch below squashed in (I hope it won't be wrapped) you can add:

to both your patches. And, yes, I like them :-)

-- Hannes

compat/mkstemps.c +2 -13
index 10f9ed6..1cf7f3d 100644
@@ -1,20 +1,9 @@
-#include <string.h>
-#include <errno.h>
-#include <stdio.h>
-#include <fcntl.h>
-#include <inttypes.h>
-#include <unistd.h>
-#include <sys/time.h>
-#include <sys/types.h>
+#include "../git-compat-util.h"
 
 #ifndef TMP_MAX
 #define TMP_MAX 16384
 #endif
 
-#ifndef O_BINARY
-#define O_BINARY 0
-#endif
-
 /* Adapted from libiberty's mkstemp.c. */
 int gitmkstemps(char *pattern, int suffix_len)
 {
@@ -57,7 +46,7 @@ int gitmkstemps(char *pattern, int suffix_len)
 		template[4] = letters[v % num_letters]; v/= num_letters;
 		template[5] = letters[v % num_letters]; v/= num_letters;
 
-		fd = open(pattern, O_BINARY|O_CREAT|O_EXCL|O_RDWR, 0600);
+		fd = open(pattern, O_CREAT|O_EXCL|O_RDWR, 0600);
 		if (fd > 0)
 			return fd;
 		/* Fatal error (EPERM, ENOSPC etc).
#6 lore raw
Quoted 7 lines · Johannes Sixt

David Aguilar schrieb:

You should not include "random" system headers, nor has mkstemps any business deciding whether files are opened in binary mode. We are not using O_BINARY anywhere else (except in compat/mingw.c). With the patch below squashed in (I hope it won't be wrapped) you can add:

Quoted 5 lines

to both your patches. And, yes, I like them :-)

-- Hannes

Thanks.
I'll squash these in and resend 1/2 in a moment.

Signature · 1 line
	David