GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: fx_file_attributes_set.c Lines: 31 31 100.0 %
Date: 2026-03-06 18:49:02 Branches: 14 14 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_attributes_set                             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 attribute set request is valid and the directory entry will be  */
52
/*    modified with the new attributes.  Otherwise, if the file is not    */
53
/*    found, the appropriate error code is returned to the caller.        */
54
/*                                                                        */
55
/*  INPUT                                                                 */
56
/*                                                                        */
57
/*    media_ptr                             Media control block pointer   */
58
/*    file_name                             File name pointer             */
59
/*    attributes                            New file attributes           */
60
/*                                                                        */
61
/*  OUTPUT                                                                */
62
/*                                                                        */
63
/*    return status                                                       */
64
/*                                                                        */
65
/*  CALLS                                                                 */
66
/*                                                                        */
67
/*    _fx_directory_entry_write             Write the new directory entry */
68
/*    _fx_directory_search                  Search for the file name in   */
69
/*                                          the directory structure       */
70
/*    _fx_fault_tolerant_transaction_start  Start fault tolerant          */
71
/*                                            transaction                 */
72
/*    _fx_fault_tolerant_transaction_end    End fault tolerant transaction*/
73
/*    _fx_fault_tolerant_recover            Recover FAT chain             */
74
/*    _fx_fault_tolerant_reset_log_file     Reset the log file            */
75
/*                                                                        */
76
/*  CALLED BY                                                             */
77
/*                                                                        */
78
/*    Application Code                                                    */
79
/*                                                                        */
80
/**************************************************************************/
81
11
UINT  _fx_file_attributes_set(FX_MEDIA *media_ptr, CHAR *file_name, UINT attributes)
82
{
83
84
UINT         status;
85
ULONG        open_count;
86
FX_FILE     *search_ptr;
87
FX_DIR_ENTRY dir_entry;
88
UCHAR        not_a_file_attr;
89
90
91
#ifndef FX_MEDIA_STATISTICS_DISABLE
92
93
    /* Increment the number of times this service has been called.  */
94
11
    media_ptr -> fx_media_file_attributes_sets++;
95
#endif
96
97
    /* Setup pointer to media name buffer.  */
98
11
    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
11
    dir_entry.fx_dir_entry_short_name[0] =  0;
102
103
    /* Check the media to make sure it is open.  */
104
11
    if (media_ptr -> fx_media_id != FX_MEDIA_ID)
105
    {
106
107
        /* Return the media not opened error.  */
108
1
        return(FX_MEDIA_NOT_OPEN);
109
    }
110
10
    not_a_file_attr = FX_DIRECTORY | FX_VOLUME;
111
112
    /* If trace is enabled, insert this event into the trace buffer.  */
113
    FX_TRACE_IN_LINE_INSERT(FX_TRACE_FILE_ATTRIBUTES_SET, media_ptr, file_name, attributes, 0, FX_TRACE_FILE_EVENTS, 0, 0)
114
115
    /* Protect against other threads accessing the media.  */
116
10
    FX_PROTECT
117
118
    /* Check for write protect at the media level (set by driver).  */
119
10
    if (media_ptr -> fx_media_driver_write_protect)
120
    {
121
122
        /* Release media protection.  */
123
1
        FX_UNPROTECT
124
125
        /* Return write protect error.  */
126
1
        return(FX_WRITE_PROTECT);
127
    }
128
129
    /* Search the system for the supplied file name.  */
130
9
    status =  _fx_directory_search(media_ptr, file_name, &dir_entry, FX_NULL, FX_NULL);
131
132
    /* Determine if the search was successful.  */
133
9
    if (status != FX_SUCCESS)
134
    {
135
136
        /* Release media protection.  */
137
2
        FX_UNPROTECT
138
139
        /* Return the error code.  */
140
2
        return(status);
141
    }
142
143
    /* Check to make sure the found entry is a file.  */
144
7
    if (dir_entry.fx_dir_entry_attributes & not_a_file_attr)
145
    {
146
147
        /* Release media protection.  */
148
1
        FX_UNPROTECT
149
150
        /* Return the not a file error code.  */
151
1
        return(FX_NOT_A_FILE);
152
    }
153
154
    /* Search the opened files to see if this file is currently
155
       opened.  */
156
6
    open_count =  media_ptr -> fx_media_opened_file_count;
157
6
    search_ptr =  media_ptr -> fx_media_opened_file_list;
158
18
    while (open_count)
159
    {
160
161
        /* Look at each opened file to see if the same file is opened.  */
162
13
        if ((search_ptr -> fx_file_dir_entry.fx_dir_entry_log_sector == dir_entry.fx_dir_entry_log_sector) &&
163
6
            (search_ptr -> fx_file_dir_entry.fx_dir_entry_byte_offset == dir_entry.fx_dir_entry_byte_offset))
164
        {
165
166
            /* Release media protection.  */
167
1
            FX_UNPROTECT
168
169
            /* The file is currently open.  */
170
1
            return(FX_ACCESS_ERROR);
171
        }
172
173
        /* Adjust the pointer and decrement the search count.  */
174
12
        search_ptr =  search_ptr -> fx_file_opened_next;
175
12
        open_count--;
176
    }
177
178
    /* Place the new attributes in the directory entry.  */
179
5
    dir_entry.fx_dir_entry_attributes =  (UCHAR)attributes;
180
181
#ifdef FX_ENABLE_FAULT_TOLERANT
182
    /* Start transaction. */
183
    _fx_fault_tolerant_transaction_start(media_ptr);
184
#endif /* FX_ENABLE_FAULT_TOLERANT */
185
186
    /* Now write out the directory entry.  */
187
5
    status = _fx_directory_entry_write(media_ptr, &dir_entry);
188
189
#ifdef FX_ENABLE_FAULT_TOLERANT
190
    /* Check for a bad status.  */
191
    if (status != FX_SUCCESS)
192
    {
193
194
        FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr);
195
196
        /* Release media protection.  */
197
        FX_UNPROTECT
198
199
        /* Return the bad status.  */
200
        return(status);
201
    }
202
203
    /* End transaction. */
204
    status = _fx_fault_tolerant_transaction_end(media_ptr);
205
#endif /* FX_ENABLE_FAULT_TOLERANT */
206
207
    /* Release media protection.  */
208
5
    FX_UNPROTECT
209
210
    /* File attribute set is complete, return status.  */
211
5
    return(status);
212
}
213