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