os/ossrv/genericopenlibs/openenvcore/libc/src/link.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description:  Implementation of link/symlink.
    15 *
    16 */
    17 
    18 
    19 // INCLUDE FILES
    20 
    21 #include <sys/errno.h>
    22 #include <sys/types.h>
    23 #include <wchar.h>
    24 #include <string.h>
    25 #include <unistd.h>
    26 #include "lposix.h"
    27 #include <sys/stat.h>
    28 #include <sys/limits.h>
    29 #include "sysreent.h"
    30 #include <stdlib.h>
    31 #include "sysif.h"
    32 #include "systemspecialfilercg.h"
    33 #include "link.h"
    34 
    35 // -----------------------------------------------------------------------------
    36 // Funcation name: link
    37 // Description: Provides link functionality.
    38 // Returns:  0    : On success
    39 //          -1    : On error
    40 // In case of error, errno value set
    41 // Remark: This is a simulated functionality and not supported by the platform     
    42 // -----------------------------------------------------------------------------
    43 //
    44 
    45 extern "C" {
    46 
    47 #define	MAXPATHLEN	260	/* E32STD.H: KMaxFullName + 4 to avoid data loss */
    48 
    49 EXPORT_C int link(const char *oldpath, const char *newpath)
    50 	{
    51     wchar_t _oldwidename[MAXPATHLEN+1];
    52     wchar_t _newwidename[MAXPATHLEN+1];
    53 
    54 	if ((size_t)-1 != mbstowcs(_oldwidename, oldpath, MAXPATHLEN) && \
    55 	    (size_t)-1 != mbstowcs(_newwidename, newpath, MAXPATHLEN)) 
    56 		{
    57 	    return _link_r(&errno, _oldwidename, _newwidename);		
    58 		}
    59 	errno = EILSEQ;
    60 	return -1;
    61 	}
    62 
    63 // -----------------------------------------------------------------------------
    64 // Funcation name: symlink
    65 // Description: Provides symlink functionality which is exactly similar to link
    66 // in our case.
    67 // Returns: ssize_t : On success
    68 //          -1      : On error
    69 // In case of error, errno value set
    70 //      
    71 // -----------------------------------------------------------------------------
    72 //
    73     
    74 EXPORT_C int symlink(const char *oldpath, const char *newpath)
    75 	{
    76     wchar_t _oldwidename[MAXPATHLEN+1];
    77     wchar_t _newwidename[MAXPATHLEN+1];
    78 
    79 	if ((size_t)-1 != mbstowcs(_oldwidename, oldpath, MAXPATHLEN) && \
    80 	    (size_t)-1 != mbstowcs(_newwidename, newpath, MAXPATHLEN)) 
    81 		{
    82 	    return _link_r(&errno, _oldwidename, _newwidename);		
    83 		}            
    84 	errno = EILSEQ;
    85 	return -1;	
    86 	}
    87 
    88 // -----------------------------------------------------------------------------
    89 // Funcation name: readlink
    90 // Description: Reads the link file.
    91 // Returns: ssize_t : On success
    92 //          -1      : On error
    93 // In case of error, errno value set
    94 //      
    95 // -----------------------------------------------------------------------------
    96 //
    97 
    98 EXPORT_C ssize_t readlink(const char *path, char *buf, int bufsize)
    99 {
   100 	TSpecialFileType fileType;
   101 	struct SLinkInfo enBuf;
   102 	int err = KErrNone;
   103 	char name[MAXPATHLEN+1];
   104 	
   105 	wchar_t widename[MAXPATHLEN+1];
   106 
   107 	if(path)
   108 		{	
   109 		if ((size_t)-1 != mbstowcs(widename, path, MAXPATHLEN)) 
   110 			{	
   111 			/*Special file identification and read of IPC is utilized here*/
   112 			if( (fileType = _SystemSpecialFileBasedFilePath(widename, err, Backend()->FileSession())) == EFileTypeSymLink)
   113 				{
   114 				if (err)
   115 				    {
   116 			    	return MapError(err, errno);
   117 					}
   118 				err = _ReadSysSplFile(widename, (char*)&enBuf, sizeof(struct SLinkInfo), errno, Backend()->FileSession());
   119 				if (err == KErrNone)
   120 					{
   121 					if((0 < bufsize && bufsize <= SSIZE_MAX) && NULL != buf )	
   122 						{
   123 						if ((size_t)-1 != wcstombs(name, enBuf.iParentPath, sizeof(enBuf.iParentPath)))
   124 							{
   125 							strncpy(buf, name, bufsize);
   126 							return (strlen(name) >= bufsize ? bufsize : strlen(buf));
   127 							}
   128 						}
   129 					else
   130 						{
   131 						errno = EINVAL;
   132 						return -1;	
   133 						}
   134 					}
   135 				}
   136 			else if(err || (EFileNotFound == fileType))
   137 				{
   138 				return MapError(err, errno);
   139 				}				
   140 			errno = EINVAL;
   141 			return -1;		
   142 			}
   143 		return MapError(EILSEQ, errno);	
   144 		}
   145 	return MapError(EINVAL, errno);	
   146 	}
   147 
   148 // -----------------------------------------------------------------------------
   149 // Funcation name: unlink
   150 // Description: Provides unlink functionality.
   151 // Returns:  0    : On success
   152 //          -1    : On error
   153 // In case of error, errno value set
   154 //      
   155 // -----------------------------------------------------------------------------
   156 //
   157 
   158 EXPORT_C int unlink(const char *pathname)
   159 {
   160 	wchar_t _pathwidename[MAXPATHLEN+1];    
   161 
   162 	if ((size_t)-1 != mbstowcs(_pathwidename, pathname, MAXPATHLEN)) 
   163 	{
   164 	    return _unlink_r(&errno, _pathwidename);		
   165 	}            
   166 	errno  = EILSEQ;
   167 	return -1;	      
   168 }
   169 
   170 // -----------------------------------------------------------------------------
   171 // Funcation name: lstat, __xstat, __lxstat
   172 // Description: All these functions directly call stat by themself .
   173 // Returns:  0      : On success
   174 //          -1      : On error
   175 // In case of error, errno value set
   176 //      
   177 // -----------------------------------------------------------------------------
   178 //
   179 
   180 EXPORT_C int lstat(const char *file_name, struct stat *buf)
   181 	{
   182     if(!buf)
   183        {
   184         errno = EFAULT ;
   185         return -1 ; 
   186        }
   187     
   188     wchar_t tmpbuf[MAXPATHLEN+1];   
   189     if ((size_t)-1 != mbstowcs(tmpbuf, file_name, MAXPATHLEN))
   190         {
   191         return _lstat_r(&errno, tmpbuf, buf);
   192         }
   193     errno = EILSEQ;     
   194     return -1;    
   195 	}
   196 EXPORT_C int __xstat(int /*vers*/, const char *file, struct stat *buf)
   197 	{
   198 	return stat(file, buf);
   199 	}
   200 
   201 EXPORT_C int __lxstat (int /*version*/, const char *file, struct stat *buf)
   202 	{
   203 	return lstat(file, buf);
   204 	}
   205 
   206     
   207 } // extern "C"
   208 
   209 
   210 //  End of File