• Kirill Smelkov's avatar
    BigFile: .data can be BTreeData or None or (possibly non-empty) str · f54e3fa6
    Kirill Smelkov authored
    Current BigFile code in many places assumes .data property is either
    None or BTreeData instance. But as already shown in 4d8f0c33 (BigFile:
    Fix non-range download of just created big file) this is not true and
    .data can be an str.
    
    This leads to situations where code wants to act on an str, like on a
    BTreeData instance, e.g.
    
        def _range_request_handler():
            ...
    
            if data is not None:
                RESPONSE.setHeader('Last-Modified', rfc1123_date(data._p_mtime))
    
    or
    
        def _read_data(... data=None):
            ...
    
            if data is None:
                btree = BTreeData()
            else:
                btree = data
    
            ...
            btree.write(...)
    
    and other places, and in all those situation we'll get AttributeError
    (str has neither ._p_mtime nor .write) and it will crash.
    
    ~~~~
    
    .data can be str at least because '' is the default value for `data`
    field in Data property sheet. From this proposition the code could be
    reorganised to work in "data is either BTreeData or empty (= None or '')"
    
    But we discussed with Romain and his idea is that non empty strings have
    to be too supported because of compatibility reasons and because of
    desire to support possible future automatic File-based documents
    migration to BigFiles.
    
    From this perspective for BigFile the invariant thus
    
        .data is either BTreeData or str (empty or not) or None.
    
    This patch goes through whole BigFile code and corrects places to either
    properly support str case, or None (in e.g. computing len(data) in
    index_html).
    
    In _read_data() if data is previously str - that means we are appending
    content to this file and thus it is a good idea to first convert str
    (empty or not) to BTreeData and then proceed with appending.
    Helped-by: Vincent Pelletier's avatarVincent Pelletier <vincent@nexedi.com>
    Reviewed-by: Romain Courteaud's avatarRomain Courteaud <romain@nexedi.com>
    f54e3fa6
BigFile.py 13.8 KB