Example #1
0
 def initialize(self, stream, session):
     super(IncomingFileTransferHandler, self).initialize(stream, session)
     try:
         directory = self.save_directory or SIPSimpleSettings().file_transfer.directory.normalized
         makedirs(directory)
         with self.metadata as metadata:
             try:
                 prev_file = metadata.pop(stream.file_selector.hash.lower())
                 mtime = os.path.getmtime(prev_file.filename)
                 if mtime != prev_file.mtime:
                     raise ValueError('file was modified')
                 filename = os.path.join(directory, os.path.basename(stream.file_selector.name))
                 try:
                     os.link(prev_file.filename, filename)
                 except (AttributeError, OSError):
                     stream.file_selector.name = prev_file.filename
                 else:
                     stream.file_selector.name = filename
                     unlink(prev_file.filename)
                 stream.file_selector.fd = openfile(stream.file_selector.name, 'ab')  # open doesn't seek to END in append mode on win32 until first write, but openfile does
                 self.offset = stream.file_selector.fd.tell()
                 self.hash = prev_file.partial_hash
             except (KeyError, EnvironmentError, ValueError):
                 for name in UniqueFilenameGenerator.generate(os.path.join(directory, os.path.basename(stream.file_selector.name))):
                     try:
                         stream.file_selector.fd = openfile(name, 'xb')
                     except FileExistsError:
                         continue
                     else:
                         stream.file_selector.name = name
                         break
     except Exception as e:
         NotificationCenter().post_notification('FileTransferHandlerDidNotInitialize', sender=self, data=NotificationData(reason=str(e)))
     else:
         NotificationCenter().post_notification('FileTransferHandlerDidInitialize', sender=self)
 def initialize(self, stream, session):
     super(IncomingFileTransferHandler, self).initialize(stream, session)
     try:
         directory = self.save_directory or SIPSimpleSettings().file_transfer.directory.normalized
         makedirs(directory)
         with self.metadata as metadata:
             try:
                 prev_file = metadata.pop(stream.file_selector.hash.lower())
                 mtime = os.path.getmtime(prev_file.filename)
                 if mtime != prev_file.mtime:
                     raise ValueError('file was modified')
                 filename = os.path.join(directory, os.path.basename(stream.file_selector.name))
                 try:
                     os.link(prev_file.filename, filename)
                 except (AttributeError, OSError):
                     stream.file_selector.name = prev_file.filename
                 else:
                     stream.file_selector.name = filename
                     unlink(prev_file.filename)
                 stream.file_selector.fd = openfile(stream.file_selector.name, 'ab')  # open doesn't seek to END in append mode on win32 until first write, but openfile does
                 self.offset = stream.file_selector.fd.tell()
                 self.hash = prev_file.partial_hash
             except (KeyError, EnvironmentError, ValueError):
                 for name in UniqueFilenameGenerator.generate(os.path.join(directory, os.path.basename(stream.file_selector.name))):
                     try:
                         stream.file_selector.fd = openfile(name, 'xb')
                     except FileExistsError:
                         continue
                     else:
                         stream.file_selector.name = name
                         break
     except Exception, e:
         NotificationCenter().post_notification('FileTransferHandlerDidNotInitialize', sender=self, data=NotificationData(reason=str(e)))
Example #3
0
 def save(self, data):
     """
     Given a dictionary conforming to the IConfigurationBackend
     specification, write the data to the file configured with this backend
     in a format suitable to be read back using load().
     """
     lines = self._build_group(data, 0)
     config_directory = os.path.dirname(self.filename)
     tmp_filename = '%s.%d.%08X' % (self.filename, os.getpid(),
                                    random.getrandbits(32))
     try:
         if config_directory:
             makedirs(config_directory)
         file = openfile(tmp_filename, 'wb', permissions=0o600)
         file.write(
             (os.linesep.join(lines) + os.linesep).encode(self.encoding))
         file.close()
         if platform.system() == 'Windows':
             # os.rename does not work on Windows if the destination file already exists.
             # It seems there is no atomic way to do this on Windows.
             unlink(self.filename)
         os.rename(tmp_filename, self.filename)
     except (IOError, OSError) as e:
         raise ConfigurationBackendError(
             "failed to write configuration file: %s" % str(e))
Example #4
0
 def save(self, name, data):
     """Write the data in a file identified by name."""
     filename = os.path.join(self.directory, self.account_id, name)
     tmp_filename = '%s.%d.%08X' % (filename, os.getpid(), random.getrandbits(32))
     try:
         makedirs(os.path.join(self.directory, self.account_id))
         file = openfile(tmp_filename, 'wb', permissions=0600)
         file.write(data)
         file.close()
         if platform.system() == 'Windows':
             # os.rename does not work on Windows if the destination file already exists.
             # It seems there is no atomic way to do this on Windows.
             unlink(filename)
         os.rename(tmp_filename, filename)
     except (IOError, OSError), e:
         raise XCAPStorageError("failed to save XCAP data for %s/%s: %s" % (self.account_id, name, str(e)))
Example #5
0
 def save(self):
     if self.trusted_file is not None:
         with openfile(self.trusted_file, 'wb',
                       permissions=0o600) as trusted_file:
             pickle.dump(self.trusted_peers, trusted_file)
Example #6
0
 def save(self, path):
     content = self._key.private_bytes(serialization.Encoding.PEM, serialization.PrivateFormat.PKCS8, serialization.NoEncryption())
     with openfile(path, 'wb', permissions=0600) as key_file:
         key_file.write(content)
Example #7
0
 def load(cls, path):
     with openfile(path, 'rb') as key_file:
         key = serialization.load_pem_private_key(key_file.read(), password=None, backend=cls.__backend__)
     return PrivateKey.new(key) if cls.__type__ is None else cls(key)
Example #8
0
 def save(self):
     if self.trusted_file is not None:
         with openfile(self.trusted_file, 'wb', permissions=0600) as trusted_file:
             pickle.dump(self.trusted_peers, trusted_file)
Example #9
0
 def save(self, path):
     content = self._key.private_bytes(serialization.Encoding.PEM, serialization.PrivateFormat.PKCS8, serialization.NoEncryption())
     with openfile(path, 'wb', permissions=0600) as key_file:
         key_file.write(content)
Example #10
0
 def load(cls, path):
     with openfile(path, 'rb') as key_file:
         key = serialization.load_pem_private_key(key_file.read(), password=None, backend=cls.__backend__)
     return PrivateKey.new(key) if cls.__type__ is None else cls(key)