apt  2.2.4
About: Apt (Advanced Package Tool) is a management system for software packages (Debian/Ubuntu). Release series 2.2.
  Fossies Dox: apt-2.2.4.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

fileutl.cc File Reference
#include <config.h>
#include <apt-pkg/aptconfiguration.h>
#include <apt-pkg/configuration.h>
#include <apt-pkg/error.h>
#include <apt-pkg/fileutl.h>
#include <apt-pkg/macros.h>
#include <apt-pkg/pkgsystem.h>
#include <apt-pkg/strutl.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <glob.h>
#include <grp.h>
#include <pwd.h>
#include <signal.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include <algorithm>
#include <memory>
#include <set>
#include <endian.h>
#include <stdint.h>
#include <apti18n.h>
Include dependency graph for fileutl.cc:

Go to the source code of this file.

Classes

struct  simple_buffer
 
class  FileFdPrivate
 
class  BufferedWriteFileFdPrivate
 
class  GzipFileFdPrivate
 
class  Bz2FileFdPrivate
 
class  Lz4FileFdPrivate
 
class  ZstdFileFdPrivate
 
class  LzmaFileFdPrivate
 
class  PipedFileFdPrivate
 
class  DirectFileFdPrivate
 

Macros

#define if_FLAGGED_SET(FLAG, MODE)   if ((OpenMode & FLAG) == FLAG) fileflags |= MODE
 
#define APT_COMPRESS_INIT(NAME, CONSTRUCTOR)
 

Functions

bool RunScripts (const char *Cnf)
 
bool CopyFile (FileFd &From, FileFd &To)
 
bool RemoveFileAt (char const *const Function, int const dirfd, std::string const &FileName)
 
bool RemoveFile (char const *const Function, std::string const &FileName)
 
static std::string GetProcessName (int pid)
 
int GetLock (string File, bool Errors)
 
bool FileExists (string File)
 
bool RealFileExists (string File)
 
bool DirectoryExists (string const &Path)
 
bool CreateDirectory (string const &Parent, string const &Path)
 
bool CreateAPTDirectoryIfNeeded (string const &Parent, string const &Path)
 Ensure the existence of the given Path. More...
 
std::vector< string > GetListOfFilesInDir (string const &Dir, string const &Ext, bool const &SortList, bool const &AllowNoExt)
 
std::vector< string > GetListOfFilesInDir (string const &Dir, std::vector< string > const &Ext, bool const &SortList)
 
std::vector< string > GetListOfFilesInDir (string const &Dir, bool SortList)
 
string SafeGetCWD ()
 
time_t GetModificationTime (string const &Path)
 
string flNotDir (string File)
 
string flNotFile (string File)
 
string flExtension (string File)
 
string flNoLink (string File)
 
string flCombine (string Dir, string File)
 
string flAbsPath (string File)
 Takes a file path and returns the absolute path. More...
 
std::string flNormalize (std::string file)
 removes superfluous /./ and // from path More...
 
void SetCloseExec (int Fd, bool Close)
 
void SetNonBlock (int Fd, bool Block)
 
bool WaitFd (int Fd, bool write, unsigned long timeout)
 
void MergeKeepFdsFromConfiguration (std::set< int > &KeepFDs)
 
pid_t ExecFork ()
 
pid_t ExecFork (std::set< int > KeepFDs)
 
bool ExecWait (pid_t Pid, const char *Name, bool Reap)
 
bool StartsWithGPGClearTextSignature (string const &FileName)
 
bool ChangeOwnerAndPermissionOfFile (char const *const requester, char const *const file, char const *const user, char const *const group, mode_t const mode)
 
static bool StatFileFd (char const *const msg, int const iFd, std::string const &FileName, struct stat &Buf, FileFdPrivate *const d)
 
std::vector< std::string > Glob (std::string const &pattern, int flags)
 
static std::string APT_NONNULL (1) GetTempDirEnv(char const *const env)
 
std::string GetTempDir ()
 
std::string GetTempDir (std::string const &User)
 
FileFdGetTempFile (std::string const &Prefix, bool ImmediateUnlink, FileFd *const TmpFd)
 
FileFdGetTempFile (std::string const &Prefix, bool ImmediateUnlink, FileFd *const TmpFd, bool Buffered)
 
bool Rename (std::string From, std::string To)
 
bool Popen (const char *Args[], FileFd &Fd, pid_t &Child, FileFd::OpenMode Mode, bool CaptureStderr, bool Sandbox)
 Popen() implementation that execv() instead of using a shell. More...
 
bool DropPrivileges ()
 Drop privileges. More...
 
bool OpenConfigurationFileFd (std::string const &File, FileFd &Fd)
 
int Inhibit (const char *what, const char *who, const char *why, const char *mode)
 

Macro Definition Documentation

◆ APT_COMPRESS_INIT

#define APT_COMPRESS_INIT (   NAME,
  CONSTRUCTOR 
)
Value:
else if (compressor.Name == NAME) \
d = new CONSTRUCTOR(this)

◆ if_FLAGGED_SET

#define if_FLAGGED_SET (   FLAG,
  MODE 
)    if ((OpenMode & FLAG) == FLAG) fileflags |= MODE

Function Documentation

◆ APT_NONNULL()

static std::string APT_NONNULL ( ) const
static

Definition at line 3083 of file fileutl.cc.

◆ ChangeOwnerAndPermissionOfFile()

bool ChangeOwnerAndPermissionOfFile ( char const *const  requester,
char const *const  file,
char const *const  user,
char const *const  group,
mode_t const  mode 
)

change file attributes to requested known good values

The method skips the user:group setting if not root.

Parameters
requesteris printed as functionname in error cases
fileis the file to be modified
useris the (new) owner of the file, e.g. _apt
groupis the (new) group owning the file, e.g. root
modeis the access mode of the file, e.g. 0644

Definition at line 1015 of file fileutl.cc.

Referenced by SigVerify::CopyMetaIndex(), IndexCopy::CopyPackages(), TranslationsCopy::CopyTranslations(), pkgAcqAuxFile::Finished(), GetAuxFileNameFromURI(), and pkgAcqChangelog::Init().

◆ CopyFile()

◆ CreateAPTDirectoryIfNeeded()

bool CreateAPTDirectoryIfNeeded ( std::string const &  Parent,
std::string const &  Path 
)

Ensure the existence of the given Path.

Parameters
Parentdirectory of the Path directory - a trailing /apt/ will be removed before CreateDirectory call.
Pathwhich should exist after (successful) call

Definition at line 400 of file fileutl.cc.

References CreateDirectory(), and DirectoryExists().

Referenced by pkgCdrom::Add(), CreateDumpFile(), ExecuteExternal(), pkgDPkgPM::OpenLog(), SetupAPTPartialDirectory(), and pkgDepCache::writeStateFile().

◆ CreateDirectory()

bool CreateDirectory ( string const &  Parent,
string const &  Path 
)

◆ DirectoryExists()

◆ DropPrivileges()

bool DropPrivileges ( )

Drop privileges.

Drop the privileges to the user _apt (or the one specified in APT::Sandbox::User). This does not set the supplementary group ids up correctly, it only uses the default group. Also prevent the process from gaining any new privileges afterwards, at least on Linux.

Returns
true on success, false on failure with _error set

Definition at line 3260 of file fileutl.cc.

References _config, and flNotDir().

Referenced by DropPrivsAndRun(), pkgAcqMethod::DropPrivsOrDie(), ExecuteExternal(), main(), and Popen().

◆ ExecFork() [1/2]

◆ ExecFork() [2/2]

pid_t ExecFork ( std::set< int >  KeepFDs)

Definition at line 890 of file fileutl.cc.

References DIR.

◆ ExecWait()

◆ FileExists()

◆ flAbsPath()

string flAbsPath ( string  File)

Takes a file path and returns the absolute path.

Definition at line 757 of file fileutl.cc.

Referenced by pkgSourceList::AddVolatileFile(), and pkgDebianIndexRealFile::pkgDebianIndexRealFile().

◆ flCombine()

◆ flExtension()

◆ flNoLink()

string flNoLink ( string  File)

Definition at line 700 of file fileutl.cc.

References flNotFile().

◆ flNormalize()

std::string flNormalize ( std::string  file)

removes superfluous /./ and // from path

Definition at line 770 of file fileutl.cc.

References APT::String::Startswith().

Referenced by Configuration::FindFile().

◆ flNotDir()

◆ flNotFile()

◆ GetListOfFilesInDir() [1/3]

std::vector<string> GetListOfFilesInDir ( string const &  Dir,
bool  SortList 
)

Definition at line 559 of file fileutl.cc.

References _config, DIR, DirectoryExists(), flCombine(), and RealFileExists().

◆ GetListOfFilesInDir() [2/3]

std::vector<string> GetListOfFilesInDir ( string const &  Dir,
std::vector< string > const &  Ext,
bool const &  SortList 
)

◆ GetListOfFilesInDir() [3/3]

std::vector<string> GetListOfFilesInDir ( string const &  Dir,
string const &  Ext,
bool const &  SortList,
bool const &  AllowNoExt 
)

◆ GetLock()

int GetLock ( string  File,
bool  Errors 
)

Definition at line 243 of file fileutl.cc.

References GetProcessName(), and SetCloseExec().

Referenced by DoAutoClean(), and GetLockMaybeWait().

◆ GetModificationTime()

time_t GetModificationTime ( string const &  Path)

Definition at line 653 of file fileutl.cc.

Referenced by pkgSourceList::GetLastModifiedTime().

◆ GetProcessName()

static std::string GetProcessName ( int  pid)
static

Definition at line 220 of file fileutl.cc.

References FileFd::ReadLine(), FileFd::ReadOnly, and strprintf().

Referenced by GetLock().

◆ GetTempDir() [1/2]

std::string GetTempDir ( )

◆ GetTempDir() [2/2]

std::string GetTempDir ( std::string const &  User)

Definition at line 3107 of file fileutl.cc.

References GetTempDir().

◆ GetTempFile() [1/2]

FileFd* GetTempFile ( std::string const &  Prefix,
bool  ImmediateUnlink,
FileFd *const  TmpFd 
)

◆ GetTempFile() [2/2]

FileFd* GetTempFile ( std::string const &  Prefix,
bool  ImmediateUnlink,
FileFd *const  TmpFd,
bool  Buffered 
)

◆ Glob()

std::vector<std::string> Glob ( std::string const &  pattern,
int  flags 
)

Definition at line 3058 of file fileutl.cc.

◆ Inhibit()

int Inhibit ( const char *  what,
const char *  who,
const char *  why,
const char *  mode 
)

Definition at line 3431 of file fileutl.cc.

Referenced by pkgDPkgPM::Go().

◆ MergeKeepFdsFromConfiguration()

void MergeKeepFdsFromConfiguration ( std::set< int > &  KeepFDs)

◆ OpenConfigurationFileFd()

bool OpenConfigurationFileFd ( std::string const &  File,
FileFd Fd 
)

◆ Popen()

bool Popen ( const char *  Args[],
FileFd Fd,
pid_t &  Child,
FileFd::OpenMode  Mode,
bool  CaptureStderr = true,
bool  Sandbox = false 
)

Popen() implementation that execv() instead of using a shell.

Parameters
Argsthe execv style command to run
FileFdis a reference to the FileFd to use for input or output
Childa reference to the integer that stores the child pid Note that you must call ExecWait() or similar to cleanup
Modeis either FileFd::ReadOnly or FileFd::WriteOnly
CaptureStderrTrue if we should capture stderr in addition to stdout. (default: True).
SandboxTrue if this should run sandboxed
Returns
true on success, false on failure with _error set

Definition at line 3198 of file fileutl.cc.

References DropPrivileges(), ExecFork(), FileFd::None, FileFd::OpenDescriptor(), FileFd::ReadOnly, and FileFd::WriteOnly.

Referenced by AutoDetectProxy().

◆ RealFileExists()

◆ RemoveFile()

◆ RemoveFileAt()

bool RemoveFileAt ( char const *const  Function,
int const  dirfd,
std::string const &  FileName 
)

Definition at line 183 of file fileutl.cc.

Referenced by pkgAcquire::Clean(), and LogCleaner::Erase().

◆ Rename()

◆ RunScripts()

◆ SafeGetCWD()

string SafeGetCWD ( )

◆ SetCloseExec()

◆ SetNonBlock()

void SetNonBlock ( int  Fd,
bool  Block 
)

◆ StartsWithGPGClearTextSignature()

bool StartsWithGPGClearTextSignature ( string const &  FileName)

Definition at line 984 of file fileutl.cc.

References _strrstrip(), and FILE.

Referenced by pkgAcqMetaClearSig::VerifyDone().

◆ StatFileFd()

static bool StatFileFd ( char const *const  msg,
int const  iFd,
std::string const &  FileName,
struct stat &  Buf,
FileFdPrivate *const  d 
)
static

◆ WaitFd()