2012-07-18 18:59:03 +00:00
|
|
|
#pragma once
|
2023-04-01 03:18:41 +00:00
|
|
|
///@file
|
2003-06-20 10:40:25 +00:00
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
#include "types.hh"
|
2006-11-30 19:19:59 +00:00
|
|
|
#include "serialise.hh"
|
2006-09-04 21:06:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace nix {
|
2003-06-20 10:40:25 +00:00
|
|
|
|
|
|
|
|
2023-03-27 01:12:25 +00:00
|
|
|
/**
|
|
|
|
* dumpPath creates a Nix archive of the specified path.
|
|
|
|
*
|
|
|
|
* @param path the file system data to dump. Dumping is recursive so if
|
|
|
|
* this is a directory we dump it and all its children.
|
|
|
|
*
|
|
|
|
* @param [out] sink The serialised archive is fed into this sink.
|
|
|
|
*
|
|
|
|
* @param filter Can be used to skip certain files.
|
|
|
|
*
|
|
|
|
* The format is as follows:
|
|
|
|
*
|
2023-04-07 13:55:28 +00:00
|
|
|
* ```
|
|
|
|
* IF path points to a REGULAR FILE:
|
|
|
|
* dump(path) = attrs(
|
|
|
|
* [ ("type", "regular")
|
|
|
|
* , ("contents", contents(path))
|
|
|
|
* ])
|
2023-03-27 01:12:25 +00:00
|
|
|
*
|
2023-04-07 13:55:28 +00:00
|
|
|
* IF path points to a DIRECTORY:
|
|
|
|
* dump(path) = attrs(
|
|
|
|
* [ ("type", "directory")
|
|
|
|
* , ("entries", concat(map(f, sort(entries(path)))))
|
|
|
|
* ])
|
|
|
|
* where f(fn) = attrs(
|
|
|
|
* [ ("name", fn)
|
|
|
|
* , ("file", dump(path + "/" + fn))
|
|
|
|
* ])
|
2023-03-27 01:12:25 +00:00
|
|
|
*
|
2023-04-07 13:55:28 +00:00
|
|
|
* where:
|
2023-03-27 01:12:25 +00:00
|
|
|
*
|
2023-04-07 13:55:28 +00:00
|
|
|
* attrs(as) = concat(map(attr, as)) + encN(0)
|
|
|
|
* attrs((a, b)) = encS(a) + encS(b)
|
2023-03-27 01:12:25 +00:00
|
|
|
*
|
2023-04-07 13:55:28 +00:00
|
|
|
* encS(s) = encN(len(s)) + s + (padding until next 64-bit boundary)
|
2023-03-27 01:12:25 +00:00
|
|
|
*
|
2023-04-07 13:55:28 +00:00
|
|
|
* encN(n) = 64-bit little-endian encoding of n.
|
2023-03-27 01:12:25 +00:00
|
|
|
*
|
2023-04-07 13:55:28 +00:00
|
|
|
* contents(path) = the contents of a regular file.
|
2023-03-27 01:12:25 +00:00
|
|
|
*
|
2023-04-07 13:55:28 +00:00
|
|
|
* sort(strings) = lexicographic sort by 8-bit value (strcmp).
|
2023-03-27 01:12:25 +00:00
|
|
|
*
|
2023-04-07 13:55:28 +00:00
|
|
|
* entries(path) = the entries of a directory, without `.` and
|
|
|
|
* `..`.
|
2023-03-27 01:12:25 +00:00
|
|
|
*
|
2023-04-07 13:55:28 +00:00
|
|
|
* `+` denotes string concatenation.
|
|
|
|
* ```
|
2023-03-27 01:12:25 +00:00
|
|
|
*/
|
2006-12-12 21:51:02 +00:00
|
|
|
void dumpPath(const Path & path, Sink & sink,
|
2006-12-12 23:05:01 +00:00
|
|
|
PathFilter & filter = defaultPathFilter);
|
2003-06-20 10:40:25 +00:00
|
|
|
|
2023-03-27 01:12:25 +00:00
|
|
|
/**
|
|
|
|
* Same as dumpPath(), but returns the last modified date of the path.
|
|
|
|
*/
|
2022-02-18 15:56:35 +00:00
|
|
|
time_t dumpPathAndGetMtime(const Path & path, Sink & sink,
|
|
|
|
PathFilter & filter = defaultPathFilter);
|
|
|
|
|
2023-03-27 01:12:25 +00:00
|
|
|
/**
|
|
|
|
* Dump an archive with a single file with these contents.
|
|
|
|
*
|
|
|
|
* @param s Contents of the file.
|
|
|
|
*/
|
2022-02-25 15:00:00 +00:00
|
|
|
void dumpString(std::string_view s, Sink & sink);
|
2016-02-24 15:52:28 +00:00
|
|
|
|
2023-03-27 01:12:25 +00:00
|
|
|
/**
|
|
|
|
* \todo Fix this API, it sucks.
|
|
|
|
*/
|
2008-12-03 17:30:32 +00:00
|
|
|
struct ParseSink
|
|
|
|
{
|
|
|
|
virtual void createDirectory(const Path & path) { };
|
2014-07-16 14:02:05 +00:00
|
|
|
|
2008-12-03 17:30:32 +00:00
|
|
|
virtual void createRegularFile(const Path & path) { };
|
2022-09-22 19:47:43 +00:00
|
|
|
virtual void closeRegularFile() { };
|
2008-12-03 17:30:32 +00:00
|
|
|
virtual void isExecutable() { };
|
2020-07-30 11:10:49 +00:00
|
|
|
virtual void preallocateContents(uint64_t size) { };
|
2020-12-02 13:00:43 +00:00
|
|
|
virtual void receiveContents(std::string_view data) { };
|
2008-12-03 17:30:32 +00:00
|
|
|
|
2022-02-25 15:00:00 +00:00
|
|
|
virtual void createSymlink(const Path & path, const std::string & target) { };
|
2008-12-03 17:30:32 +00:00
|
|
|
};
|
2014-07-16 14:02:05 +00:00
|
|
|
|
2023-03-27 01:12:25 +00:00
|
|
|
/**
|
|
|
|
* If the NAR archive contains a single file at top-level, then save
|
2023-04-07 13:55:28 +00:00
|
|
|
* the contents of the file to `s`. Otherwise barf.
|
2023-03-27 01:12:25 +00:00
|
|
|
*/
|
2020-07-16 05:09:41 +00:00
|
|
|
struct RetrieveRegularNARSink : ParseSink
|
2017-03-01 15:16:04 +00:00
|
|
|
{
|
2020-07-16 05:09:41 +00:00
|
|
|
bool regular = true;
|
|
|
|
Sink & sink;
|
2017-03-01 15:16:04 +00:00
|
|
|
|
2020-07-16 05:09:41 +00:00
|
|
|
RetrieveRegularNARSink(Sink & sink) : sink(sink) { }
|
|
|
|
|
2020-12-02 13:00:43 +00:00
|
|
|
void createDirectory(const Path & path) override
|
2020-07-16 05:09:41 +00:00
|
|
|
{
|
|
|
|
regular = false;
|
|
|
|
}
|
|
|
|
|
2020-12-02 13:00:43 +00:00
|
|
|
void receiveContents(std::string_view data) override
|
2020-07-16 05:09:41 +00:00
|
|
|
{
|
2020-12-02 13:00:43 +00:00
|
|
|
sink(data);
|
2020-07-16 05:09:41 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:00:00 +00:00
|
|
|
void createSymlink(const Path & path, const std::string & target) override
|
2020-07-16 05:09:41 +00:00
|
|
|
{
|
|
|
|
regular = false;
|
|
|
|
}
|
2017-03-01 15:16:04 +00:00
|
|
|
};
|
|
|
|
|
2008-12-03 17:30:32 +00:00
|
|
|
void parseDump(ParseSink & sink, Source & source);
|
|
|
|
|
2006-11-30 19:19:59 +00:00
|
|
|
void restorePath(const Path & path, Source & source);
|
2006-09-04 21:06:23 +00:00
|
|
|
|
2023-03-27 01:12:25 +00:00
|
|
|
/**
|
|
|
|
* Read a NAR from 'source' and write it to 'sink'.
|
|
|
|
*/
|
2018-03-21 21:56:02 +00:00
|
|
|
void copyNAR(Source & source, Sink & sink);
|
|
|
|
|
2018-10-01 15:05:55 +00:00
|
|
|
void copyPath(const Path & from, const Path & to);
|
|
|
|
|
2014-07-16 14:02:05 +00:00
|
|
|
|
2022-12-07 11:58:58 +00:00
|
|
|
inline constexpr std::string_view narVersionMagic1 = "nix-archive-1";
|
|
|
|
|
|
|
|
inline constexpr std::string_view caseHackSuffix = "~nix~case~hack~";
|
2016-02-24 15:52:28 +00:00
|
|
|
|
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
}
|