fileutl.h 3.3 KB

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