git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[RFC PATCH] Windows: Assume all file names to be UTF-8 encoded.

From
Peter Krefting <peter@softwolves.pp.se>
Date
Mar 2, 2009, 08:47 UTC
Message-ID
<alpine.DEB.2.00.0903020941120.17877@perkele.intern.softwolves.pp.se>

When opening a file through open() or fopen(), the path passed is UTF-8 encoded. To handle this on Windows, we need to convert the path string to UTF-16 and use the Unicode-based interface. --- Windows does support file names using arbitrary Unicode characters, you just need to use its wchar_t interfaces instead of the char ones (the char ones just gets converted into wchar_t on the API level anyway, for the same reasons). This is the beginnings of support for UTF-8 file names on Git on Windows.

Since there is no real file system abstraction beyond using stdio (AFAIK), I need to hack it by replacing fopen (and open). Probably opendir/readdir as well (might be trickier), and possibly even hack around main() to parse the wchar_t command-line instead of the char copy.

This will lose all chances of Windows 9x compatibility, but I don't know if there are any attempts of supporting it anyway?

Please note that MultiByteToWideChar() will reject any invalid UTF-8 strings, perhaps it should just fall back to a regular open()/fopen() in that case?

No Signed-Off line since this is unfinished, just presenting rough sketches of an idea.

  compat/mingw.c |   60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
  compat/mingw.h |    3 ++
  2 files changed, 62 insertions(+), 1 deletions(-)
diff --git a/compat/mingw.c b/compat/mingw.c
index e25cb4f..8b19b80 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -9,13 +9,30 @@ int mingw_open (const char *filename, int oflags, ...)
  {
  	va_list args;
  	unsigned mode;
+	wchar_t *unicode_filename;
+	int unicode_filename_len;
  	va_start(args, oflags);
  	mode = va_arg(args, int);
  	va_end(args);

  	if (!strcmp(filename, "/dev/null"))
  		filename = "nul";
-	int fd = open(filename, oflags, mode);
+
+	unicode_filename_len = MultiByteToWideChar(CP_UTF8, 0, filename, -1, NULL, 0);
+	if (0 == unicode_filename_len) {
+		errno = EINVAL;
+		return -1;
+	};
+
+	unicode_filename = xmalloc(unicode_filename_len * sizeof (wchar_t));
+	if (NULL == unicode_filename) {
+		errno = ENOMEM;
+		return -1;
+	}
+	MultiByteToWideChar(CP_UTF8, 0, filename, -1, unicode_filename, unicode_filename_len);
+	int fd = _wopen(unicode_filename, oflags, mode);
+	free(unicode_filename);
+
  	if (fd < 0 && (oflags & O_CREAT) && errno == EACCES) {
  		DWORD attrs = GetFileAttributes(filename);
  		if (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY))
@@ -24,6 +41,47 @@ int mingw_open (const char *filename, int oflags, ...)
  	return fd;
  }

+FILE *mingw_fopen (const char *filename, const char *mode)
+{
+	wchar_t *unicode_filename, *unicode_mode;
+	int unicode_filename_len, unicode_mode_len;
+	FILE *fh;
+
+	unicode_filename_len = MultiByteToWideChar(CP_UTF8, 0, filename, -1, NULL, 0);
+	if (0 == unicode_filename_len) {
+		errno = EINVAL;
+		return NULL;
+	};
+
+	unicode_filename = xmalloc(unicode_filename_len * sizeof (wchar_t));
+	if (NULL == unicode_filename) {
+		errno = ENOMEM;
+		return NULL;
+	}
+	MultiByteToWideChar(CP_UTF8, 0, filename, -1, unicode_filename, unicode_filename_len);
+
+	unicode_mode_len = MultiByteToWideChar(CP_UTF8, 0, mode, -1, NULL, 0);
+	if (0 == unicode_mode_len) {
+		free(unicode_filename);
+		errno = EINVAL;
+		return NULL;
+	};
+
+	unicode_mode = xmalloc(unicode_mode_len * sizeof (wchar_t));
+	if (NULL == unicode_mode) {
+		free(unicode_mode);
+		errno = ENOMEM;
+		return NULL;
+	}
+	MultiByteToWideChar(CP_UTF8, 0, mode, -1, unicode_mode, unicode_mode_len);
+
+	fh = _wfopen(unicode_filename, unicode_mode);
+	free(unicode_filename);
+	free(unicode_mode);
+
+	return fh;
+}
+
  static inline time_t filetime_to_time_t(const FILETIME *ft)
  {
  	long long winTime = ((long long)ft->dwHighDateTime << 32) + ft->dwLowDateTime;
diff --git a/compat/mingw.h b/compat/mingw.h
index 4f275cb..235df0a 100644
--- a/compat/mingw.h
+++ b/compat/mingw.h
@@ -142,6 +142,9 @@ int sigaction(int sig, struct sigaction *in, struct sigaction *out);
  int mingw_open (const char *filename, int oflags, ...);
  #define open mingw_open

+FILE *mingw_fopen (const char *filename, const char *mode);
+#define fopen mingw_fopen
+
  char *mingw_getcwd(char *pointer, int len);
  #define getcwd mingw_getcwd
-- 
1.6.0.2.1172.ga5ed0
Next: Johannes Sixt
Message 1 of 26 in “Windows: Assume all file names to be UTF-8 encoded.”
  1. Windows: Assume all file names to be UTF-8 encoded.Peter Krefting, Mar 2, 2009
  2. Johannes SixtMar 2, 2009
  3. Peter KreftingMar 2, 2009
  4. Johannes SchindelinMar 2, 2009
  5. Peter KreftingMar 2, 2009
  6. Johannes SixtMar 2, 2009
  7. Peter KreftingMar 2, 2009
  8. Robin RosenbergMar 2, 2009
  9. Peter KreftingMar 2, 2009
  10. Robin RosenbergMar 2, 2009
  11. Peter KreftingMar 3, 2009
  12. Dmitry PotapovMar 3, 2009
  13. Peter KreftingMar 3, 2009
  14. Robin RosenbergMar 7, 2009
  15. Peter KreftingMar 2, 2009
  16. Thomas RastMar 2, 2009
  17. Peter KreftingMar 2, 2009
  18. Lars NoschinskiMar 3, 2009
  19. Peter KreftingMar 3, 2009
  20. Lars NoschinskiMar 3, 2009
  21. Robin RosenbergMar 3, 2009
  22. Dmitry PotapovMar 3, 2009
  23. Peter KreftingMar 3, 2009
  24. Dmitry PotapovMar 3, 2009
  25. Peter KreftingMar 4, 2009
  26. Dmitry PotapovMar 4, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.