examples/Base/FileServer/Attributes/Attributes.cpp

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

Generated by  doxygen 1.6.2