ReactOS  0.4.15-dev-1150-g593bcce
CJournaledTestList.cpp
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Automatic Testing Utility
3  * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4  * PURPOSE: Class implementing a journaled test list for the Crash Recovery feature
5  * COPYRIGHT: Copyright 2009 Colin Finck (colin@reactos.org)
6  */
7 
8 #include "precomp.h"
9 
10 static const char szJournalHeader[] = "RAT_J-V1";
11 static const WCHAR szJournalFileName[] = L"rosautotest.journal";
12 
20  : CTestList(Test)
21 {
22  WCHAR JournalFile[MAX_PATH];
23 
25 
26  /* Build the path to the journal file */
28  FATAL("SHGetFolderPathW failed\n");
29 
30  m_JournalFile = JournalFile;
31  m_JournalFile += L"\\rosautotest\\";
32 
33  /* Create the directory if necessary */
36 
38 
39  /* Check if the journal already exists */
42  else
44 }
45 
50 {
53 }
54 
64 void
66 {
68  m_JournalFile.c_str(),
70  0,
71  NULL,
72  (CreateNew ? CREATE_ALWAYS : OPEN_EXISTING),
74  NULL
75  );
76 
78  FATAL("CreateFileW failed\n");
79 }
80 
89 void
91 {
93  WriteFile(m_hJournal, String.c_str(), (ULONG)String.size() + 1, &BytesWritten, NULL);
95 }
96 
105 void
107 {
109  WriteFile(m_hJournal, String.c_str(), ((ULONG)String.size() + 1) * sizeof(WCHAR), &BytesWritten, NULL);
111 }
112 
123 void
125 {
126  Output = string(*Buffer);
127  *Buffer += Output.size() + 1;
128 }
129 
140 void
142 {
143  Output = wstring((PWSTR)*Buffer);
144  *Buffer += (Output.size() + 1) * sizeof(WCHAR);
145 }
146 
150 void
152 {
153  char TerminatingNull = 0;
154  CTestInfo* TestInfo;
156 
157  StringOut("Writing initial journal file...\n\n");
158 
159  m_ListIterator = 0;
160 
161  /* Store all command lines in the m_List vector */
162  while((TestInfo = m_Test->GetNextTestInfo()) != 0)
163  {
164  m_List.push_back(*TestInfo);
165  delete TestInfo;
166  }
167 
168  /* Serialize the vector and the iterator into a file */
169  OpenJournal(GENERIC_WRITE, true);
170 
173 
174  for(size_t i = 0; i < m_List.size(); i++)
175  {
176  SerializeIntoJournal(m_List[i].CommandLine);
177  SerializeIntoJournal(m_List[i].Module);
179  }
180 
181  WriteFile(m_hJournal, &TerminatingNull, sizeof(TerminatingNull), &BytesWritten, NULL);
183 
186 
187  /* m_ListIterator will be incremented before its first use */
188  m_ListIterator = (size_t)-1;
189 }
190 
194 void
196 {
197  char* Buffer;
198  char* pBuffer;
199  char FileHeader[sizeof(szJournalHeader)];
201  DWORD RemainingSize;
202 
203  StringOut("Loading journal file...\n\n");
204 
206  RemainingSize = GetFileSize(m_hJournal, NULL);
207 
208  /* Verify the header of the journal file */
209  ReadFile(m_hJournal, FileHeader, sizeof(szJournalHeader), &BytesRead, NULL);
210  RemainingSize -= BytesRead;
211 
212  if(BytesRead != sizeof(szJournalHeader))
213  EXCEPTION("Journal file contains no header!\n");
214 
215  if(strcmp(FileHeader, szJournalHeader))
216  EXCEPTION("Journal file has an unsupported header!\n");
217 
218  /* Read the iterator */
220  RemainingSize -= BytesRead;
221 
222  if(BytesRead != sizeof(m_ListIterator))
223  EXCEPTION("Journal file contains no m_ListIterator member!\n");
224 
225  /* Read the rest of the file into a buffer */
226  Buffer = new char[RemainingSize];
227  pBuffer = Buffer;
228  ReadFile(m_hJournal, Buffer, RemainingSize, &BytesRead, NULL);
229 
231  m_hJournal = NULL;
232 
233  /* Now recreate the m_List vector out of that information */
234  while(*pBuffer)
235  {
236  CTestInfo TestInfo;
237 
240  UnserializeFromBuffer(&pBuffer, TestInfo.Test);
241 
242  m_List.push_back(TestInfo);
243  }
244 
245  delete[] Buffer;
246 }
247 
251 void
253 {
255 
257 
258  /* Skip the header */
260 
263 
265  m_hJournal = NULL;
266 }
267 
275 CTestInfo*
277 {
278  CTestInfo* TestInfo;
279 
280  /* Always jump to the next test here.
281  - If we're at the beginning of a new test list, the iterator will be set to 0.
282  - If we started with a loaded one, we assume that the test m_ListIterator is currently set
283  to crashed, so we move to the next test. */
284  ++m_ListIterator;
285 
286  /* Check whether the iterator would already exceed the number of stored elements */
287  if(m_ListIterator == m_List.size())
288  {
289  /* Delete the journal and return no pointer */
290  DeleteFileW(m_JournalFile.c_str());
291 
292  TestInfo = NULL;
293  }
294  else
295  {
296  /* Update the journal with the current iterator and return the test information */
297  UpdateJournal();
298 
299  TestInfo = new CTestInfo(m_List[m_ListIterator]);
300  }
301 
302  return TestInfo;
303 }
BOOL WINAPI CreateDirectoryW(IN LPCWSTR lpPathName, IN LPSECURITY_ATTRIBUTES lpSecurityAttributes)
Definition: dir.c:90
#define FATAL(Message)
Definition: precomp.h:57
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
Definition: CTest.h:8
static const char szJournalHeader[]
_Must_inspect_result_ _In_ PFILE_OBJECT _In_opt_ PLARGE_INTEGER _In_ ULONG _In_ FLT_IO_OPERATION_FLAGS _Out_opt_ PULONG BytesWritten
Definition: fltkernel.h:1293
#define CloseHandle
Definition: compat.h:487
uint16_t * PWSTR
Definition: typedefs.h:56
virtual CTestInfo * GetNextTestInfo()=0
#define FILE_CURRENT
Definition: winbase.h:113
static const WCHAR szJournalFileName[]
#define INVALID_HANDLE_VALUE
Definition: compat.h:479
static WCHAR String[]
Definition: stringtable.c:55
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
DWORD WINAPI GetFileAttributesW(LPCWSTR lpFileName)
Definition: fileinfo.c:652
#define GENERIC_WRITE
Definition: nt_native.h:90
BOOL WINAPI DeleteFileW(IN LPCWSTR lpFileName)
Definition: delete.c:39
smooth NULL
Definition: ftsmooth.c:416
PVOID pBuffer
Definition: bufpool.h:45
#define OPEN_EXISTING
Definition: compat.h:523
#define EXCEPTION(Message)
Definition: precomp.h:56
#define FILE_FLAG_WRITE_THROUGH
Definition: disk.h:47
__kernel_size_t size_t
Definition: linux.h:237
BOOL WINAPI FlushFileBuffers(IN HANDLE hFile)
Definition: fileinfo.c:25
__wchar_t WCHAR
Definition: xmlstorage.h:180
vector< CTestInfo > m_List
void push_back(const _Tp &__x=_STLP_DEFAULT_CONSTRUCTED(_Tp))
Definition: _vector.h:379
#define CSIDL_APPDATA
Definition: shlobj.h:2037
#define MAX_PATH
Definition: compat.h:34
CTestInfo * GetNextTestInfo()
unsigned long DWORD
Definition: ntddk_ex.h:95
string StringOut(const string &String, bool forcePrint=true)
Definition: tools.cpp:96
DWORD WINAPI GetFileSize(HANDLE hFile, LPDWORD lpFileSizeHigh)
Definition: fileinfo.c:331
wstring CommandLine
Definition: CTestInfo.h:11
#define INVALID_FILE_ATTRIBUTES
Definition: vfdcmd.c:23
string Module
Definition: CTestInfo.h:12
void OpenJournal(DWORD DesiredAccess, bool CreateNew=false)
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:137
static const WCHAR L[]
Definition: oid.c:1250
void SerializeIntoJournal(const string &String)
Definition: arc.h:85
#define GENERIC_READ
Definition: compat.h:135
char string[160]
Definition: util.h:11
CTest * m_Test
Definition: CTestList.h:11
#define S_OK
Definition: intsafe.h:51
#define CREATE_ALWAYS
Definition: disk.h:72
_In_ PIO_STACK_LOCATION _Inout_ PFILE_OBJECT _Inout_ PVCB _Outptr_result_maybenull_ PDCB _In_ PDCB _In_ PDIRENT _In_ ULONG _In_ ULONG _In_ PUNICODE_STRING _In_ PACCESS_MASK DesiredAccess
Definition: create.c:4137
#define ReadFile(a, b, c, d, e)
Definition: compat.h:490
#define CreateFileW
Definition: compat.h:489
CJournaledTestList(CTest *Test)
HRESULT WINAPI SHGetFolderPathW(HWND hwndOwner, int nFolder, HANDLE hToken, DWORD dwFlags, LPWSTR pszPath)
Definition: shellpath.c:2208
size_type size() const
Definition: _vector.h:192
unsigned int ULONG
Definition: retypes.h:1
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
void UnserializeFromBuffer(char **Buffer, string &Output)
string Test
Definition: CTestInfo.h:13
IN BOOLEAN OUT PSTR Buffer
Definition: progress.h:34
_Must_inspect_result_ _In_ PFILE_OBJECT _In_opt_ PLARGE_INTEGER _In_ ULONG _In_ FLT_IO_OPERATION_FLAGS _Out_opt_ PULONG BytesRead
Definition: fltkernel.h:1255
#define SetFilePointer
Definition: compat.h:491