procmail
[Top] [All Lists]

Re: how to deliver to a file with a unique time-stamped file name, reliably?

2005-01-19 01:30:54
On Tue, 18 Jan 2005, 16:36 GMT-08 Gary Funck wrote:

Not bad, but how do we make sure that the file named by $MYDATE
isn't overwritten by another process executing this script within
the same second in time?

Hi,

in my SoftlabsAV package, I am creating an unique ID number for each
mail, including the mail's processing date and time with an accurancy
of at least to the second. That ID will be in the format
"YYYYMMDD-hhmmss.xxxxxx" where the x's are either 6-digits
microseconds (if supported by the OS), or 5-digits Process-ID:

 av_ID
 :0
  {
     # Creating an unique mail ID number, based on the current time. For 
     # this, we call 'date' (only once in the entire recipe, so this 
     #   should not cause a significant performance decrease). 
     #   We want the ID to be in the format "YYYYMMDD-hhmmss.xxxxxx"
     av_ID = `date +"%Y%m%d-%H%M%S.%N"`
     # eg. "20041203-163017.405974000" or "20041203-163017.N"
     # If %N (nanoseconds) has been supported, strip its last 3 digits 
     #    (probably only zeros); otherwise use the 5-digits PID

     :0
      * av_ID ?? ^^\/........-......\.......
      {
         av_ID = $MATCH
         # eg. "20041203-163017.405974"
         #                      ^^^^^^
         #                      6-digits microseconds
      }

     :0 E
      {
         MATCH = $$
         # procmail's current Process ID; example:
         # Assigning "MATCH=3667"
         # We want the PID to be always 5 digits long - 
         #   with leeding zeros if needed
         INCLUDERC = "$av_INSTALLDIR/inc/av_length.inc"
         # Assigning "av_LENGTH=4"

         av_MULTIPLIER
         :0
          *             5 ^0
          * $ -$av_LENGTH ^0
          {
             av_MULTIPLIER = $=
             # Assigning "av_MULTIPLIER=1"
          }
         
         av_CAT_MULTIPLIED
         :0
          * av_MULTIPLIER ?? .
          {
             av_CAT = 0
             INCLUDERC = "$av_INSTALLDIR/inc/av_cat.inc"
             # Assigning "av_CAT_MULTIPLIED=0"
             av_CAT av_MULTIPLIER
          }

         :0
          * av_ID ?? ^^\/........-......\.
          {
             av_ID = "$MATCH$av_CAT_MULTIPLIED$$"
             # Assigning "av_ID=20041203-163017.03667"
             #                                  ^^^^^
             #                                  5-digits PID
             av_CAT_MULTIPLIED
          }
      }

rob.


____________________________________________________________
procmail mailing list   Procmail homepage: http://www.procmail.org/
procmail(_at_)lists(_dot_)RWTH-Aachen(_dot_)DE
http://MailMan.RWTH-Aachen.DE/mailman/listinfo/procmail