fileutl.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. using std::string;
  25. class FileFd
  26. {
  27. protected:
  28. int iFd;
  29. enum LocalFlags {AutoClose = (1<<0),Fail = (1<<1),DelOnFail = (1<<2),
  30. HitEof = (1<<3), Replace = (1<<4) };
  31. unsigned long Flags;
  32. string FileName;
  33. string TemporaryFileName;
  34. gzFile gz;
  35. public:
  36. enum OpenMode {ReadOnly,WriteEmpty,WriteExists,WriteAny,WriteTemp,ReadOnlyGzip,
  37. WriteAtomic};
  38. inline bool Read(void *To,unsigned long Size,bool AllowEof)
  39. {
  40. unsigned long Jnk;
  41. if (AllowEof)
  42. return Read(To,Size,&Jnk);
  43. return Read(To,Size);
  44. }
  45. bool Read(void *To,unsigned long Size,unsigned long *Actual = 0);
  46. bool Write(const void *From,unsigned long Size);
  47. bool Seek(unsigned long To);
  48. bool Skip(unsigned long To);
  49. bool Truncate(unsigned long To);
  50. unsigned long Tell();
  51. unsigned long Size();
  52. unsigned long FileSize();
  53. bool Open(string FileName,OpenMode Mode,unsigned long Perms = 0666);
  54. bool OpenDescriptor(int Fd, OpenMode Mode, bool AutoClose=false);
  55. bool Close();
  56. bool Sync();
  57. // Simple manipulators
  58. inline int Fd() {return iFd;};
  59. inline void Fd(int fd) {iFd = fd;};
  60. inline gzFile gzFd() {return gz;};
  61. inline bool IsOpen() {return iFd >= 0;};
  62. inline bool Failed() {return (Flags & Fail) == Fail;};
  63. inline void EraseOnFailure() {Flags |= DelOnFail;};
  64. inline void OpFail() {Flags |= Fail;};
  65. inline bool Eof() {return (Flags & HitEof) == HitEof;};
  66. inline string &Name() {return FileName;};
  67. FileFd(string FileName,OpenMode Mode,unsigned long Perms = 0666) : iFd(-1),
  68. Flags(0), gz(NULL)
  69. {
  70. Open(FileName,Mode,Perms);
  71. };
  72. FileFd(int Fd = -1) : iFd(Fd), Flags(AutoClose), gz(NULL) {};
  73. FileFd(int Fd,bool) : iFd(Fd), Flags(0), gz(NULL) {};
  74. virtual ~FileFd();
  75. };
  76. bool RunScripts(const char *Cnf);
  77. bool CopyFile(FileFd &From,FileFd &To);
  78. int GetLock(string File,bool Errors = true);
  79. bool FileExists(string File);
  80. bool RealFileExists(string File);
  81. bool DirectoryExists(string const &Path) __attrib_const;
  82. bool CreateDirectory(string const &Parent, string const &Path);
  83. time_t GetModificationTime(string const &Path);
  84. /** \brief Ensure the existence of the given Path
  85. *
  86. * \param Parent directory of the Path directory - a trailing
  87. * /apt/ will be removed before CreateDirectory call.
  88. * \param Path which should exist after (successful) call
  89. */
  90. bool CreateAPTDirectoryIfNeeded(string const &Parent, string const &Path);
  91. std::vector<string> GetListOfFilesInDir(string const &Dir, string const &Ext,
  92. bool const &SortList, bool const &AllowNoExt=false);
  93. std::vector<string> GetListOfFilesInDir(string const &Dir, std::vector<string> const &Ext,
  94. bool const &SortList);
  95. string SafeGetCWD();
  96. void SetCloseExec(int Fd,bool Close);
  97. void SetNonBlock(int Fd,bool Block);
  98. bool WaitFd(int Fd,bool write = false,unsigned long timeout = 0);
  99. pid_t ExecFork();
  100. bool ExecWait(pid_t Pid,const char *Name,bool Reap = false);
  101. // File string manipulators
  102. string flNotDir(string File);
  103. string flNotFile(string File);
  104. string flNoLink(string File);
  105. string flExtension(string File);
  106. string flCombine(string Dir,string File);
  107. #endif