Merge branch 'ar/submodule-add-config'
[git/debian.git] / compat / precompose_utf8.c
blobcce1d57a46471c9465d6a55c176a063fdfef96eb
1 /*
2 * Converts filenames from decomposed unicode into precomposed unicode.
3 * Used on MacOS X.
4 */
6 #define PRECOMPOSE_UNICODE_C
8 #include "cache.h"
9 #include "config.h"
10 #include "utf8.h"
11 #include "precompose_utf8.h"
13 typedef char *iconv_ibp;
14 static const char *repo_encoding = "UTF-8";
15 static const char *path_encoding = "UTF-8-MAC";
17 static size_t has_non_ascii(const char *s, size_t maxlen, size_t *strlen_c)
19 const uint8_t *ptr = (const uint8_t *)s;
20 size_t strlen_chars = 0;
21 size_t ret = 0;
23 if (!ptr || !*ptr)
24 return 0;
26 while (*ptr && maxlen) {
27 if (*ptr & 0x80)
28 ret++;
29 strlen_chars++;
30 ptr++;
31 maxlen--;
33 if (strlen_c)
34 *strlen_c = strlen_chars;
36 return ret;
40 void probe_utf8_pathname_composition(void)
42 struct strbuf path = STRBUF_INIT;
43 static const char *auml_nfc = "\xc3\xa4";
44 static const char *auml_nfd = "\x61\xcc\x88";
45 int output_fd;
46 if (precomposed_unicode != -1)
47 return; /* We found it defined in the global config, respect it */
48 git_path_buf(&path, "%s", auml_nfc);
49 output_fd = open(path.buf, O_CREAT|O_EXCL|O_RDWR, 0600);
50 if (output_fd >= 0) {
51 close(output_fd);
52 git_path_buf(&path, "%s", auml_nfd);
53 precomposed_unicode = access(path.buf, R_OK) ? 0 : 1;
54 git_config_set("core.precomposeunicode",
55 precomposed_unicode ? "true" : "false");
56 git_path_buf(&path, "%s", auml_nfc);
57 if (unlink(path.buf))
58 die_errno(_("failed to unlink '%s'"), path.buf);
60 strbuf_release(&path);
63 const char *precompose_string_if_needed(const char *in)
65 size_t inlen;
66 size_t outlen;
67 if (!in)
68 return NULL;
69 if (has_non_ascii(in, (size_t)-1, &inlen)) {
70 iconv_t ic_prec;
71 char *out;
72 if (precomposed_unicode < 0)
73 git_config_get_bool("core.precomposeunicode", &precomposed_unicode);
74 if (precomposed_unicode != 1)
75 return in;
76 ic_prec = iconv_open(repo_encoding, path_encoding);
77 if (ic_prec == (iconv_t) -1)
78 return in;
80 out = reencode_string_iconv(in, inlen, ic_prec, 0, &outlen);
81 if (out) {
82 if (outlen == inlen && !memcmp(in, out, outlen))
83 free(out); /* no need to return indentical */
84 else
85 in = out;
87 iconv_close(ic_prec);
90 return in;
93 const char *precompose_argv_prefix(int argc, const char **argv, const char *prefix)
95 int i = 0;
97 while (i < argc) {
98 argv[i] = precompose_string_if_needed(argv[i]);
99 i++;
101 return precompose_string_if_needed(prefix);
105 PREC_DIR *precompose_utf8_opendir(const char *dirname)
107 PREC_DIR *prec_dir = xmalloc(sizeof(PREC_DIR));
108 prec_dir->dirent_nfc = xmalloc(sizeof(dirent_prec_psx));
109 prec_dir->dirent_nfc->max_name_len = sizeof(prec_dir->dirent_nfc->d_name);
111 prec_dir->dirp = opendir(dirname);
112 if (!prec_dir->dirp) {
113 free(prec_dir->dirent_nfc);
114 free(prec_dir);
115 return NULL;
116 } else {
117 int ret_errno = errno;
118 prec_dir->ic_precompose = iconv_open(repo_encoding, path_encoding);
119 /* if iconv_open() fails, die() in readdir() if needed */
120 errno = ret_errno;
123 return prec_dir;
126 struct dirent_prec_psx *precompose_utf8_readdir(PREC_DIR *prec_dir)
128 struct dirent *res;
129 res = readdir(prec_dir->dirp);
130 if (res) {
131 size_t namelenz = strlen(res->d_name) + 1; /* \0 */
132 size_t new_maxlen = namelenz;
134 int ret_errno = errno;
136 if (new_maxlen > prec_dir->dirent_nfc->max_name_len) {
137 size_t new_len = sizeof(dirent_prec_psx) + new_maxlen -
138 sizeof(prec_dir->dirent_nfc->d_name);
140 prec_dir->dirent_nfc = xrealloc(prec_dir->dirent_nfc, new_len);
141 prec_dir->dirent_nfc->max_name_len = new_maxlen;
144 prec_dir->dirent_nfc->d_ino = res->d_ino;
145 prec_dir->dirent_nfc->d_type = res->d_type;
147 if ((precomposed_unicode == 1) && has_non_ascii(res->d_name, (size_t)-1, NULL)) {
148 if (prec_dir->ic_precompose == (iconv_t)-1) {
149 die("iconv_open(%s,%s) failed, but needed:\n"
150 " precomposed unicode is not supported.\n"
151 " If you want to use decomposed unicode, run\n"
152 " \"git config core.precomposeunicode false\"\n",
153 repo_encoding, path_encoding);
154 } else {
155 iconv_ibp cp = (iconv_ibp)res->d_name;
156 size_t inleft = namelenz;
157 char *outpos = &prec_dir->dirent_nfc->d_name[0];
158 size_t outsz = prec_dir->dirent_nfc->max_name_len;
159 errno = 0;
160 iconv(prec_dir->ic_precompose, &cp, &inleft, &outpos, &outsz);
161 if (errno || inleft) {
163 * iconv() failed and errno could be E2BIG, EILSEQ, EINVAL, EBADF
164 * MacOS X avoids illegal byte sequences.
165 * If they occur on a mounted drive (e.g. NFS) it is not worth to
166 * die() for that, but rather let the user see the original name
168 namelenz = 0; /* trigger strlcpy */
171 } else
172 namelenz = 0;
174 if (!namelenz)
175 strlcpy(prec_dir->dirent_nfc->d_name, res->d_name,
176 prec_dir->dirent_nfc->max_name_len);
178 errno = ret_errno;
179 return prec_dir->dirent_nfc;
181 return NULL;
185 int precompose_utf8_closedir(PREC_DIR *prec_dir)
187 int ret_value;
188 int ret_errno;
189 ret_value = closedir(prec_dir->dirp);
190 ret_errno = errno;
191 if (prec_dir->ic_precompose != (iconv_t)-1)
192 iconv_close(prec_dir->ic_precompose);
193 free(prec_dir->dirent_nfc);
194 free(prec_dir);
195 errno = ret_errno;
196 return ret_value;