os/ossrv/genericopenlibs/openenvcore/libc/src/link.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/openenvcore/libc/src/link.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,210 @@
     1.4 +/*
     1.5 +* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description:  Implementation of link/symlink.
    1.18 +*
    1.19 +*/
    1.20 +
    1.21 +
    1.22 +// INCLUDE FILES
    1.23 +
    1.24 +#include <sys/errno.h>
    1.25 +#include <sys/types.h>
    1.26 +#include <wchar.h>
    1.27 +#include <string.h>
    1.28 +#include <unistd.h>
    1.29 +#include "lposix.h"
    1.30 +#include <sys/stat.h>
    1.31 +#include <sys/limits.h>
    1.32 +#include "sysreent.h"
    1.33 +#include <stdlib.h>
    1.34 +#include "sysif.h"
    1.35 +#include "systemspecialfilercg.h"
    1.36 +#include "link.h"
    1.37 +
    1.38 +// -----------------------------------------------------------------------------
    1.39 +// Funcation name: link
    1.40 +// Description: Provides link functionality.
    1.41 +// Returns:  0    : On success
    1.42 +//          -1    : On error
    1.43 +// In case of error, errno value set
    1.44 +// Remark: This is a simulated functionality and not supported by the platform     
    1.45 +// -----------------------------------------------------------------------------
    1.46 +//
    1.47 +
    1.48 +extern "C" {
    1.49 +
    1.50 +#define	MAXPATHLEN	260	/* E32STD.H: KMaxFullName + 4 to avoid data loss */
    1.51 +
    1.52 +EXPORT_C int link(const char *oldpath, const char *newpath)
    1.53 +	{
    1.54 +    wchar_t _oldwidename[MAXPATHLEN+1];
    1.55 +    wchar_t _newwidename[MAXPATHLEN+1];
    1.56 +
    1.57 +	if ((size_t)-1 != mbstowcs(_oldwidename, oldpath, MAXPATHLEN) && \
    1.58 +	    (size_t)-1 != mbstowcs(_newwidename, newpath, MAXPATHLEN)) 
    1.59 +		{
    1.60 +	    return _link_r(&errno, _oldwidename, _newwidename);		
    1.61 +		}
    1.62 +	errno = EILSEQ;
    1.63 +	return -1;
    1.64 +	}
    1.65 +
    1.66 +// -----------------------------------------------------------------------------
    1.67 +// Funcation name: symlink
    1.68 +// Description: Provides symlink functionality which is exactly similar to link
    1.69 +// in our case.
    1.70 +// Returns: ssize_t : On success
    1.71 +//          -1      : On error
    1.72 +// In case of error, errno value set
    1.73 +//      
    1.74 +// -----------------------------------------------------------------------------
    1.75 +//
    1.76 +    
    1.77 +EXPORT_C int symlink(const char *oldpath, const char *newpath)
    1.78 +	{
    1.79 +    wchar_t _oldwidename[MAXPATHLEN+1];
    1.80 +    wchar_t _newwidename[MAXPATHLEN+1];
    1.81 +
    1.82 +	if ((size_t)-1 != mbstowcs(_oldwidename, oldpath, MAXPATHLEN) && \
    1.83 +	    (size_t)-1 != mbstowcs(_newwidename, newpath, MAXPATHLEN)) 
    1.84 +		{
    1.85 +	    return _link_r(&errno, _oldwidename, _newwidename);		
    1.86 +		}            
    1.87 +	errno = EILSEQ;
    1.88 +	return -1;	
    1.89 +	}
    1.90 +
    1.91 +// -----------------------------------------------------------------------------
    1.92 +// Funcation name: readlink
    1.93 +// Description: Reads the link file.
    1.94 +// Returns: ssize_t : On success
    1.95 +//          -1      : On error
    1.96 +// In case of error, errno value set
    1.97 +//      
    1.98 +// -----------------------------------------------------------------------------
    1.99 +//
   1.100 +
   1.101 +EXPORT_C ssize_t readlink(const char *path, char *buf, int bufsize)
   1.102 +{
   1.103 +	TSpecialFileType fileType;
   1.104 +	struct SLinkInfo enBuf;
   1.105 +	int err = KErrNone;
   1.106 +	char name[MAXPATHLEN+1];
   1.107 +	
   1.108 +	wchar_t widename[MAXPATHLEN+1];
   1.109 +
   1.110 +	if(path)
   1.111 +		{	
   1.112 +		if ((size_t)-1 != mbstowcs(widename, path, MAXPATHLEN)) 
   1.113 +			{	
   1.114 +			/*Special file identification and read of IPC is utilized here*/
   1.115 +			if( (fileType = _SystemSpecialFileBasedFilePath(widename, err, Backend()->FileSession())) == EFileTypeSymLink)
   1.116 +				{
   1.117 +				if (err)
   1.118 +				    {
   1.119 +			    	return MapError(err, errno);
   1.120 +					}
   1.121 +				err = _ReadSysSplFile(widename, (char*)&enBuf, sizeof(struct SLinkInfo), errno, Backend()->FileSession());
   1.122 +				if (err == KErrNone)
   1.123 +					{
   1.124 +					if((0 < bufsize && bufsize <= SSIZE_MAX) && NULL != buf )	
   1.125 +						{
   1.126 +						if ((size_t)-1 != wcstombs(name, enBuf.iParentPath, sizeof(enBuf.iParentPath)))
   1.127 +							{
   1.128 +							strncpy(buf, name, bufsize);
   1.129 +							return (strlen(name) >= bufsize ? bufsize : strlen(buf));
   1.130 +							}
   1.131 +						}
   1.132 +					else
   1.133 +						{
   1.134 +						errno = EINVAL;
   1.135 +						return -1;	
   1.136 +						}
   1.137 +					}
   1.138 +				}
   1.139 +			else if(err || (EFileNotFound == fileType))
   1.140 +				{
   1.141 +				return MapError(err, errno);
   1.142 +				}				
   1.143 +			errno = EINVAL;
   1.144 +			return -1;		
   1.145 +			}
   1.146 +		return MapError(EILSEQ, errno);	
   1.147 +		}
   1.148 +	return MapError(EINVAL, errno);	
   1.149 +	}
   1.150 +
   1.151 +// -----------------------------------------------------------------------------
   1.152 +// Funcation name: unlink
   1.153 +// Description: Provides unlink functionality.
   1.154 +// Returns:  0    : On success
   1.155 +//          -1    : On error
   1.156 +// In case of error, errno value set
   1.157 +//      
   1.158 +// -----------------------------------------------------------------------------
   1.159 +//
   1.160 +
   1.161 +EXPORT_C int unlink(const char *pathname)
   1.162 +{
   1.163 +	wchar_t _pathwidename[MAXPATHLEN+1];    
   1.164 +
   1.165 +	if ((size_t)-1 != mbstowcs(_pathwidename, pathname, MAXPATHLEN)) 
   1.166 +	{
   1.167 +	    return _unlink_r(&errno, _pathwidename);		
   1.168 +	}            
   1.169 +	errno  = EILSEQ;
   1.170 +	return -1;	      
   1.171 +}
   1.172 +
   1.173 +// -----------------------------------------------------------------------------
   1.174 +// Funcation name: lstat, __xstat, __lxstat
   1.175 +// Description: All these functions directly call stat by themself .
   1.176 +// Returns:  0      : On success
   1.177 +//          -1      : On error
   1.178 +// In case of error, errno value set
   1.179 +//      
   1.180 +// -----------------------------------------------------------------------------
   1.181 +//
   1.182 +
   1.183 +EXPORT_C int lstat(const char *file_name, struct stat *buf)
   1.184 +	{
   1.185 +    if(!buf)
   1.186 +       {
   1.187 +        errno = EFAULT ;
   1.188 +        return -1 ; 
   1.189 +       }
   1.190 +    
   1.191 +    wchar_t tmpbuf[MAXPATHLEN+1];   
   1.192 +    if ((size_t)-1 != mbstowcs(tmpbuf, file_name, MAXPATHLEN))
   1.193 +        {
   1.194 +        return _lstat_r(&errno, tmpbuf, buf);
   1.195 +        }
   1.196 +    errno = EILSEQ;     
   1.197 +    return -1;    
   1.198 +	}
   1.199 +EXPORT_C int __xstat(int /*vers*/, const char *file, struct stat *buf)
   1.200 +	{
   1.201 +	return stat(file, buf);
   1.202 +	}
   1.203 +
   1.204 +EXPORT_C int __lxstat (int /*version*/, const char *file, struct stat *buf)
   1.205 +	{
   1.206 +	return lstat(file, buf);
   1.207 +	}
   1.208 +
   1.209 +    
   1.210 +} // extern "C"
   1.211 +
   1.212 +
   1.213 +//  End of File