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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2