notes doc: split up run-on sentences
[git/gitster.git] / compat / precompose_utf8.c
blob8a9881db077320c292714595714f1604462c1504
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 "environment.h"
11 #include "gettext.h"
12 #include "utf8.h"
13 #include "precompose_utf8.h"
15 typedef char *iconv_ibp;
16 static const char *repo_encoding = "UTF-8";
17 static const char *path_encoding = "UTF-8-MAC";
19 static size_t has_non_ascii(const char *s, size_t maxlen, size_t *strlen_c)
21 const uint8_t *ptr = (const uint8_t *)s;
22 size_t strlen_chars = 0;
23 size_t ret = 0;
25 if (!ptr || !*ptr)
26 return 0;
28 while (*ptr && maxlen) {
29 if (*ptr & 0x80)
30 ret++;
31 strlen_chars++;
32 ptr++;
33 maxlen--;
35 if (strlen_c)
36 *strlen_c = strlen_chars;
38 return ret;
42 void probe_utf8_pathname_composition(void)
44 struct strbuf path = STRBUF_INIT;
45 static const char *auml_nfc = "\xc3\xa4";
46 static const char *auml_nfd = "\x61\xcc\x88";
47 int output_fd;
48 if (precomposed_unicode != -1)
49 return; /* We found it defined in the global config, respect it */
50 git_path_buf(&path, "%s", auml_nfc);
51 output_fd = open(path.buf, O_CREAT|O_EXCL|O_RDWR, 0600);
52 if (output_fd >= 0) {
53 close(output_fd);
54 git_path_buf(&path, "%s", auml_nfd);
55 precomposed_unicode = access(path.buf, R_OK) ? 0 : 1;
56 git_config_set("core.precomposeunicode",
57 precomposed_unicode ? "true" : "false");
58 git_path_buf(&path, "%s", auml_nfc);
59 if (unlink(path.buf))
60 die_errno(_("failed to unlink '%s'"), path.buf);
62 strbuf_release(&path);
65 const char *precompose_string_if_needed(const char *in)
67 size_t inlen;
68 size_t outlen;
69 if (!in)
70 return NULL;
71 if (has_non_ascii(in, (size_t)-1, &inlen)) {
72 iconv_t ic_prec;
73 char *out;
74 if (precomposed_unicode < 0)
75 git_config_get_bool("core.precomposeunicode", &precomposed_unicode);
76 if (precomposed_unicode != 1)
77 return in;
78 ic_prec = iconv_open(repo_encoding, path_encoding);
79 if (ic_prec == (iconv_t) -1)
80 return in;
82 out = reencode_string_iconv(in, inlen, ic_prec, 0, &outlen);
83 if (out) {
84 if (outlen == inlen && !memcmp(in, out, outlen))
85 free(out); /* no need to return indentical */
86 else
87 in = out;
89 iconv_close(ic_prec);
92 return in;
95 const char *precompose_argv_prefix(int argc, const char **argv, const char *prefix)
97 int i = 0;
99 while (i < argc) {
100 argv[i] = precompose_string_if_needed(argv[i]);
101 i++;
103 return precompose_string_if_needed(prefix);
107 PREC_DIR *precompose_utf8_opendir(const char *dirname)
109 PREC_DIR *prec_dir = xmalloc(sizeof(PREC_DIR));
110 prec_dir->dirent_nfc = xmalloc(sizeof(dirent_prec_psx));
111 prec_dir->dirent_nfc->max_name_len = sizeof(prec_dir->dirent_nfc->d_name);
113 prec_dir->dirp = opendir(dirname);
114 if (!prec_dir->dirp) {
115 free(prec_dir->dirent_nfc);
116 free(prec_dir);
117 return NULL;
118 } else {
119 int ret_errno = errno;
120 prec_dir->ic_precompose = iconv_open(repo_encoding, path_encoding);
121 /* if iconv_open() fails, die() in readdir() if needed */
122 errno = ret_errno;
125 return prec_dir;
128 struct dirent_prec_psx *precompose_utf8_readdir(PREC_DIR *prec_dir)
130 struct dirent *res;
131 res = readdir(prec_dir->dirp);
132 if (res) {
133 size_t namelenz = strlen(res->d_name) + 1; /* \0 */
134 size_t new_maxlen = namelenz;
136 int ret_errno = errno;
138 if (new_maxlen > prec_dir->dirent_nfc->max_name_len) {
139 size_t new_len = sizeof(dirent_prec_psx) + new_maxlen -
140 sizeof(prec_dir->dirent_nfc->d_name);
142 prec_dir->dirent_nfc = xrealloc(prec_dir->dirent_nfc, new_len);
143 prec_dir->dirent_nfc->max_name_len = new_maxlen;
146 prec_dir->dirent_nfc->d_ino = res->d_ino;
147 prec_dir->dirent_nfc->d_type = res->d_type;
149 if ((precomposed_unicode == 1) && has_non_ascii(res->d_name, (size_t)-1, NULL)) {
150 if (prec_dir->ic_precompose == (iconv_t)-1) {
151 die("iconv_open(%s,%s) failed, but needed:\n"
152 " precomposed unicode is not supported.\n"
153 " If you want to use decomposed unicode, run\n"
154 " \"git config core.precomposeunicode false\"\n",
155 repo_encoding, path_encoding);
156 } else {
157 iconv_ibp cp = (iconv_ibp)res->d_name;
158 size_t inleft = namelenz;
159 char *outpos = &prec_dir->dirent_nfc->d_name[0];
160 size_t outsz = prec_dir->dirent_nfc->max_name_len;
161 errno = 0;
162 iconv(prec_dir->ic_precompose, &cp, &inleft, &outpos, &outsz);
163 if (errno || inleft) {
165 * iconv() failed and errno could be E2BIG, EILSEQ, EINVAL, EBADF
166 * MacOS X avoids illegal byte sequences.
167 * If they occur on a mounted drive (e.g. NFS) it is not worth to
168 * die() for that, but rather let the user see the original name
170 namelenz = 0; /* trigger strlcpy */
173 } else
174 namelenz = 0;
176 if (!namelenz)
177 strlcpy(prec_dir->dirent_nfc->d_name, res->d_name,
178 prec_dir->dirent_nfc->max_name_len);
180 errno = ret_errno;
181 return prec_dir->dirent_nfc;
183 return NULL;
187 int precompose_utf8_closedir(PREC_DIR *prec_dir)
189 int ret_value;
190 int ret_errno;
191 ret_value = closedir(prec_dir->dirp);
192 ret_errno = errno;
193 if (prec_dir->ic_precompose != (iconv_t)-1)
194 iconv_close(prec_dir->ic_precompose);
195 free(prec_dir->dirent_nfc);
196 free(prec_dir);
197 errno = ret_errno;
198 return ret_value;