Commit 3c068685 authored by Julien Wadel's avatar Julien Wadel
Browse files

Use UTF8 setlocale for dealing files. This allows string convertions to be correctly interpreted.

By forcing in UTF8, we are sure that convertions into system locale will be correctly done without having to know in advance what encoding it must be use.
1 merge request!224Use UTF8 setlocale for dealing files. This allows string convertions to be correctly interpreted.
Showing with 60 additions and 14 deletions
......@@ -517,7 +517,7 @@ BCTBX_PUBLIC char *bctbx_basename(const char *path);
/**
* Tests if a file with given path exists.
*
* @param[in] path the file path to test
* @param[in] path the file path to test in UTF8.
* @return 0 if yes, -1 otherwise.
**/
BCTBX_PUBLIC int bctbx_file_exist(const char *pathname);
......@@ -525,7 +525,7 @@ BCTBX_PUBLIC int bctbx_file_exist(const char *pathname);
/**
* Tests if a directory with given pathname exists.
*
* @param[in] pathname the directory path to test
* @param[in] pathname the directory path to test in UTF8.
* @return TRUE if yes, FALSE otherwise.
**/
BCTBX_PUBLIC bool_t bctbx_directory_exists(const char *pathname);
......@@ -536,7 +536,7 @@ struct _bctbx_list;
/**
* Parse a directory and return a list of all its files and subdirectories
*
* @param[in] path The directory to parse
* @param[in] path The directory to parse in UTF8.
* @param[in] file_type select only files with that extension, can be NULL in that case it returns all files.
*
* @note . and .. file descriptors are never returned in the list, even when file_type is set to NULL
......@@ -551,7 +551,7 @@ BCTBX_PUBLIC struct _bctbx_list *bctbx_parse_directory(const char *path, const c
* Create a directory
* Note: parent directory must exists, this function cannot create a complete path
*
* @param[in] path the directory to create
* @param[in] path the directory to create in UTF8.
*
* @return 0 on success
**/
......@@ -560,7 +560,7 @@ BCTBX_PUBLIC int bctbx_mkdir(const char *path);
/**
* Delete a directory
*
* @param[in] path the directory to delete
* @param[in] path the directory to delete in UTF8.
* @param[in] recursive if false, the directory must be empty to be deleted otherwise recursively delete with all content
*
* @return 0 on success
......
......@@ -145,7 +145,7 @@ BCTBX_PUBLIC int bctbx_file_close(bctbx_vfs_file_t *pFile);
* Allocates a bctbx_vfs_file_t file handle pointer. Opens the file fName
* with the mode specified by the mode argument. Calls bctbx_file_open.
* @param pVfs Pointer to the vfs instance in use.
* @param fName Absolute file path.
* @param fName Absolute file path in UTF8.
* @param mode File access mode (char*).
* @return pointer to bctbx_vfs_file_t on success, NULL otherwise.
*/
......@@ -156,7 +156,7 @@ BCTBX_PUBLIC bctbx_vfs_file_t* bctbx_file_open(bctbx_vfs_t *pVfs, const char *fN
* Allocates a bctbx_vfs_file_t file handle pointer. Opens the file fName
* with the mode specified by the mode argument. Calls bctbx_file_open.
* @param pVfs Pointer to the vfs instance in use.
* @param fName Absolute file path.
* @param fName Absolute file path in UTF8.
* @param openFlags File access flags(integer).
* @return pointer to bctbx_vfs_file_t on success, NULL otherwise.
*/
......
......@@ -22,6 +22,7 @@
#include "config.h"
#endif
#include <locale.h>
#include "bctoolbox/defs.h"
#include "bctoolbox/logging.h"
#include "bctoolbox/port.h"
......@@ -154,20 +155,32 @@ int bctbx_socket_set_non_blocking(bctbx_socket_t sock){
int bctbx_file_exist(const char *pathname) {
return access(pathname,F_OK);
char * localeBackup = bctbx_strdup(setlocale(LC_ALL, NULL));
setlocale(LC_ALL, ".UTF-8");
int result = access(pathname,F_OK);
setlocale(LC_ALL, localeBackup);
bctbx_free(localeBackup);
return result;
}
bool_t bctbx_directory_exists(const char *pathname) {
struct stat sb;
char * localeBackup = bctbx_strdup(setlocale(LC_ALL, NULL));
setlocale(LC_ALL, ".UTF-8");
#ifdef WIN32
return stat(pathname, &sb) == 0 && (_S_IFDIR & sb.st_mode);
int result = stat(pathname, &sb) == 0 && (_S_IFDIR & sb.st_mode);
#else
return stat(pathname, &sb) == 0 && S_ISDIR(sb.st_mode);
int result = stat(pathname, &sb) == 0 && S_ISDIR(sb.st_mode);
#endif
setlocale(LC_ALL, localeBackup);
bctbx_free(localeBackup);
return result;
}
bctbx_list_t *bctbx_parse_directory(const char *path, const char *file_type) {
bctbx_list_t* file_list = NULL;
char * localeBackup = bctbx_strdup(setlocale(LC_ALL, NULL));
setlocale(LC_ALL, ".UTF-8");
#ifdef _WIN32
WIN32_FIND_DATA FileData;
HANDLE hSearch;
......@@ -189,6 +202,8 @@ bctbx_list_t *bctbx_parse_directory(const char *path, const char *file_type) {
#endif
if (hSearch == INVALID_HANDLE_VALUE) {
bctbx_message("No file (*%s) found in [%s] [%d].", file_type, szDirPath, (int)GetLastError());
setlocale(LC_ALL, localeBackup);
bctbx_free(localeBackup);
return NULL;
}
snprintf(szDirPath, sizeof(szDirPath), "%s", path);
......@@ -225,6 +240,8 @@ bctbx_list_t *bctbx_parse_directory(const char *path, const char *file_type) {
if ((dir = opendir(path)) == NULL) {
bctbx_error("Could't open [%s] directory.", path);
setlocale(LC_ALL, localeBackup);
bctbx_free(localeBackup);
return NULL;
}
......@@ -250,15 +267,22 @@ bctbx_list_t *bctbx_parse_directory(const char *path, const char *file_type) {
}
closedir(dir);
#endif
setlocale(LC_ALL, localeBackup);
bctbx_free(localeBackup);
return file_list;
}
int bctbx_mkdir(const char *path) {
char * localeBackup = bctbx_strdup(setlocale(LC_ALL, NULL));
setlocale(LC_ALL, ".UTF-8");
#ifdef _WIN32
return _mkdir(path);
int result = _mkdir(path);
#else
return mkdir(path, 0700);
int result = mkdir(path, 0700);
#endif
setlocale(LC_ALL, localeBackup);
bctbx_free(localeBackup);
return result;
};
/**
......@@ -269,11 +293,16 @@ int bctbx_mkdir(const char *path) {
* @return 0 on success
*/
static int bctbx_rmemptydir(const char *path) {
char * localeBackup = bctbx_strdup(setlocale(LC_ALL, NULL));
setlocale(LC_ALL, ".UTF-8");
#ifdef _WIN32
return _rmdir(path);
int result = _rmdir(path);
#else
return rmdir(path);
int result = rmdir(path);
#endif
setlocale(LC_ALL, localeBackup);
bctbx_free(localeBackup);
return result;
};
/**
......@@ -289,7 +318,11 @@ static void bctbx_removeFileFromList(void *v_path) {
if (bctbx_directory_exists(path)) {
bctbx_rmdir(path, TRUE);
} else {
char * localeBackup = bctbx_strdup(setlocale(LC_ALL, NULL));
setlocale(LC_ALL, ".UTF-8");
remove(path);
setlocale(LC_ALL, localeBackup);
bctbx_free(localeBackup);
}
}
......
......@@ -29,6 +29,7 @@
#include <sys/types.h>
#include <stdarg.h>
#include <errno.h>
#include <locale.h>
static ssize_t bctbx_file_flush(bctbx_vfs_file_t *pFile);
......@@ -86,7 +87,11 @@ ssize_t bctbx_file_write2(bctbx_vfs_file_t* pFile, const void *buf, size_t count
static int file_open(bctbx_vfs_t* pVfs, bctbx_vfs_file_t* pFile, const char *fName, const int oflags) {
int ret = BCTBX_VFS_ERROR;
if (pVfs && pFile ) {
char * localeBackup = bctbx_strdup(setlocale(LC_ALL, NULL));
setlocale(LC_ALL, ".UTF-8");
ret = pVfs->pFuncOpen(pVfs, pFile, fName, oflags);
setlocale(LC_ALL, localeBackup);
bctbx_free(localeBackup);
if (ret == BCTBX_VFS_ERROR) {
bctbx_error("bctbx_file_open: Error file handle");
} else if (ret < 0 ) {
......@@ -103,7 +108,11 @@ bctbx_vfs_file_t* bctbx_file_open(bctbx_vfs_t *pVfs, const char *fName, const ch
int oflags = set_flags(mode);
if (p_ret) {
memset(p_ret, 0, sizeof(bctbx_vfs_file_t));
char * localeBackup = bctbx_strdup(setlocale(LC_ALL, NULL));
setlocale(LC_ALL, ".UTF-8");
ret = file_open(pVfs, p_ret, fName, oflags);
setlocale(LC_ALL, localeBackup);
bctbx_free(localeBackup);
if (ret == BCTBX_VFS_OK) return p_ret;
}
......@@ -117,7 +126,11 @@ bctbx_vfs_file_t* bctbx_file_open2(bctbx_vfs_t *pVfs, const char *fName, const i
if (p_ret) {
memset(p_ret, 0, sizeof(bctbx_vfs_file_t));
char * localeBackup = bctbx_strdup(setlocale(LC_ALL, NULL));
setlocale(LC_ALL, ".UTF-8");
ret = file_open(pVfs, p_ret, fName, openFlags);
setlocale(LC_ALL, localeBackup);
bctbx_free(localeBackup);
if (ret == BCTBX_VFS_OK) return p_ret;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment