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