X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/bdc42211700ef0f6f40e4ef3f362e52d684d70fb..e2c8c825a5470e33c25d00e07de188d0e03922c8:/apt-pkg/contrib/fileutl.cc?ds=sidebyside diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 6bfa5ca92..affab956c 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -694,6 +694,25 @@ string flAbsPath(string File) return AbsPath; } /*}}}*/ +std::string flNormalize(std::string file) /*{{{*/ +{ + if (file.empty()) + return file; + // do some normalisation by removing // and /./ from the path + size_t found = string::npos; + while ((found = file.find("/./")) != string::npos) + file.replace(found, 3, "/"); + while ((found = file.find("//")) != string::npos) + file.replace(found, 2, "/"); + + if (APT::String::Startswith(file, "/dev/null")) + { + file.erase(strlen("/dev/null")); + return file; + } + return file; +} + /*}}}*/ // SetCloseExec - Set the close on exec flag /*{{{*/ // --------------------------------------------------------------------- /* */ @@ -916,9 +935,12 @@ bool ChangeOwnerAndPermissionOfFile(char const * const requester, char const * c // ensure the file is owned by root and has good permissions struct passwd const * const pw = getpwnam(user); struct group const * const gr = getgrnam(group); - if (pw != NULL && gr != NULL && chown(file, pw->pw_uid, gr->gr_gid) != 0) + if (pw != NULL && gr != NULL && lchown(file, pw->pw_uid, gr->gr_gid) != 0) Res &= _error->WarningE(requester, "chown to %s:%s of file %s failed", user, group, file); } + struct stat Buf; + if (lstat(file, &Buf) != 0 || S_ISLNK(Buf.st_mode)) + return Res; if (chmod(file, mode) != 0) Res &= _error->WarningE(requester, "chmod 0%o of file %s failed", mode, file); return Res; @@ -1255,9 +1277,8 @@ public: writebuffer.bufferstart += written; } - writebuffer.reset(); - return true; + return wrapped->InternalFlush(); } virtual ssize_t InternalWrite(void const * const From, unsigned long long const Size) APT_OVERRIDE { @@ -1535,7 +1556,7 @@ public: return false; unsigned int flags = (Mode & (FileFd::WriteOnly|FileFd::ReadOnly)); - if (backend.OpenDescriptor(iFd, flags) == false) + if (backend.OpenDescriptor(iFd, flags, FileFd::None, true) == false) return false; // Write the file header @@ -1646,6 +1667,11 @@ public: res = LZ4F_freeDecompressionContext(dctx); dctx = nullptr; } + if (backend.IsOpen()) + { + backend.Close(); + filefd->iFd = -1; + } return LZ4F_isError(res) == false; } @@ -1877,13 +1903,17 @@ public: if ((Mode & FileFd::ReadWrite) == FileFd::ReadWrite) return filefd->FileFdError("ReadWrite mode is not supported for file %s", filefd->FileName.c_str()); + if (compressor.Binary == "false") + return filefd->FileFdError("libapt has inbuilt support for the %s compression," + " but was forced to ignore it in favor of an external binary – which isn't installed.", compressor.Name.c_str()); bool const Comp = (Mode & FileFd::WriteOnly) == FileFd::WriteOnly; - if (Comp == false) + if (Comp == false && filefd->iFd != -1) { // Handle 'decompression' of empty files struct stat Buf; - fstat(filefd->iFd, &Buf); + if (fstat(filefd->iFd, &Buf) != 0) + return filefd->FileFdErrno("fstat", "Could not stat fd %d for file %s", filefd->iFd, filefd->FileName.c_str()); if (Buf.st_size == 0 && S_ISFIFO(Buf.st_mode) == false) return true; @@ -2372,7 +2402,7 @@ FileFd::~FileFd() gracefully. */ bool FileFd::Read(void *To,unsigned long long Size,unsigned long long *Actual) { - if (d == nullptr) + if (d == nullptr || Failed()) return false; ssize_t Res = 1; errno = 0; @@ -2414,6 +2444,37 @@ bool FileFd::Read(void *To,unsigned long long Size,unsigned long long *Actual) } return FileFdError(_("read, still have %llu to read but none left"), Size); +} +bool FileFd::Read(int const Fd, void *To, unsigned long long Size, unsigned long long * const Actual) +{ + ssize_t Res = 1; + errno = 0; + if (Actual != nullptr) + *Actual = 0; + *static_cast(To) = '\0'; + while (Res > 0 && Size > 0) + { + Res = read(Fd, To, Size); + if (Res < 0) + { + if (errno == EINTR) + { + Res = 1; + errno = 0; + continue; + } + return _error->Errno("read", _("Read error")); + } + To = static_cast(To) + Res; + Size -= Res; + if (Actual != 0) + *Actual += Res; + } + if (Size == 0) + return true; + if (Actual != nullptr) + return true; + return _error->Error(_("read, still have %llu to read but none left"), Size); } /*}}}*/ // FileFd::ReadLine - Read a complete line from the file /*{{{*/ @@ -2423,7 +2484,7 @@ bool FileFd::Read(void *To,unsigned long long Size,unsigned long long *Actual) char* FileFd::ReadLine(char *To, unsigned long long const Size) { *To = '\0'; - if (d == nullptr) + if (d == nullptr || Failed()) return nullptr; return d->InternalReadLine(To, Size); } @@ -2431,6 +2492,8 @@ char* FileFd::ReadLine(char *To, unsigned long long const Size) // FileFd::Flush - Flush the file /*{{{*/ bool FileFd::Flush() { + if (Failed()) + return false; if (d == nullptr) return true; @@ -2440,7 +2503,7 @@ bool FileFd::Flush() // FileFd::Write - Write to the file /*{{{*/ bool FileFd::Write(const void *From,unsigned long long Size) { - if (d == nullptr) + if (d == nullptr || Failed()) return false; ssize_t Res = 1; errno = 0; @@ -2496,7 +2559,7 @@ bool FileFd::Write(int Fd, const void *From, unsigned long long Size) // FileFd::Seek - Seek in the file /*{{{*/ bool FileFd::Seek(unsigned long long To) { - if (d == nullptr) + if (d == nullptr || Failed()) return false; Flags &= ~HitEof; return d->InternalSeek(To); @@ -2505,7 +2568,7 @@ bool FileFd::Seek(unsigned long long To) // FileFd::Skip - Skip over data in the file /*{{{*/ bool FileFd::Skip(unsigned long long Over) { - if (d == nullptr) + if (d == nullptr || Failed()) return false; return d->InternalSkip(Over); } @@ -2513,7 +2576,7 @@ bool FileFd::Skip(unsigned long long Over) // FileFd::Truncate - Truncate the file /*{{{*/ bool FileFd::Truncate(unsigned long long To) { - if (d == nullptr) + if (d == nullptr || Failed()) return false; // truncating /dev/null is always successful - as we get an error otherwise if (To == 0 && FileName == "/dev/null") @@ -2526,7 +2589,7 @@ bool FileFd::Truncate(unsigned long long To) /* */ unsigned long long FileFd::Tell() { - if (d == nullptr) + if (d == nullptr || Failed()) return false; off_t const Res = d->InternalTell(); if (Res == (off_t)-1) @@ -2589,7 +2652,7 @@ time_t FileFd::ModificationTime() unsigned long long FileFd::Size() { if (d == nullptr) - return false; + return 0; return d->InternalSize(); } /*}}}*/ @@ -2618,7 +2681,7 @@ bool FileFd::Close() } if ((Flags & Replace) == Replace) { - if (rename(TemporaryFileName.c_str(), FileName.c_str()) != 0) + if (Failed() == false && rename(TemporaryFileName.c_str(), FileName.c_str()) != 0) Res &= _error->Errno("rename",_("Problem renaming the file %s to %s"), TemporaryFileName.c_str(), FileName.c_str()); FileName = TemporaryFileName; // for the unlink() below. @@ -2653,13 +2716,12 @@ bool FileFd::FileFdErrno(const char *Function, const char *Description,...) va_list args; size_t msgSize = 400; int const errsv = errno; - while (true) - { + bool retry; + do { va_start(args,Description); - if (_error->InsertErrno(GlobalError::ERROR, Function, Description, args, errsv, msgSize) == false) - break; + retry = _error->InsertErrno(GlobalError::ERROR, Function, Description, args, errsv, msgSize); va_end(args); - } + } while (retry); return false; } /*}}}*/ @@ -2668,13 +2730,12 @@ bool FileFd::FileFdError(const char *Description,...) { Flags |= Fail; va_list args; size_t msgSize = 400; - while (true) - { + bool retry; + do { va_start(args,Description); - if (_error->Insert(GlobalError::ERROR, Description, args, msgSize) == false) - break; + retry = _error->Insert(GlobalError::ERROR, Description, args, msgSize); va_end(args); - } + } while (retry); return false; } /*}}}*/ @@ -2717,9 +2778,9 @@ std::vector Glob(std::string const &pattern, int flags) return result; } /*}}}*/ -std::string GetTempDir() /*{{{*/ +static std::string APT_NONNULL(1) GetTempDirEnv(char const * const env) /*{{{*/ { - const char *tmpdir = getenv("TMPDIR"); + const char *tmpdir = getenv(env); #ifdef P_tmpdir if (!tmpdir) @@ -2735,6 +2796,11 @@ std::string GetTempDir() /*{{{*/ tmpdir = "/tmp"; return string(tmpdir); +} + /*}}}*/ +std::string GetTempDir() /*{{{*/ +{ + return GetTempDirEnv("TMPDIR"); } std::string GetTempDir(std::string const &User) { @@ -2799,6 +2865,11 @@ bool Rename(std::string From, std::string To) /*{{{*/ } /*}}}*/ bool Popen(const char* Args[], FileFd &Fd, pid_t &Child, FileFd::OpenMode Mode)/*{{{*/ +{ + return Popen(Args, Fd, Child, Mode, true); +} + /*}}}*/ +bool Popen(const char* Args[], FileFd &Fd, pid_t &Child, FileFd::OpenMode Mode, bool CaptureStderr)/*{{{*/ { int fd; if (Mode != FileFd::ReadOnly && Mode != FileFd::WriteOnly) @@ -2830,7 +2901,8 @@ bool Popen(const char* Args[], FileFd &Fd, pid_t &Child, FileFd::OpenMode Mode)/ if(Mode == FileFd::ReadOnly) { dup2(fd, 1); - dup2(fd, 2); + if (CaptureStderr == true) + dup2(fd, 2); } else if(Mode == FileFd::WriteOnly) dup2(fd, 0); @@ -2984,6 +3056,32 @@ bool DropPrivileges() /*{{{*/ return _error->Error("Could restore a uid to root, privilege dropping did not work"); } + if (_config->FindB("APT::Sandbox::ResetEnvironment", true)) + { + setenv("HOME", pw->pw_dir, 1); + setenv("USER", pw->pw_name, 1); + setenv("USERNAME", pw->pw_name, 1); + setenv("LOGNAME", pw->pw_name, 1); + auto const shell = flNotDir(pw->pw_shell); + if (shell == "false" || shell == "nologin") + setenv("SHELL", "/bin/sh", 1); + else + setenv("SHELL", pw->pw_shell, 1); + auto const apt_setenv_tmp = [](char const * const env) { + auto const tmpdir = getenv(env); + if (tmpdir != nullptr) + { + auto const ourtmpdir = GetTempDirEnv(env); + if (ourtmpdir != tmpdir) + setenv(env, ourtmpdir.c_str(), 1); + } + }; + apt_setenv_tmp("TMPDIR"); + apt_setenv_tmp("TEMPDIR"); + apt_setenv_tmp("TMP"); + apt_setenv_tmp("TEMP"); + } + return true; } /*}}}*/