fileutl.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. protected:
  34. int iFd;
  35. enum LocalFlags {AutoClose = (1<<0),Fail = (1<<1),DelOnFail = (1<<2),
  36. HitEof = (1<<3), Replace = (1<<4), Compressed = (1<<5) };
  37. unsigned long Flags;
  38. std::string FileName;
  39. std::string TemporaryFileName;
  40. public:
  41. enum OpenMode {
  42. ReadOnly = (1 << 0),
  43. WriteOnly = (1 << 1),
  44. ReadWrite = ReadOnly | WriteOnly,
  45. Create = (1 << 2),
  46. Exclusive = (1 << 3),
  47. Atomic = Exclusive | (1 << 4),
  48. Empty = (1 << 5),
  49. WriteEmpty = ReadWrite | Create | Empty,
  50. WriteExists = ReadWrite,
  51. WriteAny = ReadWrite | Create,
  52. WriteTemp = ReadWrite | Create | Exclusive,
  53. ReadOnlyGzip,
  54. WriteAtomic = ReadWrite | Create | Atomic
  55. };
  56. enum CompressMode { Auto = 'A', None = 'N', Extension = 'E', Gzip = 'G', Bzip2 = 'B', Lzma = 'L', Xz = 'X' };
  57. inline bool Read(void *To,unsigned long long Size,bool AllowEof)
  58. {
  59. unsigned long long Jnk;
  60. if (AllowEof)
  61. return Read(To,Size,&Jnk);
  62. return Read(To,Size);
  63. }
  64. bool Read(void *To,unsigned long long Size,unsigned long long *Actual = 0);
  65. char* ReadLine(char *To, unsigned long long const Size);
  66. bool Write(const void *From,unsigned long long Size);
  67. bool static Write(int Fd, const void *From, unsigned long long Size);
  68. bool Seek(unsigned long long To);
  69. bool Skip(unsigned long long To);
  70. bool Truncate(unsigned long long To);
  71. unsigned long long Tell();
  72. // the size of the file content (compressed files will be uncompressed first)
  73. unsigned long long Size();
  74. // the size of the file itself
  75. unsigned long long FileSize();
  76. time_t ModificationTime();
  77. /* You want to use 'unsigned long long' if you are talking about a file
  78. to be able to support large files (>2 or >4 GB) properly.
  79. This shouldn't happen all to often for the indexes, but deb's might be…
  80. And as the auto-conversation converts a 'unsigned long *' to a 'bool'
  81. instead of 'unsigned long long *' we need to provide this explicitely -
  82. otherwise applications magically start to fail… */
  83. bool Read(void *To,unsigned long long Size,unsigned long *Actual) APT_DEPRECATED
  84. {
  85. unsigned long long R;
  86. bool const T = Read(To, Size, &R);
  87. *Actual = R;
  88. return T;
  89. }
  90. bool Open(std::string FileName,unsigned int const Mode,CompressMode Compress,unsigned long const AccessMode = 0666);
  91. bool Open(std::string FileName,unsigned int const Mode,APT::Configuration::Compressor const &compressor,unsigned long const AccessMode = 0666);
  92. inline bool Open(std::string const &FileName,unsigned int const Mode, unsigned long const AccessMode = 0666) {
  93. return Open(FileName, Mode, None, AccessMode);
  94. };
  95. bool OpenDescriptor(int Fd, unsigned int const Mode, CompressMode Compress, bool AutoClose=false);
  96. bool OpenDescriptor(int Fd, unsigned int const Mode, APT::Configuration::Compressor const &compressor, bool AutoClose=false);
  97. inline bool OpenDescriptor(int Fd, unsigned int const Mode, bool AutoClose=false) {
  98. return OpenDescriptor(Fd, Mode, None, AutoClose);
  99. };
  100. bool Close();
  101. bool Sync();
  102. // Simple manipulators
  103. inline int Fd() {return iFd;};
  104. inline void Fd(int fd) { OpenDescriptor(fd, ReadWrite);};
  105. gzFile gzFd() APT_DEPRECATED APT_PURE;
  106. inline bool IsOpen() {return iFd >= 0;};
  107. inline bool Failed() {return (Flags & Fail) == Fail;};
  108. inline void EraseOnFailure() {Flags |= DelOnFail;};
  109. inline void OpFail() {Flags |= Fail;};
  110. inline bool Eof() {return (Flags & HitEof) == HitEof;};
  111. inline bool IsCompressed() {return (Flags & Compressed) == Compressed;};
  112. inline std::string &Name() {return FileName;};
  113. FileFd(std::string FileName,unsigned int const Mode,unsigned long AccessMode = 0666) : iFd(-1), Flags(0), d(NULL)
  114. {
  115. Open(FileName,Mode, None, AccessMode);
  116. };
  117. FileFd(std::string FileName,unsigned int const Mode, CompressMode Compress, unsigned long AccessMode = 0666) : iFd(-1), Flags(0), d(NULL)
  118. {
  119. Open(FileName,Mode, Compress, AccessMode);
  120. };
  121. FileFd() : iFd(-1), Flags(AutoClose), d(NULL) {};
  122. FileFd(int const Fd, unsigned int const Mode = ReadWrite, CompressMode Compress = None) : iFd(-1), Flags(0), d(NULL)
  123. {
  124. OpenDescriptor(Fd, Mode, Compress);
  125. };
  126. FileFd(int const Fd, bool const AutoClose) : iFd(-1), Flags(0), d(NULL)
  127. {
  128. OpenDescriptor(Fd, ReadWrite, None, AutoClose);
  129. };
  130. virtual ~FileFd();
  131. private:
  132. FileFdPrivate* d;
  133. APT_HIDDEN bool OpenInternDescriptor(unsigned int const Mode, APT::Configuration::Compressor const &compressor);
  134. // private helpers to set Fail flag and call _error->Error
  135. APT_HIDDEN bool FileFdErrno(const char* Function, const char* Description,...) APT_PRINTF(3) APT_COLD;
  136. APT_HIDDEN bool FileFdError(const char* Description,...) APT_PRINTF(2) APT_COLD;
  137. };
  138. bool RunScripts(const char *Cnf);
  139. bool CopyFile(FileFd &From,FileFd &To);
  140. int GetLock(std::string File,bool Errors = true);
  141. bool FileExists(std::string File);
  142. bool RealFileExists(std::string File);
  143. bool DirectoryExists(std::string const &Path) APT_CONST;
  144. bool CreateDirectory(std::string const &Parent, std::string const &Path);
  145. time_t GetModificationTime(std::string const &Path);
  146. bool Rename(std::string From, std::string To);
  147. std::string GetTempDir();
  148. FileFd* GetTempFile(std::string const &Prefix = "",
  149. bool ImmediateUnlink = true);
  150. /** \brief Ensure the existence of the given Path
  151. *
  152. * \param Parent directory of the Path directory - a trailing
  153. * /apt/ will be removed before CreateDirectory call.
  154. * \param Path which should exist after (successful) call
  155. */
  156. bool CreateAPTDirectoryIfNeeded(std::string const &Parent, std::string const &Path);
  157. std::vector<std::string> GetListOfFilesInDir(std::string const &Dir, std::string const &Ext,
  158. bool const &SortList, bool const &AllowNoExt=false);
  159. std::vector<std::string> GetListOfFilesInDir(std::string const &Dir, std::vector<std::string> const &Ext,
  160. bool const &SortList);
  161. std::vector<std::string> GetListOfFilesInDir(std::string const &Dir, bool SortList);
  162. std::string SafeGetCWD();
  163. void SetCloseExec(int Fd,bool Close);
  164. void SetNonBlock(int Fd,bool Block);
  165. bool WaitFd(int Fd,bool write = false,unsigned long timeout = 0);
  166. pid_t ExecFork();
  167. pid_t ExecFork(std::set<int> keep_fds);
  168. void MergeKeepFdsFromConfiguration(std::set<int> &keep_fds);
  169. bool ExecWait(pid_t Pid,const char *Name,bool Reap = false);
  170. // check if the given file starts with a PGP cleartext signature
  171. bool StartsWithGPGClearTextSignature(std::string const &FileName);
  172. /**
  173. * \brief Drop privileges
  174. *
  175. * Drop the privileges to the user _apt (or the one specified in
  176. * APT::Sandbox::User). This does not set the supplementary group
  177. * ids up correctly, it only uses the default group. Also prevent
  178. * the process from gaining any new privileges afterwards, at least
  179. * on Linux.
  180. *
  181. * \return true on success, false on failure with _error set
  182. */
  183. bool DropPrivileges();
  184. // File string manipulators
  185. std::string flNotDir(std::string File);
  186. std::string flNotFile(std::string File);
  187. std::string flNoLink(std::string File);
  188. std::string flExtension(std::string File);
  189. std::string flCombine(std::string Dir,std::string File);
  190. /** \brief Takes a file path and returns the absolute path
  191. */
  192. std::string flAbsPath(std::string File);
  193. // simple c++ glob
  194. std::vector<std::string> Glob(std::string const &pattern, int flags=0);
  195. /** \brief Popen() implementation that execv() instead of using a shell
  196. *
  197. * \param Args the execv style command to run
  198. * \param FileFd is a referenz to the FileFd to use for input or output
  199. * \param Child a reference to the integer that stores the child pid
  200. * Note that you must call ExecWait() or similar to cleanup
  201. * \param Mode is either FileFd::ReadOnly or FileFd::WriteOnly
  202. * \return true on success, false on failure with _error set
  203. */
  204. bool Popen(const char* Args[], FileFd &Fd, pid_t &Child, FileFd::OpenMode Mode);
  205. #endif