javacommons/fileutils/src/fileutilityjni.cpp
branchRCL_3
changeset 19 04becd199f91
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:  JNI Layer for FileUtility
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include <errno.h>
       
    20 #include <fcntl.h>
       
    21 #include <unistd.h>
       
    22 #include <stdlib.h>
       
    23 #include <sys/param.h>
       
    24 #include <sys/stat.h>
       
    25 
       
    26 #include "logger.h"
       
    27 #include "javajniutils.h"
       
    28 #include "javacommonutils.h"
       
    29 
       
    30 #include "fileextendedcommon.h" // To be removed once common utility is fixed
       
    31 #include "fileutilities.h"
       
    32 #include "com_nokia_mj_impl_fileutils_FileUtility.h"
       
    33 
       
    34 using namespace std;
       
    35 using namespace java::util;
       
    36 using namespace java::fileutils;
       
    37 using namespace java::fileutility;
       
    38 
       
    39 /*
       
    40  * Class:     com_nokia_mj_impl_fileutils_FileUtility
       
    41  * Method:    _exists
       
    42  * Signature: (Ljava/lang/String;)Z
       
    43  */
       
    44 JNIEXPORT jboolean JNICALL Java_com_nokia_mj_impl_fileutils_FileUtility__1exists(
       
    45     JNIEnv *aJni, jclass, jstring fileName)
       
    46 {
       
    47     try
       
    48     {
       
    49         std::wstring name = FileUtil::jstringToWstring(aJni, fileName);
       
    50         return FileUtilities::exists(name);
       
    51     }
       
    52     catch (...)
       
    53     {
       
    54         ELOG(EJavaFile,
       
    55              "FileUtility: JNI: exists caught exception. Return false");
       
    56         return false;
       
    57     }
       
    58 }
       
    59 
       
    60 /*
       
    61  * Class:     com_nokia_mj_impl_fileutils_FileUtility
       
    62  * Method:    _getRealPath
       
    63  * Signature: (Ljava/lang/String;)Ljava/lang/String;
       
    64  */
       
    65 JNIEXPORT jstring JNICALL Java_com_nokia_mj_impl_fileutils_FileUtility__1getRealPath(
       
    66     JNIEnv *aJni, jclass, jstring aPath)
       
    67 {
       
    68     std::wstring wName = L"";
       
    69     try
       
    70     {
       
    71         std::wstring path = FileUtil::jstringToWstring(aJni, aPath);
       
    72         char* utf8Name = JavaCommonUtils::wstringToUtf8(path);
       
    73         char resolvepath[PATH_MAX];
       
    74         char *rpath = NULL;
       
    75 
       
    76         rpath = realpath(utf8Name, resolvepath);
       
    77         delete[] utf8Name;
       
    78         utf8Name = NULL;
       
    79 
       
    80         if (rpath == NULL)
       
    81         {
       
    82             if ((errno!=ENOENT) && (errno != EACCES))
       
    83             {
       
    84                 ELOG1(EJavaFile,"FileUtility: JNI: getRealPath: Unable to resolve. Throw IOException %d", errno);
       
    85                 JniUtils::throwNewException(aJni, "java/io/IOException", "");
       
    86                 return NULL;
       
    87             }
       
    88             else
       
    89             {
       
    90                 return aPath;
       
    91             }
       
    92         }
       
    93         else
       
    94         {
       
    95             wName = JavaCommonUtils::utf8ToWstring(rpath);
       
    96         }
       
    97     }
       
    98     catch (...)
       
    99     {
       
   100         ELOG(EJavaFile, "FileUtility: JNI: getRealPath: Exception occurred. Returning empty");
       
   101     }
       
   102 
       
   103     jstring retValue = JniUtils::wstringToJstring(aJni, wName);
       
   104     return retValue;
       
   105 }
       
   106 
       
   107 /*
       
   108  * Class:     com_nokia_mj_impl_fileutils_FileUtility
       
   109  * Method:    _availableSize
       
   110  * Signature: (Ljava/lang/String;)J
       
   111  */
       
   112 JNIEXPORT jlong JNICALL Java_com_nokia_mj_impl_fileutils_FileUtility__1availableSize(
       
   113     JNIEnv *aJni, jobject, jstring fileName)
       
   114 {
       
   115     try
       
   116     {
       
   117         std::wstring name = FileUtil::jstringToWstring(aJni, fileName);
       
   118         return FileUtilities::availableSize(name);
       
   119     }
       
   120     catch (...)
       
   121     {
       
   122         ELOG(EJavaFile,
       
   123              "FileUtility: JNI: availableSize caught exception. Return -1");
       
   124         return -1;
       
   125     }
       
   126 }
       
   127 
       
   128 /*
       
   129  * Class:     com_nokia_mj_impl_fileutils_FileUtility
       
   130  * Method:    _canRead
       
   131  * Signature: (Ljava/lang/String;)Z
       
   132  */
       
   133 JNIEXPORT jboolean JNICALL Java_com_nokia_mj_impl_fileutils_FileUtility__1canRead(
       
   134     JNIEnv *aJni, jobject, jstring fileName)
       
   135 {
       
   136     try
       
   137     {
       
   138         std::wstring name = FileUtil::jstringToWstring(aJni, fileName);
       
   139         return FileUtilities::canRead(name);
       
   140     }
       
   141     catch (...)
       
   142     {
       
   143         ELOG(EJavaFile,
       
   144              "FileUtility: JNI: canRead caught exception. Return false");
       
   145         return false;
       
   146     }
       
   147 }
       
   148 
       
   149 /*
       
   150  * Class:     com_nokia_mj_impl_fileutils_FileUtility
       
   151  * Method:    _canWrite
       
   152  * Signature: (Ljava/lang/String;)Z
       
   153  */
       
   154 JNIEXPORT jboolean JNICALL Java_com_nokia_mj_impl_fileutils_FileUtility__1canWrite(
       
   155     JNIEnv *aJni, jobject, jstring fileName)
       
   156 {
       
   157     try
       
   158     {
       
   159         std::wstring name = FileUtil::jstringToWstring(aJni, fileName);
       
   160         return FileUtilities::canWrite(name);
       
   161     }
       
   162     catch (...)
       
   163     {
       
   164         ELOG(EJavaFile,
       
   165              "FileUtility: JNI: canWrite caught exception. Return false");
       
   166         return false;
       
   167     }
       
   168 }
       
   169 
       
   170 /*
       
   171  * Class:     com_nokia_mj_impl_fileutils_FileUtility
       
   172  * Method:    _createNewDirectory
       
   173  * Signature: (Ljava/lang/String;)Z
       
   174  */
       
   175 JNIEXPORT jboolean JNICALL Java_com_nokia_mj_impl_fileutils_FileUtility__1createNewDirectory(
       
   176     JNIEnv *aJni, jobject, jstring fileName)
       
   177 {
       
   178     JELOG2(EJavaFile);
       
   179     try
       
   180     {
       
   181         std::wstring name = FileUtil::jstringToWstring(aJni, fileName);
       
   182         char* utf8Name = JavaCommonUtils::wstringToUtf8(name);
       
   183 
       
   184         int error = mkdir(utf8Name, 0777);
       
   185         delete[] utf8Name;
       
   186 
       
   187         if (error < 0)
       
   188         {
       
   189             ELOG2(
       
   190                 EJavaFile,
       
   191                 "FileUtility: JNI: createNewDirectory: Error: %d: Name: %S",
       
   192                 errno, name.c_str());
       
   193             if ((EEXIST == errno))
       
   194             {
       
   195                 // It already exists
       
   196                 return false;
       
   197             }
       
   198 
       
   199             if ((EACCES == errno) || (EPERM == errno))
       
   200             {
       
   201                 ELOG(EJavaFile,
       
   202                      "JNI: CreateNewDirectory: throwing sec exception");
       
   203                 JniUtils::throwNewException(aJni,
       
   204                                             "java/lang/SecurityException", "Permission denied");
       
   205             }
       
   206             else
       
   207             {
       
   208                 ELOG(EJavaFile, "JNI: CreateNewDirectory: ioexception");
       
   209                 JniUtils::throwNewException(aJni, "java/io/IOException", "Unable to create directory");
       
   210             }
       
   211         }
       
   212         return true;
       
   213     }
       
   214     catch (...)
       
   215     {
       
   216         ELOG(EJavaFile,
       
   217              "JNI: CreateNewDirectory: Exception occured returning false");
       
   218         return false;
       
   219     }
       
   220 }
       
   221 
       
   222 /*
       
   223  * Class:     com_nokia_mj_impl_fileutils_FileUtility
       
   224  * Method:    _createNewFile
       
   225  * Signature: (Ljava/lang/String;)Z
       
   226  */
       
   227 JNIEXPORT jboolean JNICALL Java_com_nokia_mj_impl_fileutils_FileUtility__1createNewFile(
       
   228     JNIEnv *aJni, jobject, jstring fileName)
       
   229 {
       
   230     try
       
   231     {
       
   232         JELOG2(EJavaFile);
       
   233         std::wstring name = FileUtil::jstringToWstring(aJni, fileName);
       
   234         char* utf8Name = JavaCommonUtils::wstringToUtf8(name);
       
   235 
       
   236         //jboolean iscopy;
       
   237         //const char* utf8Name = (aJni->GetStringUTFChars(fileName, &iscopy));
       
   238 
       
   239         int fileDes = open(utf8Name, O_CREAT | O_EXCL, 0666);
       
   240         delete[] utf8Name;
       
   241 
       
   242         if (fileDes < 0)
       
   243         {
       
   244             WLOG1(EJavaFile, "FileUtility: JNI: createNewFile: Error: %d", errno);
       
   245             if ((EEXIST == errno))
       
   246             {
       
   247                 // It already exists
       
   248                 return false;
       
   249             }
       
   250 
       
   251             if ((EACCES == errno) || (EPERM == errno))
       
   252             {
       
   253                 JniUtils::throwNewException(aJni,
       
   254                                             "java/lang/SecurityException", "Permission denied");
       
   255             }
       
   256             else
       
   257             {
       
   258                 // Add also string from the posix utility that gives cause as an error string.
       
   259                 JniUtils::throwNewException(aJni, "java/io/IOException", "Unable to create file");
       
   260             }
       
   261         }
       
   262         else
       
   263         {
       
   264             close(fileDes);
       
   265         }
       
   266 
       
   267         return true;
       
   268     }
       
   269     catch (...)
       
   270     {
       
   271         ELOG(EJavaFile,
       
   272              "JNI: createNewFile: unknown exception. returning false");
       
   273         return false;
       
   274     }
       
   275 }
       
   276 
       
   277 /*
       
   278  * Class:     com_nokia_mj_impl_fileutils_FileUtility
       
   279  * Method:    _delete
       
   280  * Signature: (Ljava/lang/String;)Z
       
   281  */
       
   282 JNIEXPORT jboolean JNICALL Java_com_nokia_mj_impl_fileutils_FileUtility__1delete(
       
   283     JNIEnv *aJni, jobject, jstring fileName)
       
   284 {
       
   285     JELOG2(EJavaFile);
       
   286     try
       
   287     {
       
   288         const char* tempName = aJni->GetStringUTFChars(fileName, 0);
       
   289         string name(tempName);
       
   290         aJni->ReleaseStringUTFChars(fileName, tempName);
       
   291 
       
   292         int error = remove(name.c_str());
       
   293 
       
   294         if (error < 0)
       
   295         {
       
   296             WLOG2(EJavaFile, "FileUtility: JNI: delete: Error: %d: Name:%S",
       
   297                   errno, name.c_str());
       
   298 
       
   299             if ((EACCES == errno) || (EPERM == errno))
       
   300             {
       
   301                 JniUtils::throwNewException(aJni,
       
   302                                             "java/lang/SecurityException", "Permission denied");
       
   303             }
       
   304             else
       
   305             {
       
   306                 return false;
       
   307             }
       
   308         }
       
   309         return true;
       
   310     }
       
   311     catch (...)
       
   312     {
       
   313         ELOG(EJavaFile,
       
   314              "FileUtility: JNI: delete: unknown exception occured. Returning false");
       
   315         return false;
       
   316     }
       
   317 }
       
   318 
       
   319 /*
       
   320  * Class:     com_nokia_mj_impl_fileutils_FileUtility
       
   321  * Method:    _dirSize
       
   322  * Signature: (Ljava/lang/String;Z)J
       
   323  */
       
   324 JNIEXPORT jlong JNICALL Java_com_nokia_mj_impl_fileutils_FileUtility__1dirSize(
       
   325     JNIEnv *aJni, jobject, jstring fileName, jboolean subDirs)
       
   326 {
       
   327     try
       
   328     {
       
   329         std::wstring name = FileUtil::jstringToWstring(aJni, fileName);
       
   330         int retVal = FileUtilities::getDirSize(name, subDirs);
       
   331         return (retVal < 0) ? 0 : retVal;
       
   332     }
       
   333     catch (...)
       
   334     {
       
   335         ELOG(EJavaFile, "FileUtility: JNI: dirSize threw exception. Return 0");
       
   336         return 0;
       
   337     }
       
   338 }
       
   339 
       
   340 /*
       
   341  * Class:     com_nokia_mj_impl_fileutils_FileUtility
       
   342  * Method:    _fileSize
       
   343  * Signature: (Ljava/lang/String;)J
       
   344  */
       
   345 JNIEXPORT jlong JNICALL Java_com_nokia_mj_impl_fileutils_FileUtility__1fileSize(
       
   346     JNIEnv *aJni, jobject, jstring fileName)
       
   347 {
       
   348     std::wstring name = FileUtil::jstringToWstring(aJni, fileName);
       
   349     int ret = 0;
       
   350     try
       
   351     {
       
   352         ret = FileUtilities::fileSize(name);
       
   353     }
       
   354     catch (int err)
       
   355     {
       
   356         ELOG2(EJavaFile, "FileUtility: JNI: fileSize: Error: %d: Name: %S",
       
   357               err, name.c_str());
       
   358         if ((EACCES == err) || (EPERM == err))
       
   359         {
       
   360             JniUtils::throwNewException(aJni, "java/lang/SecurityException",
       
   361                                         "Permission denied");
       
   362         }
       
   363         else
       
   364         {
       
   365             JniUtils::throwNewException(aJni, "java/io/IOException", "Unable to determine file size");
       
   366         }
       
   367 
       
   368     }
       
   369     catch (...)
       
   370     {
       
   371         ELOG1(EJavaFile,
       
   372               "FileUtility: JNI: fileSize: Unknown error caught: Name: %S",
       
   373               name.c_str());
       
   374         JniUtils::throwNewException(aJni, "java/io/IOException", "");
       
   375     }
       
   376     return ret;
       
   377 }
       
   378 
       
   379 /*
       
   380  * Class:     com_nokia_mj_impl_fileutils_FileUtility
       
   381  * Method:    _isDirectory
       
   382  * Signature: (Ljava/lang/String;)Z
       
   383  */
       
   384 JNIEXPORT jboolean JNICALL Java_com_nokia_mj_impl_fileutils_FileUtility__1isDirectory(
       
   385     JNIEnv *aJni, jobject, jstring fileName)
       
   386 {
       
   387     try
       
   388     {
       
   389         std::wstring name = FileUtil::jstringToWstring(aJni, fileName);
       
   390         return FileUtilities::isDirectory(name);
       
   391     }
       
   392     catch (...)
       
   393     {
       
   394         ELOG(EJavaFile,
       
   395              "FileUtility: JNI: isDirectory: Unknown error caught: return false");
       
   396         // Unknown error
       
   397         return false;
       
   398     }
       
   399 }
       
   400 
       
   401 /*
       
   402  * Class:     com_nokia_mj_impl_fileutils_FileUtility
       
   403  * Method:    _isFile
       
   404  * Signature: (Ljava/lang/String;)Z
       
   405  */
       
   406 JNIEXPORT jboolean JNICALL Java_com_nokia_mj_impl_fileutils_FileUtility__1isFile(
       
   407     JNIEnv *aJni, jobject, jstring fileName)
       
   408 {
       
   409     try
       
   410     {
       
   411         std::wstring name = FileUtil::jstringToWstring(aJni, fileName);
       
   412         return FileUtilities::isFile(name);
       
   413     }
       
   414     catch (...)
       
   415     {
       
   416         ELOG(EJavaFile,
       
   417              "FileUtility: JNI: isFile: Unknown error caught: return false");
       
   418         // Unknown error
       
   419         return false;
       
   420     }
       
   421 }
       
   422 
       
   423 /*
       
   424  * Class:     com_nokia_mj_impl_fileutils_FileUtility
       
   425  * Method:    _isHidden
       
   426  * Signature: (Ljava/lang/String;)Z
       
   427  */
       
   428 JNIEXPORT jboolean JNICALL Java_com_nokia_mj_impl_fileutils_FileUtility__1isHidden(
       
   429     JNIEnv *aJni, jobject, jstring fileName)
       
   430 {
       
   431     try
       
   432     {
       
   433         std::wstring name = FileUtil::jstringToWstring(aJni, fileName);
       
   434         return FileUtilities::isHidden(name);
       
   435     }
       
   436     catch (...)
       
   437     {
       
   438         ELOG(EJavaFile,
       
   439              "FileUtility: JNI: isHidden: Unknown error caught: return false");
       
   440         // Unknown error
       
   441         return false;
       
   442     }
       
   443 }
       
   444 
       
   445 /*
       
   446  * Class:     com_nokia_mj_impl_fileutils_FileUtility
       
   447  * Method:    _lastModified
       
   448  * Signature: (Ljava/lang/String;)J
       
   449  */
       
   450 JNIEXPORT jlong JNICALL Java_com_nokia_mj_impl_fileutils_FileUtility__1lastModified(
       
   451     JNIEnv *aJni, jobject, jstring fileName)
       
   452 {
       
   453     try
       
   454     {
       
   455         std::wstring name = FileUtil::jstringToWstring(aJni, fileName);
       
   456         return FileUtilities::lastModified(name);
       
   457     }
       
   458     catch (...)
       
   459     {
       
   460         ELOG(EJavaFile,
       
   461              "FileUtility: JNI: lastModified: Unknown error caught: return 0");
       
   462         // Unknown error
       
   463         return 0;
       
   464     }
       
   465 }
       
   466 
       
   467 
       
   468 /*
       
   469  * Class:     com_nokia_mj_impl_fileutils_FileUtility
       
   470  * Method:    _listDirContents
       
   471  * Signature: (Ljava/lang/String;Z)Ljava/lang/String;
       
   472  */
       
   473 JNIEXPORT jstring JNICALL Java_com_nokia_mj_impl_fileutils_FileUtility__1listDirContents
       
   474 (JNIEnv *aJni, jobject, jstring fileName, jboolean includeHidden)
       
   475 {
       
   476     try
       
   477     {
       
   478         std::wstring name = FileUtil::jstringToWstring(aJni, fileName);
       
   479         std::wstring result = FileUtilities::getDirContents(name, includeHidden);
       
   480         jstring retValue = JniUtils::wstringToJstring(aJni, result);
       
   481         return retValue;
       
   482     }
       
   483     catch (...)
       
   484     {
       
   485         ELOG(EJavaFile,
       
   486              "FileUtility: JNI: listDirContents: Unknown error caught: return null");
       
   487         // Unknown error
       
   488         return NULL;
       
   489     }
       
   490 }
       
   491 
       
   492 /*
       
   493  * Class:     com_nokia_mj_impl_fileutils_FileUtility
       
   494  * Method:    _renameFile
       
   495  * Signature: (Ljava/lang/String;Ljava/lang/String;)Z
       
   496  */
       
   497 JNIEXPORT jboolean JNICALL Java_com_nokia_mj_impl_fileutils_FileUtility__1renameFile(
       
   498     JNIEnv *aJni, jobject, jstring aOldName, jstring aNewName)
       
   499 {
       
   500     try
       
   501     {
       
   502         const char* oldName = aJni->GetStringUTFChars(aOldName, 0);
       
   503         const char* newName = aJni->GetStringUTFChars(aNewName, 0);
       
   504 
       
   505         int error = rename(oldName, newName);
       
   506 
       
   507         aJni->ReleaseStringUTFChars(aOldName, oldName);
       
   508         aJni->ReleaseStringUTFChars(aNewName, newName);
       
   509 
       
   510         if (error < 0)
       
   511         {
       
   512             WLOG1(EJavaFile, "FileUtility: JNI: renameFile Error: %d", error);
       
   513             if ((EACCES == errno) || (EPERM == errno))
       
   514             {
       
   515                 JniUtils::throwNewException(aJni,
       
   516                                             "java/lang/SecurityException", "Permission denied");
       
   517             }
       
   518             else
       
   519             {
       
   520                 JniUtils::throwNewException(aJni, "java/io/IOException", "Unable to rename file or directory");
       
   521             }
       
   522         }
       
   523         return true;
       
   524     }
       
   525     catch (...)
       
   526     {
       
   527         ELOG(EJavaFile,
       
   528              "FileUtility: JNI: renameFile: UNknown exception occured. Returning false ");
       
   529         return false;
       
   530     }
       
   531 }
       
   532 
       
   533 /*
       
   534  * Class:     com_nokia_mj_impl_fileutils_FileUtility
       
   535  * Method:    _setReadable
       
   536  * Signature: (Ljava/lang/String;Z)V
       
   537  */
       
   538 JNIEXPORT void JNICALL Java_com_nokia_mj_impl_fileutils_FileUtility__1setReadable(
       
   539     JNIEnv *aJni, jobject, jstring fileName, jboolean readable)
       
   540 {
       
   541     try
       
   542     {
       
   543         std::wstring name = FileUtil::jstringToWstring(aJni, fileName);
       
   544         FileUtilities::setReadable(name, readable);
       
   545     }
       
   546     catch (...)
       
   547     {
       
   548         JniUtils::throwNewException(aJni, "java/io/IOException", "");
       
   549         ELOG(EJavaFile, "FileUtility: JNI: setReadable: Unknown error caught");
       
   550         //nothing to do
       
   551     }
       
   552 }
       
   553 
       
   554 /*
       
   555  * Class:     com_nokia_mj_impl_fileutils_FileUtility
       
   556  * Method:    _setWritable
       
   557  * Signature: (Ljava/lang/String;Z)V
       
   558  */
       
   559 JNIEXPORT void JNICALL Java_com_nokia_mj_impl_fileutils_FileUtility__1setWritable(
       
   560     JNIEnv *aJni, jobject, jstring fileName, jboolean writable)
       
   561 {
       
   562     try
       
   563     {
       
   564         std::wstring name = FileUtil::jstringToWstring(aJni, fileName);
       
   565         FileUtilities::setWritable(name, writable);
       
   566     }
       
   567     catch (...)
       
   568     {
       
   569         JniUtils::throwNewException(aJni, "java/io/IOException", "");
       
   570         ELOG(EJavaFile, "FileUtility: JNI: setWritable: Unknown error caught");
       
   571         //nothing to do
       
   572     }
       
   573 }
       
   574 
       
   575 /*
       
   576  * Class:     com_nokia_mj_impl_fileutils_FileUtility
       
   577  * Method:    _totalSize
       
   578  * Signature: (Ljava/lang/String;)J
       
   579  */
       
   580 JNIEXPORT jlong JNICALL Java_com_nokia_mj_impl_fileutils_FileUtility__1totalSize(
       
   581     JNIEnv *aJni, jobject, jstring fileName)
       
   582 {
       
   583     try
       
   584     {
       
   585         std::wstring name = FileUtil::jstringToWstring(aJni, fileName);
       
   586         return FileUtilities::totalSize(name);
       
   587     }
       
   588     catch (...)
       
   589     {
       
   590         ELOG(EJavaFile,
       
   591              "FileUtility: JNI: totalSize: Unknown error caught: Return 0");
       
   592         return 0;
       
   593     }
       
   594 }
       
   595 
       
   596 /*
       
   597  * Class:     com_nokia_mj_impl_fileutils_FileUtility
       
   598  * Method:    _truncate
       
   599  * Signature: (Ljava/lang/String;J)J
       
   600  */
       
   601 JNIEXPORT void JNICALL Java_com_nokia_mj_impl_fileutils_FileUtility__1truncate(
       
   602     JNIEnv *aJni, jobject, jstring file, jlong offset)
       
   603 {
       
   604     try
       
   605     {
       
   606         jboolean iscopy;
       
   607         const char* utf8Name = (aJni->GetStringUTFChars(file, &iscopy));
       
   608         int error = truncate(utf8Name, offset);
       
   609 
       
   610         if (error < 0)
       
   611         {
       
   612             WLOG2(EJavaFile, "FileUtility: JNI: truncate: error: %d Name: %s",
       
   613                   errno, utf8Name);
       
   614             JniUtils::throwNewException(aJni, "java/io/IOException",
       
   615                                         "");
       
   616         }
       
   617 
       
   618         aJni->ReleaseStringUTFChars(file, utf8Name);
       
   619     }
       
   620     catch (...)
       
   621     {
       
   622         ELOG(EJavaFile, "FileUtility: JNI: truncate: Unknown error caught");
       
   623         // Nothing to do
       
   624     }
       
   625 }
       
   626 
       
   627 /*
       
   628  * Class:     com_nokia_mj_impl_fileutils_FileUtility
       
   629  * Method:    _usedSize
       
   630  * Signature: (Ljava/lang/String;)J
       
   631  */
       
   632 JNIEXPORT jlong JNICALL Java_com_nokia_mj_impl_fileutils_FileUtility__1usedSize(
       
   633     JNIEnv *aJni, jobject, jstring fileName)
       
   634 {
       
   635     try
       
   636     {
       
   637         std::wstring name = FileUtil::jstringToWstring(aJni, fileName);
       
   638         return FileUtilities::usedSize(name);
       
   639     }
       
   640     catch (...)
       
   641     {
       
   642         ELOG(EJavaFile,
       
   643              "FileUtility: JNI: usedSize: Unknown error caught: return 0");
       
   644         return 0;
       
   645     }
       
   646 }