fileutl.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 <zlib.h>
  23. #ifndef APT_8_CLEANER_HEADERS
  24. using std::string;
  25. #endif
  26. /* Define this for python-apt */
  27. #define APT_HAS_GZIP 1
  28. class FileFdPrivate;
  29. class FileFd
  30. {
  31. protected:
  32. int iFd;
  33. enum LocalFlags {AutoClose = (1<<0),Fail = (1<<1),DelOnFail = (1<<2),
  34. HitEof = (1<<3), Replace = (1<<4), Compressed = (1<<5) };
  35. unsigned long Flags;
  36. std::string FileName;
  37. std::string TemporaryFileName;
  38. public:
  39. enum OpenMode {
  40. ReadOnly = (1 << 0),
  41. WriteOnly = (1 << 1),
  42. ReadWrite = ReadOnly | WriteOnly,
  43. Create = (1 << 2),
  44. Exclusive = (1 << 3),
  45. Atomic = Exclusive | (1 << 4),
  46. Empty = (1 << 5),
  47. WriteEmpty = ReadWrite | Create | Empty,
  48. WriteExists = ReadWrite,
  49. WriteAny = ReadWrite | Create,
  50. WriteTemp = ReadWrite | Create | Exclusive,
  51. ReadOnlyGzip,
  52. WriteAtomic = ReadWrite | Create | Atomic
  53. };
  54. enum CompressMode { Auto = 'A', None = 'N', Extension = 'E', Gzip = 'G', Bzip2 = 'B', Lzma = 'L', Xz = 'X' };
  55. inline bool Read(void *To,unsigned long long Size,bool AllowEof)
  56. {
  57. unsigned long long Jnk;
  58. if (AllowEof)
  59. return Read(To,Size,&Jnk);
  60. return Read(To,Size);
  61. }
  62. bool Read(void *To,unsigned long long Size,unsigned long long *Actual = 0);
  63. char* ReadLine(char *To, unsigned long long const Size);
  64. bool Write(const void *From,unsigned long long Size);
  65. bool static Write(int Fd, const void *From, unsigned long long Size);
  66. bool Seek(unsigned long long To);
  67. bool Skip(unsigned long long To);
  68. bool Truncate(unsigned long long To);
  69. unsigned long long Tell();
  70. unsigned long long Size();
  71. unsigned long long FileSize();
  72. time_t ModificationTime();
  73. /* You want to use 'unsigned long long' if you are talking about a file
  74. to be able to support large files (>2 or >4 GB) properly.
  75. This shouldn't happen all to often for the indexes, but deb's might be…
  76. And as the auto-conversation converts a 'unsigned long *' to a 'bool'
  77. instead of 'unsigned long long *' we need to provide this explicitely -
  78. otherwise applications magically start to fail… */
  79. __deprecated bool Read(void *To,unsigned long long Size,unsigned long *Actual)
  80. {
  81. unsigned long long R;
  82. bool const T = Read(To, Size, &R);
  83. *Actual = R;
  84. return T;
  85. }
  86. bool Open(std::string FileName,unsigned int const Mode,CompressMode Compress,unsigned long const Perms = 0666);
  87. bool Open(std::string FileName,unsigned int const Mode,APT::Configuration::Compressor const &compressor,unsigned long const Perms = 0666);
  88. inline bool Open(std::string const &FileName,unsigned int const Mode, unsigned long const Perms = 0666) {
  89. return Open(FileName, Mode, None, Perms);
  90. };
  91. bool OpenDescriptor(int Fd, unsigned int const Mode, CompressMode Compress, bool AutoClose=false);
  92. bool OpenDescriptor(int Fd, unsigned int const Mode, APT::Configuration::Compressor const &compressor, bool AutoClose=false);
  93. inline bool OpenDescriptor(int Fd, unsigned int const Mode, bool AutoClose=false) {
  94. return OpenDescriptor(Fd, Mode, None, AutoClose);
  95. };
  96. bool Close();
  97. bool Sync();
  98. // Simple manipulators
  99. inline int Fd() {return iFd;};
  100. inline void Fd(int fd) { OpenDescriptor(fd, ReadWrite);};
  101. __deprecated gzFile gzFd();
  102. inline bool IsOpen() {return iFd >= 0;};
  103. inline bool Failed() {return (Flags & Fail) == Fail;};
  104. inline void EraseOnFailure() {Flags |= DelOnFail;};
  105. inline void OpFail() {Flags |= Fail;};
  106. inline bool Eof() {return (Flags & HitEof) == HitEof;};
  107. inline bool IsCompressed() {return (Flags & Compressed) == Compressed;};
  108. inline std::string &Name() {return FileName;};
  109. FileFd(std::string FileName,unsigned int const Mode,unsigned long Perms = 0666) : iFd(-1), Flags(0), d(NULL)
  110. {
  111. Open(FileName,Mode, None, Perms);
  112. };
  113. FileFd(std::string FileName,unsigned int const Mode, CompressMode Compress, unsigned long Perms = 0666) : iFd(-1), Flags(0), d(NULL)
  114. {
  115. Open(FileName,Mode, Compress, Perms);
  116. };
  117. FileFd() : iFd(-1), Flags(AutoClose), d(NULL) {};
  118. FileFd(int const Fd, unsigned int const Mode = ReadWrite, CompressMode Compress = None) : iFd(-1), Flags(0), d(NULL)
  119. {
  120. OpenDescriptor(Fd, Mode, Compress);
  121. };
  122. FileFd(int const Fd, bool const AutoClose) : iFd(-1), Flags(0), d(NULL)
  123. {
  124. OpenDescriptor(Fd, ReadWrite, None, AutoClose);
  125. };
  126. virtual ~FileFd();
  127. private:
  128. FileFdPrivate* d;
  129. bool OpenInternDescriptor(unsigned int const Mode, APT::Configuration::Compressor const &compressor);
  130. // private helpers to set Fail flag and call _error->Error
  131. bool FileFdErrno(const char* Function, const char* Description,...) __like_printf(3) __cold;
  132. bool FileFdError(const char* Description,...) __like_printf(2) __cold;
  133. };
  134. bool RunScripts(const char *Cnf);
  135. bool CopyFile(FileFd &From,FileFd &To);
  136. int GetLock(std::string File,bool Errors = true);
  137. bool FileExists(std::string File);
  138. bool RealFileExists(std::string File);
  139. bool DirectoryExists(std::string const &Path) __attrib_const;
  140. bool CreateDirectory(std::string const &Parent, std::string const &Path);
  141. time_t GetModificationTime(std::string const &Path);
  142. /** \brief Ensure the existence of the given Path
  143. *
  144. * \param Parent directory of the Path directory - a trailing
  145. * /apt/ will be removed before CreateDirectory call.
  146. * \param Path which should exist after (successful) call
  147. */
  148. bool CreateAPTDirectoryIfNeeded(std::string const &Parent, std::string const &Path);
  149. std::vector<std::string> GetListOfFilesInDir(std::string const &Dir, std::string const &Ext,
  150. bool const &SortList, bool const &AllowNoExt=false);
  151. std::vector<std::string> GetListOfFilesInDir(std::string const &Dir, std::vector<std::string> const &Ext,
  152. bool const &SortList);
  153. std::vector<std::string> GetListOfFilesInDir(std::string const &Dir, bool SortList);
  154. std::string SafeGetCWD();
  155. void SetCloseExec(int Fd,bool Close);
  156. void SetNonBlock(int Fd,bool Block);
  157. bool WaitFd(int Fd,bool write = false,unsigned long timeout = 0);
  158. pid_t ExecFork();
  159. bool ExecWait(pid_t Pid,const char *Name,bool Reap = false);
  160. // File string manipulators
  161. std::string flNotDir(std::string File);
  162. std::string flNotFile(std::string File);
  163. std::string flNoLink(std::string File);
  164. std::string flExtension(std::string File);
  165. std::string flCombine(std::string Dir,std::string File);
  166. // simple c++ glob
  167. std::vector<std::string> Glob(std::string const &pattern, int flags=0);
  168. #endif