Ticket #1636: 0003-Removable-disk-Handle-renames-dlo-9657.patch

File 0003-Removable-disk-Handle-renames-dlo-9657.patch, 2.8 KB (added by martin.langhoff, 14 years ago)
  • src/jarabe/journal/model.py

    From 3775ae20d30daa1cb5f3acbdd8001e1371ca91ad Mon Sep 17 00:00:00 2001
    From: Martin Langhoff <martin@laptop.org>
    Date: Fri, 4 Dec 2009 22:51:36 +0000
    Subject: [PATCH 3/4] Removable disk: Handle renames dlo#9657
    
    Now renames are handled correctly as a special case for write().
    ---
     src/jarabe/journal/model.py |   32 +++++++++++++++++++++++++++++++-
     1 files changed, 31 insertions(+), 1 deletions(-)
    
    diff --git a/src/jarabe/journal/model.py b/src/jarabe/journal/model.py
    index 0553a44..00b535e 100644
    a b def write(metadata, file_path='', update_mtime=True): 
    526526    """
    527527    logging.debug('model.write %r %r %r' % (metadata.get('uid', ''), file_path,
    528528                                            update_mtime))
     529
     530    rename = False
     531
    529532    if update_mtime:
    530533        metadata['mtime'] = datetime.now().isoformat()
    531534        metadata['timestamp'] = int(time.time())
    def write(metadata, file_path='', update_mtime=True): 
    541544                                                 file_path,
    542545                                                 True)
    543546    else:
     547        if os.path.exists(metadata['uid']):
     548            file_path = metadata['uid']
     549       
    544550        if not os.path.exists(file_path):
    545551            raise ValueError('Entries without a file cannot be copied to '
    546552                             'removable devices')
    def write(metadata, file_path='', update_mtime=True): 
    549555        file_name = _get_unique_file_name(metadata['mountpoint'], file_name)
    550556
    551557        destination_path = os.path.join(metadata['mountpoint'], file_name)
    552         shutil.copy(file_path, destination_path)
     558
     559        if os.path.dirname(destination_path) == os.path.dirname(file_path):
     560            ## FIXME: handle renames in a separate codepath
     561            ##        in a failsafe manner
     562            ##        (write new md, then rename and rm old)
     563            # rename on a removable disk - mv file, but let
     564            # the metadata&preview be written - at least md will
     565            # be different
     566            rename = True
     567            old_file_path = file_path
     568            os.rename(file_path, destination_path)
     569            old_fname = os.path.basename(file_path)
     570            old_files = [ os.path.join(metadata['mountpoint'],
     571                                       '.'+old_fname+'.metadata'),
     572                          os.path.join(metadata['mountpoint'],
     573                                       '.'+old_fname+'.preview') ]
     574            for ofile in old_files:
     575                if os.path.exists(ofile):
     576                    try:
     577                        os.unlink(ofile)
     578                    except:
     579                        pass
     580
     581        else:
     582            shutil.copy(file_path, destination_path)
    553583
    554584        # issue our metadata massage on a copy
    555585        md = metadata.copy()
    556 -