Use encoding of NFD to create file and directory on mac (#2668)

* Use encoding of NFD to create file and directory on mac

* Convert path to NFD before call os

---------

Co-authored-by: heran yang <heran.yang@seafile.com>
This commit is contained in:
feiniks 2023-04-14 16:09:16 +08:00 committed by GitHub
parent 4aa7b5e616
commit 8e3b6d67bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 20 deletions

View File

@ -148,7 +148,7 @@ create_parent_path (const char *path)
return 0;
}
if (g_mkdir_with_parents (dir, 0777) < 0) {
if (checkdir_with_mkdir (dir) < 0) {
seaf_warning ("Failed to create object parent path: %s.\n", dir);
g_free (dir);
return -1;
@ -472,13 +472,13 @@ block_backend_fs_new (const char *seaf_dir, const char *tmp_dir)
priv->tmp_dir = g_strdup (tmp_dir);
priv->tmp_dir_len = strlen (tmp_dir);
if (g_mkdir_with_parents (priv->block_dir, 0777) < 0) {
if (checkdir_with_mkdir (priv->block_dir) < 0) {
seaf_warning ("Block dir %s does not exist and"
" is unable to create\n", priv->block_dir);
goto onerror;
}
if (g_mkdir_with_parents (tmp_dir, 0777) < 0) {
if (checkdir_with_mkdir (tmp_dir) < 0) {
seaf_warning ("Blocks tmp dir %s does not exist and"
" is unable to create\n", tmp_dir);
goto onerror;

View File

@ -276,7 +276,7 @@ create_parent_path (const char *path)
return 0;
}
if (g_mkdir_with_parents (dir, 0777) < 0) {
if (checkdir_with_mkdir (dir) < 0) {
seaf_warning ("Failed to create object parent path %s: %s.\n",
dir, strerror(errno));
g_free (dir);
@ -518,13 +518,13 @@ obj_backend_fs_new (const char *seaf_dir, const char *obj_type)
priv->obj_dir = g_build_filename (seaf_dir, "storage", obj_type, NULL);
priv->dir_len = strlen (priv->obj_dir);
if (g_mkdir_with_parents (priv->v0_obj_dir, 0777) < 0) {
if (checkdir_with_mkdir (priv->v0_obj_dir) < 0) {
seaf_warning ("[Obj Backend] Objects dir %s does not exist and"
" is unable to create\n", priv->v0_obj_dir);
goto onerror;
}
if (g_mkdir_with_parents (priv->obj_dir, 0777) < 0) {
if (checkdir_with_mkdir (priv->obj_dir) < 0) {
seaf_warning ("[Obj Backend] Objects dir %s does not exist and"
" is unable to create\n", priv->obj_dir);
goto onerror;

View File

@ -121,7 +121,7 @@ start_clone_v2 (CloneTask *task)
GError *error = NULL;
if (g_access (task->worktree, F_OK) != 0 &&
g_mkdir_with_parents (task->worktree, 0777) < 0) {
checkdir_with_mkdir (task->worktree) < 0) {
seaf_warning ("[clone mgr] Failed to create worktree %s.\n",
task->worktree);
transition_to_error (task, SYNC_ERROR_ID_WRITE_LOCAL_DATA);

View File

@ -137,8 +137,8 @@ checkdir (const char *dir)
int
checkdir_with_mkdir (const char *dir)
{
#ifdef WIN32
int ret;
#ifdef WIN32
char *path = g_strdup(dir);
char *p = (char *)path + strlen(path) - 1;
while (*p == '\\' || *p == '/') *p-- = '\0';
@ -146,7 +146,10 @@ checkdir_with_mkdir (const char *dir)
g_free (path);
return ret;
#else
return g_mkdir_with_parents(dir, 0755);
char *dir_nfd = g_utf8_normalize (dir, -1, G_NORMALIZE_NFD);
ret = g_mkdir_with_parents(dir_nfd, 0755);
g_free (dir_nfd);
return ret;
#endif
}
@ -476,9 +479,9 @@ seaf_set_file_time (const char *path, guint64 mtime)
int
seaf_util_unlink (const char *path)
{
int ret = 0;
#ifdef WIN32
wchar_t *wpath = win32_long_path (path);
int ret = 0;
if (!DeleteFileW (wpath)) {
ret = -1;
@ -488,16 +491,19 @@ seaf_util_unlink (const char *path)
g_free (wpath);
return ret;
#else
return unlink (path);
char *path_nfd = g_utf8_normalize (path, -1, G_NORMALIZE_NFD);
ret = unlink (path_nfd);
g_free (path_nfd);
return ret;
#endif
}
int
seaf_util_rmdir (const char *path)
{
int ret = 0;
#ifdef WIN32
wchar_t *wpath = win32_long_path (path);
int ret = 0;
if (!RemoveDirectoryW (wpath)) {
ret = -1;
@ -507,16 +513,19 @@ seaf_util_rmdir (const char *path)
g_free (wpath);
return ret;
#else
return rmdir (path);
char *path_nfd = g_utf8_normalize (path, -1, G_NORMALIZE_NFD);
ret = rmdir (path_nfd);
g_free (path_nfd);
return ret;
#endif
}
int
seaf_util_mkdir (const char *path, mode_t mode)
{
int ret = 0;
#ifdef WIN32
wchar_t *wpath = win32_long_path (path);
int ret = 0;
if (!CreateDirectoryW (wpath, NULL)) {
ret = -1;
@ -526,7 +535,10 @@ seaf_util_mkdir (const char *path, mode_t mode)
g_free (wpath);
return ret;
#else
return mkdir (path, mode);
char *path_nfd = g_utf8_normalize (path, -1, G_NORMALIZE_NFD);
ret = mkdir (path_nfd, mode);
g_free (path_nfd);
return ret;
#endif
}
@ -563,7 +575,11 @@ seaf_util_open (const char *path, int flags)
g_free (wpath);
return fd;
#else
return open (path, flags);
int ret = 0;
char *path_nfd = g_utf8_normalize (path, -1, G_NORMALIZE_NFD);
ret = open (path_nfd, flags);
g_free (path_nfd);
return ret;
#endif
}
@ -600,17 +616,21 @@ seaf_util_create (const char *path, int flags, mode_t mode)
g_free (wpath);
return fd;
#else
return open (path, flags, mode);
int ret = 0;
char *path_nfd = g_utf8_normalize (path, -1, G_NORMALIZE_NFD);
ret = open (path_nfd, flags, mode);
g_free (path_nfd);
return ret;
#endif
}
int
seaf_util_rename (const char *oldpath, const char *newpath)
{
int ret = 0;
#ifdef WIN32
wchar_t *oldpathw = win32_long_path (oldpath);
wchar_t *newpathw = win32_long_path (newpath);
int ret = 0;
if (!MoveFileExW (oldpathw, newpathw, MOVEFILE_REPLACE_EXISTING)) {
ret = -1;
@ -621,7 +641,12 @@ seaf_util_rename (const char *oldpath, const char *newpath)
g_free (newpathw);
return ret;
#else
return rename (oldpath, newpath);
char *oldpath_nfd = g_utf8_normalize (oldpath, -1, G_NORMALIZE_NFD);
char *newpath_nfd = g_utf8_normalize (newpath, -1, G_NORMALIZE_NFD);
ret = rename (oldpath_nfd, newpath_nfd);
g_free (oldpath_nfd);
g_free (newpath_nfd);
return ret;
#endif
}
@ -639,7 +664,11 @@ seaf_util_exists (const char *path)
g_free (wpath);
return ret;
#else
return (access (path, F_OK) == 0);
int ret = 0;
char *path_nfd = g_utf8_normalize (path, -1, G_NORMALIZE_NFD);
ret = access (path_nfd, F_OK);
g_free (path_nfd);
return (ret == 0);
#endif
}