imgtools/romtools/rofsbuild/src/cache/cachevalidator.cpp
author jjkang
Fri, 25 Jun 2010 20:58:33 +0800
changeset 605 122d2b873fd1
parent 600 6d08f4a05d93
child 609 f76e0f94b7ab
permissions -rw-r--r--
Minor changes: rofsbuild
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
600
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
     1
/*
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
     2
* Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
     3
* All rights reserved.
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
     4
* This component and the accompanying materials are made available
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
     5
* under the terms of the License "Eclipse Public License v1.0"
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
     6
* which accompanies this distribution, and is available
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
     8
*
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
     9
* Initial Contributors:
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    11
*
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    12
* Contributors:
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    13
*
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    14
* Description: 
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    15
*
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    16
*/
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    17
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    18
#include <new>
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    19
#include <stdio.h>
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    20
#include <stdlib.h>
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    21
#include <string>
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    22
#include <cstring>
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    23
#include <time.h>
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    24
#include <map>
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    25
#include <iostream>
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    26
#include <fstream>
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    27
#include <filesystem.hpp>
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    28
#include <thread/mutex.hpp>
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    29
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    30
#include "cache/cacheexception.hpp"
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    31
#include "cache/cacheentry.hpp"
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    32
#include "cache/cache.hpp"
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    33
#include "cache/cachevalidator.hpp"
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    34
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    35
using namespace std ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    36
CacheValidator* CacheValidator::Only = (CacheValidator*)0;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    37
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    38
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    39
CacheValidator* CacheValidator::GetInstance(void) throw (CacheException)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    40
{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    41
	if(! CacheValidator::Only)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    42
	{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    43
		CacheValidator::Only = new (nothrow) CacheValidator();
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    44
		if(! CacheValidator::Only)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    45
			throw CacheException(CacheException::RESOURCE_ALLOCATION_FAILURE);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    46
	}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    47
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    48
	return CacheValidator::Only;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    49
}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    50
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    51
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    52
CacheEntry* CacheValidator::Validate(const char* OriginalFilename, int CurrentCompressionID)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    53
{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    54
	if(! OriginalFilename)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    55
		return (CacheEntry*)0;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    56
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    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.
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    58
	CacheEntry* entryref = Cache::GetInstance()->GetEntryList(OriginalFilename);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    59
	if(! entryref)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    60
	{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    61
		return (CacheEntry*)0;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    62
	}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    63
	while(entryref)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    64
	{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    65
		boost::filesystem::path originalfile(OriginalFilename);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    66
		time_t originalcreationtime = last_write_time(originalfile);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    67
		string creationtime(ctime(&originalcreationtime));
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    68
		size_t newlinepos = creationtime.find("\n");
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    69
		while(newlinepos != string::npos)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    70
		{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    71
			creationtime.erase(newlinepos, 1);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    72
			newlinepos = creationtime.find(("\n"));
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    73
		}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    74
		if((creationtime.compare(entryref->GetOriginalFileCreateTime())== 0) || (atoi(entryref->GetCachedFileCompressionID())==CurrentCompressionID))
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    75
		{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    76
			boost::filesystem::path cachedfile(entryref->GetCachedFilename());
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    77
			string filename = cachedfile.file_string(); 
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    78
			ifstream filecontentreader(filename.c_str(), ios_base::in | ios_base::binary);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    79
			if(! filecontentreader.is_open()){
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    80
				cerr << "Cannot open cached file " << filename << endl ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    81
				return NULL;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    82
			}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    83
			filecontentreader.seekg(0, ios_base::end);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    84
			int contentlength = filecontentreader.tellg();
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    85
			char* bufferref = new char[contentlength + 1];
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    86
			filecontentreader.seekg(0, ios_base::beg);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    87
			filecontentreader.read(bufferref, contentlength);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    88
			bufferref[contentlength] = 0 ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    89
			entryref->SetCachedFileBuffer(bufferref, contentlength);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    90
			delete []bufferref;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    91
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    92
			cout << "Using cached" <<  OriginalFilename << endl ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    93
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    94
			return entryref;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    95
		}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    96
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    97
		entryref = entryref->GetNextEntry();
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    98
	}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    99
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   100
	return (CacheEntry*)0;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   101
}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   102
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   103
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   104
CacheValidator::CacheValidator(void)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   105
{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   106
	return;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   107
}