genericopenlibs/cstdlib/USTLIB/POSIXFS.CPP
changeset 31 ce057bb09d0b
parent 0 e4d67989cc36
equal deleted inserted replaced
30:e20de85af2ee 31:ce057bb09d0b
       
     1 // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // PosixFilesystem class
       
    15 // 
       
    16 //
       
    17 
       
    18 #include "SYSIF.H"
       
    19 #include "FDESC.H"
       
    20 #include "LTIME.H"
       
    21 #include "LPOSIX.H"
       
    22 #include <fcntl.h>
       
    23 #include <sys/errno.h>
       
    24 
       
    25 wchar_t * PosixFilesystem::getcwd (RFs& aFs, wchar_t* buf, unsigned long len, int& anErrno)
       
    26 	{
       
    27 	TFullName name;
       
    28 	TInt err = aFs.SessionPath(name);
       
    29 	if (!err)
       
    30 		{
       
    31 		TPtr16 pathdes((TText16 *)buf, len);
       
    32 		if (pathdes.MaxLength() >= (name.Length() + 1))	//+1 to allow for the null terminator
       
    33 			{
       
    34 			pathdes.Copy(name);
       
    35 			pathdes.ZeroTerminate();
       
    36 			return buf;
       
    37 			}
       
    38 		else
       
    39 			err = ERANGE;		//out of range
       
    40 		}
       
    41 	MapError(err, anErrno);
       
    42 	return 0;
       
    43 	}
       
    44 
       
    45 int PosixFilesystem::chdir (RFs& aFs, const wchar_t* aPath, int& anErrno)
       
    46 	{
       
    47 	TParse name;
       
    48 	TInt err=GetFullPath(name, (const TText16 *)aPath, aFs, NULL);
       
    49 	if (!err)
       
    50 		{
       
    51 		TPtrC path=name.DriveAndPath();
       
    52 		TUint att=0;
       
    53 		if (path.Length()==3)	// Problem in F32 - the root directory has no attributes
       
    54 			att=KEntryAttDir;
       
    55 		else
       
    56 			err=aFs.Att(path, att);
       
    57 		if (!err)
       
    58 			if (att&KEntryAttDir)
       
    59 				err=aFs.SetSessionPath(path);
       
    60 			else
       
    61 				err=ENOTDIR; 
       
    62 		}
       
    63 	return MapError(err,anErrno);
       
    64 	}
       
    65 
       
    66 int PosixFilesystem::rmdir (RFs& aFs, const wchar_t* aPath, int& anErrno)
       
    67 	{
       
    68 	TParse name;
       
    69 	TInt err=GetFullPath(name,(const TText16 *)aPath,aFs,NULL);
       
    70 	if (!err)
       
    71 		{
       
    72 		TPtrC path=name.DriveAndPath();
       
    73 		TUint att=0;
       
    74 		if (path.Length()==3)
       
    75 			err=EPERM;	// no, you may not remove the root directory
       
    76 		else
       
    77 			err=aFs.Att(path, att);
       
    78 		if (!err)
       
    79 			if (att&KEntryAttDir)
       
    80 				{
       
    81 				err=aFs.RmDir(path);
       
    82 				if (err==KErrInUse)
       
    83 					err=EEXIST;	// i.e. directory not empty
       
    84 				}
       
    85 			else
       
    86 				err=ENOTDIR; 
       
    87 		}
       
    88 	return MapError(err,anErrno);
       
    89 	}
       
    90 
       
    91 
       
    92 
       
    93 int PosixFilesystem::mkdir (RFs& aFs, const wchar_t* aPath, int perms, int& anErrno)
       
    94 	{
       
    95 	TParse name;
       
    96 	TInt err=GetFullPath(name,(const TText16 *)aPath,aFs,NULL);
       
    97 	if (!err)
       
    98 		{
       
    99 		TPtrC path=name.DriveAndPath();
       
   100 		err=aFs.MkDir(path);
       
   101 		if (!err)
       
   102 			{
       
   103 			if ((perms&S_IWUSR)==0)
       
   104 				err=aFs.SetAtt(path,KEntryAttReadOnly,0);
       
   105 			}
       
   106 		}
       
   107 	return MapError(err,anErrno);
       
   108 	}
       
   109 
       
   110 int PosixFilesystem::stat (RFs& aFs, const wchar_t* name, struct stat *st, int& anErrno)
       
   111 	{
       
   112 	TFullName fullName;
       
   113 	TInt err=GetFullFile(fullName,(const TText16*)name,aFs);
       
   114 	if (!err)
       
   115 		{
       
   116 		TEntry entry;
       
   117 		if (fullName.Length()==3)
       
   118 			{
       
   119 			entry.iAtt=KEntryAttDir;
       
   120 			entry.iModified==TTime(0);
       
   121 			}
       
   122 		else
       
   123 			err=aFs.Entry(fullName,entry);
       
   124 		if (!err)
       
   125 			{
       
   126 			st->st_size = entry.iSize;
       
   127 			st->st_dev = st->st_rdev = (dev_t)TDriveUnit(fullName);
       
   128 			CFileDesc::MapStat(*st, entry.iModified, entry.iAtt);
       
   129 			return 0;
       
   130 			}
       
   131 		}
       
   132 	return MapError(err, anErrno);
       
   133 	}
       
   134 
       
   135 int PosixFilesystem::chmod (RFs& aFs, const wchar_t* name, int perms, int& anErrno)
       
   136 	{
       
   137 	TFullName fullName;
       
   138 	TInt err=GetFullFile(fullName,(const TText16*)name,aFs);
       
   139 	if (!err)
       
   140 		{
       
   141 		if ((perms&S_IWUSR)==0)
       
   142 			err=aFs.SetAtt(fullName,KEntryAttReadOnly,0);
       
   143 		else
       
   144 			err=aFs.SetAtt(fullName,0,KEntryAttReadOnly);
       
   145 		}
       
   146 	return MapError(err, anErrno);
       
   147 	}
       
   148 
       
   149 int PosixFilesystem::unlink (RFs& aFs, const wchar_t* name, int& anErrno)
       
   150 	{
       
   151 	TFullName fullName;
       
   152 	TInt err=GetFullFile(fullName, (TText16*)name, aFs);
       
   153 	if (!err)
       
   154 		{
       
   155 		TUint att=0;
       
   156 		err=aFs.Att(fullName, att);
       
   157 		if (!err)
       
   158 			if (att&KEntryAttDir)
       
   159 				err=EPERM; 
       
   160 			else
       
   161 				err=aFs.Delete(fullName);
       
   162 		}
       
   163 	return MapError(err, anErrno);
       
   164 	}
       
   165 
       
   166 int PosixFilesystem::rename (RFs& aFs, const wchar_t* oldname, const wchar_t* newname, int& anErrno)
       
   167 	{
       
   168 	TFileName oldFullName;
       
   169 	TInt err = GetFullFile(oldFullName,(const TText16 *)oldname,aFs);
       
   170 	if (!err)
       
   171 		{
       
   172 		TFileName newFullName;
       
   173 		err = GetFullFile(newFullName,(const TText16 *)newname,aFs);
       
   174 		if (!err)
       
   175 			{
       
   176 			// ANSI doesn't require specific handling when newname exists,
       
   177 			// so we can just use the EPOC32 semantics and insist that
       
   178 			// newname doesn't currently exist.
       
   179 			err=aFs.Rename(oldFullName,newFullName);
       
   180 			}
       
   181 		}
       
   182 	return MapError(err, anErrno);
       
   183 	}
       
   184 
       
   185 
       
   186 TInt PosixFilesystem::ResolvePath (RFs& aFs, TParse& aResult, const wchar_t* path, TDes* aFilename)
       
   187 	{
       
   188 	return GetFullPath(aResult,(const TText16*)path,aFs,aFilename);
       
   189 	}
       
   190 
       
   191 
       
   192 #ifdef __WINS__
       
   193 TInt PosixFilesystem::SetDefaultDir (RFs& /*aFs*/)
       
   194 	{
       
   195 	// NB. don't do this on WINS because the executable is
       
   196 	// something like w:\epoc32\release\wins\deb\mytest.exe (or just mytest.exe or
       
   197 	// even ./mytest !!)
       
   198 	return KErrNone;
       
   199 	}
       
   200 #else
       
   201 TInt PosixFilesystem::SetDefaultDir (RFs& aFs)
       
   202 	{
       
   203 	TParse parse;
       
   204 	parse.Set(RProcess().FileName(), NULL, NULL);
       
   205 #ifdef __SECURE_DATA__
       
   206 	return aFs.SetSessionToPrivate(TDriveUnit(parse.Drive()));
       
   207 #else
       
   208 	return aFs.SetSessionPath(parse.DriveAndPath());
       
   209 #endif
       
   210 	}
       
   211 #endif