ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/ambient.c
(Generate patch)

Comparing ray/src/rt/ambient.c (file contents):
Revision 2.128 by greg, Thu Dec 19 19:34:31 2024 UTC vs.
Revision 2.129 by greg, Thu Jan 23 02:15:33 2025 UTC

# Line 33 | Line 33 | double minarad;                /* minimum ambient radius */
33   static AMBTREE  atrunk;         /* our ambient trunk node */
34  
35   static FILE  *ambfp = NULL;     /* ambient file pointer */
36 < static int  nunflshed = 0;      /* number of unflushed ambient values */
36 > static int  nunflshed;          /* number of unflushed ambient values */
37 > static FILE  *ambinp = NULL;    /* input pointer for ambient i/o */
38  
39   static double  avsum = 0.;              /* computed ambient value sum (log) */
40   static unsigned int  navsum = 0;        /* number of values in avsum */
41   static unsigned int  nambvals = 0;      /* total number of indirect values */
42 < static unsigned int  nambshare = 0;     /* number of values from file */
42 < static FILE  *ambinp = NULL;            /* auxiliary file for input */
43 < static long  lastpos = -1;              /* last flush position */
42 > static off_t  lastpos = -1;             /* last flush position */
43  
44   #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
45  
# Line 69 | Line 68 | static int     makeambient(SCOLOR acol, RAY *r, FVECT rn,
68   static int      extambient(SCOLOR cr, AMBVAL *ap, FVECT pv, FVECT nv,
69                                  FVECT uvw[3]);
70  
72 #ifdef  F_SETLKW
73 static void aflock(int  typ);
74 #endif
71  
76
72   void
73   setambres(                              /* set ambient resolution */
74          int  ar
# Line 116 | Line 111 | setambacc(                             /* set ambient accuracy */
111   void
112   setambient(void)                                /* initialize calculation */
113   {
114 <        int     readonly = 0;
115 <        long    flen;
114 >        int     exists;
115 >        off_t   flen;
116          AMBVAL  amb;
117                                                  /* make sure we're fresh */
118          ambdone();
# Line 132 | Line 127 | setambient(void)                               /* initialize calculation */
127                  error(WARNING, errmsg);
128                  return;
129          }
130 <                                                /* open ambient file */
131 <        if ((ambfp = fopen(ambfile, "r+")) == NULL)
132 <                readonly = (ambfp = fopen(ambfile, "r")) != NULL;
133 <        if (ambfp != NULL) {
134 <                initambfile(0);                 /* file exists */
130 >        exists = access(ambfile, F_OK) == 0;    /* check existence, first */
131 >        ambfp = fopen(ambfile, "a+");           /* try read/append */
132 >        if (!exists & (ambfp == NULL)) {
133 >                sprintf(errmsg, "cannot create ambient file \"%s\"", ambfile);
134 >                error(SYSTEM, errmsg);
135 >        }
136 >        if (ambfp == NULL) {                    /* try opening read-only? */
137 >                if ((ambfp = fopen(ambfile, "r")) == NULL) {
138 >                        sprintf(errmsg,
139 >                        "cannot open ambient file \"%s\" for reading",
140 >                                        ambfile);
141 >                        error(SYSTEM, errmsg);
142 >                }
143 >                exists = -1;                    /* flag read-only */
144 >        } else if (exists)
145 >                rewind(ambfp);  /* XXX not necessary? */
146 >
147 >        if (exists) {
148 >                initambfile(0);                 /* file already exists */
149                  lastpos = ftell(ambfp);
150                  while (readambval(&amb, ambfp))
151 <                        avstore(&amb);
152 <                nambshare = nambvals;           /* share loaded values */
144 <                if (readonly) {
151 >                        avstore(&amb);          /* load what we can */
152 >                if (exists < 0) {               /* read-only? */
153                          sprintf(errmsg,
154                                  "loaded %u values from read-only ambient file",
155                                          nambvals);
156                          error(WARNING, errmsg);
157                          fclose(ambfp);          /* close file so no writes */
158                          ambfp = NULL;
159 <                        return;                 /* avoid ambsync() */
159 >                        return;
160                  }
161                                                  /* align file pointer */
162 <                lastpos += (long)nambvals*AMBVALSIZ;
163 <                flen = lseek(fileno(ambfp), (off_t)0, SEEK_END);
162 >                lastpos += (off_t)nambvals*AMBVALSIZ;
163 >                flen = lseek(fileno(ambfp), 0, SEEK_END);
164                  if (flen != lastpos) {
165                          sprintf(errmsg,
166 <                        "ignoring last %ld values in ambient file (corrupted)",
167 <                                        (flen - lastpos)/AMBVALSIZ);
166 >                        "ignoring last %lu values in ambient file (corrupted)",
167 >                                (unsigned long)((flen - lastpos)/AMBVALSIZ));
168                          error(WARNING, errmsg);
169 <                        fseek(ambfp, lastpos, SEEK_SET);
170 <                        ftruncate(fileno(ambfp), (off_t)lastpos);
169 >                                                /* fseek() not needed? */
170 >                        fseeko(ambfp, lastpos, SEEK_SET);
171 >                        ftruncate(fileno(ambfp), lastpos);
172                  }
173 <        } else if ((ambfp = fopen(ambfile, "w+")) != NULL) {
174 <                initambfile(1);                 /* else create new file */
173 >        } else {
174 >                initambfile(1);                 /* else start new file */
175                  fflush(ambfp);
176                  lastpos = ftell(ambfp);
168        } else {
169                sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile);
170                error(SYSTEM, errmsg);
177          }
172 #ifdef  F_SETLKW
173        aflock(F_UNLCK);                        /* release file */
174 #endif
178   }
179  
180  
# Line 179 | Line 182 | void
182   ambdone(void)                   /* close ambient file and free memory */
183   {
184          if (ambfp != NULL) {            /* close ambient file */
185 <                ambsync();
183 <                fclose(ambfp);
185 >                fclose(ambfp);          /* don't call ambsync() */
186                  ambfp = NULL;
187 <                if (ambinp != NULL) {  
187 >                lastpos = -1;
188 >                if (ambinp != NULL) {
189                          fclose(ambinp);
190                          ambinp = NULL;
191                  }
189                lastpos = -1;
192          }
193                                          /* free ambient tree */
194          unloadatree(&atrunk, avfree);
# Line 194 | Line 196 | ambdone(void)                  /* close ambient file and free memory
196          avsum = 0.;
197          navsum = 0;
198          nambvals = 0;
197        nambshare = 0;
199   }
200  
201  
# Line 632 | Line 633 | initambfile(           /* initialize ambient file */
633          static char  *mybuf = NULL;
634          int  ntries = 3;
635  
636 < #ifdef  F_SETLKW
637 <        aflock(cre8 ? F_WRLCK : F_RDLCK);
637 < #endif
636 >        if (!AMBFLUSH)
637 >                error(INTERNAL, "BUFSIZ too small in initambfile");
638          SET_FILE_BINARY(ambfp);
639          if (mybuf == NULL)
640 <                mybuf = (char *)bmalloc(BUFSIZ+8);
640 >                mybuf = (char *)bmalloc(BUFSIZ);
641          setbuf(ambfp, mybuf);
642 +        nunflshed = 0;
643   retry:
644          if (cre8) {                     /* new file */
645                  newheader("RADIANCE", ambfp);
# Line 667 | Line 668 | retry:
668                  putambmagic(ambfp);
669          } else if (getheader(ambfp, amb_headline, NULL) < 0 || !hasambmagic(ambfp)) {
670                  if (--ntries > 0 && ftell(ambfp) == 0) {
670 #ifdef  F_SETLKW
671                        aflock(F_UNLCK);
671                          clearerr(ambfp);
672                          sleep(2);
674                        aflock(F_RDLCK);
675 #else
676                        clearerr(ambfp);
677                        sleep(2);
678 #endif
673                          goto retry;
674                  }
675                  error(USER, "bad/incompatible ambient file");
# Line 813 | Line 807 | sortambvals(void)                      /* resort ambient values */
807   }
808  
809  
816 #ifdef  F_SETLKW
817
818 static void
819 aflock(                 /* lock/unlock ambient file */
820        int  typ
821 )
822 {
823        static struct flock  fls;       /* static so initialized to zeroes */
824
825        if (typ == fls.l_type)          /* already called? */
826                return;
827
828        fls.l_type = typ;
829        do
830                if (fcntl(fileno(ambfp), F_SETLKW, &fls) != -1)
831                        return;
832        while (errno == EINTR);
833        
834        error(SYSTEM, "cannot (un)lock ambient file");
835 }
836
837
810   int
811   ambsync(void)                   /* synchronize ambient file */
812   {
813 <        long  flen;
813 >        off_t   newpos;
814 >        int     n;
815          AMBVAL  avs;
843        int  n;
816  
817          if (ambfp == NULL)      /* no ambient file? */
818                  return(0);
819 <                                /* gain appropriate access */
820 <        aflock(nunflshed ? F_WRLCK : F_RDLCK);
821 <                                /* see if file has grown */
822 <        if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0)
819 >
820 >        if (nunflshed > 0) {    /* append new values? */
821 >                if (fflush(ambfp) < 0)
822 >                        return(EOF);
823 >        } else if (fseeko(ambfp, 0, SEEK_END) < 0)
824                  goto seekerr;
825 <        if ((n = flen - lastpos) > 0) {         /* file has grown */
826 <                if (ambinp == NULL) {           /* get new file pointer */
827 <                        ambinp = fopen(ambfile, "rb");
828 <                        if (ambinp == NULL)
829 <                                error(SYSTEM, "fopen failed in ambsync");
825 >
826 >        if ((newpos = ftello(ambfp)) < 0)
827 >                goto seekerr;
828 >                                /* how many others added? */
829 >        n = (newpos - lastpos)/AMBVALSIZ - nunflshed;
830 >        nunflshed = 0;
831 >        if (n <= 0) {           /* no one helping this time? */
832 >                lastpos = newpos;
833 >                return(0);
834 >        }
835 >        if (ambinp == NULL) {   /* else need to open for input? */
836 >                ambinp = fopen(ambfile, "r");
837 >                if (ambinp == NULL) {
838 >                        sprintf(errmsg, "cannot reopen ambient file \"%s\"",
839 >                                        ambfile);
840 >                        error(SYSTEM, errmsg);
841                  }
842 <                if (fseek(ambinp, lastpos, SEEK_SET) < 0)
843 <                        goto seekerr;
844 <                while (n >= AMBVALSIZ) {        /* load contributed values */
845 <                        if (!readambval(&avs, ambinp)) {
846 <                                sprintf(errmsg,
847 <                        "ambient file \"%s\" corrupted near character %ld",
848 <                                                ambfile, flen - n);
849 <                                error(WARNING, errmsg);
850 <                                break;
851 <                        }
852 <                        avstore(&avs);
869 <                        n -= AMBVALSIZ;
842 >                SET_FILE_BINARY(ambinp);
843 >        }
844 >                                /* read from last endpoint */
845 >        if (fseeko(ambinp, lastpos, SEEK_SET) < 0)
846 >                goto seekerr;
847 >        while (n-- > 0) {       /* load new contributed values */
848 >                if (!readambval(&avs, ambinp)) {
849 >                        sprintf(errmsg, "ambient file \"%s\" corrupted",
850 >                                        ambfile);
851 >                        error(WARNING, errmsg);
852 >                        break;
853                  }
854 <                lastpos = flen - n;             /* check alignment */
872 <                if (n && lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0)
873 <                        goto seekerr;
854 >                avstore(&avs);
855          }
856 <        n = fflush(ambfp);                      /* calls write() at last */
857 <        lastpos += (long)nunflshed*AMBVALSIZ;
877 <        aflock(F_UNLCK);                        /* release file */
878 <        nunflshed = 0;
879 <        return(n);
856 >        lastpos = newpos;       /* update endpoint */
857 >        return(0);
858   seekerr:
859          error(SYSTEM, "seek failed in ambsync");
860          return(EOF);    /* pro forma return */
861   }
884
885 #else   /* ! F_SETLKW */
886
887 int
888 ambsync(void)                   /* flush ambient file */
889 {
890        if (ambfp == NULL)
891                return(0);
892        nunflshed = 0;
893        return(fflush(ambfp));
894 }
895
896 #endif  /* ! F_SETLKW */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines