9 |
|
*/ |
10 |
|
|
11 |
|
#include "standard.h" |
12 |
– |
#include <fcntl.h> |
12 |
|
|
13 |
|
#ifndef F_SETLKW |
14 |
|
|
61 |
|
char *outfile = NULL; |
62 |
|
int outfd; |
63 |
|
long scanorig; |
64 |
< |
int syncfd = -1; /* lock file descriptor */ |
64 |
> |
FILE *syncfp = NULL; /* synchronization file pointer */ |
65 |
> |
int synclst = F_UNLCK; /* synchronization file lock status */ |
66 |
|
int nforked = 0; |
67 |
|
|
68 |
+ |
#define sflock(t) if ((t)!=synclst) dolock(fileno(syncfp),synclst=t) |
69 |
+ |
|
70 |
|
char *progname; |
71 |
|
int verbose = 0; |
72 |
+ |
int rvrlim = -1; |
73 |
|
|
74 |
|
extern long lseek(), ftell(); |
75 |
|
|
140 |
|
break; |
141 |
|
vmult = atoi(argv[++i]); |
142 |
|
continue; |
143 |
+ |
case 'R': /* recover */ |
144 |
+ |
if (argv[i][2]) |
145 |
+ |
break; |
146 |
+ |
rvrlim = 0; |
147 |
+ |
/* fall through */ |
148 |
|
case 'F': /* syncronization file */ |
149 |
|
if (argv[i][2]) |
150 |
|
break; |
151 |
< |
if ((syncfd = open(argv[++i], |
144 |
< |
O_RDWR|O_CREAT, 0666)) < 0) { |
151 |
> |
if ((syncfp = fopen(argv[++i],"r+")) == NULL) { |
152 |
|
fprintf(stderr, "%s: cannot open\n", |
153 |
|
argv[i]); |
154 |
|
exit(1); |
181 |
|
} |
182 |
|
|
183 |
|
|
184 |
+ |
dolock(fd, ltyp) /* lock or unlock a file */ |
185 |
+ |
int fd; |
186 |
+ |
int ltyp; |
187 |
+ |
{ |
188 |
+ |
static struct flock fls; /* static so initialized to zeroes */ |
189 |
+ |
|
190 |
+ |
fls.l_type = ltyp; |
191 |
+ |
if (fcntl(fd, F_SETLKW, &fls) < 0) { |
192 |
+ |
fprintf(stderr, "%s: cannot lock/unlock file\n", progname); |
193 |
+ |
exit(1); |
194 |
+ |
} |
195 |
+ |
} |
196 |
+ |
|
197 |
+ |
|
198 |
|
init(ac, av) /* set up output file and start rpict */ |
199 |
|
int ac; |
200 |
|
char **av; |
209 |
|
fprintf(stderr, "%s: %s\n", progname, err); |
210 |
|
exit(1); |
211 |
|
} |
212 |
< |
if (syncfd != -1) { |
213 |
< |
char buf[32]; |
214 |
< |
buf[read(syncfd, buf, sizeof(buf)-1)] = '\0'; |
215 |
< |
sscanf(buf, "%d %d", &hmult, &vmult); |
212 |
> |
if (syncfp != NULL) { |
213 |
> |
sflock(F_RDLCK); |
214 |
> |
fscanf(syncfp, "%d %d", &hmult, &vmult); |
215 |
> |
sflock(F_UNLCK); |
216 |
|
} |
217 |
|
/* compute piece size */ |
218 |
|
hres /= hmult; |
227 |
|
rpargv[rpargc] = NULL; |
228 |
|
/* open output file */ |
229 |
|
if ((outfd = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0666)) >= 0) { |
230 |
+ |
dolock(outfd, F_WRLCK); |
231 |
|
if ((fp = fdopen(dup(outfd), "w")) == NULL) |
232 |
|
goto filerr; |
233 |
|
printargs(ac, av, fp); /* write header */ |
241 |
|
putc('\n', fp); |
242 |
|
fprtresolu(hres*hmult, vres*vmult, fp); |
243 |
|
} else if ((outfd = open(outfile, O_RDWR)) >= 0) { |
244 |
+ |
dolock(outfd, F_RDLCK); |
245 |
|
if ((fp = fdopen(dup(outfd), "r+")) == NULL) |
246 |
|
goto filerr; |
247 |
|
getheader(fp, NULL, NULL); /* skip header */ |
259 |
|
scanorig = ftell(fp); /* record position of first scanline */ |
260 |
|
if (fclose(fp) == -1) /* done with stream i/o */ |
261 |
|
goto filerr; |
262 |
< |
#if NFS |
240 |
< |
sync(); /* flush NFS buffers */ |
241 |
< |
#endif |
262 |
> |
dolock(outfd, F_UNLCK); |
263 |
|
/* start rpict process */ |
264 |
|
if (open_process(rpd, rpargv) <= 0) { |
265 |
|
fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]); |
287 |
|
nextpiece(xp, yp) /* get next piece assignment */ |
288 |
|
int *xp, *yp; |
289 |
|
{ |
269 |
– |
struct flock fls; |
270 |
– |
char buf[64]; |
271 |
– |
|
290 |
|
if (gotalrm) /* someone wants us to quit */ |
291 |
|
return(0); |
292 |
< |
if (syncfd != -1) { /* use sync file */ |
293 |
< |
fls.l_type = F_WRLCK; /* gain exclusive access */ |
294 |
< |
fls.l_whence = 0; |
295 |
< |
fls.l_start = 0L; |
296 |
< |
fls.l_len = 0L; |
297 |
< |
fcntl(syncfd, F_SETLKW, &fls); |
298 |
< |
lseek(syncfd, 0L, 0); |
299 |
< |
buf[read(syncfd, buf, sizeof(buf)-1)] = '\0'; |
300 |
< |
if (sscanf(buf, "%*d %*d %d %d", xp, yp) < 2) { |
292 |
> |
if (syncfp != NULL) { /* use sync file */ |
293 |
> |
/* |
294 |
> |
* So we don't necessarily have to lock and unlock the file |
295 |
> |
* multiple times (very slow), we establish an exclusive |
296 |
> |
* lock at the beginning on our synchronization file and |
297 |
> |
* maintain it in the subroutine rvrpiece(). |
298 |
> |
*/ |
299 |
> |
sflock(F_WRLCK); |
300 |
> |
fseek(syncfp, 0L, 0); /* read position */ |
301 |
> |
if (fscanf(syncfp, "%*d %*d %d %d", xp, yp) < 2) { |
302 |
|
*xp = hmult-1; |
303 |
|
*yp = vmult; |
304 |
|
} |
305 |
+ |
if (rvrlim == 0) /* initialize recovery limit */ |
306 |
+ |
rvrlim = *xp*vmult + *yp; |
307 |
+ |
if (rvrpiece(xp, yp)) { /* do stragglers first */ |
308 |
+ |
sflock(F_UNLCK); |
309 |
+ |
return(1); |
310 |
+ |
} |
311 |
|
if (--(*yp) < 0) { /* decrement position */ |
312 |
|
*yp = vmult-1; |
313 |
< |
if (--(*xp) < 0) { /* all done! */ |
314 |
< |
close(syncfd); |
313 |
> |
if (--(*xp) < 0) { /* all done */ |
314 |
> |
sflock(F_UNLCK); |
315 |
|
return(0); |
316 |
|
} |
317 |
|
} |
318 |
< |
sprintf(buf, "%4d %4d\n%4d %4d\n", hmult, vmult, *xp, *yp); |
319 |
< |
lseek(syncfd, 0L, 0); /* write new position */ |
320 |
< |
write(syncfd, buf, strlen(buf)); |
321 |
< |
fls.l_type = F_UNLCK; /* release sync file */ |
297 |
< |
fcntl(syncfd, F_SETLKW, &fls); |
318 |
> |
fseek(syncfp, 0L, 0); /* write new position */ |
319 |
> |
fprintf(syncfp, "%4d %4d\n%4d %4d\n\n", hmult, vmult, *xp, *yp); |
320 |
> |
fflush(syncfp); |
321 |
> |
sflock(F_UNLCK); /* release sync file */ |
322 |
|
return(1); |
323 |
|
} |
324 |
< |
if (fgets(buf, sizeof(buf), stdin) == NULL) /* use stdin */ |
301 |
< |
return(0); |
302 |
< |
if (sscanf(buf, "%d %d", xp, yp) == 2) |
303 |
< |
return(1); |
304 |
< |
fprintf(stderr, "%s: input format error\n", progname); |
305 |
< |
exit(cleanup(1)); |
324 |
> |
return(scanf("%d %d", xp, yp) == 2); /* use stdin */ |
325 |
|
} |
326 |
|
|
327 |
|
|
328 |
|
int |
329 |
+ |
rvrpiece(xp, yp) /* check for recoverable pieces */ |
330 |
+ |
register int *xp, *yp; |
331 |
+ |
{ |
332 |
+ |
static char *pdone = NULL; /* which pieces are done */ |
333 |
+ |
static long readpos = -1; /* how far we've read */ |
334 |
+ |
register int i; |
335 |
+ |
/* |
336 |
+ |
* This routine is called by nextpiece() with an |
337 |
+ |
* exclusive lock on syncfp and the file pointer at the |
338 |
+ |
* appropriate position to read in the finished pieces. |
339 |
+ |
*/ |
340 |
+ |
if (rvrlim < 0) |
341 |
+ |
return(0); /* only check if asked */ |
342 |
+ |
if (pdone == NULL) /* first call */ |
343 |
+ |
pdone = calloc(hmult*vmult, sizeof(char)); |
344 |
+ |
if (readpos != -1) /* mark what's been done */ |
345 |
+ |
fseek(syncfp, readpos, 0); |
346 |
+ |
while (fscanf(syncfp, "%d %d", xp, yp) == 2) |
347 |
+ |
pdone[*xp*vmult+*yp] = 1; |
348 |
+ |
if (!feof(syncfp)) { |
349 |
+ |
fprintf(stderr, "%s: format error in sync file\n", progname); |
350 |
+ |
exit(1); |
351 |
+ |
} |
352 |
+ |
readpos = ftell(syncfp); |
353 |
+ |
i = hmult*vmult; /* find an unaccounted for piece */ |
354 |
+ |
while (i-- > rvrlim) |
355 |
+ |
if (!pdone[i]) { |
356 |
+ |
*xp = i / vmult; |
357 |
+ |
*yp = i % vmult; |
358 |
+ |
pdone[i] = 1; /* consider it done */ |
359 |
+ |
return(1); |
360 |
+ |
} |
361 |
+ |
rvrlim = -1; /* nothing left to recover */ |
362 |
+ |
free(pdone); |
363 |
+ |
pdone = NULL; |
364 |
+ |
return(0); |
365 |
+ |
} |
366 |
+ |
|
367 |
+ |
|
368 |
+ |
int |
369 |
|
cleanup(rstat) /* close rpict process and clean up */ |
370 |
|
int rstat; |
371 |
|
{ |
445 |
|
progname, rpargv[0]); |
446 |
|
exit(cleanup(1)); |
447 |
|
} |
448 |
+ |
if (verbose) { /* notify caller */ |
449 |
+ |
printf("%d %d begun\n", xpos, ypos); |
450 |
+ |
fflush(stdout); |
451 |
+ |
} |
452 |
|
unguard(); |
453 |
|
/* load new piece into buffer */ |
454 |
|
for (y = 0; y < vr; y++) { |
481 |
|
/* lock file section so NFS doesn't mess up */ |
482 |
|
fls.l_whence = 0; |
483 |
|
fls.l_type = F_WRLCK; |
484 |
< |
fcntl(outfd, F_SETLKW, &fls); |
484 |
> |
if (fcntl(outfd, F_SETLKW, &fls) < 0) |
485 |
> |
filerr("lock"); |
486 |
|
#endif |
487 |
|
/* write new piece to file */ |
488 |
|
if (lseek(outfd, fls.l_start, 0) == -1) |
489 |
< |
goto seekerr; |
489 |
> |
filerr("seek"); |
490 |
|
if (hmult == 1) { |
491 |
|
if (writebuf(outfd, (char *)pbuf, |
492 |
|
vr*hr*sizeof(COLR)) != vr*hr*sizeof(COLR)) |
493 |
< |
goto writerr; |
493 |
> |
filerr("write"); |
494 |
|
} else |
495 |
|
for (y = 0; y < vr; y++) { |
496 |
|
if (writebuf(outfd, (char *)(pbuf+y*hr), |
497 |
|
hr*sizeof(COLR)) != hr*sizeof(COLR)) |
498 |
< |
goto writerr; |
498 |
> |
filerr("write"); |
499 |
|
if (y < vr-1 && lseek(outfd, |
500 |
|
(long)(hmult-1)*hr*sizeof(COLR), |
501 |
|
1) == -1) |
502 |
< |
goto seekerr; |
502 |
> |
filerr("seek"); |
503 |
|
} |
504 |
+ |
#if NFS |
505 |
+ |
fls.l_type = F_UNLCK; /* release lock */ |
506 |
+ |
if (fcntl(outfd, F_SETLKW, &fls) < 0) |
507 |
+ |
filerr("lock"); |
508 |
+ |
#endif |
509 |
+ |
if (syncfp != NULL) { /* record what's been done */ |
510 |
+ |
sflock(F_WRLCK); |
511 |
+ |
fseek(syncfp, 0L, 2); /* append index */ |
512 |
+ |
fprintf(syncfp, "%4d %4d\n", xpos, ypos); |
513 |
+ |
fflush(syncfp); |
514 |
+ |
/*** Unlock not necessary, since |
515 |
+ |
sflock(F_UNLCK); _exit() or nextpiece() is next ***/ |
516 |
+ |
} |
517 |
|
if (verbose) { /* notify caller */ |
518 |
|
printf("%d %d done\n", xpos, ypos); |
519 |
|
fflush(stdout); |
520 |
|
} |
521 |
< |
if (pid == -1) { /* didn't fork or fork failed */ |
445 |
< |
#if NFS |
446 |
< |
fls.l_type = F_UNLCK; /* release lock */ |
447 |
< |
fcntl(outfd, F_SETLKW, &fls); |
448 |
< |
#endif |
521 |
> |
if (pid == -1) /* didn't fork or fork failed */ |
522 |
|
return(0); |
523 |
< |
} |
524 |
< |
_exit(0); /* else exit child process (releasing lock) */ |
525 |
< |
seekerr: |
526 |
< |
fprintf(stderr, "%s: seek error on file \"%s\"\n", progname, outfile); |
527 |
< |
_exit(1); |
528 |
< |
writerr: |
529 |
< |
fprintf(stderr, "%s: write error on file \"%s\"\n", progname, outfile); |
523 |
> |
_exit(0); /* else exit child process (releasing locks) */ |
524 |
> |
} |
525 |
> |
|
526 |
> |
|
527 |
> |
filerr(t) /* report file error and exit */ |
528 |
> |
char *t; |
529 |
> |
{ |
530 |
> |
extern char *sys_errlist[]; |
531 |
> |
|
532 |
> |
fprintf(stderr, "%s: %s error on file \"%s\": %s\n", |
533 |
> |
progname, t, outfile, sys_errlist[errno]); |
534 |
|
_exit(1); |
535 |
|
} |
536 |
|
|