+ if (!stralloc_copy(&line,&fnlastd)) die_nomem();
+ line.s[line.len - 2] = 'D';
+ fd = open_trunc(line.s); /* write lastd. Do safe */
+ /* since we read before lock*/
+ if (fd == -1) strerr_die4sys(111,FATAL,ERR_OPEN,line.s,": ");
+ substdio_fdbuf(&ssout,write,fd,outbuf,sizeof(outbuf));
+ if (substdio_put(&ssout,strnum,fmt_ulong(strnum,when)) == -1)
+ strerr_die4sys(111,FATAL,ERR_WRITE,line.s,": ");
+ if (substdio_put(&ssout,"\n",1) == -1) /* prettier */
+ strerr_die4sys(111,FATAL,ERR_WRITE,line.s,": ");
+ if (substdio_flush(&ssout) == -1)
+ strerr_die4sys(111,FATAL,ERR_FLUSH,line.s,": ");
+ if (fsync(fd) == -1)
+ strerr_die4sys(111,FATAL,ERR_SYNC,line.s,": ");
+ if (close(fd) == -1)
+ strerr_die4sys(111,FATAL,ERR_CLOSE,line.s,": ");
+
+ if (rename(line.s,fnlastd.s) == -1)
+ strerr_die4sys(111,FATAL,ERR_MOVE,fnlastd.s,": ");
+
+ /* no need to do h dir cleaning more than */
+ /* once per 1-2 days (17-30 days for all) */
+ if (stat(fnlasth.s,&st) == -1) {
+ if (errno != error_noent)
+ strerr_die4sys(111,FATAL,ERR_STAT,fnlasth.s,": ");
+ } else if (when < st.st_mtime + 100000 && when > st.st_mtime)
+ _exit(0); /* 2nd comp to guard against corruption */
+
+ if (slurp(fnlasth.s,&lasth,16) == -1) /* last h cleaned */
+ strerr_die4sys(111,FATAL,ERR_READ,fnlasth.s,": ");
+ if (!stralloc_0(&lasth)) die_nomem();
+ ch = lasth.s[0]; /* clean h */
+ if (ch >= 'a' && ch <= 'o')
+ ++ch;
+ else
+ ch = 'a';
+ lasth.s[0] = ch;
+ if (!stralloc_copys(&line,workdir)) die_nomem();
+ if (!stralloc_cats(&line,"/bounce/h/")) die_nomem();
+ if (!stralloc_catb(&line,lasth.s,1)) die_nomem();
+ if (!stralloc_0(&line)) die_nomem();
+ hdir = opendir(line.s); /* clean ./h/xxxxxx */
+
+ if (!hdir) {
+ if (errno != error_noent)
+ strerr_die4sys(111,FATAL,ERR_OPEN,line.s,": ");
+ } else {
+
+ while ((d = readdir(hdir))) {
+ if (str_equal(d->d_name,".")) continue;
+ if (str_equal(d->d_name,"..")) continue;
+ if (!stralloc_copys(&fn,line.s)) die_nomem();
+ if (!stralloc_append(&fn,"/")) die_nomem();
+ if (!stralloc_cats(&fn,d->d_name)) die_nomem();
+ if (!stralloc_0(&fn)) die_nomem();
+ if (stat(fn.s,&st) == -1) {
+ if (errno == error_noent) continue;
+ strerr_die4sys(111,FATAL,ERR_STAT,fn.s,": ");
+ }
+ if (when > st.st_mtime + 3 * bouncetimeout)
+ if (unlink(fn.s) == -1)
+ strerr_die4sys(111,FATAL,ERR_DELETE,fn.s,": ");