examples/PIPS/FileAccessExample/fileaccessexample.c

00001 /*
00002 Copyright (c) 2007-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: Component description file  
00028 This example demonstrates the Symbian platform Platform Security model.
00029 This shows the restrictions on the directories that can be accessed by a program, 
00030 depending on the capabilities that the program has.
00031 */
00032 
00033 
00034 
00035 
00036 // Include files
00037 #include <stdio.h>
00038 #include <errno.h>
00039 #include <stdlib.h>
00040 #include <unistd.h>
00041 #include <sys/stat.h>
00042 
00046 void PressAKey()
00047         {
00048         fflush(stdout);
00049         getchar();
00050         }
00055 int DisplayErrorMessage(char msg[])
00056         {
00057         printf("%s [Error NUMBER = %d]\n",msg,errno);
00058         PressAKey();
00059         return EXIT_FAILURE;
00060         }
00065 void CreateASecureDirectory()
00066         {
00067         int result;
00068         
00074         result = mkdir("mydirectory",S_IWUSR);     
00075         if(result == 0)
00076                 {
00077                 printf(" A secure directory for the program is created successfully\n");
00078                 PressAKey();
00079                 }
00080         else
00081                 {
00082                 DisplayErrorMessage("\nError in creating directory\n");
00083                 PressAKey();
00084                 }
00085         }
00090 void WriteDataToAFile(char FileName[])
00091         {
00092         FILE* fp= NULL;
00093         
00099         fp = fopen(FileName, "w");
00100         if (fp== NULL)
00101                 {
00102                 DisplayErrorMessage("\nError in writing data in to the file=%d");
00103                 PressAKey();
00104                 }
00105         else
00106                 {
00107                 fprintf(fp,"%s","hello");
00108                 fflush(fp);
00109                 fclose(fp);
00110                 printf(" Writing data in to the file from the secure directory is successful");
00111                 }
00112         }
00113 
00118 void ReadDataFromAFile(char FileName[])
00119         {
00120         FILE* fp= NULL;
00121         
00127         fp = fopen(FileName, "r");
00128         if (fp == NULL)
00129                 {
00130         DisplayErrorMessage("\nError in reading data from the file = %d"); 
00131         printf(" Press any key to exit from the example");
00132         PressAKey();
00133                 }
00134         else
00135                 {
00136                 char x[30];
00137                                 
00138                 // Seek to the beginning of the file
00139         fseek(fp, SEEK_SET, 0); 
00140         
00141         // Get the data present in file from the beginning 
00142                 fscanf(fp,"%s",x);
00143                 printf("\n Data present in the file is : %s",x);
00144                 fclose(fp);
00145                 printf("\n Reading data of the file from the directory is successful\n");
00146                 }
00147         }
00152 void RemoveADirectory(char FileName[])
00153         {
00154         int result;
00155         
00161         result = remove(FileName);
00162         if(result == 0)
00163                         {
00164                         printf(" Removing the file from the directory is successful\n");
00165                         }
00166                 else
00167                         {
00168                         DisplayErrorMessage(" Error in removing the file from the directory=%d");
00169                         PressAKey();
00170                         }
00176         result = rmdir("mydirectory");
00177    
00178         if(result == 0)
00179                 {
00180                 printf(" Removing the the directory is successful\n");
00181                 PressAKey();
00182                 }
00183         else
00184                 {
00185                 DisplayErrorMessage(" Error in removing the directory=%d");
00186                 PressAKey();
00187                 }
00188         }
00193 void WriteDataToFileOfAnotherProgram(char FileName[])
00194         {
00195         FILE* fp= NULL;
00196         
00202         fp = fopen(FileName, "w");
00203         
00204         if (fp == NULL)
00205                 {
00206                 printf(" Writing data to files in a private directory belonging to another program is not possible"); 
00207         }
00208         else
00209                 {
00210                 printf("\n Writing data to files in a private directory belonging to another program is possible"); 
00211                 PressAKey();
00212                 }
00213         }
00218 void ReadDataFromFileOfAnotherProgram(char FileName[])
00219         {
00220         FILE* fp= NULL;
00221         
00227         fp = fopen(FileName, "r");
00228         
00229         if (fp == NULL)
00230                 {
00231                 printf("\n Reading data from files in a private directory belonging to another program is not possible"); 
00232                 PressAKey();
00233                 }
00234         else
00235                 {
00236                 printf("\n Reading data from files in a private directory belonging to another program is possible"); 
00237                 PressAKey(); 
00238                 }
00239         }
00244 void WriteDataToFileOfSysBin(char FileName[])
00245         {
00246         FILE* fp= NULL;
00247         
00253         fp = fopen(FileName, "w");
00254         
00255         if (fp == NULL)
00256                 {
00257                 printf("\n Writing data to files in the sys/bin directory is not possible"); 
00258                 }
00259         else
00260                 {
00261                 printf("\n Writing data to files in the sys/bin directory is possible"); 
00262                 PressAKey();
00263                 }
00264         }
00269 void ReadDataToFileOfSysBin(char FileName[])
00270         {
00271         FILE* fp = NULL;
00272         
00278         fp = fopen(FileName, "r");
00279         
00280         if (fp == NULL)
00281                 {
00282                 printf("\n Reading data from files in the sys/bin directory is not possible"); 
00283                 PressAKey();
00284                 }
00285         else
00286                 {
00287                 printf("\n Reading data from files in the sys/bin directory is possible"); 
00288                 printf(" Press any key to exit from the example");
00289                 PressAKey(); 
00290                 }       
00291         }
00292 
00293 int main()
00294 {
00295         char ownFileName[50] = "\\private\\e80000c9\\mydirectory\\myownfile.txt";
00296         char otherFileName[40] = "\\private\\e80000c8\\otherfile.txt";
00297         char sysBinFileName[30] = "\\sys\\bin\\euser.dll";
00298         
00299         printf("\n Welcome to the file access example using POSIX APIs\n");
00300         
00301         // Print the message to start the example application.
00302         printf("\n Press enter key to step through the example\n");
00303         PressAKey();
00304         
00305         // Call CreateASecureDirectory() function to create a secure directory for the program.
00306         CreateASecureDirectory();
00307         
00308         // Call WriteDataToAFile() function to write data in to a file from the created directory.
00309         WriteDataToAFile(ownFileName);
00310         
00311         // Call WriteDataToAFile() function to write data in to a file from the created directory.
00312         ReadDataFromAFile(ownFileName);
00313         
00314         // Call RemoveADirectory() function to remove the directory.
00315         RemoveADirectory(ownFileName);
00316         
00317         // Call WriteDataToFileOfAnotherProgram() function to write data in to a file of another program.
00318         WriteDataToFileOfAnotherProgram(otherFileName);
00319         
00320         // Call ReadDataFromFileOfAnotherProgram() function to read data from a file of another program.
00321         ReadDataFromFileOfAnotherProgram(otherFileName);
00322         
00323         // Call WriteDataToFileOfSysBin() function to write data in to a file of sys/bin directory.
00324         WriteDataToFileOfSysBin(sysBinFileName);
00325         
00326         // Call ReadDataFromFileOfSysBin() function to read data from a file in the sys/bin directory.
00327         ReadDataToFileOfSysBin(sysBinFileName);
00328         
00329         // Print the message to exit from the example application.
00330         printf("\n Press enter key to exit from the example application");
00331         PressAKey();
00332         
00333         // returns the success code.
00334         return EXIT_SUCCESS;
00335 }
00336 
00337 

Generated by  doxygen 1.6.2