fx_file_close.c
| 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 | 6862173 | 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 |
2/2✓ Branch 2 → 3 taken 22 times.
✓ Branch 2 → 4 taken 6862151 times.
|
6862173 | if (file_ptr -> fx_file_id != FX_FILE_ID) |
| 78 | { | ||
| 79 | |||
| 80 | /* Return the file not open error status. */ | ||
| 81 | 22 | return(FX_NOT_OPEN); | |
| 82 | } | ||
| 83 | |||
| 84 | /* Setup a pointer to the associated media. */ | ||
| 85 | 6862151 | 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 | 6862151 | 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 | 6835015 | 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 |
4/4✓ Branch 4 → 5 taken 26832 times.
✓ Branch 4 → 6 taken 304 times.
✓ Branch 5 → 6 taken 6834635 times.
✓ Branch 5 → 7 taken 380 times.
|
6862151 | if (file_ptr == file_ptr -> fx_file_opened_next) |
| 105 | { | ||
| 106 | |||
| 107 | /* Only opened file, just set the opened list to NULL. */ | ||
| 108 | 6861467 | 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 | 684 | (file_ptr -> fx_file_opened_next) -> fx_file_opened_previous = | |
| 115 | 684 | file_ptr -> fx_file_opened_previous; | |
| 116 | 684 | (file_ptr -> fx_file_opened_previous) -> fx_file_opened_next = | |
| 117 | 684 | file_ptr -> fx_file_opened_next; | |
| 118 | |||
| 119 | /* See if we have to update the opened list head pointer. */ | ||
| 120 |
4/4✓ Branch 6 → 7 taken 268 times.
✓ Branch 6 → 8 taken 36 times.
✓ Branch 7 → 8 taken 335 times.
✓ Branch 7 → 9 taken 45 times.
|
684 | if (media_ptr -> fx_media_opened_file_list == file_ptr) |
| 121 | { | ||
| 122 | |||
| 123 | /* Yes, move the head pointer to the next opened file. */ | ||
| 124 | 603 | media_ptr -> fx_media_opened_file_list = file_ptr -> fx_file_opened_next; | |
| 125 | } | ||
| 126 | } | ||
| 127 | |||
| 128 | /* Decrement the opened file counter. */ | ||
| 129 | 6862151 | media_ptr -> fx_media_opened_file_count--; | |
| 130 | |||
| 131 | /* Finally, Indicate that this file is closed. */ | ||
| 132 | 6862151 | 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 |
4/4✓ Branch 8 → 9 taken 27062 times.
✓ Branch 8 → 13 taken 74 times.
✓ Branch 9 → 10 taken 3433690 times.
✓ Branch 9 → 17 taken 3401325 times.
|
6862151 | if ((file_ptr -> fx_file_open_mode == FX_OPEN_FOR_WRITE) && |
| 137 |
4/4✓ Branch 9 → 10 taken 25772 times.
✓ Branch 9 → 13 taken 1290 times.
✓ Branch 10 → 11 taken 3432356 times.
✓ Branch 10 → 17 taken 1334 times.
|
3460752 | (file_ptr -> fx_file_modified)) |
| 138 | { | ||
| 139 | |||
| 140 | /* Lockout interrupts for time/date access. */ | ||
| 141 | 3432356 | FX_DISABLE_INTS | |
| 142 | |||
| 143 | /* Set the new time and date. */ | ||
| 144 | 3458128 | file_ptr -> fx_file_dir_entry.fx_dir_entry_time = _fx_system_time; | |
| 145 | 3458128 | file_ptr -> fx_file_dir_entry.fx_dir_entry_date = _fx_system_date; | |
| 146 | |||
| 147 | /* Set the last access date. */ | ||
| 148 | 3458128 | file_ptr -> fx_file_dir_entry.fx_dir_entry_last_accessed_date = _fx_system_date; | |
| 149 | |||
| 150 | /* Restore interrupts. */ | ||
| 151 | 3432356 | FX_RESTORE_INTS | |
| 152 | |||
| 153 | /* Copy the new file size into the directory entry. */ | ||
| 154 | 3458128 | file_ptr -> fx_file_dir_entry.fx_dir_entry_file_size = | |
| 155 | 3458128 | file_ptr -> fx_file_current_file_size; | |
| 156 | |||
| 157 | /* Write the directory entry to the media. */ | ||
| 158 | 3458128 | status = _fx_directory_entry_write(media_ptr, &(file_ptr -> fx_file_dir_entry)); | |
| 159 | |||
| 160 | /* Check for a good status. */ | ||
| 161 |
4/4✓ Branch 11 → 12 taken 28 times.
✓ Branch 11 → 13 taken 25744 times.
✓ Branch 14 → 15 taken 35 times.
✓ Branch 14 → 17 taken 3432319 times.
|
3458126 | if (status != FX_SUCCESS) |
| 162 | { | ||
| 163 | |||
| 164 | /* Release media protection. */ | ||
| 165 | 35 | FX_UNPROTECT | |
| 166 | |||
| 167 | /* Error writing the directory. */ | ||
| 168 | 63 | return(status); | |
| 169 | } | ||
| 170 | } | ||
| 171 | |||
| 172 | /* Release media protection. */ | ||
| 173 | 6834978 | FX_UNPROTECT | |
| 174 | |||
| 175 | /* Return status to the caller. */ | ||
| 176 | 6862086 | return(FX_SUCCESS); | |
| 177 | } | ||
| 178 | |||
| 179 |