128 register int attempts = 0;
129 Name_set::iterator np, e;
130 register Property lines;
131 register int m;
132 Dependency dependency;
133 register Boolean name_printed;
134 Boolean built_this_run = false;
135 char *target_name;
136 int line_length;
137 register Cmd_line cp;
138
139
140 if (!rewrite_statefile ||
141 !command_changed ||
142 !keep_state ||
143 do_not_exec_rule ||
144 (report_dependencies_level > 0)) {
145 return;
146 }
147 /* Lock the file for writing. */
148 make_state_lockfile = getmem(strlen(make_state->string_mb) + strlen(NOCATGETS(".lock")) + 1);
149 (void) sprintf(make_state_lockfile,
150 NOCATGETS("%s.lock"),
151 make_state->string_mb);
152 if (lock_err = file_lock(make_state->string_mb,
153 make_state_lockfile,
154 (int *) &make_state_locked, 0)) {
155 retmem_mb(make_state_lockfile);
156 make_state_lockfile = NULL;
157
158 /*
159 * We need to make sure that we are not being
160 * called by the exit handler so we don't call
161 * it again.
162 */
163
164 if (exiting) {
165 (void) sprintf(buffer, NOCATGETS("%s/.make.state.%d.XXXXXX"), tmpdir, getpid());
166 report_pwd = true;
167 warning(catgets(catd, 1, 60, "Writing to %s"), buffer);
168 int fdes = mkstemp(buffer);
169 if ((fdes < 0) || (fd = fdopen(fdes, "w")) == NULL) {
170 fprintf(stderr,
171 catgets(catd, 1, 61, "Could not open statefile `%s': %s"),
172 buffer,
173 errmsg(errno));
174 return;
175 }
176 } else {
177 report_pwd = true;
178 fatal(catgets(catd, 1, 62, "Can't lock .make.state"));
179 }
180 }
181
182 (void) sprintf(make_state_tempfile,
183 NOCATGETS("%s.tmp"),
184 make_state->string_mb);
185 /* Delete old temporary statefile (in case it exists) */
186 (void) unlink(make_state_tempfile);
187 if ((fd = fopen(make_state_tempfile, "w")) == NULL) {
188 lock_err = errno; /* Save it! unlink() can change errno */
189 (void) unlink(make_state_lockfile);
190 retmem_mb(make_state_lockfile);
191 make_state_lockfile = NULL;
192 make_state_locked = false;
193 fatal(catgets(catd, 1, 59, "Could not open temporary statefile `%s': %s"),
194 make_state_tempfile,
195 errmsg(lock_err));
196 }
197 /*
198 * Set a trap for failed writes. If a write fails, the routine
199 * will try saving the .make.state file under another name in /tmp.
200 */
201 if (setjmp(long_jump)) {
202 (void) fclose(fd);
203 if (attempts++ > 5) {
204 if ((make_state_lockfile != NULL) &&
205 make_state_locked) {
206 (void) unlink(make_state_lockfile);
207 retmem_mb(make_state_lockfile);
208 make_state_lockfile = NULL;
209 make_state_locked = false;
210 }
211 fatal(catgets(catd, 1, 63, "Giving up on writing statefile"));
212 }
213 sleep(10);
214 (void) sprintf(buffer, NOCATGETS("%s/.make.state.%d.XXXXXX"), tmpdir, getpid());
215 int fdes = mkstemp(buffer);
216 if ((fdes < 0) || (fd = fdopen(fdes, "w")) == NULL) {
217 fatal(catgets(catd, 1, 64, "Could not open statefile `%s': %s"),
218 buffer,
219 errmsg(errno));
220 }
221 warning(catgets(catd, 1, 65, "Initial write of statefile failed. Trying again on %s"),
222 buffer);
223 }
224
225 /* Write the version stamp. */
226 XFWRITE(make_version->string_mb,
227 strlen(make_version->string_mb),
228 fd);
229 XPUTC(colon_char, fd);
230 XPUTC(tab_char, fd);
231 XFWRITE(current_make_version->string_mb,
232 strlen(current_make_version->string_mb),
233 fd);
234 XPUTC(newline_char, fd);
235
236 /*
237 * Go through all the targets, dump their dependencies and
238 * command used.
239 */
240 for (np = hashtab.begin(), e = hashtab.end(); np != e; np++) {
241 /*
348 }
349 }
350 XPUTC(newline_char, fd);
351 }
352 }
353 (void)free(target_name);
354 }
355 }
356 if (fclose(fd) == EOF) {
357 longjmp(long_jump, LONGJUMP_VALUE);
358 }
359 if (attempts == 0) {
360 if (unlink(make_state->string_mb) != 0 && errno != ENOENT) {
361 lock_err = errno; /* Save it! unlink() can change errno */
362 /* Delete temporary statefile */
363 (void) unlink(make_state_tempfile);
364 (void) unlink(make_state_lockfile);
365 retmem_mb(make_state_lockfile);
366 make_state_lockfile = NULL;
367 make_state_locked = false;
368 fatal(catgets(catd, 1, 356, "Could not delete old statefile `%s': %s"),
369 make_state->string_mb,
370 errmsg(lock_err));
371 }
372 if (rename(make_state_tempfile, make_state->string_mb) != 0) {
373 lock_err = errno; /* Save it! unlink() can change errno */
374 /* Delete temporary statefile */
375 (void) unlink(make_state_tempfile);
376 (void) unlink(make_state_lockfile);
377 retmem_mb(make_state_lockfile);
378 make_state_lockfile = NULL;
379 make_state_locked = false;
380 fatal(catgets(catd, 1, 357, "Could not rename `%s' to `%s': %s"),
381 make_state_tempfile,
382 make_state->string_mb,
383 errmsg(lock_err));
384 }
385 }
386 if ((make_state_lockfile != NULL) && make_state_locked) {
387 (void) unlink(make_state_lockfile);
388 retmem_mb(make_state_lockfile);
389 make_state_lockfile = NULL;
390 make_state_locked = false;
391 }
392 }
393
394 /*
395 * print_auto_depes(dependency, fd, built_this_run,
396 * line_length, target_name, long_jump)
397 *
398 * Will print a dependency list for automatic entries.
399 *
400 * Parameters:
|
128 register int attempts = 0;
129 Name_set::iterator np, e;
130 register Property lines;
131 register int m;
132 Dependency dependency;
133 register Boolean name_printed;
134 Boolean built_this_run = false;
135 char *target_name;
136 int line_length;
137 register Cmd_line cp;
138
139
140 if (!rewrite_statefile ||
141 !command_changed ||
142 !keep_state ||
143 do_not_exec_rule ||
144 (report_dependencies_level > 0)) {
145 return;
146 }
147 /* Lock the file for writing. */
148 make_state_lockfile = getmem(strlen(make_state->string_mb) + strlen(".lock") + 1);
149 (void) sprintf(make_state_lockfile,
150 "%s.lock",
151 make_state->string_mb);
152 if (lock_err = file_lock(make_state->string_mb,
153 make_state_lockfile,
154 (int *) &make_state_locked, 0)) {
155 retmem_mb(make_state_lockfile);
156 make_state_lockfile = NULL;
157
158 /*
159 * We need to make sure that we are not being
160 * called by the exit handler so we don't call
161 * it again.
162 */
163
164 if (exiting) {
165 (void) sprintf(buffer, "%s/.make.state.%d.XXXXXX", tmpdir, getpid());
166 report_pwd = true;
167 warning(gettext("Writing to %s"), buffer);
168 int fdes = mkstemp(buffer);
169 if ((fdes < 0) || (fd = fdopen(fdes, "w")) == NULL) {
170 fprintf(stderr,
171 gettext("Could not open statefile `%s': %s"),
172 buffer,
173 errmsg(errno));
174 return;
175 }
176 } else {
177 report_pwd = true;
178 fatal(gettext("Can't lock .make.state"));
179 }
180 }
181
182 (void) sprintf(make_state_tempfile,
183 "%s.tmp",
184 make_state->string_mb);
185 /* Delete old temporary statefile (in case it exists) */
186 (void) unlink(make_state_tempfile);
187 if ((fd = fopen(make_state_tempfile, "w")) == NULL) {
188 lock_err = errno; /* Save it! unlink() can change errno */
189 (void) unlink(make_state_lockfile);
190 retmem_mb(make_state_lockfile);
191 make_state_lockfile = NULL;
192 make_state_locked = false;
193 fatal(gettext("Could not open temporary statefile `%s': %s"),
194 make_state_tempfile,
195 errmsg(lock_err));
196 }
197 /*
198 * Set a trap for failed writes. If a write fails, the routine
199 * will try saving the .make.state file under another name in /tmp.
200 */
201 if (setjmp(long_jump)) {
202 (void) fclose(fd);
203 if (attempts++ > 5) {
204 if ((make_state_lockfile != NULL) &&
205 make_state_locked) {
206 (void) unlink(make_state_lockfile);
207 retmem_mb(make_state_lockfile);
208 make_state_lockfile = NULL;
209 make_state_locked = false;
210 }
211 fatal(gettext("Giving up on writing statefile"));
212 }
213 sleep(10);
214 (void) sprintf(buffer, "%s/.make.state.%d.XXXXXX", tmpdir, getpid());
215 int fdes = mkstemp(buffer);
216 if ((fdes < 0) || (fd = fdopen(fdes, "w")) == NULL) {
217 fatal(gettext("Could not open statefile `%s': %s"),
218 buffer,
219 errmsg(errno));
220 }
221 warning(gettext("Initial write of statefile failed. Trying again on %s"),
222 buffer);
223 }
224
225 /* Write the version stamp. */
226 XFWRITE(make_version->string_mb,
227 strlen(make_version->string_mb),
228 fd);
229 XPUTC(colon_char, fd);
230 XPUTC(tab_char, fd);
231 XFWRITE(current_make_version->string_mb,
232 strlen(current_make_version->string_mb),
233 fd);
234 XPUTC(newline_char, fd);
235
236 /*
237 * Go through all the targets, dump their dependencies and
238 * command used.
239 */
240 for (np = hashtab.begin(), e = hashtab.end(); np != e; np++) {
241 /*
348 }
349 }
350 XPUTC(newline_char, fd);
351 }
352 }
353 (void)free(target_name);
354 }
355 }
356 if (fclose(fd) == EOF) {
357 longjmp(long_jump, LONGJUMP_VALUE);
358 }
359 if (attempts == 0) {
360 if (unlink(make_state->string_mb) != 0 && errno != ENOENT) {
361 lock_err = errno; /* Save it! unlink() can change errno */
362 /* Delete temporary statefile */
363 (void) unlink(make_state_tempfile);
364 (void) unlink(make_state_lockfile);
365 retmem_mb(make_state_lockfile);
366 make_state_lockfile = NULL;
367 make_state_locked = false;
368 fatal(gettext("Could not delete old statefile `%s': %s"),
369 make_state->string_mb,
370 errmsg(lock_err));
371 }
372 if (rename(make_state_tempfile, make_state->string_mb) != 0) {
373 lock_err = errno; /* Save it! unlink() can change errno */
374 /* Delete temporary statefile */
375 (void) unlink(make_state_tempfile);
376 (void) unlink(make_state_lockfile);
377 retmem_mb(make_state_lockfile);
378 make_state_lockfile = NULL;
379 make_state_locked = false;
380 fatal(gettext("Could not rename `%s' to `%s': %s"),
381 make_state_tempfile,
382 make_state->string_mb,
383 errmsg(lock_err));
384 }
385 }
386 if ((make_state_lockfile != NULL) && make_state_locked) {
387 (void) unlink(make_state_lockfile);
388 retmem_mb(make_state_lockfile);
389 make_state_lockfile = NULL;
390 make_state_locked = false;
391 }
392 }
393
394 /*
395 * print_auto_depes(dependency, fd, built_this_run,
396 * line_length, target_name, long_jump)
397 *
398 * Will print a dependency list for automatic entries.
399 *
400 * Parameters:
|