about summary refs log tree commit diff
path: root/third_party/nix/src/libutil/archive.cc
blob: 4489b7d2a8c5c34d53792ef0104aee26a9b0cd48 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#include "libutil/archive.hh"

#include <algorithm>
#include <cerrno>
#include <map>
#include <vector>

#include <dirent.h>
#include <fcntl.h>
#include <glog/logging.h>
#include <strings.h>  // for strcasecmp
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include "libutil/config.hh"
#include "libutil/util.hh"

namespace nix {

struct ArchiveSettings : Config {
  Setting<bool> useCaseHack {
    this,
#if __APPLE__
        true,
#else
        false,
#endif
        "use-case-hack",
        "Whether to enable a Darwin-specific hack for dealing with file name "
        "collisions."
  };
};

static ArchiveSettings archiveSettings;

static GlobalConfig::Register r1(&archiveSettings);

const std::string narVersionMagic1 = "nix-archive-1";

static std::string caseHackSuffix = "~nix~case~hack~";

PathFilter defaultPathFilter = [](const Path& /*unused*/) { return true; };

static void dumpContents(const Path& path, size_t size, Sink& sink) {
  sink << "contents" << size;

  AutoCloseFD fd = open(path.c_str(), O_RDONLY | O_CLOEXEC);
  if (!fd) {
    throw SysError(format("opening file '%1%'") % path);
  }

  std::vector<unsigned char> buf(65536);
  size_t left = size;

  while (left > 0) {
    auto n = std::min(left, buf.size());
    readFull(fd.get(), buf.data(), n);
    left -= n;
    sink(buf.data(), n);
  }

  writePadding(size, sink);
}

static void dump(const Path& path, Sink& sink, PathFilter& filter) {
  checkInterrupt();

  struct stat st;
  if (lstat(path.c_str(), &st) != 0) {
    throw SysError(format("getting attributes of path '%1%'") % path);
  }

  sink << "(";

  if (S_ISREG(st.st_mode)) {
    sink << "type"
         << "regular";
    if ((st.st_mode & S_IXUSR) != 0u) {
      sink << "executable"
           << "";
    }
    dumpContents(path, static_cast<size_t>(st.st_size), sink);
  }

  else if (S_ISDIR(st.st_mode)) {
    sink << "type"
         << "directory";

    /* If we're on a case-insensitive system like macOS, undo
       the case hack applied by restorePath(). */
    std::map<std::string, std::string> unhacked;
    for (auto& i : readDirectory(path)) {
      if (archiveSettings.useCaseHack) {
        std::string name(i.name);
        size_t pos = i.name.find(caseHackSuffix);
        if (pos != std::string::npos) {
          DLOG(INFO) << "removing case hack suffix from " << path << "/"
                     << i.name;

          name.erase(pos);
        }
        if (unhacked.find(name) != unhacked.end()) {
          throw Error(format("file name collision in between '%1%' and '%2%'") %
                      (path + "/" + unhacked[name]) % (path + "/" + i.name));
        }
        unhacked[name] = i.name;
      } else {
        unhacked[i.name] = i.name;
      }
    }

    for (auto& i : unhacked) {
      if (filter(path + "/" + i.first)) {
        sink << "entry"
             << "("
             << "name" << i.first << "node";
        dump(path + "/" + i.second, sink, filter);
        sink << ")";
      }
    }
  }

  else if (S_ISLNK(st.st_mode)) {
    sink << "type"
         << "symlink"
         << "target" << readLink(path);

  } else {
    throw Error(format("file '%1%' has an unsupported type") % path);
  }

  sink << ")";
}

void dumpPath(const Path& path, Sink& sink, PathFilter& filter) {
  sink << narVersionMagic1;
  dump(path, sink, filter);
}

void dumpString(const std::string& s, Sink& sink) {
  sink << narVersionMagic1 << "("
       << "type"
       << "regular"
       << "contents" << s << ")";
}

static SerialisationError badArchive(const std::string& s) {
  return SerialisationError("bad archive: " + s);
}

#if 0
static void skipGeneric(Source & source)
{
    if (readString(source) == "(") {
        while (readString(source) != ")")
            skipGeneric(source);
    }
}
#endif

static void parseContents(ParseSink& sink, Source& source, const Path& path) {
  unsigned long long size = readLongLong(source);

  sink.preallocateContents(size);

  unsigned long long left = size;
  std::vector<unsigned char> buf(65536);

  while (left != 0u) {
    checkInterrupt();
    auto n = buf.size();
    if (static_cast<unsigned long long>(n) > left) {
      n = left;
    }
    source(buf.data(), n);
    sink.receiveContents(buf.data(), n);
    left -= n;
  }

  readPadding(size, source);
}

struct CaseInsensitiveCompare {
  bool operator()(const std::string& a, const std::string& b) const {
    return strcasecmp(a.c_str(), b.c_str()) < 0;
  }
};

static void parse(ParseSink& sink, Source& source, const Path& path) {
  std::string s;

  s = readString(source);
  if (s != "(") {
    throw badArchive("expected open tag");
  }

  enum { tpUnknown, tpRegular, tpDirectory, tpSymlink } type = tpUnknown;

  std::map<Path, int, CaseInsensitiveCompare> names;

  while (true) {
    checkInterrupt();

    s = readString(source);

    if (s == ")") {
      break;
    }

    if (s == "type") {
      if (type != tpUnknown) {
        throw badArchive("multiple type fields");
      }
      std::string t = readString(source);

      if (t == "regular") {
        type = tpRegular;
        sink.createRegularFile(path);
      }

      else if (t == "directory") {
        sink.createDirectory(path);
        type = tpDirectory;
      }

      else if (t == "symlink") {
        type = tpSymlink;
      }

      else {
        throw badArchive("unknown file type " + t);
      }

    }

    else if (s == "contents" && type == tpRegular) {
      parseContents(sink, source, path);
    }

    else if (s == "executable" && type == tpRegular) {
      auto s = readString(source);
      if (!s.empty()) {
        throw badArchive("executable marker has non-empty value");
      }
      sink.isExecutable();
    }

    else if (s == "entry" && type == tpDirectory) {
      std::string name;
      std::string prevName;

      s = readString(source);
      if (s != "(") {
        throw badArchive("expected open tag");
      }

      while (true) {
        checkInterrupt();

        s = readString(source);

        if (s == ")") {
          break;
        }
        if (s == "name") {
          name = readString(source);
          if (name.empty() || name == "." || name == ".." ||
              name.find('/') != std::string::npos ||
              name.find(static_cast<char>(0)) != std::string::npos) {
            throw Error(format("NAR contains invalid file name '%1%'") % name);
          }
          if (name <= prevName) {
            throw Error("NAR directory is not sorted");
          }
          prevName = name;
          if (archiveSettings.useCaseHack) {
            auto i = names.find(name);
            if (i != names.end()) {
              DLOG(INFO) << "case collision between '" << i->first << "' and '"
                         << name << "'";
              name += caseHackSuffix;
              name += std::to_string(++i->second);
            } else {
              names[name] = 0;
            }
          }
        } else if (s == "node") {
          if (s.empty()) {
            throw badArchive("entry name missing");
          }
          parse(sink, source, path + "/" + name);
        } else {
          throw badArchive("unknown field " + s);
        }
      }
    }

    else if (s == "target" && type == tpSymlink) {
      std::string target = readString(source);
      sink.createSymlink(path, target);
    }

    else {
      throw badArchive("unknown field " + s);
    }
  }
}

void parseDump(ParseSink& sink, Source& source) {
  std::string version;
  try {
    version = readString(source, narVersionMagic1.size());
  } catch (SerialisationError& e) {
    /* This generally means the integer at the start couldn't be
       decoded.  Ignore and throw the exception below. */
  }
  if (version != narVersionMagic1) {
    throw badArchive("input doesn't look like a Nix archive");
  }
  parse(sink, source, "");
}

struct RestoreSink : ParseSink {
  Path dstPath;
  AutoCloseFD fd;

  void createDirectory(const Path& path) override {
    Path p = dstPath + path;
    if (mkdir(p.c_str(), 0777) == -1) {
      throw SysError(format("creating directory '%1%'") % p);
    }
  };

  void createRegularFile(const Path& path) override {
    Path p = dstPath + path;
    fd = open(p.c_str(), O_CREAT | O_EXCL | O_WRONLY | O_CLOEXEC, 0666);
    if (!fd) {
      throw SysError(format("creating file '%1%'") % p);
    }
  }

  void isExecutable() override {
    struct stat st;
    if (fstat(fd.get(), &st) == -1) {
      throw SysError("fstat");
    }
    if (fchmod(fd.get(), st.st_mode | (S_IXUSR | S_IXGRP | S_IXOTH)) == -1) {
      throw SysError("fchmod");
    }
  }

  void preallocateContents(unsigned long long len) override {
#if HAVE_POSIX_FALLOCATE
    if (len != 0u) {
      errno = posix_fallocate(fd.get(), 0, len);
      /* Note that EINVAL may indicate that the underlying
         filesystem doesn't support preallocation (e.g. on
         OpenSolaris).  Since preallocation is just an
         optimisation, ignore it. */
      if (errno && errno != EINVAL && errno != EOPNOTSUPP && errno != ENOSYS) {
        throw SysError(format("preallocating file of %1% bytes") % len);
      }
    }
#endif
  }

  void receiveContents(unsigned char* data, unsigned int len) override {
    writeFull(fd.get(), data, len);
  }

  void createSymlink(const Path& path, const std::string& target) override {
    Path p = dstPath + path;
    nix::createSymlink(target, p);
  }
};

void restorePath(const Path& path, Source& source) {
  RestoreSink sink;
  sink.dstPath = path;
  parseDump(sink, source);
}

void copyNAR(Source& source, Sink& sink) {
  // FIXME: if 'source' is the output of dumpPath() followed by EOF,
  // we should just forward all data directly without parsing.

  ParseSink parseSink; /* null sink; just parse the NAR */

  LambdaSource wrapper([&](unsigned char* data, size_t len) {
    auto n = source.read(data, len);
    sink(data, n);
    return n;
  });

  parseDump(parseSink, wrapper);
}

}  // namespace nix