fileutl.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: fileutl.h,v 1.26 2001/05/07 05:06:52 jgg Exp $
  4. /* ######################################################################
  5. File Utilities
  6. CopyFile - Buffered copy of a single file
  7. GetLock - dpkg compatible lock file manipulation (fcntl)
  8. FileExists - Returns true if the file exists
  9. SafeGetCWD - Returns the CWD in a string with overrun protection
  10. The file class is a handy abstraction for various functions+classes
  11. that need to accept filenames.
  12. This source is placed in the Public Domain, do with it what you will
  13. It was originally written by Jason Gunthorpe.
  14. ##################################################################### */
  15. /*}}}*/
  16. #ifndef PKGLIB_FILEUTL_H
  17. #define PKGLIB_FILEUTL_H
  18. #include <apt-pkg/macros.h>
  19. #include <apt-pkg/aptconfiguration.h>
  20. #include <string>
  21. #include <vector>
  22. #include <set>
  23. #include <time.h>
  24. #include <zlib.h>
  25. #ifndef APT_8_CLEANER_HEADERS
  26. using std::string;
  27. #endif
  28. /* Define this for python-apt */
  29. #define APT_HAS_GZIP 1
  30. class FileFdPrivate;
  31. class FileFd
  32. {
  33. friend class FileFdPrivate;
  34. friend class GzipFileFdPrivate;
  35. friend class Bz2FileFdPrivate;
  36. friend class LzmaFileFdPrivate;
  37. friend class Lz4FileFdPrivate;
  38. friend class DirectFileFdPrivate;
  39. friend class PipedFileFdPrivate;
  40. protected:
  41. int iFd;
  42. enum LocalFlags {AutoClose = (1<<0),Fail = (1<<1),DelOnFail = (1<<2),
  43. HitEof = (1<<3), Replace = (1<<4), Compressed = (1<<5) };
  44. unsigned long Flags;
  45. std::string FileName;
  46. std::string TemporaryFileName;
  47. public:
  48. enum OpenMode {
  49. ReadOnly = (1 << 0),
  50. WriteOnly = (1 << 1),
  51. ReadWrite = ReadOnly | WriteOnly,
  52. Create = (1 << 2),
  53. Exclusive = (1 << 3),
  54. Atomic = Exclusive | (1 << 4),
  55. Empty = (1 << 5),
  56. BufferedWrite = (1 << 6),
  57. WriteEmpty = ReadWrite | Create | Empty,
  58. WriteExists = ReadWrite,
  59. WriteAny = ReadWrite | Create,
  60. WriteTemp = ReadWrite | Create | Exclusive,
  61. ReadOnlyGzip,
  62. WriteAtomic = ReadWrite | Create | Atomic
  63. };
  64. enum CompressMode { Auto = 'A', None = 'N', Extension = 'E', Gzip = 'G', Bzip2 = 'B', Lzma = 'L', Xz = 'X', Lz4='4' };
  65. inline bool Read(void *To,unsigned long long Size,bool AllowEof)
  66. {
  67. unsigned long long Jnk;
  68. if (AllowEof)
  69. return Read(To,Size,&Jnk);
  70. return Read(To,Size);
  71. }
  72. bool Read(void *To,unsigned long long Size,unsigned long long *Actual = 0);
  73. bool static Read(int const Fd, void *To, unsigned long long Size, unsigned long long * const Actual = 0);
  74. char* ReadLine(char *To, unsigned long long const Size);
  75. bool Flush();
  76. bool Write(const void *From,unsigned long long Size);
  77. bool static Write(int Fd, const void *From, unsigned long long Size);
  78. bool Seek(unsigned long long To);
  79. bool Skip(unsigned long long To);
  80. bool Truncate(unsigned long long To);
  81. unsigned long long Tell();
  82. // the size of the file content (compressed files will be uncompressed first)
  83. unsigned long long Size();
  84. // the size of the file itself
  85. unsigned long long FileSize();
  86. time_t ModificationTime();
  87. /* You want to use 'unsigned long long' if you are talking about a file
  88. to be able to support large files (>2 or >4 GB) properly.
  89. This shouldn't happen all to often for the indexes, but deb's might be…
  90. And as the auto-conversation converts a 'unsigned long *' to a 'bool'
  91. instead of 'unsigned long long *' we need to provide this explicitly -
  92. otherwise applications magically start to fail… */
  93. bool Read(void *To,unsigned long long Size,unsigned long *Actual) APT_DEPRECATED_MSG("The Actual variable you pass in should be an unsigned long long")
  94. {
  95. unsigned long long R;
  96. bool const T = Read(To, Size, &R);
  97. *Actual = R;
  98. return T;
  99. }
  100. bool Open(std::string FileName,unsigned int const Mode,CompressMode Compress,unsigned long const AccessMode = 0666);
  101. bool Open(std::string FileName,unsigned int const Mode,APT::Configuration::Compressor const &compressor,unsigned long const AccessMode = 0666);
  102. inline bool Open(std::string const &FileName,unsigned int const Mode, unsigned long const AccessMode = 0666) {
  103. return Open(FileName, Mode, None, AccessMode);
  104. };
  105. bool OpenDescriptor(int Fd, unsigned int const Mode, CompressMode Compress, bool AutoClose=false);
  106. bool OpenDescriptor(int Fd, unsigned int const Mode, APT::Configuration::Compressor const &compressor, bool AutoClose=false);
  107. inline bool OpenDescriptor(int Fd, unsigned int const Mode, bool AutoClose=false) {
  108. return OpenDescriptor(Fd, Mode, None, AutoClose);
  109. };
  110. bool Close();
  111. bool Sync();
  112. // Simple manipulators
  113. inline int Fd() {return iFd;};
  114. inline void Fd(int fd) { OpenDescriptor(fd, ReadWrite);};
  115. gzFile gzFd() APT_DEPRECATED_MSG("Implementation detail, do not use to be able to support bzip2, xz and co") APT_PURE;
  116. inline bool IsOpen() {return iFd >= 0;};
  117. inline bool Failed() {return (Flags & Fail) == Fail;};
  118. inline void EraseOnFailure() {Flags |= DelOnFail;};
  119. inline void OpFail() {Flags |= Fail;};
  120. inline bool Eof() {return (Flags & HitEof) == HitEof;};
  121. inline bool IsCompressed() {return (Flags & Compressed) == Compressed;};
  122. inline std::string &Name() {return FileName;};
  123. FileFd(std::string FileName,unsigned int const Mode,unsigned long AccessMode = 0666);
  124. FileFd(std::string FileName,unsigned int const Mode, CompressMode Compress, unsigned long AccessMode = 0666);
  125. FileFd();
  126. FileFd(int const Fd, unsigned int const Mode = ReadWrite, CompressMode Compress = None);
  127. FileFd(int const Fd, bool const AutoClose);
  128. virtual ~FileFd();
  129. private:
  130. FileFdPrivate * d;
  131. APT_HIDDEN FileFd & operator=(const FileFd &);
  132. APT_HIDDEN bool OpenInternDescriptor(unsigned int const Mode, APT::Configuration::Compressor const &compressor);
  133. // private helpers to set Fail flag and call _error->Error
  134. APT_HIDDEN bool FileFdErrno(const char* Function, const char* Description,...) APT_PRINTF(3) APT_COLD;
  135. APT_HIDDEN bool FileFdError(const char* Description,...) APT_PRINTF(2) APT_COLD;
  136. };
  137. int RunCmd(const char *Cmd);
  138. bool RunScripts(const char *Cnf);
  139. bool CopyFile(FileFd &From,FileFd &To);
  140. bool RemoveFile(char const * const Function, std::string const &FileName);
  141. int GetLock(std::string File,bool Errors = true);
  142. bool FileExists(std::string File);
  143. bool RealFileExists(std::string File);
  144. bool DirectoryExists(std::string const &Path);
  145. bool CreateDirectory(std::string const &Parent, std::string const &Path);
  146. time_t GetModificationTime(std::string const &Path);
  147. bool Rename(std::string From, std::string To);
  148. std::string GetTempDir();
  149. std::string GetTempDir(std::string const &User);
  150. FileFd* GetTempFile(std::string const &Prefix = "",
  151. bool ImmediateUnlink = true,
  152. FileFd * const TmpFd = NULL);
  153. /** \brief Ensure the existence of the given Path
  154. *
  155. * \param Parent directory of the Path directory - a trailing
  156. * /apt/ will be removed before CreateDirectory call.
  157. * \param Path which should exist after (successful) call
  158. */
  159. bool CreateAPTDirectoryIfNeeded(std::string const &Parent, std::string const &Path);
  160. std::vector<std::string> GetListOfFilesInDir(std::string const &Dir, std::string const &Ext,
  161. bool const &SortList, bool const &AllowNoExt=false);
  162. std::vector<std::string> GetListOfFilesInDir(std::string const &Dir, std::vector<std::string> const &Ext,
  163. bool const &SortList);
  164. std::vector<std::string> GetListOfFilesInDir(std::string const &Dir, bool SortList);
  165. std::string SafeGetCWD();
  166. void SetCloseExec(int Fd,bool Close);
  167. void SetNonBlock(int Fd,bool Block);
  168. bool WaitFd(int Fd,bool write = false,unsigned long timeout = 0);
  169. pid_t ExecFork();
  170. pid_t ExecFork(std::set<int> keep_fds);
  171. void MergeKeepFdsFromConfiguration(std::set<int> &keep_fds);
  172. bool ExecWait(pid_t Pid,const char *Name,bool Reap = false);
  173. // check if the given file starts with a PGP cleartext signature
  174. bool StartsWithGPGClearTextSignature(std::string const &FileName);
  175. /** change file attributes to requested known good values
  176. *
  177. * The method skips the user:group setting if not root.
  178. *
  179. * @param requester is printed as functionname in error cases
  180. * @param file is the file to be modified
  181. * @param user is the (new) owner of the file, e.g. _apt
  182. * @param group is the (new) group owning the file, e.g. root
  183. * @param mode is the access mode of the file, e.g. 0644
  184. */
  185. bool ChangeOwnerAndPermissionOfFile(char const * const requester, char const * const file, char const * const user, char const * const group, mode_t const mode);
  186. /**
  187. * \brief Drop privileges
  188. *
  189. * Drop the privileges to the user _apt (or the one specified in
  190. * APT::Sandbox::User). This does not set the supplementary group
  191. * ids up correctly, it only uses the default group. Also prevent
  192. * the process from gaining any new privileges afterwards, at least
  193. * on Linux.
  194. *
  195. * \return true on success, false on failure with _error set
  196. */
  197. bool DropPrivileges();
  198. // File string manipulators
  199. std::string flNotDir(std::string File);
  200. std::string flNotFile(std::string File);
  201. std::string flNoLink(std::string File);
  202. std::string flExtension(std::string File);
  203. std::string flCombine(std::string Dir,std::string File);
  204. /** \brief Takes a file path and returns the absolute path
  205. */
  206. std::string flAbsPath(std::string File);
  207. /** \brief removes superfluous /./ and // from path */
  208. APT_HIDDEN std::string flNormalize(std::string file);
  209. // simple c++ glob
  210. std::vector<std::string> Glob(std::string const &pattern, int flags=0);
  211. /** \brief Popen() implementation that execv() instead of using a shell
  212. *
  213. * \param Args the execv style command to run
  214. * \param FileFd is a referenz to the FileFd to use for input or output
  215. * \param Child a reference to the integer that stores the child pid
  216. * Note that you must call ExecWait() or similar to cleanup
  217. * \param Mode is either FileFd::ReadOnly or FileFd::WriteOnly
  218. * \param CaptureStderr True if we should capture stderr in addition to stdout.
  219. * (default: True).
  220. * \return true on success, false on failure with _error set
  221. */
  222. bool Popen(const char* Args[], FileFd &Fd, pid_t &Child, FileFd::OpenMode Mode, bool CaptureStderr);
  223. bool Popen(const char* Args[], FileFd &Fd, pid_t &Child, FileFd::OpenMode Mode);
  224. #endif