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