javacommons/fileutils/tsrc/fileutilstestinit/src.linux/filetestutils.cpp
branchRCL_3
changeset 14 04becd199f91
equal deleted inserted replaced
13:f5050f1da672 14: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:  FileTestUtils
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include <fstream>
       
    20 #include <iostream>
       
    21 #include <stdio.h>
       
    22 #include <errno.h>
       
    23 #include "filetestutils.h"
       
    24 
       
    25 using namespace std;
       
    26 using namespace java::filetest;
       
    27 
       
    28 void FileTestUtils::createFile(const wstring &aPath, const int aFlags, int aMode)
       
    29 {
       
    30     char* chr = new char[aPath.size()+1];
       
    31     sprintf(chr,"%ls",aPath.c_str());
       
    32 
       
    33     if (0 == aMode)
       
    34     {
       
    35         aMode = 0777;
       
    36     }
       
    37     int fd = open(chr, aFlags, aMode);
       
    38     delete[] chr;
       
    39 
       
    40     FileTestUtils::checkError(aPath,fd);
       
    41     close(fd);
       
    42 }
       
    43 
       
    44 
       
    45 void FileTestUtils::createSystemFile(const wstring &aPath)
       
    46 {
       
    47     FileTestUtils::createFile(aPath, O_CREAT|O_EXCL|O_WRONLY);
       
    48     //There is nothing like System File in Linux. So do nothing.
       
    49 }
       
    50 
       
    51 void FileTestUtils::createHiddenDir(const wstring &aPath, const wstring& aFile)
       
    52 {
       
    53     wstring name = aPath+L"."+aFile;
       
    54     FileTestUtils::createDir(name.c_str());
       
    55     //Creating dir which starts with .is as good as hidden :-)
       
    56 }
       
    57 
       
    58 void FileTestUtils::createSystemDir(const wstring &aPath)
       
    59 {
       
    60     FileTestUtils::createDirMode(aPath, 0777);
       
    61     //There is nothing like System File in Linux. So do nothing.
       
    62 }
       
    63 
       
    64 void FileTestUtils::createDirMode(const wstring& aPath, const int aMode)
       
    65 {
       
    66     // R W X
       
    67     // 4 2 1
       
    68     char* chr = new char[aPath.size()+1];
       
    69     sprintf(chr,"%ls",aPath.c_str());
       
    70     int error = mkdir(chr, aMode);
       
    71     delete[] chr;
       
    72     FileTestUtils::checkError(aPath, error);
       
    73 }
       
    74 
       
    75 void FileTestUtils::writeDataToFile(const wstring& aPath, const string& aData)
       
    76 {
       
    77     //Linux, just use ofstream
       
    78     char* chr = new char[aPath.size()+1];
       
    79     sprintf(chr,"%ls",aPath.c_str());
       
    80     int fd = open(chr, O_RDWR);
       
    81     delete[] chr;
       
    82     checkError(aPath, fd);
       
    83     write(fd, aData.c_str(), aData.length());
       
    84     close(fd);
       
    85 }
       
    86 
       
    87 void FileTestUtils::writeDataInLoop(const wstring& aPath, const int aData, const int aTimes)
       
    88 {
       
    89     char* chr = new char[aPath.size()+1];
       
    90     sprintf(chr,"%ls",aPath.c_str());
       
    91     ofstream file(chr, ios::out|ios::app|ios::binary);
       
    92     delete[] chr;
       
    93     for (int i=0 ; i<aTimes; i++)
       
    94     {
       
    95         file<<(char)aData;
       
    96     }
       
    97     file.close();
       
    98 
       
    99 }
       
   100 
       
   101 void FileTestUtils::appendDataToFile(const wstring& aPath, const int aData)
       
   102 {
       
   103     char* chr = new char[aPath.size()+1];
       
   104     sprintf(chr,"%ls",aPath.c_str());
       
   105     ofstream file(chr, ios::out|ios::app|ios::binary);
       
   106     delete[] chr;
       
   107     file<<(char)aData;
       
   108     file.close();
       
   109 }
       
   110 
       
   111 void FileTestUtils::appendDataToFile(const wstring& aPath, const wstring& aData)
       
   112 {
       
   113     char* chr = new char[aPath.size()+1];
       
   114     sprintf(chr,"%ls",aPath.c_str());
       
   115     ofstream file(chr, ios::out|ios::app|ios::binary);
       
   116     delete[] chr;
       
   117     file<<(char*)aData.c_str();
       
   118     file.close();
       
   119 }
       
   120 
       
   121 void FileTestUtils::setFileHidden(const wstring &aPath, const wstring &aFile)
       
   122 {
       
   123     //File Starting from . is considered hidden
       
   124     wstring oldFile = aPath+aFile;
       
   125     wstring newName = aPath+L"."+aFile;
       
   126 
       
   127     char* chr = new char[oldFile.size()+1];
       
   128     sprintf(chr,"%ls",oldFile.c_str());
       
   129 
       
   130     char* chr1 = new char[newName.size()+1];
       
   131     sprintf(chr1,"%ls",newName.c_str());
       
   132     rename(chr,chr1);
       
   133     delete[] chr;
       
   134     delete[] chr1;
       
   135 }
       
   136 
       
   137 void FileTestUtils::setFileReadOnly(const wstring &aPath)
       
   138 {
       
   139     char* chr = new char[aPath.size()+1];
       
   140     sprintf(chr,"%ls",aPath.c_str());
       
   141     chmod(chr, 0444);
       
   142     delete[] chr;
       
   143 }
       
   144 
       
   145 void FileTestUtils::setDirHidden(const wstring &aPath, const wstring &aName)
       
   146 {
       
   147     wstring oldFile = aPath+aName;
       
   148     wstring newName = aPath+L"."+aName;
       
   149 
       
   150     char* chr = new char[aPath.size()+1];
       
   151     sprintf(chr,"%ls",aPath.c_str());
       
   152 
       
   153     char* chr1 = new char[aName.size()+1];
       
   154     sprintf(chr1,"%ls",aName.c_str());
       
   155     rename(chr,chr1);
       
   156     delete[] chr;
       
   157     delete[] chr1;
       
   158 }