diff -ruN ./safecat-1.11/Makefile ./safecat-1.11.patch/Makefile --- ./safecat-1.11/Makefile Tue Jun 3 23:42:39 2003 +++ ./safecat-1.11.patch/Makefile Tue Aug 26 15:13:41 2003 @@ -102,6 +102,10 @@ compile fmt_xlong.c fmt.h ./compile fmt_xlong.c +fmt_xlong.o: \ +compile fmt_xlong.c fmt.h + ./compile fmt_xlong.c + hier.o: \ compile hier.c auto_home.h ./compile hier.c @@ -223,15 +227,16 @@ safecat: \ load safecat.o getln.a str.a stralloc.a strerr.a substdio.a alloc.o \ -alloc_re.o byte_copy.o byte_cr.o envread.o error.o error_str.o fmt_uint64.o \ -hostname.o sig.o stat_dir.o str_diffn.o str_len.o substdio_copy.o \ -substdi.o substdio.o taia_fmtfrac.o taia_now.o taia_tai.o tempfile.o \ -writefile.o +alloc_re.o byte_copy.o byte_cr.o envread.o error.o error_str.o \ +fmt_uint64.o fmt_xlong.o hostname.o sig.o stat_dir.o str_diffn.o \ +str_len.o substdio_copy.o substdi.o substdio.o taia_fmtfrac.o \ +taia_now.o taia_tai.o tempfile.o writefile.o ./load safecat getln.a str.a stralloc.a strerr.a substdio.a \ alloc.o alloc_re.o byte_copy.o byte_cr.o envread.o error.o \ - error_str.o fmt_uint64.o hostname.o sig.o stat_dir.o str_diffn.o \ - str_len.o substdi.o substdio.o substdio_copy.o taia_fmtfrac.o \ - taia_now.o taia_tai.o tempfile.o writefile.o + error_str.o fmt_uint64.o fmt_xlong.o hostname.o sig.o \ + stat_dir.o str_diffn.o str_len.o substdi.o substdio.o \ + substdio_copy.o taia_fmtfrac.o taia_now.o taia_tai.o \ + tempfile.o writefile.o safecat.0: \ safecat.1 @@ -238,8 +243,8 @@ nroff -man safecat.1 > safecat.0 safecat.o: \ -compile safecat.c uint32.h version.h sig.h tempfile.h stat_dir.h \ -writefile.h +compile safecat.c uint32.h uint64.h fmt.h fmt_xlong.o version.h sig.h \ +tempfile.h stat_dir.h writefile.h ./compile safecat.c sig.o: \ diff -ruN ./safecat-1.11/TARGETS ./safecat-1.11.patch/TARGETS --- ./safecat-1.11/TARGETS Tue Jun 3 23:42:39 2003 +++ ./safecat-1.11.patch/TARGETS Tue Aug 26 15:13:41 2003 @@ -17,6 +17,7 @@ error_str.o find-systype fmt_uint64.o +fmt_xlong.o getln.a getln.o getln2.o diff -ruN ./safecat-1.11/fmt_xlong.c ./safecat-1.11.patch/fmt_xlong.c --- ./safecat-1.11/fmt_xlong.c Wed Dec 31 19:00:00 1969 +++ ./safecat-1.11.patch/fmt_xlong.c Tue Aug 26 15:13:41 2003 @@ -0,0 +1,13 @@ +/* writes ulong u in hex to char *s, does not NULL-terminate */ +unsigned int fmt_xlong(s,u) char *s; unsigned long u; +{ + unsigned int len; unsigned long q; unsigned long c; + len = 1; q = u; + while (q > 15) { ++len; q /= 16; } + if (s) + { + s += len; + do { c = u & 15; *--s = (c > 9 ? 'a' - 10 : '0') + c; u /= 16; } while(u); + } + return len; +} diff -ruN ./safecat-1.11/maildirfn.h ./safecat-1.11.patch/maildirfn.h --- ./safecat-1.11/maildirfn.h Wed Dec 31 19:00:00 1969 +++ ./safecat-1.11.patch/maildirfn.h Tue Aug 26 15:13:41 2003 @@ -0,0 +1,17 @@ +#ifndef MAILDIRFN_H +#define MAILDIRFN_H + +#include "fmt.h" +#include "tai.h" +#include "taia.h" + +typedef struct maildirfn { + char secbuf[11]; + char inode[FMT_ULONG]; + char device[FMT_ULONG]; + char atto[TAIA_FMTFRAC]; + char pidbuf[6]; + char host[256]; +} maildirfn; + +#endif diff -ruN ./safecat-1.11/safecat.c ./safecat-1.11.patch/safecat.c --- ./safecat-1.11/safecat.c Tue Jun 3 23:42:39 2003 +++ ./safecat-1.11.patch/safecat.c Tue Aug 26 15:20:30 2003 @@ -10,6 +10,8 @@ */ #include "alloc.h" +#include "fmt.h" +#include "maildirfn.h" #include "sig.h" #include "stat_dir.h" #include "stralloc.h" @@ -29,16 +31,18 @@ static void die_nomem() { strerr_die2x(111,"safecat: fatal: ","out of memory"); } stralloc tmppath = {0}; +stralloc tmpfile = {0}; + +stralloc dstpath = {0}; +stralloc dstfile = {0}; /* ****************************************************************** */ int main(int argc, char *argv[]) { char *tempdir = NULL; char *destdir = NULL; int outfd = 0; - stralloc dstpath = {0}; - stralloc outfile = {0}; - stralloc outpath = {0}; struct stat filestat; + struct maildirfn mdfn; unsigned int count = 0; /* Check that we were called with the correct number of arguments. */ @@ -61,18 +65,26 @@ /* Step 2: Stat the temporary file. Wait for ENOENT as a response. */ for(count=1;;count++) { /* Get the temporary filename to use now for dumping data. */ - mk_tempfile(&outfile); - if (!stralloc_cats(&outpath,tempdir)) die_nomem(); - if (!stralloc_append(&outpath, "/")) die_nomem(); - if (!stralloc_cat(&outpath,&outfile)) die_nomem(); - if(stat(outpath.s,&filestat) == -1 && errno == ENOENT) { - if (!stralloc_cats(&dstpath, destdir)) die_nomem(); - if (!stralloc_append(&dstpath, "/")) die_nomem(); - if (!stralloc_cat(&dstpath,&outfile)) die_nomem(); + mk_tempfile(&mdfn); + +/* generate the tempdir filename: sec.MattoPpid.host */ + if (!stralloc_copys(&tmpfile, mdfn.secbuf)) die_nomem(); + if (!stralloc_cats(&tmpfile, ".M")) die_nomem(); + if (!stralloc_cats(&tmpfile, mdfn.atto)) die_nomem(); + if (!stralloc_cats(&tmpfile, "P")) die_nomem(); + if (!stralloc_cats(&tmpfile, mdfn.pidbuf)) die_nomem(); + if (!stralloc_cats(&tmpfile, ".")) die_nomem(); + if (!stralloc_cats(&tmpfile, mdfn.host)) die_nomem(); + if (!stralloc_0(&tmpfile)) die_nomem(); + + if (!stralloc_copys(&tmppath,tempdir)) die_nomem(); + if (!stralloc_append(&tmppath, "/")) die_nomem(); + if (!stralloc_cat(&tmppath,&tmpfile)) die_nomem(); + if(stat(tmppath.s,&filestat) == -1 && errno == ENOENT) { - if (!stralloc_cats(&tmppath, tempdir)) die_nomem(); + if (!stralloc_copys(&tmppath, tempdir)) die_nomem(); if (!stralloc_append(&tmppath, "/")) die_nomem(); - if (!stralloc_cat(&tmppath,&outfile)) die_nomem(); + if (!stralloc_cat(&tmppath,&tmpfile)) die_nomem(); break; } @@ -82,8 +94,8 @@ } /* Wait 2 seconds, and try again. */ - stralloc_copys(&outfile,""); - stralloc_copys(&outpath,""); + stralloc_copys(&tmpfile,""); + stralloc_copys(&dstfile,""); sleep(2); } @@ -104,6 +116,27 @@ } /* Step 6: Link the temp file to its final destination. */ +/* generate the destdir filename: sec.IinodeVdeviceMattoPpid.host */ + stat(tmppath.s, &filestat); + if (!stralloc_copys(&dstfile, mdfn.secbuf)) die_nomem(); + if (!stralloc_cats(&dstfile, ".I")) die_nomem(); + mdfn.inode[fmt_xlong(mdfn.inode,filestat.st_ino)] = '\0'; + if (!stralloc_cats(&dstfile, mdfn.inode)) die_nomem(); + if (!stralloc_cats(&dstfile, "V")) die_nomem(); + mdfn.device[fmt_xlong(mdfn.device,filestat.st_dev)] = '\0'; + if (!stralloc_cats(&dstfile, mdfn.device)) die_nomem(); + if (!stralloc_cats(&dstfile, "M")) die_nomem(); + if (!stralloc_cats(&dstfile, mdfn.atto)) die_nomem(); + if (!stralloc_cats(&dstfile, "P")) die_nomem(); + if (!stralloc_cats(&dstfile, mdfn.pidbuf)) die_nomem(); + if (!stralloc_cats(&dstfile, ".")) die_nomem(); + if (!stralloc_cats(&dstfile, mdfn.host)) die_nomem(); + if (!stralloc_0(&dstfile)) die_nomem(); + + if (!stralloc_copys(&dstpath, destdir)) die_nomem(); + if (!stralloc_append(&dstpath, "/")) die_nomem(); + if (!stralloc_cat(&dstpath,&dstfile)) die_nomem(); + if(link(tmppath.s,dstpath.s) == -1) { unlink(tmppath.s); strerr_die2sys(111,"safecat: fatal: ","can't link output file: "); @@ -113,8 +146,8 @@ /* Okay, delete the temporary file. If it fails, bummer. */ unlink(tmppath.s); - /* Print the name of the file we've created, as a curtesy. */ - substdio_puts(subfdoutsmall,outfile.s); + /* Print the name of the file we've created, as a courtesy. */ + substdio_puts(subfdoutsmall,dstfile.s); substdio_puts(subfdoutsmall,"\n"); substdio_flush(subfdoutsmall); exit(0); diff -ruN ./safecat-1.11/tempfile.c ./safecat-1.11.patch/tempfile.c --- ./safecat-1.11/tempfile.c Tue Jul 1 10:01:56 2003 +++ ./safecat-1.11.patch/tempfile.c Tue Aug 26 15:13:41 2003 @@ -1,6 +1,7 @@ /* Copyright (c) 2000, Len Budney. See COPYING for details. */ #include "hostname.h" +#include "maildirfn.h" #include "stralloc.h" #include "strerr.h" #include "tai.h" @@ -12,11 +13,7 @@ void die_nomem() { strerr_die2x(111,"safecat: fatal: ","out of memory"); } /* ****************************************************************** */ -void mk_tempfile(stralloc *tmpf) { - char host[256]; - char secbuf[11]; - char atto[TAIA_FMTFRAC]; - char pidbuf[6]; +void mk_tempfile(maildirfn *tmpf) { struct taia now; struct tai sec; unsigned long int pid = (unsigned long)getpid(); @@ -26,25 +23,17 @@ taia_tai(&now,&sec); /* Record the second timestamp on the string. */ - secbuf[fmt_uint64(secbuf,(uint64) sec.x - 4611686018427387904)] = '\0'; - if (!stralloc_cats(tmpf, secbuf)) die_nomem(); + tmpf->secbuf[fmt_uint64(tmpf->secbuf,(uint64) sec.x - 4611686018427387904)] = '\0'; /* Append the microsecond timestamp to the string. */ - if (!stralloc_cats(tmpf, ".M")) die_nomem(); - taia_fmtfrac(atto,&now); - atto[6] = '\0'; /* truncate at microsecond */ - if (!stralloc_cats(tmpf, atto)) die_nomem(); + taia_fmtfrac(tmpf->atto,&now); + tmpf->atto[6] = '\0'; /* truncate at microsecond */ /* Append the PID to the string. */ - if (!stralloc_append(tmpf, "P")) die_nomem(); - pidbuf[fmt_uint64(pidbuf, pid)] = '\0'; - if (!stralloc_cats(tmpf, pidbuf)) die_nomem(); + tmpf->pidbuf[fmt_uint64(tmpf->pidbuf, pid)] = '\0'; /* Copy the hostname to the buffer. */ - if (!stralloc_append(tmpf, ".")) die_nomem(); - get_hostname(host,sizeof(host)); - if (!stralloc_cats(tmpf, host)) die_nomem(); - if (!stralloc_0(tmpf)) die_nomem(); + get_hostname(tmpf->host,sizeof(tmpf->host)); } /* ****************************************************************** */ diff -ruN ./safecat-1.11/tempfile.h ./safecat-1.11.patch/tempfile.h --- ./safecat-1.11/tempfile.h Mon Feb 28 23:10:12 2000 +++ ./safecat-1.11.patch/tempfile.h Tue Aug 26 15:13:41 2003 @@ -1,5 +1,4 @@ /* Copyright (c) 2000, Len Budney. See COPYING for details. */ /* Create a temporary file, using a timestamp, PID, and the hostname. */ -#include "stralloc.h" -void mk_tempfile(stralloc *tmpf); +void mk_tempfile(maildirfn *tmpf);