Revision 41a57aab lib/ssconf.py

b/lib/ssconf.py
26 26

  
27 27
"""
28 28

  
29
import os
30
import tempfile
31
import errno
32 29
import socket
33 30

  
34 31
from ganeti import errors
35 32
from ganeti import constants
33
from ganeti import utils
36 34

  
37 35

  
38 36
class SimpleStore:
......
164 162

  
165 163
    """
166 164
    file_name = self.KeyToFilename(key)
167
    dir_name, small_name = os.path.split(file_name)
168
    fd, new_name = tempfile.mkstemp('.new', small_name, dir_name)
169
    # here we need to make sure we remove the temp file, if any error
170
    # leaves it in place
171
    try:
172
      os.chown(new_name, 0, 0)
173
      os.chmod(new_name, 0400)
174
      os.write(fd, "%s\n" % str(value))
175
      os.fsync(fd)
176
      os.rename(new_name, file_name)
177
      self._cache[key] = value
178
    finally:
179
      os.close(fd)
180
      try:
181
        os.unlink(new_name)
182
      except OSError, err:
183
        if err.errno != errno.ENOENT:
184
          raise
165
    utils.WriteFile(file_name, data="%s\n" % str(value),
166
                    uid=0, gid=0, mode=0400)
167
    self._cache[key] = value
185 168

  
186 169
  def GetFileList(self):
187 170
    """Return the lis of all config files.

Also available in: Unified diff