1 martin 1.83 //%LICENSE////////////////////////////////////////////////////////////////
2 //
3 // Licensed to The Open Group (TOG) under one or more contributor license
4 // agreements. Refer to the OpenPegasusNOTICE.txt file distributed with
5 // this work for additional information regarding copyright ownership.
6 // Each contributor licenses this file to you under the OpenPegasus Open
7 // Source License; you may not use this file except in compliance with the
8 // License.
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining a
11 // copy of this software and associated documentation files (the "Software"),
12 // to deal in the Software without restriction, including without limitation
13 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 // and/or sell copies of the Software, and to permit persons to whom the
15 // Software is furnished to do so, subject to the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be included
18 // in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 martin 1.83 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
27 marek 1.61 //
|
28 martin 1.83 //////////////////////////////////////////////////////////////////////////
|
29 marek 1.61 //
30 //%/////////////////////////////////////////////////////////////////////////////
31
32 #include <iostream>
33 //#include <cstdio>
34 #include <Pegasus/Common/Config.h>
35 #include <Pegasus/Common/System.h>
36 #include <Pegasus/Common/AutoPtr.h>
|
37 mike 1.74 #include <Executor/Match.h>
|
38 marek 1.61 #include "FileSystem.h"
39 #include "Dir.h"
|
40 msolomon 1.64 #ifndef PEGASUS_OS_TYPE_WINDOWS
41 #include <pwd.h>
42 #endif
43 #include <Pegasus/Common/Tracer.h>
|
44 marek 1.61 PEGASUS_NAMESPACE_BEGIN
45
46 // Clone the path as a C String but discard trailing slash if any:
47
48 static CString _clonePath(const String& path)
49 {
50 String clone = path;
51
52 if (clone.size() && clone[clone.size()-1] == '/')
53 clone.remove(clone.size()-1);
54
55 return clone.getCString();
56 }
57
58 Boolean FileSystem::exists(const String& path)
59 {
60 return System::exists(_clonePath(path));
61 }
62
63 Boolean FileSystem::getCurrentDirectory(String& path)
64 {
65 marek 1.61 path.clear();
66 char tmp[4096];
67
68 if (!System::getCurrentDirectory(tmp, sizeof(tmp) - 1))
69 return false;
70
71 path.append(tmp);
72 return true;
73 }
74
75 Boolean FileSystem::existsNoCase(const String& path, String& realPath)
76 {
|
77 kumpf 1.80 #if !defined(PEGASUS_OS_VMS) && \
78 !defined(PEGASUS_OS_TYPE_WINDOWS) && \
79 !defined(PEGASUS_OS_DARWIN)
|
80 mike 1.75
|
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 r.kieninger 1.79 #elif defined(PEGASUS_PLATFORM_ZOS_ZSERIES64_IBM)
477 return String("64.so");
|
478 marek 1.61 #else
|
479 b.whiteley 1.76 return String(".so");
|
480 marek 1.61 #endif
481 }
482
|
483 kumpf 1.81 Boolean GetLine(PEGASUS_STD(istream)& is, Buffer& line)
|
484 marek 1.61 {
|
485 kumpf 1.81 const Uint32 buffersize = 1024;
486 Uint32 gcount = 0;
487
|
488 marek 1.61 line.clear();
489
|
490 kumpf 1.81 // Read the input line in chunks. A non-full buffer indicates the end of
491 // the line has been reached.
492 do
493 {
494 char input[buffersize];
|
495 marek 1.61
|
496 kumpf 1.81 // This reads up to buffersize-1 char, but stops before consuming
497 // a newline character ('\n').
498 is.get(input, buffersize);
|
499 marek 1.61
|
500 kumpf 1.81 gcount = (Uint32)is.gcount();
501 line.append(input, gcount);
|
502 marek 1.61
|
503 kumpf 1.81 if (is.rdstate() & PEGASUS_STD(istream)::failbit)
504 {
505 // It is okay if we encounter the newline character without reading
506 // data.
507 is.clear();
|
508 marek 1.61 break;
|
509 kumpf 1.81 }
510 } while (gcount == buffersize-1);
|
511 marek 1.61
|
512 kumpf 1.81 if (!is.eof())
513 {
514 // we need to consume the '\n', because get() doesn't
515 char c = 0;
516 is.get(c);
|
517 marek 1.61 }
518
|
519 kumpf 1.81 return !!is;
|
520 marek 1.61 }
521
|
522 msolomon 1.64 //
523 // changes the file owner to one specified
524 //
|
525 kumpf 1.65 Boolean FileSystem::changeFileOwner(
526 const String& fileName,
527 const String& userName)
|
528 msolomon 1.64 {
529 #if defined(PEGASUS_OS_TYPE_WINDOWS)
530
531 return true;
532
533 #else
534
535 PEG_METHOD_ENTER(TRC_AUTHENTICATION, "FileSystem::changeFileOwner()");
536
|
537 kumpf 1.65 struct passwd* userPasswd;
|
538 karl 1.78 #if defined(PEGASUS_OS_SOLARIS) || \
|
539 msolomon 1.64 defined(PEGASUS_OS_HPUX) || \
|
540 carson.hovey 1.72 defined(PEGASUS_OS_LINUX) || \
|
541 cheng.sp 1.82 defined (PEGASUS_OS_VMS) || \
542 defined (PEGASUS_OS_AIX)
|
543 msolomon 1.64
544 const unsigned int PWD_BUFF_SIZE = 1024;
|
545 kumpf 1.65 struct passwd pwd;
|
546 msolomon 1.64 char pwdBuffer[PWD_BUFF_SIZE];
547
|
548 kumpf 1.66 if (getpwnam_r(userName.getCString(), &pwd, pwdBuffer, PWD_BUFF_SIZE,
|
549 msolomon 1.64 &userPasswd) != 0)
550 {
|
551 kumpf 1.65 userPasswd = (struct passwd*)NULL;
|
552 msolomon 1.64 }
553
554 #else
555
556 userPasswd = getpwnam(userName.getCString());
557 #endif
558
|
559 kumpf 1.65 if (userPasswd == NULL)
|
560 msolomon 1.64 {
561 PEG_METHOD_EXIT();
|
562 kumpf 1.65 return false;
|
563 msolomon 1.64 }
564
|
565 kumpf 1.65 Sint32 ret = chown(
566 fileName.getCString(), userPasswd->pw_uid, userPasswd->pw_gid);
|
567 ouyang.jian 1.71
|
568 kumpf 1.65 if (ret == -1)
|
569 msolomon 1.64 {
570 PEG_METHOD_EXIT();
|
571 kumpf 1.65 return false;
|
572 msolomon 1.64 }
573
574 PEG_METHOD_EXIT();
575
|
576 kumpf 1.65 return true;
|
577 msolomon 1.64 #endif
578 }
|
579 mateus.baur 1.70
580 void FileSystem::syncWithDirectoryUpdates(PEGASUS_STD(fstream)& fs)
581 {
582 #if defined(PEGASUS_OS_HPUX)
583 // Writes the data from the iostream buffers to the OS buffers
584 fs.flush();
585 // Writes the data from the OS buffers to the disk
586 fsync(fs.rdbuf()->fd());
587 #endif
588 }
589
|
590 mike 1.74 Boolean FileSystem::glob(
591 const String& path,
592 const String& pattern_,
593 Array<String>& filenames)
594 {
595 filenames.clear();
596
597 try
598 {
599 CString pattern(pattern_.getCString());
600
601 for (Dir dir(path); dir.more(); dir.next())
602 {
603 const char* name = dir.getName();
604
605 if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0)
606 continue;
607
608 if (Match(pattern, name) == 0)
609 filenames.append(name);
610 }
611 mike 1.74 }
612 catch (CannotOpenDirectory&)
613 {
614 return false;
615 }
616
617 return true;
618 }
619
|
620 marek 1.61 PEGASUS_NAMESPACE_END
|