{"thread":{"id":"31322","subject":"[PATCH v2] precompompose_utf8.c: iconv_open_or_die","startedAt":"2012-08-22T20:00:07Z","lastAt":"2012-08-22T20:00:07Z","messageCount":1,"participants":["Torsten Bögershausen"],"isPatch":true,"patchVersion":2,"patchTotal":null},"messages":[{"id":"197654","messageId":"201208222200.08803.tboegi@web.de","threadId":"31322","inReplyTo":null,"subject":"[PATCH v2] precompompose_utf8.c: iconv_open_or_die","fromName":"Torsten Bögershausen","fromEmail":"tboegi@web.de","sentAt":"2012-08-22T20:00:07Z","receivedAt":"2012-08-22T20:00:07Z","isPatch":true,"sender":{"key":"tboegi@web.de","avatar":"https://avatars.githubusercontent.com/u/7138363?v=4"},"body":"Add the function iconv_open_or_die(), which is used in both\nreaddir() and precompose_argv()\n\nOptimized away one save/restore of errnor, which is no longer nedded\n\nSigned-off-by: Torsten Bögershausen <tboegi@web.de>\n---\n compat/precompose_utf8.c | 59 ++++++++++++++++++++++--------------------------\n 1 file changed, 27 insertions(+), 32 deletions(-)\n\ndiff --git a/compat/precompose_utf8.c b/compat/precompose_utf8.c\nindex e6c6bf2..d8b189d 100644\n--- a/compat/precompose_utf8.c\n+++ b/compat/precompose_utf8.c\n@@ -13,6 +13,15 @@ typedef char *iconv_ibp;\n static const char *repo_encoding = \"UTF-8\";\n static const char *path_encoding = \"UTF-8-MAC\";\n \n+static iconv_t iconv_open_or_die(const char* tocode, const char* fromcode)\n+{\n+\ticonv_t my_iconv;\n+\tmy_iconv = iconv_open(tocode, fromcode);\n+\tif (my_iconv == (iconv_t) -1)\n+\t\tdie_errno(_(\"iconv_open(%s,%s) failed\"), repo_encoding, path_encoding);\n+\treturn my_iconv;\n+}\n+\n static size_t has_utf8(const char *s, size_t maxlen, size_t *strlen_c)\n {\n \tconst uint8_t *utf8p = (const uint8_t *)s;\n@@ -78,9 +87,7 @@ void precompose_argv(int argc, const char **argv)\n \tif (argc <= i)\n \t\treturn; /* no utf8 found */\n \n-\tic_precompose = iconv_open(repo_encoding, path_encoding);\n-\tif (ic_precompose == (iconv_t) -1)\n-\t\treturn;\n+\tic_precompose = iconv_open_or_die(repo_encoding, path_encoding);\n \n \tfor (i = 0; i < argc; i++) {\n \t\tsize_t namelen;\n@@ -106,11 +113,8 @@ PREC_DIR *precompose_utf8_opendir(const char *dirname)\n \t\tfree(prec_dir->dirent_nfc);\n \t\tfree(prec_dir);\n \t\treturn NULL;\n-\t} else {\n-\t\tint ret_errno = errno;\n+\t} else\n \t\tprec_dir->ic_precompose = (iconv_t)-1;\n-\t\terrno = ret_errno;\n-\t}\n \n \treturn prec_dir;\n }\n@@ -137,32 +141,23 @@ struct dirent_prec_psx *precompose_utf8_readdir(PREC_DIR *prec_dir)\n \t\tprec_dir->dirent_nfc->d_type = res->d_type;\n \n \t\tif ((precomposed_unicode == 1) && has_utf8(res->d_name, (size_t)-1, NULL)) {\n+\t\t\ticonv_ibp cp = (iconv_ibp)res->d_name;\n+\t\t\tsize_t inleft = namelenz;\n+\t\t\tchar *outpos = &prec_dir->dirent_nfc->d_name[0];\n+\t\t\tsize_t outsz = prec_dir->dirent_nfc->max_name_len;\n+\t\t\tsize_t cnt;\n+\t\t\terrno = 0;\n \t\t\tif (prec_dir->ic_precompose == (iconv_t)-1)\n-\t\t\t\tprec_dir->ic_precompose =\n-\t\t\t\t\ticonv_open(repo_encoding, path_encoding);\n-\t\t\tif (prec_dir->ic_precompose == (iconv_t)-1) {\n-\t\t\t\tdie(\"iconv_open(%s,%s) failed, but needed:\\n\"\n-\t\t\t\t\t\t\"    precomposed unicode is not supported.\\n\"\n-\t\t\t\t\t\t\"    If you wnat to use decomposed unicode, run\\n\"\n-\t\t\t\t\t\t\"    \\\"git config core.precomposeunicode false\\\"\\n\",\n-\t\t\t\t\t\trepo_encoding, path_encoding);\n-\t\t\t} else {\n-\t\t\t\ticonv_ibp\tcp = (iconv_ibp)res->d_name;\n-\t\t\t\tsize_t inleft = namelenz;\n-\t\t\t\tchar *outpos = &prec_dir->dirent_nfc->d_name[0];\n-\t\t\t\tsize_t outsz = prec_dir->dirent_nfc->max_name_len;\n-\t\t\t\tsize_t cnt;\n-\t\t\t\terrno = 0;\n-\t\t\t\tcnt = iconv(prec_dir->ic_precompose, &cp, &inleft, &outpos, &outsz);\n-\t\t\t\tif (errno || inleft) {\n-\t\t\t\t\t/*\n-\t\t\t\t\t * iconv() failed and errno could be E2BIG, EILSEQ, EINVAL, EBADF\n-\t\t\t\t\t * MacOS X avoids illegal byte sequemces.\n-\t\t\t\t\t * If they occur on a mounted drive (e.g. NFS) it is not worth to\n-\t\t\t\t\t * die() for that, but rather let the user see the original name\n-\t\t\t\t\t*/\n-\t\t\t\t\tnamelenz = 0; /* trigger strlcpy */\n-\t\t\t\t}\n+\t\t\t\tprec_dir->ic_precompose =\ticonv_open_or_die(repo_encoding, path_encoding);\n+\t\t\tcnt = iconv(prec_dir->ic_precompose, &cp, &inleft, &outpos, &outsz);\n+\t\t\tif (errno || inleft) {\n+\t\t\t\t/*\n+\t\t\t\t * iconv() failed and errno could be E2BIG, EILSEQ, EINVAL, EBADF\n+\t\t\t\t * MacOS X avoids illegal byte sequemces.\n+\t\t\t\t * If they occur on a mounted drive (e.g. NFS) it is not worth to\n+\t\t\t\t * die() for that, but rather let the user see the original name\n+\t\t\t\t */\n+\t\t\t\tnamelenz = 0; /* trigger strlcpy */\n \t\t\t}\n \t\t} else\n \t\t\tnamelenz = 0;\n-- \n1.7.12\n"}]}