From 327d3f06bed9eea26c9bae06bc525bbd175e8333 Mon Sep 17 00:00:00 2001 From: Gijs Vermeulen Date: Tue, 29 Jan 2019 23:34:54 +0100 Subject: [PATCH] kernel32: Implement CreateSymbolicLink Signed-off-by: Gijs Vermeulen --- dlls/kernel32/path.c | 73 ++++++++++++++++++++++++++++++++-- dlls/msvcp120/tests/msvcp120.c | 47 +++++++++++----------- 2 files changed, 93 insertions(+), 27 deletions(-) diff --git a/dlls/kernel32/path.c b/dlls/kernel32/path.c index 4f760940bfd..dae74dc2bc0 100644 --- a/dlls/kernel32/path.c +++ b/dlls/kernel32/path.c @@ -2145,8 +2145,56 @@ WCHAR * CDECL wine_get_dos_file_name( LPCSTR str ) */ BOOLEAN WINAPI CreateSymbolicLinkW(LPCWSTR link, LPCWSTR target, DWORD flags) { - FIXME("(%s %s %d): stub\n", debugstr_w(link), debugstr_w(target), flags); - return TRUE; + NTSTATUS status; + UNICODE_STRING nt_dest, nt_source; + ANSI_STRING unix_dest, unix_source; + BOOL ret = FALSE; + + TRACE("(%s, %s, %d)\n", debugstr_w(link), debugstr_w(target), flags); + + if(GetFileAttributesW(target) & FILE_ATTRIBUTE_DIRECTORY) + { + FIXME("Directories are not supported\n"); + return TRUE; + } + + nt_dest.Buffer = nt_source.Buffer = NULL; + if (!RtlDosPathNameToNtPathName_U( link, &nt_dest, NULL, NULL ) || + !RtlDosPathNameToNtPathName_U( target, &nt_source, NULL, NULL )) + { + SetLastError( ERROR_PATH_NOT_FOUND ); + goto err; + } + + unix_source.Buffer = unix_dest.Buffer = NULL; + status = wine_nt_to_unix_file_name( &nt_source, &unix_source, FILE_OPEN, FALSE ); + if (!status) + { + status = wine_nt_to_unix_file_name( &nt_dest, &unix_dest, FILE_CREATE, FALSE ); + if (!status) /* destination must not exist */ + status = STATUS_OBJECT_NAME_EXISTS; + else if (status == STATUS_NO_SUCH_FILE) + status = STATUS_SUCCESS; + } + + if (status) + SetLastError( RtlNtStatusToDosError(status) ); + else if (!symlink( unix_source.Buffer, unix_dest.Buffer )) + { + TRACE("Symlinked '%s' to '%s'\n", debugstr_a( unix_dest.Buffer ), + debugstr_a( unix_source.Buffer )); + ret = TRUE; + } + else + FILE_SetDosError(); + + RtlFreeAnsiString( &unix_source ); + RtlFreeAnsiString( &unix_dest ); + +err: + RtlFreeUnicodeString( &nt_source ); + RtlFreeUnicodeString( &nt_dest ); + return ret; } /************************************************************************* @@ -2154,8 +2202,25 @@ BOOLEAN WINAPI CreateSymbolicLinkW(LPCWSTR link, LPCWSTR target, DWORD flags) */ BOOLEAN WINAPI CreateSymbolicLinkA(LPCSTR link, LPCSTR target, DWORD flags) { - FIXME("(%s %s %d): stub\n", debugstr_a(link), debugstr_a(target), flags); - return TRUE; + WCHAR *sourceW, *destW; + BOOL res; + + if (!(sourceW = FILE_name_AtoW( target, TRUE ))) + { + return FALSE; + } + if (!(destW = FILE_name_AtoW( link, TRUE ))) + { + HeapFree( GetProcessHeap(), 0, sourceW ); + return FALSE; + } + + res = CreateSymbolicLinkW( destW, sourceW, flags ); + + HeapFree( GetProcessHeap(), 0, sourceW ); + HeapFree( GetProcessHeap(), 0, destW ); + + return res; } /************************************************************************* diff --git a/dlls/msvcp120/tests/msvcp120.c b/dlls/msvcp120/tests/msvcp120.c index 70fc0c13dc1..5bb507c7b6b 100644 --- a/dlls/msvcp120/tests/msvcp120.c +++ b/dlls/msvcp120/tests/msvcp120.c @@ -1648,7 +1648,7 @@ static void test_tr2_sys__Stat(void) { "tr2_test_dir\\f1", regular_file, ERROR_SUCCESS, FALSE }, { "tr2_test_dir\\not_exist_file ", file_not_found, ERROR_SUCCESS, FALSE }, { "tr2_test_dir\\??invalid_name>>", file_not_found, ERROR_SUCCESS, FALSE }, - { "tr2_test_dir\\f1_link" , regular_file, ERROR_SUCCESS, TRUE }, + { "tr2_test_dir\\f1_link" , regular_file, ERROR_SUCCESS, FALSE }, { "tr2_test_dir\\dir_link", directory_file, ERROR_SUCCESS, TRUE }, }; WCHAR testW[] = {'t','r','2','_','t','e','s','t','_','d','i','r',0}; @@ -1718,7 +1718,7 @@ static void test_tr2_sys__Stat(void) ok(ERROR_SUCCESS == err_code, "tr2_sys__Lstat_wchar(): err_code expect ERROR_SUCCESS, got %d\n", err_code); if(ret) { - todo_wine ok(DeleteFileA("tr2_test_dir/f1_link"), "expect tr2_test_dir/f1_link to exist\n"); + ok(DeleteFileA("tr2_test_dir/f1_link"), "expect tr2_test_dir/f1_link to exist\n"); todo_wine ok(RemoveDirectoryA("tr2_test_dir/dir_link"), "expect tr2_test_dir/dir_link to exist\n"); } ok(DeleteFileA("tr2_test_dir/f1"), "expect tr2_test_dir/f1 to exist\n"); @@ -1954,14 +1954,14 @@ static void test_tr2_sys__Symlink(void) int last_error; MSVCP_bool is_todo; } tests[] = { - { "f1", "f1_link", ERROR_SUCCESS, FALSE }, - { "f1", "tr2_test_dir\\f1_link", ERROR_SUCCESS, FALSE }, - { "tr2_test_dir\\f1_link", "tr2_test_dir\\f1_link_link", ERROR_SUCCESS, FALSE }, + { "f1", "f1_link", ERROR_SUCCESS, TRUE }, + { "f1", "tr2_test_dir\\f1_link", ERROR_SUCCESS, TRUE }, + { "tr2_test_dir\\f1_link", "tr2_test_dir\\f1_link_link", ERROR_SUCCESS, TRUE }, { "tr2_test_dir", "dir_link", ERROR_SUCCESS, FALSE }, { NULL, "NULL_link", ERROR_INVALID_PARAMETER, FALSE }, { "f1", NULL, ERROR_INVALID_PARAMETER, FALSE }, { "not_exist", "not_exist_link", ERROR_SUCCESS, FALSE }, - { "f1", "not_exist_dir\\f1_link", ERROR_PATH_NOT_FOUND, TRUE } + { "f1", "not_exist_dir\\f1_link", ERROR_PATH_NOT_FOUND, FALSE } }; ret = p_tr2_sys__Make_dir("tr2_test_dir"); @@ -1986,18 +1986,21 @@ static void test_tr2_sys__Symlink(void) } ok(errno == 0xdeadbeef, "tr2_sys__Symlink(): test %d errno expect 0xdeadbeef, got %d\n", i+1, errno); - todo_wine_if(tests[i].is_todo) - ok(ret == tests[i].last_error, "tr2_sys__Symlink(): test %d expect: %d, got %d\n", i+1, tests[i].last_error, ret); + ok(ret == tests[i].last_error, "tr2_sys__Symlink(): test %d expect: %d, got %d\n", i+1, tests[i].last_error, ret); + if(ret == ERROR_SUCCESS) - ok(p_tr2_sys__File_size(tests[i].new_path) == 0, "tr2_sys__Symlink(): expect 0, got %s\n", wine_dbgstr_longlong(p_tr2_sys__File_size(tests[i].new_path))); + { + todo_wine_if(tests[i].is_todo) + ok(p_tr2_sys__File_size(tests[i].new_path) == 0, "tr2_sys__Symlink(): test %d expect: 0, got %s\n", i+1, wine_dbgstr_longlong(p_tr2_sys__File_size(tests[i].new_path))); + } } - ok(DeleteFileA("f1"), "expect f1 to exist\n"); - todo_wine ok(DeleteFileA("f1_link"), "expect f1_link to exist\n"); - todo_wine ok(DeleteFileA("tr2_test_dir/f1_link"), "expect tr2_test_dir/f1_link to exist\n"); - todo_wine ok(DeleteFileA("tr2_test_dir/f1_link_link"), "expect tr2_test_dir/f1_link_link to exist\n"); + ok(DeleteFileA("tr2_test_dir/f1_link_link"), "expect tr2_test_dir/f1_link_link to exist\n"); todo_wine ok(DeleteFileA("not_exist_link"), "expect not_exist_link to exist\n"); todo_wine ok(DeleteFileA("dir_link"), "expect dir_link to exist\n"); + ok(DeleteFileA("f1_link"), "expect f1_link to exist\n"); + ok(DeleteFileA("tr2_test_dir/f1_link"), "expect tr2_test_dir/f1_link to exist\n"); + ok(DeleteFileA("f1"), "expect f1 to exist\n"); ret = p_tr2_sys__Remove_dir("tr2_test_dir"); ok(ret == 1, "tr2_sys__Remove_dir(): expect 1 got %d\n", ret); } @@ -2011,15 +2014,14 @@ static void test_tr2_sys__Unlink(void) struct { char const *path; int last_error; - MSVCP_bool is_todo; } tests[] = { - { "tr2_test_dir\\f1_symlink", ERROR_SUCCESS, TRUE }, - { "tr2_test_dir\\f1_link", ERROR_SUCCESS, FALSE }, - { "tr2_test_dir\\f1", ERROR_SUCCESS, FALSE }, - { "tr2_test_dir", ERROR_ACCESS_DENIED, FALSE }, - { "not_exist", ERROR_FILE_NOT_FOUND, FALSE }, - { "not_exist_dir\\not_exist_file", ERROR_PATH_NOT_FOUND, FALSE }, - { NULL, ERROR_PATH_NOT_FOUND, FALSE } + { "tr2_test_dir\\f1_symlink", ERROR_SUCCESS }, + { "tr2_test_dir\\f1_link", ERROR_SUCCESS }, + { "tr2_test_dir\\f1", ERROR_SUCCESS }, + { "tr2_test_dir", ERROR_ACCESS_DENIED }, + { "not_exist", ERROR_FILE_NOT_FOUND }, + { "not_exist_dir\\not_exist_file", ERROR_PATH_NOT_FOUND }, + { NULL, ERROR_PATH_NOT_FOUND } }; GetCurrentDirectoryA(MAX_PATH, current_path); @@ -2048,8 +2050,7 @@ static void test_tr2_sys__Unlink(void) for(i=0; i