boolByte removeDirectory(const CharString absolutePath) { boolByte result = false; #if UNIX if(!fileExists(absolutePath->data)) { return false; } // This is a bit lazy, perhaps... CharString removeCommand = newCharString(); snprintf(removeCommand->data, removeCommand->length, "/bin/rm -rf \"%s\"", absolutePath->data); result = system(removeCommand->data); freeCharString(removeCommand); return (result == 0); #elif WINDOWS SHFILEOPSTRUCTA fileOperation = {0}; fileOperation.wFunc = FO_DELETE; fileOperation.pFrom = absolutePath->data; fileOperation.fFlags = FOF_NO_UI; return (SHFileOperationA(&fileOperation) == 0); #else logUnsupportedFeature("Copy directory recursively"); return false; #endif }
bool Logger::DeleteDirectoryFile(char* lpszPath) { SHFILEOPSTRUCTA FileOp = { 0 }; FileOp.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION; FileOp.pFrom = lpszPath; FileOp.pTo = NULL; FileOp.wFunc = FO_DELETE; return SHFileOperationA(&FileOp) == 0; }
JNIEXPORT void JNICALL Java_org_gudy_azureus2_platform_win32_access_impl_AEWin32AccessInterface_moveToRecycleBinA( JNIEnv *env, jclass cla, jstring _fileName ) { char file_name[16000]; SHFILEOPSTRUCTA opFile; if ( !jstringToCharsA( env, _fileName, file_name, sizeof( file_name )-1)){ return; } HANDLE file = CreateFileA ( file_name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); // Checks if file exists if ( file == INVALID_HANDLE_VALUE ){ throwException( env, "moveToRecycleBin", "file not found" ); return; } CloseHandle(file); file_name[ strlen(file_name)+1 ] = 0; ZeroMemory(&opFile, sizeof(opFile)); opFile.wFunc = FO_DELETE; opFile.pFrom = file_name; opFile.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_SILENT; if (SHFileOperationA (&opFile)){ throwException( env, "moveToRecycleBin", "SHFileOperation failed" ); } }
//============================================================================== Error removeDirectory(const CString& dirname) { Error err = ErrorCode::NONE; SHFILEOPSTRUCTA fileOperation; fileOperation.wFunc = FO_DELETE; fileOperation.pFrom = dirname.get(); fileOperation.fFlags = FOF_NO_UI | FOF_NOCONFIRMATION; I result = SHFileOperationA(&fileOperation); if(result != 0) { ANKI_LOGE("Could not delete directory %s", dirname.get()); err = ErrorCode::FUNCTION_FAILED; } return err; }
//################################################################## // recursively deletes a folder and its subfolders and files // // @param lpszDir - [in] NUL terminated string which contains the path // to the folder to be deleted recursively // // @return value - TRUE if the operation succeeded //################################################################## BOOL DeleteDirectoryA(LPCSTR lpszDir) { size_t len = lstrlenA(lpszDir); CHAR *pszFrom = new CHAR[len+2]; lstrcpyA(pszFrom, lpszDir); pszFrom[len] = 0; pszFrom[len+1] = 0; // Append extra NUL SHFILEOPSTRUCTA fileop; fileop.hwnd = NULL; fileop.wFunc = FO_DELETE; fileop.pFrom = pszFrom; fileop.pTo = NULL; fileop.fFlags = FOF_NOCONFIRMATION|FOF_SILENT; fileop.fAnyOperationsAborted = FALSE; fileop.lpszProgressTitle = NULL; fileop.hNameMappings = NULL; int ret = SHFileOperationA(&fileop); delete [] pszFrom; return (ret == 0); }
void UpdateDownloader::CleanLeftovers() { // Note: this is called at startup. Do not use wxWidgets from this code! std::string tmpdir; if ( !Settings::ReadConfigValue("UpdateTempDir", tmpdir) ) return; tmpdir.append(1, '\0'); // double NULL-terminate for SHFileOperation SHFILEOPSTRUCTA fos = {0}; fos.wFunc = FO_DELETE; fos.pFrom = tmpdir.c_str(); fos.fFlags = FOF_NO_UI | // Vista+-only FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOERRORUI; if ( SHFileOperationA(&fos) == 0 ) { Settings::DeleteConfigValue("UpdateTempDir"); } // else: try another time, this is just a "soft" error }
/* * Tests if clicking the "Make New Folder" button in a SHBrowseForFolder * dialog box creates a new folder. (Bug 17986). * * Here follows a description of what happens on W2K,Vista, W2K8, W7: * When the "Make New Folder" button is clicked a new folder is created and * inserted into the tree. The folder is given a default name that depends on * the locale (e.g. "New Folder"). The folder name is selected and the dialog * waits for the user to type in a new name. The folder is renamed when the user * types in a name and presses enter. * * Note that XP and W2K3 do not select the folder name or wait for the user * to type in a new folder name. This behavior is considered broken as most * users would like to give the folder a name after creating it. The fact that * it originally waited for the user to type in a new folder name(W2K), and then * again was changed back wait for the new folder name(Vista, W2K8, W7), * indicates that MS also believes that it was broken in XP and W2K3. */ static void test_click_make_new_folder_button(void) { HRESULT resCoInit, hr; BROWSEINFOA bi; LPITEMIDLIST pidl = NULL; LPITEMIDLIST test_folder_pidl; IShellFolder *test_folder_object; char test_folder_path[MAX_PATH]; WCHAR test_folder_pathW[MAX_PATH]; CHAR new_folder_path[MAX_PATH]; CHAR new_folder_pidl_path[MAX_PATH]; char selected_folder[MAX_PATH]; const CHAR title[] = "test_click_make_new_folder_button"; int number_of_folders = -1; SHFILEOPSTRUCTA shfileop; if (does_folder_or_file_exist(title)) { skip("The test folder already exists.\n"); return; } /* Must initialize COM if using the NEWDIAlOGSTYLE according to MSDN. */ resCoInit = CoInitialize(NULL); if(!(resCoInit == S_OK || resCoInit == S_FALSE)) { skip("COM could not be initialized %u\n", GetLastError()); return; } /* Leave room for concatenating title, two backslashes, and an extra NULL. */ if (!GetCurrentDirectoryA(MAX_PATH-strlen(title)-3, test_folder_path)) { skip("GetCurrentDirectoryA failed %u\n", GetLastError()); } strncat(test_folder_path, "\\", 1); strncat(test_folder_path, title, MAX_PATH-1); strncat(test_folder_path, "\\", 1); /* Avoid conflicts by creating a test folder. */ if (!CreateDirectoryA(title, NULL)) { skip("CreateDirectoryA failed %u\n", GetLastError()); return; } /* Initialize browse info struct for SHBrowseForFolder */ bi.hwndOwner = NULL; bi.pszDisplayName = selected_folder; bi.lpszTitle = title; bi.ulFlags = BIF_NEWDIALOGSTYLE; bi.lpfn = create_new_folder_callback; /* Use test folder as the root folder for dialog box */ MultiByteToWideChar(CP_UTF8, 0, test_folder_path, -1, test_folder_pathW, MAX_PATH); hr = SHGetDesktopFolder(&test_folder_object); ok (SUCCEEDED(hr), "SHGetDesktopFolder failed with hr 0x%08x\n", hr); if (FAILED(hr)) { skip("SHGetDesktopFolder failed - skipping\n"); return; } test_folder_object->lpVtbl->ParseDisplayName(test_folder_object, NULL, NULL, test_folder_pathW, 0UL, &test_folder_pidl, 0UL); bi.pidlRoot = test_folder_pidl; /* Display dialog box and let callback click the buttons */ pidl = SHBrowseForFolderA(&bi); number_of_folders = get_number_of_folders(test_folder_path); ok(number_of_folders == 1 || broken(number_of_folders == 0) /* W95, W98 */, "Clicking \"Make New Folder\" button did not result in a new folder.\n"); /* There should be a new folder foo inside the test folder */ strcpy(new_folder_path, test_folder_path); strcat(new_folder_path, new_folder_name); ok(does_folder_or_file_exist(new_folder_path) || broken(!does_folder_or_file_exist(new_folder_path)) /* W95, W98, XP, W2K3 */, "The new folder did not get the name %s\n", new_folder_name); /* Dialog should return a pidl pointing to the new folder */ ok(SHGetPathFromIDListA(pidl, new_folder_pidl_path), "SHGetPathFromIDList failed for new folder.\n"); ok(strcmp(new_folder_path, new_folder_pidl_path) == 0 || broken(strcmp(new_folder_path, new_folder_pidl_path) != 0) /* earlier than Vista */, "SHBrowseForFolder did not return the pidl for the new folder. " "Expected '%s' got '%s'\n", new_folder_path, new_folder_pidl_path); /* Remove test folder and any subfolders created in this test */ shfileop.hwnd = NULL; shfileop.wFunc = FO_DELETE; /* Path must be double NULL terminated */ test_folder_path[strlen(test_folder_path)+1] = '\0'; shfileop.pFrom = test_folder_path; shfileop.pTo = NULL; shfileop.fFlags = FOF_NOCONFIRMATION|FOF_NOERRORUI|FOF_SILENT; SHFileOperationA(&shfileop); if (pidl) CoTaskMemFree(pidl); if (test_folder_pidl) CoTaskMemFree(test_folder_pidl); test_folder_object->lpVtbl->Release(test_folder_object); CoUninitialize(); }
/* tests the FO_MOVE action */ static void test_move(void) { SHFILEOPSTRUCTA shfo, shfo2; CHAR from[MAX_PATH]; CHAR to[MAX_PATH]; DWORD retval; shfo.hwnd = NULL; shfo.wFunc = FO_MOVE; shfo.pFrom = from; shfo.pTo = to; shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI; shfo.hNameMappings = NULL; shfo.lpszProgressTitle = NULL; set_curr_dir_path(from, "test1.txt\0"); set_curr_dir_path(to, "test4.txt\0"); ok(!SHFileOperationA(&shfo), "Prepare test to check how directories are moved recursively\n"); ok(!file_exists("test1.txt"), "test1.txt should not exist\n"); ok(file_exists("test4.txt\\test1.txt"), "The file is not moved\n"); set_curr_dir_path(from, "test?.txt\0"); set_curr_dir_path(to, "testdir2\0"); ok(!file_exists("testdir2\\test2.txt"), "The file is not moved yet\n"); ok(!file_exists("testdir2\\test4.txt"), "The directory is not moved yet\n"); ok(!SHFileOperationA(&shfo), "Files and directories are moved to directory\n"); ok(file_exists("testdir2\\test2.txt"), "The file is moved\n"); ok(file_exists("testdir2\\test4.txt"), "The directory is moved\n"); ok(file_exists("testdir2\\test4.txt\\test1.txt"), "The file in subdirectory is moved\n"); clean_after_shfo_tests(); init_shfo_tests(); memcpy(&shfo2, &shfo, sizeof(SHFILEOPSTRUCTA)); shfo2.fFlags |= FOF_MULTIDESTFILES; set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0"); set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0"); ok(!SHFileOperationA(&shfo2), "Move many files\n"); ok(file_exists("test6.txt"), "The file is moved - many files are " "specified as a target\n"); DeleteFileA("test6.txt"); DeleteFileA("test7.txt"); RemoveDirectoryA("test8.txt"); init_shfo_tests(); /* number of sources do not correspond to number of targets */ set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0"); set_curr_dir_path(to, "test6.txt\0test7.txt\0"); ok(SHFileOperationA(&shfo2), "Can't move many files\n"); ok(!file_exists("test6.txt"), "The file is not moved - many files are " "specified as a target\n"); init_shfo_tests(); set_curr_dir_path(from, "test3.txt\0"); set_curr_dir_path(to, "test4.txt\\test1.txt\0"); ok(!SHFileOperationA(&shfo), "File is moved moving to other directory\n"); ok(file_exists("test4.txt\\test1.txt"), "The file is moved\n"); set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0"); set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0"); ok(SHFileOperationA(&shfo), "Cannot move many files\n"); ok(file_exists("test1.txt"), "The file is not moved. Many files are specified\n"); ok(file_exists("test4.txt"), "The directory is not moved. Many files are specified\n"); set_curr_dir_path(from, "test1.txt\0"); set_curr_dir_path(to, "test6.txt\0"); ok(!SHFileOperationA(&shfo), "Move file\n"); ok(!file_exists("test1.txt"), "The file is moved\n"); ok(file_exists("test6.txt"), "The file is moved\n"); set_curr_dir_path(from, "test6.txt\0"); set_curr_dir_path(to, "test1.txt\0"); ok(!SHFileOperationA(&shfo), "Move file back\n"); set_curr_dir_path(from, "test4.txt\0"); set_curr_dir_path(to, "test6.txt\0"); ok(!SHFileOperationA(&shfo), "Move dir\n"); ok(!file_exists("test4.txt"), "The dir is moved\n"); ok(file_exists("test6.txt"), "The dir is moved\n"); set_curr_dir_path(from, "test6.txt\0"); set_curr_dir_path(to, "test4.txt\0"); ok(!SHFileOperationA(&shfo), "Move dir back\n"); /* move one file to two others */ init_shfo_tests(); shfo.pFrom = "test1.txt\0"; shfo.pTo = "a.txt\0b.txt\0"; retval = SHFileOperationA(&shfo); ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %ld\n", retval); ok(!file_exists("test1.txt"), "Expected test1.txt to not exist\n"); ok(DeleteFile("a.txt"), "Expected a.txt to exist\n"); ok(!file_exists("b.txt"), "Expected b.txt to not exist\n"); /* move two files to one other */ shfo.pFrom = "test2.txt\0test3.txt\0"; shfo.pTo = "test1.txt\0"; retval = SHFileOperationA(&shfo); ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %ld\n", retval); ok(!file_exists("test1.txt"), "Expected test1.txt to not exist\n"); ok(file_exists("test2.txt"), "Expected test2.txt to exist\n"); ok(file_exists("test3.txt"), "Expected test3.txt to exist\n"); /* move a directory into itself */ shfo.pFrom = "test4.txt\0"; shfo.pTo = "test4.txt\\b.txt\0"; retval = SHFileOperationA(&shfo); ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %ld\n", retval); ok(!RemoveDirectory("test4.txt\\b.txt"), "Expected test4.txt\\b.txt to not exist\n"); ok(file_exists("test4.txt"), "Expected test4.txt to exist\n"); /* move many files without FOF_MULTIDESTFILES */ shfo.pFrom = "test2.txt\0test3.txt\0"; shfo.pTo = "d.txt\0e.txt\0"; retval = SHFileOperationA(&shfo); ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %ld\n", retval); ok(!DeleteFile("d.txt"), "Expected d.txt to not exist\n"); ok(!DeleteFile("e.txt"), "Expected e.txt to not exist\n"); /* number of sources != number of targets */ shfo.pTo = "d.txt\0"; shfo.fFlags |= FOF_MULTIDESTFILES; retval = SHFileOperationA(&shfo); ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %ld\n", retval); ok(!DeleteFile("d.txt"), "Expected d.txt to not exist\n"); /* FO_MOVE does not create dest directories */ shfo.pFrom = "test2.txt\0"; shfo.pTo = "dir1\\dir2\\test2.txt\0"; retval = SHFileOperationA(&shfo); ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %ld\n", retval); ok(!file_exists("dir1"), "Expected dir1 to not exist\n"); /* try to overwrite an existing file */ shfo.pTo = "test3.txt\0"; retval = SHFileOperationA(&shfo); ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %ld\n", retval); ok(!file_exists("test2.txt"), "Expected test2.txt to not exist\n"); ok(file_exists("test3.txt"), "Expected test3.txt to exist\n"); }
/* tests the FO_COPY action */ static void test_copy(void) { SHFILEOPSTRUCTA shfo, shfo2; CHAR from[MAX_PATH]; CHAR to[MAX_PATH]; FILEOP_FLAGS tmp_flags; DWORD retval; shfo.hwnd = NULL; shfo.wFunc = FO_COPY; shfo.pFrom = from; shfo.pTo = to; shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI; shfo.hNameMappings = NULL; shfo.lpszProgressTitle = NULL; set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0"); set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0"); ok(SHFileOperationA(&shfo), "Can't copy many files\n"); ok(!file_exists("test6.txt"), "The file is not copied - many files are " "specified as a target\n"); memcpy(&shfo2, &shfo, sizeof(SHFILEOPSTRUCTA)); shfo2.fFlags |= FOF_MULTIDESTFILES; set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0"); set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0"); ok(!SHFileOperationA(&shfo2), "Can't copy many files\n"); ok(file_exists("test6.txt"), "The file is copied - many files are " "specified as a target\n"); DeleteFileA("test6.txt"); DeleteFileA("test7.txt"); RemoveDirectoryA("test8.txt"); /* number of sources do not correspond to number of targets */ set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0"); set_curr_dir_path(to, "test6.txt\0test7.txt\0"); ok(SHFileOperationA(&shfo2), "Can't copy many files\n"); ok(!file_exists("test6.txt"), "The file is not copied - many files are " "specified as a target\n"); set_curr_dir_path(from, "test1.txt\0"); set_curr_dir_path(to, "test4.txt\0"); ok(!SHFileOperationA(&shfo), "Prepare test to check how directories are copied recursively\n"); ok(file_exists("test4.txt\\test1.txt"), "The file is copied\n"); set_curr_dir_path(from, "test?.txt\0"); set_curr_dir_path(to, "testdir2\0"); ok(!file_exists("testdir2\\test1.txt"), "The file is not copied yet\n"); ok(!file_exists("testdir2\\test4.txt"), "The directory is not copied yet\n"); ok(!SHFileOperationA(&shfo), "Files and directories are copied to directory\n"); ok(file_exists("testdir2\\test1.txt"), "The file is copied\n"); ok(file_exists("testdir2\\test4.txt"), "The directory is copied\n"); ok(file_exists("testdir2\\test4.txt\\test1.txt"), "The file in subdirectory is copied\n"); clean_after_shfo_tests(); init_shfo_tests(); shfo.fFlags |= FOF_FILESONLY; ok(!file_exists("testdir2\\test1.txt"), "The file is not copied yet\n"); ok(!file_exists("testdir2\\test4.txt"), "The directory is not copied yet\n"); ok(!SHFileOperationA(&shfo), "Files are copied to other directory\n"); ok(file_exists("testdir2\\test1.txt"), "The file is copied\n"); ok(!file_exists("testdir2\\test4.txt"), "The directory is copied\n"); clean_after_shfo_tests(); init_shfo_tests(); set_curr_dir_path(from, "test1.txt\0test2.txt\0"); ok(!file_exists("testdir2\\test1.txt"), "The file is not copied yet\n"); ok(!file_exists("testdir2\\test2.txt"), "The file is not copied yet\n"); ok(!SHFileOperationA(&shfo), "Files are copied to other directory\n"); ok(file_exists("testdir2\\test1.txt"), "The file is copied\n"); ok(file_exists("testdir2\\test2.txt"), "The file is copied\n"); clean_after_shfo_tests(); /* Copying multiple files with one not existing as source, fails the entire operation in Win98/ME/2K/XP, but not in 95/NT */ init_shfo_tests(); tmp_flags = shfo.fFlags; set_curr_dir_path(from, "test1.txt\0test10.txt\0test2.txt\0"); ok(!file_exists("testdir2\\test1.txt"), "The file is not copied yet\n"); ok(!file_exists("testdir2\\test2.txt"), "The file is not copied yet\n"); retval = SHFileOperationA(&shfo); if (!retval) /* Win 95/NT returns success but copies only the files up to the nonexistent source */ ok(file_exists("testdir2\\test1.txt"), "The file is not copied\n"); else { /* Win 98/ME/2K/XP fail the entire operation with return code 1026 if one source file does not exist */ ok(retval == 1026, "Files are copied to other directory\n"); ok(!file_exists("testdir2\\test1.txt"), "The file is copied\n"); } ok(!file_exists("testdir2\\test2.txt"), "The file is copied\n"); shfo.fFlags = tmp_flags; /* copy into a nonexistent directory */ init_shfo_tests(); shfo.fFlags = FOF_NOCONFIRMMKDIR; set_curr_dir_path(from, "test1.txt\0"); set_curr_dir_path(to, "nonexistent\\notreal\\test2.txt\0"); retval= SHFileOperation(&shfo); ok(!retval, "Error copying into nonexistent directory\n"); ok(file_exists("nonexistent"), "nonexistent not created\n"); ok(file_exists("nonexistent\\notreal"), "nonexistent\\notreal not created\n"); ok(file_exists("nonexistent\\notreal\\test2.txt"), "Directory not created\n"); ok(!file_exists("nonexistent\\notreal\\test1.txt"), "test1.txt should not exist\n"); /* a relative dest directory is OK */ clean_after_shfo_tests(); init_shfo_tests(); shfo.pFrom = "test1.txt\0test2.txt\0test3.txt\0"; shfo.pTo = "testdir2\0"; retval = SHFileOperation(&shfo); ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %ld\n", retval); ok(file_exists("testdir2\\test1.txt"), "Expected testdir2\\test1 to exist\n"); /* try to copy files to a file */ clean_after_shfo_tests(); init_shfo_tests(); shfo.pFrom = from; shfo.pTo = to; set_curr_dir_path(from, "test1.txt\0test2.txt\0"); set_curr_dir_path(to, "test3.txt\0"); retval = SHFileOperation(&shfo); ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %ld\n", retval); ok(shfo.fAnyOperationsAborted, "Expected aborted operations\n"); ok(!file_exists("test3.txt\\test2.txt"), "Expected test3.txt\\test2.txt to not exist\n"); /* try to copy many files to nonexistent directory */ DeleteFile(to); shfo.fAnyOperationsAborted = FALSE; retval = SHFileOperation(&shfo); ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %ld\n", retval); ok(DeleteFile("test3.txt\\test1.txt"), "Expected test3.txt\\test1.txt to exist\n"); ok(DeleteFile("test3.txt\\test2.txt"), "Expected test3.txt\\test1.txt to exist\n"); ok(RemoveDirectory(to), "Expected test3.txt to exist\n"); /* send in FOF_MULTIDESTFILES with too many destination files */ init_shfo_tests(); shfo.pFrom = "test1.txt\0test2.txt\0test3.txt\0"; shfo.pTo = "testdir2\\a.txt\0testdir2\\b.txt\0testdir2\\c.txt\0testdir2\\d.txt\0"; shfo.fFlags |= FOF_NOERRORUI | FOF_MULTIDESTFILES; retval = SHFileOperation(&shfo); ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %ld\n", retval); ok(shfo.fAnyOperationsAborted, "Expected aborted operations\n"); ok(!file_exists("testdir2\\a.txt"), "Expected testdir2\\a.txt to not exist\n"); /* send in FOF_MULTIDESTFILES with too many destination files */ shfo.pFrom = "test1.txt\0test2.txt\0test3.txt\0"; shfo.pTo = "e.txt\0f.txt\0"; shfo.fAnyOperationsAborted = FALSE; retval = SHFileOperation(&shfo); ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %ld\n", retval); ok(shfo.fAnyOperationsAborted, "Expected aborted operations\n"); ok(!file_exists("e.txt"), "Expected e.txt to not exist\n"); /* use FOF_MULTIDESTFILES with files and a source directory */ shfo.pFrom = "test1.txt\0test2.txt\0test4.txt\0"; shfo.pTo = "testdir2\\a.txt\0testdir2\\b.txt\0testdir2\\c.txt\0"; shfo.fAnyOperationsAborted = FALSE; retval = SHFileOperation(&shfo); ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %ld\n", retval); ok(DeleteFile("testdir2\\a.txt"), "Expected testdir2\\a.txt to exist\n"); ok(DeleteFile("testdir2\\b.txt"), "Expected testdir2\\b.txt to exist\n"); ok(RemoveDirectory("testdir2\\c.txt"), "Expected testdir2\\c.txt to exist\n"); /* try many dest files without FOF_MULTIDESTFILES flag */ shfo.pFrom = "test1.txt\0test2.txt\0test3.txt\0"; shfo.pTo = "a.txt\0b.txt\0c.txt\0"; shfo.fAnyOperationsAborted = FALSE; shfo.fFlags &= ~FOF_MULTIDESTFILES; retval = SHFileOperation(&shfo); ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %ld\n", retval); ok(!file_exists("a.txt"), "Expected a.txt to not exist\n"); /* try a glob */ shfo.pFrom = "test?.txt\0"; shfo.pTo = "testdir2\0"; shfo.fFlags &= ~FOF_MULTIDESTFILES; retval = SHFileOperation(&shfo); ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %ld\n", retval); ok(file_exists("testdir2\\test1.txt"), "Expected testdir2\\test1.txt to exist\n"); /* try a glob with FOF_FILESONLY */ clean_after_shfo_tests(); init_shfo_tests(); shfo.pFrom = "test?.txt\0"; shfo.fFlags |= FOF_FILESONLY; retval = SHFileOperation(&shfo); ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %ld\n", retval); ok(file_exists("testdir2\\test1.txt"), "Expected testdir2\\test1.txt to exist\n"); ok(!file_exists("testdir2\\test4.txt"), "Expected testdir2\\test4.txt to not exist\n"); /* try a glob with FOF_MULTIDESTFILES and the same number * of dest files that we would expect */ clean_after_shfo_tests(); init_shfo_tests(); shfo.pTo = "testdir2\\a.txt\0testdir2\\b.txt\0testdir2\\c.txt\0testdir2\\d.txt\0"; shfo.fFlags &= ~FOF_FILESONLY; shfo.fFlags |= FOF_MULTIDESTFILES; retval = SHFileOperation(&shfo); ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %ld\n", retval); ok(shfo.fAnyOperationsAborted, "Expected aborted operations\n"); ok(!file_exists("testdir2\\a.txt"), "Expected testdir2\\test1.txt to not exist\n"); ok(!RemoveDirectory("b.txt"), "b.txt should not exist\n"); /* copy one file to two others, second is ignored */ clean_after_shfo_tests(); init_shfo_tests(); shfo.pFrom = "test1.txt\0"; shfo.pTo = "b.txt\0c.txt\0"; shfo.fAnyOperationsAborted = FALSE; retval = SHFileOperation(&shfo); ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %ld\n", retval); ok(DeleteFile("b.txt"), "Expected b.txt to exist\n"); ok(!DeleteFile("c.txt"), "Expected c.txt to not exist\n"); /* copy two file to three others, all fail */ shfo.pFrom = "test1.txt\0test2.txt\0"; shfo.pTo = "b.txt\0c.txt\0d.txt\0"; retval = SHFileOperation(&shfo); ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %ld\n", retval); ok(shfo.fAnyOperationsAborted, "Expected operations to be aborted\n"); ok(!DeleteFile("b.txt"), "Expected b.txt to not exist\n"); /* copy one file and one directory to three others */ shfo.pFrom = "test1.txt\0test4.txt\0"; shfo.pTo = "b.txt\0c.txt\0d.txt\0"; shfo.fAnyOperationsAborted = FALSE; retval = SHFileOperation(&shfo); ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %ld\n", retval); ok(shfo.fAnyOperationsAborted, "Expected operations to be aborted\n"); ok(!DeleteFile("b.txt"), "Expected b.txt to not exist\n"); ok(!DeleteFile("c.txt"), "Expected c.txt to not exist\n"); /* copy a directory with a file beneath it, plus some files */ createTestFile("test4.txt\\a.txt"); shfo.pFrom = "test4.txt\0test1.txt\0"; shfo.pTo = "testdir2\0"; shfo.fFlags &= ~FOF_MULTIDESTFILES; shfo.fAnyOperationsAborted = FALSE; retval = SHFileOperation(&shfo); ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %ld\n", retval); ok(DeleteFile("testdir2\\test1.txt"), "Expected newdir\\test1.txt to exist\n"); ok(DeleteFile("testdir2\\test4.txt\\a.txt"), "Expected a.txt to exist\n"); ok(RemoveDirectory("testdir2\\test4.txt"), "Expected testdir2\\test4.txt to exist\n"); /* copy one directory and a file in that dir to another dir */ shfo.pFrom = "test4.txt\0test4.txt\\a.txt\0"; shfo.pTo = "testdir2\0"; retval = SHFileOperation(&shfo); ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %ld\n", retval); ok(DeleteFile("testdir2\\test4.txt\\a.txt"), "Expected a.txt to exist\n"); ok(DeleteFile("testdir2\\a.txt"), "Expected testdir2\\a.txt to exist\n"); /* copy a file in a directory first, and then the directory to a nonexistent dir */ shfo.pFrom = "test4.txt\\a.txt\0test4.txt\0"; shfo.pTo = "nonexistent\0"; retval = SHFileOperation(&shfo); ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %ld\n", retval); ok(shfo.fAnyOperationsAborted, "Expected operations to be aborted\n"); ok(!file_exists("nonexistent\\test4.txt"), "Expected nonexistent\\test4.txt to not exist\n"); DeleteFile("test4.txt\\a.txt"); /* destination is same as source file */ shfo.pFrom = "test1.txt\0test2.txt\0test3.txt\0"; shfo.pTo = "b.txt\0test2.txt\0c.txt\0"; shfo.fAnyOperationsAborted = FALSE; shfo.fFlags = FOF_NOERRORUI | FOF_MULTIDESTFILES; retval = SHFileOperation(&shfo); ok(retval == ERROR_NO_MORE_SEARCH_HANDLES, "Expected ERROR_NO_MORE_SEARCH_HANDLES, got %ld\n", retval); ok(!shfo.fAnyOperationsAborted, "Expected no operations to be aborted\n"); ok(DeleteFile("b.txt"), "Expected b.txt to exist\n"); ok(!file_exists("c.txt"), "Expected c.txt to not exist\n"); /* destination is same as source directory */ shfo.pFrom = "test1.txt\0test4.txt\0test3.txt\0"; shfo.pTo = "b.txt\0test4.txt\0c.txt\0"; shfo.fAnyOperationsAborted = FALSE; retval = SHFileOperation(&shfo); ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %ld\n", retval); ok(DeleteFile("b.txt"), "Expected b.txt to exist\n"); ok(!file_exists("c.txt"), "Expected c.txt to not exist\n"); /* copy a directory into itself, error displayed in UI */ shfo.pFrom = "test4.txt\0"; shfo.pTo = "test4.txt\\newdir\0"; shfo.fFlags &= ~FOF_MULTIDESTFILES; shfo.fAnyOperationsAborted = FALSE; retval = SHFileOperation(&shfo); ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %ld\n", retval); ok(!RemoveDirectory("test4.txt\\newdir"), "Expected test4.txt\\newdir to not exist\n"); /* copy a directory to itself, error displayed in UI */ shfo.pFrom = "test4.txt\0"; shfo.pTo = "test4.txt\0"; shfo.fAnyOperationsAborted = FALSE; retval = SHFileOperation(&shfo); ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %ld\n", retval); /* copy a file into a directory, and the directory into itself */ shfo.pFrom = "test1.txt\0test4.txt\0"; shfo.pTo = "test4.txt\0"; shfo.fAnyOperationsAborted = FALSE; shfo.fFlags |= FOF_NOCONFIRMATION; retval = SHFileOperation(&shfo); ok(retval == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %ld\n", retval); ok(DeleteFile("test4.txt\\test1.txt"), "Expected test4.txt\\test1.txt to exist\n"); /* copy a file to a file, and the directory into itself */ shfo.pFrom = "test1.txt\0test4.txt\0"; shfo.pTo = "test4.txt\\a.txt\0"; shfo.fAnyOperationsAborted = FALSE; retval = SHFileOperation(&shfo); ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %ld\n", retval); ok(!file_exists("test4.txt\\a.txt"), "Expected test4.txt\\a.txt to not exist\n"); /* copy a nonexistent file to a nonexistent directory */ shfo.pFrom = "e.txt\0"; shfo.pTo = "nonexistent\0"; shfo.fAnyOperationsAborted = FALSE; retval = SHFileOperation(&shfo); ok(retval == 1026, "Expected 1026, got %ld\n", retval); ok(!file_exists("nonexistent\\e.txt"), "Expected nonexistent\\e.txt to not exist\n"); ok(!file_exists("nonexistent"), "Expected nonexistent to not exist\n"); }
/* tests the FO_RENAME action */ static void test_rename(void) { SHFILEOPSTRUCTA shfo, shfo2; CHAR from[MAX_PATH]; CHAR to[MAX_PATH]; DWORD retval; shfo.hwnd = NULL; shfo.wFunc = FO_RENAME; shfo.pFrom = from; shfo.pTo = to; shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI; shfo.hNameMappings = NULL; shfo.lpszProgressTitle = NULL; set_curr_dir_path(from, "test1.txt\0"); set_curr_dir_path(to, "test4.txt\0"); ok(SHFileOperationA(&shfo), "File is not renamed moving to other directory " "when specifying directory name only\n"); ok(file_exists("test1.txt"), "The file is removed\n"); set_curr_dir_path(from, "test3.txt\0"); set_curr_dir_path(to, "test4.txt\\test1.txt\0"); ok(!SHFileOperationA(&shfo), "File is renamed moving to other directory\n"); ok(file_exists("test4.txt\\test1.txt"), "The file is not renamed\n"); set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0"); set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0"); retval = SHFileOperationA(&shfo); /* W98 returns 0, W2K and newer returns ERROR_GEN_FAILURE, both do nothing */ ok(!retval || retval == ERROR_GEN_FAILURE || retval == ERROR_INVALID_TARGET_HANDLE, "Can't rename many files, retval = %ld\n", retval); ok(file_exists("test1.txt"), "The file is renamed - many files are specified\n"); memcpy(&shfo2, &shfo, sizeof(SHFILEOPSTRUCTA)); shfo2.fFlags |= FOF_MULTIDESTFILES; set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0"); set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0"); retval = SHFileOperationA(&shfo2); /* W98 returns 0, W2K and newer returns ERROR_GEN_FAILURE, both do nothing */ ok(!retval || retval == ERROR_GEN_FAILURE || retval == ERROR_INVALID_TARGET_HANDLE, "Can't rename many files, retval = %ld\n", retval); ok(file_exists("test1.txt"), "The file is not renamed - many files are specified\n"); set_curr_dir_path(from, "test1.txt\0"); set_curr_dir_path(to, "test6.txt\0"); retval = SHFileOperationA(&shfo); ok(!retval, "Rename file failed, retval = %ld\n", retval); ok(!file_exists("test1.txt"), "The file is not renamed\n"); ok(file_exists("test6.txt"), "The file is not renamed\n"); set_curr_dir_path(from, "test6.txt\0"); set_curr_dir_path(to, "test1.txt\0"); retval = SHFileOperationA(&shfo); ok(!retval, "Rename file back failed, retval = %ld\n", retval); set_curr_dir_path(from, "test4.txt\0"); set_curr_dir_path(to, "test6.txt\0"); retval = SHFileOperationA(&shfo); ok(!retval, "Rename dir failed, retval = %ld\n", retval); ok(!file_exists("test4.txt"), "The dir is not renamed\n"); ok(file_exists("test6.txt"), "The dir is not renamed\n"); set_curr_dir_path(from, "test6.txt\0"); set_curr_dir_path(to, "test4.txt\0"); retval = SHFileOperationA(&shfo); ok(!retval, "Rename dir back failed, retval = %ld\n", retval); /* try to rename more than one file to a single file */ shfo.pFrom = "test1.txt\0test2.txt\0"; shfo.pTo = "a.txt\0"; retval = SHFileOperationA(&shfo); ok(retval == ERROR_GEN_FAILURE, "Expected ERROR_GEN_FAILURE, got %ld\n", retval); ok(file_exists("test1.txt"), "Expected test1.txt to exist\n"); ok(file_exists("test2.txt"), "Expected test2.txt to exist\n"); /* pFrom doesn't exist */ shfo.pFrom = "idontexist\0"; shfo.pTo = "newfile\0"; retval = SHFileOperationA(&shfo); ok(retval == 1026, "Expected 1026, got %ld\n", retval); ok(!file_exists("newfile"), "Expected newfile to not exist\n"); /* pTo already exist */ shfo.pFrom = "test1.txt\0"; shfo.pTo = "test2.txt\0"; retval = SHFileOperationA(&shfo); ok(retval == ERROR_ALREADY_EXISTS, "Expected ERROR_ALREADY_EXISTS, got %ld\n", retval); /* pFrom is valid, but pTo is empty */ shfo.pFrom = "test1.txt\0"; shfo.pTo = "\0"; retval = SHFileOperationA(&shfo); ok(retval == ERROR_CANCELLED, "Expected ERROR_CANCELLED, got %ld\n", retval); ok(file_exists("test1.txt"), "Expected test1.txt to exist\n"); /* pFrom is empty */ shfo.pFrom = "\0"; retval = SHFileOperationA(&shfo); ok(retval == ERROR_ACCESS_DENIED, "Expected ERROR_ACCESS_DENIED, got %ld\n", retval); /* pFrom is NULL, commented out because it crashes on nt 4.0 */ #if 0 shfo.pFrom = NULL; retval = SHFileOperationA(&shfo); ok(retval == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %ld\n", retval); #endif }
/* tests the FO_DELETE action */ static void test_delete(void) { SHFILEOPSTRUCTA shfo; DWORD ret; CHAR buf[MAX_PATH]; sprintf(buf, "%s\\%s", CURR_DIR, "test?.txt"); buf[strlen(buf) + 1] = '\0'; shfo.hwnd = NULL; shfo.wFunc = FO_DELETE; shfo.pFrom = buf; shfo.pTo = "\0"; shfo.fFlags = FOF_FILESONLY | FOF_NOCONFIRMATION | FOF_SILENT; shfo.hNameMappings = NULL; shfo.lpszProgressTitle = NULL; ok(!SHFileOperationA(&shfo), "Deletion was successful\n"); ok(file_exists("test4.txt"), "Directory should not be removed\n"); ok(!file_exists("test1.txt"), "File should be removed\n"); ret = SHFileOperationA(&shfo); ok(!ret, "Directory exists, but is not removed, ret=%ld\n", ret); ok(file_exists("test4.txt"), "Directory should not be removed\n"); shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI; ok(!SHFileOperationA(&shfo), "Directory removed\n"); ok(!file_exists("test4.txt"), "Directory should be removed\n"); ret = SHFileOperationA(&shfo); ok(!ret, "The requested file does not exist, ret=%ld\n", ret); init_shfo_tests(); sprintf(buf, "%s\\%s", CURR_DIR, "test4.txt"); buf[strlen(buf) + 1] = '\0'; ok(MoveFileA("test1.txt", "test4.txt\\test1.txt"), "Fill the subdirectory\n"); ok(!SHFileOperationA(&shfo), "Directory removed\n"); ok(!file_exists("test4.txt"), "Directory is removed\n"); init_shfo_tests(); shfo.pFrom = "test1.txt\0test4.txt\0"; ok(!SHFileOperationA(&shfo), "Directory and a file removed\n"); ok(!file_exists("test1.txt"), "The file should be removed\n"); ok(!file_exists("test4.txt"), "Directory should be removed\n"); ok(file_exists("test2.txt"), "This file should not be removed\n"); /* FOF_FILESONLY does not delete a dir matching a wildcard */ init_shfo_tests(); shfo.fFlags |= FOF_FILESONLY; shfo.pFrom = "*.txt\0"; ok(!SHFileOperation(&shfo), "Failed to delete files\n"); ok(!file_exists("test1.txt"), "test1.txt should be removed\n"); ok(!file_exists("test_5.txt"), "test_5.txt should be removed\n"); ok(file_exists("test4.txt"), "test4.txt should not be removed\n"); /* FOF_FILESONLY only deletes a dir if explicitly specified */ init_shfo_tests(); shfo.pFrom = "test_?.txt\0test4.txt\0"; ok(!SHFileOperation(&shfo), "Failed to delete files\n"); ok(!file_exists("test4.txt"), "test4.txt should be removed\n"); ok(!file_exists("test_5.txt"), "test_5.txt should be removed\n"); ok(file_exists("test1.txt"), "test1.txt should not be removed\n"); /* try to delete an invalid filename */ init_shfo_tests(); shfo.pFrom = "\0"; shfo.fFlags &= ~FOF_FILESONLY; shfo.fAnyOperationsAborted = FALSE; ret = SHFileOperation(&shfo); ok(ret == ERROR_ACCESS_DENIED, "Expected ERROR_ACCESS_DENIED, got %ld\n", ret); ok(!shfo.fAnyOperationsAborted, "Expected no aborted operations\n"); ok(file_exists("test1.txt"), "Expected test1.txt to exist\n"); /* try an invalid function */ init_shfo_tests(); shfo.pFrom = "test1.txt\0"; shfo.wFunc = 0; ret = SHFileOperation(&shfo); ok(ret == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %ld\n", ret); ok(file_exists("test1.txt"), "Expected test1.txt to exist\n"); /* try an invalid list, only one null terminator */ init_shfo_tests(); shfo.pFrom = ""; shfo.wFunc = FO_DELETE; ret = SHFileOperation(&shfo); ok(ret == ERROR_ACCESS_DENIED, "Expected ERROR_ACCESS_DENIED, got %ld\n", ret); ok(file_exists("test1.txt"), "Expected test1.txt to exist\n"); /* delete a dir, and then a file inside the dir, same as * deleting a nonexistent file */ init_shfo_tests(); shfo.pFrom = "testdir2\0testdir2\\one.txt\0"; ret = SHFileOperation(&shfo); ok(ret == ERROR_PATH_NOT_FOUND, "Expected ERROR_PATH_NOT_FOUND, got %ld\n", ret); ok(!file_exists("testdir2"), "Expected testdir2 to not exist\n"); ok(!file_exists("testdir2\\one.txt"), "Expected testdir2\\one.txt to not exist\n"); /* try the FOF_NORECURSION flag, continues deleting subdirs */ init_shfo_tests(); shfo.pFrom = "testdir2\0"; shfo.fFlags |= FOF_NORECURSION; ret = SHFileOperation(&shfo); ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %ld\n", ret); ok(!file_exists("testdir2\\one.txt"), "Expected testdir2\\one.txt to not exist\n"); ok(!file_exists("testdir2\\nested"), "Expected testdir2\\nested to exist\n"); }
void RotateLogs() { char tmpNo[3] = {0}; char oldDirNo; char baseDirName[256] = {0}; char oldDirName[256] = {0}; char newDirName[256] = {0}; char tmpFileName[256] = {0}; if (strlen(GetLogDir()) == 0) return; GetCurrentDirectoryA(256, baseDirName); strcat(baseDirName, "\\"); strcat(baseDirName, GetLogDir()); strcat(baseDirName, "\\"); // Delete directory '9' strcpy(oldDirName, baseDirName); strcat(oldDirName, "9"); SHFILEOPSTRUCTA fileOp; fileOp.hwnd = 0; fileOp.wFunc = FO_DELETE; fileOp.pFrom = oldDirName; fileOp.pTo = NULL; fileOp.fFlags = FOF_NOERRORUI + FOF_NOCONFIRMATION; fileOp.fAnyOperationsAborted = NULL; fileOp.hNameMappings = NULL; fileOp.lpszProgressTitle = NULL; SHFileOperationA(&fileOp); // Age directories 1-8 for (oldDirNo = 99; oldDirNo > 0; oldDirNo--) { strcpy(oldDirName, baseDirName); _itoa(oldDirNo, tmpNo, 10); strcat(oldDirName, tmpNo); strcpy(newDirName, baseDirName); _itoa(oldDirNo + 1, tmpNo, 10); strcat(newDirName, tmpNo); MoveFileA(oldDirName, newDirName); } // Create youngest directory '1' CreateDirectoryA(oldDirName, NULL); // Move current log files to '1' strcpy(oldDirName, baseDirName); strcat(oldDirName, "nwnx.txt"); strcpy(newDirName, baseDirName); strcat(newDirName, "1\\nwnx.txt"); MoveFileA(oldDirName, newDirName); strcpy(oldDirName, baseDirName); strcat(oldDirName, "nwserverlog1.txt"); strcpy(newDirName, baseDirName); strcat(newDirName, "1\\nwserverlog1.txt"); MoveFileA(oldDirName, newDirName); strcpy(oldDirName, baseDirName); strcat(oldDirName, "nwserverError1.txt"); strcpy(newDirName, baseDirName); strcat(newDirName, "1\\nwserverError1.txt"); MoveFileA(oldDirName, newDirName); strcpy(oldDirName, baseDirName); strcat(oldDirName, "nwnx_odbc.txt"); strcpy(newDirName, baseDirName); strcat(newDirName, "1\\nwnx_odbc.txt"); MoveFileA(oldDirName, newDirName); }