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