|
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: FileSystemUtils |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include <errno.h> |
|
20 #include <sys/statvfs.h> |
|
21 #include <sys/stat.h> |
|
22 #include <string> |
|
23 #include <dirent.h> |
|
24 #include <errno.h> |
|
25 #include <unistd.h> |
|
26 |
|
27 #include "javajniutils.h" |
|
28 #include "javacommonutils.h" |
|
29 #include "logger.h" |
|
30 |
|
31 #include "fileutilities.h" |
|
32 #include "linuxfilesystemutils.h" |
|
33 |
|
34 using namespace std; |
|
35 using namespace java::fileutils; |
|
36 using namespace java::util; |
|
37 |
|
38 /* |
|
39 * Implementation of FileUtilities methods |
|
40 * |
|
41 */ |
|
42 OS_EXPORT long long FileUtilities::availableSize(const std::wstring& aDrive) |
|
43 { |
|
44 JELOG2(EJavaFile); |
|
45 char* mbDrive = JavaCommonUtils::wstringToUtf8(aDrive); |
|
46 long retVal = 0; |
|
47 try |
|
48 { |
|
49 retVal = LinuxFileSystemUtils::getAvailableSize(mbDrive); |
|
50 } |
|
51 catch (...) |
|
52 { |
|
53 // We need not report this error. If there was some problem, return -1 |
|
54 retVal = -1; |
|
55 } |
|
56 delete[] mbDrive; |
|
57 return retVal; |
|
58 } |
|
59 |
|
60 OS_EXPORT long long FileUtilities::totalSize(const std::wstring& aDrive) |
|
61 { |
|
62 JELOG2(EJavaFile); |
|
63 char* mbDrive = JavaCommonUtils::wstringToUtf8(aDrive); |
|
64 long retVal = 0; |
|
65 try |
|
66 { |
|
67 retVal = LinuxFileSystemUtils::getTotalSize(mbDrive); |
|
68 } |
|
69 catch (...) |
|
70 { |
|
71 // We need not report this error. If there was some problem, return -1 |
|
72 retVal = -1; |
|
73 } |
|
74 delete[] mbDrive; |
|
75 return retVal; |
|
76 } |
|
77 |
|
78 OS_EXPORT long long FileUtilities::usedSize(const std::wstring& aDrive) |
|
79 { |
|
80 JELOG2(EJavaFile); |
|
81 char* mbDrive = JavaCommonUtils::wstringToUtf8(aDrive); |
|
82 long retVal = 0; |
|
83 try |
|
84 { |
|
85 retVal = LinuxFileSystemUtils::getUsedSize(mbDrive); |
|
86 } |
|
87 catch (...) |
|
88 { |
|
89 // We need not report this error. If there was some problem, return -1 |
|
90 retVal = -1; |
|
91 } |
|
92 delete[] mbDrive; |
|
93 return retVal; |
|
94 } |
|
95 |
|
96 OS_EXPORT bool FileUtilities::isHidden(const std::wstring& aFile) |
|
97 { |
|
98 JELOG2(EJavaFile); |
|
99 std::wstring file = aFile; |
|
100 if (aFile.at(aFile.length()-1) == L'/') |
|
101 { |
|
102 file = aFile.substr(0, aFile.length()-1); |
|
103 } |
|
104 char* mbDrive = JavaCommonUtils::wstringToUtf8(file); |
|
105 |
|
106 bool retVal = LinuxFileSystemUtils::isHidden(mbDrive); |
|
107 delete[] mbDrive; |
|
108 return retVal; |
|
109 } |
|
110 |
|
111 OS_EXPORT std::wstring FileUtilities::listRoots() |
|
112 { |
|
113 JELOG2(EJavaFile); |
|
114 std::string retString = LinuxFileSystemUtils::listRoots(); |
|
115 std::wstring wcRetString = JavaCommonUtils::utf8ToWstring(retString.c_str()); |
|
116 return wcRetString; |
|
117 |
|
118 } |
|
119 |
|
120 OS_EXPORT int FileUtilities::setReadable(const std::wstring& aFile, bool aReadable) |
|
121 { |
|
122 JELOG2(EJavaFile); |
|
123 int result = 0; |
|
124 char* utf8Name = JavaCommonUtils::wstringToUtf8(aFile); |
|
125 |
|
126 struct stat fileStat; |
|
127 int error = lstat(utf8Name, &fileStat); |
|
128 |
|
129 if (0!=error) |
|
130 { |
|
131 return -1; |
|
132 } |
|
133 mode_t currentMode = fileStat.st_mode; |
|
134 |
|
135 if (aReadable) |
|
136 { |
|
137 currentMode = currentMode | S_IRUSR; |
|
138 } |
|
139 else |
|
140 { |
|
141 currentMode = currentMode & (~S_IRUSR); |
|
142 } |
|
143 |
|
144 result = chmod(utf8Name, currentMode); |
|
145 |
|
146 delete[] utf8Name; |
|
147 return result; |
|
148 } |
|
149 |
|
150 OS_EXPORT int FileUtilities::setWritable(const std::wstring& aFile, bool aWritable) |
|
151 { |
|
152 JELOG2(EJavaFile); |
|
153 int result = 0; |
|
154 char* utf8Name = JavaCommonUtils::wstringToUtf8(aFile); |
|
155 |
|
156 struct stat fileStat; |
|
157 int error = lstat(utf8Name, &fileStat); |
|
158 |
|
159 if (0!=error) |
|
160 { |
|
161 return -1; |
|
162 } |
|
163 |
|
164 mode_t currentMode = fileStat.st_mode; |
|
165 |
|
166 if (aWritable) |
|
167 { |
|
168 currentMode = currentMode | S_IWUSR; |
|
169 } |
|
170 else |
|
171 { |
|
172 currentMode = currentMode & (~S_IWUSR); |
|
173 } |
|
174 |
|
175 result = chmod(utf8Name, currentMode); |
|
176 |
|
177 delete[] utf8Name; |
|
178 return result; |
|
179 } |
|
180 |
|
181 OS_EXPORT std::wstring FileUtilities::getDirContents(const std::wstring& dName, |
|
182 bool aIncludeHidden) |
|
183 { |
|
184 JELOG2(EJavaFile); |
|
185 std::wstring origDirName = dName; |
|
186 if (origDirName.at(origDirName.length() - 1) == L'/') |
|
187 { |
|
188 int len = origDirName.length(); |
|
189 std::wstring trimName = origDirName.substr(0, len -1); |
|
190 origDirName = trimName; |
|
191 } |
|
192 |
|
193 //since there are no unicode variants of openDir and readDir in linux, we will use basic strings |
|
194 char* multiByteDirName = new char[ origDirName.length()+1 ]; |
|
195 multiByteDirName = java::util::JavaCommonUtils::wstringToUtf8(origDirName); |
|
196 std::string dirName(multiByteDirName); |
|
197 std::string origMbsDirName(multiByteDirName); |
|
198 std::wstring iFileListWrapper; |
|
199 |
|
200 DIR* wDirHandle = opendir(dirName.c_str()); |
|
201 if (wDirHandle) |
|
202 { |
|
203 struct dirent* dir = readdir(wDirHandle); |
|
204 while (dir) |
|
205 { |
|
206 //keep reading directory |
|
207 dirName = origMbsDirName; |
|
208 |
|
209 std::string name(dirName); |
|
210 name += "/"; |
|
211 name += std::string(dir->d_name); |
|
212 |
|
213 if (!aIncludeHidden) |
|
214 { |
|
215 std::wstring wName = java::util::JavaCommonUtils::utf8ToWstring(name.c_str()); |
|
216 if (isHidden(wName)) |
|
217 { |
|
218 // If it is hidden, continue. |
|
219 dir = readdir(wDirHandle); |
|
220 continue; |
|
221 } |
|
222 } |
|
223 |
|
224 LOG1(EJavaFile, EInfo, " FileUtilities::getDirContents(): Trying to Stat :%s: ",name.c_str()); |
|
225 struct stat buf; |
|
226 if (stat(name.c_str() , &buf) == 0) |
|
227 { |
|
228 iFileListWrapper += java::util::JavaCommonUtils::utf8ToWstring(dir->d_name); |
|
229 if (S_ISDIR(buf.st_mode)) |
|
230 { //it is directory |
|
231 LOG1(EJavaFile, EInfo, " FileUtilities::getDirContents(): Directory: %s",dir->d_name); |
|
232 iFileListWrapper += std::wstring(L"/*"); |
|
233 } |
|
234 else |
|
235 { |
|
236 LOG1(EJavaFile, EInfo, " FileUtilities::getDirContents(): File: %s",dir->d_name); |
|
237 iFileListWrapper += std::wstring(L"*"); |
|
238 } |
|
239 } |
|
240 else |
|
241 { |
|
242 WLOG1(EJavaFile, " FileUtilities::getDirContents(): Error in Stat: %d ",errno); |
|
243 } |
|
244 dir = readdir(wDirHandle); |
|
245 } |
|
246 closedir(wDirHandle); |
|
247 } |
|
248 else |
|
249 { |
|
250 ELOG1(EJavaFile," FileUtilities::getDirContents(): Error Opening Dir: %d",errno); |
|
251 } |
|
252 |
|
253 delete[] multiByteDirName; |
|
254 return iFileListWrapper; |
|
255 } |