1 mike 1.2 //%/////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2000, 2001 BMC Software, Hewlett-Packard Company, IBM,
4 // The Open Group, Tivoli Systems, Compaq Computer Corporation
5 //
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 //
13 // 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 mike 1.2 //==============================================================================
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 #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 mike 1.2 # error "Unsupported platform"
44 #endif
45
46
47 PEGASUS_USING_STD;
48
49 PEGASUS_NAMESPACE_BEGIN
50
51 ////////////////////////////////////////////////////////////////////////////////
52 // Constructs TraceFileHandler
53 ////////////////////////////////////////////////////////////////////////////////
54
55 TraceFileHandler::TraceFileHandler ()
56 {
|
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 {
|
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 String dirSeparator("/");
144
145 Uint32 pos = fileName.reverseFind(*(dirSeparator.getData()));
146
147 if (pos != PEG_NOT_FOUND)
148 {
149 String dirName = fileName.subString(0,pos);
150 if (!FileSystem::isDirectory(dirName))
151 {
152 return 0;
153 mike 1.2 }
154 if (!FileSystem::canWrite(dirName) )
155 {
156 return 0;
157 }
158 else
159 {
160 return 1;
161 }
162 }
163 else
164 {
165 String currentDir;
166
167 // Check if there is permission to write in the
168 // current working directory
169 FileSystem::getCurrentDirectory(currentDir);
170
171 if (!FileSystem::canWrite(currentDir))
172 {
173 return 0;
174 mike 1.2 }
175 else
176 {
177 return 1;
178 }
179 }
180 }
181 return 1;
182 }
183 PEGASUS_NAMESPACE_END
|