예제 #1
0
파일: meta.c 프로젝트: Gwenio/DragonFlyBSD
Boolean
meta_oodate(GNode *gn, Boolean oodate)
{
    static char *tmpdir = NULL;
    static char cwd[MAXPATHLEN];
    char ldir_vname[64];
    char latestdir[MAXPATHLEN];
    char fname[MAXPATHLEN];
    char fname1[MAXPATHLEN];
    char fname2[MAXPATHLEN];
    char *p;
    char *cp;
    static size_t cwdlen = 0;
    static size_t tmplen = 0;
    FILE *fp;
    Boolean ignoreOODATE = FALSE;
    Lst missingFiles;
    
    if (oodate)
	return oodate;		/* we're done */

    missingFiles = Lst_Init(FALSE);

    /*
     * We need to check if the target is out-of-date. This includes
     * checking if the expanded command has changed. This in turn
     * requires that all variables are set in the same way that they
     * would be if the target needs to be re-built.
     */
    Make_DoAllVar(gn);

    meta_name(gn, fname, sizeof(fname), NULL, NULL);

#ifdef DEBUG_META_MODE
    if (DEBUG(META))
	fprintf(debug_file, "meta_oodate: %s\n", fname);
#endif

    if ((fp = fopen(fname, "r")) != NULL) {
	static char *buf = NULL;
	static size_t bufsz;
	int lineno = 0;
	int lastpid = 0;
	int pid;
	int f = 0;
	int x;
	LstNode ln;
	struct stat fs;

	if (!buf) {
	    bufsz = 8 * BUFSIZ;
	    buf = bmake_malloc(bufsz);
	}

	if (!cwdlen) {
	    if (getcwd(cwd, sizeof(cwd)) == NULL)
		err(1, "Could not get current working directory");
	    cwdlen = strlen(cwd);
	}

	if (!tmpdir) {
	    tmpdir = getTmpdir();
	    tmplen = strlen(tmpdir);
	}

	/* we want to track all the .meta we read */
	Var_Append(".MAKE.META.FILES", fname, VAR_GLOBAL);

	ln = Lst_First(gn->commands);
	while (!oodate && (x = fgetLine(&buf, &bufsz, 0, fp)) > 0) {
	    lineno++;
	    if (buf[x - 1] == '\n')
		buf[x - 1] = '\0';
	    else {
		warnx("%s: %d: line truncated at %u", fname, lineno, x);
		oodate = TRUE;
		break;
	    }
	    /* Find the start of the build monitor section. */
	    if (!f) {
		if (strncmp(buf, "-- filemon", 10) == 0) {
		    f = 1;
		    continue;
		}
		if (strncmp(buf, "# buildmon", 10) == 0) {
		    f = 1;
		    continue;
		}
	    }		    

	    /* Delimit the record type. */
	    p = buf;
#ifdef DEBUG_META_MODE
	    if (DEBUG(META))
		fprintf(debug_file, "%s: %d: %s\n", fname, lineno, buf);
#endif
	    strsep(&p, " ");
	    if (f) {
		/*
		 * We are in the 'filemon' output section.
		 * Each record from filemon follows the general form:
		 *
		 * <key> <pid> <data>
		 *
		 * Where:
		 * <key> is a single letter, denoting the syscall.
		 * <pid> is the process that made the syscall.
		 * <data> is the arguments (of interest).
		 */
		switch(buf[0]) {
		case '#':		/* comment */
		case 'V':		/* version */
		    break;
		default:
		    /*
		     * We need to track pathnames per-process.
		     *
		     * Each process run by make, starts off in the 'CWD'
		     * recorded in the .meta file, if it chdirs ('C')
		     * elsewhere we need to track that - but only for
		     * that process.  If it forks ('F'), we initialize
		     * the child to have the same cwd as its parent.
		     *
		     * We also need to track the 'latestdir' of
		     * interest.  This is usually the same as cwd, but
		     * not if a process is reading directories.
		     *
		     * Each time we spot a different process ('pid')
		     * we save the current value of 'latestdir' in a
		     * variable qualified by 'lastpid', and
		     * re-initialize 'latestdir' to any pre-saved
		     * value for the current 'pid' and 'CWD' if none.
		     */
		    CHECK_VALID_META(p);
		    pid = atoi(p);
		    if (pid > 0 && pid != lastpid) {
			char *ldir;
			char *tp;
		    
			if (lastpid > 0) {
			    /* We need to remember this. */
			    Var_Set(ldir_vname, latestdir, VAR_GLOBAL, 0);
			}
			snprintf(ldir_vname, sizeof(ldir_vname), LDIR_VNAME_FMT, pid);
			lastpid = pid;
			ldir = Var_Value(ldir_vname, VAR_GLOBAL, &tp);
			if (ldir) {
			    strlcpy(latestdir, ldir, sizeof(latestdir));
			    if (tp)
				free(tp);
			} else 
			    strlcpy(latestdir, cwd, sizeof(latestdir));
		    }
		    /* Skip past the pid. */
		    if (strsep(&p, " ") == NULL)
			continue;
#ifdef DEBUG_META_MODE
		    if (DEBUG(META))
			fprintf(debug_file, "%s: %d: cwd=%s ldir=%s\n", fname, lineno, cwd, latestdir);
#endif
		    break;
		}

		CHECK_VALID_META(p);

		/* Process according to record type. */
		switch (buf[0]) {
		case 'X':		/* eXit */
		    Var_Delete(ldir_vname, VAR_GLOBAL);
		    lastpid = 0;	/* no need to save ldir_vname */
		    break;

		case 'F':		/* [v]Fork */
		    {
			char cldir[64];
			int child;

			child = atoi(p);
			if (child > 0) {
			    snprintf(cldir, sizeof(cldir), LDIR_VNAME_FMT, child);
			    Var_Set(cldir, latestdir, VAR_GLOBAL, 0);
			}
		    }
		    break;

		case 'C':		/* Chdir */
		    /* Update the latest directory. */
		    strlcpy(latestdir, p, sizeof(latestdir));
		    break;

		case 'M':		/* renaMe */
		    if (Lst_IsEmpty(missingFiles))
			break;
		    /* 'L' and 'M' put single quotes around the args */
		    if (*p == '\'') {
			char *ep;

			p++;
			if ((ep = strchr(p, '\'')))
			    *ep = '\0';
		    }
		    /* FALLTHROUGH */
		case 'D':		/* unlink */
		    if (*p == '/' && !Lst_IsEmpty(missingFiles)) {
			/* remove p from the missingFiles list if present */
			if ((ln = Lst_Find(missingFiles, p, string_match)) != NULL) {
			    char *tp = Lst_Datum(ln);
			    Lst_Remove(missingFiles, ln);
			    free(tp);
			}
		    }
		    break;
		case 'L':		/* Link */
		    /* we want the target */
		    if (strsep(&p, " ") == NULL)
			continue;
		    CHECK_VALID_META(p);
		    /* 'L' and 'M' put single quotes around the args */
		    if (*p == '\'') {
			char *ep;

			p++;
			if ((ep = strchr(p, '\'')))
			    *ep = '\0';
		    }
		    /* FALLTHROUGH */
		case 'W':		/* Write */
		    /*
		     * If a file we generated within our bailiwick
		     * but outside of .OBJDIR is missing,
		     * we need to do it again. 
		     */
		    /* ignore non-absolute paths */
		    if (*p != '/')
			break;

		    if (Lst_IsEmpty(metaBailiwick))
			break;

		    /* ignore cwd - normal dependencies handle those */
		    if (strncmp(p, cwd, cwdlen) == 0)
			break;

		    if (!Lst_ForEach(metaBailiwick, prefix_match, p))
			break;

		    /* tmpdir might be within */
		    if (tmplen > 0 && strncmp(p, tmpdir, tmplen) == 0)
			break;

		    /* ignore anything containing the string "tmp" */
		    if ((strstr("tmp", p)))
			break;

		    if (stat(p, &fs) < 0) {
			Lst_AtEnd(missingFiles, bmake_strdup(p));
		    }
		    break;
		case 'R':		/* Read */
		case 'E':		/* Exec */
		    /*
		     * Check for runtime files that can't
		     * be part of the dependencies because
		     * they are _expected_ to change.
		     */
		    if (strncmp(p, "/tmp/", 5) == 0 ||
			(tmplen > 0 && strncmp(p, tmpdir, tmplen) == 0))
			break;

		    if (strncmp(p, "/var/", 5) == 0)
			break;

		    /* Ignore device files. */
		    if (strncmp(p, "/dev/", 5) == 0)
			break;

		    /* Ignore /etc/ files. */
		    if (strncmp(p, "/etc/", 5) == 0)
			break;

		    if ((cp = strrchr(p, '/'))) {
			cp++;
			/*
			 * We don't normally expect to see this,
			 * but we do expect it to change.
			 */
			if (strcmp(cp, makeDependfile) == 0)
			    break;
		    }

		    /*
		     * The rest of the record is the file name.
		     * Check if it's not an absolute path.
		     */
		    {
			char *sdirs[4];
			char **sdp;
			int sdx = 0;
			int found = 0;

			if (*p == '/') {
			    sdirs[sdx++] = p; /* done */
			} else {
			    if (strcmp(".", p) == 0)
				continue;  /* no point */

			    /* Check vs latestdir */
			    snprintf(fname1, sizeof(fname1), "%s/%s", latestdir, p);
			    sdirs[sdx++] = fname1;

			    if (strcmp(latestdir, cwd) != 0) {
				/* Check vs cwd */
				snprintf(fname2, sizeof(fname2), "%s/%s", cwd, p);
				sdirs[sdx++] = fname2;
			    }
			}
			sdirs[sdx++] = NULL;

			for (sdp = sdirs; *sdp && !found; sdp++) {
#ifdef DEBUG_META_MODE
			    if (DEBUG(META))
				fprintf(debug_file, "%s: %d: looking for: %s\n", fname, lineno, *sdp);
#endif
			    if (stat(*sdp, &fs) == 0) {
				found = 1;
				p = *sdp;
			    }
			}
			if (found) {
#ifdef DEBUG_META_MODE
			    if (DEBUG(META))
				fprintf(debug_file, "%s: %d: found: %s\n", fname, lineno, p);
#endif
			    if (!S_ISDIR(fs.st_mode) &&
				fs.st_mtime > gn->mtime) {
				if (DEBUG(META))
				    fprintf(debug_file, "%s: %d: file '%s' is newer than the target...\n", fname, lineno, p);
				oodate = TRUE;
			    } else if (S_ISDIR(fs.st_mode)) {
				/* Update the latest directory. */
				realpath(p, latestdir);
			    }
			} else if (errno == ENOENT && *p == '/' &&
				   strncmp(p, cwd, cwdlen) != 0) {
			    /*
			     * A referenced file outside of CWD is missing.
			     * We cannot catch every eventuality here...
			     */
			    if (DEBUG(META))
				fprintf(debug_file, "%s: %d: file '%s' may have moved?...\n", fname, lineno, p);
			    oodate = TRUE;
			}
		    }
		    break;
		default:
		    break;
		}
	    } else if (strcmp(buf, "CMD") == 0) {
		/*
		 * Compare the current command with the one in the
		 * meta data file.
		 */
		if (ln == NULL) {
		    if (DEBUG(META))
			fprintf(debug_file, "%s: %d: there were more build commands in the meta data file than there are now...\n", fname, lineno);
		    oodate = TRUE;
		} else {
		    char *cmd = (char *)Lst_Datum(ln);

		    if (!ignoreOODATE) {
			if (strstr(cmd, "$?"))
			    ignoreOODATE = TRUE;
			else if ((cp = strstr(cmd, ".OODATE"))) {
			    /* check for $[{(].OODATE[)}] */
			    if (cp > cmd + 2 && cp[-2] == '$')
				ignoreOODATE = TRUE;
			}
			if (ignoreOODATE && DEBUG(META))
			    fprintf(debug_file, "%s: %d: cannot compare commands using .OODATE\n", fname, lineno);
		    }
		    cmd = Var_Subst(NULL, cmd, gn, TRUE);

		    if ((cp = strchr(cmd, '\n'))) {
			int n;

			/*
			 * This command contains newlines, we need to
			 * fetch more from the .meta file before we
			 * attempt a comparison.
			 */
			/* first put the newline back at buf[x - 1] */
			buf[x - 1] = '\n';
			do {
			    /* now fetch the next line */
			    if ((n = fgetLine(&buf, &bufsz, x, fp)) <= 0)
				break;
			    x = n;
			    lineno++;
			    if (buf[x - 1] != '\n') {
				warnx("%s: %d: line truncated at %u", fname, lineno, x);
				break;
			    }
			    cp = strchr(++cp, '\n');
			} while (cp);
			if (buf[x - 1] == '\n')
			    buf[x - 1] = '\0';
		    }
		    if (!ignoreOODATE &&
			!(gn->type & OP_NOMETA_CMP) &&
			strcmp(p, cmd) != 0) {
			if (DEBUG(META))
			    fprintf(debug_file, "%s: %d: a build command has changed\n%s\nvs\n%s\n", fname, lineno, p, cmd);
			if (!metaIgnoreCMDs)
			    oodate = TRUE;
		    }
		    free(cmd);
		    ln = Lst_Succ(ln);
		}
	    } else if (strcmp(buf, "CWD") == 0) {
		/*
		 * Check if there are extra commands now
		 * that weren't in the meta data file.
		 */
		if (!oodate && ln != NULL) {
		    if (DEBUG(META))
			fprintf(debug_file, "%s: %d: there are extra build commands now that weren't in the meta data file\n", fname, lineno);
		    oodate = TRUE;
		}
		if (strcmp(p, cwd) != 0) {
		    if (DEBUG(META))
			fprintf(debug_file, "%s: %d: the current working directory has changed from '%s' to '%s'\n", fname, lineno, p, curdir);
		    oodate = TRUE;
		}
	    }
	}

	fclose(fp);
	if (!Lst_IsEmpty(missingFiles)) {
	    if (DEBUG(META))
		fprintf(debug_file, "%s: missing files: %s...\n",
			fname, (char *)Lst_Datum(Lst_First(missingFiles)));
	    oodate = TRUE;
	    Lst_Destroy(missingFiles, (FreeProc *)free);
	}
    } else {
	if ((gn->type & OP_META)) {
	    if (DEBUG(META))
		fprintf(debug_file, "%s: required but missing\n", fname);
	    oodate = TRUE;
	}
    }
    if (oodate && ignoreOODATE) {
	/*
	 * Target uses .OODATE, so we need to re-compute it.
	 * We need to clean up what Make_DoAllVar() did.
	 */
	Var_Delete(ALLSRC, gn);
	Var_Delete(OODATE, gn);
	gn->flags &= ~DONE_ALLSRC;
    }
    return oodate;
}
예제 #2
0
파일: meta.c 프로젝트: ryo/netbsd-src
Boolean
meta_oodate(GNode *gn, Boolean oodate)
{
    static char *tmpdir = NULL;
    static char cwd[MAXPATHLEN];
    char lcwd_vname[64];
    char ldir_vname[64];
    char lcwd[MAXPATHLEN];
    char latestdir[MAXPATHLEN];
    char fname[MAXPATHLEN];
    char fname1[MAXPATHLEN];
    char fname2[MAXPATHLEN];
    char fname3[MAXPATHLEN];
    const char *dname;
    const char *tname;
    char *p;
    char *cp;
    char *link_src;
    char *move_target;
    static size_t cwdlen = 0;
    static size_t tmplen = 0;
    FILE *fp;
    Boolean needOODATE = FALSE;
    Lst missingFiles;
    char *pa[4];			/* >= possible uses */
    int i;
    int have_filemon = FALSE;

    if (oodate)
        return oodate;		/* we're done */

    i = 0;

    dname = Var_Value(".OBJDIR", gn, &pa[i++]);
    tname = Var_Value(TARGET, gn, &pa[i++]);

    /* if this succeeds fname3 is realpath of dname */
    if (!meta_needed(gn, dname, tname, fname3, FALSE))
        goto oodate_out;
    dname = fname3;

    missingFiles = Lst_Init(FALSE);

    /*
     * We need to check if the target is out-of-date. This includes
     * checking if the expanded command has changed. This in turn
     * requires that all variables are set in the same way that they
     * would be if the target needs to be re-built.
     */
    Make_DoAllVar(gn);

    meta_name(gn, fname, sizeof(fname), dname, tname, dname);

#ifdef DEBUG_META_MODE
    if (DEBUG(META))
        fprintf(debug_file, "meta_oodate: %s\n", fname);
#endif

    if ((fp = fopen(fname, "r")) != NULL) {
        static char *buf = NULL;
        static size_t bufsz;
        int lineno = 0;
        int lastpid = 0;
        int pid;
        int x;
        LstNode ln;
        struct stat fs;

        if (!buf) {
            bufsz = 8 * BUFSIZ;
            buf = bmake_malloc(bufsz);
        }

        if (!cwdlen) {
            if (getcwd(cwd, sizeof(cwd)) == NULL)
                err(1, "Could not get current working directory");
            cwdlen = strlen(cwd);
        }
        strlcpy(lcwd, cwd, sizeof(lcwd));
        strlcpy(latestdir, cwd, sizeof(latestdir));

        if (!tmpdir) {
            tmpdir = getTmpdir();
            tmplen = strlen(tmpdir);
        }

        /* we want to track all the .meta we read */
        Var_Append(".MAKE.META.FILES", fname, VAR_GLOBAL);

        ln = Lst_First(gn->commands);
        while (!oodate && (x = fgetLine(&buf, &bufsz, 0, fp)) > 0) {
            lineno++;
            if (buf[x - 1] == '\n')
                buf[x - 1] = '\0';
            else {
                warnx("%s: %d: line truncated at %u", fname, lineno, x);
                oodate = TRUE;
                break;
            }
            link_src = NULL;
            move_target = NULL;
            /* Find the start of the build monitor section. */
            if (!have_filemon) {
                if (strncmp(buf, "-- filemon", 10) == 0) {
                    have_filemon = TRUE;
                    continue;
                }
                if (strncmp(buf, "# buildmon", 10) == 0) {
                    have_filemon = TRUE;
                    continue;
                }
            }

            /* Delimit the record type. */
            p = buf;
#ifdef DEBUG_META_MODE
            if (DEBUG(META))
                fprintf(debug_file, "%s: %d: %s\n", fname, lineno, buf);
#endif
            strsep(&p, " ");
            if (have_filemon) {
                /*
                 * We are in the 'filemon' output section.
                 * Each record from filemon follows the general form:
                 *
                 * <key> <pid> <data>
                 *
                 * Where:
                 * <key> is a single letter, denoting the syscall.
                 * <pid> is the process that made the syscall.
                 * <data> is the arguments (of interest).
                 */
                switch(buf[0]) {
                case '#':		/* comment */
                case 'V':		/* version */
                    break;
                default:
                    /*
                     * We need to track pathnames per-process.
                     *
                     * Each process run by make, starts off in the 'CWD'
                     * recorded in the .meta file, if it chdirs ('C')
                     * elsewhere we need to track that - but only for
                     * that process.  If it forks ('F'), we initialize
                     * the child to have the same cwd as its parent.
                     *
                     * We also need to track the 'latestdir' of
                     * interest.  This is usually the same as cwd, but
                     * not if a process is reading directories.
                     *
                     * Each time we spot a different process ('pid')
                     * we save the current value of 'latestdir' in a
                     * variable qualified by 'lastpid', and
                     * re-initialize 'latestdir' to any pre-saved
                     * value for the current 'pid' and 'CWD' if none.
                     */
                    CHECK_VALID_META(p);
                    pid = atoi(p);
                    if (pid > 0 && pid != lastpid) {
                        char *ldir;
                        char *tp;

                        if (lastpid > 0) {
                            /* We need to remember these. */
                            Var_Set(lcwd_vname, lcwd, VAR_GLOBAL, 0);
                            Var_Set(ldir_vname, latestdir, VAR_GLOBAL, 0);
                        }
                        snprintf(lcwd_vname, sizeof(lcwd_vname), LCWD_VNAME_FMT, pid);
                        snprintf(ldir_vname, sizeof(ldir_vname), LDIR_VNAME_FMT, pid);
                        lastpid = pid;
                        ldir = Var_Value(ldir_vname, VAR_GLOBAL, &tp);
                        if (ldir) {
                            strlcpy(latestdir, ldir, sizeof(latestdir));
                            free(tp);
                        }
                        ldir = Var_Value(lcwd_vname, VAR_GLOBAL, &tp);
                        if (ldir) {
                            strlcpy(lcwd, ldir, sizeof(lcwd));
                            free(tp);
                        }
                    }
                    /* Skip past the pid. */
                    if (strsep(&p, " ") == NULL)
                        continue;
#ifdef DEBUG_META_MODE
                    if (DEBUG(META))
                        fprintf(debug_file, "%s: %d: %d: %c: cwd=%s lcwd=%s ldir=%s\n",
                                fname, lineno,
                                pid, buf[0], cwd, lcwd, latestdir);
#endif
                    break;
                }

                CHECK_VALID_META(p);

                /* Process according to record type. */
                switch (buf[0]) {
                case 'X':		/* eXit */
                    Var_Delete(lcwd_vname, VAR_GLOBAL);
                    Var_Delete(ldir_vname, VAR_GLOBAL);
                    lastpid = 0;	/* no need to save ldir_vname */
                    break;

                case 'F':		/* [v]Fork */
                {
                    char cldir[64];
                    int child;

                    child = atoi(p);
                    if (child > 0) {
                        snprintf(cldir, sizeof(cldir), LCWD_VNAME_FMT, child);
                        Var_Set(cldir, lcwd, VAR_GLOBAL, 0);
                        snprintf(cldir, sizeof(cldir), LDIR_VNAME_FMT, child);
                        Var_Set(cldir, latestdir, VAR_GLOBAL, 0);
#ifdef DEBUG_META_MODE
                        if (DEBUG(META))
                            fprintf(debug_file, "%s: %d: %d: cwd=%s lcwd=%s ldir=%s\n",
                                    fname, lineno,
                                    child, cwd, lcwd, latestdir);
#endif
                    }
                }
                break;

                case 'C':		/* Chdir */
                    /* Update lcwd and latest directory. */
                    strlcpy(latestdir, p, sizeof(latestdir));
                    strlcpy(lcwd, p, sizeof(lcwd));
                    Var_Set(lcwd_vname, lcwd, VAR_GLOBAL, 0);
                    Var_Set(ldir_vname, lcwd, VAR_GLOBAL, 0);
#ifdef DEBUG_META_MODE
                    if (DEBUG(META))
                        fprintf(debug_file, "%s: %d: cwd=%s ldir=%s\n", fname, lineno, cwd, lcwd);
#endif
                    break;

                case 'M':		/* renaMe */
                    /*
                     * For 'M'oves we want to check
                     * the src as for 'R'ead
                     * and the target as for 'W'rite.
                     */
                    cp = p;		/* save this for a second */
                    /* now get target */
                    if (strsep(&p, " ") == NULL)
                        continue;
                    CHECK_VALID_META(p);
                    move_target = p;
                    p = cp;
                    /* 'L' and 'M' put single quotes around the args */
                    DEQUOTE(p);
                    DEQUOTE(move_target);
                /* FALLTHROUGH */
                case 'D':		/* unlink */
                    if (*p == '/' && !Lst_IsEmpty(missingFiles)) {
                        /* remove any missingFiles entries that match p */
                        if ((ln = Lst_Find(missingFiles, p,
                                           path_match)) != NULL) {
                            LstNode nln;
                            char *tp;

                            do {
                                nln = Lst_FindFrom(missingFiles, Lst_Succ(ln),
                                                   p, path_match);
                                tp = Lst_Datum(ln);
                                Lst_Remove(missingFiles, ln);
                                free(tp);
                            } while ((ln = nln) != NULL);
                        }
                    }
                    if (buf[0] == 'M') {
                        /* the target of the mv is a file 'W'ritten */
#ifdef DEBUG_META_MODE
                        if (DEBUG(META))
                            fprintf(debug_file, "meta_oodate: M %s -> %s\n",
                                    p, move_target);
#endif
                        p = move_target;
                        goto check_write;
                    }
                    break;
                case 'L':		/* Link */
                    /*
                     * For 'L'inks check
                     * the src as for 'R'ead
                     * and the target as for 'W'rite.
                     */
                    link_src = p;
                    /* now get target */
                    if (strsep(&p, " ") == NULL)
                        continue;
                    CHECK_VALID_META(p);
                    /* 'L' and 'M' put single quotes around the args */
                    DEQUOTE(p);
                    DEQUOTE(link_src);
#ifdef DEBUG_META_MODE
                    if (DEBUG(META))
                        fprintf(debug_file, "meta_oodate: L %s -> %s\n",
                                link_src, p);
#endif
                /* FALLTHROUGH */
                case 'W':		/* Write */
check_write:
                    /*
                     * If a file we generated within our bailiwick
                     * but outside of .OBJDIR is missing,
                     * we need to do it again.
                     */
                    /* ignore non-absolute paths */
                    if (*p != '/')
                        break;

                    if (Lst_IsEmpty(metaBailiwick))
                        break;

                    /* ignore cwd - normal dependencies handle those */
                    if (strncmp(p, cwd, cwdlen) == 0)
                        break;

                    if (!Lst_ForEach(metaBailiwick, prefix_match, p))
                        break;

                    /* tmpdir might be within */
                    if (tmplen > 0 && strncmp(p, tmpdir, tmplen) == 0)
                        break;

                    /* ignore anything containing the string "tmp" */
                    if ((strstr("tmp", p)))
                        break;

                    if ((link_src != NULL && cached_lstat(p, &fs) < 0) ||
                            (link_src == NULL && cached_stat(p, &fs) < 0)) {
                        if (!meta_ignore(gn, p)) {
                            if (Lst_Find(missingFiles, p, string_match) == NULL)
                                Lst_AtEnd(missingFiles, bmake_strdup(p));
                        }
                    }
                    break;
check_link_src:
                    p = link_src;
                    link_src = NULL;
#ifdef DEBUG_META_MODE
                    if (DEBUG(META))
                        fprintf(debug_file, "meta_oodate: L src %s\n", p);
#endif
                /* FALLTHROUGH */
                case 'R':		/* Read */
                case 'E':		/* Exec */
                    /*
                     * Check for runtime files that can't
                     * be part of the dependencies because
                     * they are _expected_ to change.
                     */
                    if (meta_ignore(gn, p))
                        break;

                    /*
                     * The rest of the record is the file name.
                     * Check if it's not an absolute path.
                     */
                    {
                        char *sdirs[4];
                        char **sdp;
                        int sdx = 0;
                        int found = 0;

                        if (*p == '/') {
                            sdirs[sdx++] = p; /* done */
                        } else {
                            if (strcmp(".", p) == 0)
                                continue;  /* no point */

                            /* Check vs latestdir */
                            snprintf(fname1, sizeof(fname1), "%s/%s", latestdir, p);
                            sdirs[sdx++] = fname1;

                            if (strcmp(latestdir, lcwd) != 0) {
                                /* Check vs lcwd */
                                snprintf(fname2, sizeof(fname2), "%s/%s", lcwd, p);
                                sdirs[sdx++] = fname2;
                            }
                            if (strcmp(lcwd, cwd) != 0) {
                                /* Check vs cwd */
                                snprintf(fname3, sizeof(fname3), "%s/%s", cwd, p);
                                sdirs[sdx++] = fname3;
                            }
                        }
                        sdirs[sdx++] = NULL;

                        for (sdp = sdirs; *sdp && !found; sdp++) {
#ifdef DEBUG_META_MODE
                            if (DEBUG(META))
                                fprintf(debug_file, "%s: %d: looking for: %s\n", fname, lineno, *sdp);
#endif
                            if (cached_stat(*sdp, &fs) == 0) {
                                found = 1;
                                p = *sdp;
                            }
                        }
                        if (found) {
#ifdef DEBUG_META_MODE
                            if (DEBUG(META))
                                fprintf(debug_file, "%s: %d: found: %s\n", fname, lineno, p);
#endif
                            if (!S_ISDIR(fs.st_mode) &&
                                    fs.st_mtime > gn->mtime) {
                                if (DEBUG(META))
                                    fprintf(debug_file, "%s: %d: file '%s' is newer than the target...\n", fname, lineno, p);
                                oodate = TRUE;
                            } else if (S_ISDIR(fs.st_mode)) {
                                /* Update the latest directory. */
                                cached_realpath(p, latestdir);
                            }
                        } else if (errno == ENOENT && *p == '/' &&
                                   strncmp(p, cwd, cwdlen) != 0) {
                            /*
                             * A referenced file outside of CWD is missing.
                             * We cannot catch every eventuality here...
                             */
                            if (Lst_Find(missingFiles, p, string_match) == NULL)
                                Lst_AtEnd(missingFiles, bmake_strdup(p));
                        }
                    }
                    if (buf[0] == 'E') {
                        /* previous latestdir is no longer relevant */
                        strlcpy(latestdir, lcwd, sizeof(latestdir));
                    }
                    break;
                default:
                    break;
                }
                if (!oodate && buf[0] == 'L' && link_src != NULL)
                    goto check_link_src;
            } else if (strcmp(buf, "CMD") == 0) {
                /*
                 * Compare the current command with the one in the
                 * meta data file.
                 */
                if (ln == NULL) {
                    if (DEBUG(META))
                        fprintf(debug_file, "%s: %d: there were more build commands in the meta data file than there are now...\n", fname, lineno);
                    oodate = TRUE;
                } else {
                    char *cmd = (char *)Lst_Datum(ln);
                    Boolean hasOODATE = FALSE;

                    if (strstr(cmd, "$?"))
                        hasOODATE = TRUE;
                    else if ((cp = strstr(cmd, ".OODATE"))) {
                        /* check for $[{(].OODATE[:)}] */
                        if (cp > cmd + 2 && cp[-2] == '$')
                            hasOODATE = TRUE;
                    }
                    if (hasOODATE) {
                        needOODATE = TRUE;
                        if (DEBUG(META))
                            fprintf(debug_file, "%s: %d: cannot compare command using .OODATE\n", fname, lineno);
                    }
                    cmd = Var_Subst(NULL, cmd, gn, VARF_WANTRES|VARF_UNDEFERR);

                    if ((cp = strchr(cmd, '\n'))) {
                        int n;

                        /*
                         * This command contains newlines, we need to
                         * fetch more from the .meta file before we
                         * attempt a comparison.
                         */
                        /* first put the newline back at buf[x - 1] */
                        buf[x - 1] = '\n';
                        do {
                            /* now fetch the next line */
                            if ((n = fgetLine(&buf, &bufsz, x, fp)) <= 0)
                                break;
                            x = n;
                            lineno++;
                            if (buf[x - 1] != '\n') {
                                warnx("%s: %d: line truncated at %u", fname, lineno, x);
                                break;
                            }
                            cp = strchr(++cp, '\n');
                        } while (cp);
                        if (buf[x - 1] == '\n')
                            buf[x - 1] = '\0';
                    }
                    if (!hasOODATE &&
                            !(gn->type & OP_NOMETA_CMP) &&
                            strcmp(p, cmd) != 0) {
                        if (DEBUG(META))
                            fprintf(debug_file, "%s: %d: a build command has changed\n%s\nvs\n%s\n", fname, lineno, p, cmd);
                        if (!metaIgnoreCMDs)
                            oodate = TRUE;
                    }
                    free(cmd);
                    ln = Lst_Succ(ln);
                }
            } else if (strcmp(buf, "CWD") == 0) {
                /*
                 * Check if there are extra commands now
                 * that weren't in the meta data file.
                 */
                if (!oodate && ln != NULL) {
                    if (DEBUG(META))
                        fprintf(debug_file, "%s: %d: there are extra build commands now that weren't in the meta data file\n", fname, lineno);
                    oodate = TRUE;
                }
                if (strcmp(p, cwd) != 0) {
                    if (DEBUG(META))
                        fprintf(debug_file, "%s: %d: the current working directory has changed from '%s' to '%s'\n", fname, lineno, p, curdir);
                    oodate = TRUE;
                }
            }
        }

        fclose(fp);
        if (!Lst_IsEmpty(missingFiles)) {
            if (DEBUG(META))
                fprintf(debug_file, "%s: missing files: %s...\n",
                        fname, (char *)Lst_Datum(Lst_First(missingFiles)));
            oodate = TRUE;
        }
        if (!oodate && !have_filemon && filemonMissing) {
            if (DEBUG(META))
                fprintf(debug_file, "%s: missing filemon data\n", fname);
            oodate = TRUE;
        }
    } else {
        if (writeMeta && metaMissing) {
            cp = NULL;

            /* if target is in .CURDIR we do not need a meta file */
            if (gn->path && (cp = strrchr(gn->path, '/')) && cp > gn->path) {
                if (strncmp(curdir, gn->path, (cp - gn->path)) != 0) {
                    cp = NULL;		/* not in .CURDIR */
                }
            }
            if (!cp) {
                if (DEBUG(META))
                    fprintf(debug_file, "%s: required but missing\n", fname);
                oodate = TRUE;
                needOODATE = TRUE;	/* assume the worst */
            }
        }
    }

    Lst_Destroy(missingFiles, (FreeProc *)free);

    if (oodate && needOODATE) {
        /*
         * Target uses .OODATE which is empty; or we wouldn't be here.
         * We have decided it is oodate, so .OODATE needs to be set.
         * All we can sanely do is set it to .ALLSRC.
         */
        Var_Delete(OODATE, gn);
        Var_Set(OODATE, Var_Value(ALLSRC, gn, &cp), gn, 0);
        free(cp);
    }

oodate_out:
    for (i--; i >= 0; i--) {
        free(pa[i]);
    }
    return oodate;
}