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

[minion-cvs] Improve documentation; refactor random filename generat...



Update of /home/minion/cvsroot/src/minion/lib/mixminion
In directory moria.mit.edu:/tmp/cvs-serv20294/lib/mixminion

Modified Files:
	Crypto.py 
Log Message:
Improve documentation; refactor random filename generation into Crypto.RNG.

Index: Crypto.py
===================================================================
RCS file: /home/minion/cvsroot/src/minion/lib/mixminion/Crypto.py,v
retrieving revision 1.37
retrieving revision 1.38
diff -u -d -r1.37 -r1.38
--- Crypto.py	9 Jan 2003 06:28:58 -0000	1.37
+++ Crypto.py	4 Feb 2003 02:08:37 -0000	1.38
@@ -8,8 +8,10 @@
    the functions in mixminion.Crypto, and not call _minionlib's crypto
    functionality themselves."""
 
+import base64
 import binascii
 import copy_reg
+import errno
 import os
 import stat
 import sys
@@ -562,6 +564,34 @@
         #return o / float(0x7fffffff)
         return o / 2147483647.0
 
+    def openNewFile(self, dir, prefix="", binary=1):
+        """Generate a new random filename within a directory with a given
+           prefix within a directory, and open a new file within the directory
+           with that filename.  Return 2-tuple of a file object and the
+           random portion of the filename.
+
+           Random portions are generated by choosing 8 random characters
+           from the set 'A-Za-z0-9+-'.
+           """
+        flags = os.O_WRONLY|os.O_CREAT|os.O_EXCL
+        mode = "w"
+        if binary:
+            flags |= getattr(os, 'O_BINARY', 0)
+            mode = "wb"
+        while 1:
+            bytes = self.getBytes(6)
+            base = base64.encodestring(bytes).strip().replace("/","-")
+            fname = os.path.join(dir, "%s%s"%(prefix,base))
+            try:
+                fd = os.open(fname, flags, 0600)
+                return os.fdopen(fd, mode), base
+            except OSError, e:
+                if e.errno != errno.EEXIST:
+                    raise
+                # If the file exists (a rare event!) we pass through, and
+                # try again.  This paranoia is brought to you by user
+                # request. :)
+
     def _prng(self, n):
         """Abstract method: Must be overridden to return n bytes of fresh
            entropy."""
@@ -689,7 +719,6 @@
         b = RNG.getBytes(self, n)
         self.__lock.release()
         return b
-        
 
 # Global _TrueRNG instance, for use by trng().
 _theTrueRNG = _TrueRNG(1024)