GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: fx_file_close.c Lines: 31 31 100.0 %
Date: 2024-01-10 21:53:23 Branches: 12 12 100.0 %

Line Branch Exec Source
1
/**************************************************************************/
2
/*                                                                        */
3
/*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4
/*                                                                        */
5
/*       This software is licensed under the Microsoft Software License   */
6
/*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7
/*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8
/*       and in the root directory of this software.                      */
9
/*                                                                        */
10
/**************************************************************************/
11
12
13
/**************************************************************************/
14
/**************************************************************************/
15
/**                                                                       */
16
/** FileX Component                                                       */
17
/**                                                                       */
18
/**   File                                                                */
19
/**                                                                       */
20
/**************************************************************************/
21
/**************************************************************************/
22
23
#define FX_SOURCE_CODE
24
25
26
/* Include necessary system files.  */
27
28
#include "fx_api.h"
29
#include "fx_system.h"
30
#include "fx_file.h"
31
#include "fx_utility.h"
32
#include "fx_directory.h"
33
34
35
/**************************************************************************/
36
/*                                                                        */
37
/*  FUNCTION                                               RELEASE        */
38
/*                                                                        */
39
/*    _fx_file_close                                      PORTABLE C      */
40
/*                                                           6.1          */
41
/*  AUTHOR                                                                */
42
/*                                                                        */
43
/*    William E. Lamie, Microsoft Corporation                             */
44
/*                                                                        */
45
/*  DESCRIPTION                                                           */
46
/*                                                                        */
47
/*    This function closes the specified file.  If the file was written   */
48
/*    to this function will also write the directory entry (with the new  */
49
/*    size and time/date stamp) out to disk.                              */
50
/*                                                                        */
51
/*  INPUT                                                                 */
52
/*                                                                        */
53
/*    file_ptr                              File control block pointer    */
54
/*                                                                        */
55
/*  OUTPUT                                                                */
56
/*                                                                        */
57
/*    return status                                                       */
58
/*                                                                        */
59
/*  CALLS                                                                 */
60
/*                                                                        */
61
/*    _fx_directory_entry_write             Write the directory entry     */
62
/*                                                                        */
63
/*  CALLED BY                                                             */
64
/*                                                                        */
65
/*    Application Code                                                    */
66
/*                                                                        */
67
/*  RELEASE HISTORY                                                       */
68
/*                                                                        */
69
/*    DATE              NAME                      DESCRIPTION             */
70
/*                                                                        */
71
/*  05-19-2020     William E. Lamie         Initial Version 6.0           */
72
/*  09-30-2020     William E. Lamie         Modified comment(s),          */
73
/*                                            resulting in version 6.1    */
74
/*                                                                        */
75
/**************************************************************************/
76
6260
UINT  _fx_file_close(FX_FILE *file_ptr)
77
{
78
79
UINT      status;
80
FX_MEDIA *media_ptr;
81
FX_INT_SAVE_AREA
82
83
84
    /* First, determine if the file is still open.  */
85
6260
    if (file_ptr -> fx_file_id != FX_FILE_ID)
86
    {
87
88
        /* Return the file not open error status.  */
89
2
        return(FX_NOT_OPEN);
90
    }
91
92
    /* Setup a pointer to the associated media.  */
93
6258
    media_ptr =  file_ptr -> fx_file_media_ptr;
94
95
#ifndef FX_MEDIA_STATISTICS_DISABLE
96
97
    /* Increment the number of times this service has been called.  */
98
6258
    media_ptr -> fx_media_file_closes++;
99
#endif
100
101
    /* If trace is enabled, insert this event into the trace buffer.  */
102
    FX_TRACE_IN_LINE_INSERT(FX_TRACE_FILE_CLOSE, file_ptr, file_ptr -> fx_file_current_file_size, 0, 0, FX_TRACE_FILE_EVENTS, 0, 0)
103
104
    /* Protect against other threads accessing the media.  */
105
6258
    FX_PROTECT
106
    /* If trace is enabled, unregister this object.  */
107
    FX_TRACE_OBJECT_UNREGISTER(file_ptr)
108
109
    /* Remove this file from the opened list for the media.  */
110
111
    /* See if the file is the only one on the open list for this media.  */
112
6258
    if (file_ptr == file_ptr -> fx_file_opened_next)
113
    {
114
115
        /* Only opened file, just set the opened list to NULL.  */
116
6182
        media_ptr -> fx_media_opened_file_list =  FX_NULL;
117
    }
118
    else
119
    {
120
121
        /* Otherwise, not the only opened file, link-up the neighbors.  */
122
76
        (file_ptr -> fx_file_opened_next) -> fx_file_opened_previous =
123
76
            file_ptr -> fx_file_opened_previous;
124
76
        (file_ptr -> fx_file_opened_previous) -> fx_file_opened_next =
125
76
            file_ptr -> fx_file_opened_next;
126
127
        /* See if we have to update the opened list head pointer.  */
128
76
        if (media_ptr -> fx_media_opened_file_list == file_ptr)
129
        {
130
131
            /* Yes, move the head pointer to the next opened file. */
132
67
            media_ptr -> fx_media_opened_file_list =  file_ptr -> fx_file_opened_next;
133
        }
134
    }
135
136
    /* Decrement the opened file counter.  */
137
6258
    media_ptr -> fx_media_opened_file_count--;
138
139
    /* Finally, Indicate that this file is closed.  */
140
6258
    file_ptr -> fx_file_id =  FX_FILE_CLOSED_ID;
141
142
    /* Check to see if this file needs to have its directory entry written
143
       back to the media.  */
144
6258
    if ((file_ptr -> fx_file_open_mode == FX_OPEN_FOR_WRITE) &&
145
6190
        (file_ptr -> fx_file_modified))
146
    {
147
148
        /* Lockout interrupts for time/date access.  */
149
6146
        FX_DISABLE_INTS
150
151
        /* Set the new time and date.  */
152
6146
        file_ptr -> fx_file_dir_entry.fx_dir_entry_time =  _fx_system_time;
153
6146
        file_ptr -> fx_file_dir_entry.fx_dir_entry_date =  _fx_system_date;
154
155
        /* Set the last access date.  */
156
6146
        file_ptr -> fx_file_dir_entry.fx_dir_entry_last_accessed_date =  _fx_system_date;
157
158
        /* Restore interrupts.  */
159
6146
        FX_RESTORE_INTS
160
161
        /* Copy the new file size into the directory entry.  */
162
6146
        file_ptr -> fx_file_dir_entry.fx_dir_entry_file_size =
163
6146
            file_ptr -> fx_file_current_file_size;
164
165
        /* Write the directory entry to the media.  */
166
#ifdef FX_ENABLE_EXFAT
167
        if (media_ptr -> fx_media_FAT_type == FX_exFAT)
168
        {
169
            status = _fx_directory_exFAT_entry_write(
170
                    media_ptr, &(file_ptr -> fx_file_dir_entry), UPDATE_STREAM);
171
        }
172
        else
173
        {
174
#endif /* FX_ENABLE_EXFAT */
175
6146
            status = _fx_directory_entry_write(media_ptr, &(file_ptr -> fx_file_dir_entry));
176
#ifdef FX_ENABLE_EXFAT
177
        }
178
#endif /* FX_ENABLE_EXFAT */
179
180
        /* Check for a good status.  */
181
6146
        if (status != FX_SUCCESS)
182
        {
183
184
            /* Release media protection.  */
185
7
            FX_UNPROTECT
186
187
            /* Error writing the directory.  */
188
7
            return(status);
189
        }
190
    }
191
192
    /* Release media protection.  */
193
6251
    FX_UNPROTECT
194
195
    /* Return status to the caller.  */
196
6251
    return(FX_SUCCESS);
197
}
198