2 * Converts filenames from decomposed unicode into precomposed unicode.
6 #define PRECOMPOSE_UNICODE_C
11 #include "precompose_utf8.h"
13 typedef char *iconv_ibp
;
14 static const char *repo_encoding
= "UTF-8";
15 static const char *path_encoding
= "UTF-8-MAC";
17 static size_t has_non_ascii(const char *s
, size_t maxlen
, size_t *strlen_c
)
19 const uint8_t *ptr
= (const uint8_t *)s
;
20 size_t strlen_chars
= 0;
26 while (*ptr
&& maxlen
) {
34 *strlen_c
= strlen_chars
;
40 void probe_utf8_pathname_composition(void)
42 struct strbuf path
= STRBUF_INIT
;
43 static const char *auml_nfc
= "\xc3\xa4";
44 static const char *auml_nfd
= "\x61\xcc\x88";
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);
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
);
58 die_errno(_("failed to unlink '%s'"), path
.buf
);
60 strbuf_release(&path
);
63 static inline const char *precompose_string_if_needed(const char *in
)
67 if (has_non_ascii(in
, (size_t)-1, &inlen
)) {
70 if (precomposed_unicode
< 0)
71 git_config_get_bool("core.precomposeunicode", &precomposed_unicode
);
72 if (precomposed_unicode
!= 1)
74 ic_prec
= iconv_open(repo_encoding
, path_encoding
);
75 if (ic_prec
== (iconv_t
) -1)
78 out
= reencode_string_iconv(in
, inlen
, ic_prec
, 0, &outlen
);
80 if (outlen
== inlen
&& !memcmp(in
, out
, outlen
))
81 free(out
); /* no need to return indentical */
91 const char *precompose_argv_prefix(int argc
, const char **argv
, const char *prefix
)
96 argv
[i
] = precompose_string_if_needed(argv
[i
]);
100 prefix
= precompose_string_if_needed(prefix
);
106 PREC_DIR
*precompose_utf8_opendir(const char *dirname
)
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
);
112 prec_dir
->dirp
= opendir(dirname
);
113 if (!prec_dir
->dirp
) {
114 free(prec_dir
->dirent_nfc
);
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 */
127 struct dirent_prec_psx
*precompose_utf8_readdir(PREC_DIR
*prec_dir
)
130 res
= readdir(prec_dir
->dirp
);
132 size_t namelenz
= strlen(res
->d_name
) + 1; /* \0 */
133 size_t new_maxlen
= namelenz
;
135 int ret_errno
= errno
;
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
);
141 prec_dir
->dirent_nfc
= xrealloc(prec_dir
->dirent_nfc
, new_len
);
142 prec_dir
->dirent_nfc
->max_name_len
= new_maxlen
;
145 prec_dir
->dirent_nfc
->d_ino
= res
->d_ino
;
146 prec_dir
->dirent_nfc
->d_type
= res
->d_type
;
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
);
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
;
161 iconv(prec_dir
->ic_precompose
, &cp
, &inleft
, &outpos
, &outsz
);
162 if (errno
|| inleft
) {
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
169 namelenz
= 0; /* trigger strlcpy */
176 strlcpy(prec_dir
->dirent_nfc
->d_name
, res
->d_name
,
177 prec_dir
->dirent_nfc
->max_name_len
);
180 return prec_dir
->dirent_nfc
;
186 int precompose_utf8_closedir(PREC_DIR
*prec_dir
)
190 ret_value
= closedir(prec_dir
->dirp
);
192 if (prec_dir
->ic_precompose
!= (iconv_t
)-1)
193 iconv_close(prec_dir
->ic_precompose
);
194 free(prec_dir
->dirent_nfc
);