imgtools/romtools/rofsbuild/src/cache/cachevalidator.cpp
changeset 607 378360dbbdba
parent 600 6d08f4a05d93
child 609 f76e0f94b7ab
equal deleted inserted replaced
591:22486c9c7b15 607:378360dbbdba
       
     1 /*
       
     2 * Copyright (c) 1995-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 the License "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: 
       
    15 *
       
    16 */
       
    17 
       
    18 #include <new>
       
    19 #include <stdio.h>
       
    20 #include <stdlib.h>
       
    21 #include <string>
       
    22 #include <cstring>
       
    23 #include <time.h>
       
    24 #include <map>
       
    25 #include <iostream>
       
    26 #include <fstream>
       
    27 #include <filesystem.hpp>
       
    28 #include <thread/mutex.hpp>
       
    29 
       
    30 #include "cache/cacheexception.hpp"
       
    31 #include "cache/cacheentry.hpp"
       
    32 #include "cache/cache.hpp"
       
    33 #include "cache/cachevalidator.hpp"
       
    34 
       
    35 using namespace std ;
       
    36 CacheValidator* CacheValidator::Only = (CacheValidator*)0;
       
    37 
       
    38 
       
    39 CacheValidator* CacheValidator::GetInstance(void) throw (CacheException)
       
    40 {
       
    41 	if(! CacheValidator::Only)
       
    42 	{
       
    43 		CacheValidator::Only = new (nothrow) CacheValidator();
       
    44 		if(! CacheValidator::Only)
       
    45 			throw CacheException(CacheException::RESOURCE_ALLOCATION_FAILURE);
       
    46 	}
       
    47 
       
    48 	return CacheValidator::Only;
       
    49 }
       
    50 
       
    51 
       
    52 CacheEntry* CacheValidator::Validate(const char* OriginalFilename, int CurrentCompressionID)
       
    53 {
       
    54 	if(! OriginalFilename)
       
    55 		return (CacheEntry*)0;
       
    56 
       
    57 	//an executable will be validated if its creation time does not altered and the compression method is not different against previous image build used.
       
    58 	CacheEntry* entryref = Cache::GetInstance()->GetEntryList(OriginalFilename);
       
    59 	if(! entryref)
       
    60 	{
       
    61 		return (CacheEntry*)0;
       
    62 	}
       
    63 	while(entryref)
       
    64 	{
       
    65 		boost::filesystem::path originalfile(OriginalFilename);
       
    66 		time_t originalcreationtime = last_write_time(originalfile);
       
    67 		string creationtime(ctime(&originalcreationtime));
       
    68 		size_t newlinepos = creationtime.find("\n");
       
    69 		while(newlinepos != string::npos)
       
    70 		{
       
    71 			creationtime.erase(newlinepos, 1);
       
    72 			newlinepos = creationtime.find(("\n"));
       
    73 		}
       
    74 		if((creationtime.compare(entryref->GetOriginalFileCreateTime())== 0) || (atoi(entryref->GetCachedFileCompressionID())==CurrentCompressionID))
       
    75 		{
       
    76 			boost::filesystem::path cachedfile(entryref->GetCachedFilename());
       
    77 			string filename = cachedfile.file_string(); 
       
    78 			ifstream filecontentreader(filename.c_str(), ios_base::in | ios_base::binary);
       
    79 			if(! filecontentreader.is_open()){
       
    80 				cerr << "Cannot open cached file " << filename << endl ;
       
    81 				return NULL;
       
    82 			}
       
    83 			filecontentreader.seekg(0, ios_base::end);
       
    84 			int contentlength = filecontentreader.tellg();
       
    85 			char* bufferref = new char[contentlength + 1];
       
    86 			filecontentreader.seekg(0, ios_base::beg);
       
    87 			filecontentreader.read(bufferref, contentlength);
       
    88 			bufferref[contentlength] = 0 ;
       
    89 			entryref->SetCachedFileBuffer(bufferref, contentlength);
       
    90 			delete []bufferref;
       
    91 
       
    92 			cout << "Using cached" <<  OriginalFilename << endl ;
       
    93 
       
    94 			return entryref;
       
    95 		}
       
    96 
       
    97 		entryref = entryref->GetNextEntry();
       
    98 	}
       
    99 
       
   100 	return (CacheEntry*)0;
       
   101 }
       
   102 
       
   103 
       
   104 CacheValidator::CacheValidator(void)
       
   105 {
       
   106 	return;
       
   107 }