2023-09-08 05:31:19 +03:00
|
|
|
#pragma once
|
|
|
|
///@file
|
|
|
|
|
|
|
|
#include "types.hh"
|
|
|
|
#include "serialise.hh"
|
2023-11-01 02:39:39 +02:00
|
|
|
#include "source-accessor.hh"
|
2023-10-25 07:43:36 +03:00
|
|
|
#include "file-system.hh"
|
2023-09-08 05:31:19 +03:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
/**
|
2023-12-20 21:47:05 +02:00
|
|
|
* Actions on an open regular file in the process of creating it.
|
|
|
|
*
|
|
|
|
* See `FileSystemObjectSink::createRegularFile`.
|
2023-09-08 05:31:19 +03:00
|
|
|
*/
|
2023-12-20 21:47:05 +02:00
|
|
|
struct CreateRegularFileSink : Sink
|
2023-09-08 05:31:19 +03:00
|
|
|
{
|
2023-11-01 02:39:39 +02:00
|
|
|
virtual void isExecutable() = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An optimization. By default, do nothing.
|
|
|
|
*/
|
2023-09-08 05:31:19 +03:00
|
|
|
virtual void preallocateContents(uint64_t size) { };
|
2023-11-01 02:39:39 +02:00
|
|
|
};
|
|
|
|
|
2023-12-20 21:47:05 +02:00
|
|
|
|
|
|
|
struct FileSystemObjectSink
|
|
|
|
{
|
2023-12-21 10:49:52 +02:00
|
|
|
virtual ~FileSystemObjectSink() = default;
|
|
|
|
|
2023-12-20 21:47:05 +02:00
|
|
|
virtual void createDirectory(const Path & path) = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function in general is no re-entrant. Only one file can be
|
|
|
|
* written at a time.
|
|
|
|
*/
|
|
|
|
virtual void createRegularFile(
|
|
|
|
const Path & path,
|
|
|
|
std::function<void(CreateRegularFileSink &)>) = 0;
|
|
|
|
|
|
|
|
virtual void createSymlink(const Path & path, const std::string & target) = 0;
|
|
|
|
};
|
|
|
|
|
2024-02-27 07:39:30 +02:00
|
|
|
/**
|
|
|
|
* Recursively copy file system objects from the source into the sink.
|
|
|
|
*/
|
|
|
|
void copyRecursive(
|
|
|
|
SourceAccessor & accessor, const CanonPath & sourcePath,
|
|
|
|
FileSystemObjectSink & sink, const Path & destPath);
|
|
|
|
|
2023-11-01 02:39:39 +02:00
|
|
|
/**
|
|
|
|
* Ignore everything and do nothing
|
|
|
|
*/
|
2024-01-23 00:59:34 +02:00
|
|
|
struct NullFileSystemObjectSink : FileSystemObjectSink
|
2023-11-01 02:39:39 +02:00
|
|
|
{
|
|
|
|
void createDirectory(const Path & path) override { }
|
|
|
|
void createSymlink(const Path & path, const std::string & target) override { }
|
2023-12-20 21:47:05 +02:00
|
|
|
void createRegularFile(
|
|
|
|
const Path & path,
|
|
|
|
std::function<void(CreateRegularFileSink &)>) override;
|
2023-09-08 05:31:19 +03:00
|
|
|
};
|
|
|
|
|
2023-11-01 02:39:39 +02:00
|
|
|
/**
|
|
|
|
* Write files at the given path
|
|
|
|
*/
|
2024-01-23 00:59:34 +02:00
|
|
|
struct RestoreSink : FileSystemObjectSink
|
2023-09-08 05:49:01 +03:00
|
|
|
{
|
|
|
|
Path dstPath;
|
|
|
|
|
|
|
|
void createDirectory(const Path & path) override;
|
|
|
|
|
2023-12-20 21:47:05 +02:00
|
|
|
void createRegularFile(
|
|
|
|
const Path & path,
|
|
|
|
std::function<void(CreateRegularFileSink &)>) override;
|
2023-09-08 05:49:01 +03:00
|
|
|
|
|
|
|
void createSymlink(const Path & path, const std::string & target) override;
|
2023-11-01 02:39:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restore a single file at the top level, passing along
|
|
|
|
* `receiveContents` to the underlying `Sink`. For anything but a single
|
|
|
|
* file, set `regular = true` so the caller can fail accordingly.
|
|
|
|
*/
|
2024-01-23 00:59:34 +02:00
|
|
|
struct RegularFileSink : FileSystemObjectSink
|
2023-11-01 02:39:39 +02:00
|
|
|
{
|
|
|
|
bool regular = true;
|
|
|
|
Sink & sink;
|
|
|
|
|
|
|
|
RegularFileSink(Sink & sink) : sink(sink) { }
|
|
|
|
|
|
|
|
void createDirectory(const Path & path) override
|
|
|
|
{
|
|
|
|
regular = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void createSymlink(const Path & path, const std::string & target) override
|
|
|
|
{
|
|
|
|
regular = false;
|
|
|
|
}
|
|
|
|
|
2023-12-20 21:47:05 +02:00
|
|
|
void createRegularFile(
|
|
|
|
const Path & path,
|
|
|
|
std::function<void(CreateRegularFileSink &)>) override;
|
2023-09-08 05:49:01 +03:00
|
|
|
};
|
|
|
|
|
2023-09-08 05:31:19 +03:00
|
|
|
}
|