From 567f5f963ca0012c3499e7ecacff2d14f7021112 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ga=C5=88o?= Date: Wed, 14 Sep 2022 12:26:25 +0200 Subject: [PATCH] fatfsgen.py: use lfn record when forbidden character detected in the file name The file name is recorded as a LFN when it doesn't fit 8.3 pattern or if it contains characters prohibited in SFN --- components/fatfs/fatfs_utils/fs_object.py | 16 +++++++++++----- components/fatfs/fatfs_utils/utils.py | 4 ++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/components/fatfs/fatfs_utils/fs_object.py b/components/fatfs/fatfs_utils/fs_object.py index 00de17676c..836d988ee3 100644 --- a/components/fatfs/fatfs_utils/fs_object.py +++ b/components/fatfs/fatfs_utils/fs_object.py @@ -12,8 +12,9 @@ from .fatfs_state import FATFSState from .long_filename_utils import (build_lfn_full_name, build_lfn_unique_entry_name_order, get_required_lfn_entries_count, split_name_to_lfn_entries, split_name_to_lfn_entry_blocks) -from .utils import (DATETIME, MAX_EXT_SIZE, MAX_NAME_SIZE, FATDefaults, build_lfn_short_entry_name, build_name, - lfn_checksum, required_clusters_count, split_content_into_sectors, split_to_name_and_extension) +from .utils import (DATETIME, INVALID_SFN_CHARS_PATTERN, MAX_EXT_SIZE, MAX_NAME_SIZE, FATDefaults, + build_lfn_short_entry_name, build_name, lfn_checksum, required_clusters_count, + split_content_into_sectors, split_to_name_and_extension) class File: @@ -235,6 +236,13 @@ class Directory: time=time) return free_cluster, free_entry, target_dir + @staticmethod + def _is_valid_sfn(name: str, extension: str) -> bool: + if INVALID_SFN_CHARS_PATTERN.search(name) or INVALID_SFN_CHARS_PATTERN.search(name): + return False + ret: bool = len(name) <= MAX_NAME_SIZE and len(extension) <= MAX_EXT_SIZE + return ret + def allocate_object(self, name, entity_type, @@ -251,12 +259,10 @@ class Directory: target_dir: Directory = self if not path_from_root else self.recursive_search(path_from_root, self) free_entry: Entry = target_dir.find_free_entry() or target_dir.chain_directory() - name_fits_short_struct: bool = len(name) <= MAX_NAME_SIZE and len(extension) <= MAX_EXT_SIZE - fatfs_date_ = (object_timestamp_.year, object_timestamp_.month, object_timestamp_.day) fatfs_time_ = (object_timestamp_.hour, object_timestamp_.minute, object_timestamp_.second) - if not self.fatfs_state.long_names_enabled or name_fits_short_struct: + if not self.fatfs_state.long_names_enabled or self._is_valid_sfn(name, extension): free_entry.allocate_entry(first_cluster_id=free_cluster.id, entity_name=name, entity_extension=extension, diff --git a/components/fatfs/fatfs_utils/utils.py b/components/fatfs/fatfs_utils/utils.py index 06beb6c752..839dedbfe0 100644 --- a/components/fatfs/fatfs_utils/utils.py +++ b/components/fatfs/fatfs_utils/utils.py @@ -4,12 +4,16 @@ import argparse import binascii import os +import re import uuid from datetime import datetime from typing import List, Optional, Tuple from construct import BitsInteger, BitStruct, Int16ul +# the regex pattern defines symbols that are allowed by long file names but not by short file names +INVALID_SFN_CHARS_PATTERN = re.compile(r'[.+,;=\[\]]') + FAT12_MAX_CLUSTERS: int = 4085 FAT16_MAX_CLUSTERS: int = 65525 RESERVED_CLUSTERS_COUNT: int = 2