GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: fx_file_extended_relative_seek.c Lines: 16 16 100.0 %
Date: 2024-03-11 05:15:45 Branches: 12 12 100.0 %

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