|
|
|
#pragma once
|
|
|
|
#include "curlfuncs.h"
|
|
|
|
#include "mdatetime.h"
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using michlib::MDateTime;
|
|
|
|
using michlib::RetVal;
|
|
|
|
using michlib::Error;
|
|
|
|
|
|
|
|
class DIRRAIIDT
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// TODO: make static
|
|
|
|
void operator()(DIR* d) { closedir(d); }
|
|
|
|
};
|
|
|
|
|
|
|
|
class DIRRAII: public std::unique_ptr<DIR, DIRRAIIDT>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
operator DIR*() const { return get(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct FileInfo
|
|
|
|
{
|
|
|
|
MString url;
|
|
|
|
MString name;
|
|
|
|
MDateTime mtime;
|
|
|
|
size_t size;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Remove last element from path
|
|
|
|
inline MString DirName(const MString& name)
|
|
|
|
{
|
|
|
|
auto p = name.GetPos('/', false);
|
|
|
|
if(p == 0) return name;
|
|
|
|
return name.SubStr(1, p - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get last element from path
|
|
|
|
inline MString FileName(const MString& name)
|
|
|
|
{
|
|
|
|
auto p = name.GetPos('/', false);
|
|
|
|
if(p == 0) return name;
|
|
|
|
return name.SubStr(p + 1, name.Len() - p);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check and, if necessary, create the path to the file
|
|
|
|
bool MakePath(const MString& dname);
|
|
|
|
|
|
|
|
// Get local file list
|
|
|
|
RetVal<std::vector<struct FileInfo>> ReadLocalFileList(const MString& dir, const MString& path = "");
|
|
|
|
|
|
|
|
// Download file to the local mirror
|
|
|
|
Error DownloadFile(const CURLRAII& chandle, const struct FileInfo& rinfo, const MString& root);
|
|
|
|
|
|
|
|
// Remove file from the local mirror
|
|
|
|
Error RemoveFile(const struct FileInfo& linfo);
|
|
|
|
|
|
|
|
// Updare file in the local mirror
|
|
|
|
Error UpdateFile(const CURLRAII& chandle, const struct FileInfo& rinfo, const struct FileInfo& linfo, const MString& root);
|