1 karl 1.46 //%2003////////////////////////////////////////////////////////////////////////
|
2 mike 1.28 //
|
3 karl 1.46 // Copyright (c) 2000, 2001, 2002 BMC Software, Hewlett-Packard Development
4 // Company, L. P., IBM Corp., The Open Group, Tivoli Systems.
5 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L. P.;
6 // IBM Corp.; EMC Corporation, The Open Group.
|
7 mike 1.28 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
|
9 kumpf 1.34 // of this software and associated documentation files (the "Software"), to
10 // deal in the Software without restriction, including without limitation the
11 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
12 mike 1.28 // sell copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
|
15 kumpf 1.34 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
|
16 mike 1.28 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
17 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
|
18 kumpf 1.34 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
19 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
21 mike 1.28 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 //
24 //==============================================================================
25 //
26 // Author: Mike Brasher (mbrasher@bmc.com)
27 //
28 // Modified By:
|
29 ramnath 1.29 // Ramnath Ravindran(Ramnath.Ravindran@compaq.com)
|
30 mike 1.28 //
31 //%/////////////////////////////////////////////////////////////////////////////
32
33 #include <iostream>
34 #include <cstdio>
35 #include <Pegasus/Common/Config.h>
36 #include <Pegasus/Common/System.h>
37 #include "Destroyer.h"
38 #include "FileSystem.h"
39 #include "Dir.h"
40
41 PEGASUS_NAMESPACE_BEGIN
42
43 // Clone the path as a C String but discard trailing slash if any:
44
|
45 kumpf 1.38 static CString _clonePath(const String& path)
|
46 mike 1.28 {
|
47 kumpf 1.38 String clone = path;
|
48 mike 1.28
|
49 kumpf 1.38 if (clone.size() && clone[clone.size()-1] == '/')
50 clone.remove(clone.size()-1);
|
51 mike 1.28
|
52 kumpf 1.38 return clone.getCString();
|
53 mike 1.28 }
54
55 Boolean FileSystem::exists(const String& path)
56 {
|
57 kumpf 1.38 return System::exists(_clonePath(path));
|
58 mike 1.28 }
59
60 Boolean FileSystem::getCurrentDirectory(String& path)
61 {
62 path.clear();
63 char tmp[4096];
64
65 if (!System::getCurrentDirectory(tmp, sizeof(tmp) - 1))
66 return false;
67
68 path.append(tmp);
69 return true;
70 }
71
72 Boolean FileSystem::existsNoCase(const String& path, String& realPath)
73 {
|
74 chuck 1.41 #ifdef PEGASUS_OS_OS400
75 // The OS/400 file system is case insensitive, so just call exists( ).
76 // This is faster, but the main reason to do this is to
77 // avoid multi-threading problems with the IFS directory APIs
78 // (even though they claim to be threadsafe).
79 realPath = path;
80 return exists(path);
81 #else
|
82 mike 1.28 realPath.clear();
|
83 kumpf 1.38 CString cpath = _clonePath(path);
84 const char* p = cpath;
|
85 mike 1.28
86 const char* dirPath;
|
87 kumpf 1.38 const char* fileName;
|
88 kumpf 1.39 char* slash = (char *) strrchr(p, '/');
|
89 mike 1.28
90 if (slash)
91 {
92 *slash = '\0';
93 fileName = slash + 1;
94 dirPath = p;
95
96 if (*fileName == '\0')
97 return false;
98 }
99 else
100 {
101 fileName = p;
102 dirPath = ".";
103 }
104
105
106 for (Dir dir(dirPath); dir.more(); dir.next())
107 {
|
108 kumpf 1.40 if (System::strcasecmp(fileName, dir.getName()) == 0)
|
109 mike 1.28 {
110 if (strcmp(dirPath, ".") == 0)
111 realPath = dir.getName();
112 else
113 {
114 realPath = dirPath;
|
115 kumpf 1.37 realPath.append('/');
116 realPath.append(dir.getName());
|
117 mike 1.28 }
118 return true;
119 }
120 }
121
122 return false;
|
123 chuck 1.41 #endif
|
124 mike 1.28 }
125
126 Boolean FileSystem::canRead(const String& path)
127 {
|
128 kumpf 1.38 return System::canRead(_clonePath(path));
|
129 mike 1.28 }
130
131 Boolean FileSystem::canWrite(const String& path)
132 {
|
133 kumpf 1.38 return System::canWrite(_clonePath(path));
|
134 mike 1.28 }
135
136 Boolean FileSystem::getFileSize(const String& path, Uint32& size)
137 {
|
138 kumpf 1.38 return System::getFileSize(_clonePath(path), size);
|
139 mike 1.28 }
140
141 Boolean FileSystem::removeFile(const String& path)
142 {
|
143 kumpf 1.38 return System::removeFile(_clonePath(path));
|
144 mike 1.28 }
145
146 void FileSystem::loadFileToMemory(
147 Array<Sint8>& array,
148 const String& fileName)
149 {
150 Uint32 fileSize;
151
152 if (!getFileSize(fileName, fileSize))
153 throw CannotOpenFile(fileName);
154
|
155 kumpf 1.38 FILE* fp = fopen(fileName.getCString(), "rb");
|
156 mike 1.28
157 if (fp == NULL)
158 throw CannotOpenFile(fileName);
159
|
160 kumpf 1.36 array.reserveCapacity(fileSize);
|
161 mike 1.28 char buffer[4096];
162 size_t n;
163
164 while ((n = fread(buffer, 1, sizeof(buffer), fp)) > 0)
165 array.append(buffer, n);
166
167 fclose(fp);
168 }
169
170 Boolean FileSystem::compareFiles(
171 const String& path1,
172 const String& path2)
173 {
174 Uint32 fileSize1;
175
176 if (!getFileSize(path1, fileSize1))
177 throw CannotOpenFile(path1);
178
179 Uint32 fileSize2;
180
181 if (!getFileSize(path2, fileSize2))
182 mike 1.28 throw CannotOpenFile(path2);
183
184 if (fileSize1 != fileSize2)
185 return false;
186
|
187 kumpf 1.38 FILE* fp1 = fopen(path1.getCString(), "rb");
|
188 mike 1.28
189 if (fp1 == NULL)
190 throw CannotOpenFile(path1);
191
|
192 kumpf 1.38 FILE* fp2 = fopen(path2.getCString(), "rb");
|
193 mike 1.28
194 if (fp2 == NULL)
195 {
196 fclose(fp1);
197 throw CannotOpenFile(path2);
198 }
199
200 int c1;
201 int c2;
202
203 while ((c1 = fgetc(fp1)) != EOF && (c2 = fgetc(fp2)) != EOF)
204 {
205 if (c1 != c2)
206 {
207 fclose(fp1);
208 fclose(fp2);
209 return false;
210 }
211 }
212
213 fclose(fp1);
214 mike 1.28 fclose(fp2);
215 return true;
216 }
217
218 Boolean FileSystem::renameFile(
219 const String& oldPath,
220 const String& newPath)
221 {
|
222 kumpf 1.38 return System::renameFile(oldPath.getCString(), newPath.getCString());
|
223 mike 1.28 }
224
|
225 mike 1.31 Boolean FileSystem::copyFile(
226 const String& fromPath,
227 const String& toPath)
228 {
|
229 kumpf 1.38 return System::copyFile(fromPath.getCString(), toPath.getCString());
|
230 mike 1.31 }
231
|
232 mike 1.28 Boolean FileSystem::openNoCase(PEGASUS_STD(ifstream)& is, const String& path)
233 {
234 String realPath;
235
236 if (!existsNoCase(path, realPath))
237 return false;
238
|
239 kumpf 1.38 is.open(_clonePath(realPath) PEGASUS_IOS_BINARY);
|
240 david 1.44
|
241 mike 1.31 return !!is;
242 }
243
244 Boolean FileSystem::openNoCase(
245 PEGASUS_STD(fstream)& fs,
246 const String& path,
247 int mode)
248 {
249 String realPath;
|
250 ramnath 1.30
|
251 mike 1.31 if (!existsNoCase(path, realPath))
252 return false;
|
253 mday 1.43 #if defined(__GNUC__) && GCC_VERSION >= 30200
254 fs.open(_clonePath(realPath), PEGASUS_STD(ios_base::openmode)(mode));
255 #else
|
256 david 1.44 #if defined(PEGASUS_OS_OS400)
|
257 david 1.45 fs.open(_clonePath(realPath), mode, PEGASUS_STD(_CCSID_T(1208)) );
|
258 david 1.44 #else
|
259 kumpf 1.38 fs.open(_clonePath(realPath), mode);
|
260 david 1.44 #endif
|
261 mday 1.43 #endif
|
262 mike 1.31 return !!fs;
|
263 mike 1.28 }
264
265 Boolean FileSystem::isDirectory(const String& path)
266 {
|
267 kumpf 1.38 return System::isDirectory(_clonePath(path));
|
268 mike 1.28 }
269
270 Boolean FileSystem::changeDirectory(const String& path)
271 {
|
272 kumpf 1.38 return System::changeDirectory(_clonePath(path));
|
273 mike 1.28 }
274
275 Boolean FileSystem::makeDirectory(const String& path)
276 {
|
277 kumpf 1.38 return System::makeDirectory(_clonePath(path));
|
278 mike 1.28 }
279
280 Boolean FileSystem::removeDirectory(const String& path)
281 {
|
282 kumpf 1.38 return System::removeDirectory(_clonePath(path));
|
283 mike 1.28 }
284
285 Boolean FileSystem::removeDirectoryHier(const String& path)
286 {
287 Array<String> fileList;
288
289 // Get contents of current directory
290
291 if (!FileSystem::getDirectoryContents(path,fileList))
292 return false;
293
294 // for files-in-directory, delete or recall removedir
295
296 for (Uint32 i = 0, n = fileList.size(); i < n; i++)
297 {
298 String newPath = path; // extend path to subdir
299 newPath.append("/");
300 newPath.append(fileList[i]);
301
302 if (FileSystem::isDirectory(newPath))
303 {
304 mike 1.28 // Recall ourselves with extended path
305 if (!FileSystem::removeDirectoryHier(newPath))
306 return false;
307 }
308
309 else
310 {
311 if (!FileSystem::removeFile(newPath))
312 return false;
313 }
314 }
315
316 return removeDirectory(path);
317 }
318
319 //
320 // Get the file list in the directory into the
321 // array of strings provided
322 // @return The function should return false under these circumstances:
323 //
324 //
325 mike 1.28 // 1. The directory does not exist.
326 // 2. The file exists but is not a directory.
327 // 3. The directory is inaccessible.
328 //
329 //
330 Boolean FileSystem::getDirectoryContents(
331 const String& path,
332 Array<String>& paths)
333 {
334 paths.clear();
335
336 try
337 {
338 for (Dir dir(path); dir.more(); dir.next())
339 {
340 String name = dir.getName();
341
342 if (String::equal(name, ".") || String::equal(name, ".."))
343 continue;
344
345 paths.append(name);
346 mike 1.28 }
347 return true;
348 }
349
350 // Catch the Dir exception
351 catch(CannotOpenDirectory&)
352 {
353 return false;
354 }
355 }
356
357 Boolean FileSystem::isDirectoryEmpty(const String& path)
358 {
359 for (Dir dir(path); dir.more(); dir.next())
360 {
361 const char* name = dir.getName();
362
363 if (strcmp(name, ".") != 0 && strcmp(name, "..") != 0)
364 return false;
365 }
366
367 mike 1.28 return true;
368 }
369
370 void FileSystem::translateSlashes(String& path)
371 {
|
372 kumpf 1.35 for (Uint32 i = 0; i < path.size(); i++)
|
373 mike 1.28 {
|
374 kumpf 1.35 if (path[i] == '\\')
375 path[i] = '/';
|
376 mike 1.28 }
377 }
378
|
379 tony 1.42 // Return the just the base name from the path.
380 String FileSystem::extractFileName(const String& path)
381 {
382 char *p_path = new char[path.size() + 1];
383 String basename = System::extract_file_name((const char *)path.getCString(), p_path);
384
385 delete [] p_path;
386
387 return basename;
388 }
389
390 // Return just the path to the file or directory into path
391 String FileSystem::extractFilePath(const String& path)
392 {
393 char *p_path = new char[path.size() + 1];
394 String newpath = System::extract_file_path((const char *)path.getCString(), p_path);
395
396 delete [] p_path;
397
398 return newpath;
399 }
400 tony 1.42
401
|
402 kumpf 1.33 Boolean GetLine(PEGASUS_STD(istream)& is, String& line)
403 {
404 line.clear();
405
406 Boolean gotChar = false;
407 char c;
408
409 while (is.get(c))
410 {
411 gotChar = true;
412
413 if (c == '\n')
414 break;
415
416 line.append(c);
417 }
418
419 return gotChar;
420 }
421
|
422 mike 1.28 PEGASUS_NAMESPACE_END
|