cmfsize.c revision 272444
169800Stomsoft/******************************************************************************
269800Stomsoft *
369800Stomsoft * Module Name: cfsize - Common get file size function
479754Sdd *
569800Stomsoft *****************************************************************************/
669800Stomsoft
779754Sdd/*
869800Stomsoft * Copyright (C) 2000 - 2014, Intel Corp.
969800Stomsoft * All rights reserved.
1069800Stomsoft *
1169800Stomsoft * Redistribution and use in source and binary forms, with or without
1269800Stomsoft * modification, are permitted provided that the following conditions
1369800Stomsoft * are met:
1469800Stomsoft * 1. Redistributions of source code must retain the above copyright
1569800Stomsoft *    notice, this list of conditions, and the following disclaimer,
1669800Stomsoft *    without modification.
1769800Stomsoft * 2. Redistributions in binary form must reproduce at minimum a disclaimer
1869800Stomsoft *    substantially similar to the "NO WARRANTY" disclaimer below
1969800Stomsoft *    ("Disclaimer") and any redistribution must be conditioned upon
2069800Stomsoft *    including a substantially similar Disclaimer requirement for further
2169800Stomsoft *    binary redistribution.
2269800Stomsoft * 3. Neither the names of the above-listed copyright holders nor the names
2369800Stomsoft *    of any contributors may be used to endorse or promote products derived
2479754Sdd *    from this software without specific prior written permission.
2569800Stomsoft *
2669800Stomsoft * Alternatively, this software may be distributed under the terms of the
2769800Stomsoft * GNU General Public License ("GPL") version 2 as published by the Free
2869800Stomsoft * Software Foundation.
2969800Stomsoft *
3069800Stomsoft * NO WARRANTY
3169800Stomsoft * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3269800Stomsoft * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3369800Stomsoft * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
3469800Stomsoft * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3569800Stomsoft * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3669800Stomsoft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3769926Stomsoft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3869800Stomsoft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
3969800Stomsoft * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
4069800Stomsoft * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41159510Smaxim * POSSIBILITY OF SUCH DAMAGES.
4269977Sru */
4369800Stomsoft
4469800Stomsoft#include <contrib/dev/acpica/include/acpi.h>
45102231Strhodes#include <contrib/dev/acpica/include/accommon.h>
4669800Stomsoft#include <contrib/dev/acpica/include/acapps.h>
4769977Sru#include <stdio.h>
4869977Sru
4969800Stomsoft#define _COMPONENT          ACPI_TOOLS
5069800Stomsoft        ACPI_MODULE_NAME    ("cmfsize")
5169800Stomsoft
5269926Stomsoft
5369800Stomsoft/*******************************************************************************
5495122Scharnier *
5595122Scharnier * FUNCTION:    CmGetFileSize
5695122Scharnier *
5769800Stomsoft * PARAMETERS:  File                    - Open file descriptor
5895122Scharnier *
5969800Stomsoft * RETURN:      File Size. On error, -1 (ACPI_UINT32_MAX)
60124853Scperciva *
6169926Stomsoft * DESCRIPTION: Get the size of a file. Uses seek-to-EOF. File must be open.
6269926Stomsoft *              Does not disturb the current file pointer.
63102231Strhodes *
6469926Stomsoft ******************************************************************************/
6569926Stomsoft
6669800StomsoftUINT32
6769977SruCmGetFileSize (
6869977Sru    ACPI_FILE               File)
6969977Sru{
7069977Sru    long                    FileSize;
7169977Sru    long                    CurrentOffset;
7269977Sru    ACPI_STATUS             Status;
7369977Sru
7469800Stomsoft
7569977Sru    /* Save the current file pointer, seek to EOF to obtain file size */
7669977Sru
7769977Sru    CurrentOffset = AcpiOsGetFileOffset (File);
7869926Stomsoft    if (CurrentOffset < 0)
7969926Stomsoft    {
8069800Stomsoft        goto OffsetError;
8169926Stomsoft    }
8269977Sru
8369977Sru    Status = AcpiOsSetFileOffset (File, 0, ACPI_FILE_END);
8469977Sru    if (ACPI_FAILURE (Status))
8569977Sru    {
8669977Sru        goto SeekError;
8769977Sru    }
8869977Sru
8969977Sru    FileSize = AcpiOsGetFileOffset (File);
9069977Sru    if (FileSize < 0)
9169977Sru    {
9269977Sru        goto OffsetError;
9369977Sru    }
9469977Sru
9569977Sru    /* Restore original file pointer */
9669977Sru
9769977Sru    Status = AcpiOsSetFileOffset (File, CurrentOffset, ACPI_FILE_BEGIN);
9869977Sru    if (ACPI_FAILURE (Status))
9969977Sru    {
10069977Sru        goto SeekError;
10169977Sru    }
10269977Sru
10369977Sru    return ((UINT32) FileSize);
10469977Sru
10569977Sru
10669977SruOffsetError:
10769977Sru    AcpiLogError ("Could not get file offset");
10869800Stomsoft    return (ACPI_UINT32_MAX);
109144793Sceri
110144361SrwatsonSeekError:
11197476Sru    AcpiLogError ("Could not set file offset");
11297476Sru    return (ACPI_UINT32_MAX);
11397476Sru}
11469800Stomsoft