static void report_cb(Fl_Widget*, void*) { String bug_tool = file_path("ede-bug-report"); if(bug_tool.empty()) { alert(_("Unable to find ede-bug-report tool." " Please check if PATH variable contains directory where this tool was installed")); return; } collect_info_once(); TempFile tmp; if(!tmp.create("/tmp/.ecrash-dump")) { alert(_("Unable to create temporary file: (%i) %s"), tmp.status(), strerror(tmp.status())); return; } /* close it since we need the file name */ tmp.close(); txt_buf->savefile(tmp.name()); run_async("%s --gdb-dump %s", bug_tool.c_str(), tmp.name()); /* wait some time until the file was read; dumb, I know :( */ sleep(1); /* remove it */ tmp.unlink(); }
bool Config::save(const char* fname) { E_ASSERT(fname != NULL); TempFile t; if(!t.create(".etmp.XXXXXX")) { errcode = CONF_ERR_FILE; return false; } /* so we could explicitly handle our options */ t.set_no_close(true); t.set_auto_delete(false); FILE *f = t.fstream(); SectionListIter sit = section_list.begin(), sit_end = section_list.end(); unsigned int sz = section_list.size(); EntryListIter eit; for (; sit != sit_end; ++sit, --sz) { fprintf(f, "[%s]\n", (*sit)->sname); for (eit = (*sit)->entry_list.begin(); eit != (*sit)->entry_list.end(); ++eit) fprintf(f, "%s=%s\n", (*eit)->key, (*eit)->value); /* prevent unneeded newline at the end of file */ if(sz != 1) fprintf(f, "\n"); } /* explicitly flush */ fflush(f); t.close(); E_ASSERT(t.name() && "Temporary name NULL. Report this as bug"); if(rename(t.name(), fname) != 0) { E_WARNING("Unable to save to '%s'\n", fname); return false; } chmod(fname, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH); return true; }
bool gdb_output_generate(const char *path, TempFile &t, int pid) { E_RETURN_VAL_IF_FAIL(path != NULL, false); int tfd = -1; TempFile scr; if(!scr.create("/tmp/.ecrash-script")) { E_WARNING(E_STRLOC ": Unable to create temporary file for debugger script: (%i) %s", scr.status(), strerror(scr.status())); return false; } if(!t.create("/tmp/.ecrash-output")) { E_WARNING(E_STRLOC ": Unable to create temporary file for debugger output: (%i) %s", t.status(), strerror(t.status())); return false; } tfd = t.handle(); /* write script */ ::write(scr.handle(), "bt\nquit\n", 8); scr.set_auto_delete(true); scr.close(); String gdb_path = file_path("gdb"); if(gdb_path.empty()) { /* write straight to the file, so dialog could show it */ write_str(tfd, "Unable to find gdb. Please install it first"); /* see it as valid, so dialog could be shown */ return true; } /* * to find core file, we will try these strategies: first try to open 'core.PID' if * we got PID (default on linux); if does not exists, try to open 'core'; everything is * assumed current folder, whatever it was set */ bool core_found = false; String core_path; if(pid > -1) { core_path.printf("%s.%i", CORE_FILE, pid); if(file_test(core_path.c_str(), FILE_TEST_IS_REGULAR)) core_found = true; } if(!core_found) { core_path = CORE_FILE; if(file_test(core_path.c_str(), FILE_TEST_IS_REGULAR)) core_found = true; } if(!core_found) { write_str(tfd, "Unable to find core file. Backtrace will not be done."); /* see it as valid, so dialog could be shown */ return true; } pid_t gdb_pid = fork(); if(gdb_pid == -1) { E_WARNING(E_STRLOC ": Unable to fork the process\n"); return false; } else if(gdb_pid == 0) { /* child; redirect to the file */ dup2(tfd, 1); t.close(); ::write(1, " ", 1); char* argv[8]; argv[0] = (char*)gdb_path.c_str(); argv[1] = (char*)"--quiet"; argv[2] = (char*)"--batch"; argv[3] = (char*)"-x"; argv[4] = (char*)scr.name(); argv[5] = (char*)path; argv[6] = (char*)core_path.c_str(); argv[7] = 0; execvp(argv[0], argv); return false; } else { int status; if(waitpid(gdb_pid, &status, 0) != gdb_pid) { E_WARNING(E_STRLOC ": Failed to execute waitpid() properly\n"); return false; } } file_remove(core_path.c_str()); return true; }
#include <edelib/TempFile.h> #include <edelib/FileTest.h> #include "UnitTest.h" EDELIB_NS_USE UT_FUNC(TempFileTest, "Test TempFile") { TempFile t, t2, t3; UT_VERIFY(t.create("foo-temp.XXXXXX") == true); UT_VERIFY(t == true); UT_VERIFY(file_test(t.name(), FILE_TEST_IS_REGULAR | FILE_TEST_IS_WRITEABLE)); t.unlink(); UT_VERIFY(file_test(t.name(), FILE_TEST_IS_REGULAR) == false); UT_VERIFY(t2.create("baz-tmp") == true); UT_VERIFY(t2 == true); UT_VERIFY(file_test(t2.name(), FILE_TEST_IS_REGULAR | FILE_TEST_IS_WRITEABLE)); UT_VERIFY(t2.fstream() != 0); t2.set_auto_delete(true); UT_VERIFY(t3.create("/this/file/should/not/exists") == false); UT_VERIFY(t3 == false); }