SolidSyslog coverage report
Current view: top level - Platform/PlusFat/Source - SolidSyslogPlusFatFile.c (source / functions) Coverage Total Hit
Test: SolidSyslog Lines: 100.0 % 105 105
Test Date: 2026-11-12 23:14:35 Functions: 100.0 % 19 19
Branches: 100.0 % 46 46

             Branch data     Line data    Source code
       1                 :             : /* SPDX-FileCopyrightText: Copyright 2026 Cozens Software Solutions Limited
       2                 :             :  * SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0 OR LicenseRef-PolyForm-Internal-Use-1.0.0 OR LicenseRef-COSOSO-Commercial
       3                 :             :  */
       4                 :             : 
       5                 :             : #include "SolidSyslogPlusFatFile.h"
       6                 :             : 
       7                 :             : #include <stdbool.h>
       8                 :             : #include <stddef.h>
       9                 :             : #include <stdint.h>
      10                 :             : 
      11                 :             : #include "SolidSyslogError.h"
      12                 :             : #include "SolidSyslogErrorCategory.h"
      13                 :             : #include "SolidSyslogFileCategories.h"
      14                 :             : #include "SolidSyslogFileDefinition.h"
      15                 :             : #include "SolidSyslogNullFile.h"
      16                 :             : #include "SolidSyslogPlusFatFileErrors.h"
      17                 :             : #include "SolidSyslogPlusFatFilePrivate.h"
      18                 :             : #include "ff_stdio.h"
      19                 :             : 
      20                 :             : const struct SolidSyslogErrorSource SolidSyslogPlusFatFileErrorSource = {"PlusFatFile", SolidSyslogFileErrors_Name};
      21                 :             : 
      22                 :             : static bool PlusFatFile_Open(struct SolidSyslogFile* base, const char* path);
      23                 :             : static void PlusFatFile_Close(struct SolidSyslogFile* base);
      24                 :             : static bool PlusFatFile_IsOpen(struct SolidSyslogFile* base);
      25                 :             : static bool PlusFatFile_Read(struct SolidSyslogFile* base, void* buf, size_t count);
      26                 :             : static bool PlusFatFile_Write(struct SolidSyslogFile* base, const void* buf, size_t count);
      27                 :             : static void PlusFatFile_SeekTo(struct SolidSyslogFile* base, size_t offset);
      28                 :             : static bool PlusFatFile_Size(struct SolidSyslogFile* base, size_t* size);
      29                 :             : static void PlusFatFile_Truncate(struct SolidSyslogFile* base);
      30                 :             : static bool PlusFatFile_Exists(struct SolidSyslogFile* base, const char* path);
      31                 :             : static bool PlusFatFile_Delete(struct SolidSyslogFile* base, const char* path);
      32                 :             : 
      33                 :             : static inline struct SolidSyslogPlusFatFile* PlusFatFile_SelfFromBase(struct SolidSyslogFile* base);
      34                 :             : static inline bool PlusFatFile_IsFileOpen(const struct SolidSyslogPlusFatFile* self);
      35                 :             : static inline int PlusFatFile_ErrnoUnlessOpen(const struct SolidSyslogPlusFatFile* self);
      36                 :             : static inline bool PlusFatFile_IsAbsent(int nativeErrno);
      37                 :             : static inline void PlusFatFile_ReportFailure(enum SolidSyslogFileErrors code, int nativeCode);
      38                 :             : static inline bool PlusFatFile_Flush(struct SolidSyslogPlusFatFile* self);
      39                 :             : static inline bool PlusFatFile_IsRemovedOrAbsent(int status, int removeErrno);
      40                 :             : 
      41                 :          48 : void SolidSyslogPlusFatFile_Initialise(struct SolidSyslogFile* base)
      42                 :             : {
      43                 :          48 :     struct SolidSyslogPlusFatFile* self = PlusFatFile_SelfFromBase(base);
      44                 :          48 :     self->Base.Open = PlusFatFile_Open;
      45                 :          48 :     self->Base.Close = PlusFatFile_Close;
      46                 :          48 :     self->Base.IsOpen = PlusFatFile_IsOpen;
      47                 :          48 :     self->Base.Read = PlusFatFile_Read;
      48                 :          48 :     self->Base.Write = PlusFatFile_Write;
      49                 :          48 :     self->Base.SeekTo = PlusFatFile_SeekTo;
      50                 :          48 :     self->Base.Size = PlusFatFile_Size;
      51                 :          48 :     self->Base.Truncate = PlusFatFile_Truncate;
      52                 :          48 :     self->Base.Exists = PlusFatFile_Exists;
      53                 :          48 :     self->Base.Delete = PlusFatFile_Delete;
      54                 :          48 :     self->Fp = NULL;
      55                 :          48 : }
      56                 :             : 
      57                 :         163 : static inline struct SolidSyslogPlusFatFile* PlusFatFile_SelfFromBase(struct SolidSyslogFile* base)
      58                 :             : {
      59                 :         163 :     return (struct SolidSyslogPlusFatFile*) base;
      60                 :             : }
      61                 :             : 
      62                 :          48 : void SolidSyslogPlusFatFile_Cleanup(struct SolidSyslogFile* base)
      63                 :             : {
      64                 :          48 :     PlusFatFile_Close(base);
      65                 :             :     /* Overwrite the abstract base with the shared NullFile vtable so
      66                 :             :      * use-after-destroy is a safe no-op rather than a NULL-fn-pointer crash. */
      67                 :          48 :     *base = *SolidSyslogNullFile_Get();
      68                 :          48 : }
      69                 :             : 
      70                 :          53 : static void PlusFatFile_Close(struct SolidSyslogFile* base)
      71                 :             : {
      72                 :          53 :     struct SolidSyslogPlusFatFile* self = PlusFatFile_SelfFromBase(base);
      73         [ +  + ]:          53 :     if (PlusFatFile_IsFileOpen(self))
      74                 :             :     {
      75                 :          28 :         int status = ff_fclose(self->Fp);
      76                 :             :         /* The task errno is read straight after the call that set it. */
      77         [ +  + ]:          28 :         int closeErrno = (status != 0) ? stdioGET_ERRNO() : 0;
      78                 :             :         /* The file is released whether or not the close could flush. */
      79                 :          28 :         self->Fp = NULL;
      80         [ +  + ]:          28 :         if (status != 0)
      81                 :             :         {
      82                 :           2 :             PlusFatFile_ReportFailure(SOLIDSYSLOG_FILE_ERROR_CLOSE_FAILED, closeErrno);
      83                 :             :         }
      84                 :             :     }
      85                 :          53 : }
      86                 :             : 
      87                 :             : /* Open-state is carried by the FF_FILE* sentinel: non-NULL once a file is open,
      88                 :             :  * NULL when closed. The single source of truth for every open-state check. */
      89                 :         158 : static inline bool PlusFatFile_IsFileOpen(const struct SolidSyslogPlusFatFile* self)
      90                 :             : {
      91                 :         158 :     return self->Fp != NULL;
      92                 :             : }
      93                 :             : 
      94                 :             : /* The fault, with the code behind it when the call left one. The stdio calls
      95                 :             :  * leave a FreeRTOS errno in the task; FF_FlushCache answers an FF_Error_t. */
      96                 :             : // NOLINTNEXTLINE(bugprone-easily-swappable-parameters) -- every caller passes a named code and the code it read
      97                 :          17 : static inline void PlusFatFile_ReportFailure(enum SolidSyslogFileErrors code, int nativeCode)
      98                 :             : {
      99         [ +  + ]:          17 :     if (nativeCode != 0)
     100                 :             :     {
     101                 :          15 :         SolidSyslog_ErrorWithNativeCode(
     102                 :             :             SOLIDSYSLOG_FILE_IO_FAILED_SEVERITY,
     103                 :             :             &SolidSyslogPlusFatFileErrorSource,
     104                 :             :             SOLIDSYSLOG_CAT_FILE_IO_FAILED,
     105                 :             :             (int32_t) code,
     106                 :             :             (int32_t) nativeCode
     107                 :             :         );
     108                 :             :     }
     109                 :             :     else
     110                 :             :     {
     111                 :           2 :         PlusFatFile_Report(SOLIDSYSLOG_FILE_IO_FAILED_SEVERITY, SOLIDSYSLOG_CAT_FILE_IO_FAILED, code);
     112                 :             :     }
     113                 :          17 : }
     114                 :             : 
     115                 :          32 : static bool PlusFatFile_Open(struct SolidSyslogFile* base, const char* path)
     116                 :             : {
     117                 :          32 :     struct SolidSyslogPlusFatFile* self = PlusFatFile_SelfFromBase(base);
     118                 :             :     /* "r+" opens an existing file without truncating. Fall back to the
     119                 :             :      * file-creating "w+" only when the "r+" failure was specifically "file does
     120                 :             :      * not exist" - "w+" truncates, so an "r+" failure from any other cause
     121                 :             :      * (media/I/O error) on an existing file must never reach it, or it would
     122                 :             :      * empty a record file the BlockStore believes is durably stored. The exact
     123                 :             :      * reason lives in the task errno ff_fopen just set; ENOENT is the only code
     124                 :             :      * for which creating is safe. Plus-FAT also answers ENOENT where it could not
     125                 :             :      * read the directory, which this cannot tell apart; the platform page says
     126                 :             :      * so. Plus-FAT has no single non-truncating open-or-create mode. */
     127                 :          32 :     self->Fp = ff_fopen(path, "r+");
     128                 :          32 :     int openErrno = PlusFatFile_ErrnoUnlessOpen(self);
     129         [ +  + ]:          32 :     if (PlusFatFile_IsAbsent(openErrno))
     130                 :             :     {
     131                 :           4 :         self->Fp = ff_fopen(path, "w+");
     132                 :           4 :         openErrno = PlusFatFile_ErrnoUnlessOpen(self);
     133                 :             :     }
     134         [ +  + ]:          32 :     if (!PlusFatFile_IsFileOpen(self))
     135                 :             :     {
     136                 :           4 :         PlusFatFile_ReportFailure(SOLIDSYSLOG_FILE_ERROR_OPEN_FAILED, openErrno);
     137                 :             :     }
     138                 :          32 :     return PlusFatFile_IsFileOpen(self);
     139                 :             : }
     140                 :             : 
     141                 :             : /* The task errno the ff_fopen that just ran left behind, or none when it
     142                 :             :  * opened the file. */
     143                 :          36 : static inline int PlusFatFile_ErrnoUnlessOpen(const struct SolidSyslogPlusFatFile* self)
     144                 :             : {
     145         [ +  + ]:          36 :     return PlusFatFile_IsFileOpen(self) ? 0 : stdioGET_ERRNO();
     146                 :             : }
     147                 :             : 
     148                 :             : /* True when a call answered that the path does not exist. Plus-FAT gives the
     149                 :             :  * same answer where reading the directory failed, so it is the best the errno
     150                 :             :  * can say rather than proof. */
     151                 :          35 : static inline bool PlusFatFile_IsAbsent(int nativeErrno)
     152                 :             : {
     153                 :          35 :     return nativeErrno == pdFREERTOS_ERRNO_ENOENT;
     154                 :             : }
     155                 :             : 
     156                 :           5 : static bool PlusFatFile_IsOpen(struct SolidSyslogFile* base)
     157                 :             : {
     158                 :           5 :     return PlusFatFile_IsFileOpen(PlusFatFile_SelfFromBase(base));
     159                 :             : }
     160                 :             : 
     161                 :           6 : static bool PlusFatFile_Read(struct SolidSyslogFile* base, void* buf, size_t count)
     162                 :             : {
     163                 :           6 :     struct SolidSyslogPlusFatFile* self = PlusFatFile_SelfFromBase(base);
     164                 :             :     /* xSize == 1 so the ff_fread return value is the byte count delivered. */
     165                 :           6 :     size_t delivered = ff_fread(buf, 1, count, self->Fp);
     166                 :             :     /* ff_fread records why it stopped short in the task errno: a failure
     167                 :             :      * leaves its code, the end of the file leaves none and the store judges
     168                 :             :      * that for itself. */
     169         [ +  + ]:           6 :     int readErrno = (delivered == count) ? 0 : stdioGET_ERRNO();
     170         [ +  + ]:           6 :     if (readErrno != 0)
     171                 :             :     {
     172                 :           1 :         PlusFatFile_ReportFailure(SOLIDSYSLOG_FILE_ERROR_READ_FAILED, readErrno);
     173                 :             :     }
     174                 :           6 :     return delivered == count;
     175                 :             : }
     176                 :             : 
     177                 :           8 : static bool PlusFatFile_Write(struct SolidSyslogFile* base, const void* buf, size_t count)
     178                 :             : {
     179                 :           8 :     struct SolidSyslogPlusFatFile* self = PlusFatFile_SelfFromBase(base);
     180                 :           8 :     size_t written = ff_fwrite(buf, 1, count, self->Fp);
     181                 :             :     /* A full volume is a short write with no errno; a failure leaves its code. */
     182         [ +  + ]:           8 :     int writeErrno = (written == count) ? 0 : stdioGET_ERRNO();
     183                 :           8 :     bool wroteAll = (written == count);
     184         [ +  + ]:           8 :     if (wroteAll)
     185                 :             :     {
     186                 :             :         /* Flush the IO-manager cache after every complete write so the record's
     187                 :             :          * data sectors reach the media. Plus-FAT has no per-file flush -
     188                 :             :          * ff_stdio.h declares ff_fflush but the library never defines it;
     189                 :             :          * FF_FlushCache against the file's IO manager is the real durability
     190                 :             :          * primitive. It does not commit the directory entry: FF_Close writes the
     191                 :             :          * recorded size, so a power loss before then leaves data the size
     192                 :             :          * excludes. */
     193                 :           5 :         wroteAll = PlusFatFile_Flush(self);
     194                 :             :     }
     195                 :             :     else
     196                 :             :     {
     197                 :           3 :         PlusFatFile_ReportFailure(SOLIDSYSLOG_FILE_ERROR_WRITE_FAILED, writeErrno);
     198                 :             :     }
     199                 :           8 :     return wroteAll;
     200                 :             : }
     201                 :             : 
     202                 :             : /* Flush the IO-manager cache to the media; returns whether it succeeded. The
     203                 :             :  * FF_Error_t it answers is the code behind a failure. */
     204                 :           5 : static inline bool PlusFatFile_Flush(struct SolidSyslogPlusFatFile* self)
     205                 :             : {
     206                 :           5 :     FF_Error_t result = FF_FlushCache(self->Fp->pxIOManager);
     207         [ +  + ]:           5 :     if (result != FF_ERR_NONE)
     208                 :             :     {
     209                 :           2 :         PlusFatFile_ReportFailure(SOLIDSYSLOG_FILE_ERROR_SYNC_FAILED, (int) result);
     210                 :             :     }
     211                 :           5 :     return result == FF_ERR_NONE;
     212                 :             : }
     213                 :             : 
     214                 :           3 : static void PlusFatFile_SeekTo(struct SolidSyslogFile* base, size_t offset)
     215                 :             : {
     216                 :           3 :     struct SolidSyslogPlusFatFile* self = PlusFatFile_SelfFromBase(base);
     217                 :           3 :     int status = ff_fseek(self->Fp, (long) offset, SEEK_SET);
     218         [ +  + ]:           3 :     int seekErrno = (status != 0) ? stdioGET_ERRNO() : 0;
     219         [ +  + ]:           3 :     if (status != 0)
     220                 :             :     {
     221                 :           1 :         PlusFatFile_ReportFailure(SOLIDSYSLOG_FILE_ERROR_SEEK_FAILED, seekErrno);
     222                 :             :     }
     223                 :           3 : }
     224                 :             : 
     225                 :           4 : static bool PlusFatFile_Size(struct SolidSyslogFile* base, size_t* size)
     226                 :             : {
     227                 :             :     /* ff_filelength answers 0 for a failure as well as for an empty file; the
     228                 :             :      * task errno it sets on every call is what tells them apart. */
     229                 :           4 :     size_t length = ff_filelength(PlusFatFile_SelfFromBase(base)->Fp);
     230                 :           4 :     int sizeErrno = stdioGET_ERRNO();
     231                 :           4 :     bool known = sizeErrno == pdFREERTOS_ERRNO_NONE;
     232         [ +  + ]:           4 :     if (!known)
     233                 :             :     {
     234                 :           2 :         PlusFatFile_ReportFailure(SOLIDSYSLOG_FILE_ERROR_SIZE_FAILED, sizeErrno);
     235                 :             :     }
     236         [ +  + ]:           4 :     *size = known ? length : 0U;
     237                 :           4 :     return known;
     238                 :             : }
     239                 :             : 
     240                 :             : /* Empty the file: rewind to the start, then set EOF there so the length
     241                 :             :  * becomes zero. ff_seteof truncates at the current position and Plus-FAT has
     242                 :             :  * no truncate-to-zero call of its own, so the seek is part of the truncate:
     243                 :             :  * one that fails leaves the file where it was, and is reported as the
     244                 :             :  * truncate failing. */
     245                 :           4 : static void PlusFatFile_Truncate(struct SolidSyslogFile* base)
     246                 :             : {
     247                 :           4 :     struct SolidSyslogPlusFatFile* self = PlusFatFile_SelfFromBase(base);
     248                 :           4 :     int status = ff_fseek(self->Fp, 0, SEEK_SET);
     249         [ +  + ]:           4 :     if (status == 0)
     250                 :             :     {
     251                 :           3 :         status = ff_seteof(self->Fp);
     252                 :             :     }
     253                 :             :     /* Nothing runs between whichever call failed and this read of its errno. */
     254         [ +  + ]:           4 :     int truncateErrno = (status != 0) ? stdioGET_ERRNO() : 0;
     255         [ +  + ]:           4 :     if (status != 0)
     256                 :             :     {
     257                 :           2 :         PlusFatFile_ReportFailure(SOLIDSYSLOG_FILE_ERROR_TRUNCATE_FAILED, truncateErrno);
     258                 :             :     }
     259                 :           4 : }
     260                 :             : 
     261                 :             : /* A path that is not there is the answer, not a fault, so nothing is reported. */
     262                 :           2 : static bool PlusFatFile_Exists(struct SolidSyslogFile* base, const char* path)
     263                 :             : {
     264                 :             :     (void) base;
     265                 :             :     FF_Stat_t status;
     266                 :           2 :     return ff_stat(path, &status) == 0;
     267                 :             : }
     268                 :             : 
     269                 :           4 : static bool PlusFatFile_Delete(struct SolidSyslogFile* base, const char* path)
     270                 :             : {
     271                 :             :     (void) base;
     272                 :           4 :     int status = ff_remove(path);
     273         [ +  + ]:           4 :     int removeErrno = (status != 0) ? stdioGET_ERRNO() : 0;
     274                 :           4 :     return PlusFatFile_IsRemovedOrAbsent(status, removeErrno);
     275                 :             : }
     276                 :             : 
     277                 :             : /* The contract counts an absent path as removed. Plus-FAT answers ENOTDIR
     278                 :             :  * where the path's directory is missing. */
     279                 :           4 : static inline bool PlusFatFile_IsRemovedOrAbsent(int status, int removeErrno)
     280                 :             : {
     281   [ +  +  +  +  :           4 :     return (status == 0) || PlusFatFile_IsAbsent(removeErrno) || (removeErrno == pdFREERTOS_ERRNO_ENOTDIR);
                   +  + ]
     282                 :             : }
        

Generated by: LCOV version 2.0-1