1 karl 1.63 //%2006////////////////////////////////////////////////////////////////////////
|
2 marek 1.61 //
3 // 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 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
8 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
9 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
10 // EMC Corporation; VERITAS Software Corporation; The Open Group.
|
11 karl 1.63 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
12 // EMC Corporation; Symantec Corporation; The Open Group.
|
13 marek 1.61 //
14 // Permission is hereby granted, free of charge, to any person obtaining a copy
15 // of this software and associated documentation files (the "Software"), to
16 // deal in the Software without restriction, including without limitation the
17 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
18 // sell copies of the Software, and to permit persons to whom the Software is
19 // furnished to do so, subject to the following conditions:
20 //
21 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
22 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
23 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
24 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
25 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
26 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 //==============================================================================
31 //
32 //%/////////////////////////////////////////////////////////////////////////////
33
34 marek 1.61 #include <iostream>
35 //#include <cstdio>
36 #include <Pegasus/Common/Config.h>
37 #include <Pegasus/Common/System.h>
38 #include <Pegasus/Common/AutoPtr.h>
|
39 mike 1.71.6.2 #include <Executor/Match.h>
|
40 marek 1.61 #include "FileSystem.h"
41 #include "Dir.h"
|
42 mike 1.71.6.1 #if !defined(PEGASUS_OS_TYPE_WINDOWS) && !defined(PEGASUS_OS_VXWORKS)
|
43 msolomon 1.64 #include <pwd.h>
44 #endif
45 #include <Pegasus/Common/Tracer.h>
|
46 marek 1.61 PEGASUS_NAMESPACE_BEGIN
47
48 // Clone the path as a C String but discard trailing slash if any:
49
50 static CString _clonePath(const String& path)
51 {
52 String clone = path;
53
54 if (clone.size() && clone[clone.size()-1] == '/')
55 clone.remove(clone.size()-1);
56
57 return clone.getCString();
58 }
59
60 Boolean FileSystem::exists(const String& path)
61 {
62 return System::exists(_clonePath(path));
63 }
64
65 Boolean FileSystem::getCurrentDirectory(String& path)
66 {
67 marek 1.61 path.clear();
68 char tmp[4096];
69
70 if (!System::getCurrentDirectory(tmp, sizeof(tmp) - 1))
71 return false;
72
73 path.append(tmp);
74 return true;
75 }
76
77 Boolean FileSystem::existsNoCase(const String& path, String& realPath)
78 {
79 realPath.clear();
80 CString cpath = _clonePath(path);
81 const char* p = cpath;
82
83 const char* dirPath;
84 const char* fileName;
85 char* slash = (char *) strrchr(p, '/');
86
87 if (slash)
88 marek 1.61 {
89 *slash = '\0';
90 fileName = slash + 1;
91 dirPath = p;
92
93 if (*fileName == '\0')
94 return false;
95 }
96 else
97 {
98 fileName = p;
99 dirPath = ".";
100 }
101
102
103 for (Dir dir(dirPath); dir.more(); dir.next())
104 {
105 if (System::strcasecmp(fileName, dir.getName()) == 0)
106 {
107 if (strcmp(dirPath, ".") == 0)
108 realPath = dir.getName();
109 marek 1.61 else
110 {
111 realPath = dirPath;
112 realPath.append('/');
113 realPath.append(dir.getName());
114 }
115 return true;
116 }
117 }
118
119 return false;
120 }
121
122 Boolean FileSystem::canRead(const String& path)
123 {
124 return System::canRead(_clonePath(path));
125 }
126
127 Boolean FileSystem::canWrite(const String& path)
128 {
129 return System::canWrite(_clonePath(path));
130 marek 1.61 }
131
132 Boolean FileSystem::getFileSize(const String& path, Uint32& size)
133 {
134 return System::getFileSize(_clonePath(path), size);
135 }
136
137 Boolean FileSystem::removeFile(const String& path)
138 {
139 return System::removeFile(_clonePath(path));
140 }
141
142 void FileSystem::loadFileToMemory(
143 Buffer& array,
144 const String& fileName)
145 {
146 Uint32 fileSize;
147
148 if (!getFileSize(fileName, fileSize))
149 throw CannotOpenFile(fileName);
150
151 marek 1.61 FILE* fp = fopen(fileName.getCString(), "rb");
152
153 if (fp == NULL)
154 throw CannotOpenFile(fileName);
155
156 array.reserveCapacity(fileSize);
157 char buffer[4096];
158 size_t n;
159
160 while ((n = fread(buffer, 1, sizeof(buffer), fp)) > 0)
161 array.append(buffer, static_cast<Uint32>(n));
162
163 fclose(fp);
164 }
165
166 Boolean FileSystem::compareFiles(
167 const String& path1,
168 const String& path2)
169 {
170 Uint32 fileSize1;
171
172 marek 1.61 if (!getFileSize(path1, fileSize1))
173 throw CannotOpenFile(path1);
174
175 Uint32 fileSize2;
176
177 if (!getFileSize(path2, fileSize2))
178 throw CannotOpenFile(path2);
179
180 if (fileSize1 != fileSize2)
181 return false;
182
183 FILE* fp1 = fopen(path1.getCString(), "rb");
184
185 if (fp1 == NULL)
186 throw CannotOpenFile(path1);
187
188 FILE* fp2 = fopen(path2.getCString(), "rb");
189
190 if (fp2 == NULL)
191 {
192 fclose(fp1);
193 marek 1.61 throw CannotOpenFile(path2);
194 }
195
196 int c1;
197 int c2;
198
199 while ((c1 = fgetc(fp1)) != EOF && (c2 = fgetc(fp2)) != EOF)
200 {
201 if (c1 != c2)
202 {
203 fclose(fp1);
204 fclose(fp2);
205 return false;
206 }
207 }
208
209 fclose(fp1);
210 fclose(fp2);
211 return true;
212 }
213
214 marek 1.61 Boolean FileSystem::renameFile(
215 const String& oldPath,
216 const String& newPath)
217 {
218 return System::renameFile(oldPath.getCString(), newPath.getCString());
219 }
220
221 Boolean FileSystem::copyFile(
222 const String& fromPath,
223 const String& toPath)
224 {
225 return System::copyFile(fromPath.getCString(), toPath.getCString());
226 }
227
228 Boolean FileSystem::openNoCase(PEGASUS_STD(ifstream)& is, const String& path)
229 {
230 String realPath;
231
232 if (!existsNoCase(path, realPath))
233 return false;
234
235 marek 1.61 is.open(_clonePath(realPath) PEGASUS_IOS_BINARY);
236
237 return !!is;
238 }
239
240 Boolean FileSystem::openNoCase(
|
241 kumpf 1.66 PEGASUS_STD(fstream)& fs,
|
242 marek 1.61 const String& path,
243 int mode)
244 {
245 String realPath;
246
247 if (!existsNoCase(path, realPath))
248 return false;
249 #if defined(__GNUC__) && GCC_VERSION >= 30200
250 fs.open(_clonePath(realPath), PEGASUS_STD(ios_base::openmode)(mode));
251 #else
252 fs.open(_clonePath(realPath), mode);
253 #endif
254 return !!fs;
255 }
256
257 Boolean FileSystem::isDirectory(const String& path)
258 {
259 return System::isDirectory(_clonePath(path));
260 }
261
262 Boolean FileSystem::changeDirectory(const String& path)
263 marek 1.61 {
264 return System::changeDirectory(_clonePath(path));
265 }
266
267 Boolean FileSystem::makeDirectory(const String& path)
268 {
269 return System::makeDirectory(_clonePath(path));
270 }
271
272 Boolean FileSystem::removeDirectory(const String& path)
273 {
274 return System::removeDirectory(_clonePath(path));
275 }
276
277 Boolean FileSystem::removeDirectoryHier(const String& path)
278 {
279 Array<String> fileList;
280
281 // Get contents of current directory
282
283 if (!FileSystem::getDirectoryContents(path,fileList))
284 marek 1.61 return false;
285
286 // for files-in-directory, delete or recall removedir
287
288 for (Uint32 i = 0, n = fileList.size(); i < n; i++)
|
289 kumpf 1.66 {
|
290 marek 1.61 String newPath = path; // extend path to subdir
291 newPath.append("/");
292 newPath.append(fileList[i]);
|
293 kumpf 1.66
|
294 marek 1.61 if (FileSystem::isDirectory(newPath))
295 {
296 // Recall ourselves with extended path
297 if (!FileSystem::removeDirectoryHier(newPath))
|
298 kumpf 1.66 return false;
|
299 marek 1.61 }
300
301 else
302 {
303 if (!FileSystem::removeFile(newPath))
304 return false;
305 }
306 }
307
|
308 kumpf 1.66 return removeDirectory(path);
|
309 marek 1.61 }
310
311 //
312 // Get the file list in the directory into the
313 // array of strings provided
314 // @return The function should return false under these circumstances:
315 //
316 //
317 // 1. The directory does not exist.
318 // 2. The file exists but is not a directory.
319 // 3. The directory is inaccessible.
320 //
321 //
322 Boolean FileSystem::getDirectoryContents(
323 const String& path,
324 Array<String>& paths)
325 {
326 paths.clear();
327
328 try
|
329 kumpf 1.66 {
|
330 marek 1.61 for (Dir dir(path); dir.more(); dir.next())
331 {
332 String name = dir.getName();
333
334 if (String::equal(name, ".") || String::equal(name, ".."))
335 continue;
336
337 paths.append(name);
338 }
339 return true;
340 }
341
342 // Catch the Dir exception
|
343 kumpf 1.66 catch (CannotOpenDirectory&)
|
344 marek 1.61 {
345 return false;
346 }
347 }
348
349 Boolean FileSystem::isDirectoryEmpty(const String& path)
350 {
351 for (Dir dir(path); dir.more(); dir.next())
352 {
353 const char* name = dir.getName();
354
355 if (strcmp(name, ".") != 0 && strcmp(name, "..") != 0)
356 return false;
357 }
358
359 return true;
360 }
361
362 void FileSystem::translateSlashes(String& path)
363 {
364 for (Uint32 i = 0; i < path.size(); i++)
365 marek 1.61 {
366 if (path[i] == '\\')
367 path[i] = '/';
368 }
369 }
370
371 // Return the just the base name from the path.
|
372 kumpf 1.65 String FileSystem::extractFileName(const String& path)
|
373 marek 1.61 {
|
374 kumpf 1.65 AutoArrayPtr<char> p_path(new char[path.size() + 1]);
375 String basename = System::extract_file_name(
376 (const char*)path.getCString(), p_path.get());
|
377 kumpf 1.66
|
378 kumpf 1.65 return basename;
|
379 marek 1.61 }
380
381 // Return just the path to the file or directory into path
382 String FileSystem::extractFilePath(const String& path)
383 {
|
384 kumpf 1.65 AutoArrayPtr<char> p_path(new char[path.size() + 1]);
385 String newpath = System::extract_file_path(
386 (const char*)path.getCString(), p_path.get());
|
387 kumpf 1.66
|
388 kumpf 1.65 return newpath;
|
389 marek 1.61 }
390
391 // Changes file permissions on the given file.
392 Boolean FileSystem::changeFilePermissions(const String& path, mode_t mode)
393 {
394 CString tempPath = path.getCString();
395
396 return System::changeFilePermissions(tempPath, mode);
397 }
398
|
399 kumpf 1.65 String FileSystem::getAbsoluteFileName(
400 const String& paths,
401 const String& filename)
402 {
403 Uint32 pos = 0;
404 Uint32 token = 0;
405 String path;
406 String root;
407 String tempPath = paths;
|
408 marek 1.61
|
409 kumpf 1.65 do
410 {
411 if ((pos = tempPath.find(FileSystem::getPathDelimiter())) ==
412 PEG_NOT_FOUND)
413 {
414 pos = tempPath.size();
415 token = 0;
|
416 marek 1.61 }
|
417 kumpf 1.65 else
418 {
419 token = 1;
|
420 marek 1.61 }
421 path = tempPath.subString(0, pos);
422 tempPath.remove(0,pos+token);
|
423 kumpf 1.65 if (FileSystem::exists(path + "/" + filename) == true)
424 {
425 root = path + "/" + filename;
426 break;
427 }
428 else
429 {
430 // cout << "File does not exist.\n";
431 }
432 } while (tempPath.size() > 0);
|
433 marek 1.61
|
434 kumpf 1.65 return root;
|
435 marek 1.61 }
436
437 String FileSystem::buildLibraryFileName(const String &libraryName)
438 {
439 String fileName;
440
441 //
442 // Add the necessary prefix and suffix to convert the library name to its
443 // corresponding file name.
444 //
|
445 david.dillard 1.62 #if defined(PEGASUS_OS_TYPE_WINDOWS)
|
446 marek 1.61 fileName = libraryName + String(".dll");
447 #elif defined(PEGASUS_PLATFORM_HPUX_PARISC_ACC)
448 fileName = String("lib") + libraryName + String(".sl");
449 #elif defined(PEGASUS_OS_DARWIN)
450 fileName = String("lib") + libraryName + String(".dylib");
451 #elif defined(PEGASUS_OS_VMS)
452 fileName = String("lib") + libraryName;
453 #else
454 fileName = String("lib") + libraryName + String(".so");
455 #endif
456
457 return fileName;
458 }
459
460
461 Boolean GetLine(PEGASUS_STD(istream)& is, String& line)
462 {
463 line.clear();
464
465 Boolean gotChar = false;
466 #ifdef PEGASUS_PLATFORM_ZOS_ZSERIES_IBM
467 marek 1.61 char input[1000];
468 is.getline(input,1000);
469 line.assign(input);
470
471 gotChar = !(is.rdstate() & PEGASUS_STD(istream)::failbit);
472 #else
473 char c;
474
475 while (is.get(c))
476 {
477 gotChar = true;
478
479 if (c == '\n')
480 break;
481
482 line.append(c);
483 }
484 #endif
485
486 return gotChar;
487 }
488 marek 1.61
|
489 mike 1.71.6.1 //==============================================================================
490 //
491 // PEGASUS_OS_VXWORKS
492 //
493 //==============================================================================
494
495 #if defined(PEGASUS_OS_VXWORKS)
496
497 Boolean FileSystem::changeFileOwner(
498 const String& fileName,
499 const String& userName)
500 {
|
501 mike 1.71.6.3 // ATTN-MEB: revisit!
502
503 if (userName == PEGASUS_VXWORKS_USER)
504 return true;
505
|
506 mike 1.71.6.1 return false;
507 }
508
509 #endif /* defined(PEGASUS_OS_VXWORKS) */
510
511 //==============================================================================
512 //
513 // !PEGASUS_OS_VXWORKS
514 //
515 //==============================================================================
516
517 #if !defined(PEGASUS_OS_VXWORKS)
518
|
519 msolomon 1.64 //
520 // changes the file owner to one specified
521 //
|
522 kumpf 1.65 Boolean FileSystem::changeFileOwner(
523 const String& fileName,
524 const String& userName)
|
525 msolomon 1.64 {
526 #if defined(PEGASUS_OS_TYPE_WINDOWS)
527
528 return true;
529
530 #else
531
532 PEG_METHOD_ENTER(TRC_AUTHENTICATION, "FileSystem::changeFileOwner()");
533
|
534 kumpf 1.65 struct passwd* userPasswd;
|
535 msolomon 1.64 #if defined(PEGASUS_PLATFORM_SOLARIS_SPARC_CC) || \
536 defined(PEGASUS_OS_HPUX) || \
537 defined (PEGASUS_OS_LINUX)
538
539 const unsigned int PWD_BUFF_SIZE = 1024;
|
540 kumpf 1.65 struct passwd pwd;
|
541 msolomon 1.64 struct passwd *result;
542 char pwdBuffer[PWD_BUFF_SIZE];
543
|
544 kumpf 1.66 if (getpwnam_r(userName.getCString(), &pwd, pwdBuffer, PWD_BUFF_SIZE,
|
545 msolomon 1.64 &userPasswd) != 0)
546 {
|
547 kumpf 1.65 userPasswd = (struct passwd*)NULL;
|
548 msolomon 1.64 }
549
550 #else
551
552 userPasswd = getpwnam(userName.getCString());
553 #endif
554
|
555 kumpf 1.65 if (userPasswd == NULL)
|
556 msolomon 1.64 {
557 PEG_METHOD_EXIT();
|
558 kumpf 1.65 return false;
|
559 msolomon 1.64 }
560
|
561 kumpf 1.65 Sint32 ret = chown(
562 fileName.getCString(), userPasswd->pw_uid, userPasswd->pw_gid);
|
563 ouyang.jian 1.71
|
564 kumpf 1.65 if (ret == -1)
|
565 msolomon 1.64 {
566 PEG_METHOD_EXIT();
|
567 kumpf 1.65 return false;
|
568 msolomon 1.64 }
569
570 PEG_METHOD_EXIT();
571
|
572 kumpf 1.65 return true;
|
573 msolomon 1.64 #endif
574 }
|
575 mateus.baur 1.70
|
576 mike 1.71.6.1 #endif /* !defined(PEGASUS_OS_VXWORKS) */
577
|
578 mateus.baur 1.70 void FileSystem::syncWithDirectoryUpdates(PEGASUS_STD(fstream)& fs)
579 {
580 #if defined(PEGASUS_OS_HPUX)
581 // Writes the data from the iostream buffers to the OS buffers
582 fs.flush();
583 // Writes the data from the OS buffers to the disk
584 fsync(fs.rdbuf()->fd());
585 #endif
586 }
587
|
588 mike 1.71.6.2 Boolean FileSystem::glob(
589 const String& path,
590 const String& pattern_,
591 Array<String>& filenames)
592 {
593 filenames.clear();
594
595 try
596 {
597 CString pattern(pattern_.getCString());
598
599 for (Dir dir(path); dir.more(); dir.next())
600 {
601 const char* name = dir.getName();
602
603 if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0)
604 continue;
605
606 if (Match(name, pattern) == 0)
607 filenames.append(name);
608 }
609 mike 1.71.6.2 }
610 catch (...)
611 {
612 return false;
613 }
614
615 return true;
616 }
617
618 Boolean FileSystem::removeMatchingFiles(
619 const String& path, const String& pattern)
620 {
621
622 Array<String> filenames;
623
624 if (!FileSystem::glob(path, pattern, filenames))
625 return false;
626
627 for (size_t i = 0; i < filenames.size(); i++)
628 {
629 if (!FileSystem::removeFile(filenames[i]))
630 mike 1.71.6.2 return false;
631 }
632
633 return true;
634 }
635
|
636 marek 1.61 PEGASUS_NAMESPACE_END
|