Mercurial > xfs_email_hsm
view split.c @ 0:cd351af3a8d2 migout
import source
author | Josef 'Jeff' Sipek <jeffpc@josefsipek.net> |
---|---|
date | Thu, 27 Mar 2008 20:03:52 -0400 |
parents | |
children | 8d787ee29c64 |
line wrap: on
line source
#include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include "settings.h" #include "base85.h" #include "split.h" #include "die.h" #define INFILE "infile" #define CHUNKSIZE (SMTP_MAX_FILESIZE * 4 / 5) int do_dm_read_invis(dm_sessid_t sid, u8 *handle, size_t hlen, char *buf, int len, dm_off_t start) { int ret; int off = 0; do { ret = dm_read_invis(sid, handle, hlen, DM_NO_TOKEN, start + off, len - off, buf + off); if (ret > 0) off += ret; if (ret <= 0) break; } while(off < len); return (ret < 0 ? ret : off); } void dump_buffer(FILE *sendmail, char *buf, char *obuf, int len) { int i; len = base85_encode((u8*)buf, (u8*)obuf, len); for(i = 0; i < len; ) { fprintf(sendmail, "%c", obuf[i++]); if (i % 76 == 0) fprintf(sendmail, "\n"); } fprintf(sendmail, "\n\n"); } static inline char bin2hex(u8 u) { if (u >= 10) return u - 10 + 'a'; return u + '0'; } static void printable_handle(u8 *handle, size_t hlen, char *ascii) { size_t i; for(i=0; i<hlen; i++) { ascii[i*2] = bin2hex(handle[i] >> 4); ascii[i*2+1] = bin2hex(handle[i] & 0xf); } ascii[i*2] = '\0'; } int save_filedata(dm_sessid_t sid, u8 *handle, size_t hlen, dm_size_t fsize) { FILE *sendmail; int mbox; char *buf, *obuf; char asciihandle[HANDLE_MAX_SIZE]; int len; buf = malloc(CHUNKSIZE); obuf = malloc(CHUNKSIZE * 5 / 4); if (!buf || !obuf) { warn("could not allocate buffers"); return 1; } printable_handle(handle, hlen, asciihandle); for(mbox=0; ; mbox++) { /* * Read first to break out of the loop in case we're done */ len = do_dm_read_invis(sid, handle, hlen, buf, CHUNKSIZE, CHUNKSIZE * mbox); if (len <= 0) break; sendmail = popen(SENDMAIL_PATH " -t \"" SMTP_USER "\"", "w"); if (!sendmail) { warn("could not spawn sendmail"); return 1; } fprintf(sendmail, "From " FROM_ADDRESS " / %s\n", asciihandle); fprintf(sendmail, "From: " FROM_ADDRESS "\n"); fprintf(sendmail, "To: " SMTP_USER "\n"); fprintf(sendmail, "Subject: %s, part %d\n", asciihandle, mbox); fprintf(sendmail, "X-DM-Handle: %s\n", asciihandle); fprintf(sendmail, "X-DM-Part: %d\n", mbox); fprintf(sendmail, "X-DM-Part-Length: %d\n", len); fprintf(sendmail, "\n"); dump_buffer(sendmail, buf, obuf, len); fclose(sendmail); } return 0; }