]> git.ipfire.org Git - thirdparty/git.git/blob - compat/precompose_utf8.c
MacOS: precompose_argv_prefix()
[thirdparty/git.git] / compat / precompose_utf8.c
1 /*
2 * Converts filenames from decomposed unicode into precomposed unicode.
3 * Used on MacOS X.
4 */
5
6 #define PRECOMPOSE_UNICODE_C
7
8 #include "cache.h"
9 #include "config.h"
10 #include "utf8.h"
11 #include "precompose_utf8.h"
12
13 typedef char *iconv_ibp;
14 static const char *repo_encoding = "UTF-8";
15 static const char *path_encoding = "UTF-8-MAC";
16
17 static size_t has_non_ascii(const char *s, size_t maxlen, size_t *strlen_c)
18 {
19 const uint8_t *ptr = (const uint8_t *)s;
20 size_t strlen_chars = 0;
21 size_t ret = 0;
22
23 if (!ptr || !*ptr)
24 return 0;
25
26 while (*ptr && maxlen) {
27 if (*ptr & 0x80)
28 ret++;
29 strlen_chars++;
30 ptr++;
31 maxlen--;
32 }
33 if (strlen_c)
34 *strlen_c = strlen_chars;
35
36 return ret;
37 }
38
39
40 void probe_utf8_pathname_composition(void)
41 {
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);
59 }
60 strbuf_release(&path);
61 }
62
63 static inline const char *precompose_string_if_needed(const char *in)
64 {
65 size_t inlen;
66 size_t outlen;
67 if (has_non_ascii(in, (size_t)-1, &inlen)) {
68 iconv_t ic_prec;
69 char *out;
70 if (precomposed_unicode < 0)
71 git_config_get_bool("core.precomposeunicode", &precomposed_unicode);
72 if (precomposed_unicode != 1)
73 return in;
74 ic_prec = iconv_open(repo_encoding, path_encoding);
75 if (ic_prec == (iconv_t) -1)
76 return in;
77
78 out = reencode_string_iconv(in, inlen, ic_prec, 0, &outlen);
79 if (out) {
80 if (outlen == inlen && !memcmp(in, out, outlen))
81 free(out); /* no need to return indentical */
82 else
83 in = out;
84 }
85 iconv_close(ic_prec);
86
87 }
88 return in;
89 }
90
91 const char *precompose_argv_prefix(int argc, const char **argv, const char *prefix)
92 {
93 int i = 0;
94
95 while (i < argc) {
96 argv[i] = precompose_string_if_needed(argv[i]);
97 i++;
98 }
99 if (prefix) {
100 prefix = precompose_string_if_needed(prefix);
101 }
102 return prefix;
103 }
104
105
106 PREC_DIR *precompose_utf8_opendir(const char *dirname)
107 {
108 PREC_DIR *prec_dir = xmalloc(sizeof(PREC_DIR));
109 prec_dir->dirent_nfc = xmalloc(sizeof(dirent_prec_psx));
110 prec_dir->dirent_nfc->max_name_len = sizeof(prec_dir->dirent_nfc->d_name);
111
112 prec_dir->dirp = opendir(dirname);
113 if (!prec_dir->dirp) {
114 free(prec_dir->dirent_nfc);
115 free(prec_dir);
116 return NULL;
117 } else {
118 int ret_errno = errno;
119 prec_dir->ic_precompose = iconv_open(repo_encoding, path_encoding);
120 /* if iconv_open() fails, die() in readdir() if needed */
121 errno = ret_errno;
122 }
123
124 return prec_dir;
125 }
126
127 struct dirent_prec_psx *precompose_utf8_readdir(PREC_DIR *prec_dir)
128 {
129 struct dirent *res;
130 res = readdir(prec_dir->dirp);
131 if (res) {
132 size_t namelenz = strlen(res->d_name) + 1; /* \0 */
133 size_t new_maxlen = namelenz;
134
135 int ret_errno = errno;
136
137 if (new_maxlen > prec_dir->dirent_nfc->max_name_len) {
138 size_t new_len = sizeof(dirent_prec_psx) + new_maxlen -
139 sizeof(prec_dir->dirent_nfc->d_name);
140
141 prec_dir->dirent_nfc = xrealloc(prec_dir->dirent_nfc, new_len);
142 prec_dir->dirent_nfc->max_name_len = new_maxlen;
143 }
144
145 prec_dir->dirent_nfc->d_ino = res->d_ino;
146 prec_dir->dirent_nfc->d_type = res->d_type;
147
148 if ((precomposed_unicode == 1) && has_non_ascii(res->d_name, (size_t)-1, NULL)) {
149 if (prec_dir->ic_precompose == (iconv_t)-1) {
150 die("iconv_open(%s,%s) failed, but needed:\n"
151 " precomposed unicode is not supported.\n"
152 " If you want to use decomposed unicode, run\n"
153 " \"git config core.precomposeunicode false\"\n",
154 repo_encoding, path_encoding);
155 } else {
156 iconv_ibp cp = (iconv_ibp)res->d_name;
157 size_t inleft = namelenz;
158 char *outpos = &prec_dir->dirent_nfc->d_name[0];
159 size_t outsz = prec_dir->dirent_nfc->max_name_len;
160 errno = 0;
161 iconv(prec_dir->ic_precompose, &cp, &inleft, &outpos, &outsz);
162 if (errno || inleft) {
163 /*
164 * iconv() failed and errno could be E2BIG, EILSEQ, EINVAL, EBADF
165 * MacOS X avoids illegal byte sequences.
166 * If they occur on a mounted drive (e.g. NFS) it is not worth to
167 * die() for that, but rather let the user see the original name
168 */
169 namelenz = 0; /* trigger strlcpy */
170 }
171 }
172 } else
173 namelenz = 0;
174
175 if (!namelenz)
176 strlcpy(prec_dir->dirent_nfc->d_name, res->d_name,
177 prec_dir->dirent_nfc->max_name_len);
178
179 errno = ret_errno;
180 return prec_dir->dirent_nfc;
181 }
182 return NULL;
183 }
184
185
186 int precompose_utf8_closedir(PREC_DIR *prec_dir)
187 {
188 int ret_value;
189 int ret_errno;
190 ret_value = closedir(prec_dir->dirp);
191 ret_errno = errno;
192 if (prec_dir->ic_precompose != (iconv_t)-1)
193 iconv_close(prec_dir->ic_precompose);
194 free(prec_dir->dirent_nfc);
195 free(prec_dir);
196 errno = ret_errno;
197 return ret_value;
198 }