You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

110 lines
3.4 KiB

  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 <string>
  19. #include <vector>
  20. #include <zlib.h>
  21. using std::string;
  22. class FileFd
  23. {
  24. protected:
  25. int iFd;
  26. enum LocalFlags {AutoClose = (1<<0),Fail = (1<<1),DelOnFail = (1<<2),
  27. HitEof = (1<<3)};
  28. unsigned long Flags;
  29. string FileName;
  30. gzFile gz;
  31. public:
  32. enum OpenMode {ReadOnly,WriteEmpty,WriteExists,WriteAny,WriteTemp,ReadOnlyGzip};
  33. inline bool Read(void *To,unsigned long Size,bool AllowEof)
  34. {
  35. unsigned long Jnk;
  36. if (AllowEof)
  37. return Read(To,Size,&Jnk);
  38. return Read(To,Size);
  39. }
  40. bool Read(void *To,unsigned long Size,unsigned long *Actual = 0);
  41. bool Write(const void *From,unsigned long Size);
  42. bool Seek(unsigned long To);
  43. bool Skip(unsigned long To);
  44. bool Truncate(unsigned long To);
  45. unsigned long Tell();
  46. unsigned long Size();
  47. bool Open(string FileName,OpenMode Mode,unsigned long Perms = 0666);
  48. bool Close();
  49. bool Sync();
  50. // Simple manipulators
  51. inline int Fd() {return iFd;};
  52. inline void Fd(int fd) {iFd = fd;};
  53. inline bool IsOpen() {return iFd >= 0;};
  54. inline bool Failed() {return (Flags & Fail) == Fail;};
  55. inline void EraseOnFailure() {Flags |= DelOnFail;};
  56. inline void OpFail() {Flags |= Fail;};
  57. inline bool Eof() {return (Flags & HitEof) == HitEof;};
  58. inline string &Name() {return FileName;};
  59. FileFd(string FileName,OpenMode Mode,unsigned long Perms = 0666) : iFd(-1),
  60. Flags(0), gz(NULL)
  61. {
  62. Open(FileName,Mode,Perms);
  63. };
  64. FileFd(int Fd = -1) : iFd(Fd), Flags(AutoClose), gz(NULL) {};
  65. FileFd(int Fd,bool) : iFd(Fd), Flags(0), gz(NULL) {};
  66. virtual ~FileFd();
  67. };
  68. bool RunScripts(const char *Cnf);
  69. bool CopyFile(FileFd &From,FileFd &To);
  70. int GetLock(string File,bool Errors = true);
  71. bool FileExists(string File);
  72. // FIXME: next ABI-Break: merge the two method-headers
  73. std::vector<string> GetListOfFilesInDir(string const &Dir, string const &Ext,
  74. bool const &SortList);
  75. std::vector<string> GetListOfFilesInDir(string const &Dir, string const &Ext,
  76. bool const &SortList, bool const &AllowNoExt);
  77. std::vector<string> GetListOfFilesInDir(string const &Dir, std::vector<string> const &Ext,
  78. bool const &SortList);
  79. string SafeGetCWD();
  80. void SetCloseExec(int Fd,bool Close);
  81. void SetNonBlock(int Fd,bool Block);
  82. bool WaitFd(int Fd,bool write = false,unsigned long timeout = 0);
  83. pid_t ExecFork();
  84. bool ExecWait(pid_t Pid,const char *Name,bool Reap = false);
  85. // File string manipulators
  86. string flNotDir(string File);
  87. string flNotFile(string File);
  88. string flNoLink(string File);
  89. string flExtension(string File);
  90. string flCombine(string Dir,string File);
  91. #endif