{"thread":{"id":"31310","subject":"[PATCH] precompompose_utf8.c: iconv_open_or_die","startedAt":"2012-08-21T19:20:55Z","lastAt":"2012-08-22T06:13:46Z","messageCount":2,"participants":["Torsten Bögershausen","Nguyen Thai Ngoc Duy"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"197550","messageId":"201208212120.56444.tboegi@web.de","threadId":"31310","inReplyTo":null,"subject":"[PATCH] precompompose_utf8.c: iconv_open_or_die","fromName":"Torsten Bögershausen","fromEmail":"tboegi@web.de","sentAt":"2012-08-21T19:20:55Z","receivedAt":"2012-08-21T19:20:55Z","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 errno, which is no longer nedded\n\nSigned-off-by: Torsten Bögershausen <tboegi@web.de>\n---\n\n(Needs to be applied to pu)\n\n compat/precompose_utf8.c | 63 ++++++++++++++++++++++++------------------------\n 1 file changed, 31 insertions(+), 32 deletions(-)\n\ndiff --git a/compat/precompose_utf8.c b/compat/precompose_utf8.c\nindex e6c6bf2..95ab9a1 100644\n--- a/compat/precompose_utf8.c\n+++ b/compat/precompose_utf8.c\n@@ -13,6 +13,19 @@ 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(_(\"iconv_open(%s,%s) failed, but needed:\\n\"\n+\t\t\t\t\t\"    core.precomposeunicode not supported.\\n\"),\n+\t\t\t\trepo_encoding, path_encoding);\n+\t}\n+\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 +91,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 +117,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 +145,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"},{"id":"197572","messageId":"CACsJy8AqtsUJT7ukfn4PpT+Huy-SHPwiBNyO1r=3RFHKUjuTfA@mail.gmail.com","threadId":"31310","inReplyTo":"201208212120.56444.tboegi@web.de","subject":"Re: [PATCH] precompompose_utf8.c: iconv_open_or_die","fromName":"Nguyen Thai Ngoc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2012-08-22T06:13:46Z","receivedAt":"2012-08-22T06:13:46Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Wed, Aug 22, 2012 at 2:20 AM, Torsten Bögershausen <tboegi@web.de> wrote:\n> +static iconv_t iconv_open_or_die(const char* tocode, const char* fromcode)\n> +{\n> +       iconv_t my_iconv;\n> +       my_iconv = iconv_open(tocode, fromcode);\n> +       if (my_iconv == (iconv_t) -1) {\n> +               die(_(\"iconv_open(%s,%s) failed, but needed:\\n\"\n> +                                       \"    core.precomposeunicode not supported.\\n\"),\n> +                               repo_encoding, path_encoding);\n\nYou might want to use die_errno.\n\n> +       }\n> +\n> +       return my_iconv;\n> +}\n-- \nDuy\n"}]}