Skip to content

Commit

Permalink
Switch between copying or symlinking the file depending on ?symlink s…
Browse files Browse the repository at this point in the history
…uffix
  • Loading branch information
patmaddox committed Oct 21, 2022
1 parent 3a51e93 commit 24efb47
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 20 deletions.
25 changes: 15 additions & 10 deletions src/download.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include <cassert>

#include <boost/algorithm/string/replace.hpp>
#include <reaper_plugin_functions.h>

static const int DOWNLOAD_TIMEOUT = 15;
Expand Down Expand Up @@ -212,7 +213,13 @@ bool FileDownload::save()
return FS::remove(m_path.temp());
}

std::ostream *FileDownload::openStream()
CopyFileDownload::CopyFileDownload(const Path &target, const std::string &url,
const NetworkOpts &opts, int flags)
: FileDownload(target, url, opts, flags)
{
}

std::ostream *CopyFileDownload::openStream()
{
if(FS::open(m_stream, m_path.temp()))
return &m_stream;
Expand All @@ -222,27 +229,25 @@ std::ostream *FileDownload::openStream()
}
}

void FileDownload::closeStream()
void CopyFileDownload::closeStream()
{
m_stream.close();
}

SymlinkDownload::SymlinkDownload(const Path &target, const std::string &url,
SymlinkFileDownload::SymlinkFileDownload(const Path &target, const std::string &url,
const NetworkOpts &opts, int flags)
: Download(url, opts, flags), m_path(target)
: FileDownload(target, url, opts, flags)
{
setName(target.join());
}

bool SymlinkDownload::run()
bool SymlinkFileDownload::run()
{
std::string local_path = m_url;
local_path.erase(0, 7);
FS::symlink(local_path, m_path.temp());
return true;
boost::algorithm::replace_all(local_path, "file://", "");
return FS::symlink(local_path, m_path.temp());
}

std::ostream *SymlinkDownload::openStream()
std::ostream *SymlinkFileDownload::openStream()
{
return nullptr;
}
23 changes: 16 additions & 7 deletions src/download.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,28 +105,37 @@ class FileDownload : public Download {
const TempPath &path() const { return m_path; }
bool save();

// bool run() override;

protected:
// std::ostream *openStream() override;
TempPath m_path;
};

class CopyFileDownload : public FileDownload {
public:
CopyFileDownload(const Path &target, const std::string &url,
const NetworkOpts &, int flags = 0);

// bool run() override;

protected:
std::ostream *openStream() override;
void closeStream() override;

private:
TempPath m_path;
std::ofstream m_stream;
};

class SymlinkDownload : public Download {
class SymlinkFileDownload : public FileDownload {
public:
SymlinkDownload(const Path &target, const std::string &url,
SymlinkFileDownload(const Path &target, const std::string &url,
const NetworkOpts &, int flags = 0);

bool run() override;
const TempPath &path() const { return m_path; }

protected:
std::ostream *openStream() override;

private:
TempPath m_path;
};

#endif
21 changes: 19 additions & 2 deletions src/install.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,22 @@
#include "filesystem.hpp"
#include "index.hpp"
#include "reapack.hpp"
#include "remote.hpp"
#include "transaction.hpp"

#include <boost/algorithm/string/predicate.hpp>

#include <iostream>
#include <fstream>

InstallTask::InstallTask(const Version *ver, const int flags,
const Registry::Entry &re, const ArchiveReaderPtr &reader, Transaction *tx)
: Task(tx), m_version(ver), m_flags(flags), m_oldEntry(std::move(re)), m_reader(reader),
m_fail(false), m_index(ver->package()->category()->index()->shared_from_this())
{
Remote r = g_reapack->remote(m_oldEntry.remote);
this->m_symlink = boost::algorithm::starts_with(r.url(), "file://")
&& boost::algorithm::ends_with(r.url(), "?symlink");
}

bool InstallTask::start()
Expand Down Expand Up @@ -71,8 +80,16 @@ bool InstallTask::start()
}
else {
const NetworkOpts &opts = g_reapack->config()->network;
SymlinkDownload *dl = new SymlinkDownload(targetPath, src->url(), opts);
// dl->setExpectedChecksum(src->checksum());
FileDownload *dl;

if(m_symlink) {
dl = new SymlinkFileDownload(targetPath, src->url(), opts);
}
else {
dl = new CopyFileDownload(targetPath, src->url(), opts);
dl->setExpectedChecksum(src->checksum());
}

push(dl, dl->path());
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/synchronize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ bool SynchronizeTask::start()
if(!m_stale && mtime && (!threshold || mtime > now - threshold))
return true;

auto dl = new FileDownload(m_indexPath, m_remote.url(),
auto dl = new CopyFileDownload(m_indexPath, m_remote.url(),
netConfig, Download::NoCacheFlag);
dl->setName(m_remote.name());

Expand Down
1 change: 1 addition & 0 deletions src/task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class InstallTask : public Task {
int m_flags;
Registry::Entry m_oldEntry;
ArchiveReaderPtr m_reader;
bool m_symlink;

bool m_fail;
IndexPtr m_index; // keep in memory
Expand Down

0 comments on commit 24efb47

Please sign in to comment.