1 karl 1.8 //%2003////////////////////////////////////////////////////////////////////////
|
2 mike 1.2 //
|
3 karl 1.8 // 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 mike 1.2 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to
10 // deal in the Software without restriction, including without limitation the
11 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12 // sell copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
|
14 kumpf 1.4 //
|
15 mike 1.2 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
16 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
17 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
18 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
19 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 //
24 //==============================================================================
25 //
26 // Author: Sushma Fernandes, Hewlett-Packard Company (sushma_fernandes@hp.com)
27 //
28 // Modified By: Rudy Schuet (rudy.schuet@compaq.com) 11/12/01
29 // added nsk platform support
30 //
31 //%/////////////////////////////////////////////////////////////////////////////
32
33 #include <fstream>
34 #include <iostream>
35 #include <Pegasus/Common/FileSystem.h>
36 mike 1.2 #include <Pegasus/Common/TraceFileHandler.h>
37
38 #if defined(PEGASUS_OS_TYPE_WINDOWS)
39 # include <Pegasus/Common/TraceFileHandlerWindows.cpp>
40 #elif defined(PEGASUS_OS_TYPE_UNIX)
41 # include <Pegasus/Common/TraceFileHandlerUnix.cpp>
42 #elif defined(PEGASUS_OS_TYPE_NSK)
43 # include <Pegasus/Common/TraceFileHandlerNsk.cpp>
44 #else
45 # error "Unsupported platform"
46 #endif
47
48
49 PEGASUS_USING_STD;
50
51 PEGASUS_NAMESPACE_BEGIN
52
53 ////////////////////////////////////////////////////////////////////////////////
54 // Constructs TraceFileHandler
55 ////////////////////////////////////////////////////////////////////////////////
56
57 mike 1.2 TraceFileHandler::TraceFileHandler ()
58 {
|
59 kumpf 1.3 _fileName = 0;
|
60 mike 1.2 _fileHandle = 0;
61 _wroteToLog = false;
62 }
63
64 ////////////////////////////////////////////////////////////////////////////////
65 // Destructs TraceFileHandler
66 ////////////////////////////////////////////////////////////////////////////////
67
68 TraceFileHandler::~TraceFileHandler ()
69 {
70 // Close the File
71 if (_fileHandle)
72 {
73 fclose(_fileHandle);
74 }
|
75 kumpf 1.3 if (_fileName)
76 {
77 delete []_fileName;
78 }
|
79 mike 1.2 }
80
81 ////////////////////////////////////////////////////////////////////////////////
82 // Sets the filename to the given filename and opens the file in append
83 // mode
84 ////////////////////////////////////////////////////////////////////////////////
85
86 Uint32 TraceFileHandler::setFileName(const char* fileName)
87 {
88 if (!isValidFilePath(fileName))
89 {
90 return 1;
91 }
92 // Check if a file is already open, if so close it
93 if (_fileHandle)
94 {
95 fclose(_fileHandle);
96 }
97
98 // Now open the file
99 _fileHandle = fopen(fileName,"a+");
100 mike 1.2 if (!_fileHandle)
101 {
102 // Unable to open file, log a message
|
103 humberto 1.7 //l10n
104 //Logger::put(Logger::DEBUG_LOG,"Tracer",Logger::WARNING,
105 //"Failed to open File $0",fileName);
106 Logger::put_l(Logger::DEBUG_LOG,"Tracer",Logger::WARNING,
107 "Common.TraceFileHandler.FAILED_TO_OPEN_FILE",
|
108 mike 1.2 "Failed to open File $0",fileName);
109 return 1;
110 }
111 else
112 {
|
113 kumpf 1.3 if (_fileName)
114 {
115 delete [] _fileName;
116 }
|
117 mike 1.2 _fileName = new char [strlen(fileName)+1];
118 strcpy (_fileName,fileName);
119 }
|
120 kumpf 1.9
121 //
|
122 chuck 1.15 // Set permissions on the trace file to 0600
|
123 marek 1.12 //
|
124 chuck 1.15 #if !defined(PEGASUS_OS_TYPE_WINDOWS)
|
125 chuck 1.14 if ( !FileSystem::changeFilePermissions(String(_fileName), (S_IRUSR|S_IWUSR)) )
|
126 marek 1.12 #else
|
127 s.hills 1.13 if ( !FileSystem::changeFilePermissions(String(_fileName), (_S_IREAD | _S_IWRITE )) )
|
128 marek 1.12 #endif
|
129 kumpf 1.9 {
130 Logger::put_l(Logger::DEBUG_LOG,"Tracer",Logger::WARNING,
131 "Common.TraceFileHandler.FAILED_TO_SET_FILE_PERMISSIONS",
132 "Failed to set permissions on file $0", _fileName);
133 return 1;
134 }
135
|
136 mike 1.2 return 0;
137 }
138
139 Boolean TraceFileHandler::isValidFilePath(const char* filePath)
140 {
141 String fileName = String(filePath);
142
143 // Check if the file path is a directory
144 FileSystem::translateSlashes(fileName);
145 if (FileSystem::isDirectory(fileName))
146 {
147 return 0;
148 }
149
150 // Check if the file exists and is writable
151 if (FileSystem::exists(fileName))
152 {
153 if (!FileSystem::canWrite(fileName))
154 {
155 return 0;
156 }
157 mike 1.2 else
158 {
159 return 1;
160 }
161 }
162 else
163 {
164 // Check if directory is writable
|
165 kumpf 1.6 Uint32 index = fileName.reverseFind('/');
|
166 mike 1.2
|
167 kumpf 1.6 if (index != PEG_NOT_FOUND)
|
168 mike 1.2 {
|
169 kumpf 1.6 String dirName = fileName.subString(0,index);
|
170 mike 1.2 if (!FileSystem::isDirectory(dirName))
171 {
172 return 0;
173 }
174 if (!FileSystem::canWrite(dirName) )
175 {
176 return 0;
177 }
178 else
179 {
180 return 1;
181 }
182 }
183 else
184 {
185 String currentDir;
186
187 // Check if there is permission to write in the
188 // current working directory
189 FileSystem::getCurrentDirectory(currentDir);
190
191 mike 1.2 if (!FileSystem::canWrite(currentDir))
192 {
193 return 0;
194 }
195 else
196 {
197 return 1;
198 }
199 }
200 }
201 return 1;
202 }
203 PEGASUS_NAMESPACE_END
|