2 * Converts filenames from decomposed unicode into precomposed unicode.
7 #define PRECOMPOSE_UNICODE_C
11 #include "precompose_utf8.h"
13 typedef char *iconv_ibp;
14 const static char *repo_encoding = "UTF-8";
15 const static char *path_encoding = "UTF-8-MAC";
18 static size_t has_utf8(const char *s, size_t maxlen, size_t *strlen_c)
20 const uint8_t *utf8p = (const uint8_t*) s;
21 size_t strlen_chars = 0;
24 if ((!utf8p) || (!*utf8p)) {
28 while((*utf8p) && maxlen) {
36 *strlen_c = strlen_chars;
42 void probe_utf8_pathname_composition(char *path, int len)
44 const static char *auml_nfc = "\xc3\xa4";
45 const static char *auml_nfd = "\x61\xcc\x88";
47 if (precomposed_unicode != -1)
48 return; /* We found it defined in the global config, respect it */
50 strcpy(path + len, auml_nfc);
51 output_fd = open(path, O_CREAT|O_EXCL|O_RDWR, 0600);
55 strcpy(path + len, auml_nfd);
56 /* Indicate to the user, that we can configure it to true */
57 if (0 == access(path, R_OK))
58 git_config_set("core.precomposeunicode", "false");
59 /* To be backward compatible, set precomposed_unicode to 0 */
60 precomposed_unicode = 0;
62 strcpy(path + len, auml_nfc);
68 void precompose_argv(int argc, const char **argv)
73 iconv_t ic_precompose;
75 if (precomposed_unicode != 1)
78 ic_precompose = iconv_open(repo_encoding, path_encoding);
79 if (ic_precompose == (iconv_t) -1)
85 if (has_utf8(oldarg, (size_t)-1, &namelen)) {
86 newarg = reencode_string_iconv(oldarg, namelen, ic_precompose);
92 iconv_close(ic_precompose);
96 PREC_DIR *precompose_utf8_opendir(const char *dirname)
98 PREC_DIR *prec_dir = xmalloc(sizeof(PREC_DIR));
99 prec_dir->dirent_nfc = xmalloc(sizeof(dirent_prec_psx));
100 prec_dir->dirent_nfc->max_name_len = sizeof(prec_dir->dirent_nfc->d_name);
102 prec_dir->dirp = opendir(dirname);
103 if (!prec_dir->dirp) {
104 free(prec_dir->dirent_nfc);
108 int ret_errno = errno;
109 prec_dir->ic_precompose = iconv_open(repo_encoding, path_encoding);
110 /* if iconv_open() fails, die() in readdir() if needed */
117 struct dirent_prec_psx *precompose_utf8_readdir(PREC_DIR *prec_dir)
120 res = readdir(prec_dir->dirp);
122 size_t namelenz = strlen(res->d_name) + 1; /* \0 */
123 size_t new_maxlen = namelenz;
125 int ret_errno = errno;
127 if (new_maxlen > prec_dir->dirent_nfc->max_name_len) {
128 size_t new_len = sizeof(dirent_prec_psx) + new_maxlen -
129 sizeof(prec_dir->dirent_nfc->d_name);
131 prec_dir->dirent_nfc = xrealloc(prec_dir->dirent_nfc, new_len);
132 prec_dir->dirent_nfc->max_name_len = new_maxlen;
135 prec_dir->dirent_nfc->d_ino = res->d_ino;
136 prec_dir->dirent_nfc->d_type = res->d_type;
138 if ((precomposed_unicode == 1) && has_utf8(res->d_name, (size_t)-1, NULL)) {
139 if (prec_dir->ic_precompose == (iconv_t)-1) {
140 die("iconv_open(%s,%s) failed, but needed:\n"
141 " precomposed unicode is not supported.\n"
142 " If you wnat to use decomposed unicode, run\n"
143 " \"git config core.precomposeunicode false\"\n",
144 repo_encoding, path_encoding);
146 iconv_ibp cp = (iconv_ibp)res->d_name;
147 size_t inleft = namelenz;
148 char *outpos = &prec_dir->dirent_nfc->d_name[0];
149 size_t outsz = prec_dir->dirent_nfc->max_name_len;
152 cnt = iconv(prec_dir->ic_precompose, &cp, &inleft, &outpos, &outsz);
153 if (errno || inleft) {
155 * iconv() failed and errno could be E2BIG, EILSEQ, EINVAL, EBADF
156 * MacOS X avoids illegal byte sequemces.
157 * If they occur on a mounted drive (e.g. NFS) it is not worth to
158 * die() for that, but rather let the user see the original name
160 namelenz = 0; /* trigger strlcpy */
168 strlcpy(prec_dir->dirent_nfc->d_name, res->d_name,
169 prec_dir->dirent_nfc->max_name_len);
172 return prec_dir->dirent_nfc;
178 int precompose_utf8_closedir(PREC_DIR *prec_dir)
182 ret_value = closedir(prec_dir->dirp);
184 if (prec_dir->ic_precompose != (iconv_t)-1)
185 iconv_close(prec_dir->ic_precompose);
186 free(prec_dir->dirent_nfc);