GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: fx_unicode_directory_create.c Lines: 43 43 100.0 %
Date: 2024-01-10 21:53:23 Branches: 24 24 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_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
/*  RELEASE HISTORY                                                       */
79
/*                                                                        */
80
/*    DATE              NAME                      DESCRIPTION             */
81
/*                                                                        */
82
/*  05-19-2020     William E. Lamie         Initial Version 6.0           */
83
/*  09-30-2020     William E. Lamie         Modified comment(s),          */
84
/*                                            resulting in version 6.1    */
85
/*                                                                        */
86
/**************************************************************************/
87
350
UINT  _fx_unicode_directory_create(FX_MEDIA *media_ptr, UCHAR *source_unicode_name, ULONG source_unicode_length, CHAR *short_name)
88
{
89
90
FX_DIR_ENTRY dir_entry;
91
UINT         i, status;
92
ULONG        temp_length;
93
UCHAR        destination_shortname[13];
94
95
96
    /* Setup pointer to media name buffer.  */
97
350
    dir_entry.fx_dir_entry_name =  media_ptr -> fx_media_name_buffer + FX_MAX_LONG_NAME_LEN;
98
99
    /* Clear the short name string.  */
100
350
    dir_entry.fx_dir_entry_short_name[0] =  FX_NULL;
101
102
    /* Set destination shortname to null.  */
103
350
    destination_shortname[0] =  FX_NULL;
104
105
    /* Clear the return short name.  */
106
350
    short_name[0] =  FX_NULL;
107
108
    /* Check the media to make sure it is open.  */
109
350
    if (media_ptr -> fx_media_id != FX_MEDIA_ID)
110
    {
111
112
        /* Return the media not opened error.  */
113
1
        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
    /* If trace is enabled, insert this event into the trace buffer.  */
127
    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)
128
129
    /* Protect media.  */
130
349
    FX_PROTECT
131
132
#ifdef FX_ENABLE_FAULT_TOLERANT
133
    /* Start transaction. */
134
    _fx_fault_tolerant_transaction_start(media_ptr);
135
#endif /* FX_ENABLE_FAULT_TOLERANT */
136
137
    /* Check for write protect at the media level (set by driver).  */
138
349
    if (media_ptr -> fx_media_driver_write_protect)
139
    {
140
#ifdef FX_ENABLE_FAULT_TOLERANT
141
        FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr);
142
#endif /* FX_ENABLE_FAULT_TOLERANT */
143
144
        /* Release media protection.  */
145
1
        FX_UNPROTECT
146
147
        /* Return write protect error.  */
148
1
        return(FX_WRITE_PROTECT);
149
    }
150
151
    /* Setup temporary length.  */
152
348
    temp_length =  source_unicode_length;
153
154
    /* Determine if the destination directory is already present.  */
155
348
    status =  _fx_unicode_directory_search(media_ptr, &dir_entry, destination_shortname, sizeof(destination_shortname), source_unicode_name, &temp_length, 0);
156
157
    /* Determine if the search was successful.  */
158
348
    if (status == FX_SUCCESS)
159
    {
160
#ifdef FX_ENABLE_FAULT_TOLERANT
161
        FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr);
162
#endif /* FX_ENABLE_FAULT_TOLERANT */
163
164
        /* Release media protection.  */
165
9
        FX_UNPROTECT
166
167
        /* Return the error code.  */
168
9
        return(FX_ALREADY_CREATED);
169
    }
170
171
    /* Okay, at this point we need to create a new long directory name that has enough space for the
172
       eventual unicode directory name.  */
173
174
    /* Copy the characters from the unicode directory name and make sure they are
175
       within the ASCII range.  */
176
339
    _fx_unicode_temp_long_file_name[0] =  'z';
177
2263
    for (i = 1; i < source_unicode_length; i++)
178
    {
179
180
        /* Build temporary long file name.  */
181
1924
        _fx_unicode_temp_long_file_name[i] =  (UCHAR)((INT)'0' + (i % 9));
182
    }
183
339
    _fx_unicode_temp_long_file_name[i] =  FX_NULL;
184
185
    /* Loop to try different temp long file names... if necessary.  */
186
    do
187
    {
188
189
        /* Create a new directory with the temp long file name.  */
190
2611
        status =  _fx_directory_create(media_ptr, (CHAR *)_fx_unicode_temp_long_file_name);
191
192
        /* Determine if there was an error.  */
193
2611
        if (status == FX_ALREADY_CREATED)
194
        {
195
196
            /* Adjust the name slightly and try again!  */
197
2283
            _fx_unicode_temp_long_file_name[0]--;
198
199
            /* Determine if it is outside the lower case boundary.  */
200
2283
            if (_fx_unicode_temp_long_file_name[0] < 0x61)
201
            {
202
11
                break;
203
            }
204
        }
205
2600
    } while (status == FX_ALREADY_CREATED);
206
207
    /* Determine if there was an error.  */
208
339
    if (status)
209
    {
210
#ifdef FX_ENABLE_FAULT_TOLERANT
211
        FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr);
212
#endif /* FX_ENABLE_FAULT_TOLERANT */
213
214
        /* Release media protection.  */
215
95
        FX_UNPROTECT
216
217
        /* Return error.  */
218
95
        return(status);
219
    }
220
221
    /* Setup pointer to media name buffer.  */
222
244
    dir_entry.fx_dir_entry_name =  media_ptr -> fx_media_name_buffer + FX_MAX_LONG_NAME_LEN;
223
224
    /* Clear the short name string.  */
225
244
    dir_entry.fx_dir_entry_short_name[0] =  0;
226
227
    /* Search the system for the supplied file name.  */
228
244
    status =  _fx_directory_search(media_ptr, (CHAR *)_fx_unicode_temp_long_file_name, &dir_entry, FX_NULL, FX_NULL);
229
230
    /* Determine if the search was successful.  */
231
244
    if (status != FX_SUCCESS)
232
    {
233
#ifdef FX_ENABLE_FAULT_TOLERANT
234
        FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr);
235
#endif /* FX_ENABLE_FAULT_TOLERANT */
236
237
        /* Release media protection.  */
238
5
        FX_UNPROTECT
239
240
        /* Return the error status.  */
241
5
        return(status);
242
    }
243
244
    /* We can now change the temporary long file name with the destination unicode name.  */
245
239
    status =  _fx_unicode_directory_entry_change(media_ptr, &dir_entry,  source_unicode_name, source_unicode_length);
246
247
    /* Was this successful?  */
248
239
    if (status == FX_SUCCESS)
249
    {
250
251
        /* Yes, copy the short file name to the destination.  */
252
        /* The new short name only have 8 characters, since we didn't include a dot in temp_long_file_name. */
253
1473
        for (i = 0; i < FX_DIR_NAME_SIZE; i++)
254
        {
255
256
            /* Copy a character.  */
257
1373
            short_name[i] =  dir_entry.fx_dir_entry_short_name[i];
258
259
            /* Are we done?  */
260
1373
            if (short_name[i] == FX_NULL)
261
            {
262
137
                break;
263
            }
264
        }
265
    }
266
267
#ifdef FX_ENABLE_FAULT_TOLERANT
268
    /* Check for a bad status.  */
269
    if (status != FX_SUCCESS)
270
    {
271
272
        FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr);
273
274
        /* Release media protection.  */
275
        FX_UNPROTECT
276
277
        /* Return the bad status.  */
278
        return(status);
279
    }
280
281
    /* End transaction. */
282
    status = _fx_fault_tolerant_transaction_end(media_ptr);
283
#endif /* FX_ENABLE_FAULT_TOLERANT */
284
285
    /* Release the protection.  */
286
239
    FX_UNPROTECT
287
288
    /* Return completion status.  */
289
239
    return(status);
290
}
291