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