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