Exemplo n.º 1
0
def test_make_8dot3_name_cut_ext():
    """Test that make_8dot3_filename generates valid 8dot3 filenames."""
    fde = mock.MagicMock()
    fde.get_entries.return_value = ([], [], [])
    sfn = EightDotThree()
    lfn = sfn.make_8dot3_name("This is a long filename.TeXT", fde)
    assert "THISIS.TEX" == lfn
    assert sfn.is_8dot3_conform(lfn)
Exemplo n.º 2
0
def test_make_8dot3_name():
    """Test that make_8dot3_filename generates valid 8dot3 filenames."""
    fde = mock.MagicMock()
    fde.get_entries.return_value = ([], [], [])
    sfn = EightDotThree()
    n = sfn.make_8dot3_name("This is a long filename.txt", fde)
    sfn.set_str_name(n)
    assert "THISIS.TXT" == n
    assert sfn.is_8dot3_conform(sfn.get_unpadded_filename())
Exemplo n.º 3
0
def test_make_8dot3_name_collision():
    """Test that make_8dot3_filename generates valid 8dot3 filenames."""
    fde = mock.MagicMock()
    fde_sub = mock.MagicMock()
    fde_sub.get_short_name.side_effect = [
        "THISIS.TXT", "THISIS~1.TXT", "THISIS~2.TXT"
    ]
    fde.get_entries.return_value = ([fde_sub, fde_sub], [fde_sub], [])
    sfn = EightDotThree()
    lfn = sfn.make_8dot3_name("This is a long filename.txt", fde)
    assert "THISIS~3.TXT" == lfn
    assert sfn.is_8dot3_conform(lfn)
Exemplo n.º 4
0
def test_is_8dot3_conform_false():
    """Test that non-8.3 file names are correctly detected."""
    assert not EightDotThree.is_8dot3_conform("This is a Long file.txt")
Exemplo n.º 5
0
def test_is_8dot3_conform_true():
    """Test that 8.3 file names are correctly detected."""
    assert EightDotThree.is_8dot3_conform("TESTFILE.TXT")
Exemplo n.º 6
0
def make_lfn_entry(dir_name: str,
                   short_name):
    """Generate a `FATLongDirectoryEntry` instance from directory name.

    :param dir_name: Long name of directory
    :param short_name: `EightDotThree` class instance
    :raises PyFATException if entry name does not require an LFN
            entry or the name exceeds the FAT limitation of 255 characters
    """
    lfn_entry = FATLongDirectoryEntry()
    #: Length in bytes of an LFN entry
    lfn_entry_length = 26
    dir_name_str = dir_name
    dir_name = dir_name.encode(FAT_LFN_ENCODING)
    dir_name_modulus = len(dir_name) % lfn_entry_length

    if EightDotThree.is_8dot3_conform(dir_name_str):
        raise PyFATException("Directory entry is already 8.3 conform, "
                             "no need to create an LFN entry.",
                             errno=errno.EINVAL)

    if len(dir_name) > 255:
        raise PyFATException("Long file name exceeds 255 "
                             "characters, not supported.",
                             errno=errno.ENAMETOOLONG)

    checksum = short_name.checksum()

    if dir_name_modulus != 0:
        # Null-terminate string if required
        dir_name += '\0'.encode(FAT_LFN_ENCODING)

    # Fill the rest with 0xFF if it doesn't fit evenly
    new_sz = lfn_entry_length - len(dir_name)
    new_sz %= lfn_entry_length
    new_sz += len(dir_name)
    dir_name += b'\xFF' * (new_sz - len(dir_name))

    # Generate linked LFN entries
    lfn_entries = len(dir_name) // lfn_entry_length
    for i in range(lfn_entries):
        if i == lfn_entries-1:
            lfn_entry_ord = 0x40 | i+1
        else:
            lfn_entry_ord = i+1

        n = i*lfn_entry_length
        dirname1 = dir_name[n:n+10]
        n += 10
        dirname2 = dir_name[n:n+12]
        n += 12
        dirname3 = dir_name[n:n+4]

        lfn_entry.add_lfn_entry(LDIR_Ord=lfn_entry_ord,
                                LDIR_Name1=dirname1,
                                LDIR_Attr=FATDirectoryEntry.ATTR_LONG_NAME,
                                LDIR_Type=0x00,
                                LDIR_Chksum=checksum,
                                LDIR_Name2=dirname2,
                                LDIR_FstClusLO=0,
                                LDIR_Name3=dirname3)
    return lfn_entry