fileutl.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 <string>
  20. #include <vector>
  21. #include <zlib.h>
  22. /* Define this for python-apt */
  23. #define APT_HAS_GZIP 1
  24. class FileFd
  25. {
  26. protected:
  27. int iFd;
  28. enum LocalFlags {AutoClose = (1<<0),Fail = (1<<1),DelOnFail = (1<<2),
  29. HitEof = (1<<3), Replace = (1<<4) };
  30. unsigned long Flags;
  31. std::string FileName;
  32. std::string TemporaryFileName;
  33. gzFile gz;
  34. public:
  35. enum OpenMode {ReadOnly,WriteEmpty,WriteExists,WriteAny,WriteTemp,ReadOnlyGzip,
  36. WriteAtomic};
  37. inline bool Read(void *To,unsigned long long Size,bool AllowEof)
  38. {
  39. unsigned long long Jnk;
  40. if (AllowEof)
  41. return Read(To,Size,&Jnk);
  42. return Read(To,Size);
  43. }
  44. bool Read(void *To,unsigned long long Size,unsigned long long *Actual = 0);
  45. bool Write(const void *From,unsigned long long Size);
  46. bool Seek(unsigned long long To);
  47. bool Skip(unsigned long long To);
  48. bool Truncate(unsigned long long To);
  49. unsigned long long Tell();
  50. unsigned long long Size();
  51. unsigned long long FileSize();
  52. /* You want to use 'unsigned long long' if you are talking about a file
  53. to be able to support large files (>2 or >4 GB) properly.
  54. This shouldn't happen all to often for the indexes, but deb's might be…
  55. And as the auto-conversation converts a 'unsigned long *' to a 'bool'
  56. instead of 'unsigned long long *' we need to provide this explicitely -
  57. otherwise applications magically start to fail… */
  58. __deprecated bool Read(void *To,unsigned long long Size,unsigned long *Actual)
  59. {
  60. unsigned long long R;
  61. bool const T = Read(To, Size, &R);
  62. *Actual = R;
  63. return T;
  64. }
  65. bool Open(std::string FileName,OpenMode Mode,unsigned long Perms = 0666);
  66. bool OpenDescriptor(int Fd, OpenMode Mode, bool AutoClose=false);
  67. bool Close();
  68. bool Sync();
  69. // Simple manipulators
  70. inline int Fd() {return iFd;};
  71. inline void Fd(int fd) {iFd = fd;};
  72. inline gzFile gzFd() {return gz;};
  73. inline bool IsOpen() {return iFd >= 0;};
  74. inline bool Failed() {return (Flags & Fail) == Fail;};
  75. inline void EraseOnFailure() {Flags |= DelOnFail;};
  76. inline void OpFail() {Flags |= Fail;};
  77. inline bool Eof() {return (Flags & HitEof) == HitEof;};
  78. inline std::string &Name() {return FileName;};
  79. FileFd(std::string FileName,OpenMode Mode,unsigned long Perms = 0666) : iFd(-1),
  80. Flags(0), gz(NULL)
  81. {
  82. Open(FileName,Mode,Perms);
  83. };
  84. FileFd(int Fd = -1) : iFd(Fd), Flags(AutoClose), gz(NULL) {};
  85. FileFd(int Fd,bool) : iFd(Fd), Flags(0), gz(NULL) {};
  86. virtual ~FileFd();
  87. };
  88. bool RunScripts(const char *Cnf);
  89. bool CopyFile(FileFd &From,FileFd &To);
  90. int GetLock(std::string File,bool Errors = true);
  91. bool FileExists(std::string File);
  92. bool RealFileExists(std::string File);
  93. bool DirectoryExists(std::string const &Path) __attrib_const;
  94. bool CreateDirectory(std::string const &Parent, std::string const &Path);
  95. time_t GetModificationTime(std::string const &Path);
  96. /** \brief Ensure the existence of the given Path
  97. *
  98. * \param Parent directory of the Path directory - a trailing
  99. * /apt/ will be removed before CreateDirectory call.
  100. * \param Path which should exist after (successful) call
  101. */
  102. bool CreateAPTDirectoryIfNeeded(std::string const &Parent, std::string const &Path);
  103. std::vector<std::string> GetListOfFilesInDir(std::string const &Dir, std::string const &Ext,
  104. bool const &SortList, bool const &AllowNoExt=false);
  105. std::vector<std::string> GetListOfFilesInDir(std::string const &Dir, std::vector<std::string> const &Ext,
  106. bool const &SortList);
  107. std::string SafeGetCWD();
  108. void SetCloseExec(int Fd,bool Close);
  109. void SetNonBlock(int Fd,bool Block);
  110. bool WaitFd(int Fd,bool write = false,unsigned long timeout = 0);
  111. pid_t ExecFork();
  112. bool ExecWait(pid_t Pid,const char *Name,bool Reap = false);
  113. // File string manipulators
  114. std::string flNotDir(std::string File);
  115. std::string flNotFile(std::string File);
  116. std::string flNoLink(std::string File);
  117. std::string flExtension(std::string File);
  118. std::string flCombine(std::string Dir,std::string File);
  119. #endif