2011-07-13 00:38:21 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* fs/fat/fs_fat32dirent.c
|
|
|
|
*
|
2020-05-31 17:48:21 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2011-07-13 00:38:21 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* NOTE: If CONFIG_FAT_LFN is defined, then there may be some legal, patent
|
|
|
|
* issues. The following was extracted from the entry "File Allocation Table
|
|
|
|
* from Wikipedia, the free encyclopedia:
|
|
|
|
*
|
|
|
|
* "On December 3, 2003 Microsoft announced it would be offering licenses
|
|
|
|
* for use of its FAT specification and 'associated intellectual property',
|
|
|
|
* at the cost of a US$0.25 royalty per unit sold, with a $250,000 maximum
|
|
|
|
* royalty per license agreement.
|
|
|
|
*
|
|
|
|
* o "U.S. Patent 5,745,902 (http://www.google.com/patents?vid=5745902) -
|
|
|
|
* Method and system for accessing a file using file names having
|
|
|
|
* different file name formats. ...
|
|
|
|
* o "U.S. Patent 5,579,517 (http://www.google.com/patents?vid=5579517) -
|
|
|
|
* Common name space for long and short filenames. ...
|
|
|
|
* o "U.S. Patent 5,758,352 (http://www.google.com/patents?vid=5758352) -
|
|
|
|
* Common name space for long and short filenames. ...
|
|
|
|
* o "U.S. Patent 6,286,013 (http://www.google.com/patents?vid=6286013) -
|
|
|
|
* Method and system for providing a common name space for long and
|
|
|
|
* short file names in an operating system. ...
|
|
|
|
*
|
|
|
|
* "Many technical commentators have concluded that these patents only cover
|
|
|
|
* FAT implementations that include support for long filenames, and that
|
|
|
|
* removable solid state media and consumer devices only using short names
|
|
|
|
* would be unaffected. ..."
|
|
|
|
*
|
|
|
|
* So you have been forewarned: Use the long filename at your own risk!
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2023-02-01 14:41:12 +01:00
|
|
|
#include <sys/param.h>
|
2011-07-13 00:38:21 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2012-03-21 19:01:07 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
#include <nuttx/fs/fat.h>
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2014-09-29 15:14:38 +02:00
|
|
|
#include "inode/inode.h"
|
2011-07-13 00:38:21 +02:00
|
|
|
#include "fs_fat32.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-07-13 19:19:31 +02:00
|
|
|
enum fat_case_e
|
|
|
|
{
|
|
|
|
FATCASE_UNKNOWN = 0,
|
|
|
|
FATCASE_UPPER,
|
|
|
|
FATCASE_LOWER
|
|
|
|
};
|
|
|
|
|
2011-07-13 00:38:21 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-07-18 20:28:02 +02:00
|
|
|
#ifdef CONFIG_FAT_LFN
|
2020-05-31 17:48:21 +02:00
|
|
|
static uint8_t fat_lfnchecksum(FAR const uint8_t *sfname);
|
2011-07-18 20:28:02 +02:00
|
|
|
#endif
|
2020-05-31 17:48:21 +02:00
|
|
|
static inline int fat_parsesfname(FAR const char **path,
|
|
|
|
FAR struct fat_dirinfo_s *dirinfo,
|
|
|
|
FAR char *terminator);
|
2011-07-18 20:28:02 +02:00
|
|
|
#ifdef CONFIG_FAT_LFN
|
2020-05-31 17:48:21 +02:00
|
|
|
static inline int fat_parselfname(FAR const char **path,
|
|
|
|
FAR struct fat_dirinfo_s *dirinfo,
|
|
|
|
FAR char *terminator);
|
|
|
|
static inline int fat_createalias(FAR struct fat_dirinfo_s *dirinfo);
|
|
|
|
static inline int fat_findalias(FAR struct fat_mountpt_s *fs,
|
|
|
|
FAR struct fat_dirinfo_s *dirinfo);
|
|
|
|
static inline int fat_uniquealias(FAR struct fat_mountpt_s *fs,
|
|
|
|
FAR struct fat_dirinfo_s *dirinfo);
|
2011-07-18 20:28:02 +02:00
|
|
|
#endif
|
2020-05-31 17:48:21 +02:00
|
|
|
static int fat_path2dirname(FAR const char **path,
|
|
|
|
FAR struct fat_dirinfo_s *dirinfo,
|
|
|
|
FAR char *terminator);
|
|
|
|
static int fat_findsfnentry(FAR struct fat_mountpt_s *fs,
|
|
|
|
FAR struct fat_dirinfo_s *dirinfo);
|
2011-07-18 20:28:02 +02:00
|
|
|
#ifdef CONFIG_FAT_LFN
|
2020-07-26 12:52:22 +02:00
|
|
|
static bool fat_cmplfnchunk(FAR uint8_t *chunk, FAR const lfnchar *substr,
|
2020-05-31 17:48:21 +02:00
|
|
|
int nchunk);
|
|
|
|
static bool fat_cmplfname(FAR const uint8_t *direntry,
|
2020-07-26 12:52:22 +02:00
|
|
|
FAR const lfnchar *substr);
|
2020-05-31 17:48:21 +02:00
|
|
|
static inline int fat_findlfnentry(FAR struct fat_mountpt_s *fs,
|
|
|
|
FAR struct fat_dirinfo_s *dirinfo);
|
2011-07-18 20:28:02 +02:00
|
|
|
|
|
|
|
#endif
|
2020-05-31 17:48:21 +02:00
|
|
|
static inline int fat_allocatesfnentry(FAR struct fat_mountpt_s *fs,
|
|
|
|
FAR struct fat_dirinfo_s *dirinfo);
|
2011-07-18 20:28:02 +02:00
|
|
|
#ifdef CONFIG_FAT_LFN
|
2020-05-31 17:48:21 +02:00
|
|
|
static inline int fat_allocatelfnentry(FAR struct fat_mountpt_s *fs,
|
|
|
|
FAR struct fat_dirinfo_s *dirinfo);
|
2011-07-18 20:28:02 +02:00
|
|
|
#endif
|
2020-05-31 17:48:21 +02:00
|
|
|
static inline int fat_getsfname(FAR uint8_t *direntry, FAR char *buffer,
|
2011-07-18 20:28:02 +02:00
|
|
|
unsigned int buflen);
|
|
|
|
#ifdef CONFIG_FAT_LFN
|
2020-07-26 12:52:22 +02:00
|
|
|
static void fat_getlfnchunk(FAR uint8_t *chunk, FAR lfnchar *dest,
|
2020-05-31 17:48:21 +02:00
|
|
|
int nchunk);
|
|
|
|
static inline int fat_getlfname(FAR struct fat_mountpt_s *fs,
|
2022-07-26 08:56:41 +02:00
|
|
|
FAR struct fs_dirent_s *dir,
|
|
|
|
FAR struct dirent *entry);
|
2011-07-18 20:28:02 +02:00
|
|
|
#endif
|
2020-05-31 17:48:21 +02:00
|
|
|
static int fat_putsfname(FAR struct fat_mountpt_s *fs,
|
|
|
|
FAR struct fat_dirinfo_s *dirinfo);
|
2011-07-18 20:28:02 +02:00
|
|
|
#ifdef CONFIG_FAT_LFN
|
2020-05-31 17:48:21 +02:00
|
|
|
static void fat_initlfname(FAR uint8_t *chunk, int nchunk);
|
2020-07-26 12:52:22 +02:00
|
|
|
static void fat_putlfnchunk(FAR uint8_t *chunk, FAR const lfnchar *src,
|
2020-05-31 17:48:21 +02:00
|
|
|
int nchunk);
|
|
|
|
static int fat_putlfname(FAR struct fat_mountpt_s *fs,
|
|
|
|
FAR struct fat_dirinfo_s *dirinfo);
|
2011-07-18 20:28:02 +02:00
|
|
|
#endif
|
2020-05-31 17:48:21 +02:00
|
|
|
static int fat_putsfdirentry(FAR struct fat_mountpt_s *fs,
|
|
|
|
FAR struct fat_dirinfo_s *dirinfo,
|
2011-07-18 20:28:02 +02:00
|
|
|
uint8_t attributes, uint32_t fattime);
|
|
|
|
|
2020-07-26 12:52:22 +02:00
|
|
|
#if defined(CONFIG_FAT_LFN) && defined(CONFIG_FAT_LFN_UTF8)
|
2024-08-25 01:21:12 +02:00
|
|
|
static int fat_utf8toucs(FAR const char **str, FAR lfnchar *ucs);
|
2020-07-26 12:52:22 +02:00
|
|
|
static int fat_ucstoutf8(FAR uint8_t *dest, uint8_t offset, lfnchar ucs);
|
|
|
|
#endif
|
|
|
|
|
2011-07-13 00:38:21 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-07-26 12:52:22 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: fat_utf8toucs
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Convert the next characters from UTF8 to UCS2.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
#if defined(CONFIG_FAT_LFN) && defined(CONFIG_FAT_LFN_UTF8)
|
2024-08-25 01:21:12 +02:00
|
|
|
static int fat_utf8toucs(FAR const char **str, FAR lfnchar *ucs)
|
2020-07-26 12:52:22 +02:00
|
|
|
{
|
|
|
|
uint8_t chr;
|
|
|
|
lfnchar tucs;
|
|
|
|
int ret = ERROR;
|
|
|
|
|
|
|
|
*ucs = '\0';
|
|
|
|
chr = *((*str)++);
|
|
|
|
|
|
|
|
if ((chr & 0x80) == 0x00)
|
|
|
|
{
|
|
|
|
tucs = (lfnchar)chr;
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
else if ((chr & 0xe0) == 0xc0)
|
|
|
|
{
|
|
|
|
tucs = ((lfnchar)(chr & ~0xe0)) << 6;
|
|
|
|
chr = *((*str)++);
|
|
|
|
if ((chr & 0xc0) == 0x80)
|
|
|
|
{
|
|
|
|
tucs |= (lfnchar)(chr & ~0xc0);
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ((chr & 0xf0) == 0xe0)
|
|
|
|
{
|
|
|
|
tucs = ((lfnchar)(chr & ~0xf0)) << 12;
|
|
|
|
chr = *((*str)++);
|
|
|
|
if ((chr & 0xc0) == 0x80)
|
|
|
|
{
|
|
|
|
tucs |= (lfnchar)(chr & ~0xc0) << 6;
|
|
|
|
chr = *((*str)++);
|
|
|
|
if ((chr & 0xc0) == 0x80)
|
|
|
|
{
|
|
|
|
tucs |= (lfnchar)(chr & ~0xc0);
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
*ucs = tucs;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: fat_utf8toucs
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Convert the next character from UCS2 to UTF8, reverse.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
#if defined(CONFIG_FAT_LFN) && defined(CONFIG_FAT_LFN_UTF8)
|
|
|
|
static int fat_ucstoutf8(FAR uint8_t *dest, uint8_t offset, lfnchar ucs)
|
|
|
|
{
|
|
|
|
if (ucs < 128 && offset >= 1)
|
|
|
|
{
|
|
|
|
dest[--offset] = (uint8_t)(ucs & 0xff);
|
|
|
|
}
|
|
|
|
else if (ucs < 2048 && offset >= 2)
|
|
|
|
{
|
|
|
|
dest[--offset] = (uint8_t)((ucs >> 0) & ~0xc0) | 0x80;
|
|
|
|
dest[--offset] = (uint8_t)((ucs >> 6) & ~0xe0) | 0xc0;
|
|
|
|
}
|
|
|
|
else if (offset >= 3)
|
|
|
|
{
|
|
|
|
dest[--offset] = (uint8_t)((ucs >> 0) & ~0xc0) | 0x80;
|
|
|
|
dest[--offset] = (uint8_t)((ucs >> 6) & ~0xc0) | 0x80;
|
|
|
|
dest[--offset] = (uint8_t)((ucs >> 12) & ~0xf0) | 0xe0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-07-14 19:57:59 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: fat_lfnchecksum
|
|
|
|
*
|
2015-03-15 19:22:12 +01:00
|
|
|
* Description:
|
|
|
|
* Verify that the checksum of the short file name matches the checksum
|
|
|
|
* that we found in the long file name entries.
|
2011-07-14 19:57:59 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_FAT_LFN
|
2020-05-31 17:48:21 +02:00
|
|
|
static uint8_t fat_lfnchecksum(FAR const uint8_t *sfname)
|
2011-07-14 19:57:59 +02:00
|
|
|
{
|
|
|
|
uint8_t sum = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = DIR_MAXFNAME; i; i--)
|
|
|
|
{
|
|
|
|
sum = ((sum & 1) << 7) + (sum >> 1) + *sfname++;
|
2015-03-15 19:22:12 +01:00
|
|
|
}
|
2011-07-14 19:57:59 +02:00
|
|
|
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-07-13 00:38:21 +02:00
|
|
|
/****************************************************************************
|
2011-07-13 19:19:31 +02:00
|
|
|
* Name: fat_parsesfname
|
2011-07-13 00:38:21 +02:00
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description: Convert a user filename into a properly formatted FAT
|
2011-07-13 19:19:31 +02:00
|
|
|
* (short 8.3) filename as it would appear in a directory entry. Here are
|
|
|
|
* the rules for the 8+3 short file name in the directory:
|
2011-07-13 00:38:21 +02:00
|
|
|
*
|
|
|
|
* The first byte:
|
|
|
|
*
|
2011-07-13 19:19:31 +02:00
|
|
|
* 0xe5 = The directory is free
|
|
|
|
* 0x00 = This directory and all following directories are free
|
|
|
|
* 0x05 = Really 0xe5
|
|
|
|
* 0x20 = May NOT be ' '
|
|
|
|
*
|
|
|
|
* Other characters may be any characters except for the following:
|
|
|
|
*
|
2011-07-13 00:38:21 +02:00
|
|
|
* 0x00-0x1f = (except for 0x00 and 0x05 in the first byte)
|
|
|
|
* 0x22 = '"'
|
|
|
|
* 0x2a-0x2c = '*', '+', ','
|
|
|
|
* 0x2e-0x2f = '.', '/'
|
|
|
|
* 0x3a-0x3f = ':', ';', '<', '=', '>', '?'
|
2024-02-05 10:20:51 +01:00
|
|
|
* 0x5b-0x5d = '[', '\\', ']'
|
2011-07-13 00:38:21 +02:00
|
|
|
* 0x7c = '|'
|
|
|
|
*
|
2011-07-13 19:19:31 +02:00
|
|
|
* '.' May only occur once within string and only within the first 9
|
|
|
|
* bytes. The '.' is not save in the directory, but is implicit in
|
|
|
|
* 8+3 format.
|
|
|
|
*
|
|
|
|
* Lower case characters are not allowed in directory names (without some
|
|
|
|
* poorly documented operations on the NTRes directory byte). Lower case
|
2011-07-13 00:38:21 +02:00
|
|
|
* codes may represent different characters in other character sets ("DOS
|
2024-02-05 10:20:51 +01:00
|
|
|
* code pages"). The logic below does not, at present, support any other
|
2011-07-13 00:38:21 +02:00
|
|
|
* character sets.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2011-07-13 19:19:31 +02:00
|
|
|
* OK - The path refers to a valid 8.3 FAT file name and has been properly
|
|
|
|
* converted and stored in dirinfo.
|
|
|
|
* <0 - Otherwise an negated error is returned meaning that the string is
|
|
|
|
* not a valid 8+3 because:
|
|
|
|
*
|
|
|
|
* 1. Contains characters not in the printable character set,
|
|
|
|
* 2. Contains forbidden characters or multiple '.' characters
|
|
|
|
* 3. File name or extension is too long.
|
|
|
|
*
|
|
|
|
* If CONFIG_FAT_LFN is defined and CONFIG_FAT_LCNAMES is NOT
|
|
|
|
* defined, then:
|
|
|
|
*
|
|
|
|
* 4a. File name or extension contains lower case characters.
|
|
|
|
*
|
|
|
|
* If CONFIG_FAT_LFN is defined and CONFIG_FAT_LCNAMES is defined,
|
|
|
|
* then:
|
|
|
|
*
|
|
|
|
* 4b. File name or extension is not all the same case.
|
|
|
|
*
|
2011-07-13 00:38:21 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-31 17:48:21 +02:00
|
|
|
static inline int fat_parsesfname(FAR const char **path,
|
|
|
|
FAR struct fat_dirinfo_s *dirinfo,
|
|
|
|
FAR char *terminator)
|
2011-07-13 00:38:21 +02:00
|
|
|
{
|
|
|
|
#ifdef CONFIG_FAT_LCNAMES
|
2011-07-13 19:19:31 +02:00
|
|
|
unsigned int ntlcenable = FATNTRES_LCNAME | FATNTRES_LCEXT;
|
|
|
|
unsigned int ntlcfound = 0;
|
|
|
|
#ifdef CONFIG_FAT_LFN
|
|
|
|
enum fat_case_e namecase = FATCASE_UNKNOWN;
|
|
|
|
enum fat_case_e extcase = FATCASE_UNKNOWN;
|
|
|
|
#endif
|
2011-07-13 00:38:21 +02:00
|
|
|
#endif
|
2022-04-04 12:46:23 +02:00
|
|
|
FAR const char *node = *path;
|
2011-07-13 19:19:31 +02:00
|
|
|
int endndx;
|
|
|
|
uint8_t ch;
|
|
|
|
int ndx = 0;
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-13 19:19:31 +02:00
|
|
|
/* Initialized the name with all spaces */
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-13 19:19:31 +02:00
|
|
|
memset(dirinfo->fd_name, ' ', DIR_MAXFNAME);
|
2013-03-31 15:06:22 +02:00
|
|
|
|
2011-07-13 19:19:31 +02:00
|
|
|
/* Loop until the name is successfully parsed or an error occurs */
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-13 19:19:31 +02:00
|
|
|
endndx = 8;
|
2015-10-11 19:39:29 +02:00
|
|
|
for (; ; )
|
2011-07-13 19:19:31 +02:00
|
|
|
{
|
|
|
|
/* Get the next byte from the path */
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-13 19:19:31 +02:00
|
|
|
ch = *node++;
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-13 19:19:31 +02:00
|
|
|
/* Check if this the last byte in this node of the name */
|
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
if ((ch == '\0' || ch == '/') && ndx != 0)
|
2011-07-13 19:19:31 +02:00
|
|
|
{
|
|
|
|
/* Return the accumulated NT flags and the terminating character */
|
2011-07-13 00:38:21 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_FAT_LCNAMES
|
2011-07-13 19:19:31 +02:00
|
|
|
dirinfo->fd_ntflags = ntlcfound & ntlcenable;
|
|
|
|
#endif
|
|
|
|
*terminator = ch;
|
|
|
|
*path = node;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2011-07-18 02:20:58 +02:00
|
|
|
/* Accept only the printable character set (excluding space). Note
|
|
|
|
* that the first byte of the name could be 0x05 meaning that is it
|
|
|
|
* 0xe5, but this is not a printable character in this character in
|
|
|
|
* either case.
|
2011-07-13 19:19:31 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
else if (!isgraph(ch))
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for transition from name to extension. Only one '.' is
|
|
|
|
* permitted and it must be within first 9 characters
|
|
|
|
*/
|
|
|
|
|
|
|
|
else if (ch == '.' && endndx == 8)
|
|
|
|
{
|
|
|
|
/* Starting the extension */
|
|
|
|
|
|
|
|
ndx = 8;
|
|
|
|
endndx = 11;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reject printable characters forbidden by FAT */
|
|
|
|
|
|
|
|
else if (ch == '"' || (ch >= '*' && ch <= ',') ||
|
|
|
|
ch == '.' || ch == '/' ||
|
|
|
|
(ch >= ':' && ch <= '?') ||
|
|
|
|
(ch >= '[' && ch <= ']') ||
|
|
|
|
(ch == '|'))
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for upper case characters */
|
|
|
|
|
|
|
|
#ifdef CONFIG_FAT_LCNAMES
|
|
|
|
else if (isupper(ch))
|
|
|
|
{
|
|
|
|
/* Some or all of the characters in the name or extension
|
|
|
|
* are upper case. Force all of the characters to be interpreted
|
|
|
|
* as upper case.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (endndx == 8)
|
|
|
|
{
|
|
|
|
/* Is there mixed case in the name? */
|
|
|
|
|
|
|
|
#ifdef CONFIG_FAT_LFN
|
|
|
|
if (namecase == FATCASE_LOWER)
|
|
|
|
{
|
|
|
|
/* Mixed case in the name -- use the long file name */
|
|
|
|
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
/* So far, only upper case in the name */
|
2011-07-13 19:19:31 +02:00
|
|
|
|
|
|
|
namecase = FATCASE_UPPER;
|
2011-07-13 00:38:21 +02:00
|
|
|
#endif
|
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
/* Clear lower case name bit in mask */
|
2011-07-13 19:19:31 +02:00
|
|
|
|
2011-07-14 21:47:31 +02:00
|
|
|
ntlcenable &= ~FATNTRES_LCNAME;
|
2011-07-13 19:19:31 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Is there mixed case in the extension? */
|
|
|
|
|
|
|
|
#ifdef CONFIG_FAT_LFN
|
|
|
|
if (extcase == FATCASE_LOWER)
|
|
|
|
{
|
|
|
|
/* Mixed case in the extension -- use the long file name */
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-13 19:19:31 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
/* So far, only upper case in the extension */
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-13 19:19:31 +02:00
|
|
|
extcase = FATCASE_UPPER;
|
|
|
|
#endif
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-13 19:19:31 +02:00
|
|
|
/* Clear lower case extension in mask */
|
|
|
|
|
2011-07-14 21:47:31 +02:00
|
|
|
ntlcenable &= ~FATNTRES_LCEXT;
|
2011-07-13 19:19:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-13 19:19:31 +02:00
|
|
|
/* Check for lower case characters */
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-13 19:19:31 +02:00
|
|
|
else if (islower(ch))
|
|
|
|
{
|
|
|
|
#if defined(CONFIG_FAT_LFN) && !defined(CONFIG_FAT_LCNAMES)
|
|
|
|
/* If lower case characters are present, then a long file
|
|
|
|
* name will be constructed.
|
|
|
|
*/
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-13 19:19:31 +02:00
|
|
|
goto errout;
|
|
|
|
#else
|
|
|
|
/* Convert the character to upper case */
|
|
|
|
|
|
|
|
ch = toupper(ch);
|
|
|
|
|
|
|
|
/* Some or all of the characters in the name or extension
|
|
|
|
* are lower case. They can be interpreted as lower case if
|
|
|
|
* only if all of the characters in the name or extension are
|
|
|
|
* lower case.
|
|
|
|
*/
|
2011-07-13 00:38:21 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_FAT_LCNAMES
|
2011-07-13 19:19:31 +02:00
|
|
|
if (endndx == 8)
|
|
|
|
{
|
2019-10-27 18:48:14 +01:00
|
|
|
/* Is there mixed case in the name? */
|
2011-07-13 19:19:31 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_FAT_LFN
|
2019-10-27 18:48:14 +01:00
|
|
|
if (namecase == FATCASE_UPPER)
|
|
|
|
{
|
|
|
|
/* Mixed case in the name -- use the long file name */
|
2011-07-13 19:19:31 +02:00
|
|
|
|
2019-10-27 18:48:14 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
2011-07-13 19:19:31 +02:00
|
|
|
|
2019-10-27 18:48:14 +01:00
|
|
|
/* So far, only lower case in the name */
|
2011-07-13 19:19:31 +02:00
|
|
|
|
2019-10-27 18:48:14 +01:00
|
|
|
namecase = FATCASE_LOWER;
|
2011-07-13 19:19:31 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Set lower case name bit */
|
|
|
|
|
|
|
|
ntlcfound |= FATNTRES_LCNAME;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Is there mixed case in the extension? */
|
|
|
|
|
|
|
|
#ifdef CONFIG_FAT_LFN
|
|
|
|
if (extcase == FATCASE_UPPER)
|
2011-07-13 00:38:21 +02:00
|
|
|
{
|
2011-07-13 19:19:31 +02:00
|
|
|
/* Mixed case in the extension -- use the long file name */
|
|
|
|
|
|
|
|
goto errout;
|
2011-07-13 00:38:21 +02:00
|
|
|
}
|
2011-07-13 19:19:31 +02:00
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
/* So far, only lower case in the extension */
|
2011-07-13 19:19:31 +02:00
|
|
|
|
|
|
|
extcase = FATCASE_LOWER;
|
2011-07-13 00:38:21 +02:00
|
|
|
#endif
|
|
|
|
|
2011-07-13 19:19:31 +02:00
|
|
|
/* Set lower case extension bit */
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-14 21:47:31 +02:00
|
|
|
ntlcfound |= FATNTRES_LCEXT;
|
2011-07-13 19:19:31 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif /* CONFIG_FAT_LFN && !CONFIG_FAT_LCNAMES */
|
|
|
|
}
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-13 19:19:31 +02:00
|
|
|
/* Check if the file name exceeds the size permitted (without
|
|
|
|
* long file name support).
|
|
|
|
*/
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-13 19:19:31 +02:00
|
|
|
if (ndx >= endndx)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-13 19:19:31 +02:00
|
|
|
/* Save next character in the accumulated name */
|
|
|
|
|
|
|
|
dirinfo->fd_name[ndx++] = ch;
|
|
|
|
}
|
|
|
|
|
2015-10-04 23:04:00 +02:00
|
|
|
errout:
|
2011-07-13 19:19:31 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: fat_parselfname
|
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description: Convert a user filename into a properly formatted FAT
|
2011-07-13 19:19:31 +02:00
|
|
|
* long filename as it would appear in a directory entry. Here are
|
|
|
|
* the rules for the long file name in the directory:
|
|
|
|
*
|
|
|
|
* Valid characters are the same as for short file names EXCEPT:
|
|
|
|
*
|
|
|
|
* 1. '+', ',', ';', '=', '[', and ']' are accepted in the file name
|
|
|
|
* 2. '.' (dot) can occur more than once in a filename. Extension is
|
|
|
|
* the substring after the last dot.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2011-07-13 19:19:31 +02:00
|
|
|
* OK - The path refers to a valid long file name and has been properly
|
|
|
|
* stored in dirinfo.
|
|
|
|
* <0 - Otherwise an negated error is returned meaning that the string is
|
|
|
|
* not a valid long file name:
|
|
|
|
*
|
|
|
|
* 1. Contains characters not in the printable character set,
|
|
|
|
* 2. Contains forbidden characters
|
|
|
|
* 3. File name is too long.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_FAT_LFN
|
2020-05-31 17:48:21 +02:00
|
|
|
static inline int fat_parselfname(FAR const char **path,
|
|
|
|
FAR struct fat_dirinfo_s *dirinfo,
|
|
|
|
FAR char *terminator)
|
2011-07-13 19:19:31 +02:00
|
|
|
{
|
2020-05-31 17:48:21 +02:00
|
|
|
FAR const char *node = *path;
|
2020-07-26 12:52:22 +02:00
|
|
|
lfnchar ch;
|
2011-07-13 19:19:31 +02:00
|
|
|
int ndx = 0;
|
|
|
|
|
|
|
|
/* Loop until the name is successfully parsed or an error occurs */
|
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
for (; ; )
|
2011-07-13 19:19:31 +02:00
|
|
|
{
|
2020-07-26 12:52:22 +02:00
|
|
|
/* Get the next character from the path */
|
2011-07-13 19:19:31 +02:00
|
|
|
|
2020-07-26 12:52:22 +02:00
|
|
|
# ifdef CONFIG_FAT_LFN_UTF8
|
|
|
|
if (fat_utf8toucs(&node, &ch) != OK)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
# else
|
2011-07-13 19:19:31 +02:00
|
|
|
ch = *node++;
|
2020-07-26 12:52:22 +02:00
|
|
|
# endif
|
2011-07-13 19:19:31 +02:00
|
|
|
|
|
|
|
/* Check if this the last byte in this node of the name */
|
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
if ((ch == '\0' || ch == '/') && ndx != 0)
|
2011-07-13 19:19:31 +02:00
|
|
|
{
|
|
|
|
/* Null terminate the string */
|
|
|
|
|
|
|
|
dirinfo->fd_lfname[ndx] = '\0';
|
|
|
|
|
|
|
|
/* Return the remaining sub-string and the terminating character. */
|
|
|
|
|
2020-07-26 12:52:22 +02:00
|
|
|
*terminator = (char)ch;
|
2011-07-13 19:19:31 +02:00
|
|
|
*path = node;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2011-07-18 02:20:58 +02:00
|
|
|
/* Accept only the printable character set (including space) */
|
2020-07-26 12:52:22 +02:00
|
|
|
# ifdef CONFIG_FAT_LFN_UTF8
|
|
|
|
/* We assume all ucs2 characters printable REVISIT? */
|
2011-07-13 19:19:31 +02:00
|
|
|
|
2020-07-26 12:52:22 +02:00
|
|
|
else if (ch < ' ')
|
|
|
|
# else
|
2011-07-18 02:20:58 +02:00
|
|
|
else if (!isprint(ch))
|
2020-07-26 12:52:22 +02:00
|
|
|
# endif
|
2011-07-13 19:19:31 +02:00
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reject printable characters forbidden by FAT */
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-13 19:19:31 +02:00
|
|
|
else if (ch == '"' || ch == '*' || ch == '/' || ch == ':' ||
|
|
|
|
ch == '<' || ch == '>' || ch == '?' || ch == '\\' ||
|
|
|
|
ch == '|')
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-13 19:19:31 +02:00
|
|
|
/* Check if the file name exceeds the size permitted. */
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-13 19:19:31 +02:00
|
|
|
if (ndx >= LDIR_MAXFNAME)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-13 19:19:31 +02:00
|
|
|
/* Save next character in the accumulated name */
|
|
|
|
|
|
|
|
dirinfo->fd_lfname[ndx++] = ch;
|
|
|
|
}
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2015-10-04 23:04:00 +02:00
|
|
|
errout:
|
2019-10-27 18:48:14 +01:00
|
|
|
dirinfo->fd_lfname[0] = '\0';
|
|
|
|
return -EINVAL;
|
2011-07-13 00:38:21 +02:00
|
|
|
}
|
2011-07-13 19:19:31 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: fat_createalias
|
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description: Given a valid long file name, create a short filename alias.
|
2011-07-13 19:19:31 +02:00
|
|
|
* Here are the rules for creation of the alias:
|
|
|
|
*
|
|
|
|
* 1. All uppercase
|
|
|
|
* 2. All dots except the last deleted
|
|
|
|
* 3. First 6 (uppercase) characters used as a base
|
|
|
|
* 4. Then ~1. The number is increased if the file already exists in the
|
2020-02-23 09:50:23 +01:00
|
|
|
* directory. If the number exceeds >10, then character stripped off the
|
2011-07-13 19:19:31 +02:00
|
|
|
* base.
|
|
|
|
* 5. The extension is the first 3 uppercase chars of extension.
|
|
|
|
*
|
2011-07-14 19:57:59 +02:00
|
|
|
* This function is called only from fat_putlfname()
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2011-07-13 19:19:31 +02:00
|
|
|
* OK - The alias was created correctly.
|
|
|
|
* <0 - Otherwise an negated error is returned.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_FAT_LFN
|
2020-05-31 17:48:21 +02:00
|
|
|
static inline int fat_createalias(FAR struct fat_dirinfo_s *dirinfo)
|
2011-07-13 19:19:31 +02:00
|
|
|
{
|
2024-08-25 01:21:12 +02:00
|
|
|
uint8_t ch; /* Current character being processed */
|
|
|
|
FAR lfnchar *ext; /* Pointer to the extension substring */
|
|
|
|
FAR lfnchar *src; /* Pointer to the long file name source */
|
|
|
|
int len; /* Total length of the long file name */
|
|
|
|
int namechars; /* Number of characters available in long name */
|
|
|
|
int extchars; /* Number of characters available in long name extension */
|
|
|
|
int endndx; /* Maximum index into the short name array */
|
|
|
|
int ndx; /* Index to store next character */
|
2011-07-13 19:19:31 +02:00
|
|
|
|
|
|
|
/* First, let's decide what is name and what is extension */
|
|
|
|
|
2020-07-26 12:52:22 +02:00
|
|
|
for (len = 0, ext = NULL; dirinfo->fd_lfname[len] != '\0'; len++)
|
|
|
|
{
|
|
|
|
if (dirinfo->fd_lfname[len] == '.')
|
|
|
|
{
|
|
|
|
ext = &dirinfo->fd_lfname[len];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-13 19:19:31 +02:00
|
|
|
if (ext)
|
|
|
|
{
|
|
|
|
ptrdiff_t tmp;
|
|
|
|
|
|
|
|
/* ext points to the final '.'. The difference in bytes from the
|
|
|
|
* beginning of the string is then the name length.
|
|
|
|
*/
|
|
|
|
|
2020-07-26 12:52:22 +02:00
|
|
|
tmp = ext - (FAR lfnchar *)dirinfo->fd_lfname;
|
2011-07-13 19:19:31 +02:00
|
|
|
namechars = tmp;
|
|
|
|
|
2015-03-15 19:22:12 +01:00
|
|
|
/* And the rest, excluding the '.' is the extension. */
|
2011-07-13 19:19:31 +02:00
|
|
|
|
|
|
|
extchars = len - namechars - 1;
|
|
|
|
ext++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No '.' found. It is all name and no extension. */
|
|
|
|
|
|
|
|
namechars = len;
|
|
|
|
extchars = 0;
|
|
|
|
}
|
|
|
|
|
2015-03-15 19:22:12 +01:00
|
|
|
#ifdef CONFIG_FAT_LCNAMES
|
2011-07-13 19:19:31 +02:00
|
|
|
/* Alias are always all upper case */
|
|
|
|
|
|
|
|
dirinfo->fd_ntflags = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Initialized the short name with all spaces */
|
|
|
|
|
|
|
|
memset(dirinfo->fd_name, ' ', DIR_MAXFNAME);
|
2013-03-31 15:06:22 +02:00
|
|
|
|
2011-07-13 19:19:31 +02:00
|
|
|
/* Handle a special case where there is no name. Windows seems to use
|
2015-03-15 19:22:12 +01:00
|
|
|
* the extension plus random stuff then ~1 to pad to 8 bytes. Some
|
2011-07-13 19:19:31 +02:00
|
|
|
* examples:
|
|
|
|
*
|
|
|
|
* a.b -> a.b No long name
|
|
|
|
* a., -> A26BE~1._ Padded name to make unique, _ replaces ,
|
|
|
|
* .b -> B1DD2~1 Extension used as name
|
|
|
|
* .bbbbbbb -> BBBBBB~1 Extension used as name
|
|
|
|
* a.bbbbbbb -> AAD39~1.BBB Padded name to make unique.
|
|
|
|
* aaa.bbbbbbb -> AAA~1.BBBB Not padded, already unique?
|
|
|
|
* ,.bbbbbbb -> _82AF~1.BBB _ replaces ,
|
|
|
|
* +[],.bbbbbbb -> ____~1.BBB _ replaces +[],
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (namechars < 1)
|
|
|
|
{
|
2019-10-27 18:48:14 +01:00
|
|
|
/* Use the extension as the name */
|
2011-07-13 19:19:31 +02:00
|
|
|
|
2019-10-27 18:48:14 +01:00
|
|
|
DEBUGASSERT(ext && extchars > 0);
|
|
|
|
src = ext;
|
|
|
|
ext = NULL;
|
|
|
|
namechars = extchars;
|
|
|
|
extchars = 0;
|
2011-07-13 19:19:31 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-26 12:52:22 +02:00
|
|
|
src = (FAR lfnchar *)dirinfo->fd_lfname;
|
2011-07-13 19:19:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Then copy the name and extension, handling upper case conversions and
|
|
|
|
* excluding forbidden characters.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ndx = 0; /* Position to write the next name character */
|
|
|
|
endndx = 6; /* Maximum index before we write ~! and switch to the extension */
|
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
for (; ; )
|
2011-07-13 19:19:31 +02:00
|
|
|
{
|
|
|
|
/* Get the next byte from the path. Break out of the loop if we
|
|
|
|
* encounter the end of null-terminated the long file name string.
|
|
|
|
*/
|
|
|
|
|
2020-07-26 12:52:22 +02:00
|
|
|
# ifdef CONFIG_FAT_LFN_UTF8
|
|
|
|
/* Make sure ch is within printable characters */
|
|
|
|
|
|
|
|
if (*src > 0x7f)
|
|
|
|
{
|
|
|
|
ch = (uint8_t)(*src++ & 0x1f) + 'A';
|
|
|
|
if (ch >= '[')
|
|
|
|
{
|
|
|
|
ch -= ('[' - '0');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ch = *src++;
|
|
|
|
}
|
|
|
|
# else
|
2011-07-14 19:57:59 +02:00
|
|
|
ch = *src++;
|
2020-07-26 12:52:22 +02:00
|
|
|
# endif
|
|
|
|
|
2011-07-13 19:19:31 +02:00
|
|
|
if (ch == '\0')
|
|
|
|
{
|
2011-07-14 23:07:59 +02:00
|
|
|
/* This is the end of the source string. Do we need to add ~1. We
|
2015-03-15 19:22:12 +01:00
|
|
|
* will do that if we were parsing the name part when the end of
|
2011-07-14 23:07:59 +02:00
|
|
|
* string was encountered.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (endndx == 6)
|
|
|
|
{
|
|
|
|
/* Write the ~1 at the end of the name */
|
|
|
|
|
|
|
|
dirinfo->fd_name[ndx++] = '~';
|
|
|
|
dirinfo->fd_name[ndx] = '1';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* In any event, we are done */
|
|
|
|
|
|
|
|
return OK;
|
2011-07-13 19:19:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Exclude those few characters included in long file names, but
|
|
|
|
* excluded in short file name: '+', ',', ';', '=', '[', ']', and '.'
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ch == '+' || ch == ',' || ch == '.' || ch == ';' ||
|
2020-07-26 12:52:22 +02:00
|
|
|
ch == '=' || ch == '[' || ch == ']' || ch == '|' || ch == ' ')
|
2011-07-13 19:19:31 +02:00
|
|
|
{
|
|
|
|
/* Use the underbar character instead */
|
|
|
|
|
|
|
|
ch = '_';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle lower case characters */
|
|
|
|
|
|
|
|
ch = toupper(ch);
|
2013-03-31 15:06:22 +02:00
|
|
|
|
2011-07-13 19:19:31 +02:00
|
|
|
/* We now have a valid character to add to the name or extension. */
|
|
|
|
|
|
|
|
dirinfo->fd_name[ndx++] = ch;
|
|
|
|
|
|
|
|
/* Did we just add a character to the name? */
|
|
|
|
|
|
|
|
if (endndx == 6)
|
|
|
|
{
|
|
|
|
/* Decrement the number of characters available in the name
|
|
|
|
* portion of the long name.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namechars--;
|
|
|
|
|
2011-07-14 23:07:59 +02:00
|
|
|
/* Is it time to add ~1 to the string? We will do that if
|
2011-07-13 19:19:31 +02:00
|
|
|
* either (1) we have already added the maximum number of
|
|
|
|
* characters to the short name, or (2) if there are no further
|
|
|
|
* characters available in the name portion of the long name.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (namechars < 1 || ndx == 6)
|
|
|
|
{
|
|
|
|
/* Write the ~1 at the end of the name */
|
|
|
|
|
|
|
|
dirinfo->fd_name[ndx++] = '~';
|
2011-07-14 23:07:59 +02:00
|
|
|
dirinfo->fd_name[ndx] = '1';
|
2011-07-13 19:19:31 +02:00
|
|
|
|
|
|
|
/* Then switch to the extension (if there is one) */
|
|
|
|
|
|
|
|
if (!ext || extchars < 1)
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
ndx = 8;
|
|
|
|
endndx = 11;
|
2011-07-14 19:57:59 +02:00
|
|
|
src = ext;
|
2011-07-13 19:19:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No.. we just added a character to the extension */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Decrement the number of characters available in the name
|
|
|
|
* portion of the long name
|
|
|
|
*/
|
|
|
|
|
|
|
|
extchars--;
|
|
|
|
|
|
|
|
/* Is the extension complete? */
|
|
|
|
|
|
|
|
if (extchars < 1 || ndx == 11)
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
}
|
2018-11-09 14:46:16 +01:00
|
|
|
|
|
|
|
#if defined(CONFIG_FAT_LFN_ALIAS_TRAILCHARS) && CONFIG_FAT_LFN_ALIAS_TRAILCHARS > 0
|
|
|
|
/* Take first 6-N characters from beginning of filename and last N
|
|
|
|
* characters from end of the filename. Useful for filenames like
|
2018-11-09 14:51:43 +01:00
|
|
|
* "datafile123.txt".
|
|
|
|
*/
|
|
|
|
|
2018-11-09 14:46:16 +01:00
|
|
|
if (ndx == 6 - CONFIG_FAT_LFN_ALIAS_TRAILCHARS
|
|
|
|
&& namechars > CONFIG_FAT_LFN_ALIAS_TRAILCHARS)
|
|
|
|
{
|
|
|
|
src += namechars - CONFIG_FAT_LFN_ALIAS_TRAILCHARS;
|
|
|
|
}
|
|
|
|
#endif
|
2011-07-13 19:19:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-07-18 20:28:02 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: fat_findalias
|
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description: Make sure that the short alias for the long file name is
|
2011-07-18 20:28:02 +02:00
|
|
|
* unique, ie., that there is no other
|
|
|
|
*
|
|
|
|
* NOTE: This function does not restore the directory entry that was in the
|
|
|
|
* sector cache
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2011-07-18 20:28:02 +02:00
|
|
|
* OK - The alias is unique.
|
|
|
|
* <0 - Otherwise an negated error is returned.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_FAT_LFN
|
2020-05-31 17:48:21 +02:00
|
|
|
static inline int fat_findalias(FAR struct fat_mountpt_s *fs,
|
|
|
|
FAR struct fat_dirinfo_s *dirinfo)
|
2011-07-18 20:28:02 +02:00
|
|
|
{
|
|
|
|
struct fat_dirinfo_s tmpinfo;
|
|
|
|
|
|
|
|
/* Save the current directory info. */
|
|
|
|
|
|
|
|
memcpy(&tmpinfo, dirinfo, sizeof(struct fat_dirinfo_s));
|
|
|
|
|
2011-11-19 21:30:39 +01:00
|
|
|
/* Then re-initialize to the beginning of the current directory, starting
|
|
|
|
* with the first entry.
|
2011-07-18 20:28:02 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
tmpinfo.dir.fd_startcluster = tmpinfo.dir.fd_currcluster;
|
|
|
|
tmpinfo.dir.fd_currsector = tmpinfo.fd_seq.ds_startsector;
|
2011-11-19 21:30:39 +01:00
|
|
|
tmpinfo.dir.fd_index = 0;
|
2011-07-18 20:28:02 +02:00
|
|
|
|
2020-05-31 17:48:21 +02:00
|
|
|
/* Search for the single short file name directory entry in this
|
|
|
|
* directory.
|
|
|
|
*/
|
2011-07-18 20:28:02 +02:00
|
|
|
|
|
|
|
return fat_findsfnentry(fs, &tmpinfo);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-07-13 19:19:31 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: fat_uniquealias
|
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description: Make sure that the short alias for the long file name is
|
2011-07-18 20:28:02 +02:00
|
|
|
* unique, modifying the alias as necessary to assure uniqueness.
|
|
|
|
*
|
|
|
|
* NOTE: This function does not restore the directory entry that was in the
|
|
|
|
* sector cache
|
2011-07-13 19:19:31 +02:00
|
|
|
*
|
2011-07-18 20:28:02 +02:00
|
|
|
* information upon return.
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2011-07-13 19:19:31 +02:00
|
|
|
* OK - The alias is unique.
|
|
|
|
* <0 - Otherwise an negated error is returned.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_FAT_LFN
|
2020-05-31 17:48:21 +02:00
|
|
|
static inline int fat_uniquealias(FAR struct fat_mountpt_s *fs,
|
|
|
|
FAR struct fat_dirinfo_s *dirinfo)
|
2011-07-13 19:19:31 +02:00
|
|
|
{
|
2011-07-18 20:28:02 +02:00
|
|
|
int tilde;
|
|
|
|
int lsdigit;
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Find the position of the tilde character in the short name. The tilde
|
|
|
|
* can not occur in positions 0 or 7:
|
|
|
|
*/
|
|
|
|
|
2020-07-26 12:52:22 +02:00
|
|
|
for (tilde = 1; tilde < 7 && dirinfo->fd_name[tilde] != '~'; tilde++)
|
|
|
|
{
|
|
|
|
/* Empty */
|
|
|
|
}
|
|
|
|
|
2011-07-18 20:28:02 +02:00
|
|
|
if (tilde >= 7)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The least significant number follows the digit (and must be '1') */
|
|
|
|
|
|
|
|
lsdigit = tilde + 1;
|
|
|
|
DEBUGASSERT(dirinfo->fd_name[lsdigit] == '1');
|
|
|
|
|
2018-11-09 14:46:16 +01:00
|
|
|
#ifdef CONFIG_FAT_LFN_ALIAS_HASH
|
2018-11-09 14:51:43 +01:00
|
|
|
/* Add a hash of the long filename to the short filename, to reduce
|
|
|
|
* collisions.
|
|
|
|
*/
|
|
|
|
|
2018-11-09 14:46:16 +01:00
|
|
|
if ((ret = fat_findalias(fs, dirinfo)) == OK)
|
|
|
|
{
|
|
|
|
uint16_t hash = dirinfo->fd_seq.ds_offset;
|
2018-11-09 14:51:43 +01:00
|
|
|
|
2018-11-09 14:46:16 +01:00
|
|
|
for (i = 0; dirinfo->fd_lfname[i] != '\0'; i++)
|
|
|
|
{
|
|
|
|
hash = ((hash << 5) + hash) ^ dirinfo->fd_lfname[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < tilde - 2; i++)
|
|
|
|
{
|
2019-10-27 18:48:14 +01:00
|
|
|
uint8_t nibble = (hash >> (i * 4)) & 0x0f;
|
2020-05-31 17:48:21 +02:00
|
|
|
FAR const char *digits = "0123456789ABCDEF";
|
|
|
|
|
2018-11-09 14:46:16 +01:00
|
|
|
dirinfo->fd_name[tilde - 1 - i] = digits[nibble];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (ret == -ENOENT)
|
|
|
|
{
|
|
|
|
return OK; /* Alias was unique already */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return ret; /* Other error */
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-05-31 17:48:21 +02:00
|
|
|
/* Search for the single short file name directory entry in this
|
|
|
|
* directory.
|
|
|
|
*/
|
2011-07-18 20:28:02 +02:00
|
|
|
|
|
|
|
while ((ret = fat_findalias(fs, dirinfo)) == OK)
|
|
|
|
{
|
2020-05-31 17:48:21 +02:00
|
|
|
/* Adjust the numeric value after the '~' to make the file name
|
|
|
|
* unique.
|
|
|
|
*/
|
2011-07-18 20:28:02 +02:00
|
|
|
|
|
|
|
for (i = lsdigit; i > 0; i--)
|
|
|
|
{
|
|
|
|
/* If we have backed up to the tilde position, then we have to move
|
|
|
|
* the tilde back one position.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (i == tilde)
|
|
|
|
{
|
|
|
|
/* Is there space to back up the tilde? */
|
|
|
|
|
|
|
|
if (tilde <= 1)
|
|
|
|
{
|
|
|
|
/* No.. then we cannot add the name to the directory.
|
|
|
|
* What is the likelihood of that happening?
|
|
|
|
*/
|
|
|
|
|
|
|
|
return -ENOSPC;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Back up the tilde and break out of the inner loop */
|
|
|
|
|
|
|
|
tilde--;
|
2019-03-01 22:00:00 +01:00
|
|
|
dirinfo->fd_name[tilde] = '~';
|
|
|
|
dirinfo->fd_name[tilde + 1] = '1';
|
2011-07-18 20:28:02 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We are not yet at the tilde,. Check if this digit has already
|
|
|
|
* reached its maximum value.
|
|
|
|
*/
|
|
|
|
|
|
|
|
else if (dirinfo->fd_name[i] < '9')
|
|
|
|
{
|
2020-05-31 17:48:21 +02:00
|
|
|
/* No, it has not.. just increment the LS digit and break out
|
|
|
|
* of the inner loop.
|
2011-07-18 20:28:02 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
dirinfo->fd_name[i]++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Yes.. Reset the digit to '0' and loop to adjust the digit before
|
|
|
|
* this one.
|
|
|
|
*/
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dirinfo->fd_name[i] = '0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The while loop terminated because of an error; fat_findalias()
|
|
|
|
* returned something other than OK. The only acceptable error is
|
|
|
|
* -ENOENT, meaning that the short file name directory does not
|
|
|
|
* exist in this directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ret == -ENOENT)
|
|
|
|
{
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2011-07-13 19:19:31 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: fat_path2dirname
|
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description: Convert a user filename into a properly formatted FAT
|
2011-07-13 19:19:31 +02:00
|
|
|
* (short 8.3) filename as it would appear in a directory entry.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-31 17:48:21 +02:00
|
|
|
static int fat_path2dirname(FAR const char **path,
|
|
|
|
FAR struct fat_dirinfo_s *dirinfo,
|
|
|
|
FAR char *terminator)
|
2011-07-13 19:19:31 +02:00
|
|
|
{
|
|
|
|
#ifdef CONFIG_FAT_LFN
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Assume no long file name */
|
|
|
|
|
|
|
|
dirinfo->fd_lfname[0] = '\0';
|
|
|
|
|
|
|
|
/* Then parse the (assumed) 8+3 short file name */
|
|
|
|
|
|
|
|
ret = fat_parsesfname(path, dirinfo, terminator);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* No, the name is not a valid short 8+3 file name. Try parsing
|
|
|
|
* the long file name.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = fat_parselfname(path, dirinfo, terminator);
|
2011-07-13 23:53:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
#else
|
|
|
|
/* Only short, 8+3 filenames supported */
|
|
|
|
|
|
|
|
return fat_parsesfname(path, dirinfo, terminator);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: fat_findsfnentry
|
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description: Find a short file name directory entry. Returns OK if the
|
2011-07-18 20:28:02 +02:00
|
|
|
* directory exists; -ENOENT if it does not.
|
2011-07-13 23:53:06 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-31 17:48:21 +02:00
|
|
|
static int fat_findsfnentry(FAR struct fat_mountpt_s *fs,
|
|
|
|
FAR struct fat_dirinfo_s *dirinfo)
|
2011-07-13 23:53:06 +02:00
|
|
|
{
|
|
|
|
uint16_t diroffset;
|
2020-05-31 17:48:21 +02:00
|
|
|
FAR uint8_t *direntry;
|
2011-07-18 20:28:02 +02:00
|
|
|
#ifdef CONFIG_FAT_LFN
|
2022-09-06 08:18:45 +02:00
|
|
|
off_t startsector;
|
2011-07-18 20:28:02 +02:00
|
|
|
#endif
|
2022-09-06 08:18:45 +02:00
|
|
|
int ret;
|
2011-07-13 23:53:06 +02:00
|
|
|
|
2011-07-18 20:28:02 +02:00
|
|
|
/* Save the starting sector of the directory. This is not really needed
|
|
|
|
* for short name entries, but this keeps things consistent with long
|
|
|
|
* file name entries..
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_FAT_LFN
|
|
|
|
startsector = dirinfo->dir.fd_currsector;
|
|
|
|
#endif
|
|
|
|
|
2011-07-14 19:57:59 +02:00
|
|
|
/* Search, beginning with the current sector, for a directory entry with
|
|
|
|
* the matching short name
|
2011-07-13 23:53:06 +02:00
|
|
|
*/
|
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
for (; ; )
|
2011-07-13 23:53:06 +02:00
|
|
|
{
|
|
|
|
/* Read the next sector into memory */
|
|
|
|
|
|
|
|
ret = fat_fscacheread(fs, dirinfo->dir.fd_currsector);
|
2011-07-13 19:19:31 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2011-07-13 23:53:06 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a pointer to the directory entry */
|
|
|
|
|
|
|
|
diroffset = DIRSEC_BYTENDX(fs, dirinfo->dir.fd_index);
|
|
|
|
direntry = &fs->fs_buffer[diroffset];
|
|
|
|
|
|
|
|
/* Check if we are at the end of the directory */
|
|
|
|
|
|
|
|
if (direntry[DIR_NAME] == DIR0_ALLEMPTY)
|
|
|
|
{
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we have found the directory entry that we are looking for */
|
|
|
|
|
|
|
|
if (direntry[DIR_NAME] != DIR0_EMPTY &&
|
|
|
|
!(DIR_GETATTRIBUTES(direntry) & FATATTR_VOLUMEID) &&
|
2015-10-11 19:39:29 +02:00
|
|
|
!memcmp(&direntry[DIR_NAME], dirinfo->fd_name, DIR_MAXFNAME))
|
2011-07-13 23:53:06 +02:00
|
|
|
{
|
|
|
|
/* Yes.. Return success */
|
|
|
|
|
2011-07-18 20:28:02 +02:00
|
|
|
dirinfo->fd_seq.ds_sector = fs->fs_currentsector;
|
|
|
|
dirinfo->fd_seq.ds_offset = diroffset;
|
2011-07-13 23:53:06 +02:00
|
|
|
#ifdef CONFIG_FAT_LFN
|
2011-07-18 20:28:02 +02:00
|
|
|
dirinfo->fd_seq.ds_cluster = dirinfo->dir.fd_currcluster;
|
|
|
|
dirinfo->fd_seq.ds_startsector = startsector;
|
2011-07-13 23:53:06 +02:00
|
|
|
|
|
|
|
/* Position the last long file name directory entry at the same
|
|
|
|
* position.
|
|
|
|
*/
|
|
|
|
|
2011-07-18 20:28:02 +02:00
|
|
|
dirinfo->fd_seq.ds_lfnsector = dirinfo->fd_seq.ds_sector;
|
|
|
|
dirinfo->fd_seq.ds_lfnoffset = dirinfo->fd_seq.ds_offset;
|
|
|
|
dirinfo->fd_seq.ds_lfncluster = dirinfo->fd_seq.ds_cluster;
|
2011-07-13 23:53:06 +02:00
|
|
|
#endif
|
2019-10-27 18:48:14 +01:00
|
|
|
return OK;
|
2011-07-13 23:53:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* No... get the next directory index and try again */
|
|
|
|
|
|
|
|
if (fat_nextdirentry(fs, &dirinfo->dir) != OK)
|
|
|
|
{
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: fat_cmplfnchunk
|
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description: There are 13 characters per LFN entry, broken up into three
|
2020-02-23 09:50:23 +01:00
|
|
|
* chunks for characters 1-5, 6-11, and 12-13. This function will perform
|
2011-07-13 23:53:06 +02:00
|
|
|
* the comparison of a single chunk.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_FAT_LFN
|
2020-07-26 12:52:22 +02:00
|
|
|
static bool fat_cmplfnchunk(FAR uint8_t *chunk, FAR const lfnchar *substr,
|
2020-05-31 17:48:21 +02:00
|
|
|
int nchunk)
|
2011-07-13 23:53:06 +02:00
|
|
|
{
|
2021-12-10 02:40:30 +01:00
|
|
|
uint16_t wch;
|
2020-07-26 12:52:22 +02:00
|
|
|
lfnchar ch;
|
2011-07-15 17:37:29 +02:00
|
|
|
int i;
|
2011-07-13 23:53:06 +02:00
|
|
|
|
|
|
|
/* Check bytes 1-nchunk */
|
|
|
|
|
|
|
|
for (i = 0; i < nchunk; i++)
|
|
|
|
{
|
2011-07-18 02:20:58 +02:00
|
|
|
/* Get the next character from the name string (which might be the NUL
|
|
|
|
* terminating character).
|
2011-07-13 23:53:06 +02:00
|
|
|
*/
|
|
|
|
|
2011-07-18 02:20:58 +02:00
|
|
|
if (*substr == '\0')
|
2011-07-13 23:53:06 +02:00
|
|
|
{
|
2011-07-18 02:20:58 +02:00
|
|
|
ch = '\0';
|
2011-07-15 17:37:29 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ch = *substr++;
|
2011-07-13 23:53:06 +02:00
|
|
|
}
|
|
|
|
|
2011-07-15 17:37:29 +02:00
|
|
|
/* Get the next unicode character from the chunk. We only handle
|
|
|
|
* ASCII. For ASCII, the upper byte should be zero and the lower
|
|
|
|
* should match the ASCII code.
|
2011-07-13 23:53:06 +02:00
|
|
|
*/
|
|
|
|
|
2021-12-10 02:40:30 +01:00
|
|
|
wch = fat_getuint16((FAR uint8_t *)chunk);
|
2020-07-26 12:52:22 +02:00
|
|
|
# ifdef CONFIG_FAT_LFN_UTF8
|
|
|
|
if (wch != ch)
|
|
|
|
# else
|
2021-12-10 02:40:30 +01:00
|
|
|
if ((wch & 0xff) != (uint16_t)ch)
|
2020-07-26 12:52:22 +02:00
|
|
|
# endif
|
2011-07-13 23:53:06 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-07-18 02:20:58 +02:00
|
|
|
/* The characters match. If we just matched the NUL terminating
|
|
|
|
* character, then the strings match and we are finished.
|
2011-07-15 17:37:29 +02:00
|
|
|
*/
|
2011-07-13 23:53:06 +02:00
|
|
|
|
2011-07-18 02:20:58 +02:00
|
|
|
if (ch == '\0')
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try the next character from the directory entry. */
|
|
|
|
|
2021-12-10 02:40:30 +01:00
|
|
|
chunk += sizeof(uint16_t);
|
2011-07-13 23:53:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* All of the characters in the chunk match.. Return success */
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: fat_cmplfname
|
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description: Given an LFN directory entry, compare a substring of the name
|
2011-07-13 23:53:06 +02:00
|
|
|
* to a portion in the directory entry.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_FAT_LFN
|
2020-05-31 17:48:21 +02:00
|
|
|
static bool fat_cmplfname(FAR const uint8_t *direntry,
|
2020-07-26 12:52:22 +02:00
|
|
|
FAR const lfnchar *substr)
|
2011-07-13 23:53:06 +02:00
|
|
|
{
|
2020-05-31 17:48:21 +02:00
|
|
|
FAR uint8_t *chunk;
|
2011-07-15 17:37:29 +02:00
|
|
|
int len;
|
2011-07-18 02:20:58 +02:00
|
|
|
bool match;
|
2011-07-15 17:37:29 +02:00
|
|
|
|
2011-09-28 14:32:06 +02:00
|
|
|
/* How much of string do we have to compare? (including the NUL
|
|
|
|
* terminator).
|
|
|
|
*/
|
2011-07-15 17:37:29 +02:00
|
|
|
|
2020-07-26 12:52:22 +02:00
|
|
|
for (len = 1; substr[len - 1] != '\0'; len++)
|
|
|
|
{
|
|
|
|
/* Empty */
|
|
|
|
}
|
2011-07-13 23:53:06 +02:00
|
|
|
|
|
|
|
/* Check bytes 1-5 */
|
|
|
|
|
|
|
|
chunk = LDIR_PTRWCHAR1_5(direntry);
|
2011-07-18 02:20:58 +02:00
|
|
|
match = fat_cmplfnchunk(chunk, substr, 5);
|
2011-09-28 14:32:06 +02:00
|
|
|
if (match && len > 5)
|
2011-07-13 23:53:06 +02:00
|
|
|
{
|
2011-09-28 14:32:06 +02:00
|
|
|
/* Check bytes 6-11 */
|
2011-07-15 17:37:29 +02:00
|
|
|
|
2011-07-18 03:47:48 +02:00
|
|
|
chunk = LDIR_PTRWCHAR6_11(direntry);
|
|
|
|
match = fat_cmplfnchunk(chunk, &substr[5], 6);
|
2011-09-28 14:32:06 +02:00
|
|
|
if (match && len > 11)
|
2011-07-13 23:53:06 +02:00
|
|
|
{
|
2011-09-28 14:32:06 +02:00
|
|
|
/* Check bytes 12-13 */
|
2011-07-15 17:37:29 +02:00
|
|
|
|
2011-07-18 03:47:48 +02:00
|
|
|
chunk = LDIR_PTRWCHAR12_13(direntry);
|
|
|
|
match = fat_cmplfnchunk(chunk, &substr[11], 2);
|
2011-07-13 23:53:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-18 02:20:58 +02:00
|
|
|
return match;
|
2011-07-13 23:53:06 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: fat_findlfnentry
|
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description: Find a sequence of long file name directory entries.
|
2011-07-13 23:53:06 +02:00
|
|
|
*
|
2011-07-14 19:57:59 +02:00
|
|
|
* NOTE: As a side effect, this function returns with the sector containing
|
|
|
|
* the short file name directory entry in the cache.
|
|
|
|
*
|
2011-07-13 23:53:06 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_FAT_LFN
|
2020-05-31 17:48:21 +02:00
|
|
|
static inline int fat_findlfnentry(FAR struct fat_mountpt_s *fs,
|
|
|
|
FAR struct fat_dirinfo_s *dirinfo)
|
2011-07-13 23:53:06 +02:00
|
|
|
{
|
2020-05-31 17:48:21 +02:00
|
|
|
FAR uint8_t *direntry;
|
2011-07-13 23:53:06 +02:00
|
|
|
uint16_t diroffset;
|
|
|
|
uint8_t lastseq;
|
|
|
|
uint8_t seqno;
|
|
|
|
uint8_t nfullentries;
|
|
|
|
uint8_t nentries;
|
|
|
|
uint8_t remainder;
|
2011-07-18 02:20:58 +02:00
|
|
|
uint8_t checksum = 0;
|
2011-07-18 20:28:02 +02:00
|
|
|
off_t startsector;
|
2011-07-13 23:53:06 +02:00
|
|
|
int offset;
|
|
|
|
int namelen;
|
|
|
|
int ret;
|
|
|
|
|
2011-07-14 19:57:59 +02:00
|
|
|
/* Get the length of the long file name (size of the fd_lfname array is
|
|
|
|
* LDIR_MAXFNAME+1 we do not have to check the length of the string).
|
2011-07-13 23:53:06 +02:00
|
|
|
*/
|
|
|
|
|
2020-07-26 12:52:22 +02:00
|
|
|
for (namelen = 0; dirinfo->fd_lfname[namelen] != '\0'; namelen++)
|
|
|
|
{
|
|
|
|
/* Empty */
|
|
|
|
}
|
|
|
|
|
2019-03-01 22:00:00 +01:00
|
|
|
DEBUGASSERT(namelen <= LDIR_MAXFNAME + 1);
|
2011-07-13 23:53:06 +02:00
|
|
|
|
|
|
|
/* How many LFN directory entries are we expecting? */
|
|
|
|
|
|
|
|
nfullentries = namelen / LDIR_MAXLFNCHARS;
|
|
|
|
remainder = namelen - nfullentries * LDIR_MAXLFNCHARS;
|
|
|
|
nentries = nfullentries;
|
|
|
|
if (remainder > 0)
|
|
|
|
{
|
|
|
|
nentries++;
|
|
|
|
}
|
2015-03-16 15:49:39 +01:00
|
|
|
|
2011-07-13 23:53:06 +02:00
|
|
|
DEBUGASSERT(nentries > 0 && nentries <= LDIR_MAXLFNS);
|
|
|
|
|
2015-03-16 15:49:39 +01:00
|
|
|
/* This is the first sequence number we are looking for, the sequence
|
2011-07-13 23:53:06 +02:00
|
|
|
* number of the last LFN entry (remember that they appear in reverse
|
|
|
|
* order.. from last to first).
|
|
|
|
*/
|
2011-07-13 19:19:31 +02:00
|
|
|
|
2011-07-15 01:29:43 +02:00
|
|
|
lastseq = LDIR0_LAST | nentries;
|
2011-07-13 23:53:06 +02:00
|
|
|
seqno = lastseq;
|
|
|
|
|
2011-07-18 20:28:02 +02:00
|
|
|
/* Save the starting sector of the directory. This is needed later to
|
|
|
|
* re-scan the directory, looking duplicate short alias names.
|
|
|
|
*/
|
|
|
|
|
|
|
|
startsector = dirinfo->dir.fd_currsector;
|
|
|
|
|
2011-07-13 23:53:06 +02:00
|
|
|
/* Search, beginning with the current sector, for a directory entry this
|
|
|
|
* the match shore name
|
|
|
|
*/
|
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
for (; ; )
|
2011-07-13 23:53:06 +02:00
|
|
|
{
|
|
|
|
/* Read the next sector into memory */
|
|
|
|
|
|
|
|
ret = fat_fscacheread(fs, dirinfo->dir.fd_currsector);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2011-07-13 19:19:31 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-07-13 23:53:06 +02:00
|
|
|
/* Get a pointer to the directory entry */
|
|
|
|
|
|
|
|
diroffset = DIRSEC_BYTENDX(fs, dirinfo->dir.fd_index);
|
|
|
|
direntry = &fs->fs_buffer[diroffset];
|
|
|
|
|
|
|
|
/* Check if we are at the end of the directory */
|
|
|
|
|
|
|
|
if (direntry[DIR_NAME] == DIR0_ALLEMPTY)
|
|
|
|
{
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is this an LFN entry? Does it have the sequence number we are
|
|
|
|
* looking for?
|
|
|
|
*/
|
|
|
|
|
2011-07-14 19:57:59 +02:00
|
|
|
if (LDIR_GETATTRIBUTES(direntry) != LDDIR_LFNATTR ||
|
2011-07-14 23:07:59 +02:00
|
|
|
LDIR_GETSEQ(direntry) != seqno)
|
2011-07-13 23:53:06 +02:00
|
|
|
{
|
|
|
|
/* No, restart the search at the next entry */
|
|
|
|
|
|
|
|
seqno = lastseq;
|
2011-07-14 23:07:59 +02:00
|
|
|
goto next_entry;
|
2011-07-13 23:53:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Yes.. If this is not the "last" LFN entry, then the checksum must
|
|
|
|
* also be the same.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (seqno == lastseq)
|
|
|
|
{
|
|
|
|
/* Just save the checksum for subsequent checks */
|
|
|
|
|
2011-07-14 19:57:59 +02:00
|
|
|
checksum = LDIR_GETCHECKSUM(direntry);
|
2011-07-13 23:53:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Not the first entry in the sequence. Does the checksum match the
|
|
|
|
* previous sequences?
|
|
|
|
*/
|
|
|
|
|
2011-07-14 19:57:59 +02:00
|
|
|
else if (checksum != LDIR_GETCHECKSUM(direntry))
|
2011-07-13 23:53:06 +02:00
|
|
|
{
|
|
|
|
/* No, restart the search at the next entry */
|
|
|
|
|
|
|
|
seqno = lastseq;
|
2011-07-14 23:07:59 +02:00
|
|
|
goto next_entry;
|
2011-07-13 23:53:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the name substring in this LFN matches the corresponding
|
|
|
|
* substring of the name we are looking for.
|
2011-07-13 19:19:31 +02:00
|
|
|
*/
|
|
|
|
|
2011-07-13 23:53:06 +02:00
|
|
|
offset = ((seqno & LDIR0_SEQ_MASK) - 1) * LDIR_MAXLFNCHARS;
|
2011-07-14 23:07:59 +02:00
|
|
|
if (fat_cmplfname(direntry, &dirinfo->fd_lfname[offset]))
|
2011-07-13 23:53:06 +02:00
|
|
|
{
|
|
|
|
/* Yes.. it matches. Check the sequence number. Is this the
|
|
|
|
* "last" LFN entry (i.e., the one that appears first)?
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (seqno == lastseq)
|
|
|
|
{
|
|
|
|
/* Yes.. Save information about this LFN entry position */
|
|
|
|
|
2011-07-18 20:28:02 +02:00
|
|
|
dirinfo->fd_seq.ds_lfnsector = fs->fs_currentsector;
|
|
|
|
dirinfo->fd_seq.ds_lfnoffset = diroffset;
|
|
|
|
dirinfo->fd_seq.ds_lfncluster = dirinfo->dir.fd_currcluster;
|
|
|
|
dirinfo->fd_seq.ds_startsector = startsector;
|
2011-07-13 23:53:06 +02:00
|
|
|
seqno &= LDIR0_SEQ_MASK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is this the first sequence number (i.e., the LFN entry that
|
|
|
|
* will appear last)?
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (seqno == 1)
|
|
|
|
{
|
|
|
|
/* We have found all of the LFN entries. The next directory
|
|
|
|
* entry should be the one containing the short file name
|
|
|
|
* alias and all of the meat about the file or directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (fat_nextdirentry(fs, &dirinfo->dir) != OK)
|
|
|
|
{
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
2011-07-15 17:37:29 +02:00
|
|
|
/* Make sure that the directory entry is in the sector cache */
|
|
|
|
|
|
|
|
ret = fat_fscacheread(fs, dirinfo->dir.fd_currsector);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a pointer to the directory entry */
|
|
|
|
|
|
|
|
diroffset = DIRSEC_BYTENDX(fs, dirinfo->dir.fd_index);
|
|
|
|
direntry = &fs->fs_buffer[diroffset];
|
2011-07-13 23:53:06 +02:00
|
|
|
|
2011-07-15 17:37:29 +02:00
|
|
|
/* Verify the checksum */
|
|
|
|
|
|
|
|
if (fat_lfnchecksum(&direntry[DIR_NAME]) == checksum)
|
|
|
|
{
|
|
|
|
/* Success! Save the position of the directory entry and
|
|
|
|
* return success.
|
|
|
|
*/
|
2011-07-13 23:53:06 +02:00
|
|
|
|
2011-07-15 17:37:29 +02:00
|
|
|
dirinfo->fd_seq.ds_sector = fs->fs_currentsector;
|
|
|
|
dirinfo->fd_seq.ds_offset = diroffset;
|
|
|
|
dirinfo->fd_seq.ds_cluster = dirinfo->dir.fd_currcluster;
|
|
|
|
return OK;
|
2011-07-13 23:53:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Bad news.. reset and continue with this entry (which is
|
2020-02-23 09:50:23 +01:00
|
|
|
* probably not an LFN entry unless the file system is
|
2011-07-13 23:53:06 +02:00
|
|
|
* seriously corrupted.
|
|
|
|
*/
|
|
|
|
|
|
|
|
seqno = lastseq;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No.. there are more LFN entries to go. Decrement the sequence
|
|
|
|
* number and check the next directory entry.
|
|
|
|
*/
|
2011-07-13 19:19:31 +02:00
|
|
|
|
2011-07-13 23:53:06 +02:00
|
|
|
seqno--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No.. the names do not match. Restart the search at the next
|
|
|
|
* entry.
|
|
|
|
*/
|
2011-07-13 19:19:31 +02:00
|
|
|
|
2011-07-13 23:53:06 +02:00
|
|
|
seqno = lastseq;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Continue at the next directory entry */
|
2013-03-31 15:06:22 +02:00
|
|
|
|
2011-07-13 23:53:06 +02:00
|
|
|
next_entry:
|
|
|
|
if (fat_nextdirentry(fs, &dirinfo->dir) != OK)
|
|
|
|
{
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
2011-07-13 19:19:31 +02:00
|
|
|
}
|
2011-07-13 23:53:06 +02:00
|
|
|
}
|
|
|
|
#endif
|
2011-07-13 19:19:31 +02:00
|
|
|
|
2011-07-13 23:53:06 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: fat_allocatesfnentry
|
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description: Find a free directory entry for a short file name entry.
|
2011-07-13 23:53:06 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
2011-07-13 19:19:31 +02:00
|
|
|
|
2020-05-31 17:48:21 +02:00
|
|
|
static inline int fat_allocatesfnentry(FAR struct fat_mountpt_s *fs,
|
|
|
|
FAR struct fat_dirinfo_s *dirinfo)
|
2011-07-13 23:53:06 +02:00
|
|
|
{
|
2020-05-31 17:48:21 +02:00
|
|
|
FAR uint8_t *direntry;
|
2011-07-13 23:53:06 +02:00
|
|
|
uint16_t diroffset;
|
2011-07-18 20:28:02 +02:00
|
|
|
#ifdef CONFIG_FAT_LFN
|
|
|
|
off_t startsector;
|
|
|
|
#endif
|
2011-07-13 23:53:06 +02:00
|
|
|
uint8_t ch;
|
|
|
|
int ret;
|
|
|
|
|
2011-07-18 20:28:02 +02:00
|
|
|
/* Save the sector number of the first sector of the directory. We don't
|
|
|
|
* really need this for short file name entries; this is just done for
|
|
|
|
* consistency with the long file name logic.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_FAT_LFN
|
|
|
|
startsector = dirinfo->dir.fd_currsector;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Then search for a free short file name directory entry */
|
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
for (; ; )
|
2011-07-13 23:53:06 +02:00
|
|
|
{
|
|
|
|
/* Read the directory sector into fs_buffer */
|
|
|
|
|
|
|
|
ret = fat_fscacheread(fs, dirinfo->dir.fd_currsector);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* Make sure that the return value is NOT -ENOSPC */
|
|
|
|
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a pointer to the entry at fd_index */
|
|
|
|
|
|
|
|
diroffset = (dirinfo->dir.fd_index & DIRSEC_NDXMASK(fs)) * DIR_SIZE;
|
|
|
|
direntry = &fs->fs_buffer[diroffset];
|
|
|
|
|
|
|
|
/* Check if this directory entry is empty */
|
|
|
|
|
|
|
|
ch = direntry[DIR_NAME];
|
|
|
|
if (ch == DIR0_ALLEMPTY || ch == DIR0_EMPTY)
|
|
|
|
{
|
|
|
|
/* It is empty -- we have found a directory entry */
|
|
|
|
|
2011-07-18 20:28:02 +02:00
|
|
|
dirinfo->fd_seq.ds_sector = fs->fs_currentsector;
|
|
|
|
dirinfo->fd_seq.ds_offset = diroffset;
|
2011-07-13 23:53:06 +02:00
|
|
|
#ifdef CONFIG_FAT_LFN
|
2011-07-18 20:28:02 +02:00
|
|
|
dirinfo->fd_seq.ds_cluster = dirinfo->dir.fd_currcluster;
|
|
|
|
dirinfo->fd_seq.ds_startsector = startsector;
|
2011-07-13 23:53:06 +02:00
|
|
|
|
|
|
|
/* Set the "last" long file name offset to the same entry */
|
|
|
|
|
2011-07-18 20:28:02 +02:00
|
|
|
dirinfo->fd_seq.ds_lfnsector = dirinfo->fd_seq.ds_sector;
|
|
|
|
dirinfo->fd_seq.ds_lfnoffset = dirinfo->fd_seq.ds_offset;
|
|
|
|
dirinfo->fd_seq.ds_lfncluster = dirinfo->fd_seq.ds_cluster;
|
2011-07-13 19:19:31 +02:00
|
|
|
#endif
|
2011-07-13 23:53:06 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* It is not empty try the next one */
|
|
|
|
|
|
|
|
ret = fat_nextdirentry(fs, &dirinfo->dir);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* This will return -ENOSPC if we have examined all of the
|
|
|
|
* directory entries without finding a free entry.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
2011-07-13 19:19:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2011-07-13 23:53:06 +02:00
|
|
|
* Name: fat_allocatelfnentry
|
2011-07-13 19:19:31 +02:00
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description: Find a sequence of free directory entries for a several long
|
2011-07-13 23:53:06 +02:00
|
|
|
* and one short file name entry.
|
2011-07-13 19:19:31 +02:00
|
|
|
*
|
2011-07-18 20:28:02 +02:00
|
|
|
* On entry, dirinfo.dir refers to the first interesting entry the directory.
|
|
|
|
*
|
2011-07-13 19:19:31 +02:00
|
|
|
****************************************************************************/
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-13 23:53:06 +02:00
|
|
|
#ifdef CONFIG_FAT_LFN
|
2020-05-31 17:48:21 +02:00
|
|
|
static inline int fat_allocatelfnentry(FAR struct fat_mountpt_s *fs,
|
|
|
|
FAR struct fat_dirinfo_s *dirinfo)
|
2011-07-13 23:53:06 +02:00
|
|
|
{
|
2020-05-31 17:48:21 +02:00
|
|
|
FAR uint8_t *direntry;
|
2011-07-13 23:53:06 +02:00
|
|
|
uint16_t diroffset;
|
2011-07-18 20:28:02 +02:00
|
|
|
off_t startsector;
|
2011-07-13 23:53:06 +02:00
|
|
|
uint8_t nentries;
|
|
|
|
uint8_t remainder;
|
|
|
|
uint8_t needed;
|
2011-07-14 23:07:59 +02:00
|
|
|
uint8_t ch;
|
2011-07-13 23:53:06 +02:00
|
|
|
int namelen;
|
|
|
|
int ret;
|
|
|
|
|
2011-07-14 19:57:59 +02:00
|
|
|
/* Get the length of the long file name (size of the fd_lfname array is
|
|
|
|
* LDIR_MAXFNAME+1 we do not have to check the length of the string).
|
2011-07-13 23:53:06 +02:00
|
|
|
*/
|
|
|
|
|
2020-07-26 12:52:22 +02:00
|
|
|
for (namelen = 0; dirinfo->fd_lfname[namelen] != '\0'; namelen++)
|
|
|
|
{
|
|
|
|
/* Empty */
|
|
|
|
}
|
|
|
|
|
2019-03-01 22:00:00 +01:00
|
|
|
DEBUGASSERT(namelen <= LDIR_MAXFNAME + 1);
|
2011-07-13 23:53:06 +02:00
|
|
|
|
|
|
|
/* How many LFN directory entries are we expecting? */
|
|
|
|
|
|
|
|
nentries = namelen / LDIR_MAXLFNCHARS;
|
2011-07-14 23:07:59 +02:00
|
|
|
remainder = namelen - nentries * LDIR_MAXLFNCHARS;
|
2011-07-13 23:53:06 +02:00
|
|
|
if (remainder > 0)
|
|
|
|
{
|
|
|
|
nentries++;
|
|
|
|
}
|
2019-10-27 18:48:14 +01:00
|
|
|
|
2011-07-13 23:53:06 +02:00
|
|
|
DEBUGASSERT(nentries > 0 && nentries <= LDIR_MAXLFNS);
|
|
|
|
|
|
|
|
/* Plus another for short file name entry that follows the sequence of LFN
|
|
|
|
* entries.
|
|
|
|
*/
|
|
|
|
|
|
|
|
nentries++;
|
|
|
|
|
2011-07-18 20:28:02 +02:00
|
|
|
/* Save the sector number of the first sector of the directory. We will
|
|
|
|
* need this later for re-scanning the directory to verify that a FAT file
|
|
|
|
* name is unique.
|
|
|
|
*/
|
|
|
|
|
|
|
|
startsector = dirinfo->dir.fd_currsector;
|
|
|
|
|
2011-07-13 23:53:06 +02:00
|
|
|
/* Now, search the directory looking for a sequence for free entries that
|
|
|
|
* long.
|
|
|
|
*/
|
|
|
|
|
|
|
|
needed = nentries;
|
2015-10-11 19:39:29 +02:00
|
|
|
for (; ; )
|
2011-07-13 23:53:06 +02:00
|
|
|
{
|
|
|
|
/* Read the directory sector into fs_buffer */
|
|
|
|
|
|
|
|
ret = fat_fscacheread(fs, dirinfo->dir.fd_currsector);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* Make sure that the return value is NOT -ENOSPC */
|
|
|
|
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a pointer to the entry at fd_index */
|
|
|
|
|
|
|
|
diroffset = (dirinfo->dir.fd_index & DIRSEC_NDXMASK(fs)) * DIR_SIZE;
|
|
|
|
direntry = &fs->fs_buffer[diroffset];
|
|
|
|
|
|
|
|
/* Check if this directory entry is empty */
|
|
|
|
|
2011-07-15 01:29:43 +02:00
|
|
|
ch = LDIR_GETSEQ(direntry);
|
2011-07-13 23:53:06 +02:00
|
|
|
if (ch == DIR0_ALLEMPTY || ch == DIR0_EMPTY)
|
|
|
|
{
|
|
|
|
/* It is empty -- we have found a directory entry. Is this the
|
|
|
|
* "last" LFN entry (i.e., the one that occurs first)?
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (needed == nentries)
|
|
|
|
{
|
|
|
|
/* Yes.. remember the position of this entry */
|
|
|
|
|
2011-07-18 20:28:02 +02:00
|
|
|
dirinfo->fd_seq.ds_lfnsector = fs->fs_currentsector;
|
|
|
|
dirinfo->fd_seq.ds_lfnoffset = diroffset;
|
|
|
|
dirinfo->fd_seq.ds_lfncluster = dirinfo->dir.fd_currcluster;
|
|
|
|
dirinfo->fd_seq.ds_startsector = startsector;
|
2011-07-15 01:29:43 +02:00
|
|
|
}
|
2011-07-13 23:53:06 +02:00
|
|
|
|
|
|
|
/* Is this last entry we need (i.e., the entry for the short
|
|
|
|
* file name entry)?
|
|
|
|
*/
|
2013-03-31 15:06:22 +02:00
|
|
|
|
2011-07-13 23:53:06 +02:00
|
|
|
if (needed <= 1)
|
|
|
|
{
|
|
|
|
/* Yes.. remember the position of this entry and return
|
|
|
|
* success.
|
|
|
|
*/
|
|
|
|
|
|
|
|
dirinfo->fd_seq.ds_sector = fs->fs_currentsector;
|
|
|
|
dirinfo->fd_seq.ds_offset = diroffset;
|
|
|
|
dirinfo->fd_seq.ds_cluster = dirinfo->dir.fd_currcluster;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, just decrement the number of directory entries
|
|
|
|
* needed and continue looking.
|
|
|
|
*/
|
|
|
|
|
|
|
|
needed--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The directory entry is not available */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Reset the search and continue looking */
|
|
|
|
|
|
|
|
needed = nentries;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try the next directory entry */
|
|
|
|
|
|
|
|
ret = fat_nextdirentry(fs, &dirinfo->dir);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* This will return -ENOSPC if we have examined all of the
|
|
|
|
* directory entries without finding a free entry.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-07-13 00:38:21 +02:00
|
|
|
/****************************************************************************
|
2011-07-14 19:57:59 +02:00
|
|
|
* Name: fat_getsfname
|
2011-07-13 00:38:21 +02:00
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description: Get the 8.3 filename from a directory entry. On entry, the
|
2011-07-14 19:57:59 +02:00
|
|
|
* short file name entry is already in the cache.
|
2011-07-13 00:38:21 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-31 17:48:21 +02:00
|
|
|
static inline int fat_getsfname(FAR uint8_t *direntry, FAR char *buffer,
|
2011-07-14 19:57:59 +02:00
|
|
|
unsigned int buflen)
|
2011-07-13 00:38:21 +02:00
|
|
|
{
|
2011-07-14 19:57:59 +02:00
|
|
|
#ifdef CONFIG_FAT_LCNAMES
|
2015-10-11 19:39:29 +02:00
|
|
|
uint8_t ntflags;
|
2011-07-14 19:57:59 +02:00
|
|
|
#endif
|
2015-10-11 19:39:29 +02:00
|
|
|
int ch;
|
|
|
|
int ndx;
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
/* Check if we will be doing upper to lower case conversions */
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-14 19:57:59 +02:00
|
|
|
#ifdef CONFIG_FAT_LCNAMES
|
2015-10-11 19:39:29 +02:00
|
|
|
ntflags = DIR_GETNTRES(direntry);
|
2011-07-14 19:57:59 +02:00
|
|
|
#endif
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
/* Reserve a byte for the NUL terminator */
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
buflen--;
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
/* Get the 8-byte filename */
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
for (ndx = 0; ndx < 8 && buflen > 0; ndx++)
|
|
|
|
{
|
|
|
|
/* Get the next filename character from the directory entry */
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
ch = direntry[ndx];
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
/* Any space (or ndx==8) terminates the filename */
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
if (ch == ' ')
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
/* In this version, we never write 0xe5 in the directory filenames
|
|
|
|
* (because we do not handle any character sets where 0xe5 is valid
|
2020-02-23 09:50:23 +01:00
|
|
|
* in a filaname), but we could eencounter this in a filesystem
|
2015-10-11 19:39:29 +02:00
|
|
|
* written by some other system
|
|
|
|
*/
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
if (ndx == 0 && ch == DIR0_E5)
|
|
|
|
{
|
|
|
|
ch = 0xe5;
|
|
|
|
}
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
/* Check if we should perform upper to lower case conversion
|
|
|
|
* of the (whole) filename.
|
|
|
|
*/
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-14 19:57:59 +02:00
|
|
|
#ifdef CONFIG_FAT_LCNAMES
|
2015-10-11 19:39:29 +02:00
|
|
|
if (ntflags & FATNTRES_LCNAME && isupper(ch))
|
|
|
|
{
|
|
|
|
ch = tolower(ch);
|
|
|
|
}
|
2011-07-14 19:57:59 +02:00
|
|
|
#endif
|
2019-10-27 18:48:14 +01:00
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
/* Copy the next character into the filename */
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
*buffer++ = ch;
|
|
|
|
buflen--;
|
|
|
|
}
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
/* Check if there is an extension */
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
if (direntry[8] != ' ' && buflen > 0)
|
|
|
|
{
|
|
|
|
/* Yes, output the dot before the extension */
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
*buffer++ = '.';
|
|
|
|
buflen--;
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
/* Then output the (up to) 3 character extension */
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
for (ndx = 8; ndx < 11 && buflen > 0; ndx++)
|
|
|
|
{
|
|
|
|
/* Get the next extensions character from the directory entry */
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
ch = direntry[DIR_NAME + ndx];
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
/* Any space (or ndx==11) terminates the extension */
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
if (ch == ' ')
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
/* Check if we should perform upper to lower case conversion
|
|
|
|
* of the (whole) filename.
|
|
|
|
*/
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-14 19:57:59 +02:00
|
|
|
#ifdef CONFIG_FAT_LCNAMES
|
2015-10-11 19:39:29 +02:00
|
|
|
if (ntflags & FATNTRES_LCEXT && isupper(ch))
|
|
|
|
{
|
|
|
|
ch = tolower(ch);
|
|
|
|
}
|
2011-07-14 19:57:59 +02:00
|
|
|
#endif
|
2019-10-27 18:48:14 +01:00
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
/* Copy the next character into the filename */
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
*buffer++ = ch;
|
|
|
|
buflen--;
|
|
|
|
}
|
|
|
|
}
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
/* Put a null terminator at the end of the filename. We don't have to
|
|
|
|
* check if there is room because we reserved a byte for the NUL
|
|
|
|
* terminator at the beginning of this function.
|
|
|
|
*/
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
*buffer = '\0';
|
|
|
|
return OK;
|
2011-07-13 00:38:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2011-07-14 19:57:59 +02:00
|
|
|
* Name: fat_getlfnchunk
|
2011-07-13 00:38:21 +02:00
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description: There are 13 characters per LFN entry, broken up into three
|
2020-02-23 09:50:23 +01:00
|
|
|
* chunks for characters 1-5, 6-11, and 12-13. This function will get the
|
2011-07-14 19:57:59 +02:00
|
|
|
* file name characters from one chunk.
|
2011-07-13 00:38:21 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-07-14 19:57:59 +02:00
|
|
|
#ifdef CONFIG_FAT_LFN
|
2020-07-26 12:52:22 +02:00
|
|
|
static void fat_getlfnchunk(FAR uint8_t *chunk, FAR lfnchar *dest,
|
2020-05-31 17:48:21 +02:00
|
|
|
int nchunk)
|
2011-07-13 00:38:21 +02:00
|
|
|
{
|
2021-12-10 02:40:30 +01:00
|
|
|
uint16_t wch;
|
2011-07-14 19:57:59 +02:00
|
|
|
int i;
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-14 19:57:59 +02:00
|
|
|
/* Copy bytes 1-nchunk */
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-14 19:57:59 +02:00
|
|
|
for (i = 0; i < nchunk; i++)
|
2011-07-13 00:38:21 +02:00
|
|
|
{
|
2020-05-31 17:48:21 +02:00
|
|
|
/* Get the next unicode character from the chunk. We only handle
|
|
|
|
* ASCII. For ASCII, the upper byte should be zero and the lower
|
|
|
|
* should match the ASCII code.
|
2011-07-14 19:57:59 +02:00
|
|
|
*/
|
|
|
|
|
2021-12-10 02:40:30 +01:00
|
|
|
wch = fat_getuint16(chunk);
|
2020-07-26 12:52:22 +02:00
|
|
|
# ifdef CONFIG_FAT_LFN_UTF8
|
|
|
|
*dest++ = wch;
|
|
|
|
# else
|
2011-07-14 19:57:59 +02:00
|
|
|
*dest++ = (uint8_t)(wch & 0xff);
|
2020-07-26 12:52:22 +02:00
|
|
|
# endif
|
2021-12-10 02:40:30 +01:00
|
|
|
chunk += sizeof(uint16_t);
|
2011-07-14 19:57:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: fat_getlfname
|
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description: Get the long filename from a sequence of directory entries.
|
2011-07-14 19:57:59 +02:00
|
|
|
* On entry, the "last" long file name entry is in the cache. Returns with
|
|
|
|
* the short file name entry in the cache.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_FAT_LFN
|
2020-05-31 17:48:21 +02:00
|
|
|
static inline int fat_getlfname(FAR struct fat_mountpt_s *fs,
|
2022-07-26 08:56:41 +02:00
|
|
|
FAR struct fs_dirent_s *dir,
|
|
|
|
FAR struct dirent *entry)
|
2011-07-14 19:57:59 +02:00
|
|
|
{
|
2022-08-05 06:31:25 +02:00
|
|
|
FAR struct fat_dirent_s *fdir;
|
2020-05-31 17:48:21 +02:00
|
|
|
FAR uint8_t *direntry;
|
2020-07-26 12:52:22 +02:00
|
|
|
lfnchar lfname[LDIR_MAXLFNCHARS];
|
2011-07-14 19:57:59 +02:00
|
|
|
uint16_t diroffset;
|
|
|
|
uint8_t seqno;
|
2011-07-15 19:38:29 +02:00
|
|
|
uint8_t rawseq;
|
2011-07-14 19:57:59 +02:00
|
|
|
uint8_t offset;
|
|
|
|
uint8_t checksum;
|
|
|
|
int nsrc;
|
2011-07-14 23:07:59 +02:00
|
|
|
int ret;
|
2011-07-14 19:57:59 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Get a reference to the current directory entry */
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
fdir = (FAR struct fat_dirent_s *)dir;
|
|
|
|
diroffset = (fdir->dir.fd_index & DIRSEC_NDXMASK(fs)) * DIR_SIZE;
|
2011-07-14 19:57:59 +02:00
|
|
|
direntry = &fs->fs_buffer[diroffset];
|
|
|
|
|
|
|
|
/* Get the starting sequence number */
|
|
|
|
|
|
|
|
seqno = LDIR_GETSEQ(direntry);
|
|
|
|
DEBUGASSERT((seqno & LDIR0_LAST) != 0);
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
|
2011-07-15 19:38:29 +02:00
|
|
|
rawseq = (seqno & LDIR0_SEQ_MASK);
|
|
|
|
if (rawseq < 1 || rawseq > LDIR_MAXLFNS)
|
2011-07-14 19:57:59 +02:00
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the checksum value */
|
|
|
|
|
|
|
|
checksum = LDIR_GETCHECKSUM(direntry);
|
|
|
|
|
|
|
|
/* Loop until the whole file name has been transferred */
|
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
for (; ; )
|
2011-07-14 19:57:59 +02:00
|
|
|
{
|
2020-07-26 12:52:22 +02:00
|
|
|
# ifdef CONFIG_FAT_LFN_UTF8
|
|
|
|
/* Get the string offset associated with the "last" entry. */
|
|
|
|
|
|
|
|
/* Extract and convert the unicode name */
|
|
|
|
|
|
|
|
fat_getlfnchunk(LDIR_PTRWCHAR1_5(direntry), lfname, 5);
|
|
|
|
fat_getlfnchunk(LDIR_PTRWCHAR6_11(direntry), &lfname[5], 6);
|
|
|
|
fat_getlfnchunk(LDIR_PTRWCHAR12_13(direntry), &lfname[11], 2);
|
|
|
|
|
|
|
|
/* Ignore trailing spaces on the "last" directory entry. The
|
|
|
|
* number of characters available is LDIR_MAXLFNCHARS or that
|
|
|
|
* minus the number of trailing spaces on the "last" directory
|
|
|
|
* entry.
|
|
|
|
*/
|
|
|
|
|
|
|
|
nsrc = LDIR_MAXLFNCHARS;
|
|
|
|
if ((seqno & LDIR0_LAST) != 0)
|
|
|
|
{
|
|
|
|
/* Reduce the number of characters by the number of trailing
|
|
|
|
* spaces, init chars (0xffff) and '\0'.
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (; nsrc > 0 && (lfname[nsrc - 1] == ' ' ||
|
|
|
|
lfname[nsrc - 1] == '\0' ||
|
|
|
|
lfname[nsrc - 1] == 0xffff); nsrc--);
|
|
|
|
|
|
|
|
/* Add a null terminator to the destination string (the actual
|
|
|
|
* length of the destination buffer is NAME_MAX+1, so the NUL
|
|
|
|
* terminator will fit).
|
|
|
|
*/
|
|
|
|
|
2022-07-26 08:56:41 +02:00
|
|
|
entry->d_name[NAME_MAX] = '\0';
|
2020-07-26 12:52:22 +02:00
|
|
|
offset = NAME_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then transfer the characters */
|
|
|
|
|
|
|
|
for (i = nsrc - 1; i >= 0; i--)
|
|
|
|
{
|
2022-07-26 08:56:41 +02:00
|
|
|
offset = fat_ucstoutf8((FAR uint8_t *)entry->d_name,
|
2020-07-26 12:52:22 +02:00
|
|
|
offset, lfname[i]);
|
|
|
|
}
|
|
|
|
# else
|
2011-07-15 19:38:29 +02:00
|
|
|
/* Get the string offset associated with the "last" entry. */
|
|
|
|
|
|
|
|
offset = (rawseq - 1) * LDIR_MAXLFNCHARS;
|
|
|
|
|
2011-07-14 19:57:59 +02:00
|
|
|
/* Will any of this file name fit into the destination buffer? */
|
|
|
|
|
|
|
|
if (offset < NAME_MAX)
|
|
|
|
{
|
|
|
|
/* Yes.. extract and convert the unicode name */
|
|
|
|
|
|
|
|
fat_getlfnchunk(LDIR_PTRWCHAR1_5(direntry), lfname, 5);
|
|
|
|
fat_getlfnchunk(LDIR_PTRWCHAR6_11(direntry), &lfname[5], 6);
|
|
|
|
fat_getlfnchunk(LDIR_PTRWCHAR12_13(direntry), &lfname[11], 2);
|
|
|
|
|
|
|
|
/* Ignore trailing spaces on the "last" directory entry. The
|
2019-09-19 14:14:53 +02:00
|
|
|
* number of characters available is LDIR_MAXLFNCHARS or that
|
2011-07-14 19:57:59 +02:00
|
|
|
* minus the number of trailing spaces on the "last" directory
|
|
|
|
* entry.
|
|
|
|
*/
|
|
|
|
|
|
|
|
nsrc = LDIR_MAXLFNCHARS;
|
|
|
|
if ((seqno & LDIR0_LAST) != 0)
|
|
|
|
{
|
|
|
|
/* Reduce the number of characters by the number of trailing
|
2020-07-26 12:52:22 +02:00
|
|
|
* spaces, init chars (0xff) and '\0'.
|
2011-07-14 19:57:59 +02:00
|
|
|
*/
|
|
|
|
|
2020-07-26 12:52:22 +02:00
|
|
|
for (; nsrc > 0 && (lfname[nsrc - 1] == ' ' ||
|
|
|
|
lfname[nsrc - 1] == '\0' ||
|
|
|
|
lfname[nsrc - 1] == 0xff); nsrc--);
|
2011-07-14 19:57:59 +02:00
|
|
|
|
|
|
|
/* Further reduce the length so that it fits in the destination
|
|
|
|
* buffer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (offset + nsrc > NAME_MAX)
|
|
|
|
{
|
|
|
|
nsrc = NAME_MAX - offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add a null terminator to the destination string (the actual
|
|
|
|
* length of the destination buffer is NAME_MAX+1, so the NUL
|
|
|
|
* terminator will fit).
|
|
|
|
*/
|
|
|
|
|
2022-07-26 08:56:41 +02:00
|
|
|
entry->d_name[offset + nsrc] = '\0';
|
2011-07-14 19:57:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Then transfer the characters */
|
|
|
|
|
2019-03-01 22:00:00 +01:00
|
|
|
for (i = 0; i < nsrc && offset + i < NAME_MAX; i++)
|
2011-07-14 19:57:59 +02:00
|
|
|
{
|
2022-07-26 08:56:41 +02:00
|
|
|
entry->d_name[offset + i] = lfname[i];
|
2011-07-14 19:57:59 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-26 12:52:22 +02:00
|
|
|
#endif
|
2011-07-14 19:57:59 +02:00
|
|
|
|
|
|
|
/* Read next directory entry */
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
if (fat_nextdirentry(fs, &fdir->dir) != OK)
|
2011-07-14 19:57:59 +02:00
|
|
|
{
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure that the directory sector into the sector cache */
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
ret = fat_fscacheread(fs, fdir->dir.fd_currsector);
|
2011-07-14 19:57:59 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a reference to the current directory entry */
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
diroffset = (fdir->dir.fd_index & DIRSEC_NDXMASK(fs)) * DIR_SIZE;
|
2011-07-14 19:57:59 +02:00
|
|
|
direntry = &fs->fs_buffer[diroffset];
|
|
|
|
|
|
|
|
/* Get the next expected sequence number. */
|
|
|
|
|
2011-07-15 19:38:29 +02:00
|
|
|
seqno = --rawseq;
|
2011-07-14 19:57:59 +02:00
|
|
|
if (seqno < 1)
|
|
|
|
{
|
2020-07-26 12:52:22 +02:00
|
|
|
# ifdef CONFIG_FAT_LFN_UTF8
|
|
|
|
/* We must left align the d_name after utf8 processing */
|
|
|
|
|
|
|
|
if (offset > 0)
|
|
|
|
{
|
2022-07-26 08:56:41 +02:00
|
|
|
memmove(entry->d_name, &entry->d_name[offset],
|
2020-07-26 12:52:22 +02:00
|
|
|
(NAME_MAX + 1) - offset);
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
|
2011-07-14 19:57:59 +02:00
|
|
|
/* We just completed processing the "first" long file name entry
|
|
|
|
* and we just read the short file name entry. Verify that the
|
|
|
|
* checksum of the short file name matches the checksum that we
|
|
|
|
* found in the long file name entries.
|
|
|
|
*/
|
|
|
|
|
2011-07-14 23:07:59 +02:00
|
|
|
if (fat_lfnchecksum(direntry) == checksum)
|
2011-07-14 19:57:59 +02:00
|
|
|
{
|
|
|
|
/* Yes.. return success! */
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No, the checksum is bad. */
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify the next long file name entry. Is this an LFN entry? Does it
|
|
|
|
* have the sequence number we are looking for? Does the checksum
|
|
|
|
* match the previous entries?
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (LDIR_GETATTRIBUTES(direntry) != LDDIR_LFNATTR ||
|
|
|
|
LDIR_GETSEQ(direntry) != seqno ||
|
|
|
|
LDIR_GETCHECKSUM(direntry) != checksum)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: fat_putsfname
|
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description: Write the short directory entry name.
|
2011-07-14 19:57:59 +02:00
|
|
|
*
|
|
|
|
* Assumption: The directory sector is in the cache.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-31 17:48:21 +02:00
|
|
|
static int fat_putsfname(FAR struct fat_mountpt_s *fs,
|
|
|
|
FAR struct fat_dirinfo_s *dirinfo)
|
2011-07-14 19:57:59 +02:00
|
|
|
{
|
2020-05-31 17:48:21 +02:00
|
|
|
FAR uint8_t *direntry = &fs->fs_buffer[dirinfo->fd_seq.ds_offset];
|
2013-03-31 15:06:22 +02:00
|
|
|
|
2011-11-15 23:37:14 +01:00
|
|
|
/* Write the short directory entry */
|
|
|
|
|
2011-07-14 19:57:59 +02:00
|
|
|
memcpy(&direntry[DIR_NAME], dirinfo->fd_name, DIR_MAXFNAME);
|
2011-07-14 21:47:31 +02:00
|
|
|
#ifdef CONFIG_FAT_LCNAMES
|
2011-07-14 19:57:59 +02:00
|
|
|
DIR_PUTNTRES(direntry, dirinfo->fd_ntflags);
|
|
|
|
#else
|
|
|
|
DIR_PUTNTRES(direntry, 0);
|
|
|
|
#endif
|
|
|
|
fs->fs_dirty = true;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2011-07-18 02:20:58 +02:00
|
|
|
* Name: fat_initlfname
|
2011-07-14 19:57:59 +02:00
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description: There are 13 characters per LFN entry, broken up into three
|
2020-02-23 09:50:23 +01:00
|
|
|
* chunks for characters 1-5, 6-11, and 12-13. This function will put the
|
2011-07-18 02:20:58 +02:00
|
|
|
* 0xffff characters into one chunk.
|
2011-07-14 19:57:59 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_FAT_LFN
|
2020-05-31 17:48:21 +02:00
|
|
|
static void fat_initlfname(FAR uint8_t *chunk, int nchunk)
|
2011-07-14 19:57:59 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Initialize unicode characters 1-nchunk */
|
|
|
|
|
|
|
|
for (i = 0; i < nchunk; i++)
|
|
|
|
{
|
2011-07-18 02:20:58 +02:00
|
|
|
/* The write the 16-bit 0xffff character into the directory entry. */
|
2011-07-14 19:57:59 +02:00
|
|
|
|
2020-05-31 17:48:21 +02:00
|
|
|
fat_putuint16((FAR uint8_t *)chunk, (uint16_t)0xffff);
|
2021-12-10 02:40:30 +01:00
|
|
|
chunk += sizeof(uint16_t);
|
2011-07-14 19:57:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: fat_putlfnchunk
|
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description: There are 13 characters per LFN entry, broken up into three
|
2020-02-23 09:50:23 +01:00
|
|
|
* chunks for characters 1-5, 6-11, and 12-13. This function will put the
|
2011-07-14 19:57:59 +02:00
|
|
|
* file name characters into one chunk.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_FAT_LFN
|
2020-07-26 12:52:22 +02:00
|
|
|
static void fat_putlfnchunk(FAR uint8_t *chunk, FAR const lfnchar *src,
|
2020-05-31 17:48:21 +02:00
|
|
|
int nchunk)
|
2011-07-14 19:57:59 +02:00
|
|
|
{
|
|
|
|
uint16_t wch;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Write bytes 1-nchunk */
|
|
|
|
|
|
|
|
for (i = 0; i < nchunk; i++)
|
|
|
|
{
|
|
|
|
/* Get the next ascii character from the name substring and convert it
|
|
|
|
* to unicode. The upper byte should be zero and the lower should be
|
|
|
|
* the ASCII code. The write the unicode character into the directory
|
|
|
|
* entry.
|
|
|
|
*/
|
|
|
|
|
|
|
|
wch = (uint16_t)*src++;
|
|
|
|
fat_putuint16(chunk, wch);
|
2021-12-10 02:40:30 +01:00
|
|
|
chunk += sizeof(uint16_t);
|
2011-07-14 19:57:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: fat_putlfname
|
|
|
|
*
|
2020-05-31 17:48:21 +02:00
|
|
|
* Description:
|
|
|
|
* Write the long filename into a sequence of directory entries. On entry,
|
|
|
|
* the "last" long file name entry is in the cache. Returns with the
|
|
|
|
* short file name entry in the cache.
|
2011-07-14 19:57:59 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_FAT_LFN
|
2020-05-31 17:48:21 +02:00
|
|
|
static int fat_putlfname(FAR struct fat_mountpt_s *fs,
|
|
|
|
FAR struct fat_dirinfo_s *dirinfo)
|
2011-07-14 19:57:59 +02:00
|
|
|
{
|
2020-05-31 17:48:21 +02:00
|
|
|
FAR uint8_t *direntry;
|
2011-07-14 19:57:59 +02:00
|
|
|
uint16_t diroffset;
|
|
|
|
uint8_t nfullentries;
|
|
|
|
uint8_t nentries;
|
|
|
|
uint8_t remainder;
|
|
|
|
uint8_t offset;
|
|
|
|
uint8_t seqno;
|
|
|
|
uint8_t checksum;
|
2013-03-31 15:06:22 +02:00
|
|
|
off_t startsector;
|
2011-07-14 19:57:59 +02:00
|
|
|
int namelen;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get the length of the long file name (size of the fd_lfname array is
|
|
|
|
* LDIR_MAXFNAME+1 we do not have to check the length of the string).
|
2011-09-28 00:04:03 +02:00
|
|
|
* NOTE that remainder is conditionally incremented to include the NUL
|
|
|
|
* terminating character that may also need be written to the directory
|
|
|
|
* entry. NUL terminating is not required if length is multiple of
|
|
|
|
* LDIR_MAXLFNCHARS (13).
|
2011-07-14 19:57:59 +02:00
|
|
|
*/
|
|
|
|
|
2020-07-26 12:52:22 +02:00
|
|
|
for (namelen = 0; dirinfo->fd_lfname[namelen] != '\0'; namelen++)
|
|
|
|
{
|
|
|
|
/* Empty */
|
|
|
|
}
|
|
|
|
|
2019-03-01 22:00:00 +01:00
|
|
|
DEBUGASSERT(namelen <= LDIR_MAXFNAME + 1);
|
2011-07-14 19:57:59 +02:00
|
|
|
|
|
|
|
/* How many LFN directory entries do we need to write? */
|
|
|
|
|
|
|
|
nfullentries = namelen / LDIR_MAXLFNCHARS;
|
|
|
|
remainder = namelen - nfullentries * LDIR_MAXLFNCHARS;
|
|
|
|
nentries = nfullentries;
|
|
|
|
if (remainder > 0)
|
|
|
|
{
|
|
|
|
nentries++;
|
2011-09-28 00:04:03 +02:00
|
|
|
remainder++;
|
2011-07-14 19:57:59 +02:00
|
|
|
}
|
2015-03-15 19:22:12 +01:00
|
|
|
|
2011-07-14 19:57:59 +02:00
|
|
|
DEBUGASSERT(nentries > 0 && nentries <= LDIR_MAXLFNS);
|
|
|
|
|
|
|
|
/* Create the short file name alias */
|
|
|
|
|
|
|
|
ret = fat_createalias(dirinfo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-07-18 20:28:02 +02:00
|
|
|
/* Set up the initial positional data */
|
|
|
|
|
|
|
|
dirinfo->dir.fd_currcluster = dirinfo->fd_seq.ds_lfncluster;
|
|
|
|
dirinfo->dir.fd_currsector = dirinfo->fd_seq.ds_lfnsector;
|
|
|
|
dirinfo->dir.fd_index = dirinfo->fd_seq.ds_lfnoffset / DIR_SIZE;
|
|
|
|
|
2013-03-31 15:06:22 +02:00
|
|
|
/* ds_lfnoffset is the offset in the sector. However fd_index is used as
|
|
|
|
* index for the entire cluster. We need to add that offset
|
|
|
|
*/
|
|
|
|
|
2020-05-31 17:48:21 +02:00
|
|
|
startsector = fat_cluster2sector(fs, dirinfo->dir.fd_currcluster);
|
|
|
|
dirinfo->dir.fd_index += (dirinfo->dir.fd_currsector - startsector) *
|
|
|
|
DIRSEC_NDIRS(fs);
|
2013-03-31 15:06:22 +02:00
|
|
|
|
2015-03-15 19:22:12 +01:00
|
|
|
/* Make sure that the alias is unique in this directory */
|
2011-07-18 20:28:02 +02:00
|
|
|
|
|
|
|
ret = fat_uniquealias(fs, dirinfo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-07-14 19:57:59 +02:00
|
|
|
/* Get the short file name checksum */
|
|
|
|
|
|
|
|
checksum = fat_lfnchecksum(dirinfo->fd_name);
|
|
|
|
|
|
|
|
/* Setup the starting sequence number */
|
|
|
|
|
|
|
|
seqno = LDIR0_LAST | nentries;
|
|
|
|
|
2011-11-19 21:30:39 +01:00
|
|
|
/* Make sure that the sector containing the "last" long file name entry
|
|
|
|
* is in the sector cache (it probably is not).
|
2011-11-20 15:01:44 +01:00
|
|
|
*/
|
2013-03-31 15:06:22 +02:00
|
|
|
|
2011-11-19 21:30:39 +01:00
|
|
|
ret = fat_fscacheread(fs, dirinfo->dir.fd_currsector);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now loop, writing each long file name entry */
|
2011-07-14 19:57:59 +02:00
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
for (; ; )
|
2011-07-14 19:57:59 +02:00
|
|
|
{
|
|
|
|
/* Get the string offset associated with the directory entry. */
|
|
|
|
|
2011-07-15 01:29:43 +02:00
|
|
|
offset = (nentries - 1) * LDIR_MAXLFNCHARS;
|
2011-07-14 19:57:59 +02:00
|
|
|
|
|
|
|
/* Get a reference to the current directory entry */
|
|
|
|
|
2011-07-14 23:07:59 +02:00
|
|
|
diroffset = (dirinfo->dir.fd_index & DIRSEC_NDXMASK(fs)) * DIR_SIZE;
|
2011-07-14 19:57:59 +02:00
|
|
|
direntry = &fs->fs_buffer[diroffset];
|
|
|
|
|
|
|
|
/* Is this the "last" LFN directory entry? */
|
|
|
|
|
|
|
|
if ((seqno & LDIR0_LAST) != 0 && remainder != 0)
|
|
|
|
{
|
|
|
|
int nbytes;
|
|
|
|
|
2011-07-18 02:20:58 +02:00
|
|
|
/* Initialize the "last" directory entry name to all 0xffff */
|
2011-07-14 19:57:59 +02:00
|
|
|
|
2011-07-18 02:20:58 +02:00
|
|
|
fat_initlfname(LDIR_PTRWCHAR1_5(direntry), 5);
|
|
|
|
fat_initlfname(LDIR_PTRWCHAR6_11(direntry), 6);
|
|
|
|
fat_initlfname(LDIR_PTRWCHAR12_13(direntry), 2);
|
2011-07-14 19:57:59 +02:00
|
|
|
|
2020-05-31 17:48:21 +02:00
|
|
|
/* Store the tail portion of the long file name in directory
|
|
|
|
* entry.
|
|
|
|
*/
|
2011-07-14 19:57:59 +02:00
|
|
|
|
|
|
|
nbytes = MIN(5, remainder);
|
|
|
|
fat_putlfnchunk(LDIR_PTRWCHAR1_5(direntry),
|
|
|
|
&dirinfo->fd_lfname[offset], nbytes);
|
|
|
|
remainder -= nbytes;
|
|
|
|
|
|
|
|
if (remainder > 0)
|
|
|
|
{
|
2011-07-15 17:37:29 +02:00
|
|
|
nbytes = MIN(6, remainder);
|
2011-07-14 19:57:59 +02:00
|
|
|
fat_putlfnchunk(LDIR_PTRWCHAR6_11(direntry),
|
2019-03-01 22:00:00 +01:00
|
|
|
&dirinfo->fd_lfname[offset + 5], nbytes);
|
2011-07-14 19:57:59 +02:00
|
|
|
remainder -= nbytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (remainder > 0)
|
|
|
|
{
|
|
|
|
nbytes = MIN(2, remainder);
|
|
|
|
fat_putlfnchunk(LDIR_PTRWCHAR12_13(direntry),
|
2019-03-01 22:00:00 +01:00
|
|
|
&dirinfo->fd_lfname[offset + 11], nbytes);
|
2011-07-14 19:57:59 +02:00
|
|
|
remainder -= nbytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The remainder should now be zero */
|
2013-03-31 15:06:22 +02:00
|
|
|
|
2011-07-14 19:57:59 +02:00
|
|
|
DEBUGASSERT(remainder == 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Store a portion long file name in this directory entry */
|
|
|
|
|
|
|
|
fat_putlfnchunk(LDIR_PTRWCHAR1_5(direntry),
|
|
|
|
&dirinfo->fd_lfname[offset], 5);
|
|
|
|
fat_putlfnchunk(LDIR_PTRWCHAR6_11(direntry),
|
2019-03-01 22:00:00 +01:00
|
|
|
&dirinfo->fd_lfname[offset + 5], 6);
|
2011-07-14 19:57:59 +02:00
|
|
|
fat_putlfnchunk(LDIR_PTRWCHAR12_13(direntry),
|
2019-03-01 22:00:00 +01:00
|
|
|
&dirinfo->fd_lfname[offset + 11], 2);
|
2011-07-14 19:57:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Write the remaining directory entries */
|
|
|
|
|
|
|
|
LDIR_PUTSEQ(direntry, seqno);
|
|
|
|
LDIR_PUTATTRIBUTES(direntry, LDDIR_LFNATTR);
|
|
|
|
LDIR_PUTNTRES(direntry, 0);
|
|
|
|
LDIR_PUTCHECKSUM(direntry, checksum);
|
2022-06-30 07:13:46 +02:00
|
|
|
LDIR_PUTFSTCLUSTLO(direntry, 0);
|
2011-07-14 19:57:59 +02:00
|
|
|
fs->fs_dirty = true;
|
|
|
|
|
|
|
|
/* Read next directory entry */
|
|
|
|
|
2011-07-14 23:07:59 +02:00
|
|
|
if (fat_nextdirentry(fs, &dirinfo->dir) != OK)
|
2011-07-14 19:57:59 +02:00
|
|
|
{
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure that the sector containing the directory entry is in the
|
|
|
|
* sector cache
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = fat_fscacheread(fs, dirinfo->dir.fd_currsector);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Decrement the number of entries and get the next sequence number. */
|
|
|
|
|
|
|
|
if (--nentries <= 0)
|
|
|
|
{
|
|
|
|
/* We have written all of the long file name entries to the media
|
|
|
|
* and we have the short file name entry in the cache. We can
|
|
|
|
* just return success.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The sequence number is just the number of entries left to be
|
|
|
|
* written.
|
|
|
|
*/
|
|
|
|
|
|
|
|
seqno = nentries;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: fat_putsfdirentry
|
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description: Write a short file name directory entry
|
2011-07-14 19:57:59 +02:00
|
|
|
*
|
|
|
|
* Assumption: The directory sector is in the cache. The caller will write
|
|
|
|
* sector information.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-31 17:48:21 +02:00
|
|
|
static int fat_putsfdirentry(FAR struct fat_mountpt_s *fs,
|
|
|
|
FAR struct fat_dirinfo_s *dirinfo,
|
2011-07-14 19:57:59 +02:00
|
|
|
uint8_t attributes, uint32_t fattime)
|
|
|
|
{
|
2020-05-31 17:48:21 +02:00
|
|
|
FAR uint8_t *direntry;
|
2011-07-14 19:57:59 +02:00
|
|
|
|
|
|
|
/* Initialize the 32-byte directory entry */
|
|
|
|
|
|
|
|
direntry = &fs->fs_buffer[dirinfo->fd_seq.ds_offset];
|
|
|
|
memset(direntry, 0, DIR_SIZE);
|
|
|
|
|
|
|
|
/* Directory name info */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
fat_putsfname(fs, dirinfo);
|
2011-07-14 19:57:59 +02:00
|
|
|
|
|
|
|
/* Set the attribute attribute, write time, creation time */
|
|
|
|
|
|
|
|
DIR_PUTATTRIBUTES(direntry, attributes);
|
|
|
|
|
|
|
|
/* Set the time information */
|
|
|
|
|
|
|
|
DIR_PUTWRTTIME(direntry, fattime & 0xffff);
|
|
|
|
DIR_PUTCRTIME(direntry, fattime & 0xffff);
|
|
|
|
DIR_PUTWRTDATE(direntry, fattime >> 16);
|
|
|
|
DIR_PUTCRDATE(direntry, fattime >> 16);
|
|
|
|
|
|
|
|
fs->fs_dirty = true;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: fat_finddirentry
|
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description: Given a path to something that may or may not be in the file
|
2011-07-14 19:57:59 +02:00
|
|
|
* system, return the description of the directory entry of the requested
|
|
|
|
* item.
|
|
|
|
*
|
|
|
|
* NOTE: As a side effect, this function returns with the sector containing
|
|
|
|
* the short file name directory entry in the cache.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-31 17:48:21 +02:00
|
|
|
int fat_finddirentry(FAR struct fat_mountpt_s *fs,
|
|
|
|
FAR struct fat_dirinfo_s *dirinfo,
|
|
|
|
FAR const char *path)
|
2011-07-14 19:57:59 +02:00
|
|
|
{
|
2020-05-31 17:48:21 +02:00
|
|
|
FAR uint8_t *direntry;
|
|
|
|
off_t cluster;
|
|
|
|
char terminator;
|
|
|
|
int ret;
|
2011-07-14 19:57:59 +02:00
|
|
|
|
|
|
|
/* Initialize to traverse the chain. Set it to the cluster of the root
|
|
|
|
* directory
|
|
|
|
*/
|
|
|
|
|
|
|
|
cluster = fs->fs_rootbase;
|
|
|
|
if (fs->fs_type == FSTYPE_FAT32)
|
|
|
|
{
|
|
|
|
/* For FAT32, the root directory is variable sized and is a cluster
|
|
|
|
* chain like any other directory. fs_rootbase holds the first
|
|
|
|
* cluster of the root directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
dirinfo->dir.fd_startcluster = cluster;
|
|
|
|
dirinfo->dir.fd_currcluster = cluster;
|
|
|
|
dirinfo->dir.fd_currsector = fat_cluster2sector(fs, cluster);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* For FAT12/16, the first sector of the root directory is a sector
|
|
|
|
* relative to the first sector of the fat volume.
|
|
|
|
*/
|
|
|
|
|
|
|
|
dirinfo->dir.fd_startcluster = 0;
|
|
|
|
dirinfo->dir.fd_currcluster = 0;
|
|
|
|
dirinfo->dir.fd_currsector = cluster;
|
|
|
|
}
|
|
|
|
|
2011-11-19 21:30:39 +01:00
|
|
|
/* fd_index is the index into the current directory table. It is set to the
|
2024-02-05 10:20:51 +01:00
|
|
|
* the first entry in the root directory.
|
2011-07-18 20:28:02 +02:00
|
|
|
*/
|
2011-07-14 19:57:59 +02:00
|
|
|
|
2011-11-19 21:30:39 +01:00
|
|
|
dirinfo->dir.fd_index = 0;
|
2011-07-14 19:57:59 +02:00
|
|
|
|
|
|
|
/* If no path was provided, then the root directory must be exactly what
|
|
|
|
* the caller is looking for.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (*path == '\0')
|
|
|
|
{
|
|
|
|
dirinfo->fd_root = true;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2011-07-15 17:37:29 +02:00
|
|
|
/* This is not the root directory */
|
|
|
|
|
|
|
|
dirinfo->fd_root = false;
|
|
|
|
|
|
|
|
/* Now loop until the directory entry corresponding to the path is found */
|
2011-07-14 19:57:59 +02:00
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
for (; ; )
|
2011-07-14 19:57:59 +02:00
|
|
|
{
|
|
|
|
/* Convert the next the path segment name into the kind of name that
|
|
|
|
* we would see in the directory entry.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = fat_path2dirname(&path, dirinfo, &terminator);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* ERROR: The filename contains invalid characters or is
|
|
|
|
* too long.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is this a path segment a long or a short file. Was a long file
|
|
|
|
* name parsed?
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_FAT_LFN
|
2011-07-14 23:07:59 +02:00
|
|
|
if (dirinfo->fd_lfname[0] != '\0')
|
2011-07-14 19:57:59 +02:00
|
|
|
{
|
|
|
|
/* Yes.. Search for the sequence of long file name directory
|
|
|
|
* entries. NOTE: As a side effect, this function returns with
|
|
|
|
* the sector containing the short file name directory entry
|
|
|
|
* in the cache.
|
|
|
|
*/
|
2013-03-31 15:06:22 +02:00
|
|
|
|
2011-07-14 19:57:59 +02:00
|
|
|
ret = fat_findlfnentry(fs, dirinfo);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* No.. Search for the single short file name directory entry */
|
|
|
|
|
|
|
|
ret = fat_findsfnentry(fs, dirinfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Did we find the directory entries? */
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-02-23 09:50:23 +01:00
|
|
|
/* A return value of -ENOENT would mean that the path segment
|
2013-03-20 19:22:21 +01:00
|
|
|
* was not found. Let's distinguish two cases: (1) the final
|
2013-03-20 16:26:50 +01:00
|
|
|
* file was not found in the directory (-ENOENT), or (2) one
|
|
|
|
* of the directory path segments does not exist (-ENOTDIR)
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ret == -ENOENT && terminator != '\0')
|
|
|
|
{
|
2013-03-20 19:22:21 +01:00
|
|
|
ret = -ENOTDIR;
|
2013-03-20 16:26:50 +01:00
|
|
|
}
|
|
|
|
|
2011-07-14 19:57:59 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the terminator character in the path was the end of the string
|
|
|
|
* then we have successfully found the directory entry that describes
|
|
|
|
* the path.
|
|
|
|
*/
|
|
|
|
|
2013-03-20 16:26:50 +01:00
|
|
|
if (terminator == '\0')
|
2011-07-14 19:57:59 +02:00
|
|
|
{
|
2024-02-05 10:20:51 +01:00
|
|
|
/* Return success meaning that the description matching the
|
2011-07-14 19:57:59 +02:00
|
|
|
* directory entry is in dirinfo.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No.. then we have found one of the intermediate directories on
|
|
|
|
* the way to the final path target. In this case, make sure
|
|
|
|
* the thing that we found is, indeed, a directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
direntry = &fs->fs_buffer[dirinfo->fd_seq.ds_offset];
|
|
|
|
if (!(DIR_GETATTRIBUTES(direntry) & FATATTR_DIRECTORY))
|
|
|
|
{
|
|
|
|
/* Ooops.. we found something else */
|
|
|
|
|
|
|
|
return -ENOTDIR;
|
|
|
|
}
|
|
|
|
|
2020-09-22 08:44:41 +02:00
|
|
|
if (*path == '\0')
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2011-07-14 19:57:59 +02:00
|
|
|
/* Get the cluster number of this directory */
|
|
|
|
|
|
|
|
cluster =
|
|
|
|
((uint32_t)DIR_GETFSTCLUSTHI(direntry) << 16) |
|
|
|
|
DIR_GETFSTCLUSTLO(direntry);
|
|
|
|
|
2011-11-15 23:37:14 +01:00
|
|
|
/* Then restart scanning at the new directory, skipping over both the
|
|
|
|
* '.' and '..' entries that exist in all directories EXCEPT the root
|
|
|
|
* directory.
|
|
|
|
*/
|
2011-07-14 19:57:59 +02:00
|
|
|
|
2011-07-18 20:28:02 +02:00
|
|
|
dirinfo->dir.fd_startcluster = cluster;
|
|
|
|
dirinfo->dir.fd_currcluster = cluster;
|
|
|
|
dirinfo->dir.fd_currsector = fat_cluster2sector(fs, cluster);
|
|
|
|
dirinfo->dir.fd_index = 2;
|
2011-07-14 19:57:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: fat_allocatedirentry
|
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description:
|
2013-03-31 15:06:22 +02:00
|
|
|
* Find (or allocate) all needed directory entries to contain the file name
|
2011-07-14 19:57:59 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-31 17:48:21 +02:00
|
|
|
int fat_allocatedirentry(FAR struct fat_mountpt_s *fs,
|
|
|
|
FAR struct fat_dirinfo_s *dirinfo)
|
2011-07-14 19:57:59 +02:00
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
int32_t cluster;
|
|
|
|
int32_t prevcluster;
|
|
|
|
off_t sector;
|
|
|
|
int ret;
|
|
|
|
int i;
|
2011-07-14 19:57:59 +02:00
|
|
|
|
|
|
|
/* Re-initialize directory object */
|
|
|
|
|
|
|
|
cluster = dirinfo->dir.fd_startcluster;
|
|
|
|
|
|
|
|
/* Loop until we successfully allocate the sequence of directory entries
|
|
|
|
* or until to fail to extend the directory cluster chain.
|
|
|
|
*/
|
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
for (; ; )
|
2011-07-14 19:57:59 +02:00
|
|
|
{
|
|
|
|
/* Can this cluster chain be extended */
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-13 23:53:06 +02:00
|
|
|
if (cluster)
|
2011-07-13 00:38:21 +02:00
|
|
|
{
|
2019-10-27 18:48:14 +01:00
|
|
|
/* Cluster chain can be extended */
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-13 23:53:06 +02:00
|
|
|
dirinfo->dir.fd_currcluster = cluster;
|
|
|
|
dirinfo->dir.fd_currsector = fat_cluster2sector(fs, cluster);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-07-15 17:37:29 +02:00
|
|
|
/* Fixed size FAT12/16 root directory is at fixed offset/size */
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-13 23:53:06 +02:00
|
|
|
dirinfo->dir.fd_currsector = fs->fs_rootbase;
|
|
|
|
}
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-11-19 21:30:39 +01:00
|
|
|
/* Start at the first entry in the root directory. */
|
2011-07-18 20:28:02 +02:00
|
|
|
|
2011-11-19 21:30:39 +01:00
|
|
|
dirinfo->dir.fd_index = 0;
|
2013-03-31 15:06:22 +02:00
|
|
|
|
2011-07-13 23:53:06 +02:00
|
|
|
/* Is this a path segment a long or a short file. Was a long file
|
|
|
|
* name parsed?
|
|
|
|
*/
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-13 23:53:06 +02:00
|
|
|
#ifdef CONFIG_FAT_LFN
|
2011-07-14 23:07:59 +02:00
|
|
|
if (dirinfo->fd_lfname[0] != '\0')
|
2011-07-13 00:38:21 +02:00
|
|
|
{
|
2011-07-13 23:53:06 +02:00
|
|
|
/* Yes.. Allocate for the sequence of long file name directory
|
|
|
|
* entries plus a short file name directory entry.
|
|
|
|
*/
|
2013-03-31 15:06:22 +02:00
|
|
|
|
2011-07-13 23:53:06 +02:00
|
|
|
ret = fat_allocatelfnentry(fs, dirinfo);
|
|
|
|
}
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-13 23:53:06 +02:00
|
|
|
/* No.. Allocate only a short file name directory entry */
|
|
|
|
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
ret = fat_allocatesfnentry(fs, dirinfo);
|
2011-07-13 00:38:21 +02:00
|
|
|
}
|
|
|
|
|
2011-07-13 23:53:06 +02:00
|
|
|
/* Did we successfully allocate the directory entries? If the error
|
|
|
|
* value is -ENOSPC, then we can try to extend the directory cluster
|
|
|
|
* (we can't handle other return values)
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ret == OK || ret != -ENOSPC)
|
2011-07-13 00:38:21 +02:00
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-07-13 23:53:06 +02:00
|
|
|
/* If we get here, then we have reached the end of the directory table
|
|
|
|
* in this sector without finding a free directory entry.
|
|
|
|
*
|
2011-07-18 20:28:02 +02:00
|
|
|
* It this is a fixed size directory entry, then this is an error.
|
2011-07-13 23:53:06 +02:00
|
|
|
* Otherwise, we can try to extend the directory cluster chain to
|
|
|
|
* make space for the new directory entry.
|
|
|
|
*/
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-13 23:53:06 +02:00
|
|
|
if (!cluster)
|
|
|
|
{
|
|
|
|
/* The size is fixed */
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-13 23:53:06 +02:00
|
|
|
return -ENOSPC;
|
|
|
|
}
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-13 23:53:06 +02:00
|
|
|
/* Try to extend the cluster chain for this directory */
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2013-03-31 15:06:22 +02:00
|
|
|
prevcluster = cluster;
|
|
|
|
cluster = fat_extendchain(fs, dirinfo->dir.fd_currcluster);
|
|
|
|
|
2011-07-13 23:53:06 +02:00
|
|
|
if (cluster < 0)
|
|
|
|
{
|
|
|
|
return cluster;
|
|
|
|
}
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
/* Flush out any cached data in fs_buffer.. we are going to use
|
|
|
|
* it to initialize the new directory cluster.
|
|
|
|
*/
|
2011-07-13 23:53:06 +02:00
|
|
|
|
|
|
|
ret = fat_fscacheflush(fs);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-13 23:53:06 +02:00
|
|
|
/* Clear all sectors comprising the new directory cluster */
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-13 23:53:06 +02:00
|
|
|
fs->fs_currentsector = fat_cluster2sector(fs, cluster);
|
|
|
|
memset(fs->fs_buffer, 0, fs->fs_hwsectorsize);
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-16 17:52:15 +02:00
|
|
|
sector = fs->fs_currentsector;
|
2011-07-13 23:53:06 +02:00
|
|
|
for (i = fs->fs_fatsecperclus; i; i--)
|
2011-07-13 00:38:21 +02:00
|
|
|
{
|
2011-07-13 23:53:06 +02:00
|
|
|
ret = fat_hwwrite(fs, fs->fs_buffer, sector, 1);
|
2011-09-16 16:07:17 +02:00
|
|
|
if (ret < 0)
|
2011-07-13 23:53:06 +02:00
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2013-03-31 15:06:22 +02:00
|
|
|
|
2011-07-13 23:53:06 +02:00
|
|
|
sector++;
|
2011-07-13 00:38:21 +02:00
|
|
|
}
|
2013-03-31 15:06:22 +02:00
|
|
|
|
|
|
|
/* Start the search again */
|
|
|
|
|
|
|
|
cluster = prevcluster;
|
2011-07-13 00:38:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: fat_freedirentry
|
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description: Free the directory entry.
|
2011-07-13 00:38:21 +02:00
|
|
|
*
|
2011-07-14 19:57:59 +02:00
|
|
|
* NOTE: As a side effect, this function returns with the sector containing
|
|
|
|
* the deleted short file name directory entry in the cache.
|
|
|
|
*
|
2011-07-13 00:38:21 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-31 17:48:21 +02:00
|
|
|
int fat_freedirentry(FAR struct fat_mountpt_s *fs, struct fat_dirseq_s *seq)
|
2011-07-13 00:38:21 +02:00
|
|
|
{
|
2011-07-13 23:53:06 +02:00
|
|
|
#ifdef CONFIG_FAT_LFN
|
2011-07-14 23:07:59 +02:00
|
|
|
struct fs_fatdir_s dir;
|
2020-05-31 17:48:21 +02:00
|
|
|
FAR uint8_t *direntry;
|
2011-07-13 23:53:06 +02:00
|
|
|
uint16_t diroffset;
|
2020-05-31 17:48:21 +02:00
|
|
|
off_t startsector;
|
|
|
|
int ret;
|
2011-07-13 15:30:38 +02:00
|
|
|
|
2011-07-13 23:53:06 +02:00
|
|
|
/* Set it to the cluster containing the "last" LFN entry (that appears
|
|
|
|
* first on the media).
|
|
|
|
*/
|
|
|
|
|
2011-07-15 19:38:29 +02:00
|
|
|
dir.fd_currcluster = seq->ds_lfncluster;
|
|
|
|
dir.fd_currsector = seq->ds_lfnsector;
|
|
|
|
dir.fd_index = seq->ds_lfnoffset / DIR_SIZE;
|
2011-07-13 23:53:06 +02:00
|
|
|
|
2013-03-31 15:06:22 +02:00
|
|
|
/* Remember that ds_lfnoffset is the offset in the sector and not the
|
|
|
|
* cluster.
|
|
|
|
*/
|
|
|
|
|
|
|
|
startsector = fat_cluster2sector(fs, dir.fd_currcluster);
|
|
|
|
dir.fd_index += (dir.fd_currsector - startsector) * DIRSEC_NDIRS(fs);
|
|
|
|
|
2020-05-31 17:48:21 +02:00
|
|
|
/* Free all of the directory entries used for the sequence of long file
|
|
|
|
* name and for the single short file name entry.
|
2011-07-13 23:53:06 +02:00
|
|
|
*/
|
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
for (; ; )
|
2011-07-13 23:53:06 +02:00
|
|
|
{
|
|
|
|
/* Read the directory sector into the sector cache */
|
|
|
|
|
2011-07-14 23:07:59 +02:00
|
|
|
ret = fat_fscacheread(fs, dir.fd_currsector);
|
2011-07-13 23:53:06 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a pointer to the directory entry */
|
|
|
|
|
2011-07-14 23:07:59 +02:00
|
|
|
diroffset = (dir.fd_index & DIRSEC_NDXMASK(fs)) * DIR_SIZE;
|
2011-07-13 23:53:06 +02:00
|
|
|
direntry = &fs->fs_buffer[diroffset];
|
|
|
|
|
|
|
|
/* Then mark the entry as deleted */
|
|
|
|
|
|
|
|
direntry[DIR_NAME] = DIR0_EMPTY;
|
|
|
|
fs->fs_dirty = true;
|
|
|
|
|
|
|
|
/* Did we just free the single short file name entry? */
|
|
|
|
|
2011-07-14 23:07:59 +02:00
|
|
|
if (dir.fd_currsector == seq->ds_sector &&
|
|
|
|
diroffset == seq->ds_offset)
|
2011-07-13 23:53:06 +02:00
|
|
|
{
|
|
|
|
/* Yes.. then we are finished. flush anything remaining in the
|
|
|
|
* cache and return, probably successfully.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return fat_fscacheflush(fs);
|
|
|
|
}
|
|
|
|
|
2011-07-15 19:38:29 +02:00
|
|
|
/* There are more entries to go.. Try the next directory entry */
|
2011-07-13 23:53:06 +02:00
|
|
|
|
2011-07-14 23:07:59 +02:00
|
|
|
ret = fat_nextdirentry(fs, &dir);
|
2011-07-13 23:53:06 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2020-05-31 17:48:21 +02:00
|
|
|
FAR uint8_t *direntry;
|
2022-09-06 08:18:45 +02:00
|
|
|
int ret;
|
2011-07-13 23:53:06 +02:00
|
|
|
|
|
|
|
/* Free the single short file name entry.
|
|
|
|
*
|
|
|
|
* Make sure that the sector containing the directory entry is in the
|
2011-07-13 15:30:38 +02:00
|
|
|
* cache.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = fat_fscacheread(fs, seq->ds_sector);
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
/* Then mark the entry as deleted */
|
|
|
|
|
|
|
|
direntry = &fs->fs_buffer[seq->ds_offset];
|
|
|
|
direntry[DIR_NAME] = DIR0_EMPTY;
|
|
|
|
fs->fs_dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2011-07-13 23:53:06 +02:00
|
|
|
#endif
|
2011-07-13 00:38:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: fat_dirname2path
|
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description: Convert a filename in a raw directory entry into a user
|
2011-07-13 00:38:21 +02:00
|
|
|
* filename. This is essentially the inverse operation of that performed
|
|
|
|
* by fat_path2dirname. See that function for more details.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-31 17:48:21 +02:00
|
|
|
int fat_dirname2path(FAR struct fat_mountpt_s *fs,
|
2022-07-26 08:56:41 +02:00
|
|
|
FAR struct fs_dirent_s *dir,
|
|
|
|
FAR struct dirent *entry)
|
2011-07-13 00:38:21 +02:00
|
|
|
{
|
2022-08-05 06:31:25 +02:00
|
|
|
FAR struct fat_dirent_s *fdir;
|
2011-07-14 23:07:59 +02:00
|
|
|
uint16_t diroffset;
|
2020-05-31 17:48:21 +02:00
|
|
|
FAR uint8_t *direntry;
|
2011-07-14 19:57:59 +02:00
|
|
|
#ifdef CONFIG_FAT_LFN
|
2011-07-14 23:07:59 +02:00
|
|
|
uint8_t attribute;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Get a reference to the current directory entry */
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
fdir = (FAR struct fat_dirent_s *)dir;
|
|
|
|
diroffset = (fdir->dir.fd_index & DIRSEC_NDXMASK(fs)) * DIR_SIZE;
|
2011-07-14 23:07:59 +02:00
|
|
|
direntry = &fs->fs_buffer[diroffset];
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-14 19:57:59 +02:00
|
|
|
/* Does this entry refer to the last entry of a long file name? */
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-14 23:07:59 +02:00
|
|
|
#ifdef CONFIG_FAT_LFN
|
|
|
|
attribute = DIR_GETATTRIBUTES(direntry);
|
2011-07-14 19:57:59 +02:00
|
|
|
if (((*direntry & LDIR0_LAST) != 0 && attribute == LDDIR_LFNATTR))
|
|
|
|
{
|
|
|
|
/* Yes.. Get the name from a sequence of long file name directory
|
|
|
|
* entries.
|
|
|
|
*/
|
2013-03-31 15:06:22 +02:00
|
|
|
|
2022-07-26 08:56:41 +02:00
|
|
|
return fat_getlfname(fs, dir, entry);
|
2011-07-14 19:57:59 +02:00
|
|
|
}
|
|
|
|
else
|
2011-07-13 00:38:21 +02:00
|
|
|
#endif
|
2011-07-14 19:57:59 +02:00
|
|
|
{
|
|
|
|
/* No.. Get the name from a short file name directory entries */
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2022-07-26 08:56:41 +02:00
|
|
|
return fat_getsfname(direntry, entry->d_name, NAME_MAX + 1);
|
2011-07-14 19:57:59 +02:00
|
|
|
}
|
2011-07-13 00:38:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: fat_dirnamewrite
|
|
|
|
*
|
2020-05-31 17:48:21 +02:00
|
|
|
* Description:
|
|
|
|
* Write the (possibly long) directory entry name. This function is
|
|
|
|
* called only from fat_rename to write the new file name.
|
2011-07-13 00:38:21 +02:00
|
|
|
*
|
2020-05-31 17:48:21 +02:00
|
|
|
* Assumption:
|
|
|
|
* The directory sector containing the short file name entry is in the
|
|
|
|
* cache. *NOT* the sector containing the last long file name entry!
|
2011-07-13 00:38:21 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-31 17:48:21 +02:00
|
|
|
int fat_dirnamewrite(FAR struct fat_mountpt_s *fs,
|
|
|
|
FAR struct fat_dirinfo_s *dirinfo)
|
2011-07-13 00:38:21 +02:00
|
|
|
{
|
2011-07-14 23:07:59 +02:00
|
|
|
#ifdef CONFIG_FAT_LFN
|
|
|
|
int ret;
|
|
|
|
|
2011-07-14 19:57:59 +02:00
|
|
|
/* Is this a long file name? */
|
|
|
|
|
2011-07-14 23:07:59 +02:00
|
|
|
if (dirinfo->fd_lfname[0] != '\0')
|
2011-07-14 19:57:59 +02:00
|
|
|
{
|
2020-05-31 17:48:21 +02:00
|
|
|
/* Write the sequence of long file name directory entries (this
|
|
|
|
* function also creates the short file name alias).
|
2011-07-14 19:57:59 +02:00
|
|
|
*/
|
2013-03-31 15:06:22 +02:00
|
|
|
|
2011-07-14 19:57:59 +02:00
|
|
|
ret = fat_putlfname(fs, dirinfo);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* On return, fat_lfsfname() will leave the short file name entry in the
|
2020-05-31 17:48:21 +02:00
|
|
|
* cache. So we can just fall through to write that directory entry,
|
|
|
|
* perhaps using the short file name alias for the long file name.
|
2011-07-14 19:57:59 +02:00
|
|
|
*/
|
2019-12-01 20:01:16 +01:00
|
|
|
|
2011-07-13 00:38:21 +02:00
|
|
|
#endif
|
2011-07-14 19:57:59 +02:00
|
|
|
|
|
|
|
return fat_putsfname(fs, dirinfo);
|
2011-07-13 00:38:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: fat_dirwrite
|
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description: Write a directory entry, possibly with a long file name.
|
2011-07-14 19:57:59 +02:00
|
|
|
* Called from:
|
|
|
|
*
|
|
|
|
* fat_mkdir() to write the new FAT directory entry.
|
|
|
|
* fat_dircreate() to create any new directory entry.
|
2011-07-13 00:38:21 +02:00
|
|
|
*
|
|
|
|
* Assumption: The directory sector is in the cache. The caller will write
|
|
|
|
* sector information.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-31 17:48:21 +02:00
|
|
|
int fat_dirwrite(FAR struct fat_mountpt_s *fs,
|
|
|
|
FAR struct fat_dirinfo_s *dirinfo,
|
2011-07-13 00:38:21 +02:00
|
|
|
uint8_t attributes, uint32_t fattime)
|
|
|
|
{
|
2011-07-14 23:07:59 +02:00
|
|
|
#ifdef CONFIG_FAT_LFN
|
|
|
|
int ret;
|
|
|
|
|
2011-07-14 19:57:59 +02:00
|
|
|
/* Does this directory entry have a long file name? */
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-14 23:07:59 +02:00
|
|
|
if (dirinfo->fd_lfname[0] != '\0')
|
2011-07-14 19:57:59 +02:00
|
|
|
{
|
2020-05-31 17:48:21 +02:00
|
|
|
/* Write the sequence of long file name directory entries (this
|
|
|
|
* function also creates the short file name alias).
|
2011-07-14 23:07:59 +02:00
|
|
|
*/
|
2013-03-31 15:06:22 +02:00
|
|
|
|
2011-07-14 23:07:59 +02:00
|
|
|
ret = fat_putlfname(fs, dirinfo);
|
2011-07-14 19:57:59 +02:00
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-14 19:57:59 +02:00
|
|
|
/* On return, fat_lfsfname() will leave the short file name entry in the
|
2020-05-31 17:48:21 +02:00
|
|
|
* cache. So we can just fall through to write that directory entry,
|
|
|
|
* perhaps using the short file name alias for the long file name.
|
2011-07-14 19:57:59 +02:00
|
|
|
*/
|
2019-12-01 20:01:16 +01:00
|
|
|
|
2011-07-14 19:57:59 +02:00
|
|
|
#endif
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-14 19:57:59 +02:00
|
|
|
/* Put the short file name entry data */
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-14 19:57:59 +02:00
|
|
|
return fat_putsfdirentry(fs, dirinfo, attributes, fattime);
|
2011-07-13 00:38:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: fat_dircreate
|
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description: Create a directory entry for a new file
|
2011-07-13 00:38:21 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-31 17:48:21 +02:00
|
|
|
int fat_dircreate(FAR struct fat_mountpt_s *fs,
|
|
|
|
FAR struct fat_dirinfo_s *dirinfo)
|
2011-07-13 00:38:21 +02:00
|
|
|
{
|
|
|
|
uint32_t fattime;
|
|
|
|
int ret;
|
|
|
|
|
2011-07-14 19:57:59 +02:00
|
|
|
/* Allocate a directory entry. If long file name support is enabled, then
|
|
|
|
* this might, in fact, allocate a sequence of directory entries.
|
|
|
|
*/
|
2011-07-13 00:38:21 +02:00
|
|
|
|
|
|
|
ret = fat_allocatedirentry(fs, dirinfo);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
2011-07-14 19:57:59 +02:00
|
|
|
/* Failed to allocate the required directory entry or entries. */
|
2011-07-13 00:38:21 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-07-14 19:57:59 +02:00
|
|
|
/* Write the directory entry (or entries) with the current time and the
|
|
|
|
* ARCHIVE attribute.
|
|
|
|
*/
|
2011-07-13 00:38:21 +02:00
|
|
|
|
|
|
|
fattime = fat_systime2fattime();
|
|
|
|
return fat_dirwrite(fs, dirinfo, FATATTR_ARCHIVE, fattime);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: fat_remove
|
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description: Remove a directory or file from the file system. This
|
2011-07-13 00:38:21 +02:00
|
|
|
* implements both rmdir() and unlink().
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-31 17:48:21 +02:00
|
|
|
int fat_remove(FAR struct fat_mountpt_s *fs, FAR const char *relpath,
|
|
|
|
bool directory)
|
2011-07-13 00:38:21 +02:00
|
|
|
{
|
|
|
|
struct fat_dirinfo_s dirinfo;
|
2011-07-13 15:30:38 +02:00
|
|
|
uint32_t dircluster;
|
2024-08-25 01:21:12 +02:00
|
|
|
FAR uint8_t *direntry;
|
2011-07-13 15:30:38 +02:00
|
|
|
int ret;
|
2011-07-13 00:38:21 +02:00
|
|
|
|
|
|
|
/* Find the directory entry referring to the entry to be deleted */
|
|
|
|
|
|
|
|
ret = fat_finddirentry(fs, &dirinfo, relpath);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
2013-03-20 16:26:50 +01:00
|
|
|
/* Most likely, some element of the path does not exist. */
|
2011-07-13 00:38:21 +02:00
|
|
|
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if this is a FAT12/16 root directory */
|
|
|
|
|
2011-07-13 15:30:38 +02:00
|
|
|
if (dirinfo.fd_root)
|
2011-07-13 00:38:21 +02:00
|
|
|
{
|
|
|
|
/* The root directory cannot be removed */
|
|
|
|
|
|
|
|
return -EPERM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The object has to have write access to be deleted */
|
|
|
|
|
2011-07-13 15:30:38 +02:00
|
|
|
direntry = &fs->fs_buffer[dirinfo.fd_seq.ds_offset];
|
|
|
|
if ((DIR_GETATTRIBUTES(direntry) & FATATTR_READONLY) != 0)
|
2011-07-13 00:38:21 +02:00
|
|
|
{
|
|
|
|
/* It is a read-only entry */
|
|
|
|
|
|
|
|
return -EACCES;
|
|
|
|
}
|
|
|
|
|
2020-05-31 17:48:21 +02:00
|
|
|
/* Get the directory sector and cluster containing the entry to be
|
|
|
|
* deleted.
|
|
|
|
*/
|
2011-07-13 00:38:21 +02:00
|
|
|
|
|
|
|
dircluster =
|
2011-07-13 15:30:38 +02:00
|
|
|
((uint32_t)DIR_GETFSTCLUSTHI(direntry) << 16) |
|
|
|
|
DIR_GETFSTCLUSTLO(direntry);
|
2011-07-13 00:38:21 +02:00
|
|
|
|
|
|
|
/* Is this entry a directory? */
|
|
|
|
|
2011-07-13 15:30:38 +02:00
|
|
|
if (DIR_GETATTRIBUTES(direntry) & FATATTR_DIRECTORY)
|
2011-07-13 00:38:21 +02:00
|
|
|
{
|
|
|
|
/* It is a sub-directory. Check if we are be asked to remove
|
|
|
|
* a directory or a file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!directory)
|
|
|
|
{
|
|
|
|
/* We are asked to delete a file */
|
|
|
|
|
|
|
|
return -EISDIR;
|
|
|
|
}
|
|
|
|
|
2011-11-15 23:37:14 +01:00
|
|
|
/* We are asked to delete a directory. Check if this sub-directory is
|
|
|
|
* empty (i.e., that there are no valid entries other than the initial
|
|
|
|
* '.' and '..' entries).
|
2011-07-13 00:38:21 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
dirinfo.dir.fd_currcluster = dircluster;
|
|
|
|
dirinfo.dir.fd_currsector = fat_cluster2sector(fs, dircluster);
|
|
|
|
dirinfo.dir.fd_index = 2;
|
|
|
|
|
2011-07-14 19:57:59 +02:00
|
|
|
/* Loop until either (1) an entry is found in the directory (error),
|
|
|
|
* (2) the directory is found to be empty, or (3) some error occurs.
|
2011-07-13 00:38:21 +02:00
|
|
|
*/
|
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
for (; ; )
|
2011-07-13 00:38:21 +02:00
|
|
|
{
|
|
|
|
unsigned int subdirindex;
|
|
|
|
uint8_t *subdirentry;
|
|
|
|
|
2011-07-14 19:57:59 +02:00
|
|
|
/* Make sure that the sector containing the of the subdirectory
|
|
|
|
* sector is in the cache
|
2011-07-13 00:38:21 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
ret = fat_fscacheread(fs, dirinfo.dir.fd_currsector);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a reference to the next entry in the directory */
|
|
|
|
|
2020-05-31 17:48:21 +02:00
|
|
|
subdirindex = (dirinfo.dir.fd_index & DIRSEC_NDXMASK(fs)) *
|
|
|
|
DIR_SIZE;
|
2011-07-13 00:38:21 +02:00
|
|
|
subdirentry = &fs->fs_buffer[subdirindex];
|
|
|
|
|
2020-02-23 09:50:23 +01:00
|
|
|
/* Is this the last entry in the directory? */
|
2011-07-13 00:38:21 +02:00
|
|
|
|
|
|
|
if (subdirentry[DIR_NAME] == DIR0_ALLEMPTY)
|
|
|
|
{
|
|
|
|
/* Yes then the directory is empty. Break out of the
|
|
|
|
* loop and delete the directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the next entry refers to a file or directory */
|
|
|
|
|
|
|
|
if (subdirentry[DIR_NAME] != DIR0_EMPTY &&
|
|
|
|
!(DIR_GETATTRIBUTES(subdirentry) & FATATTR_VOLUMEID))
|
|
|
|
{
|
|
|
|
/* The directory is not empty */
|
|
|
|
|
|
|
|
return -ENOTEMPTY;
|
|
|
|
}
|
|
|
|
|
2011-07-13 15:30:38 +02:00
|
|
|
/* Get the next directory entry */
|
2011-07-13 00:38:21 +02:00
|
|
|
|
|
|
|
ret = fat_nextdirentry(fs, &dirinfo.dir);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* It is a file. Check if we are be asked to remove a directory
|
|
|
|
* or a file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (directory)
|
|
|
|
{
|
|
|
|
/* We are asked to remove a directory */
|
|
|
|
|
|
|
|
return -ENOTDIR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-14 19:57:59 +02:00
|
|
|
/* Mark the directory entry 'deleted'. If long file name support is
|
|
|
|
* enabled, then multiple directory entries may be freed.
|
|
|
|
*/
|
2011-07-13 00:38:21 +02:00
|
|
|
|
2011-07-13 15:30:38 +02:00
|
|
|
ret = fat_freedirentry(fs, &dirinfo.fd_seq);
|
2011-07-13 00:38:21 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* And remove the cluster chain making up the subdirectory */
|
|
|
|
|
|
|
|
ret = fat_removechain(fs, dircluster);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update the FSINFO sector (FAT32) */
|
|
|
|
|
|
|
|
ret = fat_updatefsinfo(fs);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|