ident: don't consider '.' a crud
[git/gitster.git] / compat / precompose_utf8.c
bloba4d11376ba5126e3c3c6d409748a106824d089ee
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 "path.h"
13 #include "utf8.h"
14 #include "precompose_utf8.h"
16 typedef char *iconv_ibp;
17 static const char *repo_encoding = "UTF-8";
18 static const char *path_encoding = "UTF-8-MAC";
20 static size_t has_non_ascii(const char *s, size_t maxlen, size_t *strlen_c)
22 const uint8_t *ptr = (const uint8_t *)s;
23 size_t strlen_chars = 0;
24 size_t ret = 0;
26 if (!ptr || !*ptr)
27 return 0;
29 while (*ptr && maxlen) {
30 if (*ptr & 0x80)
31 ret++;
32 strlen_chars++;
33 ptr++;
34 maxlen--;
36 if (strlen_c)
37 *strlen_c = strlen_chars;
39 return ret;
43 void probe_utf8_pathname_composition(void)
45 struct strbuf path = STRBUF_INIT;
46 static const char *auml_nfc = "\xc3\xa4";
47 static const char *auml_nfd = "\x61\xcc\x88";
48 int output_fd;
49 if (precomposed_unicode != -1)
50 return; /* We found it defined in the global config, respect it */
51 git_path_buf(&path, "%s", auml_nfc);
52 output_fd = open(path.buf, O_CREAT|O_EXCL|O_RDWR, 0600);
53 if (output_fd >= 0) {
54 close(output_fd);
55 git_path_buf(&path, "%s", auml_nfd);
56 precomposed_unicode = access(path.buf, R_OK) ? 0 : 1;
57 git_config_set("core.precomposeunicode",
58 precomposed_unicode ? "true" : "false");
59 git_path_buf(&path, "%s", auml_nfc);
60 if (unlink(path.buf))
61 die_errno(_("failed to unlink '%s'"), path.buf);
63 strbuf_release(&path);
66 const char *precompose_string_if_needed(const char *in)
68 size_t inlen;
69 size_t outlen;
70 if (!in)
71 return NULL;
72 if (has_non_ascii(in, (size_t)-1, &inlen)) {
73 iconv_t ic_prec;
74 char *out;
75 if (precomposed_unicode < 0)
76 git_config_get_bool("core.precomposeunicode", &precomposed_unicode);
77 if (precomposed_unicode != 1)
78 return in;
79 ic_prec = iconv_open(repo_encoding, path_encoding);
80 if (ic_prec == (iconv_t) -1)
81 return in;
83 out = reencode_string_iconv(in, inlen, ic_prec, 0, &outlen);
84 if (out) {
85 if (outlen == inlen && !memcmp(in, out, outlen))
86 free(out); /* no need to return indentical */
87 else
88 in = out;
90 iconv_close(ic_prec);
93 return in;
96 const char *precompose_argv_prefix(int argc, const char **argv, const char *prefix)
98 int i = 0;
100 while (i < argc) {
101 argv[i] = precompose_string_if_needed(argv[i]);
102 i++;
104 return precompose_string_if_needed(prefix);
108 PREC_DIR *precompose_utf8_opendir(const char *dirname)
110 PREC_DIR *prec_dir = xmalloc(sizeof(PREC_DIR));
111 prec_dir->dirent_nfc = xmalloc(sizeof(dirent_prec_psx));
112 prec_dir->dirent_nfc->max_name_len = sizeof(prec_dir->dirent_nfc->d_name);
114 prec_dir->dirp = opendir(dirname);
115 if (!prec_dir->dirp) {
116 free(prec_dir->dirent_nfc);
117 free(prec_dir);
118 return NULL;
119 } else {
120 int ret_errno = errno;
121 prec_dir->ic_precompose = iconv_open(repo_encoding, path_encoding);
122 /* if iconv_open() fails, die() in readdir() if needed */
123 errno = ret_errno;
126 return prec_dir;
129 struct dirent_prec_psx *precompose_utf8_readdir(PREC_DIR *prec_dir)
131 struct dirent *res;
132 res = readdir(prec_dir->dirp);
133 if (res) {
134 size_t namelenz = strlen(res->d_name) + 1; /* \0 */
135 size_t new_maxlen = namelenz;
137 int ret_errno = errno;
139 if (new_maxlen > prec_dir->dirent_nfc->max_name_len) {
140 size_t new_len = sizeof(dirent_prec_psx) + new_maxlen -
141 sizeof(prec_dir->dirent_nfc->d_name);
143 prec_dir->dirent_nfc = xrealloc(prec_dir->dirent_nfc, new_len);
144 prec_dir->dirent_nfc->max_name_len = new_maxlen;
147 prec_dir->dirent_nfc->d_ino = res->d_ino;
148 prec_dir->dirent_nfc->d_type = res->d_type;
150 if ((precomposed_unicode == 1) && has_non_ascii(res->d_name, (size_t)-1, NULL)) {
151 if (prec_dir->ic_precompose == (iconv_t)-1) {
152 die("iconv_open(%s,%s) failed, but needed:\n"
153 " precomposed unicode is not supported.\n"
154 " If you want to use decomposed unicode, run\n"
155 " \"git config core.precomposeunicode false\"\n",
156 repo_encoding, path_encoding);
157 } else {
158 iconv_ibp cp = (iconv_ibp)res->d_name;
159 size_t inleft = namelenz;
160 char *outpos = &prec_dir->dirent_nfc->d_name[0];
161 size_t outsz = prec_dir->dirent_nfc->max_name_len;
162 errno = 0;
163 iconv(prec_dir->ic_precompose, &cp, &inleft, &outpos, &outsz);
164 if (errno || inleft) {
166 * iconv() failed and errno could be E2BIG, EILSEQ, EINVAL, EBADF
167 * MacOS X avoids illegal byte sequences.
168 * If they occur on a mounted drive (e.g. NFS) it is not worth to
169 * die() for that, but rather let the user see the original name
171 namelenz = 0; /* trigger strlcpy */
174 } else
175 namelenz = 0;
177 if (!namelenz)
178 strlcpy(prec_dir->dirent_nfc->d_name, res->d_name,
179 prec_dir->dirent_nfc->max_name_len);
181 errno = ret_errno;
182 return prec_dir->dirent_nfc;
184 return NULL;
188 int precompose_utf8_closedir(PREC_DIR *prec_dir)
190 int ret_value;
191 int ret_errno;
192 ret_value = closedir(prec_dir->dirp);
193 ret_errno = errno;
194 if (prec_dir->ic_precompose != (iconv_t)-1)
195 iconv_close(prec_dir->ic_precompose);
196 free(prec_dir->dirent_nfc);
197 free(prec_dir);
198 errno = ret_errno;
199 return ret_value;