📄 job.c
字号:
*prev; /* Previous job */ int jobid, /* Current job ID */ fileid; /* Current file ID */ ipp_attribute_t *attr; /* Job attribute */ char method[HTTP_MAX_URI], /* Method portion of URI */ username[HTTP_MAX_URI], /* Username portion of URI */ host[HTTP_MAX_URI], /* Host portion of URI */ resource[HTTP_MAX_URI]; /* Resource portion of URI */ int port; /* Port portion of URI */ printer_t *p; /* Printer or class */ const char *dest; /* Destination */ mime_type_t **filetypes; /* New filetypes array */ int *compressions; /* New compressions array */ /* * First open the requests directory... */ LogMessage(L_DEBUG, "LoadAllJobs: Scanning %s...", RequestRoot); NumJobs = 0; if ((dir = opendir(RequestRoot)) == NULL) { LogMessage(L_ERROR, "LoadAllJobs: Unable to open spool directory %s: %s", RequestRoot, strerror(errno)); return; } /* * Read all the c##### files... */ while ((dent = readdir(dir)) != NULL) if (NAMLEN(dent) >= 6 && dent->d_name[0] == 'c') { /* * Allocate memory for the job... */ if ((job = calloc(sizeof(job_t), 1)) == NULL) { LogMessage(L_ERROR, "LoadAllJobs: Ran out of memory for jobs!"); closedir(dir); return; } if ((job->attrs = ippNew()) == NULL) { free(job); LogMessage(L_ERROR, "LoadAllJobs: Ran out of memory for job attributes!"); closedir(dir); return; } /* * Assign the job ID... */ job->id = atoi(dent->d_name + 1); job->pipe = -1; LogMessage(L_DEBUG, "LoadAllJobs: Loading attributes for job %d...\n", job->id); if (job->id >= NextJobId) NextJobId = job->id + 1; /* * Load the job control file... */ snprintf(filename, sizeof(filename), "%s/%s", RequestRoot, dent->d_name); if ((fd = open(filename, O_RDONLY)) < 0) { LogMessage(L_ERROR, "LoadAllJobs: Unable to open job control file \"%s\" - %s!", filename, strerror(errno)); ippDelete(job->attrs); free(job); unlink(filename); continue; } else { if (ippReadFile(fd, job->attrs) != IPP_DATA) { LogMessage(L_ERROR, "LoadAllJobs: Unable to read job control file \"%s\"!", filename); close(fd); ippDelete(job->attrs); free(job); unlink(filename); continue; } close(fd); } if ((job->state = ippFindAttribute(job->attrs, "job-state", IPP_TAG_ENUM)) == NULL) { LogMessage(L_ERROR, "LoadAllJobs: Missing or bad job-state attribute in control file \"%s\"!", filename); ippDelete(job->attrs); free(job); unlink(filename); continue; } if ((attr = ippFindAttribute(job->attrs, "job-printer-uri", IPP_TAG_URI)) == NULL) { LogMessage(L_ERROR, "LoadAllJobs: No job-printer-uri attribute in control file \"%s\"!", filename); ippDelete(job->attrs); free(job); unlink(filename); continue; } httpSeparate(attr->values[0].string.text, method, username, host, &port, resource); if ((dest = ValidateDest(host, resource, &(job->dtype))) == NULL && job->state != NULL && job->state->values[0].integer <= IPP_JOB_PROCESSING) { /* * Job queued on remote printer or class, so add it... */ if (strncmp(resource, "/classes/", 9) == 0) { p = AddClass(resource + 9); SetString(&p->make_model, "Remote Class on unknown"); } else { p = AddPrinter(resource + 10); SetString(&p->make_model, "Remote Printer on unknown"); } p->state = IPP_PRINTER_STOPPED; p->type |= CUPS_PRINTER_REMOTE; p->browse_time = 2147483647; SetString(&p->location, "Location Unknown"); SetString(&p->info, "No Information Available"); p->hostname[0] = '\0'; SetPrinterAttrs(p); dest = p->name; } if (dest == NULL) { LogMessage(L_ERROR, "LoadAllJobs: Unable to queue job for destination \"%s\"!", attr->values[0].string.text); ippDelete(job->attrs); free(job); unlink(filename); continue; } SetString(&job->dest, dest); job->sheets = ippFindAttribute(job->attrs, "job-media-sheets-completed", IPP_TAG_INTEGER); job->job_sheets = ippFindAttribute(job->attrs, "job-sheets", IPP_TAG_NAME); if ((attr = ippFindAttribute(job->attrs, "job-priority", IPP_TAG_INTEGER)) == NULL) { LogMessage(L_ERROR, "LoadAllJobs: Missing or bad job-priority attribute in control file \"%s\"!", filename); ippDelete(job->attrs); free(job); unlink(filename); continue; } job->priority = attr->values[0].integer; if ((attr = ippFindAttribute(job->attrs, "job-originating-user-name", IPP_TAG_NAME)) == NULL) { LogMessage(L_ERROR, "LoadAllJobs: Missing or bad job-originating-user-name attribute in control file \"%s\"!", filename); ippDelete(job->attrs); free(job); unlink(filename); continue; } SetString(&job->username, attr->values[0].string.text); /* * Insert the job into the array, sorting by job priority and ID... */ for (current = Jobs, prev = NULL; current != NULL; prev = current, current = current->next) if (job->priority > current->priority) break; else if (job->priority == current->priority && job->id < current->id) break; job->next = current; if (prev != NULL) prev->next = job; else Jobs = job; NumJobs ++; /* * Set the job hold-until time and state... */ if (job->state->values[0].integer == IPP_JOB_HELD) { if ((attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_KEYWORD)) == NULL) attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME); if (attr == NULL) job->state->values[0].integer = IPP_JOB_PENDING; else SetJobHoldUntil(job->id, attr->values[0].string.text); } else if (job->state->values[0].integer == IPP_JOB_PROCESSING) job->state->values[0].integer = IPP_JOB_PENDING; } /* * Read all the d##### files... */ rewinddir(dir); while ((dent = readdir(dir)) != NULL) if (NAMLEN(dent) > 7 && dent->d_name[0] == 'd' && strchr(dent->d_name, '-')) { /* * Find the job... */ jobid = atoi(dent->d_name + 1); fileid = atoi(strchr(dent->d_name, '-') + 1); LogMessage(L_DEBUG, "LoadAllJobs: Auto-typing document file %s...", dent->d_name); snprintf(filename, sizeof(filename), "%s/%s", RequestRoot, dent->d_name); if ((job = FindJob(jobid)) == NULL) { LogMessage(L_ERROR, "LoadAllJobs: Orphaned print file \"%s\"!", filename); unlink(filename); continue; } if (fileid > job->num_files) { if (job->num_files == 0) { compressions = (int *)calloc(fileid, sizeof(int)); filetypes = (mime_type_t **)calloc(fileid, sizeof(mime_type_t *)); } else { compressions = (int *)realloc(job->compressions, sizeof(int) * fileid); filetypes = (mime_type_t **)realloc(job->filetypes, sizeof(mime_type_t *) * fileid); } if (compressions == NULL || filetypes == NULL) { LogMessage(L_ERROR, "LoadAllJobs: Ran out of memory for job file types!"); continue; } job->compressions = compressions; job->filetypes = filetypes; job->num_files = fileid; } job->filetypes[fileid - 1] = mimeFileType(MimeDatabase, filename, job->compressions + fileid - 1); if (job->filetypes[fileid - 1] == NULL) job->filetypes[fileid - 1] = mimeType(MimeDatabase, "application", "vnd.cups-raw"); } closedir(dir); /* * Clean out old jobs as needed... */ CleanJobs();}/* * 'MoveJob()' - Move the specified job to a different destination. */voidMoveJob(int id, /* I - Job ID */ const char *dest) /* I - Destination */{ job_t *current;/* Current job */ ipp_attribute_t *attr; /* job-printer-uri attribute */ printer_t *p; /* Destination printer or class */ if ((p = FindPrinter(dest)) == NULL) p = FindClass(dest); if (p == NULL) return; for (current = Jobs; current != NULL; current = current->next) if (current->id == id) { if (current->state->values[0].integer >= IPP_JOB_PROCESSING) break; SetString(¤t->dest, dest); current->dtype = p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT); if ((attr = ippFindAttribute(current->attrs, "job-printer-uri", IPP_TAG_URI)) != NULL) { free(attr->values[0].string.text); attr->values[0].string.text = strdup(p->uri); } SaveJob(current->id); return; }}/* * 'ReleaseJob()' - Release the specified job. */voidReleaseJob(int id) /* I - Job ID */{ job_t *job; /* Job data */ LogMessage(L_DEBUG, "ReleaseJob: id = %d", id); if ((job = FindJob(id)) == NULL) return; if (job->state->values[0].integer == IPP_JOB_HELD) { DEBUG_puts("ReleaseJob: setting state to pending..."); job->state->values[0].integer = IPP_JOB_PENDING; SaveJob(id); CheckJobs(); }}/* * 'RestartJob()' - Restart the specified job. */voidRestartJob(int id) /* I - Job ID */{ job_t *job; /* Job data */ if ((job = FindJob(id)) == NULL) return; if (job->state->values[0].integer == IPP_JOB_STOPPED || JobFiles) { job->tries = 0; job->state->values[0].integer = IPP_JOB_PENDING; SaveJob(id); CheckJobs(); }}/* * 'SaveJob()' - Save a job to disk. */voidSaveJob(int id) /* I - Job ID */{ job_t *job; /* Pointer to job */ char filename[1024]; /* Job control filename */ int fd; /* File descriptor */ if ((job = FindJob(id)) == NULL) return; snprintf(filename, sizeof(filename), "%s/c%05d", RequestRoot, id); if ((fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600)) < 0) { LogMessage(L_ERROR, "SaveJob: Unable to create job control file \"%s\" - %s.", filename, strerror(errno)); return; } fchmod(fd, 0600); fchown(fd, RunUser, Group); ippWriteFile(fd, job->attrs); LogMessage(L_DEBUG2, "SaveJob: Closing file %d...", fd); close(fd);}/* * 'SetJobHoldUntil()' - Set the hold time for a job... */voidSetJobHoldUntil(int id, /* I - Job ID */ const char *when) /* I - When to resume */{ job_t *job; /* Pointer to job */ time_t curtime; /* Current time */ struct tm *curdate; /* Current date */ int hour; /* Hold hour */ int minute; /* Hold minute */ int second; /* Hold second */ LogMessage(L_DEBUG, "SetJobHoldUntil(%d, \"%s\")", id, when); if ((job = FindJob(id)) == NULL) return; second = 0; if (strcmp(when, "indefinite") == 0) { /* * Hold indefinitely... */ job->hold_until = 0; } else if (strcmp(when, "day-time") == 0) { /* * Hold to 6am the next morning unless local time is < 6pm. */ curtime = time(NULL); curdate = localtime(&curtime); if (curdate->tm_hour < 18) job->hold_until = curtime; else job->hold_until = curtime + ((29 - curdate->tm_hour) * 60 + 59 - curdate->tm_min) * 60 + 60 - curdate->tm_sec; } else if (strcmp(when, "evening") == 0 || strcmp(when, "night") == 0) { /* * Hold to 6pm unless local time is > 6pm or < 6am. */ curtime = time(NULL); curdate = localtime(&curtime); if (curdate->tm_hour < 6 || curdate->tm_hour >= 18) job->hold_until = curtime; else job->hold_until = curtime + ((17 - curdate->tm_hour) * 60 + 59 - curdate->tm_min) * 60 + 60 - curdate->tm_sec; } else if (strcmp(when, "second-shift") == 0) { /* * Hold to 4pm unless local time is > 4pm. */ curtime = time(NULL); curdate = localtime(&curtime); if (curdate->tm_hour >= 16) job->hold_until = curtime; else job->hold_until = curtime + ((15 - curdate->tm_hour) * 60 + 59 - curdate->tm_min) * 60 + 60 - curdate->tm_sec; } else if (strcmp(when, "third-shift") == 0) { /* * Hold to 12am unless local time is < 8am. */ curtime = time(NULL); curdate = localtime(&curtime); if (curdate->tm_hour < 8) job->hold_until = curtime;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -