GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: fx_unicode_directory_rename.c Lines: 63 63 100.0 %
Date: 2024-01-10 21:53:23 Branches: 46 46 100.0 %

Line Branch Exec Source
1
/**************************************************************************/
2
/*                                                                        */
3
/*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4
/*                                                                        */
5
/*       This software is licensed under the Microsoft Software License   */
6
/*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7
/*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8
/*       and in the root directory of this software.                      */
9
/*                                                                        */
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_rename                        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
/*    This function first attempts to find the specified directory.       */
51
/*    If found, the unicode rename request is valid and the directory     */
52
/*    will be changed to the new unicode name. Otherwise, if the directory*/
53
/*    is not found, the 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                    Old unicode name length       */
62
/*    new_short_name                        Pointer to new short name     */
63
/*                                                                        */
64
/*  OUTPUT                                                                */
65
/*                                                                        */
66
/*    Completion Status                                                   */
67
/*                                                                        */
68
/*  CALLS                                                                 */
69
/*                                                                        */
70
/*    _fx_unicode_short_name_get            Get the directory short name  */
71
/*    _fx_unicode_directory_search          Search for unicode name       */
72
/*    _fx_unicode_directory_entry_change    Change unicode file name      */
73
/*    _fx_directory_rename                  Rename the directory          */
74
/*    _fx_directory_search                  Search directory              */
75
/*                                                                        */
76
/*  CALLED BY                                                             */
77
/*                                                                        */
78
/*    Application Code                                                    */
79
/*                                                                        */
80
/*  RELEASE HISTORY                                                       */
81
/*                                                                        */
82
/*    DATE              NAME                      DESCRIPTION             */
83
/*                                                                        */
84
/*  05-19-2020     William E. Lamie         Initial Version 6.0           */
85
/*  09-30-2020     William E. Lamie         Modified comment(s),          */
86
/*                                            resulting in version 6.1    */
87
/*                                                                        */
88
/**************************************************************************/
89
51
UINT  _fx_unicode_directory_rename(FX_MEDIA *media_ptr, UCHAR *old_unicode_name, ULONG old_unicode_length,
90
                                   UCHAR *new_unicode_name, ULONG new_unicode_length, CHAR *new_short_name)
91
{
92
93
FX_DIR_ENTRY dir_entry;
94
UINT         i, status;
95
ULONG        temp_length;
96
CHAR         old_dir_short_name[13];
97
CHAR         new_dir_short_name[13];
98
UCHAR        alpha, beta;
99
100
101
    /* Clear the return short name.  */
102
51
    new_short_name[0] =  FX_NULL;
103
104
    /* Set shortname to null.  */
105
51
    old_dir_short_name[0] =  FX_NULL;
106
51
    new_dir_short_name[0] =  FX_NULL;
107
108
    /* Check the media to make sure it is open.  */
109
51
    if (media_ptr -> fx_media_id != FX_MEDIA_ID)
110
    {
111
112
        /* Return the media not opened error.  */
113
3
        return(FX_MEDIA_NOT_OPEN);
114
    }
115
116
#ifdef FX_ENABLE_EXFAT
117
    /* Check if media format is exFAT.  */
118
    if (media_ptr -> fx_media_FAT_type == FX_exFAT)
119
    {
120
121
        /* Return the not implemented error.  */
122
        return(FX_NOT_IMPLEMENTED);
123
    }
124
#endif
125
126
    /* Get the old short name.  */
127
48
    status = _fx_unicode_short_name_get(media_ptr, old_unicode_name, old_unicode_length, &old_dir_short_name[0]);
128
129
    /* Determine if the search was successful.  */
130
48
    if (status != FX_SUCCESS)
131
    {
132
133
        /* Return the error code.  */
134
9
        return(status);
135
    }
136
137
    /* Protect media.  */
138
39
    FX_PROTECT
139
140
    /* Setup temporary length.  */
141
39
    temp_length =  new_unicode_length;
142
143
    /* Setup pointer to media name buffer.  */
144
39
    dir_entry.fx_dir_entry_name =  media_ptr -> fx_media_name_buffer + FX_MAX_LONG_NAME_LEN;
145
146
    /* Clear the short name string.  */
147
39
    dir_entry.fx_dir_entry_short_name[0] =  FX_NULL;
148
149
    /* Determine if the new directory name is already present.  */
150
39
    status =  _fx_unicode_directory_search(media_ptr, &dir_entry, (UCHAR *)new_dir_short_name, sizeof(new_dir_short_name), new_unicode_name, &temp_length, 0);
151
152
    /* Determine if the search was successful.  */
153
39
    if (status == FX_SUCCESS)
154
    {
155
156
        /* Determine if the name length are equal.  */
157
21
        if (old_unicode_length == new_unicode_length)
158
        {
159
160
            /* Determine if the new name simply has a UNICODE case change. If so, simply let the processing
161
               continue.  */
162
18
            i =  0;
163
            do
164
            {
165
166
                /* Pickup an old name and new name character and convert to upper case if necessary.  */
167
40
                alpha =  old_unicode_name[i * 2];
168

40
                if ((alpha >= 'a') && (alpha <= 'z') && (old_unicode_name[i * 2 + 1] == 0))
169
                {
170
171
                    /* Lower case, convert to upper case!  */
172
18
                    alpha =  (UCHAR)((INT)alpha - 0x20);
173
                }
174
40
                beta =   new_unicode_name[i * 2];
175

40
                if ((beta >= 'a') && (beta <= 'z') && (new_unicode_name[i * 2 + 1] == 0))
176
                {
177
178
                    /* Lower case, convert to upper case!  */
179
15
                    beta =  (UCHAR)((INT)beta - 0x20);
180
                }
181
182
                /* Now compare the characters.  */
183
40
                if (alpha != beta)
184
                {
185
186
                    /* Get out of this loop!  */
187
6
                    break;
188
                }
189
190
                /* Pickup the high bytes. */
191
34
                alpha = old_unicode_name[i * 2 + 1];
192
34
                beta =   new_unicode_name[i * 2 + 1];
193
194
                /* Now compare the byte.  */
195
34
                if ((alpha != beta))
196
                {
197
198
                    /* Get out of this loop!  */
199
3
                    break;
200
                }
201
202
                /* Move to next character.  */
203
31
                i++;
204
31
            } while (i < old_unicode_length);
205
        }
206
        else
207
        {
208
3
            alpha = 0;
209
3
            beta = 1;
210
        }
211
212
        /* Now determine if the names match.  */
213
21
        if (alpha != beta)
214
        {
215
216
            /* Yes, the directory name already exists in the target location.  */
217
218
            /* Release media protection.  */
219
12
            FX_UNPROTECT
220
221
            /* Return the error code.  */
222
12
            return(FX_ALREADY_CREATED);
223
         }
224
    }
225
226
#ifdef FX_ENABLE_FAULT_TOLERANT
227
    /* Start transaction. */
228
    _fx_fault_tolerant_transaction_start(media_ptr);
229
#endif /* FX_ENABLE_FAULT_TOLERANT */
230
231
    /* Okay, at this point we need to create a new long directory name that has enough space for the
232
       eventual unicode directory name.  */
233
234
    /* Copy the characters from the unicode directory name and make sure they are
235
       within the ASCII range.  */
236
27
    _fx_unicode_temp_long_file_name[0] =  'z';
237
145
    for (i = 1; i < new_unicode_length; i++)
238
    {
239
240
        /* Build temporary long file name.  */
241
118
        _fx_unicode_temp_long_file_name[i] =  (UCHAR)((UINT)'0' + (i % 9));
242
    }
243
27
    _fx_unicode_temp_long_file_name[i] =  FX_NULL;
244
245
    /* Loop to try different temp long file names... if necessary.  */
246
    do
247
    {
248
249
        /* Rename the directory name.  */
250
68
        status =  _fx_directory_rename(media_ptr, old_dir_short_name, (CHAR *)_fx_unicode_temp_long_file_name);
251
252
        /* Determine if there was an error.  */
253
68
        if (status == FX_ALREADY_CREATED)
254
        {
255
256
            /* Adjust the name slightly and try again!  */
257
42
            _fx_unicode_temp_long_file_name[0]--;
258
259
            /* Determine if it is outside the lower case boundary.  */
260
42
            if (_fx_unicode_temp_long_file_name[0] < 0x61)
261
            {
262
1
                break;
263
            }
264
        }
265
67
    } while (status == FX_ALREADY_CREATED);
266
267
    /* Determine if there was an error.  */
268
27
    if (status)
269
    {
270
271
#ifdef FX_ENABLE_FAULT_TOLERANT
272
        FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr);
273
#endif /* FX_ENABLE_FAULT_TOLERANT */
274
275
        /* Release media protection.  */
276
3
        FX_UNPROTECT
277
278
        /* Return error.  */
279
3
        return(status);
280
    }
281
282
    /* Setup pointer to media name buffer.  */
283
24
    dir_entry.fx_dir_entry_name =  media_ptr -> fx_media_name_buffer + FX_MAX_LONG_NAME_LEN;
284
285
    /* Clear the short name string.  */
286
24
    dir_entry.fx_dir_entry_short_name[0] =  FX_NULL;
287
288
    /* Search the system for the supplied file name.  */
289
24
    status =  _fx_directory_search(media_ptr, (CHAR *)_fx_unicode_temp_long_file_name, &dir_entry, FX_NULL, FX_NULL);
290
291
    /* Determine if the search was successful.  */
292
24
    if (status != FX_SUCCESS)
293
    {
294
295
#ifdef FX_ENABLE_FAULT_TOLERANT
296
        FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr);
297
#endif /* FX_ENABLE_FAULT_TOLERANT */
298
299
        /* Release media protection.  */
300
1
        FX_UNPROTECT
301
302
        /* Return the error status.  */
303
1
        return(status);
304
    }
305
306
    /* We can now change the temporary long file name with the destination unicode name.  */
307
23
    status = _fx_unicode_directory_entry_change(media_ptr, &dir_entry, new_unicode_name, new_unicode_length);
308
309
    /* Determine if the rename was successful.  */
310
23
    if (status == FX_SUCCESS)
311
    {
312
313
        /* Yes, copy the short file name to the destination.  */
314
        /* The new short name only have 8 characters, since we didn't include a dot in temp_long_file_name. */
315
112
        for (i = 0; i < FX_DIR_NAME_SIZE; i++)
316
        {
317
318
            /* Copy a character.  */
319
109
            new_short_name[i] =  dir_entry.fx_dir_entry_short_name[i];
320
321
            /* Are we done?  */
322
109
            if (new_short_name[i] == FX_NULL)
323
            {
324
19
                break;
325
            }
326
        }
327
    }
328
329
#ifdef FX_ENABLE_FAULT_TOLERANT
330
    /* Check for a bad status.  */
331
    if (status != FX_SUCCESS)
332
    {
333
334
        FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr);
335
336
        /* Release media protection.  */
337
        FX_UNPROTECT
338
339
        /* Return the bad status.  */
340
        return(status);
341
    }
342
343
    /* End transaction. */
344
    status = _fx_fault_tolerant_transaction_end(media_ptr);
345
#endif /* FX_ENABLE_FAULT_TOLERANT */
346
347
    /* Release the protection.  */
348
23
    FX_UNPROTECT
349
350
    /* Return completion status.  */
351
23
    return(status);
352
}
353