--- courier-0.45.4.orig/imap/imapscanclient.c 2004-01-11 03:59:03.000000000 +0100 +++ courier-0.45.4.new/imap/imapscanclient.c 2004-06-10 11:48:02.000000000 +0200 @@ -338,7 +338,7 @@ unsigned tempinfo_cnt=0, i; FILE *fp; char *p, *q; -unsigned long uidv, nextuid; +unsigned long uidv=0L, nextuid; int version; struct stat stat_buf; DIR *dirp; @@ -363,31 +363,6 @@ ** file in the tmp subdirectory. */ - { - char uniqbuf[80]; - static unsigned tmpuniqcnt=0; - struct maildir_tmpcreate_info createInfo; - int fd; - - maildir_tmpcreate_init(&createInfo); - - createInfo.maildir=dir; - createInfo.hostname=getenv("HOSTNAME"); - sprintf(uniqbuf, "imapuid_%u", tmpuniqcnt++); - createInfo.uniq=uniqbuf; - createInfo.doordie=1; - - if ((fd=maildir_tmpcreate_fd(&createInfo)) < 0) - { - write_error_exit(0); - } - close(fd); - - newdbfilepath=createInfo.tmpname; - createInfo.tmpname=NULL; - maildir_tmpcreate_free(&createInfo); - } - /* Step 1 - read the cache file */ if ((fp=fopen(dbfilepath, "r")) != 0 && @@ -408,7 +383,6 @@ malloc(sizeof(struct tempinfo))) == 0 || (newtmpl->filename=strdup(q)) == 0) { - unlink(newdbfilepath); write_error_exit(0); } newtmpl->next=tempinfo_list; @@ -423,11 +397,11 @@ } else if(!ro) { + dowritecache=1; + nextuid=1; /* First time - create the cache file */ - - if (fp) fclose(fp); - nextuid=1; +#if 0 if ((fp=fopen(newdbfilepath, "w")) == 0 || fstat(fileno(fp), &stat_buf) != 0) { @@ -447,6 +421,7 @@ } uidv=stat_buf.st_mtime; dowritecache=1; +#endif } else { @@ -488,7 +463,6 @@ || (newtmpl->filename=strdup(strrchr(uidplus->curfilename, '/')+1)) == 0) { - unlink(newdbfilepath); write_error_exit(0); } @@ -508,28 +482,6 @@ dowritecache=1; } - if (!fp && (fp=fopen(newdbfilepath, "w")) == 0) - { - imapscanfail(newdbfilepath); - - /* bk: ignore error */ - unlink(newdbfilepath); - unlink(dbfilepath); - /* - free(dbfilepath); - unlink(newdbfilepath); - free(newdbfilepath); - while (tempinfo_list) - { - tempinfoptr=tempinfo_list; - tempinfo_list=tempinfoptr->next; - free(tempinfoptr->filename); - free(tempinfoptr); - } - return (-1); - */ - } - /* ** Convert the link list of cached files to an array, then ** sort it by filename. @@ -538,7 +490,6 @@ if ((tempinfo_array=(struct tempinfo **)malloc( (tempinfo_cnt+1)*sizeof(struct tempinfo *))) == 0) { - unlink(newdbfilepath); write_error_exit(0); } @@ -583,7 +534,6 @@ if ((newtmpl=(struct tempinfo *) malloc(sizeof(struct tempinfo))) == 0) { - unlink(newdbfilepath); write_error_exit(0); } newtmpl->filename=p; @@ -673,7 +623,6 @@ +sizeof(MDIRSEP "2,")); if (!curname) { - unlink(newdbfilepath); write_error_exit(0); } strcpy(curname, newname); @@ -690,7 +639,6 @@ if ((newtmpl=(struct tempinfo *) malloc(sizeof(struct tempinfo))) == 0) { - unlink(newdbfilepath); write_error_exit(0); } newtmpl->filename=my_strdup(z+1); @@ -746,7 +694,6 @@ if ((tempinfo_array=(struct tempinfo **)malloc( (tempinfo_cnt+1)*sizeof(struct tempinfo *))) == 0) { - unlink(newdbfilepath); write_error_exit(0); } @@ -768,58 +715,81 @@ tempinfo_array[i]->uid= nextuid++; dowritecache=1; } + + /* Ok, we need to write down the cache */ + if (!ro && dowritecache) { + /* Create a temporary file */ + char uniqbuf[80]; + static unsigned tmpuniqcnt=0; + struct maildir_tmpcreate_info createInfo; + int fd; + + maildir_tmpcreate_init(&createInfo); + + createInfo.maildir=dir; + createInfo.hostname=getenv("HOSTNAME"); + sprintf(uniqbuf, "imapuid_%u", tmpuniqcnt++); + createInfo.uniq=uniqbuf; + createInfo.doordie=1; + + if((fd=maildir_tmpcreate_fd(&createInfo)) < 0) { + if(errno != EDQUOT) + write_error_exit(0); + } else { + newdbfilepath=createInfo.tmpname; + createInfo.tmpname=NULL; + maildir_tmpcreate_free(&createInfo); + + fp=fdopen(fd, "w"); + if(!fp) + imapscanfail(newdbfilepath); - /* bk: ignore if failed to open file */ - if (!ro && dowritecache && fp) - { - /* Step 7 - write out the new cache file */ - - version=IMAPDBVERSION; - fprintf(fp, "%d %lu %lu\n", version, uidv, nextuid); - - for (i=0; ifilename, MDIRSEP[0]); - if (q) *q=0; - fprintf(fp, "%lu %s\n", tempinfo_array[i]->uid, - tempinfo_array[i]->filename); - if (q) *q=MDIRSEP[0]; - } - if (fflush(fp) || ferror(fp) || fclose(fp)) - { - imapscanfail(dir); - fclose(fp); - /* bk: ignore if failed */ - unlink(newdbfilepath); - unlink(dbfilepath); - /* - free(tempinfo_array); - free(dbfilepath); - unlink(newdbfilepath); - free(newdbfilepath); - while (tempinfo_list) - { - tempinfoptr=tempinfo_list; - tempinfo_list=tempinfoptr->next; - free(tempinfoptr->filename); - free(tempinfoptr); - } - return (-1); - */ - } - /* bk */ - else - - rename(newdbfilepath, dbfilepath); - } - else - { - if (fp) - fclose(fp); - unlink(newdbfilepath); + if(fp && !uidv) { + if(fstat(fd, &stat_buf) != 0) { + fclose(fp); + fp=NULL; + imapscanfail(newdbfilepath); + } else + uidv=stat_buf.st_mtime; + } + + if(fp) { + version=IMAPDBVERSION; + fprintf(fp, "%d %lu %lu\n", version, uidv, nextuid); + + for (i=0; ifilename, MDIRSEP[0]); + if(q) + *q=0; + fprintf(fp, "%lu %s\n", tempinfo_array[i]->uid, tempinfo_array[i]->filename); + if(q) + *q=MDIRSEP[0]; + } + + if(fflush(fp) || ferror(fp)) { + if(errno != EDQUOT) + imapscanfail(dir); + fclose(fp); + fp=NULL; + } else + rename(newdbfilepath, dbfilepath); + } + + if(fp && fclose(fp)) { + if(errno != EDQUOT) + imapscanfail(dir); + fp=NULL; + } + + if(!fp) { + unlink(dbfilepath); + unlink(newdbfilepath); + } + + free(dbfilepath); + free(newdbfilepath); + } } - free(dbfilepath); - free(newdbfilepath); /* Step 8 - create the final scaninfo array */