fileutl.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. bool Open(string FileName,OpenMode Mode,unsigned long Perms = 0666);
  53. bool OpenDescriptor(int Fd, OpenMode Mode, bool AutoClose=false);
  54. bool Close();
  55. bool Sync();
  56. // Simple manipulators
  57. inline int Fd() {return iFd;};
  58. inline void Fd(int fd) {iFd = fd;};
  59. inline bool IsOpen() {return iFd >= 0;};
  60. inline bool Failed() {return (Flags & Fail) == Fail;};
  61. inline void EraseOnFailure() {Flags |= DelOnFail;};
  62. inline void OpFail() {Flags |= Fail;};
  63. inline bool Eof() {return (Flags & HitEof) == HitEof;};
  64. inline string &Name() {return FileName;};
  65. FileFd(string FileName,OpenMode Mode,unsigned long Perms = 0666) : iFd(-1),
  66. Flags(0), gz(NULL)
  67. {
  68. Open(FileName,Mode,Perms);
  69. };
  70. FileFd(int Fd = -1) : iFd(Fd), Flags(AutoClose), gz(NULL) {};
  71. FileFd(int Fd,bool) : iFd(Fd), Flags(0), gz(NULL) {};
  72. virtual ~FileFd();
  73. };
  74. bool RunScripts(const char *Cnf);
  75. bool CopyFile(FileFd &From,FileFd &To);
  76. int GetLock(string File,bool Errors = true);
  77. bool FileExists(string File);
  78. bool DirectoryExists(string const &Path) __attrib_const;
  79. bool CreateDirectory(string const &Parent, string const &Path);
  80. /** \brief Ensure the existence of the given Path
  81. *
  82. * \param Parent directory of the Path directory - a trailing
  83. * /apt/ will be removed before CreateDirectory call.
  84. * \param Path which should exist after (successful) call
  85. */
  86. bool CreateAPTDirectoryIfNeeded(string const &Parent, string const &Path);
  87. std::vector<string> GetListOfFilesInDir(string const &Dir, string const &Ext,
  88. bool const &SortList, bool const &AllowNoExt=false);
  89. std::vector<string> GetListOfFilesInDir(string const &Dir, std::vector<string> const &Ext,
  90. bool const &SortList);
  91. string SafeGetCWD();
  92. void SetCloseExec(int Fd,bool Close);
  93. void SetNonBlock(int Fd,bool Block);
  94. bool WaitFd(int Fd,bool write = false,unsigned long timeout = 0);
  95. pid_t ExecFork();
  96. bool ExecWait(pid_t Pid,const char *Name,bool Reap = false);
  97. // File string manipulators
  98. string flNotDir(string File);
  99. string flNotFile(string File);
  100. string flNoLink(string File);
  101. string flExtension(string File);
  102. string flCombine(string Dir,string File);
  103. #endif