aboutsummaryrefslogtreecommitdiff
path: root/gnu/packages/flex.scm
diff options
context:
space:
mode:
Diffstat (limited to 'gnu/packages/flex.scm')
0 files changed, 0 insertions, 0 deletions
veVersion1, sink); dump(path, sink, filter); } static SerialisationError badArchive(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; unsigned char buf[65536]; while (left) { checkInterrupt(); unsigned int n = sizeof(buf); if ((unsigned long long) n > left) n = left; source(buf, n); sink.receiveContents(buf, n); left -= n; } readPadding(size, source); } struct CaseInsensitiveCompare { bool operator() (const string & a, const string & b) const { return strcasecmp(a.c_str(), b.c_str()) < 0; } }; static void parse(ParseSink & sink, Source & source, const Path & path) { 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 (1) { checkInterrupt(); s = readString(source); if (s == ")") { break; } else if (s == "type") { if (type != tpUnknown) throw badArchive("multiple type fields"); 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) { readString(source); sink.isExecutable(); } else if (s == "entry" && type == tpDirectory) { string name, prevName; s = readString(source); if (s != "(") throw badArchive("expected open tag"); while (1) { checkInterrupt(); s = readString(source); if (s == ")") { break; } else if (s == "name") { name = readString(source); if (name.empty() || name == "." || name == ".." || name.find('/') != string::npos || name.find((char) 0) != string::npos) throw Error(format("NAR contains invalid file name `%1%'") % name); if (name <= prevName) throw Error("NAR directory is not sorted"); prevName = name; } 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) { string target = readString(source); sink.createSymlink(path, target); } else throw badArchive("unknown field " + s); } } void parseDump(ParseSink & sink, Source & source) { string version; try { version = readString(source); } catch (SerialisationError & e) { /* This generally means the integer at the start couldn't be decoded. Ignore and throw the exception below. */ } if (version != archiveVersion1) throw badArchive("input doesn't look like a normalized archive"); parse(sink, source, ""); } struct RestoreSink : ParseSink { Path dstPath; AutoCloseFD fd; void createDirectory(const Path & path) { Path p = dstPath + path; if (mkdir(p.c_str(), 0777) == -1) throw SysError(format("creating directory `%1%'") % p); }; void createRegularFile(const Path & path) { Path p = dstPath + path; fd.close(); fd = open(p.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0666); if (fd == -1) throw SysError(format("creating file `%1%'") % p); } void isExecutable() { struct stat st; if (fstat(fd, &st) == -1) throw SysError("fstat"); if (fchmod(fd, st.st_mode | (S_IXUSR | S_IXGRP | S_IXOTH)) == -1) throw SysError("fchmod"); } void preallocateContents(unsigned long long len) { #if HAVE_POSIX_FALLOCATE if (len) { errno = posix_fallocate(fd, 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) throw SysError(format("preallocating file of %1% bytes") % len); } #endif } void receiveContents(unsigned char * data, unsigned int len) { writeFull(fd, data, len); } void createSymlink(const Path & path, const string & target) { Path p = dstPath + path; nix::createSymlink(target, p); } }; void restorePath(const Path & path, Source & source) { RestoreSink sink; sink.dstPath = path; parseDump(sink, source); } }