GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: fx_file_create.c Lines: 46 46 100.0 %
Date: 2026-03-06 18:49:02 Branches: 26 26 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
/**   File                                                                */
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_FAULT_TOLERANT
34
#include "fx_fault_tolerant.h"
35
#endif /* FX_ENABLE_FAULT_TOLERANT */
36
37
38
/**************************************************************************/
39
/*                                                                        */
40
/*  FUNCTION                                               RELEASE        */
41
/*                                                                        */
42
/*    _fx_file_create                                     PORTABLE C      */
43
/*                                                           6.1          */
44
/*  AUTHOR                                                                */
45
/*                                                                        */
46
/*    William E. Lamie, Microsoft Corporation                             */
47
/*                                                                        */
48
/*  DESCRIPTION                                                           */
49
/*                                                                        */
50
/*    This function first attempts to find the specified file.  If found, */
51
/*    the create request is invalid and an error is returned to the       */
52
/*    caller.  After the file name verification is made, a search for a   */
53
/*    free directory entry will be made.  If nothing is available, an     */
54
/*    error will be returned to the caller.  Otherwise, if all is okay, a */
55
/*    file of 0 bytes will be created.                                    */
56
/*                                                                        */
57
/*  INPUT                                                                 */
58
/*                                                                        */
59
/*    media_ptr                             Media control block pointer   */
60
/*    file_name                             File name                     */
61
/*                                                                        */
62
/*  OUTPUT                                                                */
63
/*                                                                        */
64
/*    return status                                                       */
65
/*                                                                        */
66
/*  CALLS                                                                 */
67
/*                                                                        */
68
/*    _fx_directory_entry_write             Write the new directory entry */
69
/*    _fx_directory_name_extract            Extract directory name        */
70
/*    _fx_directory_search                  Search for the file name in   */
71
/*                                          the directory structure       */
72
/*    _fx_directory_free_search             Search for a free directory   */
73
/*                                            entry                       */
74
/*    _fx_fault_tolerant_transaction_start  Start fault tolerant          */
75
/*                                            transaction                 */
76
/*    _fx_fault_tolerant_transaction_end    End fault tolerant transaction*/
77
/*    _fx_fault_tolerant_recover            Recover FAT chain             */
78
/*    _fx_fault_tolerant_reset_log_file     Reset the log file            */
79
/*                                                                        */
80
/*  CALLED BY                                                             */
81
/*                                                                        */
82
/*    Application Code                                                    */
83
/*                                                                        */
84
/**************************************************************************/
85
55363
UINT  _fx_file_create(FX_MEDIA *media_ptr, CHAR *file_name)
86
{
87
88
FX_INT_SAVE_AREA
89
90
UINT         status;
91
CHAR        *name_ptr;
92
UINT         i;
93
CHAR        *work_ptr;
94
FX_DIR_ENTRY dir_entry;
95
FX_DIR_ENTRY search_directory;
96
97
98
#ifndef FX_MEDIA_STATISTICS_DISABLE
99
100
    /* Increment the number of times this service has been called.  */
101
55363
    media_ptr -> fx_media_file_creates++;
102
#endif
103
104
    /* Determine if the supplied name is less than the maximum supported name size. The
105
       maximum name (FX_MAX_LONG_NAME_LEN) is defined in fx_api.h.  */
106
55363
    i =  0;
107
55363
    work_ptr =  (CHAR *)file_name;
108
737491
    while (*work_ptr)
109
    {
110
111
        /* Determine if the character designates a new path.  */
112

682128
        if ((*work_ptr == '\\') || (*work_ptr == '/'))
113
        {
114
            /* Yes, reset the name size.  */
115
28434
            i =  0;
116
        }
117
        /* Check for leading spaces.  */
118

653694
        else if ((*work_ptr != ' ') || (i != 0))
119
        {
120
121
            /* No leading spaces, increment the name size.  */
122
653670
            i++;
123
        }
124
125
        /* Move to the next character.  */
126
682128
        work_ptr++;
127
    }
128
129
    /* Determine if the supplied name is valid.  */
130

55363
    if ((i == 0) || (i >= FX_MAX_LONG_NAME_LEN))
131
    {
132
133
        /* Return an invalid name value.  */
134
2
        return(FX_INVALID_NAME);
135
    }
136
137
    /* Setup pointer to media name buffer.  */
138
55361
    dir_entry.fx_dir_entry_name =  media_ptr -> fx_media_name_buffer + FX_MAX_LONG_NAME_LEN;
139
140
    /* Setup another pointer to another media name buffer.  */
141
55361
    search_directory.fx_dir_entry_name =  media_ptr -> fx_media_name_buffer + FX_MAX_LONG_NAME_LEN * 2;
142
143
    /* Clear the short name strings.  */
144
55361
    dir_entry.fx_dir_entry_short_name[0] =        0;
145
55361
    search_directory.fx_dir_entry_short_name[0] = 0;
146
147
    /* Check the media to make sure it is open.  */
148
55361
    if (media_ptr -> fx_media_id != FX_MEDIA_ID)
149
    {
150
151
        /* Return the media not opened error.  */
152
1
        return(FX_MEDIA_NOT_OPEN);
153
    }
154
155
    /* If trace is enabled, insert this event into the trace buffer.  */
156
    FX_TRACE_IN_LINE_INSERT(FX_TRACE_FILE_CREATE, media_ptr, file_name, 0, 0, FX_TRACE_FILE_EVENTS, 0, 0)
157
158
    /* Protect against other threads accessing the media.  */
159
55360
    FX_PROTECT
160
161
#ifdef FX_ENABLE_FAULT_TOLERANT
162
    /* Start transaction. */
163
    _fx_fault_tolerant_transaction_start(media_ptr);
164
#endif /* FX_ENABLE_FAULT_TOLERANT */
165
166
    /* Check for write protect at the media level (set by driver).  */
167
55360
    if (media_ptr -> fx_media_driver_write_protect)
168
    {
169
170
#ifdef FX_ENABLE_FAULT_TOLERANT
171
        FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr);
172
#endif /* FX_ENABLE_FAULT_TOLERANT */
173
174
        /* Release media protection.  */
175
2
        FX_UNPROTECT
176
177
        /* Return write protect error.  */
178
2
        return(FX_WRITE_PROTECT);
179
    }
180
181
    /* Search the system for the supplied file name.  */
182
55358
    status =  _fx_directory_search(media_ptr, file_name, &dir_entry, &search_directory, &name_ptr);
183
184
    /* Determine if the search was successful.  */
185
55358
    if (status == FX_SUCCESS)
186
    {
187
188
#ifdef FX_ENABLE_FAULT_TOLERANT
189
        FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr);
190
#endif /* FX_ENABLE_FAULT_TOLERANT */
191
192
        /* Release media protection.  */
193
437
        FX_UNPROTECT
194
195
        /* File found - Return the error code.  */
196
437
        return(FX_ALREADY_CREATED);
197
    }
198
199
    /* Determine if there is anything left after the name.  */
200
54921
    if (_fx_directory_name_extract(name_ptr, &dir_entry.fx_dir_entry_name[0]))
201
    {
202
203
#ifdef FX_ENABLE_FAULT_TOLERANT
204
        FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr);
205
#endif /* FX_ENABLE_FAULT_TOLERANT */
206
207
        /* Release media protection.  */
208
2
        FX_UNPROTECT
209
210
        /* Extra information after the file name, return an invalid path
211
           error.  */
212
2
        return(FX_INVALID_PATH);
213
    }
214
215
216
    /* Find a free slot for the new file.  */
217
54919
    status =  _fx_directory_free_search(media_ptr, &search_directory, &dir_entry);
218
219
    /* Determine if the search was successful.  */
220
54919
    if (status != FX_SUCCESS)
221
    {
222
223
#ifdef FX_ENABLE_FAULT_TOLERANT
224
        FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr);
225
#endif /* FX_ENABLE_FAULT_TOLERANT */
226
227
        /* Release media protection.  */
228
21
        FX_UNPROTECT
229
230
        /* Return the error code.  */
231
21
        return(status);
232
    }
233
234
    /* Populate the directory entry.  */
235
236
    /* Isolate the file name.  */
237
54898
    _fx_directory_name_extract(name_ptr, &dir_entry.fx_dir_entry_name[0]);
238
239
    /* Disable interrupts for time/date access.  */
240
54898
    FX_DISABLE_INTS
241
242
    /* Set time and date stamps.  */
243
54898
    dir_entry.fx_dir_entry_time =  _fx_system_time;
244
54898
    dir_entry.fx_dir_entry_date =  _fx_system_date;
245
246
    /* Restore interrupts.  */
247
54898
    FX_RESTORE_INTS
248
249
    /* Set the attributes for the file.  */
250
54898
    dir_entry.fx_dir_entry_attributes =  FX_ARCHIVE;
251
252
    /* Set file size to 0. */
253
54898
    dir_entry.fx_dir_entry_file_size =  0;
254
255
256
    /* Set the cluster to NULL.  */
257
54898
    dir_entry.fx_dir_entry_cluster =    FX_NULL;
258
259
    /* Is there a leading dot?  */
260
54898
    if (dir_entry.fx_dir_entry_name[0] == '.')
261
    {
262
263
        /* Yes, toggle the hidden attribute bit.  */
264
2
        dir_entry.fx_dir_entry_attributes |=  FX_HIDDEN;
265
    }
266
267
268
    /* Now write out the directory entry.  */
269
54898
    status = _fx_directory_entry_write(media_ptr, &dir_entry);
270
271
#ifdef FX_ENABLE_FAULT_TOLERANT
272
    /* Check for a bad status.  */
273
    if (status != FX_SUCCESS)
274
    {
275
276
        FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr);
277
278
        /* Release media protection.  */
279
        FX_UNPROTECT
280
281
        /* Return the bad status.  */
282
        return(status);
283
    }
284
285
    /* End transaction. */
286
    status = _fx_fault_tolerant_transaction_end(media_ptr);
287
#endif /* FX_ENABLE_FAULT_TOLERANT */
288
289
    /* Release media protection.  */
290
54898
    FX_UNPROTECT
291
292
    /* File create is complete, return status.  */
293
54898
    return(status);
294
}
295