]> git.lizzy.rs Git - dragonfireclient.git/blob - src/filecache.cpp
Almost support loading sounds from server
[dragonfireclient.git] / src / filecache.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2012 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 General Public License as published by
8 the Free Software Foundation; either version 2 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 General Public License for more details.
15
16 You should have received a copy of the GNU 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 #include "clientserver.h"
23 #include "log.h"
24 #include "filesys.h"
25 #include "utility.h"
26 #include "hex.h"
27 #include "sha1.h"
28
29 #include <string>
30 #include <iostream>
31
32 bool FileCache::loadByPath(const std::string &path, std::ostream &os)
33 {
34         std::ifstream fis(path.c_str(), std::ios_base::binary);
35
36         if(!fis.good()){
37                 verbosestream<<"FileCache: File not found in cache: "
38                                 <<path<<std::endl;
39                 return false;
40         }
41
42         bool bad = false;
43         for(;;){
44                 char buf[1024];
45                 fis.read(buf, 1024);
46                 std::streamsize len = fis.gcount();
47                 os.write(buf, len);
48                 if(fis.eof())
49                         break;
50                 if(!fis.good()){
51                         bad = true;
52                         break;
53                 }
54         }
55         if(bad){
56                 errorstream<<"FileCache: Failed to read file from cache: \""
57                                 <<path<<"\""<<std::endl;
58         }
59
60         return !bad;
61 }
62
63 bool FileCache::updateByPath(const std::string &path, const std::string &data)
64 {
65         std::ofstream file(path.c_str(), std::ios_base::binary |
66                         std::ios_base::trunc);
67
68         if(!file.good())
69         {
70                 errorstream<<"FileCache: Can't write to file at "
71                                 <<path<<std::endl;
72                 return false;
73         }
74
75         file.write(data.c_str(), data.length());
76         file.close();
77
78         return !file.fail();
79 }
80
81 bool FileCache::update(const std::string &name, const std::string &data)
82 {
83         std::string path = m_dir + DIR_DELIM + name;
84         return updateByPath(path, data);
85 }
86 bool FileCache::update_sha1(const std::string &data)
87 {
88         SHA1 sha1;
89         sha1.addBytes(data.c_str(), data.size());
90         unsigned char *digest = sha1.getDigest();
91         std::string sha1_raw((char*)digest, 20);
92         free(digest);
93         std::string sha1_hex = hex_encode(sha1_raw);
94         return update(sha1_hex, data);
95 }
96 bool FileCache::load(const std::string &name, std::ostream &os)
97 {
98         std::string path = m_dir + DIR_DELIM + name;
99         return loadByPath(path, os);
100 }
101 bool FileCache::load_sha1(const std::string &sha1_raw, std::ostream &os)
102 {
103         std::ostringstream tmp_os(std::ios_base::binary);
104         if(!load(hex_encode(sha1_raw), tmp_os))
105                 return false;
106         SHA1 sha1;
107         sha1.addBytes(tmp_os.str().c_str(), tmp_os.str().length());
108         unsigned char *digest = sha1.getDigest();
109         std::string sha1_real_raw((char*)digest, 20);
110         free(digest);
111         if(sha1_real_raw != sha1_raw){
112                 verbosestream<<"FileCache["<<m_dir<<"]: filename "<<sha1_real_raw
113                                 <<" mismatches actual checksum"<<std::endl;
114                 return false;
115         }
116         os<<tmp_os.str();
117         return true;
118 }