Ticket #1550: sugar-datastore-bug-1550-mkstemp-only.patch

File sugar-datastore-bug-1550-mkstemp-only.patch, 1.7 KB (added by sascha_silbe, 14 years ago)

Fix file descriptor leak in filestore.retrieve() and use only mkstemp()

  • src/carquinyol/filestore.py

    From: Sascha Silbe <sascha@silbe.org>
    Subject: [PATCH] fix file descriptor leak in filestore.retrieve() and use only mkstemp()
    
    Fix file descriptor leak in filestore.retrieve() and use only mkstemp().
    
    Signed-off-by: Sascha Silbe <sascha@silbe.org>
    
    ---
     src/carquinyol/filestore.py |   19 ++++---------------
     1 files changed, 4 insertions(+), 15 deletions(-)
    
    diff --git a/src/carquinyol/filestore.py b/src/carquinyol/filestore.py
    index 5a90a8e..1e2949b 100644
    a b class FileStore(object): 
    110110        elif extension:
    111111            extension = '.' + extension
    112112
    113         destination_path = os.path.join(destination_dir, uid + extension)
    114 
    115         attempt = 1
    116         while os.path.exists(destination_path):
    117             if attempt > 10:
    118                 fd_, destination_path = tempfile.mkstemp(prefix=uid,
    119                                                          suffix=extension,
    120                                                          dir=destination_dir)
    121                 del fd_
    122                 os.unlink(destination_path)
    123                 break
    124             else:
    125                 file_name = '%s_%s%s' % (uid, attempt, extension)
    126                 destination_path = os.path.join(destination_dir, file_name)
    127                 attempt += 1
     113        fd, destination_path = tempfile.mkstemp(prefix=uid, suffix=extension,
     114            dir=destination_dir)
     115        os.close(fd)
     116        os.unlink(destination_path)
    128117
    129118        # Try to hard link from the original file to the targetpath. This can
    130119        # fail if the file is in a different filesystem. Do a symlink instead.