ptg10805159
Section 21.5 Source Code 823
318 while ((entp = readdir(dirp)) != NULL) {
319 /*
320 * Skip "." and ".."
321 */
322 if (strcmp(entp->d_name, ".") == 0 ||
323 strcmp(entp->d_name, "..") == 0)
324 continue;
325 /*
326 * Read the request structure.
327 */
328 sprintf(fname, "%s/%s/%s", SPOOLDIR, REQDIR, entp->d_name);
329 if ((fd = open(fname, O_RDONLY)) < 0)
330 continue;
331 nr=read(fd, &req, sizeof(struct printreq));
332 if (nr != sizeof(struct printreq)) {
333 if (nr < 0)
334 err =errno;
335 else
336 err =EIO;
337 close(fd);
338 log_msg("build_qonstart: can’t read %s: %s",
339 fname, strerror(err));
340 unlink(fname);
341 sprintf(fname, "%s/%s/%s", SPOOLDIR, DATADIR,
342 entp->d_name);
343 unlink(fname);
344 continue;
345 }
346 jobid=atol(entp->d_name);
347 log_msg("adding job %d to queue", jobid);
348 add_job(&req, jobid);
349 }
350 closedir(dirp);
351 }
[318 – 324] We read each entry in the directory,one at a time.We skip the entries for dot
and dot-dot.
[325 – 345] For each entry, we create the full pathname of the file and open it for
reading. If theopencall fails, we just skip the file. Otherwise, we read the
printreqstructurestored in it. If we don’t read the entirestructure, we
close the file, log an error message, and unlink the file. Then we create the
full pathname of the corresponding data file and unlink it, too.
[346 – 351] If we wereable to read a completeprintreqstructure, we convert the
filename into a job ID (the name of the file is its job ID), log a message, and
then add the request to the list of pending print jobs. When we aredone
reading the directory,readdirwill returnNULL,and we close the directory
and return.