imgtools/romtools/rofsbuild/src/cache/cachegenerator.cpp
author marvin shi <marvin.shi@nokia.com>
Wed, 17 Nov 2010 14:41:23 +0800
changeset 692 1dfff72f0961
parent 600 6d08f4a05d93
permissions -rw-r--r--
fix ou1cimx1#651819
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 <queue>
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    20
#include <iostream>
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    21
#include <fstream>
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    22
#include <filesystem.hpp>
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    23
#include <thread/thread.hpp>
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    24
#include <thread/condition_variable.hpp>
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    25
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    26
#include "cache/cacheexception.hpp"
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    27
#include "cache/cacheentry.hpp"
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    28
#include "cache/cacheablelist.hpp"
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    29
#include "cache/cachegenerator.hpp"
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    30
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    31
using namespace std ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    32
CacheGenerator* CacheGenerator::Only = (CacheGenerator*)0;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    33
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    34
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    35
CacheGenerator* CacheGenerator::GetInstance(void) throw (CacheException)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    36
{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    37
	if(! CacheGenerator::Only)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    38
	{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    39
		CacheGenerator::Only = new (nothrow) CacheGenerator();
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    40
		if(! CacheGenerator::Only)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    41
			throw CacheException(CacheException::RESOURCE_ALLOCATION_FAILURE);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    42
	}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    43
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    44
	return CacheGenerator::Only;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    45
}
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
void CacheGenerator::ProcessFiles(void) throw (CacheException)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    49
{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    50
	while(1)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    51
	{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    52
		//pick one entry from the CacheableList.
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    53
		CacheEntry* entryref = CacheableList::GetInstance()->GetCacheable();
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    54
		if(! entryref->GetCachedFileBuffer())
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    55
			break;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    56
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    57
		//write cacheable content into the cache.
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    58
		boost::filesystem::path filepath(entryref->GetCachedFilename());
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    59
		string filename = filepath.file_string(); 	
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    60
		ofstream fileref(filename.c_str(), ios_base::binary | ios_base::out | ios_base::trunc);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    61
		if(! fileref.is_open())
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    62
		{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    63
			printf("Cannot write/update cached %s\r\n", filepath.file_string().c_str());
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    64
			continue;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    65
		}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    66
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    67
		fileref.write(entryref->GetCachedFileBuffer(), entryref->GetCachedFileBufferLen());
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    68
		fileref.close();
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    69
	}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    70
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    71
	return;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    72
}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    73
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    74
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    75
CacheGenerator::CacheGenerator(void) :  boost::thread(ProcessFiles)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    76
{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    77
	return;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    78
}