GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: fx_file_close.c Lines: 31 31 100.0 %
Date: 2026-03-06 18:49:02 Branches: 12 12 100.0 %

Line Branch Exec Source
1
/***************************************************************************
2
 * Copyright (c) 2024 Microsoft Corporation
3
 * Copyright (c) 2026-present Eclipse ThreadX contributors
4
 *
5
 * This program and the accompanying materials are made available under the
6
 * terms of the MIT License which is available at
7
 * https://opensource.org/licenses/MIT.
8
 *
9
 * SPDX-License-Identifier: MIT
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
/**************************************************************************/
68
6260
UINT  _fx_file_close(FX_FILE *file_ptr)
69
{
70
71
UINT      status;
72
FX_MEDIA *media_ptr;
73
FX_INT_SAVE_AREA
74
75
76
    /* First, determine if the file is still open.  */
77
6260
    if (file_ptr -> fx_file_id != FX_FILE_ID)
78
    {
79
80
        /* Return the file not open error status.  */
81
2
        return(FX_NOT_OPEN);
82
    }
83
84
    /* Setup a pointer to the associated media.  */
85
6258
    media_ptr =  file_ptr -> fx_file_media_ptr;
86
87
#ifndef FX_MEDIA_STATISTICS_DISABLE
88
89
    /* Increment the number of times this service has been called.  */
90
6258
    media_ptr -> fx_media_file_closes++;
91
#endif
92
93
    /* If trace is enabled, insert this event into the trace buffer.  */
94
    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)
95
96
    /* Protect against other threads accessing the media.  */
97
6258
    FX_PROTECT
98
    /* If trace is enabled, unregister this object.  */
99
    FX_TRACE_OBJECT_UNREGISTER(file_ptr)
100
101
    /* Remove this file from the opened list for the media.  */
102
103
    /* See if the file is the only one on the open list for this media.  */
104
6258
    if (file_ptr == file_ptr -> fx_file_opened_next)
105
    {
106
107
        /* Only opened file, just set the opened list to NULL.  */
108
6182
        media_ptr -> fx_media_opened_file_list =  FX_NULL;
109
    }
110
    else
111
    {
112
113
        /* Otherwise, not the only opened file, link-up the neighbors.  */
114
76
        (file_ptr -> fx_file_opened_next) -> fx_file_opened_previous =
115
76
            file_ptr -> fx_file_opened_previous;
116
76
        (file_ptr -> fx_file_opened_previous) -> fx_file_opened_next =
117
76
            file_ptr -> fx_file_opened_next;
118
119
        /* See if we have to update the opened list head pointer.  */
120
76
        if (media_ptr -> fx_media_opened_file_list == file_ptr)
121
        {
122
123
            /* Yes, move the head pointer to the next opened file. */
124
67
            media_ptr -> fx_media_opened_file_list =  file_ptr -> fx_file_opened_next;
125
        }
126
    }
127
128
    /* Decrement the opened file counter.  */
129
6258
    media_ptr -> fx_media_opened_file_count--;
130
131
    /* Finally, Indicate that this file is closed.  */
132
6258
    file_ptr -> fx_file_id =  FX_FILE_CLOSED_ID;
133
134
    /* Check to see if this file needs to have its directory entry written
135
       back to the media.  */
136
6258
    if ((file_ptr -> fx_file_open_mode == FX_OPEN_FOR_WRITE) &&
137
6190
        (file_ptr -> fx_file_modified))
138
    {
139
140
        /* Lockout interrupts for time/date access.  */
141
6146
        FX_DISABLE_INTS
142
143
        /* Set the new time and date.  */
144
6146
        file_ptr -> fx_file_dir_entry.fx_dir_entry_time =  _fx_system_time;
145
6146
        file_ptr -> fx_file_dir_entry.fx_dir_entry_date =  _fx_system_date;
146
147
        /* Set the last access date.  */
148
6146
        file_ptr -> fx_file_dir_entry.fx_dir_entry_last_accessed_date =  _fx_system_date;
149
150
        /* Restore interrupts.  */
151
6146
        FX_RESTORE_INTS
152
153
        /* Copy the new file size into the directory entry.  */
154
6146
        file_ptr -> fx_file_dir_entry.fx_dir_entry_file_size =
155
6146
            file_ptr -> fx_file_current_file_size;
156
157
        /* Write the directory entry to the media.  */
158
6146
        status = _fx_directory_entry_write(media_ptr, &(file_ptr -> fx_file_dir_entry));
159
160
        /* Check for a good status.  */
161
6146
        if (status != FX_SUCCESS)
162
        {
163
164
            /* Release media protection.  */
165
7
            FX_UNPROTECT
166
167
            /* Error writing the directory.  */
168
7
            return(status);
169
        }
170
    }
171
172
    /* Release media protection.  */
173
6251
    FX_UNPROTECT
174
175
    /* Return status to the caller.  */
176
6251
    return(FX_SUCCESS);
177
}
178