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.h
Go to the documentation of this file.
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 /* ######################################################################
4 
5  File Utilities
6 
7  CopyFile - Buffered copy of a single file
8  GetLock - dpkg compatible lock file manipulation (fcntl)
9  FileExists - Returns true if the file exists
10  SafeGetCWD - Returns the CWD in a string with overrun protection
11 
12  The file class is a handy abstraction for various functions+classes
13  that need to accept filenames.
14 
15  This source is placed in the Public Domain, do with it what you will
16  It was originally written by Jason Gunthorpe.
17 
18  ##################################################################### */
19  /*}}}*/
20 #ifndef PKGLIB_FILEUTL_H
21 #define PKGLIB_FILEUTL_H
22 
24 #include <apt-pkg/macros.h>
25 
26 #include <set>
27 #include <string>
28 #include <vector>
29 #include <time.h>
30 
31 #include <zlib.h>
32 
33 
34 /* Define this for python-apt */
35 #define APT_HAS_GZIP 1
36 
37 class FileFdPrivate;
39 {
40  friend class FileFdPrivate;
41  friend class GzipFileFdPrivate;
42  friend class Bz2FileFdPrivate;
43  friend class LzmaFileFdPrivate;
44  friend class Lz4FileFdPrivate;
45  friend class ZstdFileFdPrivate;
46  friend class DirectFileFdPrivate;
47  friend class PipedFileFdPrivate;
48  protected:
49  int iFd;
50 
51  enum LocalFlags {AutoClose = (1<<0),Fail = (1<<1),DelOnFail = (1<<2),
52  HitEof = (1<<3), Replace = (1<<4), Compressed = (1<<5) };
53  unsigned long Flags;
54  std::string FileName;
55  std::string TemporaryFileName;
56 
57  public:
58  enum OpenMode {
59  ReadOnly = (1 << 0),
60  WriteOnly = (1 << 1),
61  ReadWrite = ReadOnly | WriteOnly,
62 
63  Create = (1 << 2),
64  Exclusive = (1 << 3),
65  Atomic = Exclusive | (1 << 4),
66  Empty = (1 << 5),
67  BufferedWrite = (1 << 6),
68 
69  WriteEmpty = ReadWrite | Create | Empty,
70  WriteExists = ReadWrite,
71  WriteAny = ReadWrite | Create,
72  WriteTemp = ReadWrite | Create | Exclusive,
73  ReadOnlyGzip,
74  WriteAtomic = ReadWrite | Create | Atomic
75  };
77  {
78  Auto = 'A',
79  None = 'N',
80  Extension = 'E',
81  Gzip = 'G',
82  Bzip2 = 'B',
83  Lzma = 'L',
84  Xz = 'X',
85  Lz4 = '4',
86  Zstd = 'Z'
87  };
88 
89  inline bool Read(void *To,unsigned long long Size,bool AllowEof)
90  {
91  unsigned long long Jnk;
92  if (AllowEof)
93  return Read(To,Size,&Jnk);
94  return Read(To,Size);
95  }
96  bool Read(void *To,unsigned long long Size,unsigned long long *Actual = 0);
97  bool static Read(int const Fd, void *To, unsigned long long Size, unsigned long long * const Actual = 0);
98  /** read a complete line or until buffer is full
99  *
100  * The buffer will always be \\0 terminated, so at most Size-1 characters are read.
101  * If the buffer holds a complete line the last character (before \\0) will be
102  * the newline character \\n otherwise the line was longer than the buffer.
103  *
104  * @param To buffer which will hold the line
105  * @param Size of the buffer to fill
106  * @param \b nullptr is returned in error cases, otherwise
107  * the parameter \b To now filled with the line.
108  */
109  char* ReadLine(char *To, unsigned long long const Size);
110  /** read a complete line from the file
111  *
112  * Similar to std::getline() the string does \b not include
113  * the newline, but just the content of the line as the newline
114  * is not needed to distinguish cases as for the other #ReadLine method.
115  *
116  * @param To string which will hold the line
117  * @return \b true if successful, otherwise \b false
118  */
119  bool ReadLine(std::string &To);
120  bool Flush();
121  bool Write(const void *From,unsigned long long Size);
122  bool static Write(int Fd, const void *From, unsigned long long Size);
123  bool Seek(unsigned long long To);
124  bool Skip(unsigned long long To);
125  bool Truncate(unsigned long long To);
126  unsigned long long Tell();
127  // the size of the file content (compressed files will be uncompressed first)
128  unsigned long long Size();
129  // the size of the file itself
130  unsigned long long FileSize();
131  time_t ModificationTime();
132 
133  bool Open(std::string FileName,unsigned int const Mode,CompressMode Compress,unsigned long const AccessMode = 0666);
134  bool Open(std::string FileName,unsigned int const Mode,APT::Configuration::Compressor const &compressor,unsigned long const AccessMode = 0666);
135  inline bool Open(std::string const &FileName,unsigned int const Mode, unsigned long const AccessMode = 0666) {
136  return Open(FileName, Mode, None, AccessMode);
137  };
138  bool OpenDescriptor(int Fd, unsigned int const Mode, CompressMode Compress, bool AutoClose=false);
139  bool OpenDescriptor(int Fd, unsigned int const Mode, APT::Configuration::Compressor const &compressor, bool AutoClose=false);
140  inline bool OpenDescriptor(int Fd, unsigned int const Mode, bool AutoClose=false) {
141  return OpenDescriptor(Fd, Mode, None, AutoClose);
142  };
143  bool Close();
144  bool Sync();
145 
146  // Simple manipulators
147  inline int Fd() {return iFd;};
148  inline void Fd(int fd) { OpenDescriptor(fd, ReadWrite);};
149 
150  inline bool IsOpen() {return iFd >= 0;};
151  inline bool Failed() {return (Flags & Fail) == Fail;};
152  inline void EraseOnFailure() {Flags |= DelOnFail;};
153  inline void OpFail() {Flags |= Fail;};
154  inline bool Eof() {return (Flags & HitEof) == HitEof;};
155  inline bool IsCompressed() {return (Flags & Compressed) == Compressed;};
156  inline std::string &Name() {return FileName;};
157  inline void SetFileName(std::string const &name) { FileName = name; };
158 
159  FileFd(std::string FileName,unsigned int const Mode,unsigned long AccessMode = 0666);
160  FileFd(std::string FileName,unsigned int const Mode, CompressMode Compress, unsigned long AccessMode = 0666);
161  FileFd();
162  FileFd(int const Fd, unsigned int const Mode = ReadWrite, CompressMode Compress = None);
163  FileFd(int const Fd, bool const AutoClose);
164  virtual ~~FileFd();
165 
166  private:
170  APT_HIDDEN bool OpenInternDescriptor(unsigned int const Mode, APT::Configuration::Compressor const &compressor);
171 
172  // private helpers to set Fail flag and call _error->Error
173  APT_HIDDEN bool FileFdErrno(const char* Function, const char* Description,...) APT_PRINTF(3) APT_COLD;
174  APT_HIDDEN bool FileFdError(const char* Description,...) APT_PRINTF(2) APT_COLD;
175 };
176 
177 APT_PUBLIC bool RunScripts(const char *Cnf);
178 APT_PUBLIC bool CopyFile(FileFd &From,FileFd &To);
179 APT_PUBLIC bool RemoveFile(char const * const Function, std::string const &FileName);
180 APT_PUBLIC bool RemoveFileAt(char const * const Function, int const dirfd, std::string const &FileName);
181 APT_PUBLIC int GetLock(std::string File,bool Errors = true);
182 APT_PUBLIC bool FileExists(std::string File);
183 APT_PUBLIC bool RealFileExists(std::string File);
184 APT_PUBLIC bool DirectoryExists(std::string const &Path);
185 APT_PUBLIC bool CreateDirectory(std::string const &Parent, std::string const &Path);
186 APT_PUBLIC time_t GetModificationTime(std::string const &Path);
187 APT_PUBLIC bool Rename(std::string From, std::string To);
188 
189 APT_PUBLIC std::string GetTempDir();
190 APT_PUBLIC std::string GetTempDir(std::string const &User);
191 APT_PUBLIC FileFd* GetTempFile(std::string const &Prefix = "",
192  bool ImmediateUnlink = true,
193  FileFd * const TmpFd = NULL);
194 
195 // FIXME: GetTempFile should always return a buffered file
196 APT_HIDDEN FileFd* GetTempFile(std::string const &Prefix,
197  bool ImmediateUnlink ,
198  FileFd * const TmpFd,
199  bool Buffered);
200 
201 /** \brief Ensure the existence of the given Path
202  *
203  * \param Parent directory of the Path directory - a trailing
204  * /apt/ will be removed before CreateDirectory call.
205  * \param Path which should exist after (successful) call
206  */
207 APT_PUBLIC bool CreateAPTDirectoryIfNeeded(std::string const &Parent, std::string const &Path);
208 
209 APT_PUBLIC std::vector<std::string> GetListOfFilesInDir(std::string const &Dir, std::string const &Ext,
210  bool const &SortList, bool const &AllowNoExt=false);
211 APT_PUBLIC std::vector<std::string> GetListOfFilesInDir(std::string const &Dir, std::vector<std::string> const &Ext,
212  bool const &SortList);
213 APT_PUBLIC std::vector<std::string> GetListOfFilesInDir(std::string const &Dir, bool SortList);
214 APT_PUBLIC std::string SafeGetCWD();
215 APT_PUBLIC void SetCloseExec(int Fd,bool Close);
216 APT_PUBLIC void SetNonBlock(int Fd,bool Block);
217 APT_PUBLIC bool WaitFd(int Fd,bool write = false,unsigned long timeout = 0);
218 APT_PUBLIC pid_t ExecFork();
219 APT_PUBLIC pid_t ExecFork(std::set<int> keep_fds);
220 APT_PUBLIC void MergeKeepFdsFromConfiguration(std::set<int> &keep_fds);
221 APT_PUBLIC bool ExecWait(pid_t Pid,const char *Name,bool Reap = false);
222 
223 // check if the given file starts with a PGP cleartext signature
224 APT_PUBLIC bool StartsWithGPGClearTextSignature(std::string const &FileName);
225 
226 /** change file attributes to requested known good values
227  *
228  * The method skips the user:group setting if not root.
229  *
230  * @param requester is printed as functionname in error cases
231  * @param file is the file to be modified
232  * @param user is the (new) owner of the file, e.g. _apt
233  * @param group is the (new) group owning the file, e.g. root
234  * @param mode is the access mode of the file, e.g. 0644
235  */
236 APT_PUBLIC bool ChangeOwnerAndPermissionOfFile(char const * const requester, char const * const file, char const * const user, char const * const group, mode_t const mode);
237 
238 /**
239  * \brief Drop privileges
240  *
241  * Drop the privileges to the user _apt (or the one specified in
242  * APT::Sandbox::User). This does not set the supplementary group
243  * ids up correctly, it only uses the default group. Also prevent
244  * the process from gaining any new privileges afterwards, at least
245  * on Linux.
246  *
247  * \return true on success, false on failure with _error set
248  */
250 
251 // File string manipulators
252 APT_PUBLIC std::string flNotDir(std::string File);
253 APT_PUBLIC std::string flNotFile(std::string File);
254 APT_PUBLIC std::string flNoLink(std::string File);
255 APT_PUBLIC std::string flExtension(std::string File);
256 APT_PUBLIC std::string flCombine(std::string Dir,std::string File);
257 
258 /** \brief Takes a file path and returns the absolute path
259  */
260 APT_PUBLIC std::string flAbsPath(std::string File);
261 /** \brief removes superfluous /./ and // from path */
262 APT_HIDDEN std::string flNormalize(std::string file);
263 
264 // simple c++ glob
265 APT_PUBLIC std::vector<std::string> Glob(std::string const &pattern, int flags=0);
266 
267 /** \brief Popen() implementation that execv() instead of using a shell
268  *
269  * \param Args the execv style command to run
270  * \param FileFd is a reference to the FileFd to use for input or output
271  * \param Child a reference to the integer that stores the child pid
272  * Note that you must call ExecWait() or similar to cleanup
273  * \param Mode is either FileFd::ReadOnly or FileFd::WriteOnly
274  * \param CaptureStderr True if we should capture stderr in addition to stdout.
275  * (default: True).
276  * \param Sandbox True if this should run sandboxed
277  * \return true on success, false on failure with _error set
278  */
279 APT_PUBLIC bool Popen(const char *Args[], FileFd &Fd, pid_t &Child, FileFd::OpenMode Mode, bool CaptureStderr = true, bool Sandbox = false);
280 
281 APT_HIDDEN bool OpenConfigurationFileFd(std::string const &File, FileFd &Fd);
282 
283 APT_HIDDEN int Inhibit(const char *what, const char *who, const char *why, const char *mode);
284 
285 #endif
return false
APT::Configuration::Compressor compressor
Definition: fileutl.cc:1099
Definition: fileutl.h:39
unsigned long Flags
Definition: fileutl.h:53
bool IsOpen()
Definition: fileutl.h:150
std::string FileName
Definition: fileutl.h:54
CompressMode
Definition: fileutl.h:77
APT_HIDDEN FileFd & operator=(const FileFd &)
OpenMode
Definition: fileutl.h:58
void Fd(int fd)
Definition: fileutl.h:148
std::string TemporaryFileName
Definition: fileutl.h:55
LocalFlags
Definition: fileutl.h:51
void SetFileName(std::string const &name)
Definition: fileutl.h:157
bool IsCompressed()
Definition: fileutl.h:155
void OpFail()
Definition: fileutl.h:153
APT_HIDDEN FileFd(const FileFd &)
void EraseOnFailure()
Definition: fileutl.h:152
bool Failed()
Definition: fileutl.h:151
bool Eof()
Definition: fileutl.h:154
int Fd()
Definition: fileutl.h:147
bool Open(std::string const &FileName, unsigned int const Mode, unsigned long const AccessMode=0666)
Definition: fileutl.h:135
bool Read(void *To, unsigned long long Size, bool AllowEof)
Definition: fileutl.h:89
bool OpenDescriptor(int Fd, unsigned int const Mode, bool AutoClose=false)
Definition: fileutl.h:140
FileFdPrivate * d
Definition: fileutl.h:167
int iFd
Definition: fileutl.h:49
std::string & Name()
Definition: fileutl.h:156
static bool ReadLine(int const input, std::string &line)
Definition: edsp.cc:489
APT_PUBLIC std::string flAbsPath(std::string File)
Takes a file path and returns the absolute path.
Definition: fileutl.cc:757
APT_PUBLIC std::string flCombine(std::string Dir, std::string File)
Definition: fileutl.cc:740
APT_PUBLIC std::vector< std::string > Glob(std::string const &pattern, int flags=0)
Definition: fileutl.cc:3058
APT_HIDDEN bool OpenConfigurationFileFd(std::string const &File, FileFd &Fd)
Definition: fileutl.cc:3419
APT_PUBLIC std::string flNotDir(std::string File)
Definition: fileutl.cc:664
APT_PUBLIC std::string flNoLink(std::string File)
Definition: fileutl.cc:700
APT_PUBLIC bool WaitFd(int Fd, bool write=false, unsigned long timeout=0)
Definition: fileutl.cc:819
APT_PUBLIC std::vector< std::string > GetListOfFilesInDir(std::string const &Dir, std::string const &Ext, bool const &SortList, bool const &AllowNoExt=false)
Definition: fileutl.cc:421
APT_PUBLIC void MergeKeepFdsFromConfiguration(std::set< int > &keep_fds)
Definition: fileutl.cc:860
APT_PUBLIC bool DropPrivileges()
Drop privileges.
Definition: fileutl.cc:3260
APT_PUBLIC 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.
Definition: fileutl.cc:3198
APT_PUBLIC std::string flExtension(std::string File)
Definition: fileutl.cc:688
APT_PUBLIC bool ChangeOwnerAndPermissionOfFile(char const *const requester, char const *const file, char const *const user, char const *const group, mode_t const mode)
Definition: fileutl.cc:1015
APT_PUBLIC time_t GetModificationTime(std::string const &Path)
Definition: fileutl.cc:653
APT_PUBLIC bool RealFileExists(std::string File)
Definition: fileutl.cc:337
APT_PUBLIC void SetNonBlock(int Fd, bool Block)
Definition: fileutl.cc:804
APT_PUBLIC bool CreateAPTDirectoryIfNeeded(std::string const &Parent, std::string const &Path)
Ensure the existence of the given Path.
Definition: fileutl.cc:400
APT_HIDDEN int Inhibit(const char *what, const char *who, const char *why, const char *mode)
Definition: fileutl.cc:3431
APT_PUBLIC void SetCloseExec(int Fd, bool Close)
Definition: fileutl.cc:792
APT_PUBLIC bool ExecWait(pid_t Pid, const char *Name, bool Reap=false)
Definition: fileutl.cc:942
APT_PUBLIC bool Rename(std::string From, std::string To)
Definition: fileutl.cc:3187
APT_PUBLIC bool FileExists(std::string File)
Definition: fileutl.cc:326
APT_HIDDEN std::string flNormalize(std::string file)
removes superfluous /./ and // from path
Definition: fileutl.cc:770
APT_PUBLIC pid_t ExecFork()
Definition: fileutl.cc:881
APT_PUBLIC bool StartsWithGPGClearTextSignature(std::string const &FileName)
Definition: fileutl.cc:984
APT_PUBLIC std::string GetTempDir()
Definition: fileutl.cc:3103
APT_PUBLIC bool RemoveFile(char const *const Function, std::string const &FileName)
Definition: fileutl.cc:198
APT_PUBLIC bool RunScripts(const char *Cnf)
Definition: fileutl.cc:91
APT_PUBLIC FileFd * GetTempFile(std::string const &Prefix="", bool ImmediateUnlink=true, FileFd *const TmpFd=NULL)
Definition: fileutl.cc:3134
APT_PUBLIC bool CopyFile(FileFd &From, FileFd &To)
Definition: fileutl.cc:164
APT_PUBLIC int GetLock(std::string File, bool Errors=true)
Definition: fileutl.cc:243
APT_PUBLIC std::string flNotFile(std::string File)
Definition: fileutl.cc:676
APT_PUBLIC bool DirectoryExists(std::string const &Path)
Definition: fileutl.cc:348
APT_PUBLIC bool RemoveFileAt(char const *const Function, int const dirfd, std::string const &FileName)
Definition: fileutl.cc:183
APT_PUBLIC bool CreateDirectory(std::string const &Parent, std::string const &Path)
Definition: fileutl.cc:364
APT_PUBLIC std::string SafeGetCWD()
Definition: fileutl.cc:637
#define APT_PRINTF(n)
Definition: macros.h:58
#define APT_PUBLIC
Definition: macros.h:77
#define APT_COLD
Definition: macros.h:86
#define APT_HIDDEN
Definition: macros.h:78
Representation of supported compressors.