GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: fx_directory_rename.c Lines: 78 78 100.0 %
Date: 2024-01-10 21:53:23 Branches: 52 52 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
/**   Directory                                                           */
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_system.h"
30
#include "fx_directory.h"
31
#include "fx_file.h"
32
#include "fx_utility.h"
33
#ifdef FX_ENABLE_EXFAT
34
#include "fx_directory_exFAT.h"
35
#endif /* FX_ENABLE_EXFAT */
36
#ifdef FX_ENABLE_FAULT_TOLERANT
37
#include "fx_fault_tolerant.h"
38
#endif /* FX_ENABLE_FAULT_TOLERANT */
39
40
41
/**************************************************************************/
42
/*                                                                        */
43
/*  FUNCTION                                               RELEASE        */
44
/*                                                                        */
45
/*    _fx_directory_rename                                PORTABLE C      */
46
/*                                                           6.1          */
47
/*  AUTHOR                                                                */
48
/*                                                                        */
49
/*    William E. Lamie, Microsoft Corporation                             */
50
/*                                                                        */
51
/*  DESCRIPTION                                                           */
52
/*                                                                        */
53
/*    This function first attempts to find the specified directory.       */
54
/*    If found, the rename request is valid and the directory will be     */
55
/*    changed to the new name.  Otherwise, if the directory is not found, */
56
/*    the appropriate error code is returned to the caller.               */
57
/*                                                                        */
58
/*  INPUT                                                                 */
59
/*                                                                        */
60
/*    media_ptr                             Media control block pointer   */
61
/*    old_directory_name                    Old file directory pointer    */
62
/*    new_directory_name                    New file directory pointer    */
63
/*                                                                        */
64
/*  OUTPUT                                                                */
65
/*                                                                        */
66
/*    return status                                                       */
67
/*                                                                        */
68
/*  CALLS                                                                 */
69
/*                                                                        */
70
/*    _fx_directory_entry_write             Write the new directory entry */
71
/*    _fx_directory_free_search             Search for a free directory   */
72
/*                                            entry                       */
73
/*    _fx_directory_name_extract            Extract directory name        */
74
/*    _fx_directory_search                  Search for the file name in   */
75
/*                                          the directory structure       */
76
/*    _fx_fault_tolerant_transaction_start  Start fault tolerant          */
77
/*                                            transaction                 */
78
/*    _fx_fault_tolerant_transaction_end    End fault tolerant transaction*/
79
/*    _fx_fault_tolerant_recover            Recover FAT chain             */
80
/*    _fx_fault_tolerant_reset_log_file     Reset the log file            */
81
/*                                                                        */
82
/*  CALLED BY                                                             */
83
/*                                                                        */
84
/*    Application Code                                                    */
85
/*                                                                        */
86
/*  RELEASE HISTORY                                                       */
87
/*                                                                        */
88
/*    DATE              NAME                      DESCRIPTION             */
89
/*                                                                        */
90
/*  05-19-2020     William E. Lamie         Initial Version 6.0           */
91
/*  09-30-2020     William E. Lamie         Modified comment(s),          */
92
/*                                            resulting in version 6.1    */
93
/*                                                                        */
94
/**************************************************************************/
95
88
UINT  _fx_directory_rename(FX_MEDIA *media_ptr, CHAR *old_directory_name, CHAR *new_directory_name)
96
{
97
98
UINT         status;
99
FX_DIR_ENTRY old_dir_entry;
100
FX_DIR_ENTRY new_dir_entry;
101
FX_DIR_ENTRY search_directory;
102
CHAR        *new_name_ptr;
103
ULONG        i;
104
CHAR        *work_ptr;
105
CHAR         alpha, beta;
106
#ifdef FX_RENAME_PATH_INHERIT
107
UINT         j;
108
#endif
109
110
111
#ifndef FX_MEDIA_STATISTICS_DISABLE
112
113
    /* Increment the number of times this service has been called.  */
114
88
    media_ptr -> fx_media_directory_renames++;
115
#endif
116
117
    /* Setup pointers to media name buffers.  */
118
88
    old_dir_entry.fx_dir_entry_name =     media_ptr -> fx_media_name_buffer + FX_MAX_LONG_NAME_LEN;
119
88
    new_dir_entry.fx_dir_entry_name =     media_ptr -> fx_media_name_buffer + FX_MAX_LONG_NAME_LEN * 2;
120
88
    search_directory.fx_dir_entry_name =  media_ptr -> fx_media_name_buffer + FX_MAX_LONG_NAME_LEN * 3;
121
122
    /* Clear the short name strings.  */
123
88
    old_dir_entry.fx_dir_entry_short_name[0] =     0;
124
88
    new_dir_entry.fx_dir_entry_short_name[0] =     0;
125
88
    search_directory.fx_dir_entry_short_name[0] =  0;
126
127
    /* Determine if the supplied name is less than the maximum supported name size. The
128
       maximum name (FX_MAX_LONG_NAME_LEN) is defined in fx_api.h.  */
129
88
    i =  0;
130
88
    work_ptr =  (CHAR *)new_directory_name;
131

1537
    while (*work_ptr && (i < FX_MAX_LONG_NAME_LEN))
132
    {
133
134
        /* Determine if the character designates a new path.  */
135

1449
        if ((*work_ptr == '\\') || (*work_ptr == '/'))
136
        {
137
            /* Yes, reset the name size.  */
138
12
            i =  0;
139
        }
140
        /* Check for leading spaces.  */
141

1437
        else if ((*work_ptr != ' ') || (i != 0))
142
        {
143
144
            /* No leading spaces, increment the name size.  */
145
1432
            i++;
146
        }
147
148
        /* Move to the next character.  */
149
1449
        work_ptr++;
150
    }
151
152
    /* Determine if the supplied name is valid.  */
153

88
    if ((i == 0) || (i >= FX_MAX_LONG_NAME_LEN))
154
    {
155
156
        /* Return an invalid name value.  */
157
3
        return(FX_INVALID_NAME);
158
    }
159
160
    /* Check the media to make sure it is open.  */
161
85
    if (media_ptr -> fx_media_id != FX_MEDIA_ID)
162
    {
163
164
        /* Return the media not opened error.  */
165
1
        return(FX_MEDIA_NOT_OPEN);
166
    }
167
168
    /* If trace is enabled, insert this event into the trace buffer.  */
169
    FX_TRACE_IN_LINE_INSERT(FX_TRACE_DIRECTORY_RENAME, media_ptr, old_directory_name, new_directory_name, 0, FX_TRACE_DIRECTORY_EVENTS, 0, 0)
170
171
    /* Protect against other threads accessing the media.  */
172
84
    FX_PROTECT
173
174
#ifdef FX_ENABLE_FAULT_TOLERANT
175
    /* Start transaction. */
176
    _fx_fault_tolerant_transaction_start(media_ptr);
177
#endif /* FX_ENABLE_FAULT_TOLERANT */
178
179
    /* Check for write protect at the media level (set by driver).  */
180
84
    if (media_ptr -> fx_media_driver_write_protect)
181
    {
182
183
#ifdef FX_ENABLE_FAULT_TOLERANT
184
        FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr);
185
#endif /* FX_ENABLE_FAULT_TOLERANT */
186
187
        /* Release media protection.  */
188
1
        FX_UNPROTECT
189
190
        /* Return write protect error.  */
191
1
        return(FX_WRITE_PROTECT);
192
    }
193
194
    /* Search the system for the supplied directory name.  */
195
83
    status =  _fx_directory_search(media_ptr, old_directory_name, &old_dir_entry, &search_directory, FX_NULL);
196
197
    /* Determine if the search was successful.  */
198
83
    if (status != FX_SUCCESS)
199
    {
200
201
#ifdef FX_ENABLE_FAULT_TOLERANT
202
        FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr);
203
#endif /* FX_ENABLE_FAULT_TOLERANT */
204
205
        /* Release media protection.  */
206
1
        FX_UNPROTECT
207
208
        /* Return the error code.  */
209
1
        return(status);
210
    }
211
212
    /* Check to make sure the found entry is a directory.  */
213
82
    if (!(old_dir_entry.fx_dir_entry_attributes & (UCHAR)(FX_DIRECTORY)))
214
    {
215
216
#ifdef FX_ENABLE_FAULT_TOLERANT
217
        FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr);
218
#endif /* FX_ENABLE_FAULT_TOLERANT */
219
220
        /* Release media protection.  */
221
2
        FX_UNPROTECT
222
223
        /* Return the not a directory error code.  */
224
2
        return(FX_NOT_DIRECTORY);
225
    }
226
227
#ifdef FX_RENAME_PATH_INHERIT
228
229
    /* Determine if the source directory name has a path and the target directory name does not.  */
230
    if (((old_directory_name[0] == '/') || (old_directory_name[0] == '\\')) && (new_directory_name[0] != '/') && (new_directory_name[0] != '\\'))
231
    {
232
233
        /* In this case, we need to prepend the path of the old directory name to that of the new directory name.  */
234
235
        /* Setup pointer to the rename buffer.  */
236
        work_ptr =  (CHAR *)media_ptr -> fx_media_rename_buffer;
237
238
        /* First, copy the path of the old directory name.  */
239
        i =  0;
240
        j =  0;
241
        while ((old_directory_name[i]) && (i < FX_MAXIMUM_PATH))
242
        {
243
244
            /* Copy a character into the rename buffer.  */
245
            *work_ptr++ =  old_directory_name[i];
246
247
            /* Determine if this character is directory separator.  */
248
            if ((old_directory_name[i] == '/') || (old_directory_name[i] == '\\'))
249
            {
250
251
                /* Yes, directory separator has been found - remember the index.  */
252
                j =  i;
253
            }
254
255
            /* Move to next position in the old directory name.  */
256
            i++;
257
        }
258
259
        /* At this point, we have the path stored in the rename buffer.  */
260
261
        /* Position past the last slash or backslash.  */
262
        j++;
263
264
        /* Reset the working pointer to the position after the last directory separator.  */
265
        work_ptr =  (CHAR *)&(media_ptr -> fx_media_rename_buffer[j]);
266
267
        /* Now copy the new directory name into the rename buffer.  */
268
        i =  0;
269
        while ((new_directory_name[i]) && (j < FX_MAXIMUM_PATH))
270
        {
271
272
            /* Copy a character into the rename buffer.  */
273
            *work_ptr++ =  new_directory_name[i];
274
275
            /* Move to next character.  */
276
            i++;
277
            j++;
278
        }
279
280
        /* Determine if the path was successfully prepended.  */
281
        if (new_directory_name[i])
282
        {
283
284
            /* No, there was not enough room in the destination buffer.  */
285
286
#ifdef FX_ENABLE_FAULT_TOLERANT
287
            FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr);
288
#endif /* FX_ENABLE_FAULT_TOLERANT */
289
290
            /* Release media protection.  */
291
            FX_UNPROTECT
292
293
            /* Return the invalid path error code.  */
294
            return(FX_INVALID_PATH);
295
        }
296
297
        /* Place a NULL at the end of the string.  */
298
        *work_ptr =  (CHAR)FX_NULL;
299
300
        /* At this point, we have successfully prepended the path in the new directory name, override
301
           the new directory name so it is used from now on.  */
302
        new_directory_name =  (CHAR *)media_ptr -> fx_media_rename_buffer;
303
    }
304
#endif
305
306
    /* Search the media for the new directory name - including any supplied path.  */
307
80
    status = _fx_directory_search(media_ptr, new_directory_name, &new_dir_entry, &search_directory, &new_name_ptr);
308
309
    /* Determine if the search found anything.  */
310
80
    if (status == FX_SUCCESS)
311
    {
312
313
        /* Determine if the new name simply has an ASCII case change. If so, simply let the processing
314
           continue.  */
315
58
        i =  0;
316
        do
317
        {
318
319
            /* Pickup an old name and new name character and convert to upper case if necessary.  */
320
375
            alpha =  old_directory_name[i];
321

375
            if ((alpha >= 'a') && (alpha <= 'z'))
322
            {
323
324
                /* Lower case, convert to upper case!  */
325
260
                alpha =  (CHAR)((INT)alpha - 0x20);
326
            }
327
375
            beta =   new_directory_name[i];
328

375
            if ((beta >= 'a') && (beta <= 'z'))
329
            {
330
331
                /* Lower case, convert to upper case!  */
332
315
                beta =  (CHAR)((INT)beta - 0x20);
333
            }
334
335
            /* Now compare the characters.  */
336

375
            if ((alpha != beta) || (alpha == 0))
337
            {
338
339
                /* Get out of this loop!  */
340
                break;
341
            }
342
343
            /* Move to next character.  */
344
318
            i++;
345
318
        } while (i < (FX_MAXIMUM_PATH-1));
346
347
        /* Now determine if the names match.  */
348
58
        if (alpha != beta)
349
        {
350
351
            /* Yes, the directory name already exists in the target location.  */
352
353
#ifdef FX_ENABLE_FAULT_TOLERANT
354
            FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr);
355
#endif /* FX_ENABLE_FAULT_TOLERANT */
356
357
            /* Release media protection.  */
358
43
            FX_UNPROTECT
359
360
            /* Return the error code.  */
361
43
            return(FX_ALREADY_CREATED);
362
        }
363
    }
364
365
    /* Make sure the name is valid.  */
366
37
    if (_fx_directory_name_extract(new_name_ptr, &new_dir_entry.fx_dir_entry_name[0]))
367
    {
368
369
#ifdef FX_ENABLE_FAULT_TOLERANT
370
        FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr);
371
#endif /* FX_ENABLE_FAULT_TOLERANT */
372
373
        /* Release media protection */
374
1
        FX_UNPROTECT
375
376
        /* Return the error code */
377
1
        return(FX_INVALID_NAME);
378
    }
379
380
    /* Look for a free slot in the target directory.  */
381
36
    status =  _fx_directory_free_search(media_ptr, &search_directory, &new_dir_entry);
382
383
    /* Was a free slot found?  */
384
36
    if (status != FX_SUCCESS)
385
    {
386
387
#ifdef FX_ENABLE_FAULT_TOLERANT
388
        FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr);
389
#endif /* FX_ENABLE_FAULT_TOLERANT */
390
391
        /* No, release protection.  */
392
2
        FX_UNPROTECT
393
394
        /* Return the error code.  */
395
2
        return(status);
396
    }
397
398
    /* Extract the new directory name.  */
399
34
    _fx_directory_name_extract(new_name_ptr, &new_dir_entry.fx_dir_entry_name[0]);
400
401
    /* Determine if the name was a long directory name.   */
402
34
    if (new_dir_entry.fx_dir_entry_long_name_present)
403
    {
404
405
        /* Yes, clear the short directory name to force a new one.  */
406
33
        new_dir_entry.fx_dir_entry_short_name[0] =  0;
407
    }
408
409
    /* Setup new attributes for the new directory entry.  */
410
34
    new_dir_entry.fx_dir_entry_attributes = old_dir_entry.fx_dir_entry_attributes;
411
34
    new_dir_entry.fx_dir_entry_cluster    = old_dir_entry.fx_dir_entry_cluster;
412
34
    new_dir_entry.fx_dir_entry_file_size  = old_dir_entry.fx_dir_entry_file_size;
413
414
    /* Save the reserved field.  */
415
34
    new_dir_entry.fx_dir_entry_reserved =            old_dir_entry.fx_dir_entry_reserved;
416
417
    /* Set time and date stamps.  */
418
34
    new_dir_entry.fx_dir_entry_created_time_ms =     old_dir_entry.fx_dir_entry_created_time_ms;
419
34
    new_dir_entry.fx_dir_entry_created_time =        old_dir_entry.fx_dir_entry_created_time;
420
34
    new_dir_entry.fx_dir_entry_created_date =        old_dir_entry.fx_dir_entry_created_date;
421
34
    new_dir_entry.fx_dir_entry_last_accessed_date =  old_dir_entry.fx_dir_entry_last_accessed_date;
422
34
    new_dir_entry.fx_dir_entry_time =                old_dir_entry.fx_dir_entry_time;
423
34
    new_dir_entry.fx_dir_entry_date =                old_dir_entry.fx_dir_entry_date;
424
425
#ifdef FX_ENABLE_EXFAT
426
    if (media_ptr -> fx_media_FAT_type == FX_exFAT)
427
    {
428
429
        new_dir_entry.fx_dir_entry_dont_use_fat =           old_dir_entry.fx_dir_entry_dont_use_fat;
430
        new_dir_entry.fx_dir_entry_type =                   old_dir_entry.fx_dir_entry_type;
431
        new_dir_entry.fx_dir_entry_available_file_size =    old_dir_entry.fx_dir_entry_available_file_size;
432
        new_dir_entry.fx_dir_entry_secondary_count =        old_dir_entry.fx_dir_entry_secondary_count;
433
    }
434
#endif /* FX_ENABLE_EXFAT */
435
436
    /* Is there a leading dot?  */
437
34
    if (new_dir_entry.fx_dir_entry_name[0] == '.')
438
    {
439
440
        /* Yes, toggle the hidden attribute bit.  */
441
1
        new_dir_entry.fx_dir_entry_attributes |=  FX_HIDDEN;
442
    }
443
444
#ifndef FX_MEDIA_DISABLE_SEARCH_CACHE
445
446
    /* Invalidate the directory cache.  */
447
34
    media_ptr -> fx_media_last_found_name[0] =  FX_NULL;
448
#endif
449
450
    /* Now write out the directory entry.  */
451
#ifdef FX_ENABLE_EXFAT
452
    if (media_ptr -> fx_media_FAT_type == FX_exFAT)
453
    {
454
455
        status = _fx_directory_exFAT_entry_write(media_ptr, &new_dir_entry, UPDATE_FULL);
456
    }
457
    else
458
    {
459
#endif /* FX_ENABLE_EXFAT */
460
34
        status =  _fx_directory_entry_write(media_ptr, &new_dir_entry);
461
#ifdef FX_ENABLE_EXFAT
462
    }
463
#endif /* FX_ENABLE_EXFAT */
464
465
    /* Determine if the write was successful.  */
466
34
    if (status != FX_SUCCESS)
467
    {
468
469
#ifdef FX_ENABLE_FAULT_TOLERANT
470
        FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr);
471
#endif /* FX_ENABLE_FAULT_TOLERANT */
472
473
        /* Release media protection.  */
474
1
        FX_UNPROTECT
475
476
        /* Return the error code.  */
477
1
        return(status);
478
    }
479
480
    /* Set the old directory entry to free.  */
481
33
    old_dir_entry.fx_dir_entry_name[0] =        (CHAR)FX_DIR_ENTRY_FREE;
482
33
    old_dir_entry.fx_dir_entry_short_name[0] =  (CHAR)FX_DIR_ENTRY_FREE;
483
484
    /* Now wipe out the old directory entry.  */
485
#ifdef FX_ENABLE_EXFAT
486
    if (media_ptr -> fx_media_FAT_type == FX_exFAT)
487
    {
488
489
        status = _fx_directory_exFAT_entry_write(media_ptr, &old_dir_entry, UPDATE_DELETE);
490
    }
491
    else
492
    {
493
#endif /* FX_ENABLE_EXFAT */
494
33
        status =  _fx_directory_entry_write(media_ptr, &old_dir_entry);
495
#ifdef FX_ENABLE_EXFAT
496
    }
497
#endif /* FX_ENABLE_EXFAT */
498
499
#ifdef FX_ENABLE_FAULT_TOLERANT
500
    /* Check for a bad status.  */
501
    if (status != FX_SUCCESS)
502
    {
503
504
        FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr);
505
506
        /* Release media protection.  */
507
        FX_UNPROTECT
508
509
        /* Return the bad status.  */
510
        return(status);
511
    }
512
513
    /* End transaction. */
514
    status = _fx_fault_tolerant_transaction_end(media_ptr);
515
#endif /* FX_ENABLE_FAULT_TOLERANT */
516
517
    /* Release media protection.  */
518
33
    FX_UNPROTECT
519
520
    /* Directory rename is complete, return status.  */
521
33
    return(status);
522
}
523