tarfile.cc: Don't change the cwd

Nix is multithreaded so it's not safe to change the cwd.
This commit is contained in:
Eelco Dolstra 2019-12-19 15:08:16 +01:00
parent be32da0ed0
commit 2550c11373
No known key found for this signature in database
GPG key ID: 8170B4726D7198DE

View file

@ -80,34 +80,12 @@ private:
} }
}; };
struct PushD {
char * oldDir;
PushD(const std::string &newDir) {
oldDir = getcwd(0, 0);
if (!oldDir) throw SysError("getcwd");
int r = chdir(newDir.c_str());
if (r != 0) throw SysError("changing directory to tar output path");
}
~PushD() {
int r = chdir(oldDir);
free(oldDir);
if (r != 0)
warn("failed to change directory back after tar extraction");
/* can't throw out of a destructor */
}
};
static void extract_archive(TarArchive & archive, const Path & destDir) static void extract_archive(TarArchive & archive, const Path & destDir)
{ {
// need to chdir back *after* archive closing
PushD newDir(destDir);
int flags = ARCHIVE_EXTRACT_FFLAGS int flags = ARCHIVE_EXTRACT_FFLAGS
| ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_PERM
| ARCHIVE_EXTRACT_SECURE_SYMLINKS | ARCHIVE_EXTRACT_SECURE_SYMLINKS
| ARCHIVE_EXTRACT_SECURE_NODOTDOT | ARCHIVE_EXTRACT_SECURE_NODOTDOT;
| ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS;
for (;;) { for (;;) {
struct archive_entry * entry; struct archive_entry * entry;
@ -118,6 +96,9 @@ static void extract_archive(TarArchive & archive, const Path & destDir)
else else
archive.check(r); archive.check(r);
archive_entry_set_pathname(entry,
(destDir + "/" + archive_entry_pathname(entry)).c_str());
archive.check(archive_read_extract(archive.archive, entry, flags)); archive.check(archive_read_extract(archive.archive, entry, flags));
} }