GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: fx_file_extended_relative_seek.c Lines: 16 16 100.0 %
Date: 2026-03-06 18:49:02 Branches: 12 12 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
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
/**************************************************************************/
76
108
UINT  _fx_file_extended_relative_seek(FX_FILE *file_ptr, ULONG64 byte_offset, UINT seek_from)
77
{
78
79
#ifndef FX_MEDIA_STATISTICS_DISABLE
80
FX_MEDIA *media_ptr;
81
82
    /* First, determine if the file is still open.  */
83
108
    if (file_ptr -> fx_file_id != FX_FILE_ID)
84
    {
85
86
        /* Return the file not open error status.  */
87
1
        return(FX_NOT_OPEN);
88
    }
89
90
    /* Setup pointer to media structure.  */
91
107
    media_ptr =  file_ptr -> fx_file_media_ptr;
92
93
    /* Increment the number of times this service has been called.  */
94
107
    media_ptr -> fx_media_file_relative_seeks++;
95
#endif
96
97
    /* If trace is enabled, insert this event into the trace buffer.  */
98
    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)
99
100
    /* Determine if seeking from the beginning is requested.  */
101
107
    if (seek_from == FX_SEEK_BEGIN)
102
    {
103
104
        /* Yes, use the base file seek routine to seek from the beginning of
105
           the file.  */
106
2
        return(_fx_file_extended_seek(file_ptr, byte_offset));
107
    }
108
    /* Otherwise, determine if seeking from the end is requested.  */
109
105
    else if (seek_from == FX_SEEK_END)
110
    {
111
112
        /* Yes, seek from the end of the file.  */
113
114
        /* Determine if the requested seek offset is greater than
115
           the file size.  */
116
27
        if (byte_offset >= file_ptr -> fx_file_current_file_size)
117
        {
118
119
            /* Yes, just seek to the beginning of the file.  */
120
1
            return(_fx_file_extended_seek(file_ptr, ((ULONG64) 0)));
121
        }
122
        else
123
        {
124
125
            /* Logically seek from the end of the file.  */
126
26
            return(_fx_file_extended_seek(file_ptr, file_ptr -> fx_file_current_file_size - byte_offset));
127
        }
128
    }
129
    /* Otherwise, determine if seeking from the current position is requested.  */
130
78
    else if (seek_from == FX_SEEK_FORWARD)
131
    {
132
133
        /* Yes, just seek ahead from the current file position.  */
134
51
        return(_fx_file_extended_seek(file_ptr, file_ptr -> fx_file_current_file_offset + byte_offset));
135
    }
136
    /* Otherwise, seeking backward from the current position is assumed.  */
137
    else
138
    {
139
140
        /* Determine if the backward offset is greater than the current file offset.  */
141
27
        if (byte_offset >= file_ptr -> fx_file_current_file_offset)
142
        {
143
144
            /* Yes, just position the file to the beginning.  */
145
1
            return(_fx_file_extended_seek(file_ptr, ((ULONG64) 0)));
146
        }
147
        else
148
        {
149
150
            /* Seek backward relative to the current position.  */
151
26
            return(_fx_file_extended_seek(file_ptr, file_ptr -> fx_file_current_file_offset - byte_offset));
152
        }
153
    }
154
}
155