src/hg/instinct/hgBamBam/trashDirMD5.c 1.1
1.1 2010/05/25 20:22:45 jsanborn
initial commit
Index: src/hg/instinct/hgBamBam/trashDirMD5.c
===================================================================
RCS file: src/hg/instinct/hgBamBam/trashDirMD5.c
diff -N src/hg/instinct/hgBamBam/trashDirMD5.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/hg/instinct/hgBamBam/trashDirMD5.c 25 May 2010 20:22:45 -0000 1.1
@@ -0,0 +1,71 @@
+/********************************************************************************/
+/* Copyright 2007-2009 -- The Regents of the University of California */
+/********************************************************************************/
+
+#include "common.h"
+#include "hash.h"
+#include "cart.h"
+#include "portable.h"
+#include "md5.h"
+#include "linefile.h"
+#include "hCommon.h"
+#include "hdb.h"
+#include "hPrint.h"
+#include "htmshell.h"
+#include "hui.h"
+#include "web.h"
+
+static char const rcsid[] = "$Id$";
+
+char *md5HashString(char *msg)
+{
+if (!msg)
+ return NULL;
+
+int i;
+struct md5_context ctx;
+unsigned char md5sum[16];
+
+md5_starts(&ctx);
+md5_update(&ctx, (uint8 *) msg, strlen(msg));
+md5_finish(&ctx, md5sum);
+
+if (!md5sum)
+ return NULL;
+
+struct dyString *dy = newDyString(50);
+for (i = 0; i < 16; i++)
+ dyStringPrintf(dy, "%02x", md5sum[i]);
+
+return dy->string;
+}
+
+void trashDirMD5File(struct tempName *tn, char *dirName, char *suffix,
+ char *strToHash)
+/* obtain a trash file name trash/dirName/<md5sum>.suffix */
+{
+if (!strToHash)
+ return;
+
+static struct hash *dirHash = NULL;
+if (!dirHash)
+ dirHash = newHash(0);
+
+/* already created this directory ? */
+if (!hashLookup(dirHash,dirName))
+ {
+ hashAddInt(dirHash, dirName, 1); /* remember, been here, done that */
+ mkdirTrashDirectory(dirName);
+ }
+/* no need to duplicate the _ at the end of base, makeTempName is going
+ * to add _ to the given base, some CGIs pass "base_"
+ */
+char *hash = md5HashString(strToHash);
+if (!hash)
+ errAbort("Couldn't compute md5 hash for %s", strToHash);
+
+//char filename[128];
+safef(tn->forCgi, sizeof(tn->forCgi), "../trash/%s/%s%s", dirName, hash, suffix);
+safef(tn->forHtml, sizeof(tn->forHtml), "../trash/%s/%s%s", dirName, hash, suffix);
+}
+