1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Class responsible of handling mmp files. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "../inc/CATMmp.h" |
|
20 |
|
21 CATMmp::CATMmp() |
|
22 { |
|
23 LOG_FUNC_ENTRY("CATMmp::CATMmp"); |
|
24 } |
|
25 |
|
26 CATMmp::~CATMmp() |
|
27 { |
|
28 LOG_FUNC_ENTRY("CATMmp::~CATMmp"); |
|
29 } |
|
30 |
|
31 // ----------------------------------------------------------------------------- |
|
32 // CATMmp::IsMmpEdited |
|
33 // Checks is the file edited by AT |
|
34 // ----------------------------------------------------------------------------- |
|
35 bool CATMmp::IsMmpEdited( bool bBackup) |
|
36 { |
|
37 LOG_FUNC_ENTRY("CATMmp::IsMmpEdited"); |
|
38 // Stream to read file |
|
39 ifstream in; |
|
40 // Temp char array to read line |
|
41 char cTemp[MAX_LINE_LENGTH]; |
|
42 // Open file |
|
43 if ( bBackup ) |
|
44 in.open( CreateMmpBackupPath().c_str() ); |
|
45 else |
|
46 in.open( m_sMmpFile.c_str() ); |
|
47 // Is file open ok |
|
48 if( ! in.good() ) |
|
49 { |
|
50 cout << AT_MSG << "Error, can not open file " |
|
51 << m_sMmpFile << endl; |
|
52 in.close(); |
|
53 return false; |
|
54 } |
|
55 // Search edit start line |
|
56 bool bEdited=false; |
|
57 const char* cFind = MMPFILECHANGES[0].c_str(); |
|
58 while( ! bEdited && in.good() ) |
|
59 { |
|
60 // Get line |
|
61 in.getline( cTemp, MAX_LINE_LENGTH ); |
|
62 // Compare to first line in changes |
|
63 if ( strstr( cTemp, cFind) ) |
|
64 { |
|
65 bEdited = true; |
|
66 // Stop looking any further |
|
67 break; |
|
68 } |
|
69 } |
|
70 // Close file and return result |
|
71 in.close(); |
|
72 return bEdited; |
|
73 } |
|
74 |
|
75 // ----------------------------------------------------------------------------- |
|
76 // CATMmp::EditMmpFile |
|
77 // Makes AnalyzeTool changes to given mmp file |
|
78 // ----------------------------------------------------------------------------- |
|
79 bool CATMmp::EditMmpFile(const string& sTargetType, const string& sId) |
|
80 { |
|
81 LOG_FUNC_ENTRY("CATMmp::EditMmpFile"); |
|
82 |
|
83 if ( ! RemoveWriteProtections() ) |
|
84 return false; |
|
85 |
|
86 // Stream where to add changes |
|
87 ofstream out; |
|
88 |
|
89 // Open mmp file for editing (append changes to the end) |
|
90 out.open( m_sMmpFile.c_str(), ios::out | ios::app ); |
|
91 |
|
92 // File open ok? |
|
93 if( !out.good() ) |
|
94 { |
|
95 cout << AT_MSG << "Error, can not open file " |
|
96 << m_sMmpFile; |
|
97 out.close(); |
|
98 return false; |
|
99 } |
|
100 |
|
101 // Write lines to mmp file |
|
102 if ( sTargetType.compare( "dll" ) == 0 || sTargetType.compare( "lib" ) == 0 ) |
|
103 { |
|
104 // DLL changes |
|
105 int size = sizeof( MMPFILECHANGES_DLL ) / sizeof( string ); |
|
106 for( int i = 0; i < size; i++ ) |
|
107 { |
|
108 out << endl << MMPFILECHANGES_DLL[i]; |
|
109 } |
|
110 out << endl; |
|
111 } |
|
112 else |
|
113 { |
|
114 // Other than DLL changes |
|
115 int size = sizeof( MMPFILECHANGES ) / sizeof( string ); |
|
116 for( int i = 0; i < size; i++ ) |
|
117 { |
|
118 // After second line of changes add also source statement |
|
119 out << endl << MMPFILECHANGES[i]; |
|
120 if ( i == 1 ) |
|
121 { |
|
122 out << endl |
|
123 << "SOURCE " |
|
124 << AT_TEMP_CPP_LOWER_START |
|
125 << sId |
|
126 << AT_TEMP_CPP_LOWER_END; |
|
127 } |
|
128 } |
|
129 out << endl; |
|
130 } |
|
131 // Close stream |
|
132 out.close(); |
|
133 |
|
134 cout << AT_MSG << "Mmp file : " << m_sMmpFile << " edited." << endl; |
|
135 |
|
136 return true; |
|
137 } |
|
138 |
|
139 // ----------------------------------------------------------------------------- |
|
140 // CATMmp::BackupMmpFile |
|
141 // Backups the mmp file to path/atool_temp/filename.mmp.tmp |
|
142 // Calling this function results always to |
|
143 // - none edited mmp |
|
144 // - none edited backup |
|
145 // - If mmp is write protected. Create writable copy from it. Backup the write |
|
146 // procted one. |
|
147 // ----------------------------------------------------------------------------- |
|
148 bool CATMmp::BackupMmpFile( ) |
|
149 { |
|
150 LOG_FUNC_ENTRY("CATMmp::BackupMmpFile"); |
|
151 |
|
152 if ( ! RemoveWriteProtections() ) |
|
153 return false; |
|
154 |
|
155 // Backup path+filename |
|
156 string sBackup = CreateMmpBackupPath(); |
|
157 |
|
158 // Backup mmp. |
|
159 if ( CopyFile( m_sMmpFile.c_str() , sBackup.c_str(), false ) == 0 ) |
|
160 { |
|
161 // Log and return false if failed to copy file |
|
162 LOG_STRING( "error copyfile " << m_sMmpFile << " to " << sBackup ); |
|
163 return false; |
|
164 } |
|
165 |
|
166 // If backup now edited remove changes from it. |
|
167 if ( IsMmpEdited( true ) ) |
|
168 { |
|
169 if ( ! RemoveMmpFileChanges( true ) ) |
|
170 return false; |
|
171 } |
|
172 |
|
173 return true; |
|
174 } |
|
175 // ----------------------------------------------------------------------------- |
|
176 // CATMmp::RestoreMmpFile |
|
177 // Restores the mmp file from backup |
|
178 // ----------------------------------------------------------------------------- |
|
179 bool CATMmp::RestoreMmpFile() |
|
180 { |
|
181 LOG_FUNC_ENTRY("CATMmp::RestoreMmpFile"); |
|
182 |
|
183 if ( ! RemoveWriteProtections() ) |
|
184 return false; |
|
185 |
|
186 if ( CopyFile( CreateMmpBackupPath().c_str() , m_sMmpFile.c_str(), false ) == 0 ) |
|
187 { |
|
188 // Log and return false if failed to copy file |
|
189 LOG_STRING("error copyfile " << CreateMmpBackupPath() << " to " << m_sMmpFile ); |
|
190 return false; |
|
191 } |
|
192 else |
|
193 cout << AT_MSG << "Mmp file : " << m_sMmpFile << " restored." << endl; |
|
194 return true; |
|
195 } |
|
196 |
|
197 // ----------------------------------------------------------------------------- |
|
198 // CATMmp::RemoveMmpFileChanges |
|
199 // Removes AT changes from given mmp file |
|
200 // ----------------------------------------------------------------------------- |
|
201 bool CATMmp::RemoveMmpFileChanges(bool bBackup) |
|
202 { |
|
203 LOG_FUNC_ENTRY("CATMmp::RemoveMmpFileChanges"); |
|
204 |
|
205 if ( ! RemoveWriteProtections() ) |
|
206 return false; |
|
207 |
|
208 // File reading stream |
|
209 ifstream in; |
|
210 // Vector to hold file data |
|
211 vector<string> vLines; |
|
212 // Open file |
|
213 if ( bBackup ) |
|
214 in.open( CreateMmpBackupPath().c_str(), ios::in ); |
|
215 else |
|
216 in.open( m_sMmpFile.c_str(), ios::in ); |
|
217 // Check file open ok |
|
218 if ( ! in.good() ) |
|
219 { |
|
220 cout << AT_MSG << "Error, opening file"; |
|
221 if ( bBackup ) |
|
222 cout << CreateMmpBackupPath(); |
|
223 else |
|
224 cout << m_sMmpFile; |
|
225 cout << endl; |
|
226 return false; |
|
227 } |
|
228 // Read file to temporary stream except AT changes |
|
229 char cLine[MAX_LINE_LENGTH]; |
|
230 // Boolean to know read or not |
|
231 bool bRead = true; |
|
232 // Integer to confirm that AT changes were succefully found and |
|
233 // not read even if they are found multiple times |
|
234 int iSuccessfull = 0; |
|
235 // Number of 'lines' in mmp changes |
|
236 int iChangesSize = sizeof( MMPFILECHANGES ) / sizeof( string ); |
|
237 // First mmp changes line |
|
238 string sFirstLine = MMPFILECHANGES[0]; |
|
239 // Last mmp changes line |
|
240 string sLastLine = MMPFILECHANGES[iChangesSize-1]; |
|
241 while( in.good() ) |
|
242 { |
|
243 in.getline( cLine, MAX_LINE_LENGTH ); |
|
244 // Check start of AT changes |
|
245 if( strstr( cLine, sFirstLine.c_str() ) != 0 ) |
|
246 { |
|
247 // Remove last linefeed |
|
248 vector<string>::iterator it = vLines.end(); |
|
249 it--; |
|
250 if ( it->size() == 0 ) |
|
251 vLines.erase( vLines.end()-1, vLines.end() ); |
|
252 // Stop reading |
|
253 bRead = false; |
|
254 iSuccessfull+=3; |
|
255 } |
|
256 // Read lines outside AT changes |
|
257 if ( bRead ) |
|
258 { |
|
259 // Gather all other lines except the AT edits |
|
260 vLines.push_back( string(cLine) ); |
|
261 } |
|
262 // Check end of AT changes |
|
263 if( strstr( cLine, sLastLine.c_str() ) != 0 ) |
|
264 { |
|
265 // Get empty line |
|
266 in.getline( cLine, MAX_LINE_LENGTH ); |
|
267 // Continue reading |
|
268 bRead = true; |
|
269 iSuccessfull-=1; |
|
270 } |
|
271 } |
|
272 // Close reading file stream |
|
273 in.close(); |
|
274 // To check all went ok iSuccesfull%2 = 0 |
|
275 if ( iSuccessfull%2 != 0 && iSuccessfull >= 2 ) |
|
276 { |
|
277 cout << AT_MSG << "Error, removing mmp changes from "; |
|
278 if ( bBackup ) |
|
279 cout << CreateMmpBackupPath(); |
|
280 else |
|
281 cout << m_sMmpFile; |
|
282 cout << endl; |
|
283 return false; |
|
284 } |
|
285 // Overwrite current mmp file |
|
286 ofstream out; |
|
287 // Open file (truncates old data) |
|
288 if ( bBackup ) |
|
289 out.open( CreateMmpBackupPath().c_str(), ios::trunc ); |
|
290 else |
|
291 out.open( m_sMmpFile.c_str(), ios::trunc ); |
|
292 // Is open ok |
|
293 if( ! out.good() ) |
|
294 { |
|
295 cout << AT_MSG << "Error, opening file "; |
|
296 if ( bBackup ) |
|
297 cout << CreateMmpBackupPath(); |
|
298 else |
|
299 cout << m_sMmpFile; |
|
300 cout << endl; |
|
301 } |
|
302 // Write lines to file |
|
303 for( vector<string>::iterator it = vLines.begin() ; it != vLines.end() ; it++ ) |
|
304 { |
|
305 out << *it << endl; |
|
306 } |
|
307 // Close |
|
308 out.close(); |
|
309 // Return true |
|
310 return true; |
|
311 } |
|
312 // ----------------------------------------------------------------------------- |
|
313 // CATMmp::VerifyAndRecover |
|
314 // Wont change mmp if it is not edited |
|
315 // Replaces mmp file using backup if it exists and it is |
|
316 // not edited otherwise removes changes from mmp file. |
|
317 // ----------------------------------------------------------------------------- |
|
318 bool CATMmp::VerifyAndRecover() |
|
319 { |
|
320 LOG_FUNC_ENTRY("CATMmp::VerifyAndRecover"); |
|
321 // Is it edited |
|
322 if ( IsMmpEdited() ) |
|
323 { |
|
324 string sBackup = CreateMmpBackupPath(); |
|
325 if ( FileExists( sBackup.c_str() ) ) |
|
326 { |
|
327 // Is backup edited |
|
328 if ( ! IsMmpEdited( true ) ) |
|
329 { |
|
330 // Replace original with backup |
|
331 return RestoreMmpFile(); |
|
332 } |
|
333 } |
|
334 // Remove changes from original |
|
335 return RemoveMmpFileChanges(); |
|
336 } |
|
337 // Non edited original |
|
338 return true; |
|
339 } |
|
340 |
|
341 // ----------------------------------------------------------------------------- |
|
342 // CATMmp::CreateMmpBackupPath |
|
343 // Creates string containing full path to backup mmp file |
|
344 // ----------------------------------------------------------------------------- |
|
345 string CATMmp::CreateMmpBackupPath() |
|
346 { |
|
347 LOG_FUNC_ENTRY("CATMmp::CreateMmpBackupPath"); |
|
348 // backup path+filename |
|
349 string sBackup; |
|
350 sBackup.append( GetPathOrFileName( false, m_sMmpFile ) ); |
|
351 sBackup.append( AT_TEMP_DIR ); |
|
352 sBackup.append( "\\" ); |
|
353 // Add mmp file name to it and .tmp |
|
354 sBackup.append( GetPathOrFileName( true, m_sMmpFile ) ); |
|
355 // Add .tmp |
|
356 sBackup.append( ".tmp" ); |
|
357 // Return it |
|
358 return sBackup; |
|
359 } |
|
360 |
|
361 // ----------------------------------------------------------------------------- |
|
362 // CATMmp::RemoveWriteProtections |
|
363 // Removes write protection of mmp file and backup if exists. |
|
364 // ----------------------------------------------------------------------------- |
|
365 bool CATMmp::RemoveWriteProtections() |
|
366 { |
|
367 LOG_LOW_FUNC_ENTRY("CATMmp::RemoveWriteProtections"); |
|
368 |
|
369 // Backup path+filename |
|
370 string sBackup = CreateMmpBackupPath(); |
|
371 |
|
372 // Check is mmp read-only |
|
373 if ( IsFileReadOnly( m_sMmpFile.c_str() ) ) |
|
374 { |
|
375 if( ! SetFileWritable( m_sMmpFile.c_str() ) ) |
|
376 { |
|
377 LOG_STRING( "error setting mmp file writable" << m_sMmpFile ); |
|
378 return false; |
|
379 } |
|
380 } |
|
381 |
|
382 // Check is there a backup if is remove any write protection from it. |
|
383 if ( FileExists( sBackup.c_str() ) ) |
|
384 { |
|
385 if ( IsFileReadOnly( sBackup.c_str() ) ) |
|
386 { |
|
387 if( ! SetFileWritable( sBackup.c_str() ) ) |
|
388 { |
|
389 LOG_STRING( "error setting mmp file writable" << sBackup ); |
|
390 return false; |
|
391 } |
|
392 } |
|
393 } |
|
394 return true; |
|
395 } |
|
396 // End of file |
|