Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=51971 Signed-off-by: Robert Wilhelm robert.wilhelm@gmx.net --- Supersedes 220458.
v4: tests: Use ReadFile to check contents and minor cleanups as recommended by Nikolay. --- dlls/scrrun/filesystem.c | 16 ++++++- dlls/scrrun/tests/filesystem.c | 81 ++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 2 deletions(-)
diff --git a/dlls/scrrun/filesystem.c b/dlls/scrrun/filesystem.c index 359d86bb8fd..7b0b2e110a6 100644 --- a/dlls/scrrun/filesystem.c +++ b/dlls/scrrun/filesystem.c @@ -2518,9 +2518,21 @@ static HRESULT WINAPI folder_get_Files(IFolder *iface, IFileCollection **files) static HRESULT WINAPI folder_CreateTextFile(IFolder *iface, BSTR filename, VARIANT_BOOL overwrite, VARIANT_BOOL unicode, ITextStream **stream) { + DWORD disposition; + BSTR path; + HRESULT hres; + struct folder *This = impl_from_IFolder(iface); - FIXME("(%p)->(%s %x %x %p): stub\n", This, debugstr_w(filename), overwrite, unicode, stream); - return E_NOTIMPL; + + TRACE("%p %s %d %d %p\n", iface, debugstr_w(filename), overwrite, unicode, stream); + + hres = build_path(This->path, filename, &path); + if (FAILED(hres)) return hres; + + disposition = overwrite == VARIANT_TRUE ? CREATE_ALWAYS : CREATE_NEW; + hres = create_textstream(path, disposition, ForWriting, unicode ? TristateTrue : TristateFalse, stream); + SysFreeString(path); + return hres; }
static const IFolderVtbl foldervtbl = { diff --git a/dlls/scrrun/tests/filesystem.c b/dlls/scrrun/tests/filesystem.c index 5c55965ce88..91729bb047f 100644 --- a/dlls/scrrun/tests/filesystem.c +++ b/dlls/scrrun/tests/filesystem.c @@ -1535,6 +1535,86 @@ static void test_CreateTextFile(void) SysFreeString(nameW); }
+static void test_FolderCreateTextFile(void) +{ + WCHAR pathW[MAX_PATH], dirW[MAX_PATH], buffW[10], buff2W[10]; + ITextStream *stream; + BSTR nameW, str; + HANDLE file; + IFolder *folder; + HRESULT hr; + BOOL ret; + DWORD r; + + get_temp_filepath(testfileW, pathW, dirW); + nameW = SysAllocString(L"foo.txt"); + lstrcpyW(pathW, dirW); + lstrcatW(pathW, nameW); + + ret = CreateDirectoryW(dirW, NULL); + ok(ret, "got %d, %d\n", ret, GetLastError()); + + str = SysAllocString(dirW); + hr = IFileSystem3_GetFolder(fs3, str, &folder); + ok(ret, "got %d, %d\n", ret, GetLastError()); + SysFreeString(str); + + hr = IFolder_CreateTextFile(folder, nameW, VARIANT_FALSE, VARIANT_FALSE, &stream); + ok(hr == S_OK, "got 0x%08x\n", hr); + + test_provideclassinfo(stream, &CLSID_TextStream); + + hr = ITextStream_Read(stream, 1, &str); + ok(hr == CTL_E_BADFILEMODE, "got 0x%08x\n", hr); + + hr = ITextStream_Close(stream); + ok(hr == S_OK, "got 0x%08x\n", hr); + + hr = ITextStream_Read(stream, 1, &str); + ok(hr == CTL_E_BADFILEMODE || hr == E_VAR_NOT_SET, "got 0x%08x\n", hr); + + hr = ITextStream_Close(stream); + ok(hr == S_FALSE || hr == E_VAR_NOT_SET, "got 0x%08x\n", hr); + + ITextStream_Release(stream); + + /* check it's created */ + file = CreateFileW(pathW, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + ok(file != INVALID_HANDLE_VALUE, "got %p\n", file); + CloseHandle(file); + + /* try to create again with no-overwrite mode */ + hr = IFolder_CreateTextFile(folder, nameW, VARIANT_FALSE, VARIANT_FALSE, &stream); + ok(hr == CTL_E_FILEALREADYEXISTS, "got 0x%08x\n", hr); + + /* now overwrite */ + hr = IFolder_CreateTextFile(folder, nameW, VARIANT_TRUE, VARIANT_FALSE, &stream); + ok(hr == S_OK, "got 0x%08x\n", hr); + ITextStream_Release(stream); + + /* overwrite in Unicode mode, check for BOM */ + hr = IFolder_CreateTextFile(folder, nameW, VARIANT_TRUE, VARIANT_TRUE, &stream); + ok(hr == S_OK, "got 0x%08x\n", hr); + ITextStream_Release(stream); + + /* check contents */ + file = CreateFileW(pathW, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + ok(file != INVALID_HANDLE_VALUE, "got %p\n", file); + r = 0; + ret = ReadFile(file, buffW, sizeof(buffW), &r, NULL); + ok(ret && r==2, "read %d, got %d, %d\n", r, ret, GetLastError()); + buffW[r/sizeof(WCHAR)] = 0; + + buff2W[0] = 0xfeff; + buff2W[1] = 0; + ok(!lstrcmpW(buff2W, buffW), "got %s, expected %s\n", wine_dbgstr_w(buffW), wine_dbgstr_w(buff2W)); + CloseHandle(file); + + DeleteFileW(pathW); + RemoveDirectoryW(dirW); + SysFreeString(nameW); +} + static void test_WriteLine(void) { WCHAR pathW[MAX_PATH], dirW[MAX_PATH]; @@ -2516,6 +2596,7 @@ START_TEST(filesystem) test_FileCollection(); test_DriveCollection(); test_CreateTextFile(); + test_FolderCreateTextFile(); test_WriteLine(); test_ReadAll(); test_Read(); -- 2.31.1