GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: fx_unicode_file_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_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_create                             PORTABLE C      */
43
/*                                                           6.1          */
44
/*  AUTHOR                                                                */
45
/*                                                                        */
46
/*    William E. Lamie, Microsoft Corporation                             */
47
/*                                                                        */
48
/*  DESCRIPTION                                                           */
49
/*                                                                        */
50
/*    This function creates the specified unicode name.                   */
51
/*                                                                        */
52
/*  INPUT                                                                 */
53
/*                                                                        */
54
/*    media_ptr                             Pointer to media              */
55
/*    source_unicode_name                   Pointer to source unicode name*/
56
/*    source_unicode_length                 Unicode name length           */
57
/*    short_name                            Designated short name         */
58
/*                                                                        */
59
/*  OUTPUT                                                                */
60
/*                                                                        */
61
/*    Completion Status                                                   */
62
/*                                                                        */
63
/*  CALLS                                                                 */
64
/*                                                                        */
65
/*    _fx_directory_search                  Search directory              */
66
/*    _fx_unicode_directory_entry_change    Change unicode file name      */
67
/*    _fx_unicode_directory_search          Search for unicode name       */
68
/*    _fx_file_create                       Create a file                 */
69
/*    _fx_fault_tolerant_transaction_start  Start fault tolerant          */
70
/*                                            transaction                 */
71
/*    _fx_fault_tolerant_transaction_end    End fault tolerant transaction*/
72
/*    _fx_fault_tolerant_recover            Recover FAT chain             */
73
/*    _fx_fault_tolerant_reset_log_file     Reset the log file            */
74
/*                                                                        */
75
/*  CALLED BY                                                             */
76
/*                                                                        */
77
/*    Application Code                                                    */
78
/*                                                                        */
79
/*  RELEASE HISTORY                                                       */
80
/*                                                                        */
81
/*    DATE              NAME                      DESCRIPTION             */
82
/*                                                                        */
83
/*  05-19-2020     William E. Lamie         Initial Version 6.0           */
84
/*  09-30-2020     William E. Lamie         Modified comment(s),          */
85
/*                                            resulting in version 6.1    */
86
/*                                                                        */
87
/**************************************************************************/
88
105
UINT  _fx_unicode_file_create(FX_MEDIA *media_ptr, UCHAR *source_unicode_name, ULONG source_unicode_length, CHAR *short_name)
89
{
90
91
FX_DIR_ENTRY dir_entry;
92
UINT         i, status;
93
ULONG        temp_length;
94
UCHAR        destination_shortname[13];
95
96
97
    /* Setup pointer to media name buffer.  */
98
105
    dir_entry.fx_dir_entry_name =  media_ptr -> fx_media_name_buffer + FX_MAX_LONG_NAME_LEN;
99
100
    /* Clear the short name string.  */
101
105
    dir_entry.fx_dir_entry_short_name[0] =  FX_NULL;
102
103
    /* Set destination shortname to null.  */
104
105
    destination_shortname[0] =  FX_NULL;
105
106
    /* Clear the return short name.  */
107
105
    short_name[0] =  FX_NULL;
108
109
    /* Check the media to make sure it is open.  */
110
105
    if (media_ptr -> fx_media_id != FX_MEDIA_ID)
111
    {
112
113
        /* Return the media not opened error.  */
114
1
        return(FX_MEDIA_NOT_OPEN);
115
    }
116
117
#ifdef FX_ENABLE_EXFAT
118
    /* Check if media format is exFAT.  */
119
    if (media_ptr -> fx_media_FAT_type == FX_exFAT)
120
    {
121
122
        /* Return the not implemented error.  */
123
        return(FX_NOT_IMPLEMENTED);
124
    }
125
#endif
126
127
    /* If trace is enabled, insert this event into the trace buffer.  */
128
    FX_TRACE_IN_LINE_INSERT(FX_TRACE_UNICODE_FILE_CREATE, media_ptr, source_unicode_name, source_unicode_length, short_name, FX_TRACE_FILE_EVENTS, 0, 0)
129
130
    /* Protect media.  */
131
104
    FX_PROTECT
132
133
#ifdef FX_ENABLE_FAULT_TOLERANT
134
    /* Start transaction. */
135
    _fx_fault_tolerant_transaction_start(media_ptr);
136
#endif /* FX_ENABLE_FAULT_TOLERANT */
137
138
    /* Check for write protect at the media level (set by driver).  */
139
104
    if (media_ptr -> fx_media_driver_write_protect)
140
    {
141
#ifdef FX_ENABLE_FAULT_TOLERANT
142
        FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr);
143
#endif /* FX_ENABLE_FAULT_TOLERANT */
144
145
        /* Release media protection.  */
146
1
        FX_UNPROTECT
147
148
        /* Return write protect error.  */
149
1
        return(FX_WRITE_PROTECT);
150
    }
151
152
    /* Setup temporary length.  */
153
103
    temp_length =  source_unicode_length;
154
155
    /* Determine if the destination file is already present.  */
156
103
    status =  _fx_unicode_directory_search(media_ptr, &dir_entry, destination_shortname, sizeof(destination_shortname), source_unicode_name, &temp_length, 0);
157
158
    /* Determine if the search was successful.  */
159
103
    if (status == FX_SUCCESS)
160
    {
161
#ifdef FX_ENABLE_FAULT_TOLERANT
162
        FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr);
163
#endif /* FX_ENABLE_FAULT_TOLERANT */
164
165
        /* Release media protection.  */
166
15
        FX_UNPROTECT
167
168
        /* Return the error code.  */
169
15
        return(FX_ALREADY_CREATED);
170
    }
171
172
    /* Okay, at this point we need to create a new long file name that has enough space for the
173
       eventual unicode file name.  */
174
175
    /* Copy the characters from the unicode file name and make sure they are
176
       within the ASCII range.  */
177
88
    _fx_unicode_temp_long_file_name[0] =  'z';
178
442
    for (i = 1; i < source_unicode_length; i++)
179
    {
180
181
        /* Build temporary long file name.  */
182
354
        _fx_unicode_temp_long_file_name[i] =  (UCHAR)((UINT)'0' + (i % 9));
183
    }
184
88
    _fx_unicode_temp_long_file_name[i] =  FX_NULL;
185
186
    /* Loop to try different temp long file names... if necessary.  */
187
    do
188
    {
189
190
        /* Create a new file with the temp long file name.  */
191
516
        status =  _fx_file_create(media_ptr, (CHAR *)_fx_unicode_temp_long_file_name);
192
193
        /* Determine if there was an error.  */
194
516
        if (status == FX_ALREADY_CREATED)
195
        {
196
197
            /* Adjust the name slightly and try again!  */
198
431
            _fx_unicode_temp_long_file_name[0]--;
199
200
            /* Determine if it is outside the lower case boundary.  */
201
431
            if (_fx_unicode_temp_long_file_name[0] < 0x61)
202
            {
203
3
                break;
204
            }
205
        }
206
513
    } while (status == FX_ALREADY_CREATED);
207
208
    /* Determine if there was an error.  */
209
88
    if (status)
210
    {
211
#ifdef FX_ENABLE_FAULT_TOLERANT
212
        FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr);
213
#endif /* FX_ENABLE_FAULT_TOLERANT */
214
215
        /* Release media protection.  */
216
6
        FX_UNPROTECT
217
218
        /* Return error.  */
219
6
        return(status);
220
    }
221
222
    /* Setup pointer to media name buffer.  */
223
82
    dir_entry.fx_dir_entry_name =  media_ptr -> fx_media_name_buffer + FX_MAX_LONG_NAME_LEN;
224
225
    /* Clear the short name string.  */
226
82
    dir_entry.fx_dir_entry_short_name[0] =  0;
227
228
    /* Search the system for the supplied file name.  */
229
82
    status =  _fx_directory_search(media_ptr, (CHAR *)_fx_unicode_temp_long_file_name, &dir_entry, FX_NULL, FX_NULL);
230
231
    /* Determine if the search was successful.  */
232
82
    if (status != FX_SUCCESS)
233
    {
234
#ifdef FX_ENABLE_FAULT_TOLERANT
235
        FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr);
236
#endif /* FX_ENABLE_FAULT_TOLERANT */
237
238
        /* Release media protection.  */
239
1
        FX_UNPROTECT
240
241
        /* Return the error status.  */
242
1
        return(status);
243
    }
244
245
    /* We can now change the temporary long file name with the destination unicode name.  */
246
81
    status =  _fx_unicode_directory_entry_change(media_ptr, &dir_entry,  source_unicode_name, source_unicode_length);
247
248
    /* Was this successful?  */
249
81
    if (status == FX_SUCCESS)
250
    {
251
252
        /* Yes, copy the short file name to the destination.  */
253
        /* The new short name only have 8 characters, since we didn't include a dot in temp_long_file_name. */
254
412
        for (i = 0; i < FX_DIR_NAME_SIZE; i++)
255
        {
256
257
            /* Copy a character.  */
258
401
            short_name[i] =  dir_entry.fx_dir_entry_short_name[i];
259
260
            /* Are we done?  */
261
401
            if (short_name[i] == FX_NULL)
262
            {
263
69
                break;
264
            }
265
        }
266
    }
267
268
#ifdef FX_ENABLE_FAULT_TOLERANT
269
    /* Check for a bad status.  */
270
    if (status != FX_SUCCESS)
271
    {
272
273
        FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr);
274
275
        /* Release media protection.  */
276
        FX_UNPROTECT
277
278
        /* Return the bad status.  */
279
        return(status);
280
    }
281
282
    /* End transaction. */
283
    status = _fx_fault_tolerant_transaction_end(media_ptr);
284
#endif /* FX_ENABLE_FAULT_TOLERANT */
285
286
    /* Release the protection.  */
287
81
    FX_UNPROTECT
288
289
    /* Return completion status.  */
290
81
    return(status);
291
}
292