imgtools/romtools/rofsbuild/src/cache/cachevalidator.cpp
author kelvzhu
Mon, 18 Oct 2010 16:16:46 +0800
changeset 655 3f65fd25dfd4
parent 609 f76e0f94b7ab
permissions -rw-r--r--
sync up SVN codes
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
	}
609
f76e0f94b7ab Minor change: update package_definition.xml
jjkang
parents: 600
diff changeset
    63
f76e0f94b7ab Minor change: update package_definition.xml
jjkang
parents: 600
diff changeset
    64
	boost::filesystem::path originalfile(OriginalFilename);
f76e0f94b7ab Minor change: update package_definition.xml
jjkang
parents: 600
diff changeset
    65
	time_t originalcreationtime = last_write_time(originalfile);
f76e0f94b7ab Minor change: update package_definition.xml
jjkang
parents: 600
diff changeset
    66
	string creationtime(ctime(&originalcreationtime));
f76e0f94b7ab Minor change: update package_definition.xml
jjkang
parents: 600
diff changeset
    67
	size_t newlinepos = creationtime.find("\n");
f76e0f94b7ab Minor change: update package_definition.xml
jjkang
parents: 600
diff changeset
    68
	while(newlinepos != string::npos)
f76e0f94b7ab Minor change: update package_definition.xml
jjkang
parents: 600
diff changeset
    69
	{
f76e0f94b7ab Minor change: update package_definition.xml
jjkang
parents: 600
diff changeset
    70
		creationtime.erase(newlinepos, 1);
f76e0f94b7ab Minor change: update package_definition.xml
jjkang
parents: 600
diff changeset
    71
		newlinepos = creationtime.find(("\n"));
f76e0f94b7ab Minor change: update package_definition.xml
jjkang
parents: 600
diff changeset
    72
	}
600
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    73
	while(entryref)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    74
	{
609
f76e0f94b7ab Minor change: update package_definition.xml
jjkang
parents: 600
diff changeset
    75
		if((creationtime.compare(entryref->GetOriginalFileCreateTime())== 0) && (atoi(entryref->GetCachedFileCompressionID())==CurrentCompressionID))
600
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    76
		{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    77
			boost::filesystem::path cachedfile(entryref->GetCachedFilename());
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    78
			string filename = cachedfile.file_string(); 
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    79
			ifstream filecontentreader(filename.c_str(), ios_base::in | ios_base::binary);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    80
			if(! filecontentreader.is_open()){
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    81
				cerr << "Cannot open cached file " << filename << endl ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    82
				return NULL;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    83
			}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    84
			filecontentreader.seekg(0, ios_base::end);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    85
			int contentlength = filecontentreader.tellg();
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    86
			char* bufferref = new char[contentlength + 1];
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    87
			filecontentreader.seekg(0, ios_base::beg);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    88
			filecontentreader.read(bufferref, contentlength);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    89
			bufferref[contentlength] = 0 ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    90
			entryref->SetCachedFileBuffer(bufferref, contentlength);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    91
			delete []bufferref;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    92
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    93
			cout << "Using cached" <<  OriginalFilename << endl ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    94
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    95
			return entryref;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    96
		}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    97
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    98
		entryref = entryref->GetNextEntry();
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    99
	}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   100
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   101
	return (CacheEntry*)0;
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
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   105
CacheValidator::CacheValidator(void)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   106
{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   107
	return;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   108
}