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