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

  1 karl  1.27 //%2006////////////////////////////////////////////////////////////////////////
  2 mike  1.2  //
  3 karl  1.17 // 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 karl  1.8  // IBM Corp.; EMC Corporation, The Open Group.
  7 karl  1.17 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
  8            // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
  9 karl  1.19 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 10            // EMC Corporation; VERITAS Software Corporation; The Open Group.
 11 karl  1.27 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 12            // EMC Corporation; Symantec Corporation; The Open Group.
 13 mike  1.2  //
 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 kumpf 1.4  // 
 21 mike  1.2  // 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: Sushma Fernandes, Hewlett-Packard Company (sushma_fernandes@hp.com)
 33            //
 34            // Modified By: Rudy Schuet (rudy.schuet@compaq.com) 11/12/01
 35            //              added nsk platform support
 36 a.arora 1.16 //              Amit K Arora, IBM (amita@in.ibm.com) for Bug#1527
 37 kumpf   1.18 //              Roger Kumpf, Hewlett-Packard Company (roger_kumpf@hp.com)
 38 gs.keenan 1.20 //              Sean Keenan, Hewlett-Packard Company (sean.keenan@hp.com)
 39 mike      1.2  //
 40                //%/////////////////////////////////////////////////////////////////////////////
 41                
 42 david.dillard 1.23 //#include <fstream>
 43                    //#include <iostream>
 44 mike          1.2  #include <Pegasus/Common/FileSystem.h>
 45                    #include <Pegasus/Common/TraceFileHandler.h>
 46                    
 47                    #if defined(PEGASUS_OS_TYPE_WINDOWS)
 48                    # include <Pegasus/Common/TraceFileHandlerWindows.cpp>
 49 kumpf         1.30 #elif defined(PEGASUS_OS_TYPE_UNIX) || defined(PEGASUS_OS_VMS)
 50 mike          1.29 # include <Pegasus/Common/TraceFileHandlerPOSIX.cpp>
 51 mike          1.2  #else
 52                    # error "Unsupported platform"
 53                    #endif
 54                    
 55                    
 56                    PEGASUS_USING_STD;
 57                    
 58                    PEGASUS_NAMESPACE_BEGIN
 59                    
 60                    ////////////////////////////////////////////////////////////////////////////////
 61                    //  Constructs TraceFileHandler
 62                    ////////////////////////////////////////////////////////////////////////////////
 63                    
 64                    TraceFileHandler::TraceFileHandler () 
 65                    {
 66 kumpf         1.3      _fileName = 0;
 67 mike          1.2      _fileHandle = 0;
 68                        _wroteToLog = false;
 69 a.arora       1.16 #ifdef PEGASUS_PLATFORM_LINUX_GENERIC_GNU
 70                        _baseFileName = 0;
 71                        _fileCount = 0;
 72                    #endif
 73 mike          1.2  }
 74                    
 75                    ////////////////////////////////////////////////////////////////////////////////
 76                    //  Destructs TraceFileHandler
 77                    ////////////////////////////////////////////////////////////////////////////////
 78                    
 79                    TraceFileHandler::~TraceFileHandler () 
 80                    {
 81                        // Close the File 
 82                        if (_fileHandle)
 83                        {
 84                            fclose(_fileHandle);
 85                        }
 86 kumpf         1.28     delete [] _fileName;
 87 a.arora       1.16 #ifdef PEGASUS_PLATFORM_LINUX_GENERIC_GNU
 88 kumpf         1.28     delete [] _baseFileName;
 89 a.arora       1.16 #endif
 90 mike          1.2  }
 91                    
 92                    ////////////////////////////////////////////////////////////////////////////////
 93                    //  Sets the filename to the given filename and opens the file in append
 94                    //  mode
 95                    ////////////////////////////////////////////////////////////////////////////////
 96                    
 97                    Uint32 TraceFileHandler::setFileName(const char* fileName)
 98                    {
 99 kumpf         1.18     // If a file is already open, close it
100                        if (_fileHandle)
101                        {
102                            fclose(_fileHandle);
103                            _fileHandle = 0;
104                        }
105                    
106                        delete [] _fileName;
107                        _fileName = 0;
108                    #ifdef PEGASUS_PLATFORM_LINUX_GENERIC_GNU
109                        delete [] _baseFileName;
110                        _baseFileName = 0;
111                    #endif
112                    
113 mike          1.2      if (!isValidFilePath(fileName))
114                        {
115                    	return 1;
116                        }
117 kumpf         1.18     _fileHandle = _openFile(fileName);
118 mike          1.2      if (!_fileHandle)
119                        {
120 kumpf         1.18         return 1;
121 mike          1.2      }
122 kumpf         1.18 
123                        _fileName = new char[strlen(fileName)+1];
124                        strcpy(_fileName, fileName);
125 a.arora       1.16 #ifdef PEGASUS_PLATFORM_LINUX_GENERIC_GNU
126 kumpf         1.18     _baseFileName = new char[strlen(fileName)+1];
127                        strcpy(_baseFileName, fileName);
128 a.arora       1.16 #endif
129 kumpf         1.18 
130                        return 0;
131                    }
132                    
133                    FILE* TraceFileHandler::_openFile(const char* fileName)
134                    {
135 gs.keenan     1.25 #ifdef PEGASUS_OS_VMS
136                    //    FILE* fileHandle = fopen(fileName,"a+", "shr=get,put,upd");
137                        FILE* fileHandle = fopen(fileName,"w", "shr=get,put,upd");
138                    #else
139 kumpf         1.18     FILE* fileHandle = fopen(fileName,"a+");
140 gs.keenan     1.25 #endif
141 kumpf         1.18     if (!fileHandle)
142                        {
143                            // Unable to open file, log a message
144 kumpf         1.24         Logger::put_l(Logger::DEBUG_LOG, System::CIMSERVER, Logger::WARNING,
145 kumpf         1.18             "Common.TraceFileHandler.FAILED_TO_OPEN_FILE",
146                                "Failed to open file $0", fileName);
147                            return 0;
148 mike          1.2      }
149 kumpf         1.9  
150                        //
151 kumpf         1.24     // Verify that the file has the correct owner
152                        //
153                        if (!System::verifyFileOwnership(fileName))
154                        {
155                            Logger::put_l(Logger::ERROR_LOG, System::CIMSERVER, Logger::WARNING,
156                               "Common.TraceFileHandler.UNEXPECTED_FILE_OWNER",
157                               "File $0 is not owned by user $1.", fileName,
158                               System::getEffectiveUserName());
159                            fclose(fileHandle);
160                            return 0;
161                        }
162                    
163                        //
164 kumpf         1.18     // Set the file permissions to 0600
165 marek         1.12     //
166 chuck         1.15 #if !defined(PEGASUS_OS_TYPE_WINDOWS)
167 kumpf         1.18     if (!FileSystem::changeFilePermissions(
168                                String(fileName), (S_IRUSR|S_IWUSR)) )
169 marek         1.12 #else
170 kumpf         1.18     if (!FileSystem::changeFilePermissions(
171                                String(fileName), (_S_IREAD|_S_IWRITE)) )
172 marek         1.12 #endif
173 kumpf         1.9      {
174 kumpf         1.24         Logger::put_l(Logger::DEBUG_LOG, System::CIMSERVER, Logger::WARNING,
175 kumpf         1.9             "Common.TraceFileHandler.FAILED_TO_SET_FILE_PERMISSIONS",
176 kumpf         1.18            "Failed to set permissions on file $0", fileName);
177                            fclose(fileHandle);
178                            return 0;
179                        }
180                    
181                        return fileHandle;
182 mike          1.2  }
183                    
184                    Boolean TraceFileHandler::isValidFilePath(const char* filePath)
185                    {
186                        String fileName = String(filePath);
187                    
188                        // Check if the file path is a directory
189                        FileSystem::translateSlashes(fileName);
190                        if (FileSystem::isDirectory(fileName))
191                        {
192                    	return 0;
193                        }
194                    
195                        // Check if the file exists and is writable
196                        if (FileSystem::exists(fileName))
197                        {
198                            if (!FileSystem::canWrite(fileName))
199                            {
200                    	    return 0;
201                            }
202                    	else
203 mike          1.2  	{
204                    	    return 1;
205                            }
206                        }
207                        else
208                        {
209                            // Check if directory is writable
210 kumpf         1.6          Uint32 index = fileName.reverseFind('/');
211 mike          1.2  
212 kumpf         1.6          if (index != PEG_NOT_FOUND)
213 mike          1.2  	{
214 kumpf         1.6              String dirName = fileName.subString(0,index);
215 mike          1.2              if (!FileSystem::isDirectory(dirName))
216                                {
217                    	        return 0;
218                                }
219                                if (!FileSystem::canWrite(dirName) )
220                                {
221                    		return 0;
222                                }
223                    	    else
224                    	    {
225                    		return 1;
226                                }
227                            }
228                    	else
229                            {
230                                String currentDir;
231                    
232                                // Check if there is permission to write in the
233                                // current working directory
234                                FileSystem::getCurrentDirectory(currentDir);
235                    
236 mike          1.2              if (!FileSystem::canWrite(currentDir))
237                                {
238                    		return 0;
239                                }
240                    	    else
241                    	    {
242                    		return 1;
243                                }
244                            }
245                        }
246 carson.hovey  1.22     PEGASUS_UNREACHABLE(return 1;)
247 mike          1.2  }
248                    PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2