javacommons/fileutils/src/fileutilities.cpp
branchRCL_3
changeset 19 04becd199f91
child 48 e0d6e9bd3ca7
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Contains implementation of FileUtilities
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include <dirent.h>
       
    20 #include <errno.h>
       
    21 #include <sys/stat.h>
       
    22 #include <unistd.h>
       
    23 #include <stack>
       
    24 
       
    25 #include "logger.h"
       
    26 #include "javaoslayer.h"
       
    27 #include "javacommonutils.h"
       
    28 #include "fileutilities.h"
       
    29 
       
    30 using namespace java::fileutils;
       
    31 using namespace java::util;
       
    32 
       
    33 OS_EXPORT bool FileUtilities::isFile(const std::wstring& aPath)
       
    34 {
       
    35     JELOG2(EJavaFile);
       
    36     char* utf8Name = JavaCommonUtils::wstringToUtf8(aPath);
       
    37     struct stat fileStat;
       
    38     int error = lstat(utf8Name, &fileStat);
       
    39 
       
    40     bool retValue = false;
       
    41 
       
    42     // If there was no error, then go ahead and retrieve the data out of struct.
       
    43     if (0 == error)
       
    44     {
       
    45         mode_t mode = fileStat.st_mode;
       
    46 
       
    47         if (S_ISREG(mode))
       
    48         { // If it is regular file, return true.
       
    49             retValue = true;
       
    50         }
       
    51     }
       
    52     else
       
    53     {
       
    54         LOG1(EJavaFile, EInfo,
       
    55              "FileUtilities::isFile: Unable to stat file: %s", utf8Name);
       
    56     }
       
    57 
       
    58     // Deallocate utf8 name
       
    59     delete[] utf8Name;
       
    60     return retValue;
       
    61 }
       
    62 
       
    63 OS_EXPORT bool FileUtilities::isDirectory(const std::wstring& aPath)
       
    64 {
       
    65     JELOG2(EJavaFile);
       
    66     char* utf8Name = JavaCommonUtils::wstringToUtf8(aPath);
       
    67     struct stat fileStat;
       
    68     int error = lstat(utf8Name, &fileStat);
       
    69     bool retValue = false;
       
    70 
       
    71     // If there was no error, then go ahead and retrieve the data out of struct.
       
    72     if (0 == error)
       
    73     {
       
    74         mode_t mode = fileStat.st_mode;
       
    75 
       
    76         if (S_ISDIR(mode))
       
    77         {
       
    78             // If it is directory, return true.
       
    79             retValue = true;
       
    80         }
       
    81     }
       
    82     else
       
    83     {
       
    84         LOG1(EJavaFile, EInfo,
       
    85              "FileUtilities::isDirectory: Unable to stat file: %s", utf8Name);
       
    86     }
       
    87 
       
    88     // Deallocate utf8 name
       
    89     delete[] utf8Name;
       
    90     return retValue;
       
    91 }
       
    92 
       
    93 OS_EXPORT bool FileUtilities::exists(const std::wstring& aPath)
       
    94 {
       
    95     JELOG2(EJavaFile);
       
    96     char* utf8Name = JavaCommonUtils::wstringToUtf8(aPath);
       
    97     bool retValue = false;
       
    98 
       
    99     struct stat fileStat;
       
   100 
       
   101     // We try to stat the file, if it succeeds, then we can say it exists.
       
   102     // When we try to stat, the file is not opened. So, even if access restrictions
       
   103     // are present, we can still know if it exists.
       
   104     int error = lstat(utf8Name, &fileStat);
       
   105     if (0 == error)
       
   106     {
       
   107         retValue = true;
       
   108     }
       
   109     else
       
   110     {
       
   111         LOG1(EJavaFile, EInfo, "FileUtilities::exists: Unable to stat file %s",
       
   112              utf8Name);
       
   113     }
       
   114 
       
   115     delete[] utf8Name;
       
   116     return retValue;
       
   117 }
       
   118 
       
   119 OS_EXPORT bool FileUtilities::canRead(const std::wstring& aPath)
       
   120 {
       
   121     JELOG2(EJavaFile);
       
   122     char* utf8Name = JavaCommonUtils::wstringToUtf8(aPath);
       
   123     bool retValue = false;
       
   124 
       
   125     struct stat fileStat;
       
   126     int error = lstat(utf8Name, &fileStat);
       
   127 
       
   128     if (0 == error)
       
   129     {
       
   130         mode_t curMode = fileStat.st_mode;
       
   131 
       
   132         if (curMode & (S_IRUSR))
       
   133         {
       
   134             retValue = true;
       
   135         }
       
   136     }
       
   137     else
       
   138     {
       
   139         LOG1(EJavaFile, EInfo,
       
   140              "FileUtilities::canRead: Unable to check access for %s",
       
   141              utf8Name);
       
   142     }
       
   143 
       
   144     delete[] utf8Name;
       
   145     return retValue;
       
   146 }
       
   147 
       
   148 OS_EXPORT bool FileUtilities::canWrite(const std::wstring& aPath)
       
   149 {
       
   150     JELOG2(EJavaFile);
       
   151     char* utf8Name = JavaCommonUtils::wstringToUtf8(aPath);
       
   152     bool retValue = false;
       
   153 
       
   154     struct stat fileStat;
       
   155     int error = lstat(utf8Name, &fileStat);
       
   156 
       
   157     if (0 == error)
       
   158     {
       
   159         mode_t curMode = fileStat.st_mode;
       
   160 
       
   161         if (curMode & (S_IWUSR))
       
   162         {
       
   163             retValue = true;
       
   164         }
       
   165     }
       
   166     else
       
   167     {
       
   168         LOG2(
       
   169             EJavaFile,
       
   170             EInfo,
       
   171             "FileUtilities::canWrite: Unable to check access for %s: Error: %d",
       
   172             utf8Name, errno);
       
   173     }
       
   174 
       
   175     delete[] utf8Name;
       
   176     return retValue;
       
   177 }
       
   178 
       
   179 OS_EXPORT long FileUtilities::fileSize(const std::wstring& aPath)
       
   180 {
       
   181     JELOG2(EJavaFile);
       
   182     char* utf8Name = JavaCommonUtils::wstringToUtf8(aPath);
       
   183 
       
   184     struct stat fileStat;
       
   185     int error = lstat(utf8Name, &fileStat);
       
   186 
       
   187     long retValue = 0;
       
   188     if (0 == error)
       
   189     {
       
   190         retValue = fileStat.st_size;
       
   191     }
       
   192     else
       
   193     {
       
   194         ELOG2(EJavaFile,
       
   195               "FileUtilities::fileSize: Unable to stat file %s: errno: ",
       
   196               utf8Name, errno);
       
   197 
       
   198         delete[] utf8Name;
       
   199         int error = errno;
       
   200         throw error;
       
   201     }
       
   202 
       
   203     delete[] utf8Name;
       
   204     return retValue;
       
   205 }
       
   206 
       
   207 OS_EXPORT long FileUtilities::lastModified(const std::wstring& aPath)
       
   208 {
       
   209     JELOG2(EJavaFile);
       
   210     char* utf8Name = JavaCommonUtils::wstringToUtf8(aPath);
       
   211 
       
   212     struct stat fileStat;
       
   213     int error = lstat(utf8Name, &fileStat);
       
   214 
       
   215     long retValue = 0;
       
   216     if (0 == error)
       
   217     {
       
   218         retValue = fileStat.st_mtime;
       
   219     }
       
   220     else
       
   221     {
       
   222         ELOG2(
       
   223             EJavaFile,
       
   224             "FileUtilities::lastModified: Error stating file. return = 0 %s: errno: ",
       
   225             utf8Name, errno);
       
   226         retValue = 0;
       
   227     }
       
   228 
       
   229     delete[] utf8Name;
       
   230     return retValue;
       
   231 }
       
   232 
       
   233 OS_EXPORT long FileUtilities::getDirSize(const std::wstring& origDirName,
       
   234         const bool includeSubDirs)
       
   235 {
       
   236     JELOG2(EJavaFile);
       
   237 
       
   238     long totalSize = 0;
       
   239     char* multiByteDirName = java::util::JavaCommonUtils::wstringToUtf8(
       
   240                                  origDirName);
       
   241     std::string dirName(multiByteDirName);
       
   242     std::stack<std::string> subDirs;
       
   243     subDirs.push(dirName);
       
   244 
       
   245     while (!subDirs.empty())
       
   246     {
       
   247         //while stack not empty, keep stating files
       
   248         dirName = subDirs.top();
       
   249         subDirs.pop();
       
   250         DIR* wDirHandle = opendir(dirName.c_str());
       
   251         if (wDirHandle)
       
   252         {
       
   253             struct dirent* dir = readdir(wDirHandle);
       
   254             while (dir)
       
   255             {
       
   256                 //dirName = subDirs.top();
       
   257                 std::string name(dirName);
       
   258                 name += "/";
       
   259                 name += std::string(dir->d_name);
       
   260 
       
   261                 LOG1(EJavaFile, EInfo,
       
   262                      "  FileUtilities::getDirSize(): Stating %s",
       
   263                      name.c_str());
       
   264                 struct stat buf;
       
   265                 if (stat(name.c_str(), &buf) == 0)
       
   266                 {
       
   267                     totalSize += buf.st_size;
       
   268 
       
   269                     if (includeSubDirs && S_ISDIR(buf.st_mode))
       
   270                     {
       
   271                         //if it is directory, put it onto stack
       
   272                         subDirs.push(name);
       
   273                     }
       
   274                 }
       
   275                 dir = readdir(wDirHandle);
       
   276             }
       
   277             closedir(wDirHandle);
       
   278         }
       
   279     }
       
   280 
       
   281     delete[] multiByteDirName; //JavaCommonUtils Allocates Memory
       
   282     return totalSize;
       
   283 }
       
   284 
       
   285 OS_EXPORT std::list<std::wstring> FileUtilities::getDirContentsList(const std::wstring& dirName)
       
   286 {
       
   287     JELOG2(EJavaFile);
       
   288     std::list<std::wstring> contentList;
       
   289     std::wstring stringContents = getDirContents(dirName);
       
   290 
       
   291     wchar_t* delim = L"*";
       
   292     wchar_t* last;
       
   293     wchar_t* tok;
       
   294     wchar_t* stringToTokenize = new wchar_t[ stringContents.length()+1 ];
       
   295     wcscpy(stringToTokenize, stringContents.c_str());
       
   296 
       
   297     for (tok = wcstok(stringToTokenize,delim,&last); tok!=NULL;
       
   298             tok = wcstok(NULL, delim, &last))
       
   299     {
       
   300         LOG1(EJavaFile, EInfo, "  FileUtilities::getDirContentsList(): Adding %S", tok);
       
   301         contentList.push_back(std::wstring(tok));
       
   302     }
       
   303 
       
   304     delete[] stringToTokenize;
       
   305     return contentList;
       
   306 }
       
   307 
       
   308 OS_EXPORT int FileUtilities::makeDirAll(const std::wstring& aDirPath)
       
   309 {
       
   310     JELOG2(EJavaFile);
       
   311     bool ableToOpen = true;
       
   312     std::wstring path;
       
   313     wchar_t *last, *tok, *delim =L"/" ;
       
   314     wchar_t *stringToTokenize = new wchar_t[aDirPath.length()+ 1];
       
   315     wcscpy(stringToTokenize, aDirPath.c_str());
       
   316 
       
   317     for (tok = wcstok(stringToTokenize,delim,&last); tok!=NULL;
       
   318             tok = wcstok(NULL, delim, &last))
       
   319     {
       
   320         path += std::wstring(tok);
       
   321         path += L"/";
       
   322         char *dirName = JavaCommonUtils::wstringToUtf8(path);
       
   323         if (ableToOpen)
       
   324         {
       
   325             if (!opendir(dirName))
       
   326             {
       
   327                 ableToOpen = false;
       
   328                 if (mkdir(dirName,0666)<0)
       
   329                 {
       
   330                     WLOG1(EJavaFile,"FileUtilities::makeDirAll: Directory Creation Failed : %s",dirName);
       
   331                     delete[] dirName;
       
   332                     delete[] stringToTokenize;
       
   333                     return -1;
       
   334                 }
       
   335             }
       
   336         }
       
   337         else
       
   338         {
       
   339             if (mkdir(dirName,0666)<0)
       
   340             {
       
   341                 ELOG1(EJavaFile, "FileUtilities::makeDirAll: Directory Creation Failed : %s",dirName);
       
   342                 delete[] dirName;
       
   343                 delete[] stringToTokenize;
       
   344                 return -1;
       
   345             }
       
   346         }
       
   347         delete[] dirName;
       
   348     }
       
   349     delete[] stringToTokenize;
       
   350     return 0;
       
   351 }
       
   352 
       
   353 OS_EXPORT std::wstring FileUtilities::getDirContents(const std::wstring& aDirName)
       
   354 {
       
   355     return getDirContents(aDirName, true);
       
   356 }