(file) Return to FileSystem.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2