2 * Converts filenames from decomposed unicode into precomposed unicode.
6 #define PRECOMPOSE_UNICODE_C
10 #include "precompose_utf8.h"
12 typedef char *iconv_ibp
;
13 static const char *repo_encoding
= "UTF-8";
14 static const char *path_encoding
= "UTF-8-MAC";
16 static size_t has_non_ascii(const char *s
, size_t maxlen
, size_t *strlen_c
)
18 const uint8_t *ptr
= (const uint8_t *)s
;
19 size_t strlen_chars
= 0;
25 while (*ptr
&& maxlen
) {
33 *strlen_c
= strlen_chars
;
39 void probe_utf8_pathname_composition(void)
41 struct strbuf path
= STRBUF_INIT
;
42 static const char *auml_nfc
= "\xc3\xa4";
43 static const char *auml_nfd
= "\x61\xcc\x88";
45 if (precomposed_unicode
!= -1)
46 return; /* We found it defined in the global config, respect it */
47 git_path_buf(&path
, "%s", auml_nfc
);
48 output_fd
= open(path
.buf
, O_CREAT
|O_EXCL
|O_RDWR
, 0600);
51 git_path_buf(&path
, "%s", auml_nfd
);
52 precomposed_unicode
= access(path
.buf
, R_OK
) ? 0 : 1;
53 git_config_set("core.precomposeunicode",
54 precomposed_unicode
? "true" : "false");
55 git_path_buf(&path
, "%s", auml_nfc
);
57 die_errno(_("failed to unlink '%s'"), path
.buf
);
59 strbuf_release(&path
);
63 void precompose_argv(int argc
, const char **argv
)
68 iconv_t ic_precompose
;
70 if (precomposed_unicode
!= 1)
73 ic_precompose
= iconv_open(repo_encoding
, path_encoding
);
74 if (ic_precompose
== (iconv_t
) -1)
80 if (has_non_ascii(oldarg
, (size_t)-1, &namelen
)) {
81 newarg
= reencode_string_iconv(oldarg
, namelen
, ic_precompose
, NULL
);
87 iconv_close(ic_precompose
);
91 PREC_DIR
*precompose_utf8_opendir(const char *dirname
)
93 PREC_DIR
*prec_dir
= xmalloc(sizeof(PREC_DIR
));
94 prec_dir
->dirent_nfc
= xmalloc(sizeof(dirent_prec_psx
));
95 prec_dir
->dirent_nfc
->max_name_len
= sizeof(prec_dir
->dirent_nfc
->d_name
);
97 prec_dir
->dirp
= opendir(dirname
);
98 if (!prec_dir
->dirp
) {
99 free(prec_dir
->dirent_nfc
);
103 int ret_errno
= errno
;
104 prec_dir
->ic_precompose
= iconv_open(repo_encoding
, path_encoding
);
105 /* if iconv_open() fails, die() in readdir() if needed */
112 struct dirent_prec_psx
*precompose_utf8_readdir(PREC_DIR
*prec_dir
)
115 res
= readdir(prec_dir
->dirp
);
117 size_t namelenz
= strlen(res
->d_name
) + 1; /* \0 */
118 size_t new_maxlen
= namelenz
;
120 int ret_errno
= errno
;
122 if (new_maxlen
> prec_dir
->dirent_nfc
->max_name_len
) {
123 size_t new_len
= sizeof(dirent_prec_psx
) + new_maxlen
-
124 sizeof(prec_dir
->dirent_nfc
->d_name
);
126 prec_dir
->dirent_nfc
= xrealloc(prec_dir
->dirent_nfc
, new_len
);
127 prec_dir
->dirent_nfc
->max_name_len
= new_maxlen
;
130 prec_dir
->dirent_nfc
->d_ino
= res
->d_ino
;
131 prec_dir
->dirent_nfc
->d_type
= res
->d_type
;
133 if ((precomposed_unicode
== 1) && has_non_ascii(res
->d_name
, (size_t)-1, NULL
)) {
134 if (prec_dir
->ic_precompose
== (iconv_t
)-1) {
135 die("iconv_open(%s,%s) failed, but needed:\n"
136 " precomposed unicode is not supported.\n"
137 " If you want to use decomposed unicode, run\n"
138 " \"git config core.precomposeunicode false\"\n",
139 repo_encoding
, path_encoding
);
141 iconv_ibp cp
= (iconv_ibp
)res
->d_name
;
142 size_t inleft
= namelenz
;
143 char *outpos
= &prec_dir
->dirent_nfc
->d_name
[0];
144 size_t outsz
= prec_dir
->dirent_nfc
->max_name_len
;
146 iconv(prec_dir
->ic_precompose
, &cp
, &inleft
, &outpos
, &outsz
);
147 if (errno
|| inleft
) {
149 * iconv() failed and errno could be E2BIG, EILSEQ, EINVAL, EBADF
150 * MacOS X avoids illegal byte sequemces.
151 * If they occur on a mounted drive (e.g. NFS) it is not worth to
152 * die() for that, but rather let the user see the original name
154 namelenz
= 0; /* trigger strlcpy */
161 strlcpy(prec_dir
->dirent_nfc
->d_name
, res
->d_name
,
162 prec_dir
->dirent_nfc
->max_name_len
);
165 return prec_dir
->dirent_nfc
;
171 int precompose_utf8_closedir(PREC_DIR
*prec_dir
)
175 ret_value
= closedir(prec_dir
->dirp
);
177 if (prec_dir
->ic_precompose
!= (iconv_t
)-1)
178 iconv_close(prec_dir
->ic_precompose
);
179 free(prec_dir
->dirent_nfc
);