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