GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: fx_unicode_file_rename.c Lines: 62 62 100.0 %
Date: 2026-03-06 18:49:02 Branches: 46 46 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
/**   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
44
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
44
    new_short_name[0] =  FX_NULL;
97
98
    /* Set shortname to null.  */
99
44
    old_shortname[0] =  FX_NULL;
100
44
    new_shortname[0] =  FX_NULL;
101
102
    /* Check the media to make sure it is open.  */
103
44
    if (media_ptr -> fx_media_id != FX_MEDIA_ID)
104
    {
105
106
        /* Return the media not opened error.  */
107
3
        return(FX_MEDIA_NOT_OPEN);
108
    }
109
110
111
    /* Get shortname of old unicode name. */
112
41
    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
41
    if (status != FX_SUCCESS)
116
    {
117
118
        /* Return the error code.  */
119
4
        return(status);
120
    }
121
122
    /* Protect media.  */
123
37
    FX_PROTECT
124
125
    /* Setup pointer to media name buffer.  */
126
37
    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
37
    dir_entry.fx_dir_entry_short_name[0] =  FX_NULL;
130
131
    /* Search the target directory for the same file name.  */
132
37
    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
37
    if (status == FX_SUCCESS)
137
    {
138
139
21
        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
18
            i =  0;
145
            do
146
            {
147
148
                /* Pickup an old name and new name character and convert to upper case if necessary.  */
149
40
                alpha = (CHAR)old_unicode_name[i << 1];
150

40
                if ((alpha >= 'a') && (alpha <= 'z') && (old_unicode_name[(i << 1) + 1] == 0))
151
                {
152
153
                    /* Lower case, convert to upper case!  */
154
18
                    alpha =  (CHAR)((INT)alpha - 0x20);
155
                }
156
40
                beta =   (CHAR)new_unicode_name[i << 1];
157

40
                if ((beta >= 'a') && (beta <= 'z') && (new_unicode_name[(i << 1) + 1] == 0))
158
                {
159
160
                    /* Lower case, convert to upper case!  */
161
15
                    beta =  (CHAR)((INT)beta - 0x20);
162
                }
163
164
                /* Now compare the characters.  */
165
40
                if ((alpha != beta))
166
                {
167
168
                    /* Get out of this loop!  */
169
6
                    break;
170
                }
171
172
                /* Pickup the high bytes. */
173
34
                alpha = (CHAR)old_unicode_name[(i << 1) + 1];
174
34
                beta =  (CHAR)new_unicode_name[(i << 1) + 1];
175
176
                /* Now compare the byte.  */
177
34
                if ((alpha != beta))
178
                {
179
180
                    /* Get out of this loop!  */
181
3
                    break;
182
                }
183
184
                /* Move to next character.  */
185
31
                i++;
186
31
            } while (i < old_unicode_length);
187
        }
188
        else
189
        {
190
191
            /* Names not match. */
192
3
            alpha = 0;
193
3
            beta = 1;
194
        }
195
196
        /* Now determine if the names match.  */
197
21
        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
12
            FX_UNPROTECT
205
206
            /* Return the not a file error code.  */
207
12
            return(FX_ALREADY_CREATED);
208
        }
209
    }
210
211
#ifdef FX_ENABLE_FAULT_TOLERANT
212
    /* Start transaction. */
213
    _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
25
    _fx_unicode_temp_long_file_name[0] =  'z';
222
139
    for (i = 1; i < new_unicode_length; i++)
223
    {
224
225
        /* Build temporary long file name.  */
226
114
        _fx_unicode_temp_long_file_name[i] =  (UCHAR)((UINT)'0' + (i % 9));
227
    }
228
25
    _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
65
        status = _fx_file_rename(media_ptr, old_shortname, (CHAR *)_fx_unicode_temp_long_file_name);
236
237
        /* Determine if there was an error.  */
238
65
        if (status == FX_ALREADY_CREATED)
239
        {
240
241
            /* Adjust the name slightly and try again!  */
242
41
            _fx_unicode_temp_long_file_name[0]--;
243
244
            /* Determine if it is outside the lower case boundary.  */
245
41
            if (_fx_unicode_temp_long_file_name[0] < 0x61)
246
            {
247
1
                break;
248
            }
249
        }
250
64
    } while (status == FX_ALREADY_CREATED);
251
252
    /* Determine if there was an error.  */
253
25
    if (status)
254
    {
255
#ifdef FX_ENABLE_FAULT_TOLERANT
256
        FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr);
257
#endif /* FX_ENABLE_FAULT_TOLERANT */
258
259
        /* Release media protection.  */
260
1
        FX_UNPROTECT
261
262
        /* Return error.  */
263
1
        return(status);
264
    }
265
266
    /* Setup pointer to media name buffer.  */
267
24
    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
24
    dir_entry.fx_dir_entry_short_name[0] =  0;
271
272
    /* Search the system for the supplied file name.  */
273
24
    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
24
    if (status != FX_SUCCESS)
277
    {
278
#ifdef FX_ENABLE_FAULT_TOLERANT
279
        FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr);
280
#endif /* FX_ENABLE_FAULT_TOLERANT */
281
282
        /* Release media protection.  */
283
1
        FX_UNPROTECT
284
285
        /* Return the error status.  */
286
1
        return(status);
287
    }
288
289
    /* We can now change the temporary long file name with the destination unicode name.  */
290
23
    status =  _fx_unicode_directory_entry_change(media_ptr, &dir_entry,  new_unicode_name, new_unicode_length);
291
292
    /* Was this successful?  */
293
23
    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
112
        for (i = 0; i < FX_DIR_NAME_SIZE; i++)
299
        {
300
301
            /* Copy a character.  */
302
109
            new_short_name[i] =  dir_entry.fx_dir_entry_short_name[i];
303
304
            /* Are we done?  */
305
109
            if (new_short_name[i] == FX_NULL)
306
            {
307
19
                break;
308
            }
309
        }
310
    }
311
312
#ifdef FX_ENABLE_FAULT_TOLERANT
313
    /* Check for a bad status.  */
314
    if (status != FX_SUCCESS)
315
    {
316
317
        FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr);
318
319
        /* Release media protection.  */
320
        FX_UNPROTECT
321
322
        /* Return the bad status.  */
323
        return(status);
324
    }
325
326
    /* End transaction. */
327
    status = _fx_fault_tolerant_transaction_end(media_ptr);
328
#endif /* FX_ENABLE_FAULT_TOLERANT */
329
330
    /* Release the protection.  */
331
23
    FX_UNPROTECT
332
333
    /* Return completion status.  */
334
23
    return(status);
335
}
336