GCC Code Coverage Report


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

fx_unicode_file_rename.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_rename PORTABLE C */
43 /* 6.1 */
44 /* AUTHOR */
45 /* */
46 /* William E. Lamie, Microsoft Corporation */
47 /* */
48 /* DESCRIPTION */
49 /* */
50 /* This function first attempts to find the specified file. If found, */
51 /* the rename request is valid and the directory entry will be changed */
52 /* to the new file name. Otherwise, if the file is not found, the */
53 /* appropriate error code is returned to the caller. */
54 /* */
55 /* INPUT */
56 /* */
57 /* media_ptr Pointer to media */
58 /* old_unicode_name Pointer to old unicode name */
59 /* old_unicode_length Old unicode name length */
60 /* new_unicode_name Pointer to new unicode name */
61 /* new_unicode_length New unicode name length */
62 /* new_short_name Designated new short name */
63 /* */
64 /* OUTPUT */
65 /* */
66 /* Completion Status */
67 /* */
68 /* CALLS */
69 /* */
70 /* _fx_unicode_short_name_get Get short name of unicode name*/
71 /* _fx_directory_search Search directory */
72 /* _fx_unicode_directory_entry_change Change unicode file name */
73 /* _fx_unicode_directory_search Search for unicode name */
74 /* _fx_file_rename Rename for ASCII file name */
75 /* _fx_fault_tolerant_transaction_start Start fault tolerant */
76 /* transaction */
77 /* _fx_fault_tolerant_transaction_end End fault tolerant transaction*/
78 /* */
79 /* CALLED BY */
80 /* */
81 /* Application Code */
82 /* */
83 /**************************************************************************/
84 427 UINT _fx_unicode_file_rename(FX_MEDIA *media_ptr, UCHAR *old_unicode_name, ULONG old_unicode_length,
85 UCHAR *new_unicode_name, ULONG new_unicode_length, CHAR *new_short_name)
86 {
87 ULONG i;
88 UINT status;
89 CHAR alpha, beta;
90 CHAR old_shortname[13];
91 CHAR new_shortname[13];
92 FX_DIR_ENTRY dir_entry;
93
94
95 /* Clear the return short name. */
96 427 new_short_name[0] = FX_NULL;
97
98 /* Set shortname to null. */
99 427 old_shortname[0] = FX_NULL;
100 427 new_shortname[0] = FX_NULL;
101
102 /* Check the media to make sure it is open. */
103
2/2
✓ Branch 2 → 3 taken 27 times.
✓ Branch 2 → 4 taken 400 times.
427 if (media_ptr -> fx_media_id != FX_MEDIA_ID)
104 {
105
106 /* Return the media not opened error. */
107 27 return(FX_MEDIA_NOT_OPEN);
108 }
109
110
111 /* Get shortname of old unicode name. */
112 400 status = _fx_unicode_short_name_get(media_ptr, old_unicode_name, old_unicode_length, old_shortname);
113
114 /* Determine if the result was successful. */
115
2/2
✓ Branch 5 → 6 taken 35 times.
✓ Branch 5 → 7 taken 365 times.
400 if (status != FX_SUCCESS)
116 {
117
118 /* Return the error code. */
119 35 return(status);
120 }
121
122 /* Protect media. */
123 201 FX_PROTECT
124
125 /* Setup pointer to media name buffer. */
126 365 dir_entry.fx_dir_entry_name = media_ptr -> fx_media_name_buffer + FX_MAX_LONG_NAME_LEN;
127
128 /* Clear the short name string. */
129 365 dir_entry.fx_dir_entry_short_name[0] = FX_NULL;
130
131 /* Search the target directory for the same file name. */
132 365 status = _fx_unicode_directory_search(media_ptr, &dir_entry, (UCHAR *)new_shortname, sizeof(new_shortname),
133 new_unicode_name, &new_unicode_length, 0);
134
135 /* Determine if the name already exists. */
136
4/4
✓ Branch 8 → 9 taken 84 times.
✓ Branch 8 → 29 taken 80 times.
✓ Branch 9 → 10 taken 105 times.
✓ Branch 9 → 31 taken 96 times.
365 if (status == FX_SUCCESS)
137 {
138
139
4/4
✓ Branch 9 → 10 taken 72 times.
✓ Branch 9 → 26 taken 12 times.
✓ Branch 10 → 11 taken 90 times.
✓ Branch 10 → 27 taken 15 times.
189 if (old_unicode_length == new_unicode_length)
140 {
141
142 /* Determine if the new name simply has an ASCII case change. If so, simply let the processing
143 continue. */
144 162 i = 0;
145 do
146 {
147
148 /* Pickup an old name and new name character and convert to upper case if necessary. */
149 360 alpha = (CHAR)old_unicode_name[i << 1];
150
10/10
✓ Branch 11 → 12 taken 104 times.
✓ Branch 11 → 15 taken 56 times.
✓ Branch 12 → 13 taken 214 times.
✓ Branch 12 → 15 taken 20 times.
✓ Branch 12 → 16 taken 70 times.
✓ Branch 13 → 14 taken 177 times.
✓ Branch 13 → 15 taken 12 times.
✓ Branch 13 → 16 taken 25 times.
✓ Branch 14 → 15 taken 90 times.
✓ Branch 14 → 16 taken 15 times.
360 if ((alpha >= 'a') && (alpha <= 'z') && (old_unicode_name[(i << 1) + 1] == 0))
151 {
152
153 /* Lower case, convert to upper case! */
154 162 alpha = (CHAR)((INT)alpha - 0x20);
155 }
156 360 beta = (CHAR)new_unicode_name[i << 1];
157
10/10
✓ Branch 15 → 16 taken 104 times.
✓ Branch 15 → 19 taken 56 times.
✓ Branch 16 → 17 taken 214 times.
✓ Branch 16 → 19 taken 20 times.
✓ Branch 16 → 20 taken 70 times.
✓ Branch 17 → 18 taken 165 times.
✓ Branch 17 → 19 taken 24 times.
✓ Branch 17 → 20 taken 25 times.
✓ Branch 18 → 19 taken 75 times.
✓ Branch 18 → 20 taken 30 times.
360 if ((beta >= 'a') && (beta <= 'z') && (new_unicode_name[(i << 1) + 1] == 0))
158 {
159
160 /* Lower case, convert to upper case! */
161 135 beta = (CHAR)((INT)beta - 0x20);
162 }
163
164 /* Now compare the characters. */
165
4/4
✓ Branch 19 → 20 taken 24 times.
✓ Branch 19 → 21 taken 136 times.
✓ Branch 20 → 21 taken 30 times.
✓ Branch 20 → 22 taken 170 times.
360 if ((alpha != beta))
166 {
167
168 /* Get out of this loop! */
169 54 break;
170 }
171
172 /* Pickup the high bytes. */
173 306 alpha = (CHAR)old_unicode_name[(i << 1) + 1];
174 306 beta = (CHAR)new_unicode_name[(i << 1) + 1];
175
176 /* Now compare the byte. */
177
4/4
✓ Branch 21 → 22 taken 12 times.
✓ Branch 21 → 23 taken 124 times.
✓ Branch 22 → 23 taken 15 times.
✓ Branch 22 → 24 taken 155 times.
306 if ((alpha != beta))
178 {
179
180 /* Get out of this loop! */
181 27 break;
182 }
183
184 /* Move to next character. */
185 279 i++;
186
4/4
✓ Branch 23 → 24 taken 88 times.
✓ Branch 23 → 25 taken 36 times.
✓ Branch 24 → 25 taken 110 times.
✓ Branch 24 → 26 taken 45 times.
279 } while (i < old_unicode_length);
187 }
188 else
189 {
190
191 /* Names not match. */
192 27 alpha = 0;
193 27 beta = 1;
194 }
195
196 /* Now determine if the names match. */
197
4/4
✓ Branch 27 → 28 taken 48 times.
✓ Branch 27 → 29 taken 36 times.
✓ Branch 28 → 29 taken 60 times.
✓ Branch 28 → 31 taken 45 times.
189 if (alpha != beta)
198 {
199
200 /* No, the names do not match so simply return an error
201 to the caller. */
202
203 /* Release media protection. */
204 60 FX_UNPROTECT
205
206 /* Return the not a file error code. */
207 108 return(FX_ALREADY_CREATED);
208 }
209 }
210
211 #ifdef FX_ENABLE_FAULT_TOLERANT
212 /* Start transaction. */
213 132 _fx_fault_tolerant_transaction_start(media_ptr);
214 #endif /* FX_ENABLE_FAULT_TOLERANT */
215
216 /* Okay, at this point we need to rename to a new long file name that has enough space for the
217 eventual unicode file name. */
218
219 /* Copy the characters from the unicode file name and make sure they are
220 within the ASCII range. */
221 257 _fx_unicode_temp_long_file_name[0] = 'z';
222
8/8
✓ Branch 31 → 30 taken 228 times.
✓ Branch 31 → 32 taken 50 times.
✓ Branch 32 → 31 taken 316 times.
✓ Branch 32 → 33 taken 66 times.
✓ Branch 33 → 32 taken 342 times.
✓ Branch 33 → 34 taken 75 times.
✓ Branch 34 → 33 taken 316 times.
✓ Branch 34 → 35 taken 66 times.
1459 for (i = 1; i < new_unicode_length; i++)
223 {
224
225 /* Build temporary long file name. */
226 1202 _fx_unicode_temp_long_file_name[i] = (UCHAR)((UINT)'0' + (i % 9));
227 }
228 257 _fx_unicode_temp_long_file_name[i] = FX_NULL;
229
230 /* Loop to try different temp long file names... if necessary. */
231 do
232 {
233
234 /* Create a new file with the temp long file name. */
235 641 status = _fx_file_rename(media_ptr, old_shortname, (CHAR *)_fx_unicode_temp_long_file_name);
236
237 /* Determine if there was an error. */
238
8/8
✓ Branch 34 → 35 taken 82 times.
✓ Branch 34 → 37 taken 48 times.
✓ Branch 35 → 36 taken 94 times.
✓ Branch 35 → 38 taken 64 times.
✓ Branch 36 → 37 taken 123 times.
✓ Branch 36 → 39 taken 72 times.
✓ Branch 37 → 38 taken 94 times.
✓ Branch 37 → 40 taken 64 times.
641 if (status == FX_ALREADY_CREATED)
239 {
240
241 /* Adjust the name slightly and try again! */
242 393 _fx_unicode_temp_long_file_name[0]--;
243
244 /* Determine if it is outside the lower case boundary. */
245
8/8
✓ Branch 35 → 36 taken 2 times.
✓ Branch 35 → 37 taken 80 times.
✓ Branch 36 → 37 taken 2 times.
✓ Branch 36 → 38 taken 92 times.
✓ Branch 37 → 38 taken 3 times.
✓ Branch 37 → 39 taken 120 times.
✓ Branch 38 → 39 taken 2 times.
✓ Branch 38 → 40 taken 92 times.
393 if (_fx_unicode_temp_long_file_name[0] < 0x61)
246 {
247 9 break;
248 }
249 }
250
8/8
✓ Branch 37 → 38 taken 80 times.
✓ Branch 37 → 39 taken 48 times.
✓ Branch 38 → 39 taken 92 times.
✓ Branch 38 → 40 taken 64 times.
✓ Branch 39 → 40 taken 120 times.
✓ Branch 39 → 41 taken 72 times.
✓ Branch 40 → 41 taken 92 times.
✓ Branch 40 → 42 taken 64 times.
632 } while (status == FX_ALREADY_CREATED);
251
252 /* Determine if there was an error. */
253
8/8
✓ Branch 39 → 40 taken 2 times.
✓ Branch 39 → 41 taken 48 times.
✓ Branch 40 → 41 taken 2 times.
✓ Branch 40 → 43 taken 64 times.
✓ Branch 41 → 42 taken 3 times.
✓ Branch 41 → 44 taken 72 times.
✓ Branch 42 → 43 taken 2 times.
✓ Branch 42 → 46 taken 64 times.
257 if (status)
254 {
255 #ifdef FX_ENABLE_FAULT_TOLERANT
256 4 FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr);
257 #endif /* FX_ENABLE_FAULT_TOLERANT */
258
259 /* Release media protection. */
260 5 FX_UNPROTECT
261
262 /* Return error. */
263 9 return(status);
264 }
265
266 /* Setup pointer to media name buffer. */
267 248 dir_entry.fx_dir_entry_name = media_ptr -> fx_media_name_buffer + FX_MAX_LONG_NAME_LEN;
268
269 /* Clear the short name string. */
270 248 dir_entry.fx_dir_entry_short_name[0] = 0;
271
272 /* Search the system for the supplied file name. */
273 248 status = _fx_directory_search(media_ptr, (CHAR *)_fx_unicode_temp_long_file_name, &dir_entry, FX_NULL, FX_NULL);
274
275 /* Determine if the search was successful. */
276
8/8
✓ Branch 42 → 43 taken 2 times.
✓ Branch 42 → 44 taken 46 times.
✓ Branch 44 → 45 taken 4 times.
✓ Branch 44 → 47 taken 60 times.
✓ Branch 45 → 46 taken 3 times.
✓ Branch 45 → 48 taken 69 times.
✓ Branch 47 → 48 taken 4 times.
✓ Branch 47 → 51 taken 60 times.
248 if (status != FX_SUCCESS)
277 {
278 #ifdef FX_ENABLE_FAULT_TOLERANT
279 8 FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr);
280 #endif /* FX_ENABLE_FAULT_TOLERANT */
281
282 /* Release media protection. */
283 7 FX_UNPROTECT
284
285 /* Return the error status. */
286 13 return(status);
287 }
288
289 /* We can now change the temporary long file name with the destination unicode name. */
290 235 status = _fx_unicode_directory_entry_change(media_ptr, &dir_entry, new_unicode_name, new_unicode_length);
291
292 /* Was this successful? */
293
8/8
✓ Branch 45 → 46 taken 44 times.
✓ Branch 45 → 51 taken 2 times.
✓ Branch 48 → 49 taken 56 times.
✓ Branch 48 → 54 taken 4 times.
✓ Branch 49 → 50 taken 66 times.
✓ Branch 49 → 55 taken 3 times.
✓ Branch 52 → 53 taken 56 times.
✓ Branch 52 → 58 taken 4 times.
235 if (status == FX_SUCCESS)
294 {
295
296 /* Yes, copy the short file name to the destination. */
297 /* The new short name only have 8 characters, since we didn't include a dot in temp_long_file_name. */
298
8/8
✓ Branch 50 → 47 taken 218 times.
✓ Branch 50 → 51 taken 6 times.
✓ Branch 53 → 50 taken 314 times.
✓ Branch 53 → 54 taken 18 times.
✓ Branch 54 → 51 taken 327 times.
✓ Branch 54 → 55 taken 9 times.
✓ Branch 57 → 54 taken 314 times.
✓ Branch 57 → 58 taken 18 times.
1224 for (i = 0; i < FX_DIR_NAME_SIZE; i++)
299 {
300
301 /* Copy a character. */
302 1173 new_short_name[i] = dir_entry.fx_dir_entry_short_name[i];
303
304 /* Are we done? */
305
8/8
✓ Branch 47 → 48 taken 38 times.
✓ Branch 47 → 49 taken 180 times.
✓ Branch 50 → 51 taken 38 times.
✓ Branch 50 → 52 taken 276 times.
✓ Branch 51 → 52 taken 57 times.
✓ Branch 51 → 53 taken 270 times.
✓ Branch 54 → 55 taken 38 times.
✓ Branch 54 → 56 taken 276 times.
1173 if (new_short_name[i] == FX_NULL)
306 {
307 171 break;
308 }
309 }
310 }
311
312 #ifdef FX_ENABLE_FAULT_TOLERANT
313 /* Check for a bad status. */
314
4/4
✓ Branch 54 → 55 taken 4 times.
✓ Branch 54 → 57 taken 56 times.
✓ Branch 58 → 59 taken 4 times.
✓ Branch 58 → 62 taken 56 times.
120 if (status != FX_SUCCESS)
315 {
316
317 8 FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr);
318
319 /* Release media protection. */
320 4 FX_UNPROTECT
321
322 /* Return the bad status. */
323 8 return(status);
324 }
325
326 /* End transaction. */
327 112 status = _fx_fault_tolerant_transaction_end(media_ptr);
328 #endif /* FX_ENABLE_FAULT_TOLERANT */
329
330 /* Release the protection. */
331 113 FX_UNPROTECT
332
333 /* Return completion status. */
334 203 return(status);
335 }
336
337