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.74 #include <Executor/Match.h>
|
40 marek 1.61 #include "FileSystem.h"
41 #include "Dir.h"
|
42 msolomon 1.64 #ifndef PEGASUS_OS_TYPE_WINDOWS
43 #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 mike 1.75 #if !defined(PEGASUS_OS_VMS) && !defined(PEGASUS_OS_TYPE_WINDOWS)
80
|
81 mike 1.74 // If a file exists that has the same case as the path parmater,
82 // then we can bypass the expensive directory scanning below.
83
84 if (FileSystem::exists(path))
85 {
86 realPath = path;
87 return true;
88 }
89
|
90 mike 1.75 #endif
|
91 mike 1.74
|
92 marek 1.61 realPath.clear();
93 CString cpath = _clonePath(path);
94 const char* p = cpath;
95
96 const char* dirPath;
97 const char* fileName;
98 char* slash = (char *) strrchr(p, '/');
99
100 if (slash)
101 {
102 *slash = '\0';
103 fileName = slash + 1;
104 dirPath = p;
105
106 if (*fileName == '\0')
107 return false;
108 }
109 else
110 {
111 fileName = p;
112 dirPath = ".";
113 marek 1.61 }
114
115
116 for (Dir dir(dirPath); dir.more(); dir.next())
117 {
118 if (System::strcasecmp(fileName, dir.getName()) == 0)
119 {
120 if (strcmp(dirPath, ".") == 0)
121 realPath = dir.getName();
122 else
123 {
124 realPath = dirPath;
125 realPath.append('/');
126 realPath.append(dir.getName());
127 }
128 return true;
129 }
130 }
131
132 return false;
133 }
134 marek 1.61
135 Boolean FileSystem::canRead(const String& path)
136 {
137 return System::canRead(_clonePath(path));
138 }
139
140 Boolean FileSystem::canWrite(const String& path)
141 {
142 return System::canWrite(_clonePath(path));
143 }
144
145 Boolean FileSystem::getFileSize(const String& path, Uint32& size)
146 {
147 return System::getFileSize(_clonePath(path), size);
148 }
149
150 Boolean FileSystem::removeFile(const String& path)
151 {
152 return System::removeFile(_clonePath(path));
153 }
154
155 marek 1.61 void FileSystem::loadFileToMemory(
156 Buffer& array,
157 const String& fileName)
158 {
159 Uint32 fileSize;
160
161 if (!getFileSize(fileName, fileSize))
162 throw CannotOpenFile(fileName);
163
164 FILE* fp = fopen(fileName.getCString(), "rb");
165
166 if (fp == NULL)
167 throw CannotOpenFile(fileName);
168
169 array.reserveCapacity(fileSize);
170 char buffer[4096];
171 size_t n;
172
173 while ((n = fread(buffer, 1, sizeof(buffer), fp)) > 0)
174 array.append(buffer, static_cast<Uint32>(n));
175
176 marek 1.61 fclose(fp);
177 }
178
179 Boolean FileSystem::compareFiles(
180 const String& path1,
181 const String& path2)
182 {
183 Uint32 fileSize1;
184
185 if (!getFileSize(path1, fileSize1))
186 throw CannotOpenFile(path1);
187
188 Uint32 fileSize2;
189
190 if (!getFileSize(path2, fileSize2))
191 throw CannotOpenFile(path2);
192
193 if (fileSize1 != fileSize2)
194 return false;
195
196 FILE* fp1 = fopen(path1.getCString(), "rb");
197 marek 1.61
198 if (fp1 == NULL)
199 throw CannotOpenFile(path1);
200
201 FILE* fp2 = fopen(path2.getCString(), "rb");
202
203 if (fp2 == NULL)
204 {
205 fclose(fp1);
206 throw CannotOpenFile(path2);
207 }
208
209 int c1;
210 int c2;
211
212 while ((c1 = fgetc(fp1)) != EOF && (c2 = fgetc(fp2)) != EOF)
213 {
214 if (c1 != c2)
215 {
216 fclose(fp1);
217 fclose(fp2);
218 marek 1.61 return false;
219 }
220 }
221
222 fclose(fp1);
223 fclose(fp2);
224 return true;
225 }
226
227 Boolean FileSystem::renameFile(
228 const String& oldPath,
229 const String& newPath)
230 {
231 return System::renameFile(oldPath.getCString(), newPath.getCString());
232 }
233
234 Boolean FileSystem::copyFile(
235 const String& fromPath,
236 const String& toPath)
237 {
238 return System::copyFile(fromPath.getCString(), toPath.getCString());
239 marek 1.61 }
240
241 Boolean FileSystem::openNoCase(PEGASUS_STD(ifstream)& is, const String& path)
242 {
243 String realPath;
244
245 if (!existsNoCase(path, realPath))
246 return false;
247
248 is.open(_clonePath(realPath) PEGASUS_IOS_BINARY);
249
250 return !!is;
251 }
252
253 Boolean FileSystem::openNoCase(
|
254 kumpf 1.66 PEGASUS_STD(fstream)& fs,
|
255 marek 1.61 const String& path,
256 int mode)
257 {
258 String realPath;
259
260 if (!existsNoCase(path, realPath))
261 return false;
262 #if defined(__GNUC__) && GCC_VERSION >= 30200
263 fs.open(_clonePath(realPath), PEGASUS_STD(ios_base::openmode)(mode));
264 #else
265 fs.open(_clonePath(realPath), mode);
266 #endif
267 return !!fs;
268 }
269
270 Boolean FileSystem::isDirectory(const String& path)
271 {
272 return System::isDirectory(_clonePath(path));
273 }
274
275 Boolean FileSystem::changeDirectory(const String& path)
276 marek 1.61 {
277 return System::changeDirectory(_clonePath(path));
278 }
279
280 Boolean FileSystem::makeDirectory(const String& path)
281 {
282 return System::makeDirectory(_clonePath(path));
283 }
284
285 Boolean FileSystem::removeDirectory(const String& path)
286 {
287 return System::removeDirectory(_clonePath(path));
288 }
289
290 Boolean FileSystem::removeDirectoryHier(const String& path)
291 {
292 Array<String> fileList;
293
294 // Get contents of current directory
295
296 if (!FileSystem::getDirectoryContents(path,fileList))
297 marek 1.61 return false;
298
299 // for files-in-directory, delete or recall removedir
300
301 for (Uint32 i = 0, n = fileList.size(); i < n; i++)
|
302 kumpf 1.66 {
|
303 marek 1.61 String newPath = path; // extend path to subdir
304 newPath.append("/");
305 newPath.append(fileList[i]);
|
306 kumpf 1.66
|
307 marek 1.61 if (FileSystem::isDirectory(newPath))
308 {
309 // Recall ourselves with extended path
310 if (!FileSystem::removeDirectoryHier(newPath))
|
311 kumpf 1.66 return false;
|
312 marek 1.61 }
313
314 else
315 {
316 if (!FileSystem::removeFile(newPath))
317 return false;
318 }
319 }
320
|
321 kumpf 1.66 return removeDirectory(path);
|
322 marek 1.61 }
323
324 //
325 // Get the file list in the directory into the
326 // array of strings provided
327 // @return The function should return false under these circumstances:
328 //
329 //
330 // 1. The directory does not exist.
331 // 2. The file exists but is not a directory.
332 // 3. The directory is inaccessible.
333 //
334 //
335 Boolean FileSystem::getDirectoryContents(
336 const String& path,
337 Array<String>& paths)
338 {
339 paths.clear();
340
341 try
|
342 kumpf 1.66 {
|
343 marek 1.61 for (Dir dir(path); dir.more(); dir.next())
344 {
345 String name = dir.getName();
346
347 if (String::equal(name, ".") || String::equal(name, ".."))
348 continue;
349
350 paths.append(name);
351 }
352 return true;
353 }
354
355 // Catch the Dir exception
|
356 kumpf 1.66 catch (CannotOpenDirectory&)
|
357 marek 1.61 {
358 return false;
359 }
360 }
361
362 Boolean FileSystem::isDirectoryEmpty(const String& path)
363 {
364 for (Dir dir(path); dir.more(); dir.next())
365 {
366 const char* name = dir.getName();
367
368 if (strcmp(name, ".") != 0 && strcmp(name, "..") != 0)
369 return false;
370 }
371
372 return true;
373 }
374
375 void FileSystem::translateSlashes(String& path)
376 {
377 for (Uint32 i = 0; i < path.size(); i++)
378 marek 1.61 {
379 if (path[i] == '\\')
380 path[i] = '/';
381 }
382 }
383
384 // Return the just the base name from the path.
|
385 kumpf 1.65 String FileSystem::extractFileName(const String& path)
|
386 marek 1.61 {
|
387 kumpf 1.65 AutoArrayPtr<char> p_path(new char[path.size() + 1]);
388 String basename = System::extract_file_name(
389 (const char*)path.getCString(), p_path.get());
|
390 kumpf 1.66
|
391 kumpf 1.65 return basename;
|
392 marek 1.61 }
393
394 // Return just the path to the file or directory into path
395 String FileSystem::extractFilePath(const String& path)
396 {
|
397 kumpf 1.65 AutoArrayPtr<char> p_path(new char[path.size() + 1]);
398 String newpath = System::extract_file_path(
399 (const char*)path.getCString(), p_path.get());
|
400 kumpf 1.66
|
401 kumpf 1.65 return newpath;
|
402 marek 1.61 }
403
404 // Changes file permissions on the given file.
405 Boolean FileSystem::changeFilePermissions(const String& path, mode_t mode)
406 {
407 CString tempPath = path.getCString();
408
409 return System::changeFilePermissions(tempPath, mode);
410 }
411
|
412 kumpf 1.65 String FileSystem::getAbsoluteFileName(
413 const String& paths,
414 const String& filename)
415 {
416 Uint32 pos = 0;
417 Uint32 token = 0;
418 String path;
419 String root;
420 String tempPath = paths;
|
421 marek 1.61
|
422 kumpf 1.65 do
423 {
424 if ((pos = tempPath.find(FileSystem::getPathDelimiter())) ==
425 PEG_NOT_FOUND)
426 {
427 pos = tempPath.size();
428 token = 0;
|
429 marek 1.61 }
|
430 kumpf 1.65 else
431 {
432 token = 1;
|
433 marek 1.61 }
434 path = tempPath.subString(0, pos);
435 tempPath.remove(0,pos+token);
|
436 kumpf 1.65 if (FileSystem::exists(path + "/" + filename) == true)
437 {
438 root = path + "/" + filename;
439 break;
440 }
441 else
442 {
443 // cout << "File does not exist.\n";
444 }
445 } while (tempPath.size() > 0);
|
446 marek 1.61
|
447 kumpf 1.65 return root;
|
448 marek 1.61 }
449
450 String FileSystem::buildLibraryFileName(const String &libraryName)
451 {
452 String fileName;
453
454 //
455 // Add the necessary prefix and suffix to convert the library name to its
456 // corresponding file name.
457 //
|
458 david.dillard 1.62 #if defined(PEGASUS_OS_TYPE_WINDOWS)
|
459 marek 1.61 fileName = libraryName + String(".dll");
|
460 b.whiteley 1.76 #else
461 fileName = String("lib") + libraryName + getDynamicLibraryExtension();
462 #endif
463 return fileName;
464 }
465
466 String FileSystem::getDynamicLibraryExtension()
467 {
468 #if defined(PEGASUS_OS_TYPE_WINDOWS)
469 return String(".dll");
|
470 marek 1.61 #elif defined(PEGASUS_PLATFORM_HPUX_PARISC_ACC)
|
471 b.whiteley 1.76 return String(".sl");
|
472 marek 1.61 #elif defined(PEGASUS_OS_DARWIN)
|
473 b.whiteley 1.76 return String(".dylib");
|
474 marek 1.61 #elif defined(PEGASUS_OS_VMS)
|
475 b.whiteley 1.76 return String(".exe");
|
476 marek 1.61 #else
|
477 b.whiteley 1.76 return String(".so");
|
478 marek 1.61 #endif
479 }
480
481 Boolean GetLine(PEGASUS_STD(istream)& is, String& line)
482 {
483 line.clear();
484
485 Boolean gotChar = false;
486 #ifdef PEGASUS_PLATFORM_ZOS_ZSERIES_IBM
487 char input[1000];
488 is.getline(input,1000);
489 line.assign(input);
490
491 gotChar = !(is.rdstate() & PEGASUS_STD(istream)::failbit);
492 #else
493 char c;
494
495 while (is.get(c))
496 {
497 gotChar = true;
498
499 marek 1.61 if (c == '\n')
500 break;
501
502 line.append(c);
503 }
504 #endif
505
506 return gotChar;
507 }
508
|
509 msolomon 1.64 //
510 // changes the file owner to one specified
511 //
|
512 kumpf 1.65 Boolean FileSystem::changeFileOwner(
513 const String& fileName,
514 const String& userName)
|
515 msolomon 1.64 {
516 #if defined(PEGASUS_OS_TYPE_WINDOWS)
517
518 return true;
519
520 #else
521
522 PEG_METHOD_ENTER(TRC_AUTHENTICATION, "FileSystem::changeFileOwner()");
523
|
524 kumpf 1.65 struct passwd* userPasswd;
|
525 karl 1.78 #if defined(PEGASUS_OS_SOLARIS) || \
|
526 msolomon 1.64 defined(PEGASUS_OS_HPUX) || \
|
527 carson.hovey 1.72 defined(PEGASUS_OS_LINUX) || \
528 defined (PEGASUS_OS_VMS)
|
529 msolomon 1.64
530 const unsigned int PWD_BUFF_SIZE = 1024;
|
531 kumpf 1.65 struct passwd pwd;
|
532 msolomon 1.64 char pwdBuffer[PWD_BUFF_SIZE];
533
|
534 kumpf 1.66 if (getpwnam_r(userName.getCString(), &pwd, pwdBuffer, PWD_BUFF_SIZE,
|
535 msolomon 1.64 &userPasswd) != 0)
536 {
|
537 kumpf 1.65 userPasswd = (struct passwd*)NULL;
|
538 msolomon 1.64 }
539
540 #else
541
542 userPasswd = getpwnam(userName.getCString());
543 #endif
544
|
545 kumpf 1.65 if (userPasswd == NULL)
|
546 msolomon 1.64 {
547 PEG_METHOD_EXIT();
|
548 kumpf 1.65 return false;
|
549 msolomon 1.64 }
550
|
551 kumpf 1.65 Sint32 ret = chown(
552 fileName.getCString(), userPasswd->pw_uid, userPasswd->pw_gid);
|
553 ouyang.jian 1.71
|
554 kumpf 1.65 if (ret == -1)
|
555 msolomon 1.64 {
556 PEG_METHOD_EXIT();
|
557 kumpf 1.65 return false;
|
558 msolomon 1.64 }
559
560 PEG_METHOD_EXIT();
561
|
562 kumpf 1.65 return true;
|
563 msolomon 1.64 #endif
564 }
|
565 mateus.baur 1.70
566 void FileSystem::syncWithDirectoryUpdates(PEGASUS_STD(fstream)& fs)
567 {
568 #if defined(PEGASUS_OS_HPUX)
569 // Writes the data from the iostream buffers to the OS buffers
570 fs.flush();
571 // Writes the data from the OS buffers to the disk
572 fsync(fs.rdbuf()->fd());
573 #endif
574 }
575
|
576 mike 1.74 Boolean FileSystem::glob(
577 const String& path,
578 const String& pattern_,
579 Array<String>& filenames)
580 {
581 filenames.clear();
582
583 try
584 {
585 CString pattern(pattern_.getCString());
586
587 for (Dir dir(path); dir.more(); dir.next())
588 {
589 const char* name = dir.getName();
590
591 if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0)
592 continue;
593
594 if (Match(pattern, name) == 0)
595 filenames.append(name);
596 }
597 mike 1.74 }
598 catch (CannotOpenDirectory&)
599 {
600 return false;
601 }
602
603 return true;
604 }
605
|
606 marek 1.61 PEGASUS_NAMESPACE_END
|