--- ray/src/hd/holofile.c 1997/12/11 11:29:35 3.10 +++ ray/src/hd/holofile.c 1997/12/12 19:52:31 3.12 @@ -157,18 +157,23 @@ memerr: int -hdsync(hp) /* update directory on disk if necessary */ +hdsync(hp, all) /* update beams and directory on disk */ register HOLO *hp; +int all; { register int j, n; - if (hp == NULL) { /* do all */ + if (hp == NULL) { /* do all holodecks */ n = 0; for (j = 0; hdlist[j] != NULL; j++) - n += hdsync(hdlist[j]); + n += hdsync(hdlist[j], all); return(n); } - if (!hp->dirty) /* check first */ + /* sync the beams */ + for (j = all ? nbeams(hp) : 0; j > 0; j--) + if (hp->bl[j] != NULL) + hdsyncbeam(hp, j); + if (!hp->dirty) /* directory dirty? */ return(0); errno = 0; if (lseek(hp->fd, biglob(hp)->fo, 0) < 0) @@ -335,21 +340,30 @@ register int i; int -hdgetbi(hp, i) /* allocate a file fragment */ +hdsyncbeam(hp, i) /* sync beam in memory with beam on disk */ register HOLO *hp; register int i; { - int nrays = hp->bl[i]->nrm; - - if (hp->bi[i].nrd == nrays) /* current one will do? */ + unsigned int nrays; + long nfo; + unsigned int n; +#ifdef DEBUG + if (i < 1 | i > nbeams(hp)) + error(CONSISTENCY, "bad beam index in hdsyncbeam"); +#endif + /* is current fragment OK? */ + if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd) return(0); + /* check file status */ + if (hp->dirty < 0) + return(-1); if (hp->fd >= nhdfrags || !hdfrag[hp->fd].nlinks) /* untracked */ hp->bi[i].fo = lseek(hp->fd, 0L, 2); else if (hp->bi[i].fo + hp->bi[i].nrd*sizeof(RAYVAL) == hdfrag[hp->fd].flen) /* EOF special case */ - hdfrag[hp->fd].flen = hp->bi[i].fo + nrays*sizeof(RAYVAL); + hdfrag[hp->fd].flen = (nfo=hp->bi[i].fo) + nrays*sizeof(RAYVAL); else { /* general case */ register struct fragment *f = &hdfrag[hp->fd]; @@ -370,7 +384,7 @@ register int i; (j+FRAGBLK)*sizeof(BEAMI)); if (f->fi == NULL) error(SYSTEM, - "out of memory in hdgetbi"); + "out of memory in hdsyncbeam"); } for ( ; ; j--) { /* insert in descending list */ if (!j || hp->bi[i].fo < f->fi[j-1].fo) { @@ -395,16 +409,16 @@ register int i; f->nfrags = j; } k = -1; /* find closest-sized fragment */ - for (j = f->nfrags; j-- > 0; ) + for (j = nrays ? f->nfrags : 0; j-- > 0; ) if (f->fi[j].nrd >= nrays && (k < 0 || f->fi[j].nrd < f->fi[k].nrd)) if (f->fi[k=j].nrd == nrays) break; if (k < 0) { /* no fragment -- extend file */ - hp->bi[i].fo = f->flen; + nfo = f->flen; f->flen += nrays*sizeof(RAYVAL); } else { /* else use fragment */ - hp->bi[i].fo = f->fi[k].fo; + nfo = f->fi[k].fo; if (f->fi[k].nrd == nrays) { /* delete fragment */ f->nfrags--; for (j = k; j < f->nfrags; j++) @@ -415,8 +429,19 @@ register int i; } } } + if (nrays) { /* write the new fragment */ + errno = 0; + if (lseek(hp->fd, nfo, 0) < 0) + error(SYSTEM, "cannot seek on holodeck file"); + n = hp->bl[i]->nrm * sizeof(RAYVAL); + if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) { + hp->dirty = -1; /* avoid recursive error */ + error(SYSTEM, "write error in hdsyncbeam"); + } + } biglob(hp)->nrd += nrays - hp->bi[i].nrd; hp->bi[i].nrd = nrays; + hp->bi[i].fo = nfo; markdirty(hp); /* section directory now out of date */ return(1); } @@ -427,7 +452,7 @@ hdfreebeam(hp, i) /* free beam, writing if dirty */ register HOLO *hp; register int i; { - int nchanged, n; + int nchanged; if (hp == NULL) { /* clear all holodecks */ nchanged = 0; @@ -435,8 +460,6 @@ register int i; nchanged += hdfreebeam(hdlist[i], 0); return(nchanged); } - if (hp->fd < 0) /* check for recursive error */ - return(-1); if (i == 0) { /* clear entire holodeck */ nchanged = 0; for (i = nbeams(hp); i > 0; i--) @@ -444,23 +467,16 @@ register int i; nchanged += hdfreebeam(hp, i); return(nchanged); } +#ifdef DEBUG if (i < 1 | i > nbeams(hp)) error(CONSISTENCY, "bad beam index to hdfreebeam"); +#endif if (hp->bl[i] == NULL) return(0); /* check for additions */ nchanged = hp->bl[i]->nrm - hp->bi[i].nrd; - if (nchanged) { - hdgetbi(hp, i); /* allocate a file position */ - errno = 0; - if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) - error(SYSTEM, "cannot seek on holodeck file"); - n = hp->bl[i]->nrm * sizeof(RAYVAL); - if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) { - hp->fd = -1; /* avoid recursive error */ - error(SYSTEM, "write error in hdfreebeam"); - } - } + if (nchanged) + hdsyncbeam(hp, i); /* write new fragment */ blglob(hp)->nrm -= hp->bl[i]->nrm; free((char *)hp->bl[i]); /* free memory */ hp->bl[i] = NULL; @@ -474,7 +490,7 @@ register HOLO *hp; register int i; { static BEAM emptybeam; - int nchanged, n; + int nchanged; if (hp == NULL) { /* clobber all holodecks */ nchanged = 0; @@ -493,8 +509,10 @@ register int i; #endif return(nchanged); } +#ifdef DEBUG if (i < 1 | i > nbeams(hp)) error(CONSISTENCY, "bad beam index to hdkillbeam"); +#endif if (hp->bl[i] != NULL) { /* free memory */ blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm; free((char *)hp->bl[i]); @@ -502,7 +520,7 @@ register int i; nchanged = hp->bi[i].nrd; if (hp->bi[i].nrd) { /* free file fragment */ hp->bl[i] = &emptybeam; - hdgetbi(hp, i); + hdsyncbeam(hp, i); } hp->bl[i] = NULL; return(nchanged); @@ -569,7 +587,7 @@ register HOLO *honly; /* NULL means check all */ if ((freetarget -= hp[i]->bi[bn[i]].nrd) <= 0) break; } - hdsync(honly); /* synchronize directories as necessary */ + hdsync(honly, 0); /* synchronize directories as necessary */ }