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

  1 marek 1.61 //%2005////////////////////////////////////////////////////////////////////////
  2            //
  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            //
 12            // Permission is hereby granted, free of charge, to any person obtaining a copy
 13            // of this software and associated documentation files (the "Software"), to
 14            // deal in the Software without restriction, including without limitation the
 15            // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 16            // sell copies of the Software, and to permit persons to whom the Software is
 17            // furnished to do so, subject to the following conditions:
 18            // 
 19            // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
 20            // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
 21            // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 22 marek 1.61 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 23            // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 24            // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 25            // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 26            // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 27            //
 28            //==============================================================================
 29            //
 30            // Author: Mike Brasher (mbrasher@bmc.com)
 31            //
 32            // Modified By:
 33            //         Ramnath Ravindran(Ramnath.Ravindran@compaq.com)
 34            //         Amit K Arora, IBM (amita@in.ibm.com)
 35            //         Roger Kumpf, Hewlett-Packard Company (roger_kumpf@hp.com)
 36            //         Sean Keenan, Hewlett-Packard Company (sean.keenan@hp.com)
 37            //         David Dillard, VERITAS Software Corp.
 38            //             (david.dillard@veritas.com)
 39            //         Sean Keenan, Hewlett-Packard Company (sean.keenan@hp.com)
 40            //
 41            //%/////////////////////////////////////////////////////////////////////////////
 42            
 43 marek 1.61 #include <iostream>
 44            //#include <cstdio>
 45            #include <Pegasus/Common/Config.h>
 46            #include <Pegasus/Common/System.h>
 47            #include <Pegasus/Common/AutoPtr.h>
 48            #include "FileSystem.h"
 49            #include "Dir.h"
 50            
 51            PEGASUS_NAMESPACE_BEGIN
 52            
 53            // Clone the path as a C String but discard trailing slash if any:
 54            
 55            static CString _clonePath(const String& path)
 56            {
 57                String clone = path;
 58            
 59                if (clone.size() && clone[clone.size()-1] == '/')
 60                    clone.remove(clone.size()-1);
 61            
 62                return clone.getCString();
 63            }
 64 marek 1.61 
 65            Boolean FileSystem::exists(const String& path)
 66            {
 67                return System::exists(_clonePath(path));
 68            }
 69            
 70            Boolean FileSystem::getCurrentDirectory(String& path)
 71            {
 72                path.clear();
 73                char tmp[4096];
 74            
 75                if (!System::getCurrentDirectory(tmp, sizeof(tmp) - 1))
 76                    return false;
 77            
 78                path.append(tmp);
 79                return true;
 80            }
 81            
 82            Boolean FileSystem::existsNoCase(const String& path, String& realPath)
 83            {
 84            #ifdef PEGASUS_OS_OS400
 85 marek 1.61     // The OS/400 file system is case insensitive, so just call exists( ).
 86                // This is faster, but the main reason to do this is to
 87                // avoid multi-threading problems with the IFS directory APIs
 88                // (even though they claim to be threadsafe).
 89                realPath = path;
 90                return exists(path);
 91            #else
 92                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 marek 1.61         if (*fileName == '\0')
107                        return false;
108                }
109                else
110                {
111                    fileName = p;
112                    dirPath = ".";
113                }
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 marek 1.61             }
128                        return true;
129                    }
130                }
131            
132                return false;
133            #endif
134            }
135            
136            Boolean FileSystem::canRead(const String& path)
137            {
138                return System::canRead(_clonePath(path));
139            }
140            
141            Boolean FileSystem::canWrite(const String& path)
142            {
143                return System::canWrite(_clonePath(path));
144            }
145            
146            Boolean FileSystem::getFileSize(const String& path, Uint32& size)
147            {
148 marek 1.61     return System::getFileSize(_clonePath(path), size);
149            }
150            
151            Boolean FileSystem::removeFile(const String& path)
152            {
153                return System::removeFile(_clonePath(path));
154            }
155            
156            void FileSystem::loadFileToMemory(
157                Buffer& array,
158                const String& fileName)
159            {
160                Uint32 fileSize;
161            
162                if (!getFileSize(fileName, fileSize))
163                    throw CannotOpenFile(fileName);
164            
165                FILE* fp = fopen(fileName.getCString(), "rb");
166            
167                if (fp == NULL)
168                    throw CannotOpenFile(fileName);
169 marek 1.61 
170                array.reserveCapacity(fileSize);
171                char buffer[4096];
172                size_t n;
173            
174                while ((n = fread(buffer, 1, sizeof(buffer), fp)) > 0)
175                    array.append(buffer, static_cast<Uint32>(n));
176            
177                fclose(fp);
178            }
179            
180            Boolean FileSystem::compareFiles(
181                const String& path1,
182                const String& path2)
183            {
184                Uint32 fileSize1;
185            
186                if (!getFileSize(path1, fileSize1))
187                    throw CannotOpenFile(path1);
188            
189                Uint32 fileSize2;
190 marek 1.61 
191                if (!getFileSize(path2, fileSize2))
192                    throw CannotOpenFile(path2);
193            
194                if (fileSize1 != fileSize2)
195                    return false;
196            
197                FILE* fp1 = fopen(path1.getCString(), "rb");
198            
199                if (fp1 == NULL)
200                    throw CannotOpenFile(path1);
201            
202                FILE* fp2 = fopen(path2.getCString(), "rb");
203            
204                if (fp2 == NULL)
205                {
206                    fclose(fp1);
207                    throw CannotOpenFile(path2);
208                }
209            
210                int c1;
211 marek 1.61     int c2;
212            
213                while ((c1 = fgetc(fp1)) != EOF && (c2 = fgetc(fp2)) != EOF)
214                {
215                    if (c1 != c2)
216                    {
217                        fclose(fp1);
218                        fclose(fp2);
219                        return false;
220                    }
221                }
222            
223                fclose(fp1);
224                fclose(fp2);
225                return true;
226            }
227            
228            Boolean FileSystem::renameFile(
229                const String& oldPath,
230                const String& newPath)
231            {
232 marek 1.61     return System::renameFile(oldPath.getCString(), newPath.getCString());
233            }
234            
235            Boolean FileSystem::copyFile(
236                const String& fromPath,
237                const String& toPath)
238            {
239                return System::copyFile(fromPath.getCString(), toPath.getCString());
240            }
241            
242            Boolean FileSystem::openNoCase(PEGASUS_STD(ifstream)& is, const String& path)
243            {
244                String realPath;
245            
246                if (!existsNoCase(path, realPath))
247                    return false;
248            
249                is.open(_clonePath(realPath) PEGASUS_IOS_BINARY);
250            
251                return !!is;
252            }
253 marek 1.61 
254            Boolean FileSystem::openNoCase(
255                PEGASUS_STD(fstream)& fs, 
256                const String& path,
257                int mode)
258            {
259                String realPath;
260            
261                if (!existsNoCase(path, realPath))
262                    return false;
263            #if defined(__GNUC__) && GCC_VERSION >= 30200
264                fs.open(_clonePath(realPath), PEGASUS_STD(ios_base::openmode)(mode));
265            #else
266            #if defined(PEGASUS_OS_OS400)
267                fs.open(_clonePath(realPath), mode, PEGASUS_STD(_CCSID_T(1208)) );
268            #else
269                fs.open(_clonePath(realPath), mode);
270            #endif
271            #endif
272                return !!fs;
273            }
274 marek 1.61 
275            Boolean FileSystem::isDirectory(const String& path)
276            {
277                return System::isDirectory(_clonePath(path));
278            }
279            
280            Boolean FileSystem::changeDirectory(const String& path)
281            {
282                return System::changeDirectory(_clonePath(path));
283            }
284            
285            Boolean FileSystem::makeDirectory(const String& path)
286            {
287                return System::makeDirectory(_clonePath(path));
288            }
289            
290            Boolean FileSystem::removeDirectory(const String& path)
291            {
292                return System::removeDirectory(_clonePath(path));
293            }
294            
295 marek 1.61 Boolean FileSystem::removeDirectoryHier(const String& path)
296            {
297                Array<String> fileList;
298            
299                // Get contents of current directory
300            
301                if (!FileSystem::getDirectoryContents(path,fileList))
302                    return false;
303            
304                // for files-in-directory, delete or recall removedir
305            
306                for (Uint32 i = 0, n = fileList.size(); i < n; i++)
307                {   
308                    String newPath = path;   // extend path to subdir
309                    newPath.append("/");
310                    newPath.append(fileList[i]);
311                    
312                    if (FileSystem::isDirectory(newPath))
313                    {
314                        // Recall ourselves with extended path
315                        if (!FileSystem::removeDirectoryHier(newPath))
316 marek 1.61                 return false; 
317                    }
318            
319                    else
320                    {
321                      if (!FileSystem::removeFile(newPath))
322                            return false;
323                    }
324                }
325            
326                return removeDirectory(path);       
327            }
328            
329            //
330            //  Get the file list in the directory into the
331            //  array of strings provided
332            //  @return The function should return false under these circumstances:
333            //
334            //
335            //  1. The directory does not exist.
336            //  2. The file exists but is not a directory.
337 marek 1.61 //  3. The directory is inaccessible.
338            //
339            //
340            Boolean FileSystem::getDirectoryContents(
341                const String& path,
342                Array<String>& paths)
343            {
344                paths.clear();
345            
346                try
347                { 
348                    for (Dir dir(path); dir.more(); dir.next())
349                    {
350                        String name = dir.getName();
351            
352                        if (String::equal(name, ".") || String::equal(name, ".."))
353                            continue;
354            
355                        paths.append(name);
356                    }
357                    return true;
358 marek 1.61     }
359            
360                // Catch the Dir exception
361                catch(CannotOpenDirectory&)
362                {
363                    return false;
364                }
365            }
366            
367            Boolean FileSystem::isDirectoryEmpty(const String& path)
368            {
369                for (Dir dir(path); dir.more(); dir.next())
370                {
371                    const char* name = dir.getName();
372            
373                    if (strcmp(name, ".") != 0 && strcmp(name, "..") != 0)
374                        return false;
375                }
376            
377                return true;
378            }
379 marek 1.61 
380            void FileSystem::translateSlashes(String& path)
381            {
382                for (Uint32 i = 0; i < path.size(); i++)
383                {
384                    if (path[i] == '\\')
385                        path[i] = '/';
386                }
387            }
388            
389            // Return the just the base name from the path.
390            String  FileSystem::extractFileName(const String& path)
391            {
392              AutoArrayPtr<char> p_path(new char[path.size() + 1]);
393              String basename = System::extract_file_name((const char *)path.getCString(), p_path.get());
394              
395              return basename;
396            }
397            
398            // Return just the path to the file or directory into path
399            String FileSystem::extractFilePath(const String& path)
400 marek 1.61 {
401              AutoArrayPtr<char> p_path(new char[path.size() + 1]);
402              String newpath = System::extract_file_path((const char *)path.getCString(), p_path.get());
403              
404              return newpath;
405            }
406            
407            // Changes file permissions on the given file.
408            Boolean FileSystem::changeFilePermissions(const String& path, mode_t mode)
409            {
410            #if defined(PEGASUS_OS_OS400)
411                // ATTN: If getCString() is modified to return UTF8, then handle the 
412                //       EBCDIC coversion in SystemUnix.cpp
413                CString tempPath = path.getCString();
414            #else
415                CString tempPath = path.getCString();
416            #endif
417            
418                return System::changeFilePermissions(tempPath, mode);
419            }
420            
421 marek 1.61 String FileSystem::getAbsoluteFileName(const String &paths, const String &filename) {
422            
423              Uint32 pos =0;
424              Uint32 token=0;
425              String path = String::EMPTY;
426              String root = String::EMPTY;
427              String tempPath = paths;
428            
429            
430              do {
431                if (( pos = tempPath.find(FileSystem::getPathDelimiter())) == PEG_NOT_FOUND) {
432                            pos = tempPath.size();
433                            token = 0;
434                    }
435                    else {
436                            token = 1;
437                    }
438                    path = tempPath.subString(0, pos);
439                    tempPath.remove(0,pos+token);
440                    if (FileSystem::exists( path + "/" + filename) == true) {
441                      root = path + "/" + filename;
442 marek 1.61           break;
443                    } else
444                      {
445                      //  cout << "File does not exist.\n";
446                      }
447              } while (tempPath.size() > 0);
448            
449              return root;
450            }
451            
452            String FileSystem::buildLibraryFileName(const String &libraryName)
453            {
454                String fileName;
455            
456                //
457                // Add the necessary prefix and suffix to convert the library name to its
458                // corresponding file name.
459                //
460            #if defined(PEGASUS_PLATFORM_WIN32_IX86_MSVC)
461                fileName = libraryName + String(".dll");
462            #elif defined(PEGASUS_PLATFORM_HPUX_PARISC_ACC)
463 marek 1.61     fileName = String("lib") + libraryName + String(".sl");
464            #elif defined(PEGASUS_OS_OS400)
465                fileName = libraryName;
466            #elif defined(PEGASUS_OS_DARWIN)
467                fileName = String("lib") + libraryName + String(".dylib");
468            #elif defined(PEGASUS_OS_VMS)
469                fileName = String("lib") + libraryName;
470            #else
471                fileName = String("lib") + libraryName + String(".so");
472            #endif
473            
474                return fileName;
475            }
476            
477            
478            Boolean GetLine(PEGASUS_STD(istream)& is, String& line)
479            {
480                line.clear();
481            
482                Boolean gotChar = false;
483            #ifdef PEGASUS_PLATFORM_ZOS_ZSERIES_IBM
484 marek 1.61     char input[1000];
485                is.getline(input,1000);
486                line.assign(input);
487            
488                gotChar = !(is.rdstate() & PEGASUS_STD(istream)::failbit);
489            #else
490                char c;
491            
492                while (is.get(c))
493                {
494                    gotChar = true;
495            
496                    if (c == '\n')
497                        break;
498            
499                    line.append(c);
500                }
501            #endif
502            
503                return gotChar;
504            }
505 marek 1.61 
506            PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2