Subject: [PATCH v2] precompompose_utf8.c: iconv_open_or_die

From: Torsten Bögershausen <[email-masked]>
Date: Wed, 22 Aug 2012 22:00:07 +0200

Add the function iconv_open_or_die(), which is used in both
readdir() and precompose_argv()

Optimized away one save/restore of errnor, which is no longer nedded

Signed-off-by: Torsten Bögershausen <[email-masked]>
---
 compat/precompose_utf8.c | 59 ++++++++++++++++++++++--------------------------
 1 file changed, 27 insertions(+), 32 deletions(-)

diff --git a/compat/precompose_utf8.c b/compat/precompose_utf8.c
index e6c6bf2..d8b189d 100644
--- a/compat/precompose_utf8.c
+++ b/compat/precompose_utf8.c
@@ -13,6 +13,15 @@ typedef char *iconv_ibp;
 static const char *repo_encoding = "UTF-8";
 static const char *path_encoding = "UTF-8-MAC";
 
+static iconv_t iconv_open_or_die(const char* tocode, const char* fromcode)
+{
+	iconv_t my_iconv;
+	my_iconv = iconv_open(tocode, fromcode);
+	if (my_iconv == (iconv_t) -1)
+		die_errno(_("iconv_open(%s,%s) failed"), repo_encoding, path_encoding);
+	return my_iconv;
+}
+
 static size_t has_utf8(const char *s, size_t maxlen, size_t *strlen_c)
 {
 	const uint8_t *utf8p = (const uint8_t *)s;
@@ -78,9 +87,7 @@ void precompose_argv(int argc, const char **argv)
 	if (argc <= i)
 		return; /* no utf8 found */
 
-	ic_precompose = iconv_open(repo_encoding, path_encoding);
-	if (ic_precompose == (iconv_t) -1)
-		return;
+	ic_precompose = iconv_open_or_die(repo_encoding, path_encoding);
 
 	for (i = 0; i < argc; i++) {
 		size_t namelen;
@@ -106,11 +113,8 @@ PREC_DIR *precompose_utf8_opendir(const char *dirname)
 		free(prec_dir->dirent_nfc);
 		free(prec_dir);
 		return NULL;
-	} else {
-		int ret_errno = errno;
+	} else
 		prec_dir->ic_precompose = (iconv_t)-1;
-		errno = ret_errno;
-	}
 
 	return prec_dir;
 }
@@ -137,32 +141,23 @@ struct dirent_prec_psx *precompose_utf8_readdir(PREC_DIR *prec_dir)
 		prec_dir->dirent_nfc->d_type = res->d_type;
 
 		if ((precomposed_unicode == 1) && has_utf8(res->d_name, (size_t)-1, NULL)) {
+			iconv_ibp cp = (iconv_ibp)res->d_name;
+			size_t inleft = namelenz;
+			char *outpos = &prec_dir->dirent_nfc->d_name[0];
+			size_t outsz = prec_dir->dirent_nfc->max_name_len;
+			size_t cnt;
+			errno = 0;
 			if (prec_dir->ic_precompose == (iconv_t)-1)
-				prec_dir->ic_precompose =
-					iconv_open(repo_encoding, path_encoding);
-			if (prec_dir->ic_precompose == (iconv_t)-1) {
-				die("iconv_open(%s,%s) failed, but needed:\n"
-						"    precomposed unicode is not supported.\n"
-						"    If you wnat to use decomposed unicode, run\n"
-						"    \"git config core.precomposeunicode false\"\n",
-						repo_encoding, path_encoding);
-			} else {
-				iconv_ibp	cp = (iconv_ibp)res->d_name;
-				size_t inleft = namelenz;
-				char *outpos = &prec_dir->dirent_nfc->d_name[0];
-				size_t outsz = prec_dir->dirent_nfc->max_name_len;
-				size_t cnt;
-				errno = 0;
-				cnt = iconv(prec_dir->ic_precompose, &cp, &inleft, &outpos, &outsz);
-				if (errno || inleft) {
-					/*
-					 * iconv() failed and errno could be E2BIG, EILSEQ, EINVAL, EBADF
-					 * MacOS X avoids illegal byte sequemces.
-					 * If they occur on a mounted drive (e.g. NFS) it is not worth to
-					 * die() for that, but rather let the user see the original name
-					*/
-					namelenz = 0; /* trigger strlcpy */
-				}
+				prec_dir->ic_precompose =	iconv_open_or_die(repo_encoding, path_encoding);
+			cnt = iconv(prec_dir->ic_precompose, &cp, &inleft, &outpos, &outsz);
+			if (errno || inleft) {
+				/*
+				 * iconv() failed and errno could be E2BIG, EILSEQ, EINVAL, EBADF
+				 * MacOS X avoids illegal byte sequemces.
+				 * If they occur on a mounted drive (e.g. NFS) it is not worth to
+				 * die() for that, but rather let the user see the original name
+				 */
+				namelenz = 0; /* trigger strlcpy */
 			}
 		} else
 			namelenz = 0;
-- 
1.7.12