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