Ticket #184: bind10_cfgmgr_tmpfile.patch

File bind10_cfgmgr_tmpfile.patch, 2.0 KB (added by jelte, 10 years ago)
  • cfgmgr.py

     
    2525import pprint
    2626import os
    2727import copy
     28import tempfile
    2829from isc.cc import data
    2930
    3031class ConfigManagerDataReadError(Exception):
     
    8485        """Writes the current configuration data to a file. If
    8586           output_file_name is not specified, the file used in
    8687           read_from_file is used."""
     88        filename = None
    8789        try:
    88             tmp_filename = self.db_filename + ".tmp"
    89             file = open(tmp_filename, 'w');
     90            file = tempfile.NamedTemporaryFile(mode='w',
     91                                               prefix="b10-config.db.",
     92                                               dir=self.data_path,
     93                                               delete=False)
     94            filename = file.name
    9095            pp = pprint.PrettyPrinter(indent=4)
    9196            s = pp.pformat(self.data)
    9297            file.write(s)
    9398            file.write("\n")
    9499            file.close()
    95100            if output_file_name:
    96                 os.rename(tmp_filename, output_file_name)
     101                os.rename(filename, output_file_name)
    97102            else:
    98                 os.rename(tmp_filename, self.db_filename)
     103                os.rename(filename, self.db_filename)
    99104        except IOError as ioe:
    100105            # TODO: log this (level critical)
    101106            print("[b10-cfgmgr] Unable to write config file; configuration not stored: " + str(ioe))
     107            # TODO: debug option to keep file?
    102108        except OSError as ose:
    103109            # TODO: log this (level critical)
    104110            print("[b10-cfgmgr] Unable to write config file; configuration not stored: " + str(ose))
     111        try:
     112            if filename and os.path.exists(filename):
     113                os.remove(filename)
     114        except OSError:
     115            # Ok if we really can't delete it anymore, leave it
     116            pass
    105117
    106118    def __eq__(self, other):
    107119        """Returns True if the data contained is equal. data_path and