2 * PROJECT: ReactOS Automatic Testing Utility
3 * LICENSE: GNU GPLv2 or any later version as published by the Free Software Foundation
4 * PURPOSE: Class implementing a journaled test list for the Crash Recovery feature
5 * COPYRIGHT: Copyright 2009 Colin Finck <colin@reactos.org>
10 static const char szJournalHeader
[] = "RAT_J-V1";
11 static const WCHAR szJournalFileName
[] = L
"rosautotest.journal";
14 * Constructs a CJournaledTestList object for an associated CTest-derived object.
17 * Pointer to a CTest-derived object, for which this test list shall serve.
19 CJournaledTestList::CJournaledTestList(CTest
* Test
)
22 WCHAR JournalFile
[MAX_PATH
];
24 m_hJournal
= INVALID_HANDLE_VALUE
;
26 /* Build the path to the journal file */
27 if(SHGetFolderPathW(NULL
, CSIDL_APPDATA
, NULL
, SHGFP_TYPE_CURRENT
, JournalFile
) != S_OK
)
28 FATAL("SHGetFolderPathW failed\n");
30 m_JournalFile
= JournalFile
;
31 m_JournalFile
+= L
"\\rosautotest\\";
33 /* Create the directory if necessary */
34 if(GetFileAttributesW(m_JournalFile
.c_str()) == INVALID_FILE_ATTRIBUTES
)
35 CreateDirectoryW(m_JournalFile
.c_str(), NULL
);
37 m_JournalFile
+= szJournalFileName
;
39 /* Check if the journal already exists */
40 if(GetFileAttributesW(m_JournalFile
.c_str()) == INVALID_FILE_ATTRIBUTES
)
41 WriteInitialJournalFile();
47 * Destructs a CJournaledTestList object.
49 CJournaledTestList::~CJournaledTestList()
51 if(m_hJournal
!= INVALID_HANDLE_VALUE
)
52 CloseHandle(m_hJournal
);
56 * Opens the journal file through the CreateFileW API using the m_hJournal handle.
58 * @param DesiredAccess
59 * dwDesiredAccess parameter passed to CreateFileW
62 * true if the journal file shall be created, false if an existing one shall be opened
65 CJournaledTestList::OpenJournal(DWORD DesiredAccess
, bool CreateNew
)
67 m_hJournal
= CreateFileW(m_JournalFile
.c_str(), DesiredAccess
, 0, NULL
, (CreateNew
? CREATE_ALWAYS
: OPEN_EXISTING
), FILE_ATTRIBUTE_NORMAL
, NULL
);
69 if(m_hJournal
== INVALID_HANDLE_VALUE
)
70 FATAL("CreateFileW failed\n");
74 * Serializes a std::string and writes it into the opened journal file.
77 * The std::string to serialize
79 * @see UnserializeFromBuffer
82 CJournaledTestList::SerializeIntoJournal(const string
& String
)
85 WriteFile(m_hJournal
, String
.c_str(), String
.size() + 1, &BytesWritten
, NULL
);
86 FlushFileBuffers(m_hJournal
);
90 * Serializes a std::wstring and writes it into the opened journal file.
93 * The std::wstring to serialize
95 * @see UnserializeFromBuffer
98 CJournaledTestList::SerializeIntoJournal(const wstring
& String
)
101 WriteFile(m_hJournal
, String
.c_str(), (String
.size() + 1) * sizeof(WCHAR
), &BytesWritten
, NULL
);
102 FlushFileBuffers(m_hJournal
);
106 * Unserializes the next std::string from the journal buffer.
107 * The passed buffer pointer will point at the next element afterwards.
110 * Pointer to a pointer to a char array containing the journal buffer
113 * The std::string to unserialize the value into.
116 CJournaledTestList::UnserializeFromBuffer(char** Buffer
, string
& Output
)
118 Output
= string(*Buffer
);
119 *Buffer
+= Output
.size() + 1;
123 * Unserializes the next std::wstring from the journal buffer.
124 * The passed buffer pointer will point at the next element afterwards.
127 * Pointer to a pointer to a char array containing the journal buffer
130 * The std::wstring to unserialize the value into.
133 CJournaledTestList::UnserializeFromBuffer(char** Buffer
, wstring
& Output
)
135 Output
= wstring((PWSTR
)*Buffer
);
136 *Buffer
+= (Output
.size() + 1) * sizeof(WCHAR
);
140 * Gets all tests to be run and writes an initial journal file with this information.
143 CJournaledTestList::WriteInitialJournalFile()
145 char TerminatingNull
= 0;
149 StringOut("Writing initial journal file...\n\n", TRUE
);
153 /* Store all command lines in the m_List vector */
154 while((TestInfo
= m_Test
->GetNextTestInfo()) != 0)
156 m_List
.push_back(*TestInfo
);
160 /* Serialize the vector and the iterator into a file */
161 OpenJournal(GENERIC_WRITE
, true);
163 WriteFile(m_hJournal
, szJournalHeader
, sizeof(szJournalHeader
), &BytesWritten
, NULL
);
164 WriteFile(m_hJournal
, &m_ListIterator
, sizeof(m_ListIterator
), &BytesWritten
, NULL
);
166 for(size_t i
= 0; i
< m_List
.size(); i
++)
168 SerializeIntoJournal(m_List
[i
].CommandLine
);
169 SerializeIntoJournal(m_List
[i
].Module
);
170 SerializeIntoJournal(m_List
[i
].Test
);
173 WriteFile(m_hJournal
, &TerminatingNull
, sizeof(TerminatingNull
), &BytesWritten
, NULL
);
174 FlushFileBuffers(m_hJournal
);
176 CloseHandle(m_hJournal
);
177 m_hJournal
= INVALID_HANDLE_VALUE
;
179 /* m_ListIterator will be incremented before its first use */
180 m_ListIterator
= (size_t)-1;
184 * Loads the existing journal file and sets all members to the values saved in that file.
187 CJournaledTestList::LoadJournalFile()
191 char FileHeader
[sizeof(szJournalHeader
)];
195 StringOut("Loading journal file...\n\n", TRUE
);
197 OpenJournal(GENERIC_READ
);
198 RemainingSize
= GetFileSize(m_hJournal
, NULL
);
200 /* Verify the header of the journal file */
201 ReadFile(m_hJournal
, FileHeader
, sizeof(szJournalHeader
), &BytesRead
, NULL
);
202 RemainingSize
-= BytesRead
;
204 if(BytesRead
!= sizeof(szJournalHeader
))
205 EXCEPTION("Journal file contains no header!\n");
207 if(strcmp(FileHeader
, szJournalHeader
))
208 EXCEPTION("Journal file has an unsupported header!\n");
210 /* Read the iterator */
211 ReadFile(m_hJournal
, &m_ListIterator
, sizeof(m_ListIterator
), &BytesRead
, NULL
);
212 RemainingSize
-= BytesRead
;
214 if(BytesRead
!= sizeof(m_ListIterator
))
215 EXCEPTION("Journal file contains no m_ListIterator member!\n");
217 /* Read the rest of the file into a buffer */
218 Buffer
= new char[RemainingSize
];
220 ReadFile(m_hJournal
, Buffer
, RemainingSize
, &BytesRead
, NULL
);
222 CloseHandle(m_hJournal
);
225 /* Now recreate the m_List vector out of that information */
230 UnserializeFromBuffer(&pBuffer
, TestInfo
.CommandLine
);
231 UnserializeFromBuffer(&pBuffer
, TestInfo
.Module
);
232 UnserializeFromBuffer(&pBuffer
, TestInfo
.Test
);
234 m_List
.push_back(TestInfo
);
241 * Writes the current m_ListIterator value into the journal.
244 CJournaledTestList::UpdateJournal()
248 OpenJournal(GENERIC_WRITE
);
250 /* Skip the header */
251 SetFilePointer(m_hJournal
, sizeof(szJournalHeader
), NULL
, FILE_CURRENT
);
253 WriteFile(m_hJournal
, &m_ListIterator
, sizeof(m_ListIterator
), &BytesWritten
, NULL
);
254 FlushFileBuffers(m_hJournal
);
256 CloseHandle(m_hJournal
);
261 * Interface to other classes for receiving information about the next test to be run.
264 * A pointer to a CTestInfo object, which contains all available information about the next test.
265 * The caller needs to free that object.
268 CJournaledTestList::GetNextTestInfo()
272 /* Always jump to the next test here.
273 - If we're at the beginning of a new test list, the iterator will be set to 0.
274 - If we started with a loaded one, we assume that the test m_ListIterator is currently set
275 to crashed, so we move to the next test. */
278 /* Check whether the iterator would already exceed the number of stored elements */
279 if(m_ListIterator
== m_List
.size())
281 /* Delete the journal and return no pointer */
282 DeleteFileW(m_JournalFile
.c_str());
288 /* Update the journal with the current iterator and return the test information */
291 TestInfo
= new CTestInfo(m_List
[m_ListIterator
]);