⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 posixfse.cpp

📁 mini http server,可以集成嵌入到程序中,实现简单的web功能
💻 CPP
字号:
/*____________________________________________________________________________*\
 *

 Copyright (c) 1997-2003 John Roy, Holger Zimmermann. All rights reserved.

 These sources, libraries and applications are
 FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
 as long as the following conditions are adhered to.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:

 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer. 

 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in
    the documentation and/or other materials provided with the
    distribution.

 3. The name of the author may not be used to endorse or promote products
    derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL THE AUTHORS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 OF THE POSSIBILITY OF SUCH DAMAGE.

 *____________________________________________________________________________*|
 *
 * $Source: /cvsroot/pi3web/Pi3Web_200/Source/Platform/POSIXFSE.cpp,v $
 * $Date: 2003/05/13 18:42:14 $
 *
 Description:
	File system entity functions for interfaces which use the
	POSIX stat facilties and related. This includes UNIX and Win16 applications.

\*____________________________________________________________________________*/
//$SourceTop:$

#include "OSConfig.h"

#if defined(CONFIG_POSIXISH)

/* --- system includes --- */
#if defined(CONFIG_OS_POSIX)
#	include <sys/stat.h>
#	include <limits.h>
#	include <unistd.h>

#elif defined(CONFIG_OS_WIN16)
#	include <dirent.h>
	/*
	** define mode macros
	*/
#	define S_ISREG(m)  (((m) & S_IFMT) == S_IFREG)
#	define S_ISDIR(m)  (((m) & S_IFMT) == S_IFDIR)
#	define S_ISCHR(m)  (((m) & S_IFMT) == S_IFCHR)
#	define S_ISBLK(m)  (((m) & S_IFMT) == S_IFBLK)
#	define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
#	define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)

#else
#	error Unsupported Operating system
#endif

/* --- C library includes --- */
#include <assert.h>

/* --- local includes --- */
#include "POSIXFSE.h"
#include "PICompat.h"
#include "Platform.h"
#include "PlatDefs.h"

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
	Copy constructor, make a copy of the internal data elements of the
	other POSIXFileSystemEntity, including the stat structure. 
\*____________________________________________________________________________*/
POSIXFileSystemEntity::POSIXFileSystemEntity(
		const POSIXFileSystemEntity &tFSE )
:	FileSystemEntity( tFSE ),
	bPopulated( tFSE.bPopulated ),
	pParentDir( 0 ), pDirEntMe( 0 )
{
	if ( bPopulated )
		{ memcpy( &tData, &(tFSE.tData), sizeof( struct stat ) ); };
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:	private constructor
 Description:
\*____________________________________________________________________________*/
POSIXFileSystemEntity::POSIXFileSystemEntity(
	const FileSysType &tTheFSType,
	const PIString &sThePath,
	DIR *pParent,
	struct dirent *pMe )
:	FileSystemEntity( sThePath, tTheFSType ),
	bPopulated( false ),
	pParentDir( pParent ), pDirEntMe( pMe )
{
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
POSIXFileSystemEntity::POSIXFileSystemEntity(
	const FileSysType &tTheFSType, 
	const PIString &sFullPath )
:	FileSystemEntity( tTheFSType, sFullPath ),
	bPopulated( false ),
	pParentDir( 0 ), pDirEntMe( 0 )
{
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
POSIXFileSystemEntity::~POSIXFileSystemEntity()
{
	if ( pParentDir )
		{
		if ( ::closedir( pParentDir ) == -1 )
			{ PIOSERR; };
		};
#if defined(CONFIG_MULTITHREADED) && !defined(CONFIG_MT_USER)
	PI_DELETE( [] pDirEntMe );
#endif
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
	Here the system call 'stat()' is used to fill the stat structure with	
	information on the file. It may be necessary to used an alternative
	system call such as 'lstat' to handle links differently.
\*____________________________________________________________________________*/
bool POSIXFileSystemEntity::Populate()
{
	if ( !bPopulated )
		{ bPopulated=(stat( GetPath(), &tData )?false:true); };
	if ( !bPopulated )
		{ PIOSERR; };
	return bPopulated;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
bool POSIXFileSystemEntity::Exists()
{
	if ( !bPopulated && !Populate() ) return false;
	return true;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
bool POSIXFileSystemEntity::IsRegularFile()
{
	if ( !bPopulated && !Populate() ) return false;
	return S_ISREG( tData.st_mode );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
bool POSIXFileSystemEntity::IsDirectory()
{
	if ( !bPopulated && !Populate() ) return false;
	return S_ISDIR( tData.st_mode );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
bool POSIXFileSystemEntity::IsLink()
{
	if ( !bPopulated && !Populate() ) return false;
#if defined(CONFIG_OS_POSIX)
	return S_ISLNK( tData.st_mode );
#else
	return false;
#endif
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
	This function should be optimised to reexamine cache the group id
	or user id or maybe using an operating system call to verify readibility

	Q: is it better just to use the access() function here?
\*____________________________________________________________________________*/
bool POSIXFileSystemEntity::IsReadable()
{
	if ( !bPopulated && !Populate() ) { return false; };
#if defined(CONFIG_OS_POSIX)
	mode_t tMode=tData.st_mode;
	return
	(tMode & S_IROTH) 	||								/* read by others */
	((tMode & S_IRGRP) && (tData.st_gid==getgid())) ||	/* read by group */
	((tMode & S_IRUSR) && (tData.st_uid==getuid()));	/* read by owner */
#else
	return ( tData.st_mode & S_IREAD );
#endif
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
	This function should be optimised to reexamine cache the group id
	or user id or maybe using an operating system call to verify readibility

	Q: is it better just to use the access() function here?
\*____________________________________________________________________________*/
bool POSIXFileSystemEntity::IsWritable()
{
	/* this simple system call 'access' could/should? be used here
	** but that stat()'s the file again, this uses the cached stat
	** structure.
	*/
	if ( !bPopulated && !Populate() )
		{ return false; };
#if defined(CONFIG_OS_POSIX)
	mode_t tMode=tData.st_mode;
	return
	(tMode & S_IWOTH) 	||								/* write by others */
	((tMode & S_IWGRP) && (tData.st_gid==getgid())) ||	/* write by group */
	((tMode & S_IWUSR) && (tData.st_uid==getuid()));	/* write by owner */
#else
	return ( tData.st_mode & S_IWRITE );
#endif
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
	This function should be optimised to reexamine cache the group id
	or user id or maybe using an operating system call to verify readibility

	Q: is it better just to use the access() function here?
\*____________________________________________________________________________*/
bool POSIXFileSystemEntity::IsExecutable()
{
	/* this simple system call 'access' could/should? be used here
	** but that stat()'s the file again, this uses the cached stat
	** structure
	*/
	if ( !bPopulated && !Populate() )
		{ return false; };
#if defined(CONFIG_OS_POSIX)
	mode_t tMode=tData.st_mode;
	return
	(tMode & S_IXOTH) 	||								/* execute by others */
 	((tMode & S_IXGRP) && (tData.st_gid==getgid())) ||	/* execute by group */
 	((tMode & S_IXUSR) && (tData.st_uid==getuid()));	/* execute by owner */
#else
	return ( tData.st_mode & S_IEXEC );
#endif
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
unsigned long POSIXFileSystemEntity::GetFileSize()
{
	if ( !bPopulated && !Populate() ) return false;
	return tData.st_size;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
time_t POSIXFileSystemEntity::GetLastModified()
{
	if ( !bPopulated && !Populate() ) return false;
	return tData.st_mtime;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
	Returns the first entry in the directory with this entities path.
	The new file will have its pDir and pDirRes methods set appropriately
	so that calls to NextSiblingFile() can be used to traverse all
	files in the directory.
\*____________________________________________________________________________*/
FileSystemEntity *POSIXFileSystemEntity::FirstFileInDirectory( int /*iFlags*/ )
{
	DIR *pDir=opendir( GetPath() );
	if ( !pDir ) { PIOSERR; return 0; };
	struct dirent *pDirRes=0;
#if (defined(CONFIG_MULTITHREADED) && !defined(CONFIG_MT_USER)) && !defined(CONFIG_OS_LINUX)
	pDirRes=(struct dirent *)
		new char[sizeof(struct dirent)+_POSIX_PATH_MAX]; 
	if ( !pDirRes ) { PIERROR( PIAPI_EXHAUSTED ); return 0; };
#else
	pDirRes=0;
#endif
	POSIXFileSystemEntity *pNew=new POSIXFileSystemEntity(
		GetFSType(), GetPath(), pDir, pDirRes);
	if ( !pNew ) { PIERROR( PIAPI_EXHAUSTED ); return 0; };
	if ( !pNew->NextSiblingFile() )
		{
		PI_DELETE( pNew );
		return 0;
		}
	else
		{
		return pNew;
		};
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
bool POSIXFileSystemEntity::NextSiblingFile()
{
#if (defined(CONFIG_MULTITHREADED) && !defined(CONFIG_MT_USER)) && !defined(CONFIG_OS_LINUX)
#	if defined(CONFIG_OS_LINUX)
	/*
	** Note, is this right? - adapted for linux
	*/
	if ( !(readdir_r( pParentDir, pDirEntMe, &pDirEntMe ) ) )
#	else
	pDirEntMe = readdir_r( pParentDir, pDirEntMe );
	if ( !pDirEntMe )
#	endif
#else
	pDirEntMe = readdir( pParentDir );
	if ( !pDirEntMe  )
#endif
		{
		PIOSERR;
		return false;
		}

	/* --- pDirEntMe contains a new filename is the same directory --- */
	NewFileInSamePathRoot( pDirEntMe->d_name );
	return true;
}

#endif	/* #if defined(CONFIG_POSIXISH) */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -