Files
m241Neelima/components/webcli-4.9/target/src/wrn/wm/common/wmFileLib.c
T
2026-07-30 11:19:44 +05:30

872 lines
18 KiB
C

/* wmFileLib.c - ansi file support */
/* adapted from rc_ansifs.c */
/*
* Copyright (c) 2002-2010, 2012 Wind River Systems, Inc.
*
* The right to copy, distribute, modify or otherwise make use
* of this software may be licensed only pursuant to the terms
* of an applicable Wind River license agreement.
*/
/*
modification history
--------------------
01h,12nov12,f_f don't attach fileDir if it's invalid (WIND00387021)
01g,02aug12,f_f correct the wrong file dir from the full path (WIND00367073)
01f,17may12,h_l replace NO_STRING with NULL_STRING (WIND00032212)
01e,07may10,m_z eliminate compile warning
01d,29jan10,m_z VxWorks 64-bit support
(WIND00193649)
01c,24feb07,msa Correct the apigen comments so they match the actual routines
and correct any apigen errors that may occur.
01b,10aug06,ps fix missing arguments to wmosSemTake
01a,17may02,rs updated refgen comments
01 , ,ps imported from rc 3.3 vob
*/
/*
DESCRIPTION:
WindManage ANSI file support library.
INCLUDE FILES:
wm.h
*/
#include "wm.h"
#ifdef __ANSI_FILE_MANAGER_ENABLED__
#ifndef kDIRECTORY_BUFFER_SIZE
#define kDIRECTORY_BUFFER_SIZE 256
#endif
static sbyte * mDefaultPath = NULL;
static OS_SEM_T mFileMutex;
/*-----------------------------------------------------------------------*/
#ifdef __DECOMPRESSION_ENABLED__
LOCAL void wmFileLock
(
void
)
{
wmosSemTake( mFileMutex, WAIT_FOREVER_K );
}
/*-----------------------------------------------------------------------*/
LOCAL void wmFileUnlock
(
void
)
{
wmosSemGive( mFileMutex );
}
#endif /* __DECOMPRESSION_ENABLED__ */
/*-----------------------------------------------------------------------*/
LOCAL void wmFileDetermineFileLength
(
FILE * pFile,
Length * pLength
)
{
long start, end;
/* Is there an easier way of doing this? */
fseek(pFile, 0, SEEK_SET);
start = ftell(pFile);
fseek(pFile, 0, SEEK_END);
end = ftell(pFile);
fseek(pFile, 0, SEEK_SET);
*pLength = (Length)end - (Length)start;
}
/**************************************************************************
*
* wmFileCreateDirDOS - WMB file open.
*
* This routine will.
*
* RETURNS: OK, or ERROR if the file is not found or if there is insufficient
* memory to open the file.
*
*
* NOMANUAL
*/
#if 0
LOCAL WM_DIR_T * wmFileCreateDirDOS
(
sbyte * pFileName
)
{
WM_DIR_T * pDir;
if (! wmNew(pDir))
{
wmError(ERROR_MEMMGR_NO_MEMORY, "wmFileCreateDirDOS: no memory");
return NULL;
}
if (NULL == (pDir->pDirHandle = (DIR_HANDLE *) wmMalloc(sizeof(DIR_HANDLE))))
{
wmFree(pDir)
wmError(ERROR_MEMMGR_NO_MEMORY, "wmFileCreateDirDOS: no memory");
return NULL;
}
return pDir;
}
#endif
/*-----------------------------------------------------------------------*/
LOCAL void * wmFileOpenFile
(
sbyte * pFileName,
sbyte * pPathName,
#ifndef _WRS_CONFIG_LP64
sbyte4 * pLength,
#else
sbyte8 * pLength,
#endif
BOOL compression
)
{
sbyte *pFullPath = NULL;
sbyte *pData = NULL;
sbyte *pReturn = NULL;
FILE *pFile = NULL;
Length pathLen = 0;
Length fullPathLen = 0;
Length bytesRead = 0;
Length fileLen = 0;
#ifdef __DECOMPRESSION_ENABLED__
BOOL isCompressed = FALSE;
sbyte *pDecompressedData;
sbyte4 decompressedLen;
#endif /* __DECOMPRESSION_ENABLED__ */
*pLength = -1; /* to differentiate a null for empty file */
pathLen = STRLEN(pPathName);
fullPathLen = pathLen + STRLEN(pFileName) + 3;
pFullPath = (sbyte*) wmosMalloc(fullPathLen);
if (NULL == pFullPath)
{
DEBUG_MSG_2("error malloc -- %d bytes, for %s\n", fullPathLen, pFileName);
return NULL;
}
STRCPY(pFullPath, pPathName);
/* does the base path have proper path separator? */
if (0 < pathLen)
{
--pathLen;
#ifdef __WIN32_OS__
if ('\\' != pPathName[pathLen])
STRCAT(pFullPath, "\\");
#else
if ('/' != pPathName[pathLen])
STRCAT(pFullPath, "/");
#endif
}
STRCAT(pFullPath, pFileName);
pFile = fopen(pFullPath, "rb");
if (NULL == pFile)
{
DEBUG_MSG_1("error fopen -- %s", pFullPath);
wmosFree(pFullPath);
return NULL;
}
wmFileDetermineFileLength( pFile, &fileLen );
if (0 >= fileLen)
{
*pLength = 0;
wmosFree(pFullPath);
return NULL;
}
pData = (sbyte*) wmosMalloc(fileLen + 1); /* +1 for EOL */
if (NULL == pData)
{
DEBUG_MSG_1("error malloc -- %d bytes, for buffer\n", fileLen);
wmosFree(pFullPath);
return NULL;
}
bytesRead = fread(pData, sizeof(sbyte), fileLen, pFile);
*pLength = fileLen;
pReturn = pData;
pData[fileLen] = 0; /* null terminate string */
#ifdef __DECOMPRESSION_ENABLED__
if (compression)
{
/* If the file is compressed, then use the decompression code.
Otherwise, just return the data as is. */
DECOMP_IsCompressed(pData, &isCompressed);
if ( isCompressed )
{
wmFileLock();
DECOMP_DecompressData(pData, bytesRead,
&pDecompressedData, &decompressedLen);
wmFileUnlock();
wmosFree(pData);
*pLength = decompressedLen;
pReturn = pDecompressedData;
}
}
#endif /* __DECOMPRESSION_ENABLED__ */
wmosFree(pFullPath);
fclose(pFile);
return pReturn;
}
/**************************************************************************
*
* wmFileReadFile - .
*
* This routine will .
*
* RETURNS: N/A.
*
*
* NOMANUAL
*/
void * wmFileReadFile
(
WM_DIR_T * pDir,
#ifndef _WRS_CONFIG_LP64
sbyte4 * pLen,
#else
sbyte8 * pLen,
#endif
sbyte * pId
)
{
sbyte * pBasePath;
if (NULL == pDir)
pBasePath = mDefaultPath;
else
pBasePath = pDir->cwd;
return wmFileOpenFile(pId, pBasePath, pLen, TRUE);
}
/**************************************************************************
*
* wmFileRetrieveFile - File retrieve.
*
* This routine will .
*
* RETURNS: Pointer to .
*
*
* NOMANUAL
*/
void * wmFileRetrieveFile
(
#ifndef _WRS_CONFIG_LP64
sbyte4 * pLen,
#else
sbyte8 * pLen,
#endif
sbyte * pId
)
{
return wmFileOpenFile(pId, mDefaultPath, pLen, TRUE);
}
/**************************************************************************
*
* wmFileReleaseFile - WMB file release.
*
* This routine will .
*
* RETURNS: N/A.
*
*
* NOMANUAL
*/
void wmFileReleaseFile
(
void *pObject
)
{
if (NULL != pObject)
wmosFree(pObject);
}
/**************************************************************************
*
* wmFileRetreiveResource - WMB resource retreive.
*
* This routine will .
*
* RETURNS: N/A.
*
*
* NOMANUAL
*/
void* wmFileRetrieveResource
(
#ifndef _WRS_CONFIG_LP64
sbyte4 * pLen,
#else
sbyte8 * pLen,
#endif
sbyte *pId
)
{
return wmFileOpenFile(pId, mDefaultPath, pLen, FALSE);
}
/**************************************************************************
*
* wmFileReleaseResource - WMB resource release.
*
* This routine will .
*
* RETURNS: N/A.
*
*
* NOMANUAL
*/
void wmFileReleaseResource
(
void *pObject
)
{
wmosFree(pObject);
}
/**************************************************************************
*
* wmFileInit - WMB file Lib init.
*
* This routine will .
*
* RETURNS: N/A.
*
*
* NOMANUAL
*/
STATUS wmFileInit
(
sbyte *pPath
)
{
/* already initialized? */
if (NULL != mDefaultPath)
return ERROR_GENERAL;
mDefaultPath = wmosMalloc (STRLEN(pPath) + 1 );
if ( NULL == mDefaultPath )
return ERROR_MEMMGR_NO_MEMORY;
STRCPY( mDefaultPath, pPath );
return wmosSemCreate(&mFileMutex, 1, 0);
}
#ifdef __ENABLE_SESSION_DIRECTORIES__
LOCAL BOOL wmFileChangePath
(
sbyte * pOldPath,
sbyte * pNewPath
)
{
sbyte *pParent;
/* relative change */
while (('.' == *pNewPath) && ('.' == *(pNewPath + 1)))
{
/* move up old path */
pParent = STRRCHR(pOldPath, PATH_CHAR_K);
pNewPath = STRCHR(pNewPath, PATH_CHAR_K);
/*
if (NULL == pNewPath)
return FALSE;
*/
if (NULL == pParent)
break;
*(++pParent) = 0;
if (NULL == pNewPath)
break;
}
return TRUE;
}
/**************************************************************************
*
* wmFileChdir - WMB chdir.
*
* This routine will .
*
* RETURNS: OK, or ERROR if <pPath> is invlaid. <pPath> can be relative or
* absolute.
*
*
* NOMANUAL
*/
STATUS wmFileChdir
(
WM_DIR_T * pDir,
sbyte * pPath
)
{
STATUS status = OK;
sbyte tempPath[kDIRECTORY_BUFFER_SIZE];
#ifndef _WRS_CONFIG_LP64
ubyte4 length;
#else
ubyte8 length;
#endif
#ifdef __WIN32_OS__
DWORD attributes;
#else
DIR *handle;
#endif
if (NULL == pDir)
return wmError(ERROR_GENERAL_NULL_POINTER, "no dir info");
if (NULL_STRING(pPath))
return wmError(ERROR_GENERAL_INVALID_PATH, "invalid path");
if (kDIRECTORY_BUFFER_SIZE < STRLEN(pPath))
return wmError(ERROR_GENERAL_BUFFER_OVERRUN, "buffer overflow");
STRNCPY(tempPath, pDir->cwd, kDIRECTORY_BUFFER_SIZE);
switch (*pPath)
{
case '.':
/* relative path */
wmFileChangePath(tempPath, pPath);
break;
case PATH_CHAR_K:
/* absolute path */
STRNCPY(tempPath, pPath, kDIRECTORY_BUFFER_SIZE);
break;
default:
#ifdef __WIN32_OS__
/* grrrr... stupid DOS */
if (':' == *(pPath + 1))
STRNCPY(tempPath, pPath, kDIRECTORY_BUFFER_SIZE);
else
#endif
{
length = STRLEN(tempPath);
if (PATH_CHAR_K != tempPath[length - 1])
{
tempPath[length++] = PATH_CHAR_K;
tempPath[length ] = 0;
}
STRNCAT(tempPath, pPath, kDIRECTORY_BUFFER_SIZE - length);
}
break;
}
/* make sure path is valid and have access */
#ifdef __WIN32_OS__
if ( -1 == (attributes = GetFileAttributes(tempPath)))
return ERROR_GENERAL_INVALID_PATH;
if ( 0 == (FILE_ATTRIBUTE_DIRECTORY & attributes))
return ERROR_GENERAL_INVALID_PATH;
#else
if (NULL == (handle = opendir(tempPath)))
return ERROR_GENERAL_INVALID_PATH;
closedir(handle);
#endif
STRNCPY(pDir->cwd, tempPath, kDIRECTORY_BUFFER_SIZE);
return status;
}
/**************************************************************************
*
* wmFileOpenDir - WMB file open. Win32 specific!
*
* This routine will.
*
* RETURNS: OK, or ERROR if the file is not found or if there is insufficient
* memory to open the file.
*
*
* NOMANUAL
*/
#ifdef __WIN32_OS__
STATUS wmFileOpenDir
(
WM_DIR_T * pDir,
sbyte * pFileName
)
{
sbyte * pFullPath;
sbyte4 length;
if (wmDirBad(pDir))
return wmError(ERROR_GENERAL_NULL_POINTER, "wmFileOpenDir: invalid dir struct");
if (NULL == pFileName)
pFileName = "*";
if (NULL == (pFullPath = wmMalloc(kDIRECTORY_BUFFER_SIZE)))
return wmError(ERROR_MEMMGR_NO_MEMORY, "wmFileOpenDir: no memory");
length = STRLEN(pDir->cwd);
STRCPY(pFullPath, pDir->cwd);
if (PATH_CHAR_K != pFullPath[length - 1])
{
pFullPath[length++] = PATH_CHAR_K;
pFullPath[length] = 0;
}
STRNCAT(pFullPath, pFileName, kDIRECTORY_BUFFER_SIZE - length);
/* Find first file in current directory */
if (-1L == (pDir->handle = _findfirst(pFullPath, &pDir->fileInfo)))
{
pDir->found = FALSE;
wmFree(pFullPath);
return wmError(ERROR_GENERAL_FILE_NOT_FOUND, "file not found");
}
wmFree(pFullPath);
return OK;
}
#else /* ! __WIN32_OS__ */
/**************************************************************************
*
* wmFileOpenDir - WMB file open.
*
* This routine will.
*
* RETURNS: OK, or ERROR if the file is not found or if there is insufficient
* memory to open the file.
*
*
* NOMANUAL
*/
STATUS wmFileOpenDir
(
WM_DIR_T * pDir,
sbyte * pFileName
)
{
#ifndef _WRS_CONFIG_LP64
ubyte4 length;
#else
ubyte8 length;
#endif
sbyte * pFullPath = NULL;
sbyte * pTruncate = NULL;
if (wmDirBad(pDir))
return wmError(ERROR_MEMMGR_NO_MEMORY, "wmFileOpenDir: invalid dir struct");
DEBUG_MSG_1("Open directory for filename: %s\n", pFileName);
if (NULL == (pFullPath = wmMalloc(kDIRECTORY_BUFFER_SIZE)))
return wmError(ERROR_MEMMGR_NO_MEMORY, "wmFileOpenDir: no memory");
length = STRLEN(pDir->cwd);
STRCPY(pFullPath, pDir->cwd);
if (PATH_CHAR_K != pFullPath[length - 1])
{
pFullPath[length++] = PATH_CHAR_K;
pFullPath[length] = 0;
}
DEBUG_MSG_1("Current working directory: %s\n", pFullPath);
if (! NULL_STRING(pFileName))
{
DEBUG_MSG_1("Appending filename: %s\n", pFileName);
STRNCAT(pFullPath, pFileName, kDIRECTORY_BUFFER_SIZE - length);
pTruncate = STRRCHR(pFullPath, PATH_CHAR_K);
if (NULL == pTruncate)
pTruncate = STRRCHR(pFullPath, '.');
}
DEBUG_MSG_0("Appended filename\n");
if (NULL != pTruncate)
{
pTruncate++;
DEBUG_MSG_1("Filemask: %s\n", pTruncate);
STRNCPY(pDir->fileMask, pTruncate, sizeof(pDir->fileMask));
*pTruncate = 0;
STRNCPY(pDir->fileDir, pFileName, sizeof(pDir->fileDir));
}
else
{
DEBUG_MSG_0("No filename given\n");
if (NULL_STRING(pFileName))
pDir->fileMask[0] = 0;
else
STRNCPY(pDir->fileMask, pFileName, sizeof(pDir->fileMask));
pDir->fileDir[0] = 0;
}
pDir->nameLen = (sbyte4)STRLEN(pDir->fileMask);
DEBUG_MSG_1("Adjusted directory: %s\n", pFullPath);
DEBUG_MSG_1("Filemask: %s\n", pDir->fileMask);
if (NULL == (pDir->handle = opendir (pFullPath)))
{
wmFree(pFullPath);
return wmError(ERROR_GENERAL_FILE_NOT_FOUND, "file not found");
}
wmFree(pFullPath);
return OK;
}
#endif /* ! __WIN32_OS__ */
/**************************************************************************
*
* wmFileNextDir - Get next file in dir. Win32 specific!
*
* This routine will .
*
* RETURNS: Poitner to next filename, or NULL if ?
*
*
* NOMANUAL
*/
#ifdef __WIN32_OS__
sbyte * wmFileNextDir
(
WM_DIR_T * pDir,
sbyte4 * size,
ubyte4 * flags
)
{
*size = 0;
*flags = 0;
if (NULL == pDir)
return NULL;
/* First file in current directory? */
if (FALSE == pDir->found)
pDir->found = TRUE;
else
{
if (0 > _findnext(pDir->handle, &pDir->fileInfo))
return NULL;
}
if (pDir->fileInfo.attrib & _A_SUBDIR)
*flags |= FS_DIRECTORY_K;
if (pDir->fileInfo.attrib & _A_RDONLY)
*flags |= FS_READ_ONLY_K;
*size = pDir->fileInfo.size;
return pDir->fileInfo.name;
}
#else /* ! __WIN32_OS__ */
/**************************************************************************
*
* wmFileNextDir - Get next file in dir.
*
* This routine will .
*
* RETURNS: Poitner to next filename, or NULL if .
*
*
* NOMANUAL
*/
sbyte * wmFileNextDir
(
WM_DIR_T * pDir,
#ifndef _WRS_CONFIG_LP64
sbyte4 *size,
#else
sbyte8 *size,
#endif
ubyte4 *flags
)
{
sbyte fileSeparator = PATH_CHAR_K;
sbyte * pFullName;
struct dirent * pDirInfo;
sbyte * pName;
struct stat fileStats;
ubyte4 dirlen;
*size = 0;
*flags = 0;
if (wmDirBad(pDir))
return NULL;
if (NULL == (pFullName = wmMalloc(kDIRECTORY_BUFFER_SIZE)))
return NULL;
*pFullName = 0;
if (NULL == (pDirInfo = readdir(pDir->handle)))
{
DEBUG_MSG_0("Readdir failed\n");
wmFree(pFullName);
return NULL;
}
pName = pDirInfo->d_name;
if (0 == STRCMP(pName, ".") || (0 == STRCMP(pName, "..")))
return pName;
/*
if ( (!NULL_STRING(pName)) && (0 != STRNCMP(pName, pDir->fileMask, pDir->nameLen)))
return NULL;
*/
if (!NULL_STRING(pDir->cwd))
STRCPY(pFullName, pDir->cwd);
dirlen = STRLEN(pDir->fileDir);
if (dirlen > 1 && PATH_CHAR_K == pDir->fileDir[dirlen-1])
{
if (!NULL_STRING(pFullName))
STRNCAT(pFullName, &fileSeparator, 1);
if (!NULL_STRING(pDir->fileDir))
STRCAT(pFullName, pDir->fileDir);
}
if (!NULL_STRING(pFullName))
STRNCAT(pFullName, &fileSeparator, 1);
if (!NULL_STRING(pDirInfo->d_name))
STRCAT(pFullName, pDirInfo->d_name);
DEBUG_MSG_1("Stat for: %s\n", pFullName);
/* expand error handling -- get err info */
if (0 != stat(pFullName, &fileStats))
{
DEBUG_MSG_0("Stat failed\n");
wmFree(pFullName);
return NULL;
}
*size = fileStats.st_size;
DEBUG_MSG_1("File size: %d\n", *size);
if (S_ISDIR(fileStats.st_mode))
*flags |= FS_DIRECTORY_K;
wmFree(pFullName);
return pDirInfo->d_name;
}
#endif /* __WIN32_OS__ */
/**************************************************************************
*
* wmFileCloseDir - WMB file close.
*
* This routine will .
*
* RETURNS: N/A.
*
*
* NOMANUAL
*/
void wmFileCloseDir
(
WM_DIR_T * pDir
)
{
CLOSEDIR(pDir->handle);
}
#else /* __ENABLE_SESSION_DIRECTORIES__ */
STATUS wmFileChdir(WM_DIR_T * pDir, sbyte *pPath) {return OK;}
void wmFileCloseDir(WM_DIR_T * pDir){}
STATUS wmFileOpenDir(WM_DIR_T * pDir, sbyte *pFileName) {return OK;}
sbyte* wmFileNextDir(
WM_DIR_T * pDir,
#ifndef _WRS_CONFIG_LP64
sbyte4 *size,
#else
sbyte8 *size,
#endif
ubyte4 *flags
)
{
*size = 0;
*flags = 0;
return NULL;
}
#endif /* __ENABLE_SESSION_DIRECTORIES__ */
#endif /* __ANSI_FILE_MANAGER_ENABLED__ */