2024-10-28 15:57:39 +01:00
|
|
|
// Luanti
|
|
|
|
// SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
// Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
|
|
|
// Copyright (C) 2013 Jonathan Neuschäfer <j.neuschaefer@gmx.net>
|
2012-02-08 11:49:24 +01:00
|
|
|
|
|
|
|
#include "filecache.h"
|
2012-06-17 01:29:13 +03:00
|
|
|
|
2012-02-08 11:49:24 +01:00
|
|
|
#include "log.h"
|
|
|
|
#include "filesys.h"
|
|
|
|
#include <string>
|
|
|
|
#include <iostream>
|
2012-06-17 01:29:13 +03:00
|
|
|
#include <fstream>
|
2017-08-18 07:44:42 +02:00
|
|
|
#include <cstdlib>
|
2012-02-08 11:49:24 +01:00
|
|
|
|
2024-01-20 16:26:05 +01:00
|
|
|
void FileCache::createDir()
|
|
|
|
{
|
|
|
|
if (!fs::CreateAllDirs(m_dir)) {
|
|
|
|
errorstream << "Could not create cache directory: "
|
|
|
|
<< m_dir << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-25 11:50:29 +03:00
|
|
|
bool FileCache::loadByPath(const std::string &path, std::ostream &os)
|
2012-02-08 11:49:24 +01:00
|
|
|
{
|
2024-05-14 23:12:49 +02:00
|
|
|
auto fis = open_ifstream(path.c_str(), false);
|
2024-05-08 20:37:10 +02:00
|
|
|
if (!fis.good())
|
2012-02-08 11:49:24 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
bool bad = false;
|
|
|
|
for(;;){
|
2024-01-20 16:26:05 +01:00
|
|
|
char buf[4096];
|
|
|
|
fis.read(buf, sizeof(buf));
|
2012-02-08 11:49:24 +01:00
|
|
|
std::streamsize len = fis.gcount();
|
|
|
|
os.write(buf, len);
|
|
|
|
if(fis.eof())
|
|
|
|
break;
|
|
|
|
if(!fis.good()){
|
|
|
|
bad = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(bad){
|
2012-03-25 11:50:29 +03:00
|
|
|
errorstream<<"FileCache: Failed to read file from cache: \""
|
2012-03-25 14:47:51 +03:00
|
|
|
<<path<<"\""<<std::endl;
|
2012-02-08 11:49:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return !bad;
|
|
|
|
}
|
|
|
|
|
2024-02-17 15:35:33 +01:00
|
|
|
bool FileCache::updateByPath(const std::string &path, std::string_view data)
|
2012-02-08 11:49:24 +01:00
|
|
|
{
|
2024-01-20 16:26:05 +01:00
|
|
|
createDir();
|
2012-02-08 11:49:24 +01:00
|
|
|
|
2024-05-08 20:37:10 +02:00
|
|
|
auto file = open_ofstream(path.c_str(), true);
|
|
|
|
if (!file.good())
|
2012-02-08 11:49:24 +01:00
|
|
|
return false;
|
|
|
|
|
2024-02-17 15:35:33 +01:00
|
|
|
file << data;
|
2012-02-08 11:49:24 +01:00
|
|
|
file.close();
|
|
|
|
|
|
|
|
return !file.fail();
|
|
|
|
}
|
|
|
|
|
2024-02-17 15:35:33 +01:00
|
|
|
bool FileCache::update(const std::string &name, std::string_view data)
|
2012-02-08 11:49:24 +01:00
|
|
|
{
|
|
|
|
std::string path = m_dir + DIR_DELIM + name;
|
2012-03-25 11:50:29 +03:00
|
|
|
return updateByPath(path, data);
|
2012-02-08 11:49:24 +01:00
|
|
|
}
|
2020-06-13 19:03:26 +02:00
|
|
|
|
2012-03-25 14:47:51 +03:00
|
|
|
bool FileCache::load(const std::string &name, std::ostream &os)
|
2012-02-08 11:49:24 +01:00
|
|
|
{
|
2012-03-25 14:47:51 +03:00
|
|
|
std::string path = m_dir + DIR_DELIM + name;
|
2012-03-25 11:50:29 +03:00
|
|
|
return loadByPath(path, os);
|
2012-02-08 11:49:24 +01:00
|
|
|
}
|
2020-06-13 19:03:26 +02:00
|
|
|
|
|
|
|
bool FileCache::exists(const std::string &name)
|
|
|
|
{
|
|
|
|
std::string path = m_dir + DIR_DELIM + name;
|
2024-05-08 20:37:10 +02:00
|
|
|
return fs::PathExists(path);
|
2020-06-13 19:03:26 +02:00
|
|
|
}
|
2024-01-20 16:26:05 +01:00
|
|
|
|
|
|
|
bool FileCache::updateCopyFile(const std::string &name, const std::string &src_path)
|
|
|
|
{
|
|
|
|
std::string path = m_dir + DIR_DELIM + name;
|
|
|
|
|
|
|
|
createDir();
|
|
|
|
return fs::CopyFileContents(src_path, path);
|
|
|
|
}
|