2022-02-09 10:09:09 -05:00
|
|
|
# SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
|
2021-09-21 18:32:54 -04:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2022-04-04 09:33:00 -04:00
|
|
|
from typing import List, Optional, Union
|
2021-09-21 18:32:54 -04:00
|
|
|
|
|
|
|
from construct import Const, Int8ul, Int16ul, Int32ul, PaddedString, Struct
|
|
|
|
|
|
|
|
from .exceptions import LowerCaseException, TooLongNameException
|
|
|
|
from .fatfs_state import FATFSState
|
2022-04-04 09:33:00 -04:00
|
|
|
from .utils import (DATETIME, EMPTY_BYTE, FATFS_INCEPTION, MAX_EXT_SIZE, MAX_NAME_SIZE, SHORT_NAMES_ENCODING,
|
|
|
|
FATDefaults, build_date_entry, build_time_entry, is_valid_fatfs_name, pad_string)
|
2021-09-21 18:32:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
class Entry:
|
|
|
|
"""
|
|
|
|
The class Entry represents entry of the directory.
|
|
|
|
"""
|
2022-02-09 10:09:09 -05:00
|
|
|
ATTR_READ_ONLY: int = 0x01
|
|
|
|
ATTR_HIDDEN: int = 0x02
|
|
|
|
ATTR_SYSTEM: int = 0x04
|
|
|
|
ATTR_VOLUME_ID: int = 0x08
|
|
|
|
ATTR_DIRECTORY: int = 0x10
|
|
|
|
ATTR_ARCHIVE: int = 0x20
|
|
|
|
ATTR_LONG_NAME: int = ATTR_READ_ONLY | ATTR_HIDDEN | ATTR_SYSTEM | ATTR_VOLUME_ID
|
|
|
|
|
|
|
|
# indexes in the entry structure and sizes in bytes, not in characters (encoded using 2 bytes for lfn)
|
|
|
|
LDIR_Name1_IDX: int = 1
|
|
|
|
LDIR_Name1_SIZE: int = 5
|
|
|
|
LDIR_Name2_IDX: int = 14
|
|
|
|
LDIR_Name2_SIZE: int = 6
|
|
|
|
LDIR_Name3_IDX: int = 28
|
|
|
|
LDIR_Name3_SIZE: int = 2
|
|
|
|
|
|
|
|
# one entry can hold 13 characters with size 2 bytes distributed in three regions of the 32 bytes entry
|
|
|
|
CHARS_PER_ENTRY: int = LDIR_Name1_SIZE + LDIR_Name2_SIZE + LDIR_Name3_SIZE
|
|
|
|
|
|
|
|
SHORT_ENTRY: int = -1
|
|
|
|
SHORT_ENTRY_LN: int = 0
|
2021-09-21 18:32:54 -04:00
|
|
|
|
2022-03-17 13:02:16 -04:00
|
|
|
# The 1st January 1980 00:00:00
|
|
|
|
DEFAULT_DATE: DATETIME = (FATFS_INCEPTION.year, FATFS_INCEPTION.month, FATFS_INCEPTION.day)
|
|
|
|
DEFAULT_TIME: DATETIME = (FATFS_INCEPTION.hour, FATFS_INCEPTION.minute, FATFS_INCEPTION.second)
|
|
|
|
|
2021-09-21 18:32:54 -04:00
|
|
|
ENTRY_FORMAT_SHORT_NAME = Struct(
|
2022-03-17 13:02:16 -04:00
|
|
|
'DIR_Name' / PaddedString(MAX_NAME_SIZE, SHORT_NAMES_ENCODING),
|
|
|
|
'DIR_Name_ext' / PaddedString(MAX_EXT_SIZE, SHORT_NAMES_ENCODING),
|
2021-09-21 18:32:54 -04:00
|
|
|
'DIR_Attr' / Int8ul,
|
2022-04-04 09:33:00 -04:00
|
|
|
'DIR_NTRes' / Const(EMPTY_BYTE),
|
|
|
|
'DIR_CrtTimeTenth' / Const(EMPTY_BYTE), # ignored by esp-idf fatfs library
|
|
|
|
'DIR_CrtTime' / Int16ul, # ignored by esp-idf fatfs library
|
|
|
|
'DIR_CrtDate' / Int16ul, # ignored by esp-idf fatfs library
|
|
|
|
'DIR_LstAccDate' / Int16ul, # must be same as DIR_WrtDate
|
|
|
|
'DIR_FstClusHI' / Const(2 * EMPTY_BYTE),
|
2022-03-17 13:02:16 -04:00
|
|
|
'DIR_WrtTime' / Int16ul,
|
|
|
|
'DIR_WrtDate' / Int16ul,
|
2021-09-21 18:32:54 -04:00
|
|
|
'DIR_FstClusLO' / Int16ul,
|
|
|
|
'DIR_FileSize' / Int32ul,
|
|
|
|
)
|
|
|
|
|
|
|
|
def __init__(self,
|
|
|
|
entry_id: int,
|
|
|
|
parent_dir_entries_address: int,
|
|
|
|
fatfs_state: FATFSState) -> None:
|
2022-02-09 10:09:09 -05:00
|
|
|
self.fatfs_state: FATFSState = fatfs_state
|
|
|
|
self.id: int = entry_id
|
2022-04-04 09:33:00 -04:00
|
|
|
self.entry_address: int = parent_dir_entries_address + self.id * FATDefaults.ENTRY_SIZE
|
2022-02-09 10:09:09 -05:00
|
|
|
self._is_alias: bool = False
|
|
|
|
self._is_empty: bool = True
|
2021-09-21 18:32:54 -04:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_empty(self) -> bool:
|
|
|
|
return self._is_empty
|
|
|
|
|
2022-02-09 10:09:09 -05:00
|
|
|
@staticmethod
|
2022-04-04 09:33:00 -04:00
|
|
|
def _parse_entry(entry_bytearray: Union[bytearray, bytes]) -> dict:
|
|
|
|
entry_: dict = Entry.ENTRY_FORMAT_SHORT_NAME.parse(entry_bytearray)
|
|
|
|
return entry_
|
2021-09-21 18:32:54 -04:00
|
|
|
|
2022-02-09 10:09:09 -05:00
|
|
|
@staticmethod
|
2022-04-04 09:33:00 -04:00
|
|
|
def _build_entry(**kwargs) -> bytes: # type: ignore
|
|
|
|
entry_: bytes = Entry.ENTRY_FORMAT_SHORT_NAME.build(dict(**kwargs))
|
|
|
|
return entry_
|
2021-09-21 18:32:54 -04:00
|
|
|
|
2022-02-09 10:09:09 -05:00
|
|
|
@staticmethod
|
|
|
|
def _build_entry_long(names: List[bytes], checksum: int, order: int, is_last: bool, entity_type: int) -> bytes:
|
|
|
|
"""
|
|
|
|
Long entry starts with 1 bytes of the order, if the entry is the last in the chain it is or-masked with 0x40,
|
|
|
|
otherwise is without change (or masked with 0x00). The following example shows 3 entries:
|
|
|
|
first two (0x2000-0x2040) are long in the reverse order and the last one (0x2040-0x2060) is short.
|
|
|
|
The entries define file name "thisisverylongfilenama.txt".
|
|
|
|
|
|
|
|
00002000: 42 67 00 66 00 69 00 6C 00 65 00 0F 00 43 6E 00 Bg.f.i.l.e...Cn.
|
|
|
|
00002010: 61 00 6D 00 61 00 2E 00 74 00 00 00 78 00 74 00 a.m.a...t...x.t.
|
|
|
|
00002020: 01 74 00 68 00 69 00 73 00 69 00 0F 00 43 73 00 .t.h.i.s.i...Cs.
|
|
|
|
00002030: 76 00 65 00 72 00 79 00 6C 00 00 00 6F 00 6E 00 v.e.r.y.l...o.n.
|
|
|
|
00002040: 54 48 49 53 49 53 7E 31 54 58 54 20 00 00 00 00 THISIS~1TXT.....
|
|
|
|
00002050: 21 00 00 00 00 00 00 00 21 00 02 00 15 00 00 00 !.......!.......
|
|
|
|
"""
|
|
|
|
order |= (0x40 if is_last else 0x00)
|
|
|
|
long_entry: bytes = (Int8ul.build(order) + # order of the long name entry (possibly masked with 0x40)
|
|
|
|
names[0] + # first 5 characters (10 bytes) of the name part
|
|
|
|
Int8ul.build(entity_type) + # one byte entity type ATTR_LONG_NAME
|
|
|
|
Int8ul.build(0) + # one byte of zeros
|
|
|
|
Int8ul.build(checksum) + # lfn_checksum defined in utils.py
|
|
|
|
names[1] + # next 6 characters (12 bytes) of the name part
|
|
|
|
Int16ul.build(0) + # 2 bytes of zeros
|
|
|
|
names[2]) # last 2 characters (4 bytes) of the name part
|
|
|
|
return long_entry
|
|
|
|
|
2021-09-21 18:32:54 -04:00
|
|
|
@property
|
2022-04-04 09:33:00 -04:00
|
|
|
def entry_bytes(self) -> bytes:
|
|
|
|
"""
|
|
|
|
:returns: Bytes defining the entry belonging to the given instance.
|
|
|
|
"""
|
|
|
|
start_: int = self.entry_address
|
|
|
|
entry_: bytes = self.fatfs_state.binary_image[start_: start_ + FATDefaults.ENTRY_SIZE]
|
|
|
|
return entry_
|
2021-09-21 18:32:54 -04:00
|
|
|
|
|
|
|
@entry_bytes.setter
|
2022-04-04 09:33:00 -04:00
|
|
|
def entry_bytes(self, value: bytes) -> None:
|
|
|
|
"""
|
|
|
|
:param value: new content of the entry
|
|
|
|
:returns: None
|
|
|
|
|
|
|
|
The setter sets the content of the entry in bytes.
|
|
|
|
"""
|
|
|
|
self.fatfs_state.binary_image[self.entry_address: self.entry_address + FATDefaults.ENTRY_SIZE] = value
|
2021-09-21 18:32:54 -04:00
|
|
|
|
|
|
|
def _clean_entry(self) -> None:
|
2022-04-04 09:33:00 -04:00
|
|
|
self.entry_bytes: bytes = FATDefaults.ENTRY_SIZE * EMPTY_BYTE
|
2021-09-21 18:32:54 -04:00
|
|
|
|
|
|
|
def allocate_entry(self,
|
|
|
|
first_cluster_id: int,
|
|
|
|
entity_name: str,
|
|
|
|
entity_type: int,
|
|
|
|
entity_extension: str = '',
|
2022-02-09 10:09:09 -05:00
|
|
|
size: int = 0,
|
2022-03-17 13:02:16 -04:00
|
|
|
date: DATETIME = DEFAULT_DATE,
|
|
|
|
time: DATETIME = DEFAULT_TIME,
|
2022-02-09 10:09:09 -05:00
|
|
|
lfn_order: int = SHORT_ENTRY,
|
|
|
|
lfn_names: Optional[List[bytes]] = None,
|
|
|
|
lfn_checksum_: int = 0,
|
|
|
|
lfn_is_last: bool = False) -> None:
|
2021-09-21 18:32:54 -04:00
|
|
|
"""
|
|
|
|
:param first_cluster_id: id of the first data cluster for given entry
|
|
|
|
:param entity_name: name recorded in the entry
|
|
|
|
:param entity_extension: extension recorded in the entry
|
|
|
|
:param size: size of the content of the file
|
2022-03-17 13:02:16 -04:00
|
|
|
:param date: denotes year (actual year minus 1980), month number day of the month (minimal valid is (0, 1, 1))
|
|
|
|
:param time: denotes hour, minute and second with granularity 2 seconds (sec // 2)
|
2021-09-21 18:32:54 -04:00
|
|
|
:param entity_type: type of the entity (file [0x20] or directory [0x10])
|
2022-02-09 10:09:09 -05:00
|
|
|
:param lfn_order: if long names support is enabled, defines order in long names entries sequence (-1 for short)
|
|
|
|
:param lfn_names: if the entry is dedicated for long names the lfn_names contains
|
|
|
|
LDIR_Name1, LDIR_Name2 and LDIR_Name3 in this order
|
|
|
|
:param lfn_checksum_: use only for long file names, checksum calculated lfn_checksum function
|
|
|
|
:param lfn_is_last: determines if the long file name entry is holds last part of the name,
|
|
|
|
thus its address is first in the physical order
|
2021-09-21 18:32:54 -04:00
|
|
|
:returns: None
|
|
|
|
|
|
|
|
:raises LowerCaseException: In case when long_names_enabled is set to False and filename exceeds 8 chars
|
|
|
|
for name or 3 chars for extension the exception is raised
|
2022-03-17 13:02:16 -04:00
|
|
|
:raises TooLongNameException: When long_names_enabled is set to False and name doesn't fit to 8.3 filename
|
|
|
|
an exception is raised
|
2021-09-21 18:32:54 -04:00
|
|
|
"""
|
2022-02-09 10:09:09 -05:00
|
|
|
valid_full_name: bool = is_valid_fatfs_name(entity_name) and is_valid_fatfs_name(entity_extension)
|
|
|
|
if not (valid_full_name or lfn_order >= 0):
|
|
|
|
raise LowerCaseException('Lower case is not supported in short name entry, use upper case.')
|
2021-09-21 18:32:54 -04:00
|
|
|
|
2022-03-17 13:02:16 -04:00
|
|
|
if self.fatfs_state.use_default_datetime:
|
|
|
|
date = self.DEFAULT_DATE
|
|
|
|
time = self.DEFAULT_TIME
|
|
|
|
|
2021-09-21 18:32:54 -04:00
|
|
|
# clean entry before allocation
|
|
|
|
self._clean_entry()
|
|
|
|
self._is_empty = False
|
|
|
|
|
2022-02-09 10:09:09 -05:00
|
|
|
object_name = entity_name.upper() if not self.fatfs_state.long_names_enabled else entity_name
|
|
|
|
object_extension = entity_extension.upper() if not self.fatfs_state.long_names_enabled else entity_extension
|
|
|
|
|
|
|
|
exceeds_short_name: bool = len(object_name) > MAX_NAME_SIZE or len(object_extension) > MAX_EXT_SIZE
|
2021-09-21 18:32:54 -04:00
|
|
|
if not self.fatfs_state.long_names_enabled and exceeds_short_name:
|
|
|
|
raise TooLongNameException(
|
2022-02-09 10:09:09 -05:00
|
|
|
'Maximal length of the object name is {} characters and {} characters for extension!'.format(
|
|
|
|
MAX_NAME_SIZE, MAX_EXT_SIZE
|
2022-03-17 13:02:16 -04:00
|
|
|
)
|
|
|
|
)
|
2021-09-21 18:32:54 -04:00
|
|
|
|
|
|
|
start_address = self.entry_address
|
2022-04-04 09:33:00 -04:00
|
|
|
end_address = start_address + FATDefaults.ENTRY_SIZE
|
2022-02-09 10:09:09 -05:00
|
|
|
if lfn_order in (self.SHORT_ENTRY, self.SHORT_ENTRY_LN):
|
2022-03-17 13:02:16 -04:00
|
|
|
date_entry_: int = build_date_entry(*date)
|
|
|
|
time_entry: int = build_time_entry(*time)
|
2022-02-09 10:09:09 -05:00
|
|
|
self.fatfs_state.binary_image[start_address: end_address] = self._build_entry(
|
|
|
|
DIR_Name=pad_string(object_name, size=MAX_NAME_SIZE),
|
|
|
|
DIR_Name_ext=pad_string(object_extension, size=MAX_EXT_SIZE),
|
|
|
|
DIR_Attr=entity_type,
|
|
|
|
DIR_FstClusLO=first_cluster_id,
|
2022-03-17 13:02:16 -04:00
|
|
|
DIR_FileSize=size,
|
|
|
|
DIR_CrtDate=date_entry_, # ignored by esp-idf fatfs library
|
|
|
|
DIR_LstAccDate=date_entry_, # must be same as DIR_WrtDate
|
|
|
|
DIR_WrtDate=date_entry_,
|
|
|
|
DIR_CrtTime=time_entry, # ignored by esp-idf fatfs library
|
|
|
|
DIR_WrtTime=time_entry
|
2022-02-09 10:09:09 -05:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
assert lfn_names is not None
|
|
|
|
self.fatfs_state.binary_image[start_address: end_address] = self._build_entry_long(lfn_names,
|
|
|
|
lfn_checksum_,
|
|
|
|
lfn_order,
|
|
|
|
lfn_is_last,
|
|
|
|
self.ATTR_LONG_NAME)
|
2021-09-21 18:32:54 -04:00
|
|
|
|
|
|
|
def update_content_size(self, content_size: int) -> None:
|
2022-04-04 09:33:00 -04:00
|
|
|
"""
|
|
|
|
:param content_size: the new size of the file content in bytes
|
|
|
|
:returns: None
|
|
|
|
|
|
|
|
This method parses the binary entry to the construct structure, updates the content size of the file
|
|
|
|
and builds new binary entry.
|
|
|
|
"""
|
2021-09-21 18:32:54 -04:00
|
|
|
parsed_entry = self._parse_entry(self.entry_bytes)
|
|
|
|
parsed_entry.DIR_FileSize = content_size # type: ignore
|
|
|
|
self.entry_bytes = Entry.ENTRY_FORMAT_SHORT_NAME.build(parsed_entry)
|