fileutl.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. char* ReadLine(char *To, unsigned long long const Size);
  74. bool Flush();
  75. bool Write(const void *From,unsigned long long Size);
  76. bool static Write(int Fd, const void *From, unsigned long long Size);
  77. bool Seek(unsigned long long To);
  78. bool Skip(unsigned long long To);
  79. bool Truncate(unsigned long long To);
  80. unsigned long long Tell();
  81. // the size of the file content (compressed files will be uncompressed first)
  82. unsigned long long Size();
  83. // the size of the file itself
  84. unsigned long long FileSize();
  85. time_t ModificationTime();
  86. /* You want to use 'unsigned long long' if you are talking about a file
  87. to be able to support large files (>2 or >4 GB) properly.
  88. This shouldn't happen all to often for the indexes, but deb's might be…
  89. And as the auto-conversation converts a 'unsigned long *' to a 'bool'
  90. instead of 'unsigned long long *' we need to provide this explicitly -
  91. otherwise applications magically start to fail… */
  92. 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")
  93. {
  94. unsigned long long R;
  95. bool const T = Read(To, Size, &R);
  96. *Actual = R;
  97. return T;
  98. }
  99. bool Open(std::string FileName,unsigned int const Mode,CompressMode Compress,unsigned long const AccessMode = 0666);
  100. bool Open(std::string FileName,unsigned int const Mode,APT::Configuration::Compressor const &compressor,unsigned long const AccessMode = 0666);
  101. inline bool Open(std::string const &FileName,unsigned int const Mode, unsigned long const AccessMode = 0666) {
  102. return Open(FileName, Mode, None, AccessMode);
  103. };
  104. bool OpenDescriptor(int Fd, unsigned int const Mode, CompressMode Compress, bool AutoClose=false);
  105. bool OpenDescriptor(int Fd, unsigned int const Mode, APT::Configuration::Compressor const &compressor, bool AutoClose=false);
  106. inline bool OpenDescriptor(int Fd, unsigned int const Mode, bool AutoClose=false) {
  107. return OpenDescriptor(Fd, Mode, None, AutoClose);
  108. };
  109. bool Close();
  110. bool Sync();
  111. // Simple manipulators
  112. inline int Fd() {return iFd;};
  113. inline void Fd(int fd) { OpenDescriptor(fd, ReadWrite);};
  114. gzFile gzFd() APT_DEPRECATED_MSG("Implementation detail, do not use to be able to support bzip2, xz and co") APT_PURE;
  115. inline bool IsOpen() {return iFd >= 0;};
  116. inline bool Failed() {return (Flags & Fail) == Fail;};
  117. inline void EraseOnFailure() {Flags |= DelOnFail;};
  118. inline void OpFail() {Flags |= Fail;};
  119. inline bool Eof() {return (Flags & HitEof) == HitEof;};
  120. inline bool IsCompressed() {return (Flags & Compressed) == Compressed;};
  121. inline std::string &Name() {return FileName;};
  122. FileFd(std::string FileName,unsigned int const Mode,unsigned long AccessMode = 0666);
  123. FileFd(std::string FileName,unsigned int const Mode, CompressMode Compress, unsigned long AccessMode = 0666);
  124. FileFd();
  125. FileFd(int const Fd, unsigned int const Mode = ReadWrite, CompressMode Compress = None);
  126. FileFd(int const Fd, bool const AutoClose);
  127. virtual ~FileFd();
  128. private:
  129. FileFdPrivate * d;
  130. APT_HIDDEN FileFd & operator=(const FileFd &);
  131. APT_HIDDEN bool OpenInternDescriptor(unsigned int const Mode, APT::Configuration::Compressor const &compressor);
  132. // private helpers to set Fail flag and call _error->Error
  133. APT_HIDDEN bool FileFdErrno(const char* Function, const char* Description,...) APT_PRINTF(3) APT_COLD;
  134. APT_HIDDEN bool FileFdError(const char* Description,...) APT_PRINTF(2) APT_COLD;
  135. };
  136. bool RunScripts(const char *Cnf);
  137. bool CopyFile(FileFd &From,FileFd &To);
  138. bool RemoveFile(char const * const Function, std::string const &FileName);
  139. int GetLock(std::string File,bool Errors = true);
  140. bool FileExists(std::string File);
  141. bool RealFileExists(std::string File);
  142. bool DirectoryExists(std::string const &Path) APT_CONST;
  143. bool CreateDirectory(std::string const &Parent, std::string const &Path);
  144. time_t GetModificationTime(std::string const &Path);
  145. bool Rename(std::string From, std::string To);
  146. std::string GetTempDir();
  147. std::string GetTempDir(std::string const &User);
  148. FileFd* GetTempFile(std::string const &Prefix = "",
  149. bool ImmediateUnlink = true,
  150. FileFd * const TmpFd = NULL);
  151. /** \brief Ensure the existence of the given Path
  152. *
  153. * \param Parent directory of the Path directory - a trailing
  154. * /apt/ will be removed before CreateDirectory call.
  155. * \param Path which should exist after (successful) call
  156. */
  157. bool CreateAPTDirectoryIfNeeded(std::string const &Parent, std::string const &Path);
  158. std::vector<std::string> GetListOfFilesInDir(std::string const &Dir, std::string const &Ext,
  159. bool const &SortList, bool const &AllowNoExt=false);
  160. std::vector<std::string> GetListOfFilesInDir(std::string const &Dir, std::vector<std::string> const &Ext,
  161. bool const &SortList);
  162. std::vector<std::string> GetListOfFilesInDir(std::string const &Dir, bool SortList);
  163. std::string SafeGetCWD();
  164. void SetCloseExec(int Fd,bool Close);
  165. void SetNonBlock(int Fd,bool Block);
  166. bool WaitFd(int Fd,bool write = false,unsigned long timeout = 0);
  167. pid_t ExecFork();
  168. pid_t ExecFork(std::set<int> keep_fds);
  169. void MergeKeepFdsFromConfiguration(std::set<int> &keep_fds);
  170. bool ExecWait(pid_t Pid,const char *Name,bool Reap = false);
  171. // check if the given file starts with a PGP cleartext signature
  172. bool StartsWithGPGClearTextSignature(std::string const &FileName);
  173. /** change file attributes to requested known good values
  174. *
  175. * The method skips the user:group setting if not root.
  176. *
  177. * @param requester is printed as functionname in error cases
  178. * @param file is the file to be modified
  179. * @param user is the (new) owner of the file, e.g. _apt
  180. * @param group is the (new) group owning the file, e.g. root
  181. * @param mode is the access mode of the file, e.g. 0644
  182. */
  183. bool ChangeOwnerAndPermissionOfFile(char const * const requester, char const * const file, char const * const user, char const * const group, mode_t const mode);
  184. /**
  185. * \brief Drop privileges
  186. *
  187. * Drop the privileges to the user _apt (or the one specified in
  188. * APT::Sandbox::User). This does not set the supplementary group
  189. * ids up correctly, it only uses the default group. Also prevent
  190. * the process from gaining any new privileges afterwards, at least
  191. * on Linux.
  192. *
  193. * \return true on success, false on failure with _error set
  194. */
  195. bool DropPrivileges();
  196. // File string manipulators
  197. std::string flNotDir(std::string File);
  198. std::string flNotFile(std::string File);
  199. std::string flNoLink(std::string File);
  200. std::string flExtension(std::string File);
  201. std::string flCombine(std::string Dir,std::string File);
  202. /** \brief Takes a file path and returns the absolute path
  203. */
  204. std::string flAbsPath(std::string File);
  205. // simple c++ glob
  206. std::vector<std::string> Glob(std::string const &pattern, int flags=0);
  207. /** \brief Popen() implementation that execv() instead of using a shell
  208. *
  209. * \param Args the execv style command to run
  210. * \param FileFd is a referenz to the FileFd to use for input or output
  211. * \param Child a reference to the integer that stores the child pid
  212. * Note that you must call ExecWait() or similar to cleanup
  213. * \param Mode is either FileFd::ReadOnly or FileFd::WriteOnly
  214. * \return true on success, false on failure with _error set
  215. */
  216. bool Popen(const char* Args[], FileFd &Fd, pid_t &Child, FileFd::OpenMode Mode);
  217. #endif