//initialize file for output BOOL initialize_files(execution_details * details){ SECURITY_ATTRIBUTES security_attributes; ZeroMemory(&security_attributes, sizeof(security_attributes)); security_attributes.nLength = sizeof(security_attributes); security_attributes.bInheritHandle = TRUE; details->temp_filename = get_temp_filename(); // setup redirection handles // output handle must be WRITE mode, share READ // redirect handle must be READ mode, share WRITE details->file_output = INVALID_HANDLE_VALUE; details->file_output = CreateFile(details->temp_filename, GENERIC_WRITE, FILE_SHARE_READ, &security_attributes, CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY, 0); if (details->file_output == INVALID_HANDLE_VALUE) { return FALSE; } details->file_output_read = INVALID_HANDLE_VALUE; details->file_output_read = CreateFile(details->temp_filename, GENERIC_READ, FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0); if (details->file_output_read == INVALID_HANDLE_VALUE) { CloseHandle(details->file_output); return FALSE; } return TRUE; }
std::pair<int, std::string> Filesystem::create_temp_file(std::string filename_tag, std::string filename_end) { std::string temp_filename = get_temp_filename(filename_tag, filename_end); int fd = open(temp_filename.c_str(), O_RDWR | O_CREAT | O_EXCL, 0600); if (fd == -1) Error::printf("Could not create temporary file '%s' error:%d\n", temp_filename.data(), errno); return std::make_pair(fd, temp_filename.data()); }
void ribi::qtasciiartermaindialog_test::all_tests() { const ribi::fileio::FileIo f; const std::string tmp_filename { get_temp_filename(".png") }; QVERIFY(!f.IsRegularFile(tmp_filename)); //Load image from resources, save to file { QPixmap p(":/AsciiArter/images/R.png"); QVERIFY(p.width() > 0); QVERIFY(p.height() > 0); const bool ok{p.save(tmp_filename.c_str())}; QVERIFY(ok); } QVERIFY(f.IsRegularFile(tmp_filename)); AsciiArterMainDialog dialog(tmp_filename,40); const std::vector<std::string> v { dialog.GetAsciiArt() }; QVERIFY(!v.empty()); f.DeleteFile(tmp_filename); QVERIFY(!f.IsRegularFile(tmp_filename)); }
static void test_SetupCopyOEMInf(void) { CHAR toolong[MAX_PATH * 2]; CHAR path[MAX_PATH], dest[MAX_PATH]; CHAR tmpfile[MAX_PATH], dest_save[MAX_PATH]; LPSTR inf = NULL; DWORD size; BOOL res; /* try NULL SourceInfFileName */ SetLastError(0xdeadbeef); res = pSetupCopyOEMInfA(NULL, NULL, 0, SP_COPY_NOOVERWRITE, NULL, 0, NULL, NULL); ok(res == FALSE, "Expected FALSE, got %d\n", res); ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError()); /* try empty SourceInfFileName */ SetLastError(0xdeadbeef); res = pSetupCopyOEMInfA("", NULL, 0, SP_COPY_NOOVERWRITE, NULL, 0, NULL, NULL); ok(res == FALSE, "Expected FALSE, got %d\n", res); ok(GetLastError() == ERROR_FILE_NOT_FOUND || GetLastError() == ERROR_BAD_PATHNAME || /* Win98 */ GetLastError() == ERROR_INVALID_PARAMETER, /* Vista, W2K8 */ "Unexpected error : %d\n", GetLastError()); /* try a relative nonexistent SourceInfFileName */ SetLastError(0xdeadbeef); res = pSetupCopyOEMInfA("nonexistent", NULL, 0, SP_COPY_NOOVERWRITE, NULL, 0, NULL, NULL); ok(res == FALSE, "Expected FALSE, got %d\n", res); ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError()); /* try an absolute nonexistent SourceInfFileName */ lstrcpy(path, CURR_DIR); lstrcat(path, "\\nonexistent"); SetLastError(0xdeadbeef); res = pSetupCopyOEMInfA(path, NULL, 0, SP_COPY_NOOVERWRITE, NULL, 0, NULL, NULL); ok(res == FALSE, "Expected FALSE, got %d\n", res); ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError()); /* try a long SourceInfFileName */ memset(toolong, 'a', MAX_PATH * 2); toolong[MAX_PATH * 2 - 1] = '\0'; SetLastError(0xdeadbeef); res = pSetupCopyOEMInfA(toolong, NULL, 0, SP_COPY_NOOVERWRITE, NULL, 0, NULL, NULL); ok(res == FALSE, "Expected FALSE, got %d\n", res); ok(GetLastError() == ERROR_FILE_NOT_FOUND || GetLastError() == ERROR_FILENAME_EXCED_RANGE, /* Win98 */ "Expected ERROR_FILE_NOT_FOUND or ERROR_FILENAME_EXCED_RANGE, got %d\n", GetLastError()); get_temp_filename(tmpfile); create_inf_file(tmpfile); /* try a relative SourceInfFileName */ SetLastError(0xdeadbeef); res = pSetupCopyOEMInfA(tmpfile, NULL, 0, SP_COPY_NOOVERWRITE, NULL, 0, NULL, NULL); ok(res == FALSE || broken(res == TRUE), /* Win98 */ "Expected FALSE, got %d\n", res); if (GetLastError() == ERROR_WRONG_INF_TYPE || GetLastError() == ERROR_UNSUPPORTED_TYPE /* Win7 */) { /* FIXME: * Vista needs a [Manufacturer] entry in the inf file. Doing this will give some * popups during the installation though as it also needs a catalog file (signed?). */ win_skip("Needs a different inf file on Vista+\n"); DeleteFile(tmpfile); return; } ok(GetLastError() == ERROR_FILE_NOT_FOUND || broken(GetLastError() == ERROR_SUCCESS), /* Win98 */ "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError()); ok(file_exists(tmpfile), "Expected tmpfile to exist\n"); /* try SP_COPY_REPLACEONLY, dest does not exist */ SetLastError(0xdeadbeef); res = pSetupCopyOEMInfA(path, NULL, SPOST_NONE, SP_COPY_REPLACEONLY, NULL, 0, NULL, NULL); ok(res == FALSE, "Expected FALSE, got %d\n", res); ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError()); ok(file_exists(tmpfile), "Expected source inf to exist\n"); /* try an absolute SourceInfFileName, without DestinationInfFileName */ lstrcpy(path, CURR_DIR); lstrcat(path, "\\"); lstrcat(path, tmpfile); SetLastError(0xdeadbeef); res = pSetupCopyOEMInfA(path, NULL, SPOST_NONE, 0, NULL, 0, NULL, NULL); ok(res == TRUE, "Expected TRUE, got %d\n", res); ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError()); ok(file_exists(path), "Expected source inf to exist\n"); /* try SP_COPY_REPLACEONLY, dest exists */ SetLastError(0xdeadbeef); res = pSetupCopyOEMInfA(path, NULL, SPOST_NONE, SP_COPY_REPLACEONLY, NULL, 0, NULL, NULL); ok(res == TRUE, "Expected TRUE, got %d\n", res); ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError()); ok(file_exists(path), "Expected source inf to exist\n"); /* try SP_COPY_NOOVERWRITE */ SetLastError(0xdeadbeef); res = pSetupCopyOEMInfA(path, NULL, SPOST_NONE, SP_COPY_NOOVERWRITE, NULL, 0, NULL, NULL); ok(res == FALSE, "Expected FALSE, got %d\n", res); ok(GetLastError() == ERROR_FILE_EXISTS, "Expected ERROR_FILE_EXISTS, got %d\n", GetLastError()); /* get the DestinationInfFileName */ SetLastError(0xdeadbeef); res = pSetupCopyOEMInfA(path, NULL, SPOST_NONE, 0, dest, MAX_PATH, NULL, NULL); ok(res == TRUE, "Expected TRUE, got %d\n", res); ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError()); ok(lstrlen(dest) != 0, "Expected a non-zero length string\n"); ok(file_exists(dest), "Expected destination inf to exist\n"); ok(check_format(dest, NULL), "Expected %%windir%%\\inf\\OEMx.inf, got %s\n", dest); ok(file_exists(path), "Expected source inf to exist\n"); lstrcpy(dest_save, dest); DeleteFile(dest_save); /* get the DestinationInfFileName, DestinationInfFileNameSize is too small * - inf is still copied */ lstrcpy(dest, "aaa"); size = 0; SetLastError(0xdeadbeef); res = pSetupCopyOEMInfA(path, NULL, SPOST_NONE, 0, dest, 5, &size, NULL); ok(res == FALSE, "Expected FALSE, got %d\n", res); ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError()); ok(file_exists(path), "Expected source inf to exist\n"); ok(file_exists(dest_save), "Expected dest inf to exist\n"); ok(!lstrcmp(dest, "aaa"), "Expected dest to be unchanged\n"); ok(size == lstrlen(dest_save) + 1, "Expected size to be lstrlen(dest_save) + 1\n"); /* get the DestinationInfFileName and DestinationInfFileNameSize */ SetLastError(0xdeadbeef); res = pSetupCopyOEMInfA(path, NULL, SPOST_NONE, 0, dest, MAX_PATH, &size, NULL); ok(res == TRUE, "Expected TRUE, got %d\n", res); ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError()); ok(lstrlen(dest) + 1 == size, "Expected sizes to match, got (%d, %d)\n", lstrlen(dest), size); ok(file_exists(dest), "Expected destination inf to exist\n"); ok(check_format(dest, NULL), "Expected %%windir%%\\inf\\OEMx.inf, got %s\n", dest); ok(file_exists(path), "Expected source inf to exist\n"); ok(size == lstrlen(dest_save) + 1, "Expected size to be lstrlen(dest_save) + 1\n"); test_original_file_name(strrchr(path, '\\') + 1, dest); /* get the DestinationInfFileName, DestinationInfFileNameSize, and DestinationInfFileNameComponent */ SetLastError(0xdeadbeef); res = pSetupCopyOEMInfA(path, NULL, SPOST_NONE, 0, dest, MAX_PATH, &size, &inf); ok(res == TRUE, "Expected TRUE, got %d\n", res); ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError()); ok(lstrlen(dest) + 1 == size, "Expected sizes to match, got (%d, %d)\n", lstrlen(dest), size); ok(file_exists(dest), "Expected destination inf to exist\n"); ok((inf && inf[0] != 0) || broken(!inf), /* Win98 */ "Expected inf to point to the filename\n"); ok(check_format(dest, inf), "Expected %%windir%%\\inf\\OEMx.inf, got %s\n", dest); ok(file_exists(path), "Expected source inf to exist\n"); ok(size == lstrlen(dest_save) + 1, "Expected size to be lstrlen(dest_save) + 1\n"); /* try SP_COPY_DELETESOURCE */ SetLastError(0xdeadbeef); res = pSetupCopyOEMInfA(path, NULL, SPOST_NONE, SP_COPY_DELETESOURCE, NULL, 0, NULL, NULL); ok(res == TRUE, "Expected TRUE, got %d\n", res); ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError()); ok(!file_exists(path), "Expected source inf to not exist\n"); if (pSetupUninstallOEMInfA) { char *destfile = strrchr(dest, '\\') + 1; SetLastError(0xdeadbeef); ok(pSetupUninstallOEMInfA(destfile, 0, NULL), "Failed to uninstall '%s' : %d\n", destfile, GetLastError()); } else { /* Win9x/WinMe */ SetLastError(0xdeadbeef); ok(DeleteFileA(dest), "Failed to delete file '%s' : %d\n", dest, GetLastError()); /* On WinMe we also need to remove the .pnf file */ *(strrchr(dest, '.') + 1) = 'p'; DeleteFileA(dest); } }
static void test_SetupCopyOEMInf(void) { CHAR toolong[MAX_PATH * 2]; CHAR path[MAX_PATH], dest[MAX_PATH]; CHAR tmpfile[MAX_PATH], dest_save[MAX_PATH]; LPSTR inf; DWORD size; BOOL res; /* try NULL SourceInfFileName */ SetLastError(0xdeadbeef); res = pSetupCopyOEMInfA(NULL, NULL, 0, SP_COPY_NOOVERWRITE, NULL, 0, NULL, NULL); ok(res == FALSE, "Expected FALSE, got %d\n", res); ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError()); /* try empty SourceInfFileName */ SetLastError(0xdeadbeef); res = pSetupCopyOEMInfA("", NULL, 0, SP_COPY_NOOVERWRITE, NULL, 0, NULL, NULL); ok(res == FALSE, "Expected FALSE, got %d\n", res); ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError()); /* try a relative nonexistent SourceInfFileName */ SetLastError(0xdeadbeef); res = pSetupCopyOEMInfA("nonexistent", NULL, 0, SP_COPY_NOOVERWRITE, NULL, 0, NULL, NULL); ok(res == FALSE, "Expected FALSE, got %d\n", res); ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError()); /* try an absolute nonexistent SourceInfFileName */ lstrcpy(path, CURR_DIR); lstrcat(path, "\\nonexistent"); SetLastError(0xdeadbeef); res = pSetupCopyOEMInfA(path, NULL, 0, SP_COPY_NOOVERWRITE, NULL, 0, NULL, NULL); ok(res == FALSE, "Expected FALSE, got %d\n", res); ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError()); /* try a long SourceInfFileName */ memset(toolong, 'a', MAX_PATH * 2); toolong[MAX_PATH * 2 - 1] = '\0'; SetLastError(0xdeadbeef); res = pSetupCopyOEMInfA(toolong, NULL, 0, SP_COPY_NOOVERWRITE, NULL, 0, NULL, NULL); ok(res == FALSE, "Expected FALSE, got %d\n", res); ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError()); get_temp_filename(tmpfile); create_inf_file(tmpfile); /* try a relative SourceInfFileName */ SetLastError(0xdeadbeef); res = pSetupCopyOEMInfA(tmpfile, NULL, 0, SP_COPY_NOOVERWRITE, NULL, 0, NULL, NULL); ok(res == FALSE, "Expected FALSE, got %d\n", res); ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError()); ok(file_exists(tmpfile), "Expected tmpfile to exist\n"); /* try SP_COPY_REPLACEONLY, dest does not exist */ SetLastError(0xdeadbeef); res = pSetupCopyOEMInfA(path, NULL, SPOST_NONE, SP_COPY_REPLACEONLY, NULL, 0, NULL, NULL); ok(res == FALSE, "Expected FALSE, got %d\n", res); ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError()); ok(file_exists(tmpfile), "Expected source inf to exist\n"); /* try an absolute SourceInfFileName, without DestinationInfFileName */ lstrcpy(path, CURR_DIR); lstrcat(path, "\\"); lstrcat(path, tmpfile); SetLastError(0xdeadbeef); res = pSetupCopyOEMInfA(path, NULL, SPOST_NONE, 0, NULL, 0, NULL, NULL); ok(res == TRUE, "Expected TRUE, got %d\n", res); ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError()); ok(file_exists(path), "Expected source inf to exist\n"); /* try SP_COPY_REPLACEONLY, dest exists */ SetLastError(0xdeadbeef); res = pSetupCopyOEMInfA(path, NULL, SPOST_NONE, SP_COPY_REPLACEONLY, NULL, 0, NULL, NULL); ok(res == TRUE, "Expected TRUE, got %d\n", res); ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError()); ok(file_exists(path), "Expected source inf to exist\n"); /* try SP_COPY_NOOVERWRITE */ SetLastError(0xdeadbeef); res = pSetupCopyOEMInfA(path, NULL, SPOST_NONE, SP_COPY_NOOVERWRITE, NULL, 0, NULL, NULL); ok(res == FALSE, "Expected FALSE, got %d\n", res); ok(GetLastError() == ERROR_FILE_EXISTS, "Expected ERROR_FILE_EXISTS, got %d\n", GetLastError()); /* get the DestinationInfFileName */ SetLastError(0xdeadbeef); res = pSetupCopyOEMInfA(path, NULL, SPOST_NONE, 0, dest, MAX_PATH, NULL, NULL); ok(res == TRUE, "Expected TRUE, got %d\n", res); ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError()); ok(lstrlen(dest) != 0, "Expected a non-zero length string\n"); ok(file_exists(dest), "Expected destination inf to exist\n"); ok(check_format(dest, NULL), "Expected %%windir%%\\inf\\OEMx.inf, got %s\n", dest); ok(file_exists(path), "Expected source inf to exist\n"); lstrcpy(dest_save, dest); DeleteFile(dest_save); /* get the DestinationInfFileName, DestinationInfFileNameSize is too small * - inf is still copied */ lstrcpy(dest, "aaa"); size = 0; SetLastError(0xdeadbeef); res = pSetupCopyOEMInfA(path, NULL, SPOST_NONE, 0, dest, 5, &size, NULL); ok(res == FALSE, "Expected FALSE, got %d\n", res); ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError()); ok(file_exists(path), "Expected source inf to exist\n"); ok(file_exists(dest_save), "Expected dest inf to exist\n"); ok(!lstrcmp(dest, "aaa"), "Expected dest to be unchanged\n"); ok(size == lstrlen(dest_save) + 1, "Expected size to be lstrlen(dest_save) + 1\n"); /* get the DestinationInfFileName and DestinationInfFileNameSize */ SetLastError(0xdeadbeef); res = pSetupCopyOEMInfA(path, NULL, SPOST_NONE, 0, dest, MAX_PATH, &size, NULL); ok(res == TRUE, "Expected TRUE, got %d\n", res); ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError()); ok(lstrlen(dest) + 1 == size, "Expected sizes to match, got (%d, %d)\n", lstrlen(dest), size); ok(file_exists(dest), "Expected destination inf to exist\n"); ok(check_format(dest, NULL), "Expected %%windir%%\\inf\\OEMx.inf, got %s\n", dest); ok(file_exists(path), "Expected source inf to exist\n"); ok(size == lstrlen(dest_save) + 1, "Expected size to be lstrlen(dest_save) + 1\n"); test_original_file_name(strrchr(path, '\\') + 1, dest); /* get the DestinationInfFileName, DestinationInfFileNameSize, and DestinationInfFileNameComponent */ SetLastError(0xdeadbeef); res = pSetupCopyOEMInfA(path, NULL, SPOST_NONE, 0, dest, MAX_PATH, &size, &inf); ok(res == TRUE, "Expected TRUE, got %d\n", res); ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError()); ok(lstrlen(dest) + 1 == size, "Expected sizes to match, got (%d, %d)\n", lstrlen(dest), size); ok(file_exists(dest), "Expected destination inf to exist\n"); ok(check_format(dest, inf), "Expected %%windir%%\\inf\\OEMx.inf, got %s\n", dest); ok(file_exists(path), "Expected source inf to exist\n"); ok(size == lstrlen(dest_save) + 1, "Expected size to be lstrlen(dest_save) + 1\n"); /* try SP_COPY_DELETESOURCE */ SetLastError(0xdeadbeef); res = pSetupCopyOEMInfA(path, NULL, SPOST_NONE, SP_COPY_DELETESOURCE, NULL, 0, NULL, NULL); ok(res == TRUE, "Expected TRUE, got %d\n", res); ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError()); ok(!file_exists(path), "Expected source inf to not exist\n"); }