GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: fx_file_extended_relative_seek.c Lines: 16 16 100.0 %
Date: 2024-01-10 21:53:23 Branches: 12 12 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
/**   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
34
35
/**************************************************************************/
36
/*                                                                        */
37
/*  FUNCTION                                               RELEASE        */
38
/*                                                                        */
39
/*    _fx_file_extended_relative_seek                     PORTABLE C      */
40
/*                                                           6.1          */
41
/*  AUTHOR                                                                */
42
/*                                                                        */
43
/*    William E. Lamie, Microsoft Corporation                             */
44
/*                                                                        */
45
/*  DESCRIPTION                                                           */
46
/*                                                                        */
47
/*    This function positions the internal file pointers to the specified */
48
/*    byte relative offset such that the next read or write operation is  */
49
/*    performed there.                                                    */
50
/*                                                                        */
51
/*  INPUT                                                                 */
52
/*                                                                        */
53
/*    file_ptr                              File control block pointer    */
54
/*    byte_offset                           Byte offset of the seek       */
55
/*    seek_from                             Direction for relative seek,  */
56
/*                                          legal values are:             */
57
/*                                                                        */
58
/*                                              FX_SEEK_BEGIN             */
59
/*                                              FX_SEEK_END               */
60
/*                                              FX_SEEK_FORWARD           */
61
/*                                              FX_SEEK_BACK              */
62
/*                                                                        */
63
/*  OUTPUT                                                                */
64
/*                                                                        */
65
/*    return status                                                       */
66
/*                                                                        */
67
/*  CALLS                                                                 */
68
/*                                                                        */
69
/*    _fx_file_extended_seek                Seek to specified position    */
70
/*                                                                        */
71
/*  CALLED BY                                                             */
72
/*                                                                        */
73
/*    Application Code                                                    */
74
/*                                                                        */
75
/*  RELEASE HISTORY                                                       */
76
/*                                                                        */
77
/*    DATE              NAME                      DESCRIPTION             */
78
/*                                                                        */
79
/*  05-19-2020     William E. Lamie         Initial Version 6.0           */
80
/*  09-30-2020     William E. Lamie         Modified comment(s),          */
81
/*                                            resulting in version 6.1    */
82
/*                                                                        */
83
/**************************************************************************/
84
108
UINT  _fx_file_extended_relative_seek(FX_FILE *file_ptr, ULONG64 byte_offset, UINT seek_from)
85
{
86
87
#ifndef FX_MEDIA_STATISTICS_DISABLE
88
FX_MEDIA *media_ptr;
89
90
    /* First, determine if the file is still open.  */
91
108
    if (file_ptr -> fx_file_id != FX_FILE_ID)
92
    {
93
94
        /* Return the file not open error status.  */
95
1
        return(FX_NOT_OPEN);
96
    }
97
98
    /* Setup pointer to media structure.  */
99
107
    media_ptr =  file_ptr -> fx_file_media_ptr;
100
101
    /* Increment the number of times this service has been called.  */
102
107
    media_ptr -> fx_media_file_relative_seeks++;
103
#endif
104
105
    /* If trace is enabled, insert this event into the trace buffer.  */
106
    FX_TRACE_IN_LINE_INSERT(FX_TRACE_FILE_RELATIVE_SEEK, file_ptr, byte_offset, seek_from, file_ptr -> fx_file_current_file_offset, FX_TRACE_FILE_EVENTS, 0, 0)
107
108
    /* Determine if seeking from the beginning is requested.  */
109
107
    if (seek_from == FX_SEEK_BEGIN)
110
    {
111
112
        /* Yes, use the base file seek routine to seek from the beginning of
113
           the file.  */
114
2
        return(_fx_file_extended_seek(file_ptr, byte_offset));
115
    }
116
    /* Otherwise, determine if seeking from the end is requested.  */
117
105
    else if (seek_from == FX_SEEK_END)
118
    {
119
120
        /* Yes, seek from the end of the file.  */
121
122
        /* Determine if the requested seek offset is greater than
123
           the file size.  */
124
27
        if (byte_offset >= file_ptr -> fx_file_current_file_size)
125
        {
126
127
            /* Yes, just seek to the beginning of the file.  */
128
1
            return(_fx_file_extended_seek(file_ptr, ((ULONG64) 0)));
129
        }
130
        else
131
        {
132
133
            /* Logically seek from the end of the file.  */
134
26
            return(_fx_file_extended_seek(file_ptr, file_ptr -> fx_file_current_file_size - byte_offset));
135
        }
136
    }
137
    /* Otherwise, determine if seeking from the current position is requested.  */
138
78
    else if (seek_from == FX_SEEK_FORWARD)
139
    {
140
141
        /* Yes, just seek ahead from the current file position.  */
142
51
        return(_fx_file_extended_seek(file_ptr, file_ptr -> fx_file_current_file_offset + byte_offset));
143
    }
144
    /* Otherwise, seeking backward from the current position is assumed.  */
145
    else
146
    {
147
148
        /* Determine if the backward offset is greater than the current file offset.  */
149
27
        if (byte_offset >= file_ptr -> fx_file_current_file_offset)
150
        {
151
152
            /* Yes, just position the file to the beginning.  */
153
1
            return(_fx_file_extended_seek(file_ptr, ((ULONG64) 0)));
154
        }
155
        else
156
        {
157
158
            /* Seek backward relative to the current position.  */
159
26
            return(_fx_file_extended_seek(file_ptr, file_ptr -> fx_file_current_file_offset - byte_offset));
160
        }
161
    }
162
}
163