HylaFAX The world's most advanced open source fax server

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

[hylafax-users] faxarchive: program to archive old faxes



Hi all,

	Since it would be nice to archive faxes by sequence number, and
hylafax doesn't seem to have builtin facilities to do so, I hacked up a
little program to do it.

	It's designed to run from cron, and it archives faxes in batches
of 100 in a user defined subdirectory.

	The code is cobbled together, not too readable, and could
certainly be improved a lot.  But it does what I need - thought perhaps
someone else out there might be interested in it.

Ross Vandegrift
ross@willow.seitz.com

/* 
   faxarchive is a program to archive faxes in batches
   of 100, by sequence number.

   by Ross Vandegrift <ross@willow.seitz.com>

   This code is in the public domain.
*/

#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <dirent.h>
#include <malloc.h>
#include <string.h>

#define RECVQ_DIR "/var/spool/hylafax/recvq"
#define ARCHIVE_DIR "/var/spool/hylafax/recvq/archive"
#define SEQF "/var/spool/hylafax/recvq/seqf"
#define LAST_SEQF "/var/spool/hylafax/recvq/archive/seqf"

/* - Testing settings
  #define RECVQ_DIR "/home/usr/ross/fax/test"
  #define ARCHIVE_DIR "/home/usr/ross/fax/test/archive"
  #define SEQF "/home/usr/ross/fax/test/seqf"
  #define LAST_SEQF "/home/usr/ross/fax/test/archive/seqf"
*/
    
int
main(void)
{
  FILE *fp;
  char buf [8];
  int seq, last_seq;
  char filescratch [13];
  char *command_buf;
  int index, s;
  char *archive_dir;
  
  filescratch [12] = '\0';
  
  /* 
     First open up the current and previous sequence numbers.
     The current seq is where hylafax is at right now.
     The previous seq is the last archived sequence number.
  */
  
  fp = fopen (SEQF, "r");
  if (!fp)
    exit (-1);
  fread (buf, 1, 8, fp);
  seq = atoi (buf);
  fclose (fp);
  
  fp = fopen (LAST_SEQF, "r");
  if (!fp)
    exit (-1);
  fread (buf, 1, 8, fp);
  last_seq = atoi (buf);
  fclose (fp);
  
  /* If 100 ahead of the last sequence number, archive.
     Otherwise, quit now */
  if (seq - last_seq < 100)
    exit(-1);
  
  if (last_seq % 100 == 0)
    {
      /* 
	 If the last_seq % 100 is zero, then the last archived
	 fax filled a to the bound.  Then the new directory is
	 last_seq + 100 
      */ 
      
      /* The following code builds the name of the directory to make */
      archive_dir = (char *) malloc (strlen (ARCHIVE_DIR) + 15);
      sprintf (archive_dir, "%s/%d", ARCHIVE_DIR, last_seq+100);
      
      /* Make it */
      command_buf = (char *) malloc (strlen (archive_dir) + 7);
      sprintf (command_buf, "mkdir %s", archive_dir);
      system (command_buf);
      free (command_buf);
    }
  else
    {
      printf ("Big problem: The last archived sequence is not = 0 (mod 100)!\n");
      exit (-1);
    }
  
  for (index = 0; index < 100; index ++)
    {
      char temp [11];
      s = last_seq + index;
      
      /* There is no spoon^Wfax 0 */
      if (s == 0)
	continue;
      
      /* Build up the command to move the file */
      command_buf = (char *) malloc (strlen (archive_dir) + 
				     strlen (RECVQ_DIR) + 20);
      sprintf (command_buf, "mv ");
      strcat (command_buf, RECVQ_DIR);
      sprintf (temp, "/fax");
      strcat (command_buf, temp);
      if (s < 10)
	strcat (command_buf, "0000");
      else if (s < 100)
	strcat (command_buf, "000");
      else if (s < 1000)
	strcat (command_buf, "00");
      else if (s < 10000)
	strcat (command_buf, "0");
      
      sprintf (temp, "%d.tif ", s);
      strcat (command_buf, temp);
      strcat (command_buf, archive_dir);
      
      printf ("%s\n", command_buf);
      /* Run it */ 
	 system (command_buf);
    }

  fp = fopen (LAST_SEQF, "w+");
  if (!fp)
    exit (-1); /*shit*/
  sprintf (buf, "%d\n", last_seq += 100);
  fwrite (buf, 1, strlen (buf), fp);
  fclose (fp);
  return 0;
}



Project hosted by iFAX Solutions