1 mike 1.18 //%/////////////////////////////////////////////////////////////////////////////
|
2 mike 1.1 //
3 // Copyright (c) 2000 The Open Group, BMC Software, Tivoli Systems, IBM
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a
6 // copy of this software and associated documentation files (the "Software"),
7 // to deal in the Software without restriction, including without limitation
8 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 // and/or sell copies of the Software, and to permit persons to whom the
10 // Software is furnished to do so, subject to the following conditions:
11 //
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
15 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
18 // DEALINGS IN THE SOFTWARE.
19 //
|
20 mike 1.18 //==============================================================================
|
21 mike 1.1 //
|
22 mike 1.18 // Author: Mike Brasher (mbrasher@bmc.com)
|
23 mike 1.1 //
|
24 mike 1.18 // Modified By:
|
25 mike 1.17 //
|
26 mike 1.18 //%/////////////////////////////////////////////////////////////////////////////
|
27 mike 1.1
|
28 mike 1.14 #include <iostream>
|
29 mike 1.15 #include <cstdio>
|
30 mike 1.1 #include <Pegasus/Common/Config.h>
|
31 mike 1.14 #include <Pegasus/Common/System.h>
|
32 mike 1.1 #include "Destroyer.h"
33 #include "FileSystem.h"
|
34 mike 1.14 #include "System.h"
|
35 mike 1.2 #include "Dir.h"
|
36 mike 1.12
|
37 mike 1.14 PEGASUS_NAMESPACE_BEGIN
|
38 mike 1.1
|
39 mike 1.14 // Clone the path as a C String but discard trailing slash if any:
|
40 mike 1.1
41 static char* _clonePath(const String& path)
42 {
43 char* p = path.allocateCString();
44
45 if (!*p)
46 return p;
47
|
48 mike 1.20 char* last = p + path.size() - 1;
|
49 mike 1.1
50 if (*last == '/')
51 *last = '\0';
52
53 return p;
54 }
55
56 Boolean FileSystem::exists(const String& path)
57 {
|
58 mike 1.5 ArrayDestroyer<char> p(_clonePath(path));
|
59 mike 1.14 return System::exists(p.getPointer());
|
60 mike 1.1 }
61
|
62 karl 1.6 Boolean FileSystem::getCurrentDirectory(String& path)
63 {
|
64 mike 1.14 path.clear();
65 char tmp[4096];
|
66 karl 1.6
|
67 mike 1.14 if (!System::getCurrentDirectory(tmp, sizeof(tmp) - 1))
|
68 karl 1.6 return false;
69
70 path.append(tmp);
71 return true;
72 }
73
|
74 mike 1.22 Boolean FileSystem::existsNoCase(const String& path, String& realPath)
|
75 mike 1.2 {
76 realPath.clear();
|
77 mike 1.5 ArrayDestroyer<char> destroyer(_clonePath(path));
|
78 mike 1.2 char* p = destroyer.getPointer();
79
|
80 mike 1.24 const char* dirPath;
|
81 mike 1.2 char* fileName;
82 char* slash = strrchr(p, '/');
83
84 if (slash)
85 {
86 *slash = '\0';
87 fileName = slash + 1;
88 dirPath = p;
|
89 mike 1.14
|
90 mike 1.2 if (*fileName == '\0')
91 return false;
92 }
93 else
94 {
95 fileName = p;
96 dirPath = ".";
97 }
98
|
99 mike 1.21
|
100 mike 1.2 for (Dir dir(dirPath); dir.more(); dir.next())
101 {
|
102 mike 1.22 if (CompareNoCase(fileName, dir.getName()) == 0)
|
103 mike 1.2 {
104 if (strcmp(dirPath, ".") == 0)
105 realPath = dir.getName();
106 else
107 {
108 realPath = dirPath;
109 realPath += '/';
110 realPath += dir.getName();
111 }
112 return true;
113 }
114 }
115
116 return false;
117 }
118
|
119 mike 1.1 Boolean FileSystem::canRead(const String& path)
120 {
|
121 mike 1.5 ArrayDestroyer<char> p(_clonePath(path));
|
122 mike 1.14 return System::canRead(p.getPointer());
|
123 mike 1.1 }
124
125 Boolean FileSystem::canWrite(const String& path)
126 {
|
127 mike 1.5 ArrayDestroyer<char> p(_clonePath(path));
|
128 mike 1.14 return System::canWrite(p.getPointer());
|
129 mike 1.1 }
130
131 Boolean FileSystem::getFileSize(const String& path, Uint32& size)
132 {
|
133 mike 1.5 ArrayDestroyer<char> p(_clonePath(path));
|
134 mike 1.14 return System::getFileSize(p.getPointer(), size);
|
135 mike 1.1 }
136
137 Boolean FileSystem::removeFile(const String& path)
138 {
|
139 mike 1.5 ArrayDestroyer<char> p(_clonePath(path));
|
140 mike 1.15 return System::removeFile(p.getPointer());
|
141 mike 1.1 }
142
143 void FileSystem::loadFileToMemory(
144 Array<Sint8>& array,
145 const String& fileName)
146 {
147 Uint32 fileSize;
148
149 if (!getFileSize(fileName, fileSize))
150 throw CannotOpenFile(fileName);
151
152 char* tmp = fileName.allocateCString();
153 FILE* fp = fopen(tmp, "rb");
154 delete [] tmp;
155
156 if (fp == NULL)
157 throw CannotOpenFile(fileName);
158
159 array.reserve(fileSize);
160 char buffer[4096];
161 size_t n;
162 mike 1.1
163 while ((n = fread(buffer, 1, sizeof(buffer), fp)) > 0)
164 array.append(buffer, n);
165
166 fclose(fp);
167 }
168
|
169 mike 1.22 Boolean FileSystem::compareFiles(
170 const String& path1,
171 const String& path2)
|
172 mike 1.1 {
173 Uint32 fileSize1;
174
|
175 mike 1.22 if (!getFileSize(path1, fileSize1))
176 throw CannotOpenFile(path1);
|
177 mike 1.1
178 Uint32 fileSize2;
179
|
180 mike 1.22 if (!getFileSize(path2, fileSize2))
181 throw CannotOpenFile(path2);
|
182 mike 1.1
183 if (fileSize1 != fileSize2)
184 return false;
185
|
186 mike 1.22 char* tmp1 = path1.allocateCString();
|
187 mike 1.1 FILE* fp1 = fopen(tmp1, "rb");
188 delete [] tmp1;
189
190 if (fp1 == NULL)
|
191 mike 1.22 throw CannotOpenFile(path1);
|
192 mike 1.1
|
193 mike 1.22 char* tmp2 = path2.allocateCString();
|
194 mike 1.1 FILE* fp2 = fopen(tmp2, "rb");
195 delete [] tmp2;
196
197 if (fp2 == NULL)
198 {
199 fclose(fp1);
|
200 mike 1.22 throw CannotOpenFile(path2);
|
201 mike 1.1 }
202
203 int c1;
204 int c2;
205
206 while ((c1 = fgetc(fp1)) != EOF && (c2 = fgetc(fp2)) != EOF)
207 {
208 if (c1 != c2)
209 {
210 fclose(fp1);
211 fclose(fp2);
212 return false;
213 }
214 }
215
216 fclose(fp1);
217 fclose(fp2);
218 return true;
219 }
220
|
221 mike 1.22 Boolean FileSystem::renameFile(
222 const String& oldPath,
223 const String& newPath)
224 {
225 ArrayDestroyer<char> p(oldPath.allocateCString());
226 ArrayDestroyer<char> q(newPath.allocateCString());
227 return System::renameFile(p.getPointer(), q.getPointer());
228 }
229
|
230 mike 1.23 Boolean FileSystem::openNoCase(PEGASUS_STD(ifstream)& is, const String& path)
|
231 mike 1.22 {
232 String realPath;
233
234 if (!existsNoCase(path, realPath))
235 return false;
236
237 ArrayDestroyer<char> p(_clonePath(path));
238
239 is.open(p.getPointer() PEGASUS_IOS_BINARY);
240 return is != 0;
241 }
242
243 Boolean FileSystem::isDirectory(const String& path)
244 {
245 ArrayDestroyer<char> p(_clonePath(path));
246 return System::isDirectory(p.getPointer());
247 }
248
249 Boolean FileSystem::changeDirectory(const String& path)
250 {
251 ArrayDestroyer<char> p(_clonePath(path));
252 mike 1.22 return System::changeDirectory(p.getPointer());
253 }
254
255 Boolean FileSystem::makeDirectory(const String& path)
256 {
257 ArrayDestroyer<char> p(_clonePath(path));
258 return System::makeDirectory(p.getPointer());
259 }
260
261 Boolean FileSystem::removeDirectory(const String& path)
262 {
263 ArrayDestroyer<char> p(_clonePath(path));
264 return System::removeDirectory(p.getPointer());
265 }
266
267 Boolean FileSystem::removeDirectoryHier(const String& path)
268 {
269 Array<String> fileList;
270
271 // Get contents of current directory
272
273 mike 1.22 if (!FileSystem::getDirectoryContents(path,fileList))
274 return false;
275
276 // for files-in-directory, delete or recall removedir
277
278 for (Uint32 i = 0, n = fileList.size(); i < n; i++)
279 {
280 String newPath = path; // extend path to subdir
281 newPath.append("/");
282 newPath.append(fileList[i]);
283
284 if (FileSystem::isDirectory(newPath))
285 {
286 // Recall ourselves with extended path
287 if (!FileSystem::removeDirectoryHier(newPath))
288 return false;
289 }
290
291 else
292 {
293 if (!FileSystem::removeFile(newPath))
294 mike 1.22 return false;
295 }
296 }
297
298 return removeDirectory(path);
299 }
300
|
301 mike 1.14 //
302 // Get the file list in the directory into the
303 // array of strings provided
304 // @return The function should return false under these circumstances:
305 //
306 //
307 // 1. The directory does not exist.
308 // 2. The file exists but is not a directory.
309 // 3. The directory is inaccessible.
310 //
311 //
|
312 mike 1.1 Boolean FileSystem::getDirectoryContents(
313 const String& path,
314 Array<String>& paths)
315 {
316 paths.clear();
|
317 mike 1.14
|
318 karl 1.6 try
319 {
320 for (Dir dir(path); dir.more(); dir.next())
321 {
322 String name = dir.getName();
|
323 mike 1.14
|
324 karl 1.6 if (String::equal(name, ".") || String::equal(name, ".."))
325 continue;
|
326 mike 1.14
|
327 karl 1.6 paths.append(name);
328 }
329 return true;
330 }
|
331 mike 1.1
|
332 karl 1.6 // Catch the Dir exception
333 catch(CannotOpenDirectory&)
|
334 mike 1.1 {
|
335 karl 1.6 return false;
|
336 mike 1.1 }
|
337 mike 1.3 }
338
|
339 mike 1.19 Boolean FileSystem::isDirectoryEmpty(const String& path)
340 {
341 for (Dir dir(path); dir.more(); dir.next())
342 {
343 const char* name = dir.getName();
344
345 if (strcmp(name, ".") != 0 && strcmp(name, "..") != 0)
346 return false;
347 }
348
349 return true;
|
350 mike 1.22 }
351
352 void FileSystem::translateSlashes(String& path)
353 {
354 for (Char16* p = (Char16*)path.getData(); *p; p++)
355 {
356 if (*p == '\\')
357 *p = '/';
358 }
|
359 mike 1.19 }
360
|
361 mike 1.1 PEGASUS_NAMESPACE_END
|