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 a.arora 1.50 // Amit K Arora, IBM (amita@in.ibm.com)
|
31 mike 1.28 //
32 //%/////////////////////////////////////////////////////////////////////////////
33
34 #include <iostream>
|
35 konrad.r 1.48 //#include <cstdio>
|
36 mike 1.28 #include <Pegasus/Common/Config.h>
37 #include <Pegasus/Common/System.h>
|
38 a.arora 1.50 #include <Pegasus/Common/AutoPtr.h>
|
39 mike 1.28 #include "FileSystem.h"
40 #include "Dir.h"
41
42 PEGASUS_NAMESPACE_BEGIN
43
44 // Clone the path as a C String but discard trailing slash if any:
45
|
46 kumpf 1.38 static CString _clonePath(const String& path)
|
47 mike 1.28 {
|
48 kumpf 1.38 String clone = path;
|
49 mike 1.28
|
50 kumpf 1.38 if (clone.size() && clone[clone.size()-1] == '/')
51 clone.remove(clone.size()-1);
|
52 mike 1.28
|
53 kumpf 1.38 return clone.getCString();
|
54 mike 1.28 }
55
56 Boolean FileSystem::exists(const String& path)
57 {
|
58 kumpf 1.38 return System::exists(_clonePath(path));
|
59 mike 1.28 }
60
61 Boolean FileSystem::getCurrentDirectory(String& path)
62 {
63 path.clear();
64 char tmp[4096];
65
66 if (!System::getCurrentDirectory(tmp, sizeof(tmp) - 1))
67 return false;
68
69 path.append(tmp);
70 return true;
71 }
72
73 Boolean FileSystem::existsNoCase(const String& path, String& realPath)
74 {
|
75 chuck 1.41 #ifdef PEGASUS_OS_OS400
76 // The OS/400 file system is case insensitive, so just call exists( ).
77 // This is faster, but the main reason to do this is to
78 // avoid multi-threading problems with the IFS directory APIs
79 // (even though they claim to be threadsafe).
80 realPath = path;
81 return exists(path);
82 #else
|
83 mike 1.28 realPath.clear();
|
84 kumpf 1.38 CString cpath = _clonePath(path);
85 const char* p = cpath;
|
86 mike 1.28
87 const char* dirPath;
|
88 kumpf 1.38 const char* fileName;
|
89 kumpf 1.39 char* slash = (char *) strrchr(p, '/');
|
90 mike 1.28
91 if (slash)
92 {
93 *slash = '\0';
94 fileName = slash + 1;
95 dirPath = p;
96
97 if (*fileName == '\0')
98 return false;
99 }
100 else
101 {
102 fileName = p;
103 dirPath = ".";
104 }
105
106
107 for (Dir dir(dirPath); dir.more(); dir.next())
108 {
|
109 kumpf 1.40 if (System::strcasecmp(fileName, dir.getName()) == 0)
|
110 mike 1.28 {
111 if (strcmp(dirPath, ".") == 0)
112 realPath = dir.getName();
113 else
114 {
115 realPath = dirPath;
|
116 kumpf 1.37 realPath.append('/');
117 realPath.append(dir.getName());
|
118 mike 1.28 }
119 return true;
120 }
121 }
122
123 return false;
|
124 chuck 1.41 #endif
|
125 mike 1.28 }
126
127 Boolean FileSystem::canRead(const String& path)
128 {
|
129 kumpf 1.38 return System::canRead(_clonePath(path));
|
130 mike 1.28 }
131
132 Boolean FileSystem::canWrite(const String& path)
133 {
|
134 kumpf 1.38 return System::canWrite(_clonePath(path));
|
135 mike 1.28 }
136
137 Boolean FileSystem::getFileSize(const String& path, Uint32& size)
138 {
|
139 kumpf 1.38 return System::getFileSize(_clonePath(path), size);
|
140 mike 1.28 }
141
142 Boolean FileSystem::removeFile(const String& path)
143 {
|
144 kumpf 1.38 return System::removeFile(_clonePath(path));
|
145 mike 1.28 }
146
147 void FileSystem::loadFileToMemory(
148 Array<Sint8>& array,
149 const String& fileName)
150 {
151 Uint32 fileSize;
152
153 if (!getFileSize(fileName, fileSize))
154 throw CannotOpenFile(fileName);
155
|
156 kumpf 1.38 FILE* fp = fopen(fileName.getCString(), "rb");
|
157 mike 1.28
158 if (fp == NULL)
159 throw CannotOpenFile(fileName);
160
|
161 kumpf 1.36 array.reserveCapacity(fileSize);
|
162 mike 1.28 char buffer[4096];
163 size_t n;
164
165 while ((n = fread(buffer, 1, sizeof(buffer), fp)) > 0)
166 array.append(buffer, n);
167
168 fclose(fp);
169 }
170
171 Boolean FileSystem::compareFiles(
172 const String& path1,
173 const String& path2)
174 {
175 Uint32 fileSize1;
176
177 if (!getFileSize(path1, fileSize1))
178 throw CannotOpenFile(path1);
179
180 Uint32 fileSize2;
181
182 if (!getFileSize(path2, fileSize2))
183 mike 1.28 throw CannotOpenFile(path2);
184
185 if (fileSize1 != fileSize2)
186 return false;
187
|
188 kumpf 1.38 FILE* fp1 = fopen(path1.getCString(), "rb");
|
189 mike 1.28
190 if (fp1 == NULL)
191 throw CannotOpenFile(path1);
192
|
193 kumpf 1.38 FILE* fp2 = fopen(path2.getCString(), "rb");
|
194 mike 1.28
195 if (fp2 == NULL)
196 {
197 fclose(fp1);
198 throw CannotOpenFile(path2);
199 }
200
201 int c1;
202 int c2;
203
204 while ((c1 = fgetc(fp1)) != EOF && (c2 = fgetc(fp2)) != EOF)
205 {
206 if (c1 != c2)
207 {
208 fclose(fp1);
209 fclose(fp2);
210 return false;
211 }
212 }
213
214 fclose(fp1);
215 mike 1.28 fclose(fp2);
216 return true;
217 }
218
219 Boolean FileSystem::renameFile(
220 const String& oldPath,
221 const String& newPath)
222 {
|
223 kumpf 1.38 return System::renameFile(oldPath.getCString(), newPath.getCString());
|
224 mike 1.28 }
225
|
226 mike 1.31 Boolean FileSystem::copyFile(
227 const String& fromPath,
228 const String& toPath)
229 {
|
230 kumpf 1.38 return System::copyFile(fromPath.getCString(), toPath.getCString());
|
231 mike 1.31 }
232
|
233 mike 1.28 Boolean FileSystem::openNoCase(PEGASUS_STD(ifstream)& is, const String& path)
234 {
235 String realPath;
236
237 if (!existsNoCase(path, realPath))
238 return false;
239
|
240 kumpf 1.38 is.open(_clonePath(realPath) PEGASUS_IOS_BINARY);
|
241 david 1.44
|
242 mike 1.31 return !!is;
243 }
244
245 Boolean FileSystem::openNoCase(
246 PEGASUS_STD(fstream)& fs,
247 const String& path,
248 int mode)
249 {
250 String realPath;
|
251 ramnath 1.30
|
252 mike 1.31 if (!existsNoCase(path, realPath))
253 return false;
|
254 mday 1.43 #if defined(__GNUC__) && GCC_VERSION >= 30200
255 fs.open(_clonePath(realPath), PEGASUS_STD(ios_base::openmode)(mode));
256 #else
|
257 david 1.44 #if defined(PEGASUS_OS_OS400)
|
258 david 1.45 fs.open(_clonePath(realPath), mode, PEGASUS_STD(_CCSID_T(1208)) );
|
259 david 1.44 #else
|
260 kumpf 1.38 fs.open(_clonePath(realPath), mode);
|
261 david 1.44 #endif
|
262 mday 1.43 #endif
|
263 mike 1.31 return !!fs;
|
264 mike 1.28 }
265
266 Boolean FileSystem::isDirectory(const String& path)
267 {
|
268 kumpf 1.38 return System::isDirectory(_clonePath(path));
|
269 mike 1.28 }
270
271 Boolean FileSystem::changeDirectory(const String& path)
272 {
|
273 kumpf 1.38 return System::changeDirectory(_clonePath(path));
|
274 mike 1.28 }
275
276 Boolean FileSystem::makeDirectory(const String& path)
277 {
|
278 kumpf 1.38 return System::makeDirectory(_clonePath(path));
|
279 mike 1.28 }
280
281 Boolean FileSystem::removeDirectory(const String& path)
282 {
|
283 kumpf 1.38 return System::removeDirectory(_clonePath(path));
|
284 mike 1.28 }
285
286 Boolean FileSystem::removeDirectoryHier(const String& path)
287 {
288 Array<String> fileList;
289
290 // Get contents of current directory
291
292 if (!FileSystem::getDirectoryContents(path,fileList))
293 return false;
294
295 // for files-in-directory, delete or recall removedir
296
297 for (Uint32 i = 0, n = fileList.size(); i < n; i++)
298 {
299 String newPath = path; // extend path to subdir
300 newPath.append("/");
301 newPath.append(fileList[i]);
302
303 if (FileSystem::isDirectory(newPath))
304 {
305 mike 1.28 // Recall ourselves with extended path
306 if (!FileSystem::removeDirectoryHier(newPath))
307 return false;
308 }
309
310 else
311 {
312 if (!FileSystem::removeFile(newPath))
313 return false;
314 }
315 }
316
317 return removeDirectory(path);
318 }
319
320 //
321 // Get the file list in the directory into the
322 // array of strings provided
323 // @return The function should return false under these circumstances:
324 //
325 //
326 mike 1.28 // 1. The directory does not exist.
327 // 2. The file exists but is not a directory.
328 // 3. The directory is inaccessible.
329 //
330 //
331 Boolean FileSystem::getDirectoryContents(
332 const String& path,
333 Array<String>& paths)
334 {
335 paths.clear();
336
337 try
338 {
339 for (Dir dir(path); dir.more(); dir.next())
340 {
341 String name = dir.getName();
342
343 if (String::equal(name, ".") || String::equal(name, ".."))
344 continue;
345
346 paths.append(name);
347 mike 1.28 }
348 return true;
349 }
350
351 // Catch the Dir exception
352 catch(CannotOpenDirectory&)
353 {
354 return false;
355 }
356 }
357
358 Boolean FileSystem::isDirectoryEmpty(const String& path)
359 {
360 for (Dir dir(path); dir.more(); dir.next())
361 {
362 const char* name = dir.getName();
363
364 if (strcmp(name, ".") != 0 && strcmp(name, "..") != 0)
365 return false;
366 }
367
368 mike 1.28 return true;
369 }
370
371 void FileSystem::translateSlashes(String& path)
372 {
|
373 kumpf 1.35 for (Uint32 i = 0; i < path.size(); i++)
|
374 mike 1.28 {
|
375 kumpf 1.35 if (path[i] == '\\')
376 path[i] = '/';
|
377 mike 1.28 }
378 }
379
|
380 tony 1.42 // Return the just the base name from the path.
381 String FileSystem::extractFileName(const String& path)
382 {
|
383 a.arora 1.50 AutoArrayPtr<char> p_path(new char[path.size() + 1]);
384 String basename = System::extract_file_name((const char *)path.getCString(), p_path.get());
|
385 tony 1.42
386 return basename;
387 }
388
389 // Return just the path to the file or directory into path
390 String FileSystem::extractFilePath(const String& path)
391 {
|
392 a.arora 1.50 AutoArrayPtr<char> p_path(new char[path.size() + 1]);
393 String newpath = System::extract_file_path((const char *)path.getCString(), p_path.get());
|
394 tony 1.42
395 return newpath;
396 }
397
|
398 kumpf 1.47 // Changes file permissions on the given file.
399 Boolean FileSystem::changeFilePermissions(const String& path, mode_t mode)
400 {
401 #if defined(PEGASUS_OS_OS400)
402 // ATTN: If getCString() is modified to return UTF8, then handle the
403 // EBCDIC coversion in SystemUnix.cpp
404 CString tempPath = path.getCString();
405 #else
|
406 david 1.49 CString tempPath = path.getCString();
|
407 kumpf 1.47 #endif
408
409 return System::changeFilePermissions(tempPath, mode);
|
410 konrad.r 1.48 }
411
412 String FileSystem::getAbsoluteFileName(const String &paths, const String &filename) {
413
414 Uint32 pos =0;
415 Uint32 token=0;
416 String path = String::EMPTY;
417 String root = String::EMPTY;
418 String tempPath = paths;
419 do {
420 if (( pos = tempPath.find(FileSystem::getPathDelimiter())) == PEG_NOT_FOUND) {
421 pos = tempPath.size();
422 token = 0;
423 }
424 else {
425 token = 1;
426 }
427 path = tempPath.subString(0, pos);
428 tempPath.remove(0,pos+token);
429 if (FileSystem::exists( path + "/" + filename ) == true) {
430 root = path + "/" + filename;
431 konrad.r 1.48 break;
432 } else
433 {
434 // cout << "File does not exist.\n";
435 }
436 } while (tempPath.size() > 0);
437 return root;
|
438 kumpf 1.47 }
439
|
440 tony 1.42
|
441 kumpf 1.33 Boolean GetLine(PEGASUS_STD(istream)& is, String& line)
442 {
443 line.clear();
444
445 Boolean gotChar = false;
446 char c;
447
448 while (is.get(c))
449 {
450 gotChar = true;
451
452 if (c == '\n')
453 break;
454
455 line.append(c);
456 }
457
458 return gotChar;
459 }
460
|
461 mike 1.28 PEGASUS_NAMESPACE_END
|