| 38 |
|
|
| 39 |
|
static struct fragment { |
| 40 |
|
short nlinks; /* number of holodeck sections using us */ |
| 41 |
< |
short nfrags; /* number of known fragments */ |
| 41 |
> |
short writerr; /* write error encountered */ |
| 42 |
> |
int nfrags; /* number of known fragments */ |
| 43 |
|
BEAMI *fi; /* fragments, descending file position */ |
| 44 |
|
long flen; /* last known file length */ |
| 45 |
|
} *hdfrag; /* fragment lists, indexed by file descriptor */ |
| 139 |
|
hp->fd = fd; |
| 140 |
|
hp->dirty = 0; |
| 141 |
|
biglob(hp)->fo = fpos + sizeof(HDGRID); |
| 142 |
< |
biglob(hp)->nrd = 0; /* count rays on disk */ |
| 143 |
< |
for (n = nbeams(hp); n > 0; n--) |
| 144 |
< |
biglob(hp)->nrd += hp->bi[n].nrd; |
| 142 |
> |
/* start tracking fragments */ |
| 143 |
> |
hdattach(fd); |
| 144 |
> |
/* check rays on disk */ |
| 145 |
> |
fpos = hdfilen(fd); |
| 146 |
> |
biglob(hp)->nrd = 0; |
| 147 |
> |
for (n = hproto == NULL ? nbeams(hp) : 0; n > 0; n--) |
| 148 |
> |
if (hp->bi[n].nrd) |
| 149 |
> |
if (hp->bi[n].fo + hp->bi[n].nrd > fpos) |
| 150 |
> |
hp->bi[n].nrd = 0; /* off end */ |
| 151 |
> |
else |
| 152 |
> |
biglob(hp)->nrd += hp->bi[n].nrd; |
| 153 |
|
/* add to holodeck list */ |
| 154 |
|
for (n = 0; n < HDMAX; n++) |
| 155 |
|
if (hdlist[n] == NULL) { |
| 156 |
|
hdlist[n] = hp; |
| 157 |
|
break; |
| 158 |
|
} |
| 150 |
– |
/* start tracking fragments (last) */ |
| 151 |
– |
hdattach(fd); |
| 159 |
|
/* all done */ |
| 160 |
|
return(hp); |
| 161 |
|
memerr: |
| 164 |
|
|
| 165 |
|
|
| 166 |
|
int |
| 167 |
< |
hdsync(hp) /* update directory on disk if necessary */ |
| 167 |
> |
hdsync(hp, all) /* update beams and directory on disk */ |
| 168 |
|
register HOLO *hp; |
| 169 |
+ |
int all; |
| 170 |
|
{ |
| 171 |
|
register int j, n; |
| 172 |
|
|
| 173 |
< |
if (hp == NULL) { /* do all */ |
| 173 |
> |
if (hp == NULL) { /* do all holodecks */ |
| 174 |
|
n = 0; |
| 175 |
|
for (j = 0; hdlist[j] != NULL; j++) |
| 176 |
< |
n += hdsync(hdlist[j]); |
| 176 |
> |
n += hdsync(hdlist[j], all); |
| 177 |
|
return(n); |
| 178 |
|
} |
| 179 |
< |
if (!hp->dirty) /* check first */ |
| 179 |
> |
/* sync the beams */ |
| 180 |
> |
for (j = all ? nbeams(hp) : 0; j > 0; j--) |
| 181 |
> |
if (hp->bl[j] != NULL) |
| 182 |
> |
hdsyncbeam(hp, j); |
| 183 |
> |
if (!hp->dirty) /* directory clean? */ |
| 184 |
|
return(0); |
| 185 |
|
errno = 0; |
| 186 |
|
if (lseek(hp->fd, biglob(hp)->fo, 0) < 0) |
| 347 |
|
|
| 348 |
|
|
| 349 |
|
int |
| 350 |
< |
hdgetbi(hp, i) /* allocate a file fragment */ |
| 350 |
> |
hdsyncbeam(hp, i) /* sync beam in memory with beam on disk */ |
| 351 |
|
register HOLO *hp; |
| 352 |
|
register int i; |
| 353 |
|
{ |
| 354 |
< |
int nrays = hp->bl[i]->nrm; |
| 355 |
< |
|
| 356 |
< |
if (hp->bi[i].nrd == nrays) /* current one will do? */ |
| 354 |
> |
unsigned int nrays; |
| 355 |
> |
long nfo; |
| 356 |
> |
unsigned int n; |
| 357 |
> |
/* check file status */ |
| 358 |
> |
if (hdfrag[hp->fd].writerr) |
| 359 |
> |
return(-1); |
| 360 |
> |
#ifdef DEBUG |
| 361 |
> |
if (i < 1 | i > nbeams(hp)) |
| 362 |
> |
error(CONSISTENCY, "bad beam index in hdsyncbeam"); |
| 363 |
> |
#endif |
| 364 |
> |
/* is current fragment OK? */ |
| 365 |
> |
if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd) |
| 366 |
|
return(0); |
| 367 |
< |
|
| 367 |
> |
/* locate fragment */ |
| 368 |
|
if (hp->fd >= nhdfrags || !hdfrag[hp->fd].nlinks) /* untracked */ |
| 369 |
|
hp->bi[i].fo = lseek(hp->fd, 0L, 2); |
| 370 |
|
|
| 371 |
|
else if (hp->bi[i].fo + hp->bi[i].nrd*sizeof(RAYVAL) == |
| 372 |
|
hdfrag[hp->fd].flen) /* EOF special case */ |
| 373 |
< |
hdfrag[hp->fd].flen = hp->bi[i].fo + nrays*sizeof(RAYVAL); |
| 373 |
> |
hdfrag[hp->fd].flen = (nfo=hp->bi[i].fo) + nrays*sizeof(RAYVAL); |
| 374 |
|
|
| 375 |
|
else { /* general case */ |
| 376 |
|
register struct fragment *f = &hdfrag[hp->fd]; |
| 391 |
|
(j+FRAGBLK)*sizeof(BEAMI)); |
| 392 |
|
if (f->fi == NULL) |
| 393 |
|
error(SYSTEM, |
| 394 |
< |
"out of memory in hdgetbi"); |
| 394 |
> |
"out of memory in hdsyncbeam"); |
| 395 |
|
} |
| 396 |
|
for ( ; ; j--) { /* insert in descending list */ |
| 397 |
|
if (!j || hp->bi[i].fo < f->fi[j-1].fo) { |
| 416 |
|
f->nfrags = j; |
| 417 |
|
} |
| 418 |
|
k = -1; /* find closest-sized fragment */ |
| 419 |
< |
for (j = f->nfrags; j-- > 0; ) |
| 419 |
> |
for (j = nrays ? f->nfrags : 0; j-- > 0; ) |
| 420 |
|
if (f->fi[j].nrd >= nrays && |
| 421 |
|
(k < 0 || f->fi[j].nrd < f->fi[k].nrd)) |
| 422 |
|
if (f->fi[k=j].nrd == nrays) |
| 423 |
|
break; |
| 424 |
|
if (k < 0) { /* no fragment -- extend file */ |
| 425 |
< |
hp->bi[i].fo = f->flen; |
| 425 |
> |
nfo = f->flen; |
| 426 |
|
f->flen += nrays*sizeof(RAYVAL); |
| 427 |
|
} else { /* else use fragment */ |
| 428 |
< |
hp->bi[i].fo = f->fi[k].fo; |
| 428 |
> |
nfo = f->fi[k].fo; |
| 429 |
|
if (f->fi[k].nrd == nrays) { /* delete fragment */ |
| 430 |
|
f->nfrags--; |
| 431 |
|
for (j = k; j < f->nfrags; j++) |
| 436 |
|
} |
| 437 |
|
} |
| 438 |
|
} |
| 439 |
+ |
if (nrays) { /* write the new fragment */ |
| 440 |
+ |
errno = 0; |
| 441 |
+ |
if (lseek(hp->fd, nfo, 0) < 0) |
| 442 |
+ |
error(SYSTEM, "cannot seek on holodeck file"); |
| 443 |
+ |
n = hp->bl[i]->nrm * sizeof(RAYVAL); |
| 444 |
+ |
if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) { |
| 445 |
+ |
hdfrag[hp->fd].writerr++; |
| 446 |
+ |
hdsync(hp, 0); /* sync directory */ |
| 447 |
+ |
error(SYSTEM, "write error in hdsyncbeam"); |
| 448 |
+ |
} |
| 449 |
+ |
} |
| 450 |
|
biglob(hp)->nrd += nrays - hp->bi[i].nrd; |
| 451 |
|
hp->bi[i].nrd = nrays; |
| 452 |
+ |
hp->bi[i].fo = nfo; |
| 453 |
|
markdirty(hp); /* section directory now out of date */ |
| 454 |
|
return(1); |
| 455 |
|
} |
| 460 |
|
register HOLO *hp; |
| 461 |
|
register int i; |
| 462 |
|
{ |
| 463 |
< |
int nchanged, n; |
| 463 |
> |
int nchanged; |
| 464 |
|
|
| 465 |
|
if (hp == NULL) { /* clear all holodecks */ |
| 466 |
|
nchanged = 0; |
| 468 |
|
nchanged += hdfreebeam(hdlist[i], 0); |
| 469 |
|
return(nchanged); |
| 470 |
|
} |
| 471 |
< |
if (hp->fd < 0) /* check for recursive error */ |
| 472 |
< |
return(-1); |
| 471 |
> |
if (hdfrag[hp->fd].writerr) /* check for file error */ |
| 472 |
> |
return(0); |
| 473 |
|
if (i == 0) { /* clear entire holodeck */ |
| 474 |
|
nchanged = 0; |
| 475 |
|
for (i = nbeams(hp); i > 0; i--) |
| 477 |
|
nchanged += hdfreebeam(hp, i); |
| 478 |
|
return(nchanged); |
| 479 |
|
} |
| 480 |
+ |
#ifdef DEBUG |
| 481 |
|
if (i < 1 | i > nbeams(hp)) |
| 482 |
|
error(CONSISTENCY, "bad beam index to hdfreebeam"); |
| 483 |
+ |
#endif |
| 484 |
|
if (hp->bl[i] == NULL) |
| 485 |
|
return(0); |
| 486 |
|
/* check for additions */ |
| 487 |
|
nchanged = hp->bl[i]->nrm - hp->bi[i].nrd; |
| 488 |
< |
if (nchanged) { |
| 489 |
< |
hdgetbi(hp, i); /* allocate a file position */ |
| 455 |
< |
errno = 0; |
| 456 |
< |
if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) |
| 457 |
< |
error(SYSTEM, "cannot seek on holodeck file"); |
| 458 |
< |
n = hp->bl[i]->nrm * sizeof(RAYVAL); |
| 459 |
< |
if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) { |
| 460 |
< |
hp->fd = -1; /* avoid recursive error */ |
| 461 |
< |
error(SYSTEM, "write error in hdfreebeam"); |
| 462 |
< |
} |
| 463 |
< |
} |
| 488 |
> |
if (nchanged) |
| 489 |
> |
hdsyncbeam(hp, i); /* write new fragment */ |
| 490 |
|
blglob(hp)->nrm -= hp->bl[i]->nrm; |
| 491 |
|
free((char *)hp->bl[i]); /* free memory */ |
| 492 |
|
hp->bl[i] = NULL; |
| 500 |
|
register int i; |
| 501 |
|
{ |
| 502 |
|
static BEAM emptybeam; |
| 503 |
< |
int nchanged, n; |
| 503 |
> |
int nchanged; |
| 504 |
|
|
| 505 |
|
if (hp == NULL) { /* clobber all holodecks */ |
| 506 |
|
nchanged = 0; |
| 519 |
|
#endif |
| 520 |
|
return(nchanged); |
| 521 |
|
} |
| 522 |
+ |
#ifdef DEBUG |
| 523 |
|
if (i < 1 | i > nbeams(hp)) |
| 524 |
|
error(CONSISTENCY, "bad beam index to hdkillbeam"); |
| 525 |
+ |
#endif |
| 526 |
|
if (hp->bl[i] != NULL) { /* free memory */ |
| 527 |
|
blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm; |
| 528 |
|
free((char *)hp->bl[i]); |
| 530 |
|
nchanged = hp->bi[i].nrd; |
| 531 |
|
if (hp->bi[i].nrd) { /* free file fragment */ |
| 532 |
|
hp->bl[i] = &emptybeam; |
| 533 |
< |
hdgetbi(hp, i); |
| 533 |
> |
hdsyncbeam(hp, i); |
| 534 |
|
} |
| 535 |
|
hp->bl[i] = NULL; |
| 536 |
|
return(nchanged); |
| 597 |
|
if ((freetarget -= hp[i]->bi[bn[i]].nrd) <= 0) |
| 598 |
|
break; |
| 599 |
|
} |
| 600 |
< |
hdsync(honly); /* synchronize directories as necessary */ |
| 600 |
> |
hdsync(honly, 0); /* synchronize directories as necessary */ |
| 601 |
|
} |
| 602 |
|
|
| 603 |
|
|