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 |
< |
hdsyncbi(hp, i) /* sync beam in memory with beam on disk */ |
350 |
> |
hdsyncbeam(hp, i) /* sync beam in memory with beam on disk */ |
351 |
|
register HOLO *hp; |
352 |
|
register int i; |
353 |
|
{ |
354 |
< |
unsigned int nrays = hp->bl[i]->nrm; |
354 |
> |
unsigned int nrays; |
355 |
|
long nfo; |
356 |
|
unsigned int n; |
345 |
– |
/* is current fragment OK? */ |
346 |
– |
if (nrays == hp->bi[i].nrd) |
347 |
– |
return(0); |
357 |
|
/* check file status */ |
358 |
< |
if (hp->dirty < 0) |
358 |
> |
if (hdfrag[hp->fd].writerr) |
359 |
|
return(-1); |
360 |
< |
|
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 |
> |
/* locate fragment */ |
368 |
|
if (hp->fd >= nhdfrags || !hdfrag[hp->fd].nlinks) /* untracked */ |
369 |
|
hp->bi[i].fo = lseek(hp->fd, 0L, 2); |
370 |
|
|
391 |
|
(j+FRAGBLK)*sizeof(BEAMI)); |
392 |
|
if (f->fi == NULL) |
393 |
|
error(SYSTEM, |
394 |
< |
"out of memory in hdsyncbi"); |
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) { |
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 |
< |
hp->dirty = -1; /* avoid recursive error */ |
446 |
< |
error(SYSTEM, "write error in hdsyncbi"); |
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; |
468 |
|
nchanged += hdfreebeam(hdlist[i], 0); |
469 |
|
return(nchanged); |
470 |
|
} |
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 |
< |
hdsyncbi(hp, i); /* write new fragment */ |
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; |
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 |
< |
hdsyncbi(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 |
|
|