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 #include "FileSystem.h"
40 #include "Dir.h"
|
41 msolomon 1.64 #ifndef PEGASUS_OS_TYPE_WINDOWS
42 #include <pwd.h>
43 #endif
44 #include <Pegasus/Common/Tracer.h>
|
45 marek 1.61 PEGASUS_NAMESPACE_BEGIN
46
47 // Clone the path as a C String but discard trailing slash if any:
48
49 static CString _clonePath(const String& path)
50 {
51 String clone = path;
52
53 if (clone.size() && clone[clone.size()-1] == '/')
54 clone.remove(clone.size()-1);
55
56 return clone.getCString();
57 }
58
59 Boolean FileSystem::exists(const String& path)
60 {
61 return System::exists(_clonePath(path));
62 }
63
64 Boolean FileSystem::getCurrentDirectory(String& path)
65 {
66 marek 1.61 path.clear();
67 char tmp[4096];
68
69 if (!System::getCurrentDirectory(tmp, sizeof(tmp) - 1))
70 return false;
71
72 path.append(tmp);
73 return true;
74 }
75
76 Boolean FileSystem::existsNoCase(const String& path, String& realPath)
77 {
78 #ifdef PEGASUS_OS_OS400
79 // The OS/400 file system is case insensitive, so just call exists( ).
80 // This is faster, but the main reason to do this is to
81 // avoid multi-threading problems with the IFS directory APIs
82 // (even though they claim to be threadsafe).
83 realPath = path;
84 return exists(path);
85 #else
86 realPath.clear();
87 marek 1.61 CString cpath = _clonePath(path);
88 const char* p = cpath;
89
90 const char* dirPath;
91 const char* fileName;
92 char* slash = (char *) strrchr(p, '/');
93
94 if (slash)
95 {
96 *slash = '\0';
97 fileName = slash + 1;
98 dirPath = p;
99
100 if (*fileName == '\0')
101 return false;
102 }
103 else
104 {
105 fileName = p;
106 dirPath = ".";
107 }
108 marek 1.61
109
110 for (Dir dir(dirPath); dir.more(); dir.next())
111 {
112 if (System::strcasecmp(fileName, dir.getName()) == 0)
113 {
114 if (strcmp(dirPath, ".") == 0)
115 realPath = dir.getName();
116 else
117 {
118 realPath = dirPath;
119 realPath.append('/');
120 realPath.append(dir.getName());
121 }
122 return true;
123 }
124 }
125
126 return false;
127 #endif
128 }
129 marek 1.61
130 Boolean FileSystem::canRead(const String& path)
131 {
132 return System::canRead(_clonePath(path));
133 }
134
135 Boolean FileSystem::canWrite(const String& path)
136 {
137 return System::canWrite(_clonePath(path));
138 }
139
140 Boolean FileSystem::getFileSize(const String& path, Uint32& size)
141 {
142 return System::getFileSize(_clonePath(path), size);
143 }
144
145 Boolean FileSystem::removeFile(const String& path)
146 {
147 return System::removeFile(_clonePath(path));
148 }
149
150 marek 1.61 void FileSystem::loadFileToMemory(
151 Buffer& array,
152 const String& fileName)
153 {
154 Uint32 fileSize;
155
156 if (!getFileSize(fileName, fileSize))
157 throw CannotOpenFile(fileName);
158
159 FILE* fp = fopen(fileName.getCString(), "rb");
160
161 if (fp == NULL)
162 throw CannotOpenFile(fileName);
163
164 array.reserveCapacity(fileSize);
165 char buffer[4096];
166 size_t n;
167
168 while ((n = fread(buffer, 1, sizeof(buffer), fp)) > 0)
169 array.append(buffer, static_cast<Uint32>(n));
170
171 marek 1.61 fclose(fp);
172 }
173
174 Boolean FileSystem::compareFiles(
175 const String& path1,
176 const String& path2)
177 {
178 Uint32 fileSize1;
179
180 if (!getFileSize(path1, fileSize1))
181 throw CannotOpenFile(path1);
182
183 Uint32 fileSize2;
184
185 if (!getFileSize(path2, fileSize2))
186 throw CannotOpenFile(path2);
187
188 if (fileSize1 != fileSize2)
189 return false;
190
191 FILE* fp1 = fopen(path1.getCString(), "rb");
192 marek 1.61
193 if (fp1 == NULL)
194 throw CannotOpenFile(path1);
195
196 FILE* fp2 = fopen(path2.getCString(), "rb");
197
198 if (fp2 == NULL)
199 {
200 fclose(fp1);
201 throw CannotOpenFile(path2);
202 }
203
204 int c1;
205 int c2;
206
207 while ((c1 = fgetc(fp1)) != EOF && (c2 = fgetc(fp2)) != EOF)
208 {
209 if (c1 != c2)
210 {
211 fclose(fp1);
212 fclose(fp2);
213 marek 1.61 return false;
214 }
215 }
216
217 fclose(fp1);
218 fclose(fp2);
219 return true;
220 }
221
222 Boolean FileSystem::renameFile(
223 const String& oldPath,
224 const String& newPath)
225 {
226 return System::renameFile(oldPath.getCString(), newPath.getCString());
227 }
228
229 Boolean FileSystem::copyFile(
230 const String& fromPath,
231 const String& toPath)
232 {
233 return System::copyFile(fromPath.getCString(), toPath.getCString());
234 marek 1.61 }
235
236 Boolean FileSystem::openNoCase(PEGASUS_STD(ifstream)& is, const String& path)
237 {
238 String realPath;
239
240 if (!existsNoCase(path, realPath))
241 return false;
242
243 is.open(_clonePath(realPath) PEGASUS_IOS_BINARY);
244
245 return !!is;
246 }
247
248 Boolean FileSystem::openNoCase(
249 PEGASUS_STD(fstream)& fs,
250 const String& path,
251 int mode)
252 {
253 String realPath;
254
255 marek 1.61 if (!existsNoCase(path, realPath))
256 return false;
257 #if defined(__GNUC__) && GCC_VERSION >= 30200
258 fs.open(_clonePath(realPath), PEGASUS_STD(ios_base::openmode)(mode));
259 #else
260 #if defined(PEGASUS_OS_OS400)
261 fs.open(_clonePath(realPath), mode, PEGASUS_STD(_CCSID_T(1208)) );
262 #else
263 fs.open(_clonePath(realPath), mode);
264 #endif
265 #endif
266 return !!fs;
267 }
268
269 Boolean FileSystem::isDirectory(const String& path)
270 {
271 return System::isDirectory(_clonePath(path));
272 }
273
274 Boolean FileSystem::changeDirectory(const String& path)
275 {
276 marek 1.61 return System::changeDirectory(_clonePath(path));
277 }
278
279 Boolean FileSystem::makeDirectory(const String& path)
280 {
281 return System::makeDirectory(_clonePath(path));
282 }
283
284 Boolean FileSystem::removeDirectory(const String& path)
285 {
286 return System::removeDirectory(_clonePath(path));
287 }
288
289 Boolean FileSystem::removeDirectoryHier(const String& path)
290 {
291 Array<String> fileList;
292
293 // Get contents of current directory
294
295 if (!FileSystem::getDirectoryContents(path,fileList))
296 return false;
297 marek 1.61
298 // for files-in-directory, delete or recall removedir
299
300 for (Uint32 i = 0, n = fileList.size(); i < n; i++)
301 {
302 String newPath = path; // extend path to subdir
303 newPath.append("/");
304 newPath.append(fileList[i]);
305
306 if (FileSystem::isDirectory(newPath))
307 {
308 // Recall ourselves with extended path
309 if (!FileSystem::removeDirectoryHier(newPath))
310 return false;
311 }
312
313 else
314 {
315 if (!FileSystem::removeFile(newPath))
316 return false;
317 }
318 marek 1.61 }
319
320 return removeDirectory(path);
321 }
322
323 //
324 // Get the file list in the directory into the
325 // array of strings provided
326 // @return The function should return false under these circumstances:
327 //
328 //
329 // 1. The directory does not exist.
330 // 2. The file exists but is not a directory.
331 // 3. The directory is inaccessible.
332 //
333 //
334 Boolean FileSystem::getDirectoryContents(
335 const String& path,
336 Array<String>& paths)
337 {
338 paths.clear();
339 marek 1.61
340 try
341 {
342 for (Dir dir(path); dir.more(); dir.next())
343 {
344 String name = dir.getName();
345
346 if (String::equal(name, ".") || String::equal(name, ".."))
347 continue;
348
349 paths.append(name);
350 }
351 return true;
352 }
353
354 // Catch the Dir exception
355 catch(CannotOpenDirectory&)
356 {
357 return false;
358 }
359 }
360 marek 1.61
361 Boolean FileSystem::isDirectoryEmpty(const String& path)
362 {
363 for (Dir dir(path); dir.more(); dir.next())
364 {
365 const char* name = dir.getName();
366
367 if (strcmp(name, ".") != 0 && strcmp(name, "..") != 0)
368 return false;
369 }
370
371 return true;
372 }
373
374 void FileSystem::translateSlashes(String& path)
375 {
376 for (Uint32 i = 0; i < path.size(); i++)
377 {
378 if (path[i] == '\\')
379 path[i] = '/';
380 }
381 marek 1.61 }
382
383 // Return the just the base name from the path.
|
384 kumpf 1.65 String FileSystem::extractFileName(const String& path)
|
385 marek 1.61 {
|
386 kumpf 1.65 AutoArrayPtr<char> p_path(new char[path.size() + 1]);
387 String basename = System::extract_file_name(
388 (const char*)path.getCString(), p_path.get());
|
389 marek 1.61
|
390 kumpf 1.65 return basename;
|
391 marek 1.61 }
392
393 // Return just the path to the file or directory into path
394 String FileSystem::extractFilePath(const String& path)
395 {
|
396 kumpf 1.65 AutoArrayPtr<char> p_path(new char[path.size() + 1]);
397 String newpath = System::extract_file_path(
398 (const char*)path.getCString(), p_path.get());
|
399 marek 1.61
|
400 kumpf 1.65 return newpath;
|
401 marek 1.61 }
402
403 // Changes file permissions on the given file.
404 Boolean FileSystem::changeFilePermissions(const String& path, mode_t mode)
405 {
406 #if defined(PEGASUS_OS_OS400)
407 // ATTN: If getCString() is modified to return UTF8, then handle the
408 // EBCDIC coversion in SystemUnix.cpp
409 CString tempPath = path.getCString();
410 #else
411 CString tempPath = path.getCString();
412 #endif
413
414 return System::changeFilePermissions(tempPath, mode);
415 }
416
|
417 kumpf 1.65 String FileSystem::getAbsoluteFileName(
418 const String& paths,
419 const String& filename)
420 {
421 Uint32 pos = 0;
422 Uint32 token = 0;
423 String path;
424 String root;
425 String tempPath = paths;
|
426 marek 1.61
|
427 kumpf 1.65 do
428 {
429 if ((pos = tempPath.find(FileSystem::getPathDelimiter())) ==
430 PEG_NOT_FOUND)
431 {
432 pos = tempPath.size();
433 token = 0;
|
434 marek 1.61 }
|
435 kumpf 1.65 else
436 {
437 token = 1;
|
438 marek 1.61 }
439 path = tempPath.subString(0, pos);
440 tempPath.remove(0,pos+token);
|
441 kumpf 1.65 if (FileSystem::exists(path + "/" + filename) == true)
442 {
443 root = path + "/" + filename;
444 break;
445 }
446 else
447 {
448 // cout << "File does not exist.\n";
449 }
450 } while (tempPath.size() > 0);
|
451 marek 1.61
|
452 kumpf 1.65 return root;
|
453 marek 1.61 }
454
455 String FileSystem::buildLibraryFileName(const String &libraryName)
456 {
457 String fileName;
458
459 //
460 // Add the necessary prefix and suffix to convert the library name to its
461 // corresponding file name.
462 //
|
463 david.dillard 1.62 #if defined(PEGASUS_OS_TYPE_WINDOWS)
|
464 marek 1.61 fileName = libraryName + String(".dll");
465 #elif defined(PEGASUS_PLATFORM_HPUX_PARISC_ACC)
466 fileName = String("lib") + libraryName + String(".sl");
467 #elif defined(PEGASUS_OS_OS400)
468 fileName = libraryName;
469 #elif defined(PEGASUS_OS_DARWIN)
470 fileName = String("lib") + libraryName + String(".dylib");
471 #elif defined(PEGASUS_OS_VMS)
472 fileName = String("lib") + libraryName;
473 #else
474 fileName = String("lib") + libraryName + String(".so");
475 #endif
476
477 return fileName;
478 }
479
480
481 Boolean GetLine(PEGASUS_STD(istream)& is, String& line)
482 {
483 line.clear();
484
485 marek 1.61 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 if (c == '\n')
500 break;
501
502 line.append(c);
503 }
504 #endif
505
506 marek 1.61 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 msolomon 1.64 #if defined(PEGASUS_PLATFORM_SOLARIS_SPARC_CC) || \
526 defined(PEGASUS_OS_HPUX) || \
527 defined (PEGASUS_OS_LINUX)
528
529 const unsigned int PWD_BUFF_SIZE = 1024;
|
530 kumpf 1.65 struct passwd pwd;
|
531 msolomon 1.64 struct passwd *result;
532 char pwdBuffer[PWD_BUFF_SIZE];
533
534 if(getpwnam_r(userName.getCString(), &pwd, pwdBuffer, PWD_BUFF_SIZE,
535 &userPasswd) != 0)
536 {
|
537 kumpf 1.65 userPasswd = (struct passwd*)NULL;
|
538 msolomon 1.64 }
539
540 #elif defined(PEGASUS_OS_OS400)
541 CString tempName = userName.getCString();
|
542 kumpf 1.65 const char* tmp = tempName;
|
543 msolomon 1.64 AtoE((char *)tmp);
544 userPasswd = getpwnam(tmp);
545 #else
546
547 userPasswd = getpwnam(userName.getCString());
548 #endif
549
|
550 kumpf 1.65 if (userPasswd == NULL)
|
551 msolomon 1.64 {
552 PEG_METHOD_EXIT();
|
553 kumpf 1.65 return false;
|
554 msolomon 1.64 }
555
556 #if defined(PEGASUS_OS_OS400)
557 CString tempPath = fileName.getCString();
558 const char * tmp1 = tempPath;
559 AtoE((char *)tmp1);
560 Sint32 ret = chown(tmp1, userPasswd->pw_uid, userPasswd->pw_gid);
561 #else
|
562 kumpf 1.65 Sint32 ret = chown(
563 fileName.getCString(), userPasswd->pw_uid, userPasswd->pw_gid);
|
564 msolomon 1.64 #endif
|
565 kumpf 1.65 if (ret == -1)
|
566 msolomon 1.64 {
567 PEG_METHOD_EXIT();
|
568 kumpf 1.65 return false;
|
569 msolomon 1.64 }
570
571 PEG_METHOD_EXIT();
572
|
573 kumpf 1.65 return true;
|
574 msolomon 1.64 #endif
575 }
|
576 marek 1.61 PEGASUS_NAMESPACE_END
|