examples/Base/FileServer/Attributes/Attributes.cpp

00001 // Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
00002 // All rights reserved.
00003 // This component and the accompanying materials are made available
00004 // under the terms of "Eclipse Public License v1.0"
00005 // which accompanies this distribution, and is available
00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00007 //
00008 // Initial Contributors:
00009 // Nokia Corporation - initial contribution.
00010 //
00011 // Contributors:
00012 //
00013 // Description:
00014 // This code creates several files and directories inside
00015 // the private directory on the writable drive
00016 // i.e. the directory "\private\0FFFFF03."
00017 // Note that no special capabilities are needed provided all file
00018 // operations are directed at files that lie within
00019 // the private directory. This is the case with this example.
00020 // Before the program terminates,
00021 // all files and directories created will be deleted.
00022 //
00023 
00024 #include <f32file.h>
00025 #include "CommonFramework.h"            
00026 
00027 LOCAL_D RFs fsSession;
00028 
00029 // example functions
00030 void DoDirectoryAttribsL();
00031 void PrintDirectoryListsL();    
00032 void DeleteAllL();
00033 
00034 // utility functions
00035 void FormatEntry(TDes& aBuffer, const TEntry& aEntry);
00036 void FormatAtt(TDes& aBuffer, const TUint aValue);
00037 void MakeSmallFileL(const TDesC& aFileName);
00038 
00039 void WaitForKey()
00040         {
00041         _LIT(KMessage,"Press any key to continue\n");
00042         console->Printf(KMessage);
00043         console->Getch();
00044         }
00045 
00046 LOCAL_C void doExampleL()
00047     {
00048           // connect to file server
00049         User::LeaveIfError(fsSession.Connect());
00050         
00051       // create the private directory
00052           // on the writable drive
00053           // i.e. "\private\0FFFFF03\"
00054           // Note that the number 0FFFFF03 is the 
00055       // process security id taken from the 2nd UID
00056       // specified in the mmp file.
00057     fsSession.CreatePrivatePath(RFs::GetSystemDrive());
00058     
00059       // Set the session path to
00060       // this private directory on writable drive
00061     fsSession.SetSessionToPrivate(RFs::GetSystemDrive());
00062 
00063       
00064         DoDirectoryAttribsL();
00065         WaitForKey();
00066         PrintDirectoryListsL();
00067         WaitForKey();
00068         DeleteAllL();
00069         
00070           // close session with file server
00071         fsSession.Close(); 
00072         }
00073 
00074 void DoDirectoryAttribsL()
00075         {
00076             // Define text to be used for display at the console.
00077         _LIT(KAttsMsg,"\nAttributes and entry details\n");
00078         _LIT(KDateString,"%D%M%Y%/0%1%/1%2%/2%3%/3 %-B%:0%J%:1%T%:2%S%:3%+B");
00079         _LIT(KDateMsg,"Using Entry():\nModification time of %S is %S\n");
00080         _LIT(KSizeMsg,"Size = %d bytes\n");
00081         _LIT(KBuffer,"%S");
00082         _LIT(KEntryMsg,"Using Modified():\nModification time of %S is %S\n");
00083         _LIT(KAttMsg,"Using Att():\n%S");
00084 
00085       // Define subdirectory name and the file name to be used.
00086     _LIT(KSubDirName,"f32examp\\");
00087         _LIT(KFileName,"tmpfile.txt");
00088 
00089           // Create a file. 
00090           // Display its entry details, its modification time 
00091           // and its attributes.
00092           // Then change some attributes and print them again.
00093         console->Printf(KAttsMsg);
00094         
00095           // When referring to a directory rather than a file, 
00096           // a backslash must be appended to the path.
00097         TFileName thePath;
00098         fsSession.PrivatePath(thePath);
00099         thePath.Append(KSubDirName);
00100         
00101            // Make the directory   
00102         TInt err=fsSession.MkDir(thePath);
00103         if (err!=KErrAlreadyExists)  // Don't leave if it already exists
00104                 User::LeaveIfError(err);
00105                 
00106           // Create a file in "private\0ffffff03\f32examp\ "
00107         thePath.Append(KFileName);
00108         MakeSmallFileL(thePath);
00109         
00110           // Get entry details for file and print them  
00111         TEntry entry;   
00112         User::LeaveIfError(fsSession.Entry(thePath,entry)); 
00113         TBuf<30> dateString;
00114         entry.iModified.FormatL(dateString,KDateString);
00115           
00116           // Modification date and time = time of file's creation
00117     console->Printf(KDateMsg,&entry.iName,&dateString);
00118           
00119           // Print size of file 
00120         console->Printf(KSizeMsg,entry.iSize);
00121         TBuf<80> buffer;
00122         FormatEntry(buffer,entry); // Archive attribute should be set
00123         console->Printf(KBuffer,&buffer);
00124         buffer.Zero();
00125 
00126           // get the entry details using Att() and Modified()
00127         TTime time;
00128         User::LeaveIfError(fsSession.Modified(thePath,time));
00129         time.FormatL(dateString,KDateString);
00130         
00131           // Modification date and time = time of file's creation
00132     console->Printf(KEntryMsg,&entry.iName,&dateString);
00133         TUint value;
00134         User::LeaveIfError(fsSession.Att(thePath,value));
00135         FormatAtt(buffer,value); // get and print file attributes
00136         console->Printf(KAttMsg,&buffer);
00137         buffer.Zero();
00138 
00139           // Change entry details using SetEntry() to clear archive
00140         User::LeaveIfError(fsSession.SetEntry(thePath,time,
00141                         NULL,KEntryAttArchive));
00142         }
00143 
00144 void PrintDirectoryListsL()
00145         {
00146            // Define text to be used for display at the console.
00147         _LIT(KListMsg,"\nDirectory listings\n");
00148         _LIT(KListMsg2,"\nDirectories and files:\n");
00149         _LIT(KDirList,"%S\n");
00150         _LIT(KDirs,"\nDirectories:\n");
00151         _LIT(KFilesSizes,"\nFiles and sizes:\n");
00152         _LIT(KBytes," %d bytes\n");
00153         _LIT(KNewLine,"\n");
00154 
00155        // Define subdirectory names and the file names to be used here.
00156     _LIT(KDir1,"f32examp\\tmpdir1\\");
00157         _LIT(KDir2,"f32examp\\tmpdir2\\");
00158         _LIT(KFile2,"f32examp\\tmpfile2.txt");
00159         _LIT(KDirName,"f32examp\\*");
00160 
00161         // Create some directories and files
00162         // in private"\f32examp\." 
00163         // List them using GetDir(), then list files and 
00164         // directories in separate lists. 
00165 
00166         console->Printf(KListMsg);
00167 
00168         TFileName thePrivatePath;
00169         fsSession.PrivatePath(thePrivatePath);
00170         
00171         TFileName thePath;
00172         TInt err;
00173         
00174           // Create private\0fffff03\f32examp\tmpdir1
00175         thePath = thePrivatePath;
00176         thePath.Append(KDir1);
00177         err=fsSession.MkDir(thePath);
00178         if (err!=KErrAlreadyExists)
00179                 User::LeaveIfError(err); // Don't leave if it already exists 
00180         
00181           // Create "private\0fffff03\f32examp\tmpdir2"
00182         thePath = thePrivatePath;
00183         thePath.Append(KDir2);
00184         err=fsSession.MkDir(thePath);
00185         if (err!=KErrAlreadyExists)
00186                 User::LeaveIfError(err); // Don't leave if it already exists 
00187                 
00188           // Create "private\0ffffff03\f32examp\tmpfile2.txt"
00189         thePath = thePrivatePath;
00190         thePath.Append(KFile2);
00191         MakeSmallFileL(thePath);
00192         
00193           // Now list all files and directories in "\f32examp\" 
00194           // 
00195           // in alphabetical order.
00196         thePath = thePrivatePath;
00197         thePath.Append(KDirName);
00198         CDir* dirList;
00199          
00200         //err =  fsSession.GetDir(thePath,KEntryAttMaskSupported,ESortByName,dirList);
00201         
00202         User::LeaveIfError(fsSession.GetDir(thePath,KEntryAttMaskSupported,ESortByName,dirList));
00203         console->Printf(KListMsg2);
00204         TInt i;
00205         for (i=0;i<dirList->Count();i++)
00206                 console->Printf(KDirList,&(*dirList)[i].iName);
00207         delete dirList;
00208         
00209           // List the files and directories in \f32examp\ separately
00210         CDir* fileList;
00211         User::LeaveIfError(fsSession.GetDir(thePath,KEntryAttNormal,ESortByName,fileList,dirList));
00212         console->Printf(KDirs);
00213         for (i=0;i<dirList->Count();i++)
00214                 console->Printf(KDirList,&(*dirList)[i].iName);
00215         console->Printf(KFilesSizes);
00216         for (i=0;i<fileList->Count();i++)
00217                 {
00218                 console->Printf(KDirList,&(*fileList)[i].iName);
00219                 console->Printf(KBytes,(*fileList)[i].iSize);
00220                 }
00221         console->Printf(KNewLine);
00222         delete dirList;
00223         delete fileList;
00224 
00225         }
00226 
00227 void DeleteAllL()
00228 // Delete all the files and directories which have been created
00229         {
00230         // Define descriptor constants using the _LIT macro 
00231         _LIT(KDeleteMsg,"\nDeleteAll()\n");
00232         _LIT(KFile2,"f32examp\\tmpfile2.txt");
00233         _LIT(KDir1,"f32examp\\tmpdir1\\");
00234         _LIT(KDir2,"f32examp\\tmpdir2\\");
00235         _LIT(KFile1,"f32examp\\tmpfile.txt");
00236         _LIT(KTopDir,"f32examp\\");
00237         console->Printf(KDeleteMsg);
00238                 
00239         TFileName thePrivatePath;
00240         fsSession.PrivatePath(thePrivatePath);
00241         
00242         TFileName thePath;
00243                 
00244         thePath = thePrivatePath;
00245         thePath.Append(KFile2);
00246         User::LeaveIfError(fsSession.Delete(thePath));
00247         
00248         thePath = thePrivatePath;
00249         thePath.Append(KDir1);
00250         User::LeaveIfError(fsSession.RmDir(thePath));
00251         
00252         thePath = thePrivatePath;
00253         thePath.Append(KDir2);
00254         User::LeaveIfError(fsSession.RmDir(thePath));
00255         
00256         thePath = thePrivatePath;
00257         thePath.Append(KFile1);
00258         User::LeaveIfError(fsSession.Delete(thePath));
00259         
00260         thePath = thePrivatePath;
00261         thePath.Append(KTopDir);
00262         User::LeaveIfError(fsSession.RmDir(thePath));
00263         }
00264 
00265 void MakeSmallFileL(const TDesC& aFileName)
00266         {
00267         _LIT8(KFileData,"Some data");
00268         RFile file;
00269         User::LeaveIfError(file.Replace(fsSession,aFileName,EFileWrite));
00270         User::LeaveIfError(file.Write(KFileData));
00271         User::LeaveIfError(file.Flush()); // Commit data
00272         file.Close(); // close file having finished with it
00273         }
00274                  
00275 void FormatEntry(TDes& aBuffer, const TEntry& aEntry)
00276         {
00277         _LIT(KEntryDetails,"Entry details: ");
00278         _LIT(KReadOnly," Read-only");
00279         _LIT(KHidden," Hidden");
00280         _LIT(KSystem," System");
00281         _LIT(KDirectory," Directory");
00282         _LIT(KArchive," Archive");
00283         _LIT(KNewLIne,"\n");
00284         aBuffer.Append(KEntryDetails);
00285         if(aEntry.IsReadOnly())
00286                 aBuffer.Append(KReadOnly);
00287         if(aEntry.IsHidden())
00288                 aBuffer.Append(KHidden);
00289         if(aEntry.IsSystem())
00290                 aBuffer.Append(KSystem);
00291         if(aEntry.IsDir())
00292                 aBuffer.Append(KDirectory);
00293         if(aEntry.IsArchive())
00294                 aBuffer.Append(KArchive);               
00295         aBuffer.Append(KNewLIne);
00296         }
00297 
00298 void FormatAtt(TDes& aBuffer, const TUint aValue)
00299         {
00300         _LIT(KAttsMsg,"Attributes set are:");
00301         _LIT(KNormal," Normal");
00302         _LIT(KReadOnly," Read-only");
00303         _LIT(KHidden," Hidden");
00304         _LIT(KSystem," System");
00305         _LIT(KVolume," Volume");
00306         _LIT(KDir," Directory");
00307         _LIT(KArchive," Archive");
00308         _LIT(KNewLine,"\n");
00309         aBuffer.Append(KAttsMsg);
00310         if (aValue & KEntryAttNormal)
00311                 {
00312                 aBuffer.Append(KNormal);
00313                 return;
00314                 }
00315         if (aValue & KEntryAttReadOnly)
00316                 aBuffer.Append(KReadOnly);
00317         if (aValue & KEntryAttHidden)
00318                 aBuffer.Append(KHidden);
00319         if (aValue & KEntryAttSystem)
00320                 aBuffer.Append(KSystem);
00321         if (aValue & KEntryAttVolume)
00322                 aBuffer.Append(KVolume);
00323         if (aValue & KEntryAttDir)
00324                 aBuffer.Append(KDir);
00325         if (aValue & KEntryAttArchive)
00326                 aBuffer.Append(KArchive);
00327         aBuffer.Append(KNewLine);
00328         }
00329 

Generated by  doxygen 1.6.2