LCOV - code coverage report
Current view: top level - src - filecache.cpp (source / functions) Hit Total Coverage
Test: report Lines: 17 36 47.2 %
Date: 2015-07-11 18:23:49 Functions: 4 6 66.7 %

          Line data    Source code
       1             : /*
       2             : Minetest
       3             : Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
       4             : Copyright (C) 2013 Jonathan Neuschäfer <j.neuschaefer@gmx.net>
       5             : 
       6             : This program is free software; you can redistribute it and/or modify
       7             : it under the terms of the GNU Lesser General Public License as published by
       8             : the Free Software Foundation; either version 2.1 of the License, or
       9             : (at your option) any later version.
      10             : 
      11             : This program is distributed in the hope that it will be useful,
      12             : but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             : GNU Lesser General Public License for more details.
      15             : 
      16             : You should have received a copy of the GNU Lesser General Public License along
      17             : with this program; if not, write to the Free Software Foundation, Inc.,
      18             : 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      19             : */
      20             : 
      21             : #include "filecache.h"
      22             : 
      23             : #include "network/networkprotocol.h"
      24             : #include "log.h"
      25             : #include "filesys.h"
      26             : #include <string>
      27             : #include <iostream>
      28             : #include <fstream>
      29             : #include <stdlib.h>
      30             : 
      31        2635 : bool FileCache::loadByPath(const std::string &path, std::ostream &os)
      32             : {
      33        5270 :         std::ifstream fis(path.c_str(), std::ios_base::binary);
      34             : 
      35        2635 :         if(!fis.good()){
      36           0 :                 verbosestream<<"FileCache: File not found in cache: "
      37           0 :                                 <<path<<std::endl;
      38           0 :                 return false;
      39             :         }
      40             : 
      41        2635 :         bool bad = false;
      42       11656 :         for(;;){
      43             :                 char buf[1024];
      44       14291 :                 fis.read(buf, 1024);
      45       14291 :                 std::streamsize len = fis.gcount();
      46       14291 :                 os.write(buf, len);
      47       14291 :                 if(fis.eof())
      48        5270 :                         break;
      49       11656 :                 if(!fis.good()){
      50           0 :                         bad = true;
      51           0 :                         break;
      52             :                 }
      53             :         }
      54        2635 :         if(bad){
      55           0 :                 errorstream<<"FileCache: Failed to read file from cache: \""
      56           0 :                                 <<path<<"\""<<std::endl;
      57             :         }
      58             : 
      59        2635 :         return !bad;
      60             : }
      61             : 
      62           0 : bool FileCache::updateByPath(const std::string &path, const std::string &data)
      63             : {
      64             :         std::ofstream file(path.c_str(), std::ios_base::binary |
      65           0 :                         std::ios_base::trunc);
      66             : 
      67           0 :         if(!file.good())
      68             :         {
      69           0 :                 errorstream<<"FileCache: Can't write to file at "
      70           0 :                                 <<path<<std::endl;
      71           0 :                 return false;
      72             :         }
      73             : 
      74           0 :         file.write(data.c_str(), data.length());
      75           0 :         file.close();
      76             : 
      77           0 :         return !file.fail();
      78             : }
      79             : 
      80           0 : bool FileCache::update(const std::string &name, const std::string &data)
      81             : {
      82           0 :         std::string path = m_dir + DIR_DELIM + name;
      83           0 :         return updateByPath(path, data);
      84             : }
      85        2635 : bool FileCache::load(const std::string &name, std::ostream &os)
      86             : {
      87        5270 :         std::string path = m_dir + DIR_DELIM + name;
      88        5270 :         return loadByPath(path, os);
      89           3 : }

Generated by: LCOV version 1.11