Ejemplo n.º 1
0
def erase_file( file_path, volume=GET_FROM_SESSION ):
   # unlike delete_file, overwrite the file data with random bits a few times if local 
   if volume == GET_FROM_SESSION:
      volume = singleton.get_volume()
   
   if volume is None:
      try:
         size = os.stat(file_path).st_size
         fd = open(file_path, "w")
         
         for i in xrange(0,10):
            fd.seek(0)
            
            # overwrite with junk
            buf = ''.join(chr(random.randint(0,255)) for i in xrange(0,size))
            fd.write( buf )
            fd.flush()

         fd.close()
         
         # now safe to unlink
         os.unlink( file_path )
         return True 
      except Exception, e:
         log.exception(e)
         return False
Ejemplo n.º 2
0
def write_encrypted_file( receiver_pubkey_pem, file_path, data, volume=GET_FROM_SESSION, sender_privkey_pem=None ):
   global MY_PRIVKEY_PEM
   if sender_privkey_pem is None:
      sender_privkey_pem = MY_PRIVKEY_PEM

   if sender_privkey_pem is None:
      raise Exception("No storage access key set.")

   if volume == GET_FROM_SESSION:
      volume = singleton.get_volume()
     
   print "write (%s) %s" % (str(volume), file_path)

   enc_data = encrypt_data( sender_privkey_pem, receiver_pubkey_pem, data )
   if enc_data is None:
      log.error("encrypt_data returned None")
      return False
   
   try:
      rc = write_file( file_path, enc_data, volume=volume )
      if not rc:
         log.error("write_file failed")
         return False
      
      return True
   
   except Exception, e:
      log.error("write_file(%s) failed" % file_path)
      log.exception(e)
      return False
Ejemplo n.º 3
0
def write_file( file_path, data, volume=GET_FROM_SESSION, create_mode=0600 ):
   if volume == GET_FROM_SESSION:
      volume = singleton.get_volume()
      
   if volume is None:
      try:
         fd = open( file_path, "w+" )
      except Exception, e:
         log.exception(e)
         log.error("Failed to open '%s' for writing" % file_path)
         return False
      
      try:
         fd.write(data)
         fd.flush()
      except Exception, e:
         log.exception(e)
         log.error("Failed to write '%s'" % file_path)
         try:
            os.unlink(file_path)
         except:
            pass 
         
         try:
            fd.close()
         except:
            pass
         
         return False
Ejemplo n.º 4
0
def read_encrypted_file( receiver_privkey_pem, file_path, volume=GET_FROM_SESSION, sender_pubkey_pem=None ):
   global MY_PUBKEY_PEM

   use_my_key = False
   if sender_pubkey_pem is None:
      use_my_key = True
      sender_pubkey_pem = MY_PUBKEY_PEM

   if sender_pubkey_pem is None:
      raise Exception("No storage access key set.")

   print "read (%s) %s, use_my_key = %s" % (str(volume), file_path, use_my_key)

   if volume == GET_FROM_SESSION:
      volume = singleton.get_volume()
      
   # get file data
   try:
      enc_data = read_file( file_path, volume=volume )
      if enc_data == None:
         log.error( "No data for %s" % file_path )
         return None
      
   except Exception, e:
      log.error("read_file(%s) failed" % file_path)
      log.exception(e)
      return None
Ejemplo n.º 5
0
def delete_dirs( dirs, prefix="/", remove_contents=True, volume=GET_FROM_SESSION ):
   if volume == GET_FROM_SESSION:
      volume = singleton.get_volume()
      
   for dirname in dirs:
      if volume is None:
         dir_path = local_path( prefix, dirname )
         if remove_contents:
            try:
               shutil.rmtree( dir_path )
            except:
               return False
         
         else:
            try:
               os.rmdir( dir_path )
            except:
               return False
            
      else:
         dir_path = volume_path( prefix, dirname )
         if remove_contents:
            try:
               volume_rmtree( volume, dir_path )
            except Exception, e:
               log.exception(e)
               return False
         
         else:
            try:
               volume.rmdir( dir_path )
            except Exception, e:
               log.exception(e)
               return False
Ejemplo n.º 6
0
def setup_dirs( dir_names, prefix="/", volume=GET_FROM_SESSION ):
   if volume == GET_FROM_SESSION:
      volume = singleton.get_volume()
      
   if prefix is None:
      raise Exception("Invalid argument: paseed None as prefix")
   
   for dirname in dir_names:
      if dirname is None:
         raise Exception("Invalid argument: passed None as a directory")
      try:
         dir_path = None
         
         if volume is None:
            dir_path = local_path(prefix, dirname)
            os.makedirs( dir_path, mode=0700 )
            
         else:
            dir_path = volume_path(prefix, dirname)
            volume_makedirs( volume, dir_path, mode=0700 )
            
      except OSError, oe:
         if oe.errno != errno.EEXIST:
            log.error("Failed to mkdir '%s'" % dir_path )
            log.exception(oe)
            return False
         else:
            pass
         
      except Exception, e:
         log.error("Failed to mkdir '%s'" % dirname )
         log.exception(e)
         return False
Ejemplo n.º 7
0
def listdir( path, volume=GET_FROM_SESSION ):
   if volume == GET_FROM_SESSION:
      volume = singleton.get_volume()
      
   if volume is None:
      return os.listdir(path)
   else:
      return volume_listdir( volume, path)
Ejemplo n.º 8
0
def read_file( file_path, volume=GET_FROM_SESSION ):
   if volume == GET_FROM_SESSION:
      volume = singleton.get_volume()
      
   if volume is None:
      try:
         fd = open( file_path, "r" )
      except:
         log.error("Failed to open '%s' for reading" % file_path)
         return None
      
      try:
         buf = fd.read()
      except:
         log.error("Failed to read '%s'" % file_path)
         try:
            fd.close()
         except:
            pass 
         
         return None
      
      try:
         fd.close()
         return buf
      except:
         log.error("Failed to close '%s'" % file_path)
         return None
      
   else:
      try:
         statbuf = volume.stat( file_path )
         if isinstance(statbuf, int) and statbuf < 0:
            raise Exception("volume.stat rc = %d", statbuf)
         
         size = statbuf.st_size

         fd = volume.open( file_path, os.O_RDONLY )
         if fd is None:
            raise Exception("Failed to open Volume file %s" % file_path)
         
         buf = volume.read( fd, size )
         volume.close( fd )
         return buf 
      
      except Exception, e:
         log.exception(e)
         log.error("Failed to read Volume file %s" % file_path)
         return None
Ejemplo n.º 9
0
def delete_file( file_path, volume=GET_FROM_SESSION ):
   if volume == GET_FROM_SESSION:
      volume = singleton.get_volume()
      
   if volume is None:
      try:
         os.unlink( file_path )
      except OSError, oe:
         if oe.errno != errno.EEXIST:
            return False
         else:
            log.exception(oe)
            return True
      
      except Exception, e:
         log.exception(e)
         return False
Ejemplo n.º 10
0
def path_exists( file_path, volume=GET_FROM_SESSION ):
   if volume == GET_FROM_SESSION:
      volume = singleton.get_volume()
      
   if volume is None:
      return os.path.exists( file_path )
   
   else:
      try:
         statbuf = volume.stat( file_path )
         if statbuf is None:
            return False
         elif isinstance(statbuf, int) and statbuf < 0:
            return False
         else:
            return True
      except Exception, e:
         return False
Ejemplo n.º 11
0
def setup_storage( my_pkey_pem, volume_root_dir, local_dir, modules, volume=GET_FROM_SESSION ):
   global MY_PUBKEY_PEM, MY_PRIVKEY_PEM

   if volume == GET_FROM_SESSION:
      volume = singleton.get_volume()
      
   rc = setup_local_storage( local_dir, modules )
   if not rc:
      log.error("Failed to set up local storage")
      return False
   
   rc = setup_volume_storage( volume_root_dir, modules, volume )
   if not rc:
      log.error("Failed to set up volume storage")
      return False
      
   rc = setup_storage_access( my_pkey_pem )
   if not rc:
      log.error("Failed to set up storage access")
      return False 

   return True