]> git.lizzy.rs Git - dragonfireclient.git/blob - src/filecache.cpp
Omnicleanup: header cleanup, add ModApiUtil shared between game and mainmenu
[dragonfireclient.git] / src / filecache.cpp
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 "clientserver.h"
24 #include "log.h"
25 #include "filesys.h"
26 #include "hex.h"
27 #include "sha1.h"
28 #include <string>
29 #include <iostream>
30 #include <fstream>
31 #include <sstream>
32 #include <stdlib.h>
33
34 bool FileCache::loadByPath(const std::string &path, std::ostream &os)
35 {
36         std::ifstream fis(path.c_str(), std::ios_base::binary);
37
38         if(!fis.good()){
39                 verbosestream<<"FileCache: File not found in cache: "
40                                 <<path<<std::endl;
41                 return false;
42         }
43
44         bool bad = false;
45         for(;;){
46                 char buf[1024];
47                 fis.read(buf, 1024);
48                 std::streamsize len = fis.gcount();
49                 os.write(buf, len);
50                 if(fis.eof())
51                         break;
52                 if(!fis.good()){
53                         bad = true;
54                         break;
55                 }
56         }
57         if(bad){
58                 errorstream<<"FileCache: Failed to read file from cache: \""
59                                 <<path<<"\""<<std::endl;
60         }
61
62         return !bad;
63 }
64
65 bool FileCache::updateByPath(const std::string &path, const std::string &data)
66 {
67         std::ofstream file(path.c_str(), std::ios_base::binary |
68                         std::ios_base::trunc);
69
70         if(!file.good())
71         {
72                 errorstream<<"FileCache: Can't write to file at "
73                                 <<path<<std::endl;
74                 return false;
75         }
76
77         file.write(data.c_str(), data.length());
78         file.close();
79
80         return !file.fail();
81 }
82
83 bool FileCache::update(const std::string &name, const std::string &data)
84 {
85         std::string path = m_dir + DIR_DELIM + name;
86         return updateByPath(path, data);
87 }
88 bool FileCache::update_sha1(const std::string &data)
89 {
90         SHA1 sha1;
91         sha1.addBytes(data.c_str(), data.size());
92         unsigned char *digest = sha1.getDigest();
93         std::string sha1_raw((char*)digest, 20);
94         free(digest);
95         std::string sha1_hex = hex_encode(sha1_raw);
96         return update(sha1_hex, data);
97 }
98 bool FileCache::load(const std::string &name, std::ostream &os)
99 {
100         std::string path = m_dir + DIR_DELIM + name;
101         return loadByPath(path, os);
102 }
103 bool FileCache::load_sha1(const std::string &sha1_raw, std::ostream &os)
104 {
105         std::ostringstream tmp_os(std::ios_base::binary);
106         if(!load(hex_encode(sha1_raw), tmp_os))
107                 return false;
108         SHA1 sha1;
109         sha1.addBytes(tmp_os.str().c_str(), tmp_os.str().length());
110         unsigned char *digest = sha1.getDigest();
111         std::string sha1_real_raw((char*)digest, 20);
112         free(digest);
113         if(sha1_real_raw != sha1_raw){
114                 verbosestream<<"FileCache["<<m_dir<<"]: filename "<<sha1_real_raw
115                                 <<" mismatches actual checksum"<<std::endl;
116                 return false;
117         }
118         os<<tmp_os.str();
119         return true;
120 }