GCC Code Coverage Report


Directory: common/src/
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 100.0% 53 / 0 / 53
Functions: 100.0% 1 / 0 / 1
Branches: 100.0% 72 / 0 / 72

fx_unicode_file_create.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 /** Unicode */
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_unicode.h"
30 #include "fx_directory.h"
31 #include "fx_file.h"
32 #include "fx_utility.h"
33 #ifdef FX_ENABLE_FAULT_TOLERANT
34 #include "fx_fault_tolerant.h"
35 #endif /* FX_ENABLE_FAULT_TOLERANT */
36
37
38 /**************************************************************************/
39 /* */
40 /* FUNCTION RELEASE */
41 /* */
42 /* _fx_unicode_file_create PORTABLE C */
43 /* 6.1 */
44 /* AUTHOR */
45 /* */
46 /* William E. Lamie, Microsoft Corporation */
47 /* */
48 /* DESCRIPTION */
49 /* */
50 /* This function creates the specified unicode name. */
51 /* */
52 /* INPUT */
53 /* */
54 /* media_ptr Pointer to media */
55 /* source_unicode_name Pointer to source unicode name*/
56 /* source_unicode_length Unicode name length */
57 /* short_name Designated short name */
58 /* */
59 /* OUTPUT */
60 /* */
61 /* Completion Status */
62 /* */
63 /* CALLS */
64 /* */
65 /* _fx_directory_search Search directory */
66 /* _fx_unicode_directory_entry_change Change unicode file name */
67 /* _fx_unicode_directory_search Search for unicode name */
68 /* _fx_file_create Create a file */
69 /* _fx_fault_tolerant_transaction_start Start fault tolerant */
70 /* transaction */
71 /* _fx_fault_tolerant_transaction_end End fault tolerant transaction*/
72 /* _fx_fault_tolerant_recover Recover FAT chain */
73 /* _fx_fault_tolerant_reset_log_file Reset the log file */
74 /* */
75 /* CALLED BY */
76 /* */
77 /* Application Code */
78 /* */
79 /**************************************************************************/
80 1198 UINT _fx_unicode_file_create(FX_MEDIA *media_ptr, UCHAR *source_unicode_name, ULONG source_unicode_length, CHAR *short_name)
81 {
82
83 FX_DIR_ENTRY dir_entry;
84 UINT i, status;
85 ULONG temp_length;
86 UCHAR destination_shortname[13];
87
88
89 /* Setup pointer to media name buffer. */
90 1198 dir_entry.fx_dir_entry_name = media_ptr -> fx_media_name_buffer + FX_MAX_LONG_NAME_LEN;
91
92 /* Clear the short name string. */
93 1198 dir_entry.fx_dir_entry_short_name[0] = FX_NULL;
94
95 /* Set destination shortname to null. */
96 1198 destination_shortname[0] = FX_NULL;
97
98 /* Clear the return short name. */
99 1198 short_name[0] = FX_NULL;
100
101 /* Check the media to make sure it is open. */
102
2/2
✓ Branch 2 → 3 taken 9 times.
✓ Branch 2 → 4 taken 1189 times.
1198 if (media_ptr -> fx_media_id != FX_MEDIA_ID)
103 {
104
105 /* Return the media not opened error. */
106 9 return(FX_MEDIA_NOT_OPEN);
107 }
108
109
110 /* If trace is enabled, insert this event into the trace buffer. */
111 FX_TRACE_IN_LINE_INSERT(FX_TRACE_UNICODE_FILE_CREATE, media_ptr, source_unicode_name, source_unicode_length, short_name, FX_TRACE_FILE_EVENTS, 0, 0)
112
113 /* Protect media. */
114 646 FX_PROTECT
115
116 #ifdef FX_ENABLE_FAULT_TOLERANT
117 /* Start transaction. */
118 674 _fx_fault_tolerant_transaction_start(media_ptr);
119 #endif /* FX_ENABLE_FAULT_TOLERANT */
120
121 /* Check for write protect at the media level (set by driver). */
122
6/6
✓ Branch 4 → 5 taken 2 times.
✓ Branch 4 → 6 taken 204 times.
✓ Branch 5 → 6 taken 5 times.
✓ Branch 5 → 8 taken 641 times.
✓ Branch 6 → 7 taken 2 times.
✓ Branch 6 → 10 taken 335 times.
1189 if (media_ptr -> fx_media_driver_write_protect)
123 {
124 #ifdef FX_ENABLE_FAULT_TOLERANT
125 4 FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr);
126 #endif /* FX_ENABLE_FAULT_TOLERANT */
127
128 /* Release media protection. */
129 5 FX_UNPROTECT
130
131 /* Return write protect error. */
132 9 return(FX_WRITE_PROTECT);
133 }
134
135 /* Setup temporary length. */
136 1180 temp_length = source_unicode_length;
137
138 /* Determine if the destination file is already present. */
139 1180 status = _fx_unicode_directory_search(media_ptr, &dir_entry, destination_shortname, sizeof(destination_shortname), source_unicode_name, &temp_length, 0);
140
141 /* Determine if the search was successful. */
142
6/6
✓ Branch 7 → 8 taken 30 times.
✓ Branch 7 → 9 taken 174 times.
✓ Branch 9 → 10 taken 87 times.
✓ Branch 9 → 12 taken 554 times.
✓ Branch 11 → 12 taken 42 times.
✓ Branch 11 → 15 taken 293 times.
1180 if (status == FX_SUCCESS)
143 {
144 #ifdef FX_ENABLE_FAULT_TOLERANT
145 84 FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr);
146 #endif /* FX_ENABLE_FAULT_TOLERANT */
147
148 /* Release media protection. */
149 87 FX_UNPROTECT
150
151 /* Return the error code. */
152 159 return(FX_ALREADY_CREATED);
153 }
154
155 /* Okay, at this point we need to create a new long file name that has enough space for the
156 eventual unicode file name. */
157
158 /* Copy the characters from the unicode file name and make sure they are
159 within the ASCII range. */
160 1021 _fx_unicode_temp_long_file_name[0] = 'z';
161
6/6
✓ Branch 11 → 10 taken 682 times.
✓ Branch 11 → 12 taken 174 times.
✓ Branch 14 → 13 taken 2530 times.
✓ Branch 14 → 15 taken 554 times.
✓ Branch 17 → 16 taken 1505 times.
✓ Branch 17 → 18 taken 293 times.
5738 for (i = 1; i < source_unicode_length; i++)
162 {
163
164 /* Build temporary long file name. */
165 4717 _fx_unicode_temp_long_file_name[i] = (UCHAR)((UINT)'0' + (i % 9));
166 }
167 1021 _fx_unicode_temp_long_file_name[i] = FX_NULL;
168
169 /* Loop to try different temp long file names... if necessary. */
170 do
171 {
172
173 /* Create a new file with the temp long file name. */
174 5069 status = _fx_file_create(media_ptr, (CHAR *)_fx_unicode_temp_long_file_name);
175
176 /* Determine if there was an error. */
177
6/6
✓ Branch 14 → 15 taken 865 times.
✓ Branch 14 → 17 taken 168 times.
✓ Branch 17 → 18 taken 2253 times.
✓ Branch 17 → 20 taken 539 times.
✓ Branch 20 → 21 taken 957 times.
✓ Branch 20 → 23 taken 287 times.
5069 if (status == FX_ALREADY_CREATED)
178 {
179
180 /* Adjust the name slightly and try again! */
181 4075 _fx_unicode_temp_long_file_name[0]--;
182
183 /* Determine if it is outside the lower case boundary. */
184
6/6
✓ Branch 15 → 16 taken 6 times.
✓ Branch 15 → 17 taken 859 times.
✓ Branch 18 → 19 taken 15 times.
✓ Branch 18 → 20 taken 2238 times.
✓ Branch 21 → 22 taken 6 times.
✓ Branch 21 → 23 taken 951 times.
4075 if (_fx_unicode_temp_long_file_name[0] < 0x61)
185 {
186 27 break;
187 }
188 }
189
6/6
✓ Branch 17 → 18 taken 859 times.
✓ Branch 17 → 19 taken 168 times.
✓ Branch 20 → 21 taken 2238 times.
✓ Branch 20 → 22 taken 539 times.
✓ Branch 23 → 24 taken 951 times.
✓ Branch 23 → 25 taken 287 times.
5042 } while (status == FX_ALREADY_CREATED);
190
191 /* Determine if there was an error. */
192
6/6
✓ Branch 19 → 20 taken 12 times.
✓ Branch 19 → 21 taken 162 times.
✓ Branch 22 → 23 taken 28 times.
✓ Branch 22 → 25 taken 526 times.
✓ Branch 25 → 26 taken 10 times.
✓ Branch 25 → 29 taken 283 times.
1021 if (status)
193 {
194 #ifdef FX_ENABLE_FAULT_TOLERANT
195 20 FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr);
196 #endif /* FX_ENABLE_FAULT_TOLERANT */
197
198 /* Release media protection. */
199 28 FX_UNPROTECT
200
201 /* Return error. */
202 50 return(status);
203 }
204
205 /* Setup pointer to media name buffer. */
206 971 dir_entry.fx_dir_entry_name = media_ptr -> fx_media_name_buffer + FX_MAX_LONG_NAME_LEN;
207
208 /* Clear the short name string. */
209 971 dir_entry.fx_dir_entry_short_name[0] = 0;
210
211 /* Search the system for the supplied file name. */
212 971 status = _fx_directory_search(media_ptr, (CHAR *)_fx_unicode_temp_long_file_name, &dir_entry, FX_NULL, FX_NULL);
213
214 /* Determine if the search was successful. */
215
6/6
✓ Branch 22 → 23 taken 1 time.
✓ Branch 22 → 24 taken 161 times.
✓ Branch 26 → 27 taken 5 times.
✓ Branch 26 → 29 taken 521 times.
✓ Branch 30 → 31 taken 3 times.
✓ Branch 30 → 34 taken 280 times.
971 if (status != FX_SUCCESS)
216 {
217 #ifdef FX_ENABLE_FAULT_TOLERANT
218 6 FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr);
219 #endif /* FX_ENABLE_FAULT_TOLERANT */
220
221 /* Release media protection. */
222 5 FX_UNPROTECT
223
224 /* Return the error status. */
225 9 return(status);
226 }
227
228 /* We can now change the temporary long file name with the destination unicode name. */
229 962 status = _fx_unicode_directory_entry_change(media_ptr, &dir_entry, source_unicode_name, source_unicode_length);
230
231 /* Was this successful? */
232
6/6
✓ Branch 25 → 26 taken 160 times.
✓ Branch 25 → 31 taken 1 time.
✓ Branch 30 → 31 taken 515 times.
✓ Branch 30 → 36 taken 6 times.
✓ Branch 35 → 36 taken 276 times.
✓ Branch 35 → 41 taken 4 times.
962 if (status == FX_SUCCESS)
233 {
234
235 /* Yes, copy the short file name to the destination. */
236 /* The new short name only have 8 characters, since we didn't include a dot in temp_long_file_name. */
237
6/6
✓ Branch 30 → 27 taken 802 times.
✓ Branch 30 → 31 taken 22 times.
✓ Branch 35 → 32 taken 2891 times.
✓ Branch 35 → 36 taken 140 times.
✓ Branch 40 → 37 taken 1696 times.
✓ Branch 40 → 41 taken 108 times.
5659 for (i = 0; i < FX_DIR_NAME_SIZE; i++)
238 {
239
240 /* Copy a character. */
241 5389 short_name[i] = dir_entry.fx_dir_entry_short_name[i];
242
243 /* Are we done? */
244
6/6
✓ Branch 27 → 28 taken 138 times.
✓ Branch 27 → 29 taken 664 times.
✓ Branch 32 → 33 taken 375 times.
✓ Branch 32 → 34 taken 2516 times.
✓ Branch 37 → 38 taken 168 times.
✓ Branch 37 → 39 taken 1528 times.
5389 if (short_name[i] == FX_NULL)
245 {
246 681 break;
247 }
248 }
249 }
250
251 #ifdef FX_ENABLE_FAULT_TOLERANT
252 /* Check for a bad status. */
253
4/4
✓ Branch 36 → 37 taken 4 times.
✓ Branch 36 → 39 taken 276 times.
✓ Branch 41 → 42 taken 4 times.
✓ Branch 41 → 45 taken 276 times.
560 if (status != FX_SUCCESS)
254 {
255
256 8 FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr);
257
258 /* Release media protection. */
259 4 FX_UNPROTECT
260
261 /* Return the bad status. */
262 8 return(status);
263 }
264
265 /* End transaction. */
266 552 status = _fx_fault_tolerant_transaction_end(media_ptr);
267 #endif /* FX_ENABLE_FAULT_TOLERANT */
268
269 /* Release the protection. */
270 505 FX_UNPROTECT
271
272 /* Return completion status. */
273 930 return(status);
274 }
275
276