4299: Fix fs_makedir, fs_storage_path, fs_getcwd and fs_chdir with unicode on windows r=def- a=Robyt3

Use the wide char variants of `getenv`, `_mkdir`, `_getcwd` and `chdir`.

Tested the change on upstream with japanese characters and german umlauts in username.

This should fix the issues with cyrillic username, I don't know if that "out of memory" from sqlite was also caused by an invalid appdata path (#4293).

`fs_chdir` is unused anyway but this also fixes it for completeness.

## Checklist

- [ ] Tested the change ingame
- [ ] Provided screenshots if it is a visual change
- [ ] Tested in combination with possibly related configuration options
- [ ] Written a unit test if it works standalone, system.c especially
- [ ] Considered possible null pointers and out of bounds array indexing
- [ ] Changed no physics that affect existing maps
- [ ] Tested the change with [ASan+UBSan or valgrind's memcheck](https://github.com/ddnet/ddnet/#using-addresssanitizer--undefinedbehavioursanitizer-or-valgrinds-memcheck) (optional)


Co-authored-by: Robert Müller <robert.mueller@uni-siegen.de>
This commit is contained in:
bors[bot] 2021-11-04 21:38:32 +00:00 committed by GitHub
commit 1c809fad22
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2195,10 +2195,12 @@ void fs_listdir_fileinfo(const char *dir, FS_LISTDIR_CALLBACK_FILEINFO cb, int t
int fs_storage_path(const char *appname, char *path, int max)
{
#if defined(CONF_FAMILY_WINDOWS)
char *home = getenv("APPDATA");
WCHAR *home = _wgetenv(L"APPDATA");
if(!home)
return -1;
_snprintf(path, max, "%s/%s", home, appname);
char buffer[IO_MAX_PATH_LENGTH];
WideCharToMultiByte(CP_UTF8, 0, home, -1, buffer, IO_MAX_PATH_LENGTH, NULL, NULL);
_snprintf(path, max, "%s/%s", buffer, appname);
return 0;
#elif defined(CONF_PLATFORM_ANDROID)
// just use the data directory
@ -2249,7 +2251,9 @@ int fs_makedir_rec_for(const char *path)
int fs_makedir(const char *path)
{
#if defined(CONF_FAMILY_WINDOWS)
if(_mkdir(path) == 0)
WCHAR wBuffer[IO_MAX_PATH_LENGTH];
MultiByteToWideChar(CP_UTF8, 0, path, -1, wBuffer, IO_MAX_PATH_LENGTH);
if(_wmkdir(wBuffer) == 0)
return 0;
if(errno == EEXIST)
return 0;
@ -2319,10 +2323,19 @@ int fs_chdir(const char *path)
{
if(fs_is_dir(path))
{
#if defined(CONF_FAMILY_WINDOWS)
WCHAR wBuffer[IO_MAX_PATH_LENGTH];
MultiByteToWideChar(CP_UTF8, 0, path, -1, wBuffer, IO_MAX_PATH_LENGTH);
if(_wchdir(wBuffer))
return 1;
else
return 0;
#else
if(chdir(path))
return 1;
else
return 0;
#endif
}
else
return 1;
@ -2333,7 +2346,11 @@ char *fs_getcwd(char *buffer, int buffer_size)
if(buffer == 0)
return 0;
#if defined(CONF_FAMILY_WINDOWS)
return _getcwd(buffer, buffer_size);
WCHAR wBuffer[IO_MAX_PATH_LENGTH];
if(_wgetcwd(wBuffer, buffer_size) == 0)
return 0;
WideCharToMultiByte(CP_UTF8, 0, wBuffer, IO_MAX_PATH_LENGTH, buffer, buffer_size, NULL, NULL);
return buffer;
#else
return getcwd(buffer, buffer_size);
#endif