5ba8ef5cce6404debba6bfa6e93a397c76bfd3a2
[oweals/minetest.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
28 #include <string>
29 #include <iostream>
30
31 bool FileCache::loadByPath(const std::string &path, std::ostream &os)
32 {
33         std::ifstream fis(path.c_str(), std::ios_base::binary);
34
35         if(!fis.good()){
36                 verbosestream<<"FileCache: File not found in cache: "
37                         <<path<<std::endl;
38                 return false;
39         }
40
41         bool bad = false;
42         for(;;){
43                 char buf[1024];
44                 fis.read(buf, 1024);
45                 std::streamsize len = fis.gcount();
46                 os.write(buf, len);
47                 if(fis.eof())
48                         break;
49                 if(!fis.good()){
50                         bad = true;
51                         break;
52                 }
53         }
54         if(bad){
55                 errorstream<<"FileCache: Failed to read file from cache: \""
56                         <<path<<"\""<<std::endl;
57         }
58
59         return !bad;
60 }
61
62 bool FileCache::updateByPath(const std::string &path, const std::string &data)
63 {
64         std::ofstream file(path.c_str(), std::ios_base::binary |
65                         std::ios_base::trunc);
66
67         if(!file.good())
68         {
69                 errorstream<<"FileCache: Can't write to file at "
70                         <<path<<std::endl;
71                 return false;
72         }
73
74         file.write(data.c_str(), data.length());
75         file.close();
76
77         return !file.fail();
78 }
79
80 bool FileCache::loadByName(const std::string &name, std::ostream &os)
81 {
82         std::string path = m_dir + DIR_DELIM + name;
83         return loadByPath(path, os);
84 }
85
86
87 bool FileCache::updateByName(const std::string &name, const std::string &data)
88 {
89         std::string path = m_dir + DIR_DELIM + name;
90         return updateByPath(path, data);
91 }
92
93 std::string FileCache::getPathFromChecksum(const std::string &checksum)
94 {
95         std::string checksum_hex = hex_encode(checksum.c_str(), checksum.length());
96         return m_dir + DIR_DELIM + checksum_hex;
97 }
98
99 bool FileCache::loadByChecksum(const std::string &checksum, std::ostream &os)
100 {
101         std::string path = getPathFromChecksum(checksum);
102         return loadByPath(path, os);
103 }
104
105 bool FileCache::updateByChecksum(const std::string &checksum,
106                 const std::string &data)
107 {
108         std::string path = getPathFromChecksum(checksum);
109         return updateByPath(path, data);
110 }