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

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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2