[PATCH v3 09/10] scrrun: Move source dir into destination dir if destination ends with separator in MoveFolder().

Robert Wilhelm wine at gitlab.winehq.org
Tue Jul 5 10:53:28 CDT 2022


From: Robert Wilhelm <robert.wilhelm at gmx.net>

Signed-off-by: Robert Wilhelm <robert.wilhelm at gmx.net>
---
 dlls/scrrun/filesystem.c       | 113 +++++++++------------------------
 dlls/scrrun/tests/filesystem.c |  69 ++++++--------------
 2 files changed, 48 insertions(+), 134 deletions(-)

diff --git a/dlls/scrrun/filesystem.c b/dlls/scrrun/filesystem.c
index fc5ffe752c6..70c54a5ecb3 100644
--- a/dlls/scrrun/filesystem.c
+++ b/dlls/scrrun/filesystem.c
@@ -3301,37 +3301,10 @@ static HRESULT WINAPI filesys_GetParentFolderName(IFileSystem3 *iface, BSTR Path
     return S_OK;
 }
 
-static HRESULT get_file_name(const WCHAR *path, int *start, int *end)
-{
-    int s,e;
-
-    if(!path)
-        return 0;
-
-    for(e=lstrlenW(path)-1; e>=0; e--)
-        if(path[e]!='/' && path[e]!='\\')
-            break;
-
-    for(s=e; s>=0; s--)
-        if(path[s]=='/' || path[s]=='\\')
-            break;
-
-    s++;
-
-    if(s>e || (s==0 && e==1 && path[1]==':')) {
-        return E_FAIL;
-    }
-
-    *start = s;
-    *end = e;
-
-    return S_OK;
-}
-
 static HRESULT WINAPI filesys_GetFileName(IFileSystem3 *iface, BSTR Path,
                                             BSTR *pbstrResult)
 {
-    int start=0, end=0;
+    int i, end;
 
     TRACE("%p %s %p\n", iface, debugstr_w(Path), pbstrResult);
 
@@ -3343,12 +3316,21 @@ static HRESULT WINAPI filesys_GetFileName(IFileSystem3 *iface, BSTR Path,
         return S_OK;
     }
 
-    if (FAILED(get_file_name( Path, &start, &end))) {
+    for(end=lstrlenW(Path)-1; end>=0; end--)
+        if(Path[end]!='/' && Path[end]!='\\')
+            break;
+
+    for(i=end; i>=0; i--)
+        if(Path[i]=='/' || Path[i]=='\\')
+            break;
+    i++;
+
+    if(i>end || (i==0 && end==1 && Path[1]==':')) {
         *pbstrResult = NULL;
         return S_OK;
     }
 
-    *pbstrResult = SysAllocStringLen(Path+start, end-start+1);
+    *pbstrResult = SysAllocStringLen(Path+i, end-i+1);
     if(!*pbstrResult)
         return E_OUTOFMEMORY;
     return S_OK;
@@ -3785,71 +3767,34 @@ static HRESULT WINAPI filesys_MoveFile(IFileSystem3 *iface, BSTR source, BSTR de
 static HRESULT WINAPI filesys_MoveFolder(IFileSystem3 *iface, BSTR source, BSTR destination)
 {
     DWORD attrs;
-    int i,len,len2,slen,start,end;
-    WCHAR src_path[MAX_PATH],dst_path[MAX_PATH];
-    WIN32_FIND_DATAW ffd;
-    HANDLE f;
-    BOOL wildcard = FALSE;
+    int len;
+    WCHAR src_drive[MAX_PATH],src_dir[MAX_PATH],dst_path[MAX_PATH],
+          file_name[MAX_PATH],file_ext[MAX_PATH];
 
     TRACE("%p %s %s\n", iface, debugstr_w(source), debugstr_w(destination));
 
     if(!source || !destination)
         return E_INVALIDARG;
 
-    if (FAILED(get_file_name( source, &start, &end)))
-        return E_INVALIDARG;
-
-    for(i = start; i <= end; i++)
-    {
-        if( source[i]=='*' || source[i]=='?')
-        {
-           wildcard = TRUE;
-           break;
-        }
-    }
+    _wsplitpath(source, src_drive, src_dir, file_name, file_ext);
 
-    if (!wildcard) {
-        attrs = GetFileAttributesW(source);
-        if((attrs == INVALID_FILE_ATTRIBUTES) || !(attrs & FILE_ATTRIBUTE_DIRECTORY))
-            return CTL_E_PATHNOTFOUND;
-    }
+    attrs = GetFileAttributesW(source);
+    if((attrs == INVALID_FILE_ATTRIBUTES) || !(attrs & FILE_ATTRIBUTE_DIRECTORY))
+        return CTL_E_PATHNOTFOUND;
 
-    slen = lstrlenW(source);
     len = lstrlenW(destination);
 
-    if (wildcard || destination[len-1] == '\\')
-    {
-        f = FindFirstFileW(source, &ffd);
-        if(f == INVALID_HANDLE_VALUE)
-            return create_error(GetLastError());
-
-        do {
-            memcpy(dst_path, destination, (len+1)*sizeof(WCHAR));
-            memcpy(src_path, source, (slen+1)*sizeof(WCHAR));
-            if (wildcard) {
-                len2 = len;
-                if (destination[len-1] != '\\')
-                {
-                    dst_path[len] = '\\';
-                    len2++;
-                }
-                memcpy(dst_path+len2, ffd.cFileName, (lstrlenW(ffd.cFileName)+1)*sizeof(WCHAR));
-                memcpy(src_path+start,ffd.cFileName,(lstrlenW(ffd.cFileName)+1)*sizeof(WCHAR));
-            }
-            else {
-                memcpy(dst_path+len, source + start, (end-start+1)*sizeof(WCHAR));
-                dst_path[len+end-start+1]=0;
-            }
-            if (!MoveFileW(src_path, dst_path))
-                return create_error(GetLastError());
-        } while(FindNextFileW(f, &ffd));
-        FindClose(f);
-    }
-    else
-    {
-        return MoveFileW(source, destination) ? S_OK : create_error(GetLastError());
+    if (destination[len-1] == '\\') {
+        lstrcpyW(dst_path, destination);
+        lstrcatW(dst_path, file_name);
+        if (*file_ext) {
+            lstrcatW(dst_path, L".");
+            lstrcatW(dst_path, file_ext);
+        }
+        TRACE(" %s %s\n",  debugstr_w(source), debugstr_w(dst_path));
+        return MoveFileW(source, dst_path) ? S_OK : create_error(GetLastError());
     }
-    return S_OK;
+    return MoveFileW(source, destination) ? S_OK : create_error(GetLastError());
 }
 
 static inline HRESULT copy_file(const WCHAR *source, DWORD source_len,
diff --git a/dlls/scrrun/tests/filesystem.c b/dlls/scrrun/tests/filesystem.c
index df02cd1186b..be7f090daec 100644
--- a/dlls/scrrun/tests/filesystem.c
+++ b/dlls/scrrun/tests/filesystem.c
@@ -2536,7 +2536,6 @@ static void test_MoveFile(void)
 {
     ITextStream *stream;
     BSTR str, src, dst;
-    WCHAR buffW1[MAX_PATH],buffW2[MAX_PATH],pathW[MAX_PATH],srcW[MAX_PATH];
     HRESULT hr;
 
     str = SysAllocString(L"test.txt");
@@ -2599,60 +2598,12 @@ static void test_MoveFile(void)
     hr = IFileSystem3_MoveFile(fs3, NULL, str);
     ok(hr == E_INVALIDARG, "Unexpected hr %#lx.\n", hr);
     SysFreeString(str);
-
-    GetTempPathW(MAX_PATH, buffW1);
-    lstrcatW(buffW1,L"foo");
-    GetTempPathW(MAX_PATH, buffW2);
-    lstrcatW(buffW2,L"bar");
-    ok(CreateDirectoryW(buffW1, NULL), "CreateDirectory(%s) failed\n", wine_dbgstr_w(buffW1));
-    ok(CreateDirectoryW(buffW2, NULL), "CreateDirectory(%s) failed\n", wine_dbgstr_w(buffW2));
-    lstrcpyW(pathW,buffW2);
-    lstrcatW(pathW,L"\\");
-    src = SysAllocString(buffW1);
-    dst = SysAllocString(pathW);
-    hr = IFileSystem3_MoveFolder(fs3, src, dst);
-    ok(hr == S_OK, "Unexpected hr %#lx.\n", hr);
-    SysFreeString(src);
-    SysFreeString(dst);
-    lstrcatW(pathW,L"foo");
-    ok(RemoveDirectoryW(pathW), "can't remove %s directory\n", wine_dbgstr_w(pathW));
-    ok(RemoveDirectoryW(buffW2), "can't remove %s directory\n", wine_dbgstr_w(buffW2));
-
-    GetTempPathW(MAX_PATH, buffW1);
-    lstrcatW(buffW1,L"foo1");
-    GetTempPathW(MAX_PATH, buffW2);
-    lstrcatW(buffW2,L"foo2");
-    GetTempPathW(MAX_PATH, srcW);
-    lstrcatW(srcW,L"foo?");
-    GetTempPathW(MAX_PATH, pathW);
-    lstrcatW(pathW,L"bar");
-    RemoveDirectoryW(buffW1);
-    RemoveDirectoryW(buffW2);
-    RemoveDirectoryW(pathW);
-    ok(CreateDirectoryW(buffW1, NULL), "CreateDirectory(%s) failed\n", wine_dbgstr_w(buffW1));
-    ok(CreateDirectoryW(buffW2, NULL), "CreateDirectory(%s) failed\n", wine_dbgstr_w(buffW2));
-    ok(CreateDirectoryW(pathW, NULL), "CreateDirectory(%s) failed\n", wine_dbgstr_w(pathW));
-    src = SysAllocString(srcW);
-    dst = SysAllocString(pathW);
-    hr = IFileSystem3_MoveFolder(fs3, src, dst);
-    ok(hr == S_OK, "Unexpected hr %#lx.\n", hr);
-    SysFreeString(src);
-    SysFreeString(dst);
-    lstrcpyW(buffW1,pathW);
-    lstrcatW(buffW1,L"\\");
-    lstrcatW(buffW1,L"foo1");
-    lstrcpyW(buffW2,pathW);
-    lstrcatW(buffW2,L"\\");
-    lstrcatW(buffW2,L"foo2");
-    ok(RemoveDirectoryW(buffW1), "can't remove %s directory\n", wine_dbgstr_w(buffW1));
-    ok(RemoveDirectoryW(buffW2), "can't remove %s directory\n", wine_dbgstr_w(buffW2));
-    ok(RemoveDirectoryW(pathW), "can't remove %s directory\n", wine_dbgstr_w(pathW));
 }
 
 static void test_MoveFolder(void)
 {
     BSTR src, dst, str;
-    WCHAR buffW1[MAX_PATH],buffW2[MAX_PATH];
+    WCHAR buffW1[MAX_PATH],buffW2[MAX_PATH],pathW[MAX_PATH];
     HRESULT hr;
     File *file;
 
@@ -2705,6 +2656,24 @@ static void test_MoveFolder(void)
     SysFreeString(src);
     SysFreeString(dst);
     DeleteFileW(buffW1);
+
+    GetTempPathW(MAX_PATH, buffW1);
+    lstrcatW(buffW1,L"foo");
+    GetTempPathW(MAX_PATH, buffW2);
+    lstrcatW(buffW2,L"bar");
+    ok(CreateDirectoryW(buffW1, NULL), "CreateDirectory(%s) failed\n", wine_dbgstr_w(buffW1));
+    ok(CreateDirectoryW(buffW2, NULL), "CreateDirectory(%s) failed\n", wine_dbgstr_w(buffW2));
+    lstrcpyW(pathW,buffW2);
+    lstrcatW(pathW,L"\\");
+    src = SysAllocString(buffW1);
+    dst = SysAllocString(pathW);
+    hr = IFileSystem3_MoveFolder(fs3, src, dst);
+    ok(hr == S_OK, "Unexpected hr %#lx.\n", hr);
+    SysFreeString(src);
+    SysFreeString(dst);
+    lstrcatW(pathW,L"foo");
+    ok(RemoveDirectoryW(pathW), "can't remove %s directory\n", wine_dbgstr_w(pathW));
+    ok(RemoveDirectoryW(buffW2), "can't remove %s directory\n", wine_dbgstr_w(buffW2));
 }
 
 static void test_DoOpenPipeStream(void)
-- 
GitLab


https://gitlab.winehq.org/wine/wine/-/merge_requests/343



More information about the wine-devel mailing list