+{
+ struct arcq new_arcq;
+
+ begin_critical_section(S_SUPPMSGMAIN);
+ if (arcfp == NULL) {
+ arcfp = fopen(file_arcq, "ab+");
+ }
+ end_critical_section(S_SUPPMSGMAIN);
+
+ /* msgnum < 0 means that we're trying to close the file */
+ if (msgnum < 0) {
+ lprintf(CTDL_DEBUG, "Closing the AdjRefCount queue file\n");
+ begin_critical_section(S_SUPPMSGMAIN);
+ if (arcfp != NULL) {
+ fclose(arcfp);
+ arcfp = NULL;
+ }
+ end_critical_section(S_SUPPMSGMAIN);
+ return;
+ }
+
+ /*
+ * If we can't open the queue, perform the operation synchronously.
+ */
+ if (arcfp == NULL) {
+ TDAP_AdjRefCount(msgnum, incr);
+ return;
+ }
+
+ new_arcq.arcq_msgnum = msgnum;
+ new_arcq.arcq_delta = incr;
+ fwrite(&new_arcq, sizeof(struct arcq), 1, arcfp);
+ fflush(arcfp);
+
+ return;
+}
+
+
+/*
+ * TDAP_ProcessAdjRefCountQueue()
+ *
+ * Process the queue of message count adjustments that was created by calls
+ * to AdjRefCount() ... by reading the queue and calling TDAP_AdjRefCount()
+ * for each one. This should be an "off hours" operation.
+ */
+int TDAP_ProcessAdjRefCountQueue(void)
+{
+ char file_arcq_temp[PATH_MAX];
+ int r;
+ FILE *fp;
+ struct arcq arcq_rec;
+ int num_records_processed = 0;
+
+ snprintf(file_arcq_temp, sizeof file_arcq_temp, "%s2", file_arcq);
+
+ begin_critical_section(S_SUPPMSGMAIN);
+ if (arcfp != NULL) {
+ fclose(arcfp);
+ arcfp = NULL;
+ }
+
+ r = link(file_arcq, file_arcq_temp);
+ if (r != 0) {
+ lprintf(CTDL_CRIT, "%s: %s\n", file_arcq_temp, strerror(errno));
+ end_critical_section(S_SUPPMSGMAIN);
+ return(num_records_processed);
+ }
+
+ unlink(file_arcq);
+ end_critical_section(S_SUPPMSGMAIN);
+
+ fp = fopen(file_arcq_temp, "rb");
+ if (fp == NULL) {
+ lprintf(CTDL_CRIT, "%s: %s\n", file_arcq_temp, strerror(errno));
+ return(num_records_processed);
+ }
+
+ while (fread(&arcq_rec, sizeof(struct arcq), 1, fp) == 1) {
+ TDAP_AdjRefCount(arcq_rec.arcq_msgnum, arcq_rec.arcq_delta);
+ ++num_records_processed;
+ }
+
+ fclose(fp);
+ r = unlink(file_arcq_temp);
+ if (r != 0) {
+ lprintf(CTDL_CRIT, "%s: %s\n", file_arcq_temp, strerror(errno));
+ }
+
+ return(num_records_processed);
+}
+
+
+
+/*
+ * TDAP_AdjRefCount - adjust the reference count for a message.
+ * This one does it "for real" because it's called by
+ * the autopurger function that processes the queue
+ * created by AdjRefCount(). If a message's reference
+ * count becomes zero, we also delete the message from
+ * disk and de-index it.
+ */
+void TDAP_AdjRefCount(long msgnum, int incr)