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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2