[LOCALSPL]
[reactos.git] / reactos / win32ss / printing / providers / localspl / jobs.c
1 /*
2 * PROJECT: ReactOS Local Spooler
3 * LICENSE: GNU LGPL v2.1 or any later version as published by the Free Software Foundation
4 * PURPOSE: Functions for managing print jobs
5 * COPYRIGHT: Copyright 2015 Colin Finck <colin@reactos.org>
6 */
7
8 #include "precomp.h"
9
10 // Global Variables
11 SKIPLIST GlobalJobList;
12
13 // Local Variables
14 static DWORD _dwLastJobID;
15
16
17 /**
18 * @name _GlobalJobListCompareRoutine
19 *
20 * SKIPLIST_COMPARE_ROUTINE for the Global Job List.
21 * We need the Global Job List to check whether a Job ID is already in use. Consequently, this list is sorted by ID.
22 */
23 static int WINAPI
24 _GlobalJobListCompareRoutine(PVOID FirstStruct, PVOID SecondStruct)
25 {
26 PLOCAL_JOB A = (PLOCAL_JOB)FirstStruct;
27 PLOCAL_JOB B = (PLOCAL_JOB)SecondStruct;
28
29 return A->dwJobID - B->dwJobID;
30 }
31
32 /**
33 * @name _PrinterJobListCompareRoutine
34 *
35 * SKIPLIST_COMPARE_ROUTINE for the each Printer's Job List.
36 * Jobs in this list are sorted in the desired order of processing.
37 */
38 static int WINAPI
39 _PrinterJobListCompareRoutine(PVOID FirstStruct, PVOID SecondStruct)
40 {
41 PLOCAL_JOB A = (PLOCAL_JOB)FirstStruct;
42 PLOCAL_JOB B = (PLOCAL_JOB)SecondStruct;
43 int iComparison;
44 FILETIME ftSubmittedA;
45 FILETIME ftSubmittedB;
46 ULARGE_INTEGER uliSubmittedA;
47 ULARGE_INTEGER uliSubmittedB;
48 ULONGLONG ullResult;
49
50 // First compare the priorities to determine the order.
51 // The job with a higher priority shall come first.
52 iComparison = A->dwPriority - B->dwPriority;
53 if (iComparison != 0)
54 return iComparison;
55
56 // Both have the same priority, so go by creation time.
57 // Comparison is done using the MSDN-recommended way for comparing SYSTEMTIMEs.
58 if (!SystemTimeToFileTime(&A->stSubmitted, &ftSubmittedA))
59 {
60 ERR("SystemTimeToFileTime failed for A with error %lu!\n", GetLastError());
61 return 0;
62 }
63
64 if (!SystemTimeToFileTime(&B->stSubmitted, &ftSubmittedB))
65 {
66 ERR("SystemTimeToFileTime failed for B with error %lu!\n", GetLastError());
67 return 0;
68 }
69
70 uliSubmittedA.LowPart = ftSubmittedA.dwLowDateTime;
71 uliSubmittedA.HighPart = ftSubmittedA.dwHighDateTime;
72 uliSubmittedB.LowPart = ftSubmittedB.dwLowDateTime;
73 uliSubmittedB.HighPart = ftSubmittedB.dwHighDateTime;
74 ullResult = uliSubmittedA.QuadPart - uliSubmittedB.QuadPart;
75
76 if (ullResult < 0)
77 return -1;
78 else if (ullResult > 0)
79 return 1;
80
81 return 0;
82 }
83
84 BOOL
85 GetNextJobID(PDWORD dwJobID)
86 {
87 ++_dwLastJobID;
88
89 while (LookupElementSkiplist(&GlobalJobList, &_dwLastJobID, NULL))
90 {
91 // This ID is already taken. Try the next one.
92 ++_dwLastJobID;
93 }
94
95 if (!IS_VALID_JOB_ID(_dwLastJobID))
96 {
97 ERR("Job ID %lu isn't valid!\n", _dwLastJobID);
98 return FALSE;
99 }
100
101 *dwJobID = _dwLastJobID;
102 return TRUE;
103 }
104
105 void
106 InitializeGlobalJobList()
107 {
108 const WCHAR wszPath[] = L"\\PRINTERS\\?????.SHD";
109 const DWORD cchPath = _countof(wszPath) - 1;
110 const DWORD cchFolders = sizeof("\\PRINTERS\\") - 1;
111 const DWORD cchPattern = sizeof("?????") - 1;
112
113 DWORD dwJobID;
114 HANDLE hFind;
115 PLOCAL_JOB pJob = NULL;
116 PWSTR p;
117 WCHAR wszFullPath[MAX_PATH];
118 WIN32_FIND_DATAW FindData;
119
120 // This one is incremented in GetNextJobID.
121 _dwLastJobID = 0;
122
123 // Initialize an empty list for all jobs of all local printers.
124 // We will search it by Job ID (supply a pointer to a DWORD in LookupElementSkiplist).
125 InitializeSkiplist(&GlobalJobList, DllAllocSplMem, _GlobalJobListCompareRoutine, (PSKIPLIST_FREE_ROUTINE)DllFreeSplMem);
126
127 // Construct the full path search pattern.
128 CopyMemory(wszFullPath, wszSpoolDirectory, cchSpoolDirectory * sizeof(WCHAR));
129 CopyMemory(&wszFullPath[cchSpoolDirectory], wszPath, (cchPath + 1) * sizeof(WCHAR));
130
131 // Use the search pattern to look for unfinished jobs serialized in shadow files (.SHD)
132 hFind = FindFirstFileW(wszFullPath, &FindData);
133 if (hFind == INVALID_HANDLE_VALUE)
134 {
135 // No unfinished jobs found.
136 goto Cleanup;
137 }
138
139 do
140 {
141 // Skip possible subdirectories.
142 if (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
143 continue;
144
145 // Extract the Job ID and verify the file name format at the same time.
146 dwJobID = wcstoul(FindData.cFileName, &p, 10);
147 if (!IS_VALID_JOB_ID(dwJobID))
148 continue;
149
150 if (wcsicmp(p, L".SHD") != 0)
151 continue;
152
153 // This shadow file has a valid name. Construct the full path and try to load it.
154 CopyMemory(&wszFullPath[cchSpoolDirectory + cchFolders], FindData.cFileName, cchPattern);
155 pJob = ReadJobShadowFile(wszFullPath);
156 if (!pJob)
157 continue;
158
159 // Add it to the Global Job List.
160 if (!InsertElementSkiplist(&GlobalJobList, pJob))
161 {
162 ERR("InsertElementSkiplist failed for job %lu for the GlobalJobList!\n", pJob->dwJobID);
163 goto Cleanup;
164 }
165
166 // Add it to the Printer's Job List.
167 if (!InsertElementSkiplist(&pJob->pPrinter->JobList, pJob))
168 {
169 ERR("InsertElementSkiplist failed for job %lu for the Printer's Job List!\n", pJob->dwJobID);
170 goto Cleanup;
171 }
172 }
173 while (FindNextFileW(hFind, &FindData));
174
175 Cleanup:
176 // Outside the loop
177 if (hFind)
178 FindClose(hFind);
179 }
180
181 void
182 InitializePrinterJobList(PLOCAL_PRINTER pPrinter)
183 {
184 // Initialize an empty list for this printer's jobs.
185 // This one is only for sorting the jobs. If you need to lookup a job, search the GlobalJobList by Job ID.
186 InitializeSkiplist(&pPrinter->JobList, DllAllocSplMem, _PrinterJobListCompareRoutine, (PSKIPLIST_FREE_ROUTINE)DllFreeSplMem);
187 }
188
189 BOOL WINAPI
190 LocalAddJob(HANDLE hPrinter, DWORD Level, LPBYTE pData, DWORD cbBuf, LPDWORD pcbNeeded)
191 {
192 const WCHAR wszDoubleBackslash[] = L"\\";
193 const DWORD cchDoubleBackslash = _countof(wszDoubleBackslash) - 1;
194 const WCHAR wszPrintersPath[] = L"\\PRINTERS\\";
195 const DWORD cchPrintersPath = _countof(wszPrintersPath) - 1;
196 const DWORD cchSpl = _countof("?????.SPL") - 1;
197
198 ADDJOB_INFO_1W AddJobInfo1;
199 DWORD cchMachineName;
200 DWORD cchUserName;
201 DWORD dwErrorCode;
202 PBYTE p;
203 PLOCAL_HANDLE pHandle;
204 PLOCAL_JOB pJob;
205 PLOCAL_PRINTER_HANDLE pPrinterHandle;
206 RPC_BINDING_HANDLE hServerBinding = NULL;
207 RPC_WSTR pwszBinding = NULL;
208 RPC_WSTR pwszMachineName = NULL;
209
210 // Check if this is a printer handle.
211 pHandle = (PLOCAL_HANDLE)hPrinter;
212 if (pHandle->HandleType != Printer)
213 {
214 dwErrorCode = ERROR_INVALID_HANDLE;
215 goto Cleanup;
216 }
217
218 pPrinterHandle = (PLOCAL_PRINTER_HANDLE)pHandle->pSpecificHandle;
219
220 // This handle must not have started a job yet!
221 if (pPrinterHandle->pStartedJob)
222 {
223 dwErrorCode = ERROR_INVALID_HANDLE;
224 goto Cleanup;
225 }
226
227 // Check if this is the right structure level.
228 if (Level != 1)
229 {
230 dwErrorCode = ERROR_INVALID_LEVEL;
231 goto Cleanup;
232 }
233
234 // Check if the printer is set to do direct printing.
235 // The Job List isn't used in this case.
236 if (pPrinterHandle->pPrinter->dwAttributes & PRINTER_ATTRIBUTE_DIRECT)
237 {
238 dwErrorCode = ERROR_INVALID_ACCESS;
239 goto Cleanup;
240 }
241
242 // Check if the supplied buffer is large enough.
243 *pcbNeeded = sizeof(ADDJOB_INFO_1W) + (cchSpoolDirectory + cchPrintersPath + cchSpl + 1) * sizeof(WCHAR);
244 if (cbBuf < *pcbNeeded)
245 {
246 dwErrorCode = ERROR_INSUFFICIENT_BUFFER;
247 goto Cleanup;
248 }
249
250 // Create a new job.
251 pJob = DllAllocSplMem(sizeof(LOCAL_JOB));
252 if (!pJob)
253 {
254 dwErrorCode = GetLastError();
255 ERR("DllAllocSplMem failed with error %lu!\n", dwErrorCode);
256 goto Cleanup;
257 }
258
259 // Reserve an ID for this job.
260 if (!GetNextJobID(&pJob->dwJobID))
261 {
262 dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
263 goto Cleanup;
264 }
265
266 // Copy over defaults to the LOCAL_JOB structure.
267 pJob->pPrinter = pPrinterHandle->pPrinter;
268 pJob->pPrintProcessor = pPrinterHandle->pPrinter->pPrintProcessor;
269 pJob->dwPriority = DEF_PRIORITY;
270 pJob->pwszDatatype = AllocSplStr(pPrinterHandle->pwszDatatype);
271 pJob->pwszDocumentName = AllocSplStr(wszDefaultDocumentName);
272 CopyMemory(&pJob->DevMode, &pPrinterHandle->DevMode, sizeof(DEVMODEW));
273 GetSystemTime(&pJob->stSubmitted);
274
275 // Get the user name for the Job.
276 cchUserName = UNLEN + 1;
277 pJob->pwszUserName = DllAllocSplMem(cchUserName * sizeof(WCHAR));
278 if (!GetUserNameW(pJob->pwszUserName, &cchUserName))
279 {
280 dwErrorCode = GetLastError();
281 ERR("GetUserNameW failed with error %lu!\n", dwErrorCode);
282 goto Cleanup;
283 }
284
285 // FIXME: For now, pwszNotifyName equals pwszUserName.
286 pJob->pwszNotifyName = AllocSplStr(pJob->pwszUserName);
287
288 // Get the name of the machine that submitted the Job over RPC.
289 dwErrorCode = RpcBindingServerFromClient(NULL, &hServerBinding);
290 if (dwErrorCode != RPC_S_OK)
291 {
292 ERR("RpcBindingServerFromClient failed with status %lu!\n", dwErrorCode);
293 goto Cleanup;
294 }
295
296 dwErrorCode = RpcBindingToStringBindingW(hServerBinding, &pwszBinding);
297 if (dwErrorCode != RPC_S_OK)
298 {
299 ERR("RpcBindingToStringBindingW failed with status %lu!\n", dwErrorCode);
300 goto Cleanup;
301 }
302
303 dwErrorCode = RpcStringBindingParseW(pwszBinding, NULL, NULL, &pwszMachineName, NULL, NULL);
304 if (dwErrorCode != RPC_S_OK)
305 {
306 ERR("RpcStringBindingParseW failed with status %lu!\n", dwErrorCode);
307 goto Cleanup;
308 }
309
310 cchMachineName = wcslen(pwszMachineName);
311 pJob->pwszMachineName = DllAllocSplMem((cchMachineName + cchDoubleBackslash + 1) * sizeof(WCHAR));
312 CopyMemory(pJob->pwszMachineName, wszDoubleBackslash, cchDoubleBackslash * sizeof(WCHAR));
313 CopyMemory(pJob->pwszMachineName + cchDoubleBackslash, pwszMachineName, (cchMachineName + 1) * sizeof(WCHAR));
314
315 // Add the job to the Global Job List.
316 if (!InsertElementSkiplist(&GlobalJobList, pJob))
317 {
318 dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
319 ERR("InsertElementSkiplist failed for job %lu for the GlobalJobList!\n", pJob->dwJobID);
320 goto Cleanup;
321 }
322
323 // Add the job at the end of the Printer's Job List.
324 // As all new jobs are created with default priority, we can be sure that it would always be inserted at the end.
325 if (!InsertTailElementSkiplist(&pJob->pPrinter->JobList, pJob))
326 {
327 dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
328 ERR("InsertTailElementSkiplist failed for job %lu for the Printer's Job List!\n", pJob->dwJobID);
329 goto Cleanup;
330 }
331
332 // Return a proper ADDJOB_INFO_1W structure.
333 AddJobInfo1.JobId = pJob->dwJobID;
334 AddJobInfo1.Path = (PWSTR)(pData + sizeof(ADDJOB_INFO_1W));
335 p = pData;
336 CopyMemory(p, &AddJobInfo1, sizeof(ADDJOB_INFO_1W));
337 p += sizeof(ADDJOB_INFO_1W);
338 CopyMemory(p, wszSpoolDirectory, cchSpoolDirectory);
339 p += cchSpoolDirectory;
340 CopyMemory(p, wszPrintersPath, cchPrintersPath);
341 p += cchPrintersPath;
342 swprintf((PWSTR)p, L"%05lu.SPL", pJob->dwJobID);
343
344 dwErrorCode = ERROR_SUCCESS;
345
346 Cleanup:
347 if (pwszMachineName)
348 RpcStringFreeW(&pwszMachineName);
349
350 if (pwszBinding)
351 RpcStringFreeW(&pwszBinding);
352
353 if (hServerBinding)
354 RpcBindingFree(&hServerBinding);
355
356 SetLastError(dwErrorCode);
357 return (dwErrorCode == ERROR_SUCCESS);
358 }
359
360
361 static DWORD
362 _LocalGetJobLevel1(PLOCAL_PRINTER_HANDLE pPrinterHandle, PLOCAL_JOB pJob, PBYTE* ppStart, PBYTE* ppEnd, DWORD cbBuf, PDWORD pcbNeeded)
363 {
364 DWORD cbDatatype = (wcslen(pJob->pwszDatatype) + 1) * sizeof(WCHAR);
365 DWORD cbDocumentName = (wcslen(pJob->pwszDocumentName) + 1) * sizeof(WCHAR);
366 DWORD cbMachineName = (wcslen(pJob->pwszMachineName) + 1) * sizeof(WCHAR);
367 DWORD cbPrinterName = (wcslen(pJob->pPrinter->pwszPrinterName) + 1) * sizeof(WCHAR);
368 DWORD cbStatus = 0;
369 DWORD cbUserName = (wcslen(pJob->pwszUserName) + 1) * sizeof(WCHAR);
370 DWORD dwErrorCode;
371 JOB_INFO_1W JobInfo1 = { 0 };
372
373 // A Status Message is optional.
374 if (pJob->pwszStatus)
375 cbStatus = (wcslen(pJob->pwszStatus) + 1) * sizeof(WCHAR);
376
377 // Check if the supplied buffer is large enough.
378 *pcbNeeded += sizeof(JOB_INFO_1W) + cbDatatype + cbDocumentName + cbMachineName + cbPrinterName + cbStatus + cbUserName;
379 if (cbBuf < *pcbNeeded)
380 {
381 dwErrorCode = ERROR_INSUFFICIENT_BUFFER;
382 goto Cleanup;
383 }
384
385 // Put the strings at the end of the buffer.
386 *ppEnd -= cbDatatype;
387 JobInfo1.pDatatype = (PWSTR)*ppEnd;
388 CopyMemory(*ppEnd, pJob->pwszDatatype, cbDatatype);
389
390 *ppEnd -= cbDocumentName;
391 JobInfo1.pDocument = (PWSTR)*ppEnd;
392 CopyMemory(*ppEnd, pJob->pwszDocumentName, cbDocumentName);
393
394 *ppEnd -= cbMachineName;
395 JobInfo1.pMachineName = (PWSTR)*ppEnd;
396 CopyMemory(*ppEnd, pJob->pwszMachineName, cbMachineName);
397
398 *ppEnd -= cbPrinterName;
399 JobInfo1.pPrinterName = (PWSTR)*ppEnd;
400 CopyMemory(*ppEnd, pJob->pPrinter->pwszPrinterName, cbPrinterName);
401
402 if (cbStatus)
403 {
404 *ppEnd -= cbStatus;
405 JobInfo1.pStatus = (PWSTR)*ppEnd;
406 CopyMemory(*ppEnd, pJob->pwszStatus, cbStatus);
407 }
408
409 *ppEnd -= cbUserName;
410 JobInfo1.pUserName = (PWSTR)*ppEnd;
411 CopyMemory(*ppEnd, pJob->pwszUserName, cbUserName);
412
413 // Fill the rest of the structure.
414 JobInfo1.JobId = pJob->dwJobID;
415 JobInfo1.Priority = pJob->dwPriority;
416 JobInfo1.Status = pJob->dwStatus;
417 JobInfo1.TotalPages = pJob->dwTotalPages;
418 CopyMemory(&JobInfo1.Submitted, &pJob->stSubmitted, sizeof(SYSTEMTIME));
419
420 // Finally copy the structure to the output pointer.
421 CopyMemory(*ppStart, &JobInfo1, sizeof(JOB_INFO_1W));
422 *ppStart += sizeof(JOB_INFO_1W);
423 dwErrorCode = ERROR_SUCCESS;
424
425 Cleanup:
426 return dwErrorCode;
427 }
428
429 static DWORD
430 _LocalGetJobLevel2(PLOCAL_PRINTER_HANDLE pPrinterHandle, PLOCAL_JOB pJob, PBYTE* ppStart, PBYTE* ppEnd, DWORD cbBuf, PDWORD pcbNeeded)
431 {
432 DWORD cbDatatype = (wcslen(pJob->pwszDatatype) + 1) * sizeof(WCHAR);
433 DWORD cbDocumentName = (wcslen(pJob->pwszDocumentName) + 1) * sizeof(WCHAR);
434 DWORD cbDriverName = (wcslen(pJob->pPrinter->pwszPrinterDriver) + 1) * sizeof(WCHAR);
435 DWORD cbMachineName = (wcslen(pJob->pwszMachineName) + 1) * sizeof(WCHAR);
436 DWORD cbNotifyName = (wcslen(pJob->pwszNotifyName) + 1) * sizeof(WCHAR);
437 DWORD cbPrinterName = (wcslen(pJob->pPrinter->pwszPrinterName) + 1) * sizeof(WCHAR);
438 DWORD cbPrintProcessor = (wcslen(pJob->pPrintProcessor->pwszName) + 1) * sizeof(WCHAR);
439 DWORD cbPrintProcessorParameters = 0;
440 DWORD cbStatus = 0;
441 DWORD cbUserName = (wcslen(pJob->pwszUserName) + 1) * sizeof(WCHAR);
442 DWORD dwErrorCode;
443 FILETIME ftNow;
444 FILETIME ftSubmitted;
445 JOB_INFO_2W JobInfo2 = { 0 };
446 ULARGE_INTEGER uliNow;
447 ULARGE_INTEGER uliSubmitted;
448
449 // Print Processor Parameters and Status Message are optional.
450 if (pJob->pwszPrintProcessorParameters)
451 cbPrintProcessorParameters = (wcslen(pJob->pwszPrintProcessorParameters) + 1) * sizeof(WCHAR);
452
453 if (pJob->pwszStatus)
454 cbStatus = (wcslen(pJob->pwszStatus) + 1) * sizeof(WCHAR);
455
456 // Check if the supplied buffer is large enough.
457 *pcbNeeded += sizeof(JOB_INFO_2W) + cbDatatype + sizeof(DEVMODEW) + cbDocumentName + cbDriverName + cbMachineName + cbNotifyName + cbPrinterName + cbPrintProcessor + cbPrintProcessorParameters + cbStatus + cbUserName;
458 if (cbBuf < *pcbNeeded)
459 {
460 dwErrorCode = ERROR_INSUFFICIENT_BUFFER;
461 goto Cleanup;
462 }
463
464 // Put the strings at the end of the buffer.
465 *ppEnd -= cbDatatype;
466 JobInfo2.pDatatype = (PWSTR)*ppEnd;
467 CopyMemory(*ppEnd, pJob->pwszDatatype, cbDatatype);
468
469 *ppEnd -= sizeof(DEVMODEW);
470 JobInfo2.pDevMode = (PDEVMODEW)*ppEnd;
471 CopyMemory(*ppEnd, &pJob->DevMode, sizeof(DEVMODEW));
472
473 *ppEnd -= cbDocumentName;
474 JobInfo2.pDocument = (PWSTR)*ppEnd;
475 CopyMemory(*ppEnd, pJob->pwszDocumentName, cbDocumentName);
476
477 *ppEnd -= cbDriverName;
478 JobInfo2.pDriverName = (PWSTR)*ppEnd;
479 CopyMemory(*ppEnd, pJob->pPrinter->pwszPrinterDriver, cbDriverName);
480
481 *ppEnd -= cbMachineName;
482 JobInfo2.pMachineName = (PWSTR)*ppEnd;
483 CopyMemory(*ppEnd, pJob->pwszMachineName, cbMachineName);
484
485 *ppEnd -= cbNotifyName;
486 JobInfo2.pNotifyName = (PWSTR)*ppEnd;
487 CopyMemory(*ppEnd, pJob->pwszNotifyName, cbNotifyName);
488
489 *ppEnd -= cbPrinterName;
490 JobInfo2.pPrinterName = (PWSTR)*ppEnd;
491 CopyMemory(*ppEnd, pJob->pPrinter->pwszPrinterName, cbPrinterName);
492
493 *ppEnd -= cbPrintProcessor;
494 JobInfo2.pPrintProcessor = (PWSTR)*ppEnd;
495 CopyMemory(*ppEnd, pJob->pPrintProcessor->pwszName, cbPrintProcessor);
496
497 if (cbPrintProcessorParameters)
498 {
499 *ppEnd -= cbPrintProcessorParameters;
500 JobInfo2.pParameters = (PWSTR)*ppEnd;
501 CopyMemory(*ppEnd, pJob->pwszPrintProcessorParameters, cbPrintProcessorParameters);
502 }
503
504 if (cbStatus)
505 {
506 *ppEnd -= cbStatus;
507 JobInfo2.pStatus = (PWSTR)*ppEnd;
508 CopyMemory(*ppEnd, pJob->pwszStatus, cbStatus);
509 }
510
511 *ppEnd -= cbUserName;
512 JobInfo2.pUserName = (PWSTR)*ppEnd;
513 CopyMemory(*ppEnd, pJob->pwszUserName, cbUserName);
514
515 // Time in JOB_INFO_2W is the number of milliseconds elapsed since the job was submitted. Calculate this time.
516 if (!SystemTimeToFileTime(&pJob->stSubmitted, &ftSubmitted))
517 {
518 ERR("SystemTimeToFileTime failed with error %lu!\n", GetLastError());
519 return FALSE;
520 }
521
522 GetSystemTimeAsFileTime(&ftNow);
523 uliSubmitted.LowPart = ftSubmitted.dwLowDateTime;
524 uliSubmitted.HighPart = ftSubmitted.dwHighDateTime;
525 uliNow.LowPart = ftNow.dwLowDateTime;
526 uliNow.HighPart = ftNow.dwHighDateTime;
527 JobInfo2.Time = (DWORD)((uliNow.QuadPart - uliSubmitted.QuadPart) / 10000);
528
529 // Position in JOB_INFO_2W is the 1-based index of the job in the processing queue.
530 // Retrieve this through the element index of the job in the Printer's Job List.
531 if (!LookupElementSkiplist(&pJob->pPrinter->JobList, pJob, &JobInfo2.Position))
532 {
533 dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
534 ERR("pJob could not be located in the Printer's Job List!\n");
535 goto Cleanup;
536 }
537
538 // Make the index 1-based.
539 ++JobInfo2.Position;
540
541 // Fill the rest of the structure.
542 JobInfo2.JobId = pJob->dwJobID;
543 JobInfo2.PagesPrinted = pJob->dwPagesPrinted;
544 JobInfo2.Priority = pJob->dwPriority;
545 JobInfo2.StartTime = pJob->dwStartTime;
546 JobInfo2.Status = pJob->dwStatus;
547 JobInfo2.TotalPages = pJob->dwTotalPages;
548 JobInfo2.UntilTime = pJob->dwUntilTime;
549 CopyMemory(&JobInfo2.Submitted, &pJob->stSubmitted, sizeof(SYSTEMTIME));
550
551 // Finally copy the structure to the output pointer.
552 CopyMemory(*ppStart, &JobInfo2, sizeof(JOB_INFO_2W));
553 *ppStart += sizeof(JOB_INFO_2W);
554 dwErrorCode = ERROR_SUCCESS;
555
556 Cleanup:
557 return dwErrorCode;
558 }
559
560 BOOL WINAPI
561 LocalGetJob(HANDLE hPrinter, DWORD JobId, DWORD Level, PBYTE pStart, DWORD cbBuf, LPDWORD pcbNeeded)
562 {
563 DWORD dwErrorCode;
564 PBYTE pEnd = &pStart[cbBuf];
565 PLOCAL_HANDLE pHandle;
566 PLOCAL_JOB pJob;
567 PLOCAL_PRINTER_HANDLE pPrinterHandle;
568
569 // Check if this is a printer handle.
570 pHandle = (PLOCAL_HANDLE)hPrinter;
571 if (pHandle->HandleType != Printer)
572 {
573 dwErrorCode = ERROR_INVALID_HANDLE;
574 goto Cleanup;
575 }
576
577 pPrinterHandle = (PLOCAL_PRINTER_HANDLE)pHandle->pSpecificHandle;
578
579 // Get the desired job.
580 pJob = LookupElementSkiplist(&GlobalJobList, &JobId, NULL);
581 if (!pJob || pJob->pPrinter != pPrinterHandle->pPrinter)
582 {
583 dwErrorCode = ERROR_INVALID_PARAMETER;
584 goto Cleanup;
585 }
586
587 // Begin counting.
588 *pcbNeeded = 0;
589
590 // The function behaves differently for each level.
591 if (Level == 1)
592 dwErrorCode = _LocalGetJobLevel1(pPrinterHandle, pJob, &pStart, &pEnd, cbBuf, pcbNeeded);
593 else if (Level == 2)
594 dwErrorCode = _LocalGetJobLevel2(pPrinterHandle, pJob, &pStart, &pEnd, cbBuf, pcbNeeded);
595 else
596 dwErrorCode = ERROR_INVALID_LEVEL;
597
598 Cleanup:
599 SetLastError(dwErrorCode);
600 return (dwErrorCode == ERROR_SUCCESS);
601 }
602
603 static DWORD
604 _LocalSetJobLevel1(PLOCAL_PRINTER_HANDLE pPrinterHandle, PLOCAL_JOB pJob, PJOB_INFO_1W pJobInfo)
605 {
606 DWORD dwErrorCode;
607
608 // First check the validity of the input before changing anything.
609 if (!FindDatatype(pJob->pPrintProcessor, pJobInfo->pDatatype))
610 {
611 dwErrorCode = ERROR_INVALID_DATATYPE;
612 goto Cleanup;
613 }
614
615 // Check if the datatype has changed.
616 if (wcscmp(pJob->pwszDatatype, pJobInfo->pDatatype) != 0)
617 {
618 // Use the new value.
619 if (!ReallocSplStr(&pJob->pwszDatatype, pJobInfo->pDatatype))
620 {
621 dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
622 ERR("ReallocSplStr failed, last error is %lu!\n", GetLastError());
623 goto Cleanup;
624 }
625 }
626
627 // Check if the document name has changed.
628 if (wcscmp(pJob->pwszDocumentName, pJobInfo->pDocument) != 0)
629 {
630 // Use the new value.
631 if (!ReallocSplStr(&pJob->pwszDocumentName, pJobInfo->pDocument))
632 {
633 dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
634 ERR("ReallocSplStr failed, last error is %lu!\n", GetLastError());
635 goto Cleanup;
636 }
637 }
638
639 // Check if the status message has changed.
640 if ((!pJob->pwszStatus && pJobInfo->pStatus) || wcscmp(pJob->pwszStatus, pJobInfo->pStatus) != 0)
641 {
642 // Use the new value.
643 if (!ReallocSplStr(&pJob->pwszStatus, pJobInfo->pStatus))
644 {
645 dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
646 ERR("ReallocSplStr failed, last error is %lu!\n", GetLastError());
647 goto Cleanup;
648 }
649 }
650
651 // Check if the user name has changed.
652 if (wcscmp(pJob->pwszUserName, pJobInfo->pUserName) != 0)
653 {
654 // The new user name doesn't need to exist, so no additional verification is required.
655
656 // Use the new value.
657 if (!ReallocSplStr(&pJob->pwszUserName, pJobInfo->pUserName))
658 {
659 dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
660 ERR("ReallocSplStr failed, last error is %lu!\n", GetLastError());
661 goto Cleanup;
662 }
663 }
664
665 // Check if the priority has changed.
666 if (pJob->dwPriority != pJobInfo->Priority && IS_VALID_PRIORITY(pJobInfo->Priority))
667 {
668 // Set the new priority.
669 pJob->dwPriority = pJobInfo->Priority;
670
671 // Remove and reinsert the job in the Printer's Job List.
672 // The Compare function will be used to find the right position now considering the new priority.
673 DeleteElementSkiplist(&pJob->pPrinter->JobList, pJob);
674 InsertElementSkiplist(&pJob->pPrinter->JobList, pJob);
675 }
676
677 // Check if the status flags have changed.
678 if (pJob->dwStatus != pJobInfo->Status)
679 {
680 // Only add status flags that make sense.
681 if (pJobInfo->Status & JOB_STATUS_PAUSED)
682 pJob->dwStatus |= JOB_STATUS_PAUSED;
683
684 if (pJobInfo->Status & JOB_STATUS_ERROR)
685 pJob->dwStatus |= JOB_STATUS_ERROR;
686
687 if (pJobInfo->Status & JOB_STATUS_OFFLINE)
688 pJob->dwStatus |= JOB_STATUS_OFFLINE;
689
690 if (pJobInfo->Status & JOB_STATUS_PAPEROUT)
691 pJob->dwStatus |= JOB_STATUS_PAPEROUT;
692 }
693
694 dwErrorCode = ERROR_SUCCESS;
695
696 Cleanup:
697 return dwErrorCode;
698 }
699
700 static DWORD
701 _LocalSetJobLevel2(PLOCAL_PRINTER_HANDLE pPrinterHandle, PLOCAL_JOB pJob, PJOB_INFO_2W pJobInfo)
702 {
703 DWORD dwErrorCode;
704 PLOCAL_PRINT_PROCESSOR pPrintProcessor;
705
706 // First check the validity of the input before changing anything.
707 pPrintProcessor = FindPrintProcessor(pJobInfo->pPrintProcessor);
708 if (!pPrintProcessor)
709 {
710 dwErrorCode = ERROR_UNKNOWN_PRINTPROCESSOR;
711 goto Cleanup;
712 }
713
714 if (!FindDatatype(pPrintProcessor, pJobInfo->pDatatype))
715 {
716 dwErrorCode = ERROR_INVALID_DATATYPE;
717 goto Cleanup;
718 }
719
720 // Check if the datatype has changed.
721 if (wcscmp(pJob->pwszDatatype, pJobInfo->pDatatype) != 0)
722 {
723 // Use the new value.
724 if (!ReallocSplStr(&pJob->pwszDatatype, pJobInfo->pDatatype))
725 {
726 dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
727 ERR("ReallocSplStr failed, last error is %lu!\n", GetLastError());
728 goto Cleanup;
729 }
730 }
731
732 // Check if the document name has changed.
733 if (wcscmp(pJob->pwszDocumentName, pJobInfo->pDocument) != 0)
734 {
735 // Use the new value.
736 if (!ReallocSplStr(&pJob->pwszDocumentName, pJobInfo->pDocument))
737 {
738 dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
739 ERR("ReallocSplStr failed, last error is %lu!\n", GetLastError());
740 goto Cleanup;
741 }
742 }
743
744 // Check if the notify name has changed.
745 if (wcscmp(pJob->pwszNotifyName, pJobInfo->pNotifyName) != 0)
746 {
747 // The new notify name doesn't need to exist, so no additional verification is required.
748
749 // Use the new value.
750 if (!ReallocSplStr(&pJob->pwszNotifyName, pJobInfo->pNotifyName))
751 {
752 dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
753 ERR("ReallocSplStr failed, last error is %lu!\n", GetLastError());
754 goto Cleanup;
755 }
756 }
757
758 // Check if the (optional) Print Processor Parameters have changed.
759 if ((!pJob->pwszPrintProcessorParameters && pJobInfo->pParameters) || wcscmp(pJob->pwszPrintProcessorParameters, pJobInfo->pParameters) != 0)
760 {
761 // Use the new value.
762 if (!ReallocSplStr(&pJob->pwszPrintProcessorParameters, pJobInfo->pParameters))
763 {
764 dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
765 ERR("ReallocSplStr failed, last error is %lu!\n", GetLastError());
766 goto Cleanup;
767 }
768 }
769
770 // Check if the (optional) Status Message has changed.
771 if ((!pJob->pwszStatus && pJobInfo->pStatus) || wcscmp(pJob->pwszStatus, pJobInfo->pStatus) != 0)
772 {
773 // Use the new value.
774 if (!ReallocSplStr(&pJob->pwszStatus, pJobInfo->pStatus))
775 {
776 dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
777 ERR("ReallocSplStr failed, last error is %lu!\n", GetLastError());
778 goto Cleanup;
779 }
780 }
781
782 // Check if the user name has changed.
783 if (wcscmp(pJob->pwszUserName, pJobInfo->pUserName) != 0)
784 {
785 // The new user name doesn't need to exist, so no additional verification is required.
786
787 // Use the new value.
788 if (!ReallocSplStr(&pJob->pwszUserName, pJobInfo->pUserName))
789 {
790 dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
791 ERR("ReallocSplStr failed, last error is %lu!\n", GetLastError());
792 goto Cleanup;
793 }
794 }
795
796 // Check if the priority has changed.
797 if (pJob->dwPriority != pJobInfo->Priority && IS_VALID_PRIORITY(pJobInfo->Priority))
798 {
799 // Set the new priority.
800 pJob->dwPriority = pJobInfo->Priority;
801
802 // Remove and reinsert the job in the Printer's Job List.
803 // The Compare function will be used to find the right position now considering the new priority.
804 DeleteElementSkiplist(&pJob->pPrinter->JobList, pJob);
805 InsertElementSkiplist(&pJob->pPrinter->JobList, pJob);
806 }
807
808 // Check if the status flags have changed.
809 if (pJob->dwStatus != pJobInfo->Status)
810 {
811 // Only add status flags that make sense.
812 if (pJobInfo->Status & JOB_STATUS_PAUSED)
813 pJob->dwStatus |= JOB_STATUS_PAUSED;
814
815 if (pJobInfo->Status & JOB_STATUS_ERROR)
816 pJob->dwStatus |= JOB_STATUS_ERROR;
817
818 if (pJobInfo->Status & JOB_STATUS_OFFLINE)
819 pJob->dwStatus |= JOB_STATUS_OFFLINE;
820
821 if (pJobInfo->Status & JOB_STATUS_PAPEROUT)
822 pJob->dwStatus |= JOB_STATUS_PAPEROUT;
823 }
824
825 dwErrorCode = ERROR_SUCCESS;
826
827 Cleanup:
828 return dwErrorCode;
829 }
830
831 BOOL WINAPI
832 LocalSetJob(HANDLE hPrinter, DWORD JobId, DWORD Level, PBYTE pJobInfo, DWORD Command)
833 {
834 DWORD dwErrorCode;
835 PLOCAL_HANDLE pHandle;
836 PLOCAL_JOB pJob;
837 PLOCAL_PRINTER_HANDLE pPrinterHandle;
838
839 // Check if this is a printer handle.
840 pHandle = (PLOCAL_HANDLE)hPrinter;
841 if (pHandle->HandleType != Printer)
842 {
843 dwErrorCode = ERROR_INVALID_HANDLE;
844 goto Cleanup;
845 }
846
847 pPrinterHandle = (PLOCAL_PRINTER_HANDLE)pHandle->pSpecificHandle;
848
849 // Get the desired job.
850 pJob = LookupElementSkiplist(&GlobalJobList, &JobId, NULL);
851 if (!pJob || pJob->pPrinter != pPrinterHandle->pPrinter)
852 {
853 dwErrorCode = ERROR_INVALID_PARAMETER;
854 goto Cleanup;
855 }
856
857 // Setting job information is handled differently for each level.
858 if (Level)
859 {
860 if (Level == 1)
861 dwErrorCode = _LocalSetJobLevel1(pPrinterHandle, pJob, (PJOB_INFO_1W)pJobInfo);
862 else if (Level == 2)
863 dwErrorCode = _LocalSetJobLevel2(pPrinterHandle, pJob, (PJOB_INFO_2W)pJobInfo);
864 else
865 dwErrorCode = ERROR_INVALID_LEVEL;
866 }
867
868 if (dwErrorCode != ERROR_SUCCESS)
869 goto Cleanup;
870
871 // Perform an additional command if desired.
872 if (Command)
873 {
874 // TODO
875 }
876
877 dwErrorCode = ERROR_SUCCESS;
878
879 Cleanup:
880 SetLastError(dwErrorCode);
881 return (dwErrorCode == ERROR_SUCCESS);
882 }
883
884 BOOL WINAPI
885 LocalEnumJobs(HANDLE hPrinter, DWORD FirstJob, DWORD NoJobs, DWORD Level, PBYTE pStart, DWORD cbBuf, LPDWORD pcbNeeded, LPDWORD pcReturned)
886 {
887 DWORD dwErrorCode;
888 PBYTE pEnd = &pStart[cbBuf];
889 PLOCAL_HANDLE pHandle;
890 PLOCAL_JOB pJob;
891 PSKIPLIST_NODE pFirstJobNode;
892 PSKIPLIST_NODE pNode;
893 PLOCAL_PRINTER_HANDLE pPrinterHandle;
894
895 // Check if this is a printer handle.
896 pHandle = (PLOCAL_HANDLE)hPrinter;
897 if (pHandle->HandleType != Printer)
898 {
899 dwErrorCode = ERROR_INVALID_HANDLE;
900 goto Cleanup;
901 }
902
903 pPrinterHandle = (PLOCAL_PRINTER_HANDLE)pHandle->pSpecificHandle;
904
905 // Check the level.
906 if (Level > 2)
907 {
908 dwErrorCode = ERROR_INVALID_LEVEL;
909 goto Cleanup;
910 }
911
912 // Begin counting.
913 *pcbNeeded = 0;
914 *pcReturned = 0;
915
916 // Lookup the node of the first job requested by the caller in the Printer's Job List.
917 pFirstJobNode = LookupNodeByIndexSkiplist(&pPrinterHandle->pPrinter->JobList, FirstJob);
918
919 // Count the required buffer size and the number of jobs.
920 pNode = pFirstJobNode;
921
922 while (*pcReturned < NoJobs && pNode)
923 {
924 pJob = (PLOCAL_JOB)pNode->Element;
925
926 // The function behaves differently for each level.
927 if (Level == 1)
928 _LocalGetJobLevel1(pPrinterHandle, pJob, NULL, NULL, 0, pcbNeeded);
929 else if (Level == 2)
930 _LocalGetJobLevel2(pPrinterHandle, pJob, NULL, NULL, 0, pcbNeeded);
931
932 // We stop either when there are no more jobs in the list or when the caller didn't request more, whatever comes first.
933 *pcReturned++;
934 pNode = pNode->Next[0];
935 }
936
937 // Check if the supplied buffer is large enough.
938 if (cbBuf < *pcbNeeded)
939 {
940 dwErrorCode = ERROR_INSUFFICIENT_BUFFER;
941 goto Cleanup;
942 }
943
944 // Begin counting again and also empty the given buffer.
945 *pcbNeeded = 0;
946 *pcReturned = 0;
947 ZeroMemory(pStart, cbBuf);
948
949 // Now call the same functions again to copy the actual data for each job into the buffer.
950 pNode = pFirstJobNode;
951
952 while (*pcReturned < NoJobs && pNode)
953 {
954 pJob = (PLOCAL_JOB)pNode->Element;
955
956 // The function behaves differently for each level.
957 if (Level == 1)
958 dwErrorCode = _LocalGetJobLevel1(pPrinterHandle, pJob, &pStart, &pEnd, cbBuf, pcbNeeded);
959 else if (Level == 2)
960 dwErrorCode = _LocalGetJobLevel2(pPrinterHandle, pJob, &pStart, &pEnd, cbBuf, pcbNeeded);
961
962 if (dwErrorCode != ERROR_SUCCESS)
963 goto Cleanup;
964
965 // We stop either when there are no more jobs in the list or when the caller didn't request more, whatever comes first.
966 *pcReturned++;
967 pNode = pNode->Next[0];
968 }
969
970 dwErrorCode = ERROR_SUCCESS;
971
972 Cleanup:
973 SetLastError(dwErrorCode);
974 return (dwErrorCode == ERROR_SUCCESS);
975 }
976
977 PLOCAL_JOB
978 ReadJobShadowFile(PCWSTR pwszFilePath)
979 {
980 DWORD cbFileSize;
981 DWORD cbRead;
982 HANDLE hFile = INVALID_HANDLE_VALUE;
983 PLOCAL_JOB pJob;
984 PLOCAL_JOB pReturnValue = NULL;
985 PLOCAL_PRINTER pPrinter;
986 PSHD_HEADER pShadowFile = NULL;
987 PWSTR pwszPrinterName;
988
989 // Try to open the file.
990 hFile = CreateFileW(pwszFilePath, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
991 if (hFile == INVALID_HANDLE_VALUE)
992 {
993 ERR("CreateFileW failed with error %lu for file \"%S\"!\n", GetLastError(), pwszFilePath);
994 goto Cleanup;
995 }
996
997 // Get its file size (small enough for a single DWORD) and allocate memory for all of it.
998 cbFileSize = GetFileSize(hFile, NULL);
999 pShadowFile = DllAllocSplMem(cbFileSize);
1000 if (!pShadowFile)
1001 {
1002 ERR("DllAllocSplMem failed with error %lufor file \"%S\"!\n", GetLastError(), pwszFilePath);
1003 goto Cleanup;
1004 }
1005
1006 // Read the entire file.
1007 if (!ReadFile(hFile, pShadowFile, cbFileSize, &cbRead, NULL))
1008 {
1009 ERR("ReadFile failed with error %lu for file \"%S\"!\n", GetLastError(), pwszFilePath);
1010 goto Cleanup;
1011 }
1012
1013 // Check signature and header size.
1014 if (pShadowFile->dwSignature != SHD_WIN2003_SIGNATURE || pShadowFile->cbHeader != sizeof(SHD_HEADER))
1015 {
1016 ERR("Signature or Header Size mismatch for file \"%S\"!\n", pwszFilePath);
1017 goto Cleanup;
1018 }
1019
1020 // Retrieve the associated printer from the list.
1021 pwszPrinterName = (PWSTR)((ULONG_PTR)pShadowFile + pShadowFile->offPrinterName);
1022 pPrinter = LookupElementSkiplist(&PrinterList, &pwszPrinterName, NULL);
1023 if (!pPrinter)
1024 {
1025 ERR("Shadow file \"%S\" references a non-existing printer!\n", pwszFilePath);
1026 goto Cleanup;
1027 }
1028
1029 // Create a new job structure and copy over the relevant fields.
1030 pJob = DllAllocSplMem(sizeof(LOCAL_JOB));
1031 if (!pJob)
1032 {
1033 ERR("DllAllocSplMem failed with error %lu for file \"%S\"!\n", GetLastError(), pwszFilePath);
1034 goto Cleanup;
1035 }
1036
1037 pJob->dwJobID = pShadowFile->dwJobID;
1038 pJob->dwTotalPages = pShadowFile->dwTotalPages;
1039 pJob->dwPriority = pShadowFile->dwPriority;
1040 pJob->pPrinter = pPrinter;
1041 pJob->pwszDatatype = AllocSplStr((PCWSTR)((ULONG_PTR)pShadowFile + pShadowFile->offDatatype));
1042 pJob->pwszDocumentName = AllocSplStr((PCWSTR)((ULONG_PTR)pShadowFile + pShadowFile->offDocumentName));
1043 pJob->pwszOutputFile = NULL;
1044 CopyMemory(&pJob->stSubmitted, &pShadowFile->stSubmitted, sizeof(SYSTEMTIME));
1045 CopyMemory(&pJob->DevMode, (PDEVMODEW)((ULONG_PTR)pShadowFile + pShadowFile->offDevMode), sizeof(DEVMODEW));
1046
1047 pReturnValue = pJob;
1048
1049 Cleanup:
1050 if (pShadowFile)
1051 DllFreeSplMem(pShadowFile);
1052
1053 if (hFile != INVALID_HANDLE_VALUE)
1054 CloseHandle(hFile);
1055
1056 return pReturnValue;
1057 }
1058
1059 BOOL
1060 WriteJobShadowFile(PCWSTR pwszFilePath, const PLOCAL_JOB pJob)
1061 {
1062 BOOL bReturnValue = FALSE;
1063 DWORD cbDatatype;
1064 DWORD cbDocumentName;
1065 DWORD cbFileSize;
1066 DWORD cbPrinterName;
1067 DWORD cbWritten;
1068 DWORD dwCurrentOffset;
1069 HANDLE hFile = INVALID_HANDLE_VALUE;
1070 PSHD_HEADER pShadowFile = NULL;
1071
1072 // Try to open the file.
1073 hFile = CreateFileW(pwszFilePath, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, 0, NULL);
1074 if (hFile == INVALID_HANDLE_VALUE)
1075 {
1076 ERR("CreateFileW failed with error %lu for file \"%S\"!\n", GetLastError(), pwszFilePath);
1077 goto Cleanup;
1078 }
1079
1080 // Compute the total size of the shadow file.
1081 cbPrinterName = (wcslen(pJob->pPrinter->pwszPrinterName) + 1) * sizeof(WCHAR);
1082 cbDatatype = (wcslen(pJob->pwszDatatype) + 1) * sizeof(WCHAR);
1083 cbDocumentName = (wcslen(pJob->pwszDocumentName) + 1) * sizeof(WCHAR);
1084 cbFileSize = sizeof(SHD_HEADER) + cbPrinterName + cbDatatype + cbDocumentName + sizeof(DEVMODEW);
1085
1086 // Allocate memory for it.
1087 pShadowFile = DllAllocSplMem(cbFileSize);
1088 if (!pShadowFile)
1089 {
1090 ERR("DllAllocSplMem failed with error %lu for file \"%S\"!\n", GetLastError(), pwszFilePath);
1091 goto Cleanup;
1092 }
1093
1094 // Fill out the shadow file header information.
1095 pShadowFile->dwSignature = SHD_WIN2003_SIGNATURE;
1096 pShadowFile->cbHeader = sizeof(SHD_HEADER);
1097
1098 // Copy the values.
1099 pShadowFile->dwJobID = pJob->dwJobID;
1100 pShadowFile->dwTotalPages = pJob->dwTotalPages;
1101 pShadowFile->dwPriority = pJob->dwPriority;
1102 CopyMemory(&pShadowFile->stSubmitted, &pJob->stSubmitted, sizeof(SYSTEMTIME));
1103
1104 // Add the extra values that are stored as offsets in the shadow file.
1105 // The first value begins right after the shadow file header.
1106 dwCurrentOffset = sizeof(SHD_HEADER);
1107
1108 CopyMemory((PBYTE)pShadowFile + dwCurrentOffset, pJob->pPrinter->pwszPrinterName, cbPrinterName);
1109 pShadowFile->offPrinterName = dwCurrentOffset;
1110 dwCurrentOffset += cbPrinterName;
1111
1112 CopyMemory((PBYTE)pShadowFile + dwCurrentOffset, pJob->pwszDatatype, cbDatatype);
1113 pShadowFile->offDatatype = dwCurrentOffset;
1114 dwCurrentOffset += cbDatatype;
1115
1116 CopyMemory((PBYTE)pShadowFile + dwCurrentOffset, pJob->pwszDocumentName, cbDocumentName);
1117 pShadowFile->offDocumentName = dwCurrentOffset;
1118 dwCurrentOffset += cbDocumentName;
1119
1120 CopyMemory((PBYTE)pShadowFile + dwCurrentOffset, &pJob->DevMode, sizeof(DEVMODEW));
1121 pShadowFile->offDevMode = dwCurrentOffset;
1122 dwCurrentOffset += sizeof(DEVMODEW);
1123
1124 // Write the file.
1125 if (!WriteFile(hFile, pShadowFile, cbFileSize, &cbWritten, NULL))
1126 {
1127 ERR("WriteFile failed with error %lu for file \"%S\"!\n", GetLastError(), pwszFilePath);
1128 goto Cleanup;
1129 }
1130
1131 bReturnValue = TRUE;
1132
1133 Cleanup:
1134 if (pShadowFile)
1135 DllFreeSplMem(pShadowFile);
1136
1137 if (hFile != INVALID_HANDLE_VALUE)
1138 CloseHandle(hFile);
1139
1140 return bReturnValue;
1141 }
1142
1143 BOOL
1144 FreeJob(PLOCAL_JOB pJob)
1145 {
1146 ////////// TODO /////////
1147 /// Add some checks
1148 DllFreeSplStr(pJob->pwszDatatype);
1149 DllFreeSplStr(pJob->pwszDocumentName);
1150 DllFreeSplStr(pJob->pwszOutputFile);
1151 DllFreeSplMem(pJob);
1152
1153 return TRUE;
1154 }