/*! Initializes all jobs for the specified target (may be \c NULL). Jobs that cannot be initialized, and those that never will be due to conditions, will be removed from the list. */ void LaunchDaemon::_InitJobs(Target* target) { for (JobMap::iterator iterator = fJobs.begin(); iterator != fJobs.end();) { Job* job = iterator->second; JobMap::iterator remove = iterator++; if (job->Target() != target) continue; status_t status = B_NO_INIT; if (job->IsEnabled()) { // Filter out jobs that have a constant and failing condition if (job->Condition() == NULL || !job->Condition()->IsConstant(*this) || job->Condition()->Test(*this)) { std::set<BString> dependencies; status = job->Init(*this, dependencies); if (status == B_OK && job->Event() != NULL) status = job->Event()->Register(*this); } } if (status != B_OK) { if (status != B_NO_INIT) { // TODO: log error debug_printf("Init \"%s\" failed: %s\n", job->Name(), strerror(status)); } // Remove jobs that won't be used later on fJobs.erase(remove); delete job; } } }
status_t Job::Init(const Finder& finder, std::set<BString>& dependencies) { // Only initialize the jobs once if (fInitStatus != B_NO_INIT) return fInitStatus; fInitStatus = B_OK; if (fTarget != NULL) fTarget->AddDependency(this); // Check dependencies for (int32 index = 0; index < Requirements().CountStrings(); index++) { const BString& requires = Requirements().StringAt(index); if (dependencies.find(requires) != dependencies.end()) { // Found a cyclic dependency // TODO: log error return fInitStatus = B_ERROR; } dependencies.insert(requires); Job* dependency = finder.FindJob(requires); if (dependency != NULL) { std::set<BString> subDependencies = dependencies; fInitStatus = dependency->Init(finder, subDependencies); if (fInitStatus != B_OK) { // TODO: log error return fInitStatus; } fInitStatus = _AddRequirement(dependency); } else { ::Target* target = finder.FindTarget(requires); if (target != NULL) fInitStatus = _AddRequirement(dependency); else { // Could not find dependency fInitStatus = B_NAME_NOT_FOUND; } } if (fInitStatus != B_OK) { // TODO: log error return fInitStatus; } } return fInitStatus; }