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