os/ossrv/genericopenlibs/cstdlib/USTLIB/POSIXFS.CPP
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/USTLIB/POSIXFS.CPP	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,211 @@
     1.4 +// Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// PosixFilesystem class
    1.18 +// 
    1.19 +//
    1.20 +
    1.21 +#include "SYSIF.H"
    1.22 +#include "FDESC.H"
    1.23 +#include "LTIME.H"
    1.24 +#include "LPOSIX.H"
    1.25 +#include <fcntl.h>
    1.26 +#include <sys/errno.h>
    1.27 +
    1.28 +wchar_t * PosixFilesystem::getcwd (RFs& aFs, wchar_t* buf, unsigned long len, int& anErrno)
    1.29 +	{
    1.30 +	TFullName name;
    1.31 +	TInt err = aFs.SessionPath(name);
    1.32 +	if (!err)
    1.33 +		{
    1.34 +		TPtr16 pathdes((TText16 *)buf, len);
    1.35 +		if (pathdes.MaxLength() >= (name.Length() + 1))	//+1 to allow for the null terminator
    1.36 +			{
    1.37 +			pathdes.Copy(name);
    1.38 +			pathdes.ZeroTerminate();
    1.39 +			return buf;
    1.40 +			}
    1.41 +		else
    1.42 +			err = ERANGE;		//out of range
    1.43 +		}
    1.44 +	MapError(err, anErrno);
    1.45 +	return 0;
    1.46 +	}
    1.47 +
    1.48 +int PosixFilesystem::chdir (RFs& aFs, const wchar_t* aPath, int& anErrno)
    1.49 +	{
    1.50 +	TParse name;
    1.51 +	TInt err=GetFullPath(name, (const TText16 *)aPath, aFs, NULL);
    1.52 +	if (!err)
    1.53 +		{
    1.54 +		TPtrC path=name.DriveAndPath();
    1.55 +		TUint att=0;
    1.56 +		if (path.Length()==3)	// Problem in F32 - the root directory has no attributes
    1.57 +			att=KEntryAttDir;
    1.58 +		else
    1.59 +			err=aFs.Att(path, att);
    1.60 +		if (!err)
    1.61 +			if (att&KEntryAttDir)
    1.62 +				err=aFs.SetSessionPath(path);
    1.63 +			else
    1.64 +				err=ENOTDIR; 
    1.65 +		}
    1.66 +	return MapError(err,anErrno);
    1.67 +	}
    1.68 +
    1.69 +int PosixFilesystem::rmdir (RFs& aFs, const wchar_t* aPath, int& anErrno)
    1.70 +	{
    1.71 +	TParse name;
    1.72 +	TInt err=GetFullPath(name,(const TText16 *)aPath,aFs,NULL);
    1.73 +	if (!err)
    1.74 +		{
    1.75 +		TPtrC path=name.DriveAndPath();
    1.76 +		TUint att=0;
    1.77 +		if (path.Length()==3)
    1.78 +			err=EPERM;	// no, you may not remove the root directory
    1.79 +		else
    1.80 +			err=aFs.Att(path, att);
    1.81 +		if (!err)
    1.82 +			if (att&KEntryAttDir)
    1.83 +				{
    1.84 +				err=aFs.RmDir(path);
    1.85 +				if (err==KErrInUse)
    1.86 +					err=EEXIST;	// i.e. directory not empty
    1.87 +				}
    1.88 +			else
    1.89 +				err=ENOTDIR; 
    1.90 +		}
    1.91 +	return MapError(err,anErrno);
    1.92 +	}
    1.93 +
    1.94 +
    1.95 +
    1.96 +int PosixFilesystem::mkdir (RFs& aFs, const wchar_t* aPath, int perms, int& anErrno)
    1.97 +	{
    1.98 +	TParse name;
    1.99 +	TInt err=GetFullPath(name,(const TText16 *)aPath,aFs,NULL);
   1.100 +	if (!err)
   1.101 +		{
   1.102 +		TPtrC path=name.DriveAndPath();
   1.103 +		err=aFs.MkDir(path);
   1.104 +		if (!err)
   1.105 +			{
   1.106 +			if ((perms&S_IWUSR)==0)
   1.107 +				err=aFs.SetAtt(path,KEntryAttReadOnly,0);
   1.108 +			}
   1.109 +		}
   1.110 +	return MapError(err,anErrno);
   1.111 +	}
   1.112 +
   1.113 +int PosixFilesystem::stat (RFs& aFs, const wchar_t* name, struct stat *st, int& anErrno)
   1.114 +	{
   1.115 +	TFullName fullName;
   1.116 +	TInt err=GetFullFile(fullName,(const TText16*)name,aFs);
   1.117 +	if (!err)
   1.118 +		{
   1.119 +		TEntry entry;
   1.120 +		if (fullName.Length()==3)
   1.121 +			{
   1.122 +			entry.iAtt=KEntryAttDir;
   1.123 +			entry.iModified==TTime(0);
   1.124 +			}
   1.125 +		else
   1.126 +			err=aFs.Entry(fullName,entry);
   1.127 +		if (!err)
   1.128 +			{
   1.129 +			st->st_size = entry.iSize;
   1.130 +			st->st_dev = st->st_rdev = (dev_t)TDriveUnit(fullName);
   1.131 +			CFileDesc::MapStat(*st, entry.iModified, entry.iAtt);
   1.132 +			return 0;
   1.133 +			}
   1.134 +		}
   1.135 +	return MapError(err, anErrno);
   1.136 +	}
   1.137 +
   1.138 +int PosixFilesystem::chmod (RFs& aFs, const wchar_t* name, int perms, int& anErrno)
   1.139 +	{
   1.140 +	TFullName fullName;
   1.141 +	TInt err=GetFullFile(fullName,(const TText16*)name,aFs);
   1.142 +	if (!err)
   1.143 +		{
   1.144 +		if ((perms&S_IWUSR)==0)
   1.145 +			err=aFs.SetAtt(fullName,KEntryAttReadOnly,0);
   1.146 +		else
   1.147 +			err=aFs.SetAtt(fullName,0,KEntryAttReadOnly);
   1.148 +		}
   1.149 +	return MapError(err, anErrno);
   1.150 +	}
   1.151 +
   1.152 +int PosixFilesystem::unlink (RFs& aFs, const wchar_t* name, int& anErrno)
   1.153 +	{
   1.154 +	TFullName fullName;
   1.155 +	TInt err=GetFullFile(fullName, (TText16*)name, aFs);
   1.156 +	if (!err)
   1.157 +		{
   1.158 +		TUint att=0;
   1.159 +		err=aFs.Att(fullName, att);
   1.160 +		if (!err)
   1.161 +			if (att&KEntryAttDir)
   1.162 +				err=EPERM; 
   1.163 +			else
   1.164 +				err=aFs.Delete(fullName);
   1.165 +		}
   1.166 +	return MapError(err, anErrno);
   1.167 +	}
   1.168 +
   1.169 +int PosixFilesystem::rename (RFs& aFs, const wchar_t* oldname, const wchar_t* newname, int& anErrno)
   1.170 +	{
   1.171 +	TFileName oldFullName;
   1.172 +	TInt err = GetFullFile(oldFullName,(const TText16 *)oldname,aFs);
   1.173 +	if (!err)
   1.174 +		{
   1.175 +		TFileName newFullName;
   1.176 +		err = GetFullFile(newFullName,(const TText16 *)newname,aFs);
   1.177 +		if (!err)
   1.178 +			{
   1.179 +			// ANSI doesn't require specific handling when newname exists,
   1.180 +			// so we can just use the EPOC32 semantics and insist that
   1.181 +			// newname doesn't currently exist.
   1.182 +			err=aFs.Rename(oldFullName,newFullName);
   1.183 +			}
   1.184 +		}
   1.185 +	return MapError(err, anErrno);
   1.186 +	}
   1.187 +
   1.188 +
   1.189 +TInt PosixFilesystem::ResolvePath (RFs& aFs, TParse& aResult, const wchar_t* path, TDes* aFilename)
   1.190 +	{
   1.191 +	return GetFullPath(aResult,(const TText16*)path,aFs,aFilename);
   1.192 +	}
   1.193 +
   1.194 +
   1.195 +#ifdef __WINS__
   1.196 +TInt PosixFilesystem::SetDefaultDir (RFs& /*aFs*/)
   1.197 +	{
   1.198 +	// NB. don't do this on WINS because the executable is
   1.199 +	// something like w:\epoc32\release\wins\deb\mytest.exe (or just mytest.exe or
   1.200 +	// even ./mytest !!)
   1.201 +	return KErrNone;
   1.202 +	}
   1.203 +#else
   1.204 +TInt PosixFilesystem::SetDefaultDir (RFs& aFs)
   1.205 +	{
   1.206 +	TParse parse;
   1.207 +	parse.Set(RProcess().FileName(), NULL, NULL);
   1.208 +#ifdef __SECURE_DATA__
   1.209 +	return aFs.SetSessionToPrivate(TDriveUnit(parse.Drive()));
   1.210 +#else
   1.211 +	return aFs.SetSessionPath(parse.DriveAndPath());
   1.212 +#endif
   1.213 +	}
   1.214 +#endif